package org.jeecg.modules.xcx.auth.service.impl;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.TencentUtils;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.clockinauth.entity.ClockinAuth;
import org.jeecg.modules.clockinauth.service.IClockinAuthService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.xcx.auth.req.ApplyAuthReq;
import org.jeecg.modules.xcx.auth.service.IAuthService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
@Slf4j
@Service
public class AuthServiceImpl implements IAuthService {

    @Resource
    private IClockinAuthService clockinAuthService;
    @Resource
    private ShiroRealm shiroRealm;
    @Value("${tencent.cloud.secretId}")
    private String secretId;
    @Value("${tencent.cloud.secretKey}")
    private String secretKey;
    @Value("${tencent.cloud.endpoint}")
    private String endpoint;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> applyAuth(ApplyAuthReq applyAuthReq) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(applyAuthReq.getToken());
        if(StringUtils.isBlank(applyAuthReq.getName())){
            throw new JeecgBootException("请填写真实姓名");
        }
        if(StringUtils.isBlank(applyAuthReq.getCardNo())){
            throw new JeecgBootException("请填写身份证");
        }
        ClockinAuth clockinAuth1 = clockinAuthService.lambdaQuery()
                .eq(ClockinAuth::getDelFlag, 0)
                .eq(ClockinAuth::getUserId, hanHaiMember.getId())
                .in(ClockinAuth::getStatus, 0, 1)
                .one();

        if(clockinAuth1 != null){
            throw new JeecgBootException("已提交过实名认证，请勿重复提交");
        }

        ClockinAuth clockinAuth = new ClockinAuth();
        clockinAuth.setUserId(hanHaiMember.getId());
        clockinAuth.setName(applyAuthReq.getName());
        clockinAuth.setCardNo(applyAuthReq.getCardNo());
        log.error("图片地址:"+applyAuthReq.getPic());
        clockinAuth.setPic(applyAuthReq.getPic());
        clockinAuth.setStatus(1);
        IdCardVerificationRequest req = new IdCardVerificationRequest();
        req.setName(clockinAuth.getName());
        req.setIdCard(clockinAuth.getCardNo());
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            FaceidClient faceidClient = new FaceidClient(cred,"",clientProfile);
            IdCardVerificationResponse resp = faceidClient.IdCardVerification(req);
            if(!StringUtils.equals(resp.getResult(),"0")){
                throw new JeecgBootException(resp.getDescription());
            }
        }catch (TencentCloudSDKException e){
            log.error(e.toString());
            throw new JeecgBootException("实名制失败");
        }

        clockinAuthService.save(clockinAuth);


        TencentUtils.createPerson(clockinAuth);
        return Result.OK();
    }

    @Override
    public Result<?> authInfo(String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        ClockinAuth clockinAuth = clockinAuthService.lambdaQuery()
                .eq(ClockinAuth::getDelFlag, 0)
                .eq(ClockinAuth::getUserId, hanHaiMember.getId())
                .one();
        return Result.OK(clockinAuth);
    }
}
