package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.bweb.config.SysConst;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.QRCodeGenerator;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.AESEncodeUtil;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.common.utils.IDGenerator;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsGasStaff;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.CommonService;
import com.hfkj.service.gas.BsGasStaffService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/gasStaff")
@Api(value = "加油站员工管理")
public class BsGasStaffController {

    private static Logger log = LoggerFactory.getLogger(BsGasStaffController.class);

    @Resource
    private BsGasStaffService gasStaffService;
    @Resource
    private CommonService commonService;
    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/addGasStaff",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加员工")
    public ResponseData addStaff(@RequestBody JSONObject body) {
        try {
            SecUserSessionObject userInfoModel = userCenter.getSessionModel(SecUserSessionObject.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }
            if (!userInfoModel.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type2.getCode())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }
            if (StringUtils.isBlank(body.getString("name"))
                || StringUtils.isBlank(body.getString("telephone"))
                || StringUtils.isBlank(body.getString("positionType"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            BsGasStaff gasStaff = new BsGasStaff();
            gasStaff.setMerId(userInfoModel.getAccount().getObjectId());
            gasStaff.setAvatar(body.getString("avatar"));
            gasStaff.setName(body.getString("name"));
            gasStaff.setTelephone(body.getString("telephone"));
            gasStaff.setPositionType(body.getInteger("positionType"));
            gasStaff.setPositionName(commonService.getDictionaryCodeName("GAS_STAFF_POSITION_TYPE", body.getString("positionType")));
            gasStaffService.addStaff(gasStaff);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighGasStaffController -> addStaff() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/updateGasStaff",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改员工")
    public ResponseData updateGasStaff(@RequestBody JSONObject body) {
        try {
            if (body.getLong("id") == null
                    || StringUtils.isBlank(body.getString("name"))
                    || StringUtils.isBlank(body.getString("telephone"))
                    || StringUtils.isBlank(body.getString("positionType"))
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询员工信息
            BsGasStaff gasStaff = gasStaffService.getStaffDetailById(body.getLong("id"));
            if (gasStaff == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到员工信息");
            }
            gasStaff.setAvatar(body.getString("avatar"));
            gasStaff.setName(body.getString("name"));
            gasStaff.setTelephone(body.getString("telephone"));
            gasStaff.setPositionType(body.getInteger("positionType"));
            gasStaff.setPositionName(commonService.getDictionaryCodeName("GAS_STAFF_POSITION_TYPE", body.getString("positionType")));
            gasStaffService.updateStaff(gasStaff);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighGasStaffController -> updateGasStaff() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/queryQrCode",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询二维码")
    public ResponseData queryQrCode(@RequestParam(name = "staffId", required = true) Long staffId) {
        try {
            // 查询员工信息
            BsGasStaff gasStaff = gasStaffService.getStaffDetailById(staffId);
            if (gasStaff == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到员工信息");
            }

            if (StringUtils.isBlank(gasStaff.getQrCodeImgUrl())) {
                // 员工ID签名
                String signId = AESEncodeUtil.aesEncrypt(gasStaff.getId().toString());

                // 生成二维码
                String param = "/hsgH5?accountId=0000021";
                String qrCodeImgUrl = DateUtil.date2String(new Date(),"yyyyMMddHHmmss") + IDGenerator.nextId(1) +".jpg";
                QRCodeGenerator.generateQRCodeImage(param, 350, 350, SysConst.getSysConfig().getFileUrl()+"/gasStaff/"+qrCodeImgUrl);

                gasStaff.setQrCodeImgUrl(qrCodeImgUrl);
                gasStaffService.updateStaff(gasStaff);
            }

            return ResponseMsgUtil.success(gasStaff.getQrCodeImgUrl());

        } catch (Exception e) {
            log.error("HighGasStaffController -> queryQrCode() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/disabled",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "禁用员工")
    public ResponseData disabled(@RequestBody JSONObject body) {
        try {
            if (body.getLong("staffId") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            gasStaffService.disabled(body.getLong("staffId"));

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighGasStaffController -> disabled() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/recover",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "恢复正常")
    public ResponseData recover(@RequestBody JSONObject body) {
        try {
            if (body.getLong("staffId") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            gasStaffService.recover(body.getLong("staffId"));

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighGasStaffController -> delStaff() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/delStaff",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除员工")
    public ResponseData delStaff(@RequestBody JSONObject body) {
        try {

            if (body.getLong("staffId") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            gasStaffService.delStaff(body.getLong("staffId"));
            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighGasStaffController -> delStaff() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getStaffDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询员工详情")
    public ResponseData getStaffDetail(@RequestParam(name = "staffId", required = true) Long staffId) {
        try {

            return ResponseMsgUtil.success(gasStaffService.getStaffDetailById(staffId));

        } catch (Exception e) {
            log.error("HighGasStaffController -> getStaffDetail() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value="/getStaffList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询员工列表")
    public ResponseData getStaffList(@RequestParam(name = "merId", required = false) Long merId,
                                     @RequestParam(name = "name", required = false) String name,
                                     @RequestParam(name = "telephone", required = false) String telephone,
                                     @RequestParam(name = "positionType", required = false) Integer positionType,
                                     @RequestParam(name = "status", required = false) Integer status,
                                     @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                     @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            SecUserSessionObject userInfoModel = userCenter.getSessionModel(SecUserSessionObject.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }
            Map<String,Object> param = new HashMap<>();
            if (userInfoModel.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type1.getCode())) {
                param.put("merId", merId);
            } else if (userInfoModel.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type2.getCode())) {
                param.put("merId", userInfoModel.getAccount().getObjectId());
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }
            param.put("name", name);
            param.put("telephone", telephone);
            param.put("positionType", positionType);
            param.put("status", status);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(gasStaffService.getStaffList(param)));

        } catch (Exception e) {
            log.error("HighGasStaffController -> getStaffList() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
