package com.mortals.xhx.module.device.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.service.IUser;
import com.mortals.framework.util.EntityUtil;
import com.mortals.framework.util.StringUtils;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.daemon.task.CustomerInfo;
import com.mortals.xhx.module.device.model.DeviceLogEntity;
import com.mortals.xhx.module.device.service.DeviceLogService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Map;

/**
 * 设备日志
 *
 * @author zxfei
 * @date 2022-03-09
 */
@RestController
@RequestMapping("device/log")
public class DeviceLogController extends BaseCRUDJsonMappingController<DeviceLogService, DeviceLogForm, DeviceLogEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private UserService userService;


    public DeviceLogController() {
        super.setFormClass(DeviceLogForm.class);
        super.setModuleDesc("设备日志");
    }


    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, DeviceLogForm form, Map<String, Object> model, Context context) {
        this.addDict(model, "logType", paramService.getParamBySecondOrganize("DeviceLog", "logType"));

        super.init(request, response, form, model, context);
    }

    @Override
    protected void doListBefore(HttpServletRequest request, HttpServletResponse response, DeviceLogForm form, Map<String, Object> model, Context context) throws AppException {
        form.getQuery().setOrderColList(new ArrayList<OrderCol>() {
            {
                add(new OrderCol("createTime", OrderCol.DESCENDING));
            }
        });
        super.doListBefore(request, response, form, model, context);
    }

/*
    @Override
    protected IUser getCurUser() {
        //String token = request.getParameter("token");
        String token =  authTokenService.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            try {
                Claims claims = authTokenService.parseToken(token);
                Map objMap = claims.get("userInfo", Map.class);
                CustomerInfo userInfo =new CustomerInfo();
                EntityUtil.mapToEntity(objMap,userInfo);
                if (!ObjectUtils.isEmpty(userInfo)
                        && !ObjectUtils.isEmpty(userInfo.getId())
                        && !ObjectUtils.isEmpty(userInfo.getAccount())) {
                    UserQuery userQuery = new UserQuery();
                    userQuery.setCustomerId(userInfo.getId());
                    userQuery.setLoginName(userInfo.getAccount());
                    UserEntity userEntity = userService.selectOne(userQuery);
                    if (!ObjectUtils.isEmpty(userEntity)) {
                        return new IUser() {
                            @Override
                            public Long getId() {
                                return userEntity.getId();
                            }

                            @Override
                            public Long getDeptId() {
                                return null;
                            }

                            @Override
                            public String getDeptName() {
                                return null;
                            }

                            @Override
                            public Long getCustomerId() {
                                return userEntity.getCustomerId();
                            }

                            @Override
                            public Long getSiteId() {
                                return userEntity.getSiteId();
                            }

                            @Override
                            public Long getCustomerJoinId() {
                                return null;
                            }

                            @Override
                            public String getCustomerNum() {
                                return null;
                            }

                            @Override
                            public String getLoginName() {
                                return userEntity.getLoginName();
                            }


                            @Override
                            public String getRealName() {
                                return userEntity.getRealName();
                            }

                            @Override
                            public boolean isAdmin() {
                                return userEntity.getId() == 1L;
                            }

                            @Override
                            public boolean isSystemUser() {
                                return userEntity.isSystemUser();
                            }

                            @Override
                            public boolean isManager() {
                                return userEntity.isManager();
                            }

                            @Override
                            public Integer getUserType() {
                                return userEntity.getUserType();
                            }

                            @Override
                            public String getToken() {
                                return null;
                            }


                            @Override
                            public Long getLoginTime() {
                                return userEntity.getLoginTime();
                            }

                            @Override
                            public Long getExpireTime() {
                                return userEntity.getExpireTime();
                            }

                            @Override
                            public void setExpireTime(Long expireTime) {

                            }

                            @Override
                            public String getMenuUrl() {
                                return userEntity.getMenuUrl();
                            }

                        };
                    }
                }
            } catch (Exception e) {
                log.error("解析jwt token异常！", e);
                return null;
            }
        }


        return super.getCurUser();
    }*/

}