package org.dromara.mp.controller;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.log.enums.OperatorType;
import org.dromara.common.web.core.BaseController;
import org.dromara.mp.domain.bo.UserWorkerBo;
import org.dromara.mp.domain.vo.UserWorkerVo;
import org.dromara.mp.service.ICardIdService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author sunzexing
 * @version 1.0
 * @title CardIdController
 * @description 身份证信息
 * @create 2024-04-01 17:26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cardId")
@Slf4j
public class CardIdController extends BaseController {


    @Resource
    public ICardIdService cardIdService;

//    @Log(title = "身份证识别", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Log(title = "身份证识别", businessType = BusinessType.INSERT,operatorType= OperatorType.MOBILE)
    public R<Map> idCardOcr(@RequestPart("file") MultipartFile file, String type,String code ){
        if (ObjectUtil.isNull(file)|| file.isEmpty()) {
            return R.fail("上传文件不能为空");
        }
        Map reultMap = null;
        try{
            reultMap = this.cardIdService.idCardOcr(file,type,code);
        }catch(Exception e){
            e.printStackTrace();
            log.error("身份证识别错误===>"+e.getMessage());
            return R.fail(e.getMessage());
        }
        return R.ok(reultMap);
    }

    /**
     * 银行卡识别上传接口
     * 使用OCR技术对上传的银行卡图片进行识别，并返回识别结果。
     * @param file 上传的银行卡图片文件，必须是multipart/form-data类型。
     * @param code 业务代码或标识，可用于特定的业务逻辑处理。
     * @return 返回一个包含识别结果的Map对象，如果失败则返回错误信息。
     */
    @PostMapping(value = "/bankUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Log(title = "银行卡识别上传接口", businessType = BusinessType.INSERT,operatorType= OperatorType.MOBILE)
    public R<Map> bankUploadOcr(@RequestPart("file") MultipartFile file, String code) {
        // 检查上传的文件是否为空
        if (ObjectUtil.isNull(file) || file.isEmpty()) {
            return R.fail("上传文件不能为空");
        }
        Map<String, Object> reultMap = null;
        try {
            // 调用服务进行银行卡识别处理
            reultMap = this.cardIdService.bankUploadOcr(file, null, code);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("银行卡识别错误===>"+e.getMessage());
            return R.fail("银行卡识别错误");
        }
        // 返回识别结果
        return R.ok(reultMap);
    }




    /**
     * @Param
     * @Return
     * @Author sunzexing
     * @Date 2024-04-08 10:46
     * 说明：身份证识别信息提交
     **/
    @PostMapping(value = "/authSubmit")
    @Log(title = "身份证识别信息提交", businessType = BusinessType.INSERT,operatorType= OperatorType.MOBILE)
    public R<Map> submitIdCardInfo(@Validated(AddGroup.class)  UserWorkerBo userWorkerBo, String code)throws  Exception{
        Map resultMap = this.cardIdService.submitIdCardInfo(userWorkerBo,code);
        return R.ok(resultMap);
    }





    /**
     * 提交银行认证信息
     * @param cardNo 银行卡号
     * @param code 验证码
     * @param bankImg 银行卡图片
     * @param bankInfo 银行信息
     * @param cardType 卡类型
     * @param cardName 卡名称
     * @param phone 手机号
     * @param channelName  通道名称
     * @return 返回认证结果，包含认证状态和相关信息
     * @throws Exception 处理过程中可能抛出的异常
     */
    @PostMapping(value = "/bankAuth")
    @Log(title = "提交银行认证信息", businessType = BusinessType.INSERT,operatorType= OperatorType.MOBILE)
    public R<Map> submitBank(String cardNo,String code,String bankImg,
                             String bankInfo,String cardType,String cardName,String phone,String channelName) throws Exception{

        Map resultMap = new HashMap();
         resultMap = this.cardIdService.bankAuth4(cardNo,code,bankImg,bankInfo,cardType,cardName,phone,channelName);
        return R.ok(resultMap);
    }




    /**
     * 修改银行卡信息
     * @param cardNo 银行卡号
     * @param code 验证码
     * @param bankImg 银行卡图片
     * @param bankInfo 银行信息
     * @param cardType 卡类型
     * @param cardName 卡名称
     * @param phone 手机号
     * @param id  银行卡ID
     * @return 返回认证结果，包含认证状态和相关信息
     * @throws Exception 处理过程中可能抛出的异常
     */
    @PostMapping(value = "/mdyBank")
    @Log(title = "修改银行卡信息", businessType = BusinessType.UPDATE,operatorType= OperatorType.MOBILE)
    public R<Boolean> mdyBank(String cardNo,String code,String bankImg,
                             String bankInfo,String cardType,String cardName,String phone,Long id) throws Exception{
        return R.ok(this.cardIdService.mdyBank(cardNo,code,bankImg,bankInfo,cardType,cardName,phone,id));
    }


    /***
     * 获取用户信息
     * @return
     */
    @GetMapping(value = "/getInfo")
    @Log(title = "获取用户信息", businessType = BusinessType.SELECT,operatorType= OperatorType.MOBILE)
    public R<Map> getUserInfo(){
        return R.ok(this.cardIdService.getUserInfo());
    }




    @GetMapping(value = "/getCardInfo")
    @Log(title = "获取身份信息", businessType = BusinessType.SELECT,operatorType= OperatorType.MOBILE)
    public R<Map> getUserCardInfo(){
        return R.ok(this.cardIdService.getUserCardInfo());
    }



    /****
     * 获取人身核验地址
     * @param code
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getFa")
    @Log(title = "获取人身核验地址", businessType = BusinessType.SELECT,operatorType= OperatorType.MOBILE)
    public R<Map> getFaceAuth(String code)throws Exception{
        return R.ok(this.cardIdService.getFaceAuth(code));
    }


    /****
     * 获取人脸核验结果
     * @param bizToken 人脸核验token
     * @return
     * @throws Exception
     */

    @GetMapping(value = "/getFaResult")
    @Log(title = "获取人脸核验结果", businessType = BusinessType.SELECT,operatorType= OperatorType.MOBILE)
    public R<Boolean> getFaceAuthResult(String bizToken,String code)throws Exception{
        return R.ok(this.cardIdService.getFaceAuthResult(bizToken, code));
    }



}
