package cn.hanghui.outapi.platform.wuxiairport.service.impl;

import cn.hanghui.outapi.platform.wuxiairport.constant.ExternalInfo;
import cn.hanghui.outapi.platform.wuxiairport.entity.dto.PushPersonInfoDto;
import cn.hanghui.outapi.platform.wuxiairport.entity.po.ExternalInfConfig;
import cn.hanghui.outapi.platform.wuxiairport.mapper.ExternalInfConfigMapper;
import cn.hanghui.outapi.platform.wuxiairport.service.PersonInfoService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor(onConstructor_ = @Autowired)
public class PersonInfoServiceImpl implements PersonInfoService {

    private final ExternalInfConfigMapper externalInfConfigMapper;

    /**
     * 自助办证机人员信息推送
     *
     * @param pushPersonInfoDto PushPersonInfoDto
     */
    @Override
    public void pushPersonInfo(PushPersonInfoDto pushPersonInfoDto) {
        ExternalInfConfig externalInfConfig = externalInfConfigMapper.selectOne(Wrappers.lambdaQuery(ExternalInfConfig.class)
                .eq(ExternalInfConfig::getInfName, ExternalInfo.PUSH_PERSON_INFO.infName));
        if (ObjectUtil.isNotEmpty(externalInfConfig)) {
            SM2 sm2 = new SM2(null, externalInfConfig.getPublicKey());
            if (ObjectUtil.isNotEmpty(pushPersonInfoDto.getXm())) {
                pushPersonInfoDto.setXm(sm2.encryptHex(pushPersonInfoDto.getXm(), KeyType.PublicKey));
            }
            if (ObjectUtil.isNotEmpty(pushPersonInfoDto.getPhone())) {
                pushPersonInfoDto.setPhone(sm2.encryptHex(pushPersonInfoDto.getPhone(), KeyType.PublicKey));
            }
            if (ObjectUtil.isNotEmpty(pushPersonInfoDto.getSfzh())) {
                pushPersonInfoDto.setSfzh(sm2.encryptHex(pushPersonInfoDto.getSfzh(), KeyType.PublicKey));
            }
            HttpUtil.post(externalInfConfig.getUrl(), JSON.toJSONString(pushPersonInfoDto));
        }
    }
}
