package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.AESEncodeUtil;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighCompanyTwoPwd;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighCompanyTwoPwdService;
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.dao.DeadlockLoserDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Controller
@RequestMapping(value = "/companyTwoPwd")
@Api(value = "代理商接口")
public class HighCompanyTwoPwdController {

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

    @Resource
    private HighCompanyTwoPwdService companyTwoPwdService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/setTwoPwd", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "设置二级密码")
    public ResponseData setTwoPwd(@RequestBody JSONObject body) {
        try {

            if (StringUtils.isBlank(body.getString("password"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel.getBsOrganization() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            if (companyTwoPwdService.getTwoPwdByOrg(userInfoModel.getBsOrganization().getId()) != null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "二级密码已设置，请勿重复设置");
            }

            HighCompanyTwoPwd twoPwd = new HighCompanyTwoPwd();
            twoPwd.setCompanyId(userInfoModel.getBsCompany().getId());
            twoPwd.setOrgId(userInfoModel.getBsOrganization().getId());
            twoPwd.setPassword(AESEncodeUtil.aesEncrypt(body.getString("password")));
            companyTwoPwdService.editData(twoPwd);

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

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

    @RequestMapping(value = "/updateTwoPwd", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改二级密码")
    public ResponseData updateTwoPwd(@RequestBody JSONObject body) {
        try {

            if (StringUtils.isBlank(body.getString("oldPassword"))
                    || StringUtils.isBlank(body.getString("newPassword"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel.getBsOrganization() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            // 二级密码
            HighCompanyTwoPwd twoPwd = companyTwoPwdService.getTwoPwdByOrg(userInfoModel.getBsOrganization().getId());
            if (twoPwd == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "修改密码失败，未设置二级密码");
            }
            if (!AESEncodeUtil.aesEncrypt(body.getString("oldPassword")).equals(twoPwd.getPassword())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "旧密码错误");
            }

            twoPwd.setPassword(AESEncodeUtil.aesEncrypt(body.getString("newPassword")));
            companyTwoPwdService.editData(twoPwd);

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

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

    @RequestMapping(value = "/isSetTwoPwd", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "是否设置二级码")
    public ResponseData isSetTwoPwd() {
        try {
            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel.getBsOrganization() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            HighCompanyTwoPwd twoPwd = companyTwoPwdService.getTwoPwdByOrg(userInfoModel.getBsOrganization().getId());
            if (twoPwd == null) {
                return ResponseMsgUtil.success(false);
            }
            return ResponseMsgUtil.success(true);

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

}
