package com.bonus.canteen.core.customer.controller;

import com.bonus.canteen.core.common.base.BaseController;
import com.bonus.canteen.core.common.utils.HeaderFetchUtil;
import com.bonus.canteen.core.customer.dto.*;
import com.bonus.canteen.core.customer.vo.PageCustInfoPhotoVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.houqin.constant.GlobalConstants;
import com.bonus.common.houqin.constant.SourceTypeEnum;
import com.bonus.canteen.core.customer.model.CustInfo;
import com.bonus.canteen.core.customer.vo.CustInfoVo;
import com.bonus.common.houqin.utils.SM4EncryptUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.bonus.canteen.core.customer.service.CustInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.bonus.canteen.core.common.encrypt.SM4EncryptUtils.sm4Encrypt;
import static com.bonus.common.core.utils.PageUtils.startPage;
import static com.bonus.common.core.web.domain.AjaxResult.success;
import static com.bonus.canteen.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(value = "当前系统时间戳")
    @PostMapping({"/get-yst-timestamp"})
    public Long getYstTimeStamp() {
        Long timeStamp = System.currentTimeMillis();
        log.info("当前系统时间戳:" + timeStamp);
        return timeStamp;
    }

    @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();
        if (StringUtils.isNotEmpty(custInfo.getMobile())) {
            custInfo.setMobile(sm4Encrypt(custInfo.getMobile()));
        }
        CustInfoVo result = this.custInfoService.queryCustInfoDetail(custInfo, sourceType);
        //System.out.println(sm4Decrypt("29G1AUHDmLvPe1tt0t12Dw==##encrypted"));
        result.setCustName(sm4Decrypt(result.getCustName()));
        result.setMobile(Sm4Utils.encrypt(sm4Decrypt(result.getMobile())));
        result.setEmail(Sm4Utils.encrypt(sm4Decrypt(result.getEmail())));
        result.setIdCard(Sm4Utils.encrypt(sm4Decrypt(result.getIdCard())));
        ar.put("data", result);
        return ar;
    }

    @ApiOperation("分页查询人员及生物识别特征")
    @GetMapping({"/pageCustInfoPhoto"})
    public TableDataInfo pageCustInfoPhoto(@RequestHeader Map<String, String> requestHeader, @Valid CustInfoParam dto) {
        if (!GlobalConstants.CANTEEN_YIZHAN.equals(SM4EncryptUtils.sm4Decrypt(HeaderFetchUtil.getSign(requestHeader)))) {
            throw new ServiceException("访问缺少认证信息");
        }
        try {
            startPage();
            List<PageCustInfoPhotoVO> list = this.custInfoService.pageCustInfoPhoto(dto);
            return getDataTable(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return getDataTable(null);
        }
    }
}
