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.IBipPzService;
import com.hzya.frame.sysnew.application.entity.SysExtensionApiEntity;
import com.hzya.frame.web.entity.BaseResult;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;


@Service(value = "bipPzService")
public class BipPzServiceImpl extends BaseService<BipTokenVo, String> implements IBipPzService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private IBipArchivesDao bipArchivesDao;
    /**
     * @param entity
     * @return com.hzya.frame.sysnew.application.entity.SysExtensionApiEntity
     * @Author lvleigang
     * @Description 组装凭证数据
     * @Date 10:52 上午 2024/11/5
     **/
    @Override
    public SysExtensionApiEntity getPz(SysExtensionApiEntity entity) {
        String bodys = entity.getBodys();
        BipVoucher voucher = JSONObject.parseObject(bodys, BipVoucher.class);
        List<String> fzhs = Arrays.asList(new String[]{"1", "2", "6", "73", "D01", "jobass", "93", "96", "22"});
        if (voucher.getDetails() != null && voucher.getDetails().size() > 0) {
            for (int a = 0; a < voucher.getDetails().size(); a++) {
                BipVoucherDetails voucherDetails = voucher.getDetails().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.queryArchivesData(archivesEntity);
                                if (archivesEntities != null && archivesEntities.size() > 0) {
                                    ass.setCheckvaluecode(archivesEntities.get(0).getRightCode());
                                }
                            }
                        }
                    }
                }
            }
        }
        bodys = JSON.toJSONString(voucher);
        entity.setBodys(bodys);
        return entity;
    }
}
