package org.dromara.zhishu.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import org.dromara.zhishu.domain.CardResult;
import org.dromara.zhishu.domain.LicenseResult;
import org.dromara.zhishu.domain.bo.NewUserBo;
import org.dromara.zhishu.service.ITDepotService;
import org.dromara.zhishu.service.NewUserService;
import org.dromara.zhishu.util.OcrUtil;
import org.dromara.zhishu.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import com.tencentcloudapi.common.AbstractModel;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;


@Validated
@RequiredArgsConstructor
@RestController
    @RequestMapping("/system/newUser")
public class NewUserController extends BaseController {

    @Autowired
    private final NewUserService newUserService;

    @Autowired
    private OcrUtil ocrUtil;




    @SaIgnore
//@SaCheckPermission("system:user:newuser")
@Log(title = "申请入驻", businessType = BusinessType.INSERT)
@RepeatSubmit()
@PostMapping
public R<Void> add(@Validated(AddGroup.class) @RequestBody NewUserBo newUserBo) {
    return toAjax(newUserService.insertByBo(newUserBo));
}


//身份证验证
@PostMapping("/cardRedirect")
public R<CardResult> cardRedirect(@RequestParam("file") MultipartFile file) {
    CardResult cardResult = new CardResult();
     try {
         OcrClient client = ocrUtil.upload(file);
         // 实例化一个请求对象,每个接口都会对应一个request对象
        IDCardOCRRequest req = new IDCardOCRRequest();
         //        base64图片字节数组
         req.setEnableDateVerify(true);
         req.setImageBase64(Base64.getEncoder().encodeToString(file.getBytes()));
        // 返回的resp是一个IDCardOCRResponse的实例，与请求对象对应
        IDCardOCRResponse resp = client.IDCardOCR(req);
        cardResult.setName(resp.getName());
        cardResult.setSex(resp.getSex());
        cardResult.setNation(resp.getNation());
        cardResult.setAddress(resp.getAddress());
        cardResult.setBirth(resp.getBirth());
        cardResult.setIdNum(resp.getIdNum());
        cardResult.setAuthority(resp.getAuthority());
        cardResult.setValidDate(resp.getValidDate());
        cardResult.setUrl(UploadUtil.upload(file,"user"));
     } catch (TencentCloudSDKException e) {
        cardResult.setMessage(e.getMessage());
     } catch (IOException e) {
         throw new RuntimeException(e);
     }
    return R.ok(cardResult);
}

//营业执照验证
@PostMapping("/licenseRedirect")
public R<LicenseResult> licenseRedirect(@RequestParam("file") MultipartFile file) {
    LicenseResult licenseResult = new LicenseResult();
   try {
       OcrClient client = ocrUtil.upload(file);
      // 实例化一个请求对象,每个接口都会对应一个request对象
        BizLicenseOCRRequest req = new BizLicenseOCRRequest();
       req.setImageBase64(Base64.getEncoder().encodeToString(file.getBytes()));
      // 返回的resp是一个BizLicenseOCRResponse的实例，与请求对象对应
        BizLicenseOCRResponse resp = client.BizLicenseOCR(req);
        licenseResult.setAddress(resp.getAddress());
        licenseResult.setBusiness(resp.getBusiness());
        licenseResult.setCapital(resp.getCapital());
        licenseResult.setName(resp.getName());
        String[] period = resp.getPeriod().split("至");
        if(period[1].contains("长期")||period[1].contains("永久")){
            period[1]=null;
        }
       licenseResult.setPeriod(period[1]);
        licenseResult.setRegistrationAuthority(resp.getRegistrationAuthority());
        licenseResult.setRegNum(resp.getRegNum());
        licenseResult.setPerson(resp.getPerson());
        licenseResult.setUrl(UploadUtil.upload(file,"user"));
   } catch (TencentCloudSDKException e) {
       licenseResult.setMessage(e.getMessage());
   } catch (IOException e) {
       throw new RuntimeException(e);
   }
    return R.ok(licenseResult);
}
}
