package com.hzya.frame.bip.v3.v2207.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.bip.v3.v2207.dao.IBipArchivesDao;
import com.hzya.frame.bip.v3.v2207.entity.BipArchivesEntity;
import com.hzya.frame.bip.v3.v2207.entity.BipAss;
import com.hzya.frame.bip.v3.v2207.entity.BipTokenVo;
import com.hzya.frame.bip.v3.v2207.entity.BipVoucher;
import com.hzya.frame.bip.v3.v2207.entity.BipVoucherDetails;
import com.hzya.frame.bip.v3.v2207.service.IBipV32207BillService;
import com.hzya.frame.bip.v3.v2207.service.IBipV32207TokenService;
import com.hzya.frame.sysnew.application.entity.SysExtensionApiEntity;
import com.hzya.frame.util.bipV3.Encryption;
import com.hzya.frame.util.bipV3.SHA256Util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(value = "BipV32207BillServiceImpl")
public class BipV32207BillServiceImpl extends BaseService<BipTokenVo, String> implements IBipV32207BillService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private IBipArchivesDao bipArchivesDao;
    /**
     *
     * @content 此方法获取拼接单据动态加密，heade参数动态传入
     * @author laborer
     * @date 2024/5/27 0027 11:38
     *
     */

    @Override
    public SysExtensionApiEntity getBipBill(SysExtensionApiEntity entity) {
        try {
            Map<String, String> headers = entity.getHeaders();
            String client_id = headers.get("client_id");
            //token信息
            String access_token =  headers.get("access_token");
            //公钥
            String pubKey =  headers.get("pubKey");
            StringBuffer sb = new StringBuffer();
            sb.append(client_id);
            String parm = entity.getBodys();
            if (StringUtils.isNotBlank(parm)) {
                sb.append(parm);
            }
            sb.append(pubKey);
            // 签名
            String sign = SHA256Util.getSHA256(sb.toString(), pubKey);
            StringBuffer querys = new StringBuffer();
            querys.append("access_token="+access_token);
            querys.append("&signature="+sign);
            querys.append("&client_id="+client_id);
            entity.setQuerys(querys.toString());
            headers.put("signature",sign);
//            headers.put("content-type","application/x-www-form-urlencoded");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }
    @Override
    public SysExtensionApiEntity getBipBillYs(SysExtensionApiEntity entity) {
        try {
            String bodys = entity.getBodys();
            BipVoucher voucher = JSONObject.parseObject(bodys, BipVoucher.class);
            List<String> fzhs = Arrays.asList(new String[]{"0012", "0006", "0004"});
            if (voucher.getDetail() != null && voucher.getDetail().size() > 0) {
                for (int a = 0; a < voucher.getDetail().size(); a++) {
                    BipVoucherDetails voucherDetails = voucher.getDetail().get(a);
                    if (voucherDetails.getAss() != null && voucherDetails.getAss().size() > 0) {
                        for (int b = 0; b < voucherDetails.getAss().size(); b++) {
                            BipAss ass = voucherDetails.getAss().get(b);
                            if (ass.getChecktypecode() != null && !"".equals(ass.getChecktypecode())) {
                                //存在需要转换的
                                if (fzhs.contains(ass.getChecktypecode())) {
                                    BipArchivesEntity archivesEntity = new BipArchivesEntity();
                                    archivesEntity.setArchivesType(ass.getChecktypecode());
                                    archivesEntity.setLeftCode(ass.getCheckvaluecode());
                                    archivesEntity.setLeftOrg(voucher.getPk_corp());

                                    List<BipArchivesEntity> archivesEntities = bipArchivesDao.queryArchivesDatays(archivesEntity);
                                    if (archivesEntities != null && archivesEntities.size() > 0) {
                                        ass.setCheckvaluecode(archivesEntities.get(0).getRightCode());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            bodys = JSON.toJSONString(voucher);
            entity.setBodys(bodys);

            Map<String, String> headers = entity.getHeaders();
            String client_id = headers.get("client_id");
            //token信息
            String access_token =  headers.get("access_token");
            //公钥
            String pubKey =  headers.get("pubKey");
            StringBuffer sb = new StringBuffer();
            sb.append(client_id);
            String parm = entity.getBodys();
            if (StringUtils.isNotBlank(parm)) {
                sb.append(parm);
            }
            sb.append(pubKey);
            // 签名
            String sign = SHA256Util.getSHA256(sb.toString(), pubKey);
            StringBuffer querys = new StringBuffer();
            querys.append("access_token="+access_token);
            querys.append("&signature="+sign);
            querys.append("&client_id="+client_id);
            entity.setQuerys(querys.toString());
            headers.put("signature",sign);
//            headers.put("content-type","application/x-www-form-urlencoded");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }
}
