package com.mortals.xhx.module.idCard.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.xhx.busiz.rsp.IdCardResp;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.idCard.model.IdCardCons;
import com.mortals.xhx.module.idCard.model.XzxIdCard;
import com.mortals.xhx.module.idCard.service.IdCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;

/**
 * 新中新身份证实现类
 *
 * @author:
 * @date: 2022/12/26 14:09
 */
@Service
@Slf4j
@ConditionalOnProperty(prefix = "config", name = "idcard", havingValue = "xzx")
public class XZXIdCardServiceImpl implements IdCardService {
    @Value("${config.idcard.xzxUrl:http://localhost:8989/api/ReadMsg}")
    private String url;

    @Override
    public IdCardResp readIdCardMessage(IdCardCons cons) {
        HashMap<String, String> params = new HashMap<>();
        if (cons.getNoPhoto() != null) {
            params.put("NoPhoto", cons.getNoPhoto());
        }
        if (cons.getReadOnce() != null) {
            params.put("readOnce", cons.getReadOnce());
        }
        if (cons.getWaitTime() != null) {
            params.put("waitTime", cons.getWaitTime());
        }
        if (cons.getCallback() != null) {
            params.put("callback ", cons.getCallback());
        }
        String queryParams = HttpUtil.toParams(params);
        String readUrl = url;
        if (!ObjectUtils.isEmpty(readUrl)) {
            readUrl = url + "?" + queryParams;
        }
        log.info("readUrl={}", readUrl);
        String resp = HttpUtil.get(readUrl);
        XzxIdCard idCard = JSON.parseObject(resp, XzxIdCard.class);
        IdCardResp idCardResp = new IdCardResp();
        if ("0".equals(idCard.getCode())) {
            //映射
            idCardResp.setUsername(idCard.getName());
            idCardResp.setIdcardno(idCard.getCardno());
            idCardResp.setSex(idCard.getSex());
            idCardResp.setAddress(idCard.getAddress());
            idCardResp.setBorn(idCard.getBorn());
            idCardResp.setGrantDept(idCard.getPolice());
            idCardResp.setNation(idCard.getNation());
            idCardResp.setUserLifeBegin(idCard.getUserlifeb());
            idCardResp.setUserLifeEnd(idCard.getUserlifee());
            //idCardResp.setPhotoFileName();
            idCardResp.setPhotoBase64String("data:image/jpg;base64," + idCard.getPhotobase64());
            idCardResp.setStatus(YesNoEnum.YES.getValue() + "");
            idCardResp.setMessage(idCard.getRetmsg());
        } else {
            idCardResp.setStatus(YesNoEnum.NO.getValue() + "");
            idCardResp.setMessage(idCard.getErrmsg());
        }
        return idCardResp;
    }
    
}
