package com.bonus.core.customer.controller;

import com.bonus.core.common.base.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.constant.SourceTypeEnum;
import com.bonus.core.customer.dto.CustBindMobileDTO;
import com.bonus.core.customer.dto.CustQueryDTO;
import com.bonus.core.customer.model.CustInfo;
import com.bonus.core.customer.vo.CustInfoVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.bonus.core.customer.dto.CustChangePasswordDTO;
import com.bonus.core.customer.dto.CustForgetPasswordDTO;
import com.bonus.core.customer.service.CustInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

import static com.bonus.common.core.web.domain.AjaxResult.success;
import static com.bonus.core.common.encrypt.SM4EncryptUtils.sm4Decrypt;

@RestController
@RequestMapping("/custInfo")
@Slf4j
public class CustInfoController extends BaseController {

    @Autowired
    CustInfoService custInfoService;

    @ApiOperation("小程序修改密码")
    @PostMapping({"/change/password"})
    public AjaxResult changePassword(@Validated @RequestBody CustChangePasswordDTO content) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = this.custInfoService.custChangePassword(content);
            return ajaxResult;
        } catch (Exception e) {
            log.error("小程序修改密码异常", e);
            return AjaxResult.error("小程序修改密码异常");
        }
    }

    @ApiOperation("忘记密码")
    @PostMapping({"/forget/password"})
    public AjaxResult forgetPassword(@Validated @RequestBody CustForgetPasswordDTO content) {
        //content.setMobile(AesEncryptUtil.aesDecode(content.getMobile()));
        //content.setNewPassword(AesEncryptUtil.aesDecode(content.getNewPassword()));
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = this.custInfoService.custForgetPassword(content);
            return ajaxResult;
        } catch (Exception e) {
            log.error("忘记密码异常", e);
            return AjaxResult.error("忘记密码异常");
        }
    }

    @ApiOperation("重新绑定手机号")
    @PostMapping({"/rebind/mobile"})
    public AjaxResult rebindMobile(@RequestHeader Map<String, String> headers, @Validated @RequestBody CustBindMobileDTO content) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            Integer sourceType = this.getSourceTypeByHeaders(headers);
            ajaxResult = this.custInfoService.rebindMobile(content, sourceType);
            return ajaxResult;
        } catch (Exception e) {
            log.error("忘记密码异常", e);
            return AjaxResult.error("忘记密码异常");
        }
    }

    @ApiOperation(value = "获取支付二维码")
    @PostMapping(value = "/getOrderQRCode")
    public AjaxResult getOrderQRCode(@RequestBody CustQueryDTO custQueryDTO) {
        return success(custInfoService.getOrderQRCode(SourceTypeEnum.WECHAT_SMALL_PRO.getKey(), String.valueOf(custQueryDTO.getCustId())));
    }

    @ApiOperation("小程序查询人员详细资料")
    @PostMapping({"/queryCustInfoDetailForApp"})
    public AjaxResult queryCustInfoDetailForApp(@RequestHeader Map<String, String> headers, @RequestBody CustInfo custInfo) {
        log.info("[人员资料]小程序查询人员详细资料_开始");
        Integer sourceType = this.getSourceTypeByHeaders(headers);
        sourceType = Objects.isNull(sourceType) ? SourceTypeEnum.HT_H5_MOBILE.getKey() : sourceType;
        AjaxResult ar = new AjaxResult();
        CustInfoVo result = this.custInfoService.queryCustInfoDetail(custInfo, sourceType);
        //System.out.println(sm4Decrypt("29G1AUHDmLvPe1tt0t12Dw==##encrypted"));
        result.setMobile(sm4Decrypt(result.getMobile()));
        result.setCustName(sm4Decrypt(result.getCustName()));
        result.setIdCard(sm4Decrypt(result.getIdCard()));
        ar.put("data", result);
        return ar;
    }

}
