package com.ghy.web.controller.tool;

import cn.hutool.json.JSONUtil;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.utils.StringUtils;
import com.ghy.web.pojo.vo.CertNoTwoElementReq;
import com.ghy.web.service.AliCloudService;
import com.ghy.worker.domain.Worker;
import com.ghy.worker.service.WorkerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Tag(name = "阿里云Api接口")
@Controller
@RequestMapping("/tool/ali")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AliCloudController {

    private final AliCloudService aliCloudService;

    private final WorkerService workerService;

    @Operation(summary = "身份证二要素校验")
    @PostMapping("/certNoTwoElementVerification")
    @ResponseBody
    public AjaxResult certNoTwoElementVerification(@RequestBody CertNoTwoElementReq certNoTwoElementReq){
        try {
            Worker worker = workerService.selectById(certNoTwoElementReq.getWorkerId());
            if(StringUtils.isNotEmpty(worker.getRemark())){
                return AjaxResult.success("操作成功!");
            }else {
                aliCloudService.certNoTwoElementVerification(certNoTwoElementReq);
                worker.setRemark(JSONUtil.toJsonStr(certNoTwoElementReq));
                workerService.updateWorker(worker);
                return AjaxResult.success();
            }
        }catch (Exception e){
            log.error("身份证二要素校验失败:{}", e.getMessage(), e);
            return AjaxResult.success(e.getMessage(), "身份证二要素校验失败!");
        }
    }

}
