package com.inkasso.factoring.electronicSignature.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.adapter.util.CollectionUtil;
import com.inkasso.auth.exception.ValidatorException;
import com.inkasso.factoring.acceptanceManagement.entity.CAcceptanceSignConfirmationElectronicInfo;
import com.inkasso.factoring.acceptanceManagement.service.ICAcceptanceSignConfirmationElectronicInfoService;
import com.inkasso.factoring.common.util.ElectronicSignatureUtil;
import com.inkasso.factoring.electronicSignature.dao.ElectronicSignaturePersonalMapper;
import com.inkasso.factoring.electronicSignature.entity.ElectronicSignaturePersonal;
import com.inkasso.factoring.electronicSignature.service.IElectronicSignaturePersonalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * --电子签个人信息表 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-05-13
 */
@Service
public class ElectronicSignaturePersonalServiceImpl extends ServiceImpl<ElectronicSignaturePersonalMapper, ElectronicSignaturePersonal>
        implements IElectronicSignaturePersonalService {

    @Autowired
    private ElectronicSignaturePersonalMapper  electronicSignaturePersonalMapper;

    @Autowired
    private ICAcceptanceSignConfirmationElectronicInfoService acceptanceSignConfirmationElectronicInfoService;


    @Override
    public String getAuthDeatil(String customerId) {
        ElectronicSignaturePersonal electronicSignaturePersonal = lambdaQuery().eq(ElectronicSignaturePersonal::getFddCustomerId, customerId).one();
        if (electronicSignaturePersonal == null) {
            throw new ValidatorException(CodeUtil.FAIL,"不存在该认证信息！");
        }
        String result = ElectronicSignatureUtil.getAuthInfoDetail(electronicSignaturePersonal.getFddCustomerId(), "1");
        return result;
    }






    @Override
    public List<ElectronicSignaturePersonal> findByIdForC(Long id) {
        List<ElectronicSignaturePersonal> list ;
        if(id != null){
            list = lambdaQuery().eq(ElectronicSignaturePersonal::getId, id).list();
        }else {
            list = list();
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateInfo(ElectronicSignaturePersonal electronicSignaturePersonal) {
        ElectronicSignaturePersonal old = lambdaQuery().eq(ElectronicSignaturePersonal::getName, electronicSignaturePersonal.getName())
            .eq(ElectronicSignaturePersonal::getMobile, electronicSignaturePersonal.getMobile()).one();
        if(old!=null && !old.getId().equals(electronicSignaturePersonal.getId())){
            throw new ValidatorException(CodeUtil.FAIL,"已经存在相同名称，相同电话的个人信息!");
        }
        ElectronicSignaturePersonal oldInfo = getById(electronicSignaturePersonal.getId());
        if (oldInfo == null) {
            throw new ValidatorException(CodeUtil.FAIL, "要修改的数据不存在！");
        } else {
            if (oldInfo.getAuthStatus().equals(1)) {
                throw new ValidatorException(CodeUtil.FAIL, "已认证，不能修改！");
            } else if (oldInfo.getAuthStatus().equals(0)) {
                electronicSignaturePersonal = getAuthInfo(electronicSignaturePersonal);
                updateById(electronicSignaturePersonal);
            } else {
                throw new ValidatorException(CodeUtil.FAIL, "数据状态错误！");
            }
        }
    }

    @Override
    public ElectronicSignaturePersonal getAuthInfo(ElectronicSignaturePersonal electronicSignaturePersonal) {
        String openId = UUID.randomUUID().toString().trim().replaceAll("-", "").toUpperCase();
        //法大大认证
        String name = electronicSignaturePersonal.getName();
        String mobile = electronicSignaturePersonal.getMobile();
        String customerId = ElectronicSignatureUtil.registerAccount(openId, "1");
        String authResult = ElectronicSignatureUtil.getAuthUrlPersonalFace(customerId,"1",name,mobile);
        JSONObject authJson = (JSONObject) JSONObject.parse(authResult);
        String authUrl = authJson.getString("data");
        if (authUrl == null || customerId == null) {
            throw new ValidatorException(CodeUtil.FAIL, "生成认证链接出错");
        }
        String shortUrl = ElectronicSignatureUtil.shortUrl(null, authUrl);
        electronicSignaturePersonal.setAuthStatus(0);
        electronicSignaturePersonal.setOpenId(openId);
        electronicSignaturePersonal.setAuthUrl(authUrl);
        electronicSignaturePersonal.setShortAuthUrl(shortUrl);
        electronicSignaturePersonal.setFddCustomerId(customerId);
        return electronicSignaturePersonal;
    }


    @Override
    public void flushUrl(Long id) {
        List<CAcceptanceSignConfirmationElectronicInfo> acceptanceSignConfirmationElectronicInfos = acceptanceSignConfirmationElectronicInfoService.lambdaQuery().eq(CAcceptanceSignConfirmationElectronicInfo::getElectronicSignaturePersonalId, id).eq(CAcceptanceSignConfirmationElectronicInfo::getSignStatus, 0).list();
        //List<SignConfirmationElectronicInfo> signConfirmationElectronicInfos = signConfirmationElectronicInfoService.lambdaQuery().eq(SignConfirmationElectronicInfo::getElectronicSignaturePersonalId, id).eq(SignConfirmationElectronicInfo::getSignStatus, 0).list();
        if(CollectionUtil.isNotEmpty(acceptanceSignConfirmationElectronicInfos)){
            throw new ValidatorException(CodeUtil.FAIL,"该签署人有未签署的验收单签署确认信息，不能重新刷新认证链接！");
        }
//        if(CollectionUtil.isNotEmpty(signConfirmationElectronicInfos)){
//            throw new ValidatorException(CodeUtil.FAIL,"该签署人有未签署的签署确认信息，不能重新刷新认证链接！");
//        }
        ElectronicSignaturePersonal electronicSignaturePersonal = getById(id);
        electronicSignaturePersonal = getAuthInfo(electronicSignaturePersonal);
        updateById(electronicSignaturePersonal);
    }

    @Override
    public void delete(Long id) {
        ElectronicSignaturePersonal electronicSignaturePersonal = getById(id);
        if(electronicSignaturePersonal.getAuthStatus().equals(1)){
            throw new ValidatorException(CodeUtil.FAIL,"已认证不支持删除！");
        }else {
            removeById(id);
        }
    }

}
