package com.hzya.frame.sys.voucher.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.dateutil.DateUtil;
import com.hzya.frame.sys.entity.ModuleDto;
import com.hzya.frame.sys.proof.dao.IProofDao;
import com.hzya.frame.sys.proof.entity.ProofEntity;
import com.hzya.frame.sys.proof.entity.PullVo;
import com.hzya.frame.sys.proofDetail.dao.IProofDetailDao;
import com.hzya.frame.sys.proofDetail.entity.ProofDetailEntity;
import com.hzya.frame.sys.receiptConf.dao.IReceiptConfDao;
import com.hzya.frame.sys.receiptConf.entity.ReceiptConfEntity;
import com.hzya.frame.sys.templateFields.entity.TemplateFields;
import com.hzya.frame.sys.voucher.entity.VoucherEntityDto;
import com.hzya.frame.sys.voucherDetail.dao.IVoucherDetailDao;
import com.hzya.frame.sys.voucherDetail.entity.VoucherDetailEntity;
import com.hzya.frame.uuid.UUIDUtils;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.hzya.frame.web.exception.BaseSystemException;
import org.apache.commons.beanutils.BeanUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import com.hzya.frame.sys.voucher.entity.VoucherEntity;
import com.hzya.frame.sys.voucher.service.IVoucherService;
import com.hzya.frame.sys.voucher.dao.IVoucherDao;
import com.hzya.frame.basedao.service.impl.BaseService;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @description: 凭证单据  service
 * @tableName: sys_voucher
 * @entityName: VoucherEntity
 * @author: gjh
 * @history: 1.0
 */
@Service(value = "sys_voucherService")
public class VoucherServiceImpl extends BaseService<VoucherEntity, String> implements IVoucherService {

    protected IVoucherDao voucherDao;
    @Resource
    protected IProofDao proofDao;
    @Resource
    protected IReceiptConfDao receiptconfDao;
    @Resource
    protected IVoucherDetailDao voucherdetailDao;

    @Resource
    protected IProofDetailDao proofdetailDao;
    @Autowired
    public void setVoucherDao(IVoucherDao dao) {
        this.voucherDao = dao;
        this.dao = dao;
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询数据分页
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity queryVoucherPage(JSONObject object) {
        VoucherEntity entity = getData("jsonStr", object, VoucherEntity.class);
        //判断单据状态
        if (!checkStr(entity.getReceipt_status())) {
            return BaseResult.getFailureMessageEntity("请先传递单据状态");
        }
		if (!checkInt(entity.getPageNum())) {
			return BaseResult.getFailureMessageEntity("请先传递分页参数");
		}
		if (!checkInt(entity.getPageSize())) {
			return BaseResult.getFailureMessageEntity("请先传递分页参数");
		}
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<VoucherEntity> voucherEntities = voucherDao.queryByLike(entity);
        setReceiptTypeName(voucherEntities);
        PageInfo pageInfo = new PageInfo(voucherEntities);
        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    /**
     * @Author lvleigang
     * @Description
     * @Date 8:45 上午 2023/8/3
     * @param voucherEntities
     * @return void
     **/
    private void setReceiptTypeName(List<VoucherEntity> voucherEntities) {
        if(voucherEntities != null && voucherEntities.size() > 0){
            ReceiptConfEntity receiptConfEntity = new ReceiptConfEntity();
            receiptConfEntity.setVoucherEntities(voucherEntities);
            List<ReceiptConfEntity> receiptConfEntityList = receiptconfDao.query(receiptConfEntity);
            if(receiptConfEntityList != null && receiptConfEntityList.size() > 0){
                for (int i = 0; i < voucherEntities.size(); i++) {
                    for (int i1 = 0; i1 < receiptConfEntityList.size(); i1++) {
                        if(voucherEntities.get(i).getReceipt_type().equals(receiptConfEntityList.get(i1).getReceipt_code())){
                            voucherEntities.get(i).setReceipt_type_name(receiptConfEntityList.get(i1).getReceipt_type());
                            continue;
                        }
                    }
                }
            }
        }
    }
    /**
     * @Author lvleigang
     * @Description  凭证制单
     * @Date 1:33 下午 2023/7/12
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity proofPrepare(JSONObject object) {

        //判断是否有数据
        VoucherEntityDto entity = getData("jsonStr", object, VoucherEntityDto.class);
        if(entity == null ){
            return BaseResult.getFailureMessageEntity("请先选择数据");
        }
        if(entity.getIds() == null || entity.getIds().size() == 0 ){
            return BaseResult.getFailureMessageEntity("请先选择数据");
        }
        if(entity.getConsolidationEntries() == null || "".equals(entity.getConsolidationEntries()) ){
            return BaseResult.getFailureMessageEntity("请先选择分录类型");
        }
        if(entity.getSortsType() == null || "".equals(entity.getSortsType()) ){
            return BaseResult.getFailureMessageEntity("请先选择排序类型");
        }
        //获取凭证单据数据
        List<VoucherEntity> voucherEntityList = getVoucherList(entity.getIds());
        if(voucherEntityList == null ||  voucherEntityList.size() == 0){
            return BaseResult.getFailureMessageEntity("凭证单据不存在");
        }
        //判断单据类型的帐套是否都一样，不一样返回
        if(!checkSobNo(voucherEntityList,entity)){
            return BaseResult.getFailureMessageEntity("选择数据帐套号不一样不能生成数据");
        }

        //如果选择按照辅助核算分录，判断5个值是否一样，不一样返回  可能不需要判断
        //if(!checkConsolidationEntries(entity.getConsolidationEntries(),entity.getIds())){
        //    return BaseResult.getFailureMessageEntity("选择数据帐套号不一样不能生成数据");
        //}
        //先统计明细的分录和金额
        List<ProofDetailEntity> proofDetailEntityList = getfldata(voucherEntityList,entity);

        //组装数据
        ProofEntity proofEntity = new ProofEntity();
        StringBuffer title = new StringBuffer();
        Date certificate_date = new Date();
        String documenter = null;
        for (int i = 0; i < voucherEntityList.size(); i++) {
            if(i != 0){
                title.append("，");
            }
            title.append(voucherEntityList.get(i).getReceipt_title());
            if(voucherEntityList.get(i).getReceipt_date() != null){
                certificate_date = voucherEntityList.get(i).getReceipt_date();
            }

            if(voucherEntityList.get(i).getDocumenter() != null){
                documenter = voucherEntityList.get(i).getDocumenter();
            }
        }
        proofEntity.setDocumenter(querydataType(documenter,"4"));
        proofEntity.setCertificate_date(certificate_date);
        proofEntity.setReceipt_title(title.toString());
        proofEntity.setAp(DateUtil.getMonthStr(new Date()));
        proofEntity.setVou(entity.getVou());
        proofEntity.setSob_no(entity.getSobs());
        //proofEntity.setSobs(entity.getSobs());
        proofEntity.setYear(DateUtil.getYearStr(new Date()));
        proofEntity.setConsolidation_entries(entity.getConsolidationEntries());
        proofEntity.setProofDetails(proofDetailEntityList);
        proofEntity.setIds(entity.getIds());
        return BaseResult.getSuccessMessageEntity("获取凭证成功",proofEntity);
    }

    /**
     * @Author lvleigang
     * @Description  凭证制单所有数据
     * @Date 1:33 下午 2023/7/12
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity proofPrepareall(JSONObject object) {

        //判断是否有数据
        VoucherEntityDto entity = getData("jsonStr", object, VoucherEntityDto.class);
        if(entity == null ){
            return BaseResult.getFailureMessageEntity("请先选择数据");
        }
        if(entity.getIds() == null || entity.getIds().size() == 0 ){
            return BaseResult.getFailureMessageEntity("请先选择数据");
        }
        if(entity.getConsolidationEntries() == null || "".equals(entity.getConsolidationEntries()) ){
            return BaseResult.getFailureMessageEntity("请先选择分录类型");
        }
        if(entity.getSortsType() == null || "".equals(entity.getSortsType()) ){
            return BaseResult.getFailureMessageEntity("请先选择排序类型");
        }
        //获取凭证单据数据
        List<VoucherEntity> voucherEntityList = getVoucherList(entity.getIds());
        if(voucherEntityList == null ||  voucherEntityList.size() == 0){
            return BaseResult.getFailureMessageEntity("凭证单据不存在");
        }
        //判断单据类型的帐套是否都一样，不一样返回
        if(!checkSobNo(voucherEntityList,entity)){
            return BaseResult.getFailureMessageEntity("选择数据帐套号不一样不能生成数据");
        }

        //如果选择按照辅助核算分录，判断5个值是否一样，不一样返回  可能不需要判断
        //if(!checkConsolidationEntries(entity.getConsolidationEntries(),entity.getIds())){
        //    return BaseResult.getFailureMessageEntity("选择数据帐套号不一样不能生成数据");
        //}
        //先统计明细的分录和金额
        List<ProofDetailEntity> proofDetailEntityList = getfldata(voucherEntityList,entity);

        //组装数据
        ProofEntity proofEntity = new ProofEntity();
        StringBuffer title = new StringBuffer();
        Date certificate_date = new Date();
        String documenter = null;
        for (int i = 0; i < voucherEntityList.size(); i++) {
            if(i != 0){
                title.append("，");
            }
            title.append(voucherEntityList.get(i).getReceipt_title());
            if(voucherEntityList.get(i).getReceipt_date() != null){
                certificate_date = voucherEntityList.get(i).getReceipt_date();
            }

            if(voucherEntityList.get(i).getDocumenter() != null){
                documenter = voucherEntityList.get(i).getDocumenter();
            }
        }
        proofEntity.setDocumenter(querydataType(documenter,"4"));
        proofEntity.setCertificate_date(certificate_date);
        proofEntity.setReceipt_title(title.toString());
        proofEntity.setAp(DateUtil.getMonthStr(new Date()));
        proofEntity.setVou(entity.getVou());
        proofEntity.setSob_no(entity.getSobs());
        proofEntity.setYear(DateUtil.getYearStr(new Date()));
        proofEntity.setConsolidation_entries(entity.getConsolidationEntries());
        proofEntity.setProofDetails(proofDetailEntityList);
        proofEntity.setIds(entity.getIds());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("proof",proofEntity);
        jsonObject.put("voucher",voucherEntityList);
        return BaseResult.getSuccessMessageEntity("获取凭证成功",jsonObject);
    }

    /**
     * @Author lvleigang
     * @Description  保存凭证
     * @Date 1:33 下午 2023/7/12
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity saveProofPrepare(JSONObject object) {
        //判断是否有数据
        ProofEntity entity = getData("jsonStr", object, ProofEntity.class);
        if(entity == null ){
            return BaseResult.getFailureMessageEntity("请先选择数据");
        }

        if(entity.getIds() == null || entity.getIds().size() == 0){
            return BaseResult.getFailureMessageEntity("原单据信息错误");
        }
        entity.setCertificate_status("1");
        setProofEntityCreate(entity);
        if(entity.getProofDetails() != null && entity.getProofDetails().size() > 0){
            for (int i1 = 0; i1 < entity.getProofDetails().size(); i1++) {
                entity.getProofDetails().get(i1).setFormmain_id(entity.getId());
                entity.getProofDetails().get(i1).setSorts(i1+1L);
                setProofDetailEntityCreate(entity.getProofDetails().get(i1));
                proofdetailDao.save(entity.getProofDetails().get(i1));
            }
        }
        proofDao.save(entity);
        //修改原数据
        VoucherEntity voucherEntity = new VoucherEntity();
        voucherEntity.setIds(entity.getIds());
        voucherEntity.setProof_id(entity.getId());
        voucherEntity.setReceipt_status("2");
        voucherEntity.setModify_user_id(StpUtil.getLoginIdAsString());
        voucherEntity.setModify_time(new Date());
        voucherDao.VoucherEntity_update_ids(voucherEntity);
        return BaseResult.getSuccessMessageEntity("保存凭证成功");
    }

    /**
     * @Author lvleigang
     * @Description  查询需要获取凭证号的数据
     * @Date 1:33 下午 2023/7/12
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity queryProofPrepare(JSONObject object) {
        ProofEntity entity = new ProofEntity();
        List<ProofEntity> proofEntities = proofDao.queryProofPrepare(entity);
        return BaseResult.getSuccessMessageEntity("查询需要获取凭证号的数据成功",proofEntities);
    }

    /**
     * @Author lvleigang
     * @Description  保存凭证号
     * @Date 1:33 下午 2023/7/12
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity saveVoucherNumber(JSONObject object) {
        //判断是否有数据
        ProofEntity entity = getData("jsonStr", object, ProofEntity.class);
        if(entity == null ){
            return BaseResult.getFailureMessageEntity("请先选择数据");
        }

        if(entity.getId() == null || "".equals(entity.getId())){
            return BaseResult.getFailureMessageEntity("id为空");
        }
        if(entity.getVoucher_number() == null || "".equals(entity.getVoucher_number())){
            return BaseResult.getFailureMessageEntity("凭证号为空");
        }
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_time(new Date());
        proofDao.update(entity);
        return BaseResult.getSuccessMessageEntity("保存凭证号成功");
    }







    public void setProofDetailEntityCreate(ProofDetailEntity entity) {
        entity.setId(UUIDUtils.getUUID());
        entity.setSts("Y");
        entity.setCreate_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setCreate_time(new Date());
        entity.setModify_time(new Date());
    }

    public void setProofEntityCreate(ProofEntity entity) {
        entity.setId(UUIDUtils.getUUID());
        entity.setSts("Y");
        entity.setCreate_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setCreate_time(new Date());
        entity.setModify_time(new Date());
    }
    /**
     * @Author lvleigang
     * @Description  先统计分录
     * @Date 11:03 上午 2023/8/3
     * @param voucherEntityList
     * @param entity
     * @return java.util.List<com.hzya.frame.sys.proofDetail.entity.ProofDetailEntity>
     **/
    private List<ProofDetailEntity> getfldata(List<VoucherEntity> voucherEntityList, VoucherEntityDto entity) {
        //设置数据
        List<ProofDetailEntity> returnList = new ArrayList<>();
        //获取数据
        List<ProofDetailEntity> proofDetailEntityList = getfldataNoSort(voucherEntityList,entity);
        if(proofDetailEntityList == null || proofDetailEntityList.size() == 0){
            return returnList;
        }
        if("1".equals(entity.getConsolidationEntries())){// 不合并
            if("1".equals(entity.getSortsType())){// 先借后贷
                proofDetailEntityList.sort(new Comparator<ProofDetailEntity>() {//使用List接口的方法排序
                    @Override
                    public int compare(ProofDetailEntity o1, ProofDetailEntity o2) {
                        if(o1.getDirection().equals(o2.getDirection())){
                            return 0;
                        }
                        if("1".equals(o1.getDirection())){
                            return -1;
                        }else {
                            return 1;
                        }
                    }
                });
            }
            if("2".equals(entity.getSortsType())){// 按表单先借后贷
                proofDetailEntityList.sort(new Comparator<ProofDetailEntity>() {//使用List接口的方法排序
                    @Override
                    public int compare(ProofDetailEntity o1, ProofDetailEntity o2) {
                        if(o1.getVoucher_id() == o2.getVoucher_id()){
                            if(o1.getDirection().equals(o2.getDirection())){
                                return 0;
                            }
                            if("1".equals(o1.getDirection())){
                                return -1;
                            }else {
                                return 1;
                            }
                        }
                        if(o1.getVoucher_id() > o2.getVoucher_id()){
                            return -1;
                        }else {
                            return 1;
                        }
                    }
                });
            }
            if("3".equals(entity.getSortsType())){// 按表单填制顺序
            }
            returnList = proofDetailEntityList;
        }
        if("2".equals(entity.getConsolidationEntries())){// 科目+辅助核算
            //先借后贷
            for (int i = 0; i < proofDetailEntityList.size(); i++) {
                boolean flag = false;//没有重复
                if(i != 0){
                    for (int i1 = 0; i1 < returnList.size(); i1++) {
                        if(checkDataFlagsNoZy(proofDetailEntityList.get(i),returnList.get(i1))){
                            flag = true;
                        }
                    }
                }
                if(!flag){
                    returnList.add(proofDetailEntityList.get(i));
                }
            }
            returnList.sort(new Comparator<ProofDetailEntity>() {//使用List接口的方法排序
                @Override
                public int compare(ProofDetailEntity o1, ProofDetailEntity o2) {
                    if(o1.getDirection().equals(o2.getDirection())){
                        return 0;
                    }
                    if("1".equals(o1.getDirection())){
                        return -1;
                    }else {
                        return 1;
                    }

                }
            });
        }
        if("3".equals(entity.getConsolidationEntries())){// 科目+辅助核算+摘要
            //先借后贷
            for (int i = 0; i < proofDetailEntityList.size(); i++) {
                boolean flag = false;//没有重复
                if(i != 0){
                    for (int i1 = 0; i1 < returnList.size(); i1++) {
                        if(checkDataFlags(proofDetailEntityList.get(i),returnList.get(i1))){
                            flag = true;
                        }
                    }
                }
                if(!flag){
                    returnList.add(proofDetailEntityList.get(i));
                }
            }
            returnList.sort(new Comparator<ProofDetailEntity>() {//使用List接口的方法排序
                @Override
                public int compare(ProofDetailEntity o1, ProofDetailEntity o2) {
                    if(o1.getDirection().equals(o2.getDirection())){
                        return 0;
                    }
                    if("1".equals(o1.getDirection())){
                        return -1;
                    }else {
                        return 1;
                    }

                }
            });
        }
        return returnList;
    }

    private boolean checkDataFlagsNoZy(ProofDetailEntity proofDetailEntity, ProofDetailEntity proofDetailEntity1) {
        if(proofDetailEntity.getDirection().equals(proofDetailEntity1.getDirection())){//方向
            if(proofDetailEntity.getSubject().equals(proofDetailEntity1.getSubject())){//科目
                if("true".equals(proofDetailEntity.getProject_flag())
                        && "true".equals(proofDetailEntity1.getProject_flag())
                        && checkValue(proofDetailEntity.getProject(),proofDetailEntity1.getProject())){//项目
                    if("true".equals(proofDetailEntity.getSupplier_flag())
                            && "true".equals(proofDetailEntity1.getSupplier_flag())
                            && checkValue(proofDetailEntity.getSupplier(),proofDetailEntity1.getSupplier())){//供应商
                        if("true".equals(proofDetailEntity.getClient_flag())
                                && "true".equals(proofDetailEntity1.getClient_flag())
                                && checkValue(proofDetailEntity.getClient(),proofDetailEntity1.getClient())){//客户
                            if("true".equals(proofDetailEntity.getSalesman_flag())
                                    && "true".equals(proofDetailEntity1.getSalesman_flag())
                                    && checkValue(proofDetailEntity.getSalesman(),proofDetailEntity1.getSalesman())){//业务员
                                if("true".equals(proofDetailEntity.getSection_flag())
                                        && "true".equals(proofDetailEntity1.getSection_flag())
                                        && checkValue(proofDetailEntity.getSection(),proofDetailEntity1.getSection())){//部门
                                    if("1".equals(proofDetailEntity1.getDirection())){
                                        proofDetailEntity1.setCredit_amount(proofDetailEntity1.getCredit_amount().add(proofDetailEntity.getCredit_amount()));
                                    }else {
                                        proofDetailEntity1.setDebit_amount(proofDetailEntity1.getDebit_amount().add(proofDetailEntity.getDebit_amount()));
                                    }
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;

    }

    private boolean checkDataFlags(ProofDetailEntity direction, ProofDetailEntity direction1) {
        if(direction.getDirection().equals(direction1.getDirection())){//方向
            if(direction.getSubject().equals(direction1.getSubject())){//科目
                if("true".equals(direction.getProject_flag())
                        && "true".equals(direction1.getProject_flag())
                        && checkValue(direction.getProject(),direction1.getProject())){//项目

                    if("true".equals(direction.getSupplier_flag())
                            && "true".equals(direction1.getSupplier_flag())
                            && checkValue(direction.getSupplier(),direction1.getSupplier())){//供应商

                        if("true".equals(direction.getClient_flag())
                                && "true".equals(direction1.getClient_flag())
                                && checkValue(direction.getClient(),direction1.getClient())){//客户
                            if("true".equals(direction.getSalesman_flag())
                                    && "true".equals(direction1.getSalesman_flag())
                                    && checkValue(direction.getSalesman(),direction1.getSalesman())){//业务员

                                if("true".equals(direction.getSection_flag())
                                        && "true".equals(direction1.getSection_flag())
                                        && checkValue(direction.getSection(),direction1.getSection())){//部门
                                    if(direction.getAbstract_msg().equals(direction1.getAbstract_msg())){//摘要
                                        if("1".equals(direction1.getDirection())){
                                            direction1.setCredit_amount(direction1.getCredit_amount().add(direction.getCredit_amount()));
                                        }else {
                                            direction1.setDebit_amount(direction1.getDebit_amount().add(direction.getDebit_amount()));
                                        }
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    private boolean checkValue(String project, String project1) {
        if(project == null && project1 == null){
            return true;
        }
        if(project == null && project1 != null){
            return false;
        }
        if(project != null && project1 == null){
            return false;
        }
        return project.equals(project1);
    }

    /**
     * @Author lvleigang
     * @Description  获取全部的数据
     * @Date 11:33 上午 2023/8/3
     * @param voucherEntityList
     * @return java.util.List<com.hzya.frame.sys.proofDetail.entity.ProofDetailEntity>
     **/
    private List<ProofDetailEntity> getfldataNoSort(List<VoucherEntity> voucherEntityList,VoucherEntityDto entity) {
        List<ProofDetailEntity> proofDetailEntityList = new ArrayList<>();
        for (int i = 0; i < voucherEntityList.size(); i++) {
            VoucherEntity voucherEntity = voucherEntityList.get(i);
            //获取单据类型表
            ReceiptConfEntity receiptConfEntity = new ReceiptConfEntity();
            receiptConfEntity.setReceipt_code(voucherEntity.getReceipt_type());
            List<ReceiptConfEntity> receiptConfEntityList = receiptconfDao.query(receiptConfEntity);
            if(receiptConfEntityList != null && receiptConfEntityList.size() == 1){
                receiptConfEntity = receiptConfEntityList.get(0);
                if(entity.getVou() == null){
                    entity.setVou(receiptConfEntity.getProof_class_word());
                }
            }else {
                throw new BaseSystemException("单据配置信息错误");
            }
            //根据配置的科目查询辅助核算信息
            //获取贷方辅助核算
            PullVo jfpullVo = new PullVo();
            jfpullVo.setCodes(receiptConfEntity.getDebit_account());
            List<PullVo> jf = proofDao.queryFzhs(jfpullVo);
            boolean jfxm = false;
            boolean jfgys = false;
            boolean jfkh = false;
            boolean jfywy = false;
            boolean jfbm = false;
            if(jf != null && jf.size() > 0){
                for (int i1 = 0; i1 < jf.size(); i1++) {
                    if("xm".equals(jf.get(i1).getCodes())){
                        jfxm = true;
                    }
                    if("gys".equals(jf.get(i1).getCodes())){
                        jfgys = true;
                    }
                    if("kh".equals(jf.get(i1).getCodes())){
                        jfkh = true;
                    }
                    if("ywy".equals(jf.get(i1).getCodes())){
                        jfywy = true;
                    }
                    if("bm".equals(jf.get(i1).getCodes())){
                        jfbm = true;
                    }
                }
            }


            PullVo dfpullVo = new PullVo();
            dfpullVo.setCodes(receiptConfEntity.getCredit_account());
            List<PullVo> df = proofDao.queryFzhs(dfpullVo);
            boolean dfxm = false;
            boolean dfgys = false;
            boolean dfkh = false;
            boolean dfywy = false;
            boolean dfbm = false;
            if(df != null && df.size() > 0){
                for (int i1 = 0; i1 < df.size(); i1++) {
                    if("xm".equals(df.get(i1).getCodes())){
                        dfxm = true;
                    }
                    if("gys".equals(df.get(i1).getCodes())){
                        dfgys = true;
                    }
                    if("kh".equals(df.get(i1).getCodes())){
                        dfkh = true;
                    }
                    if("ywy".equals(df.get(i1).getCodes())){
                        dfywy = true;
                    }
                    if("bm".equals(df.get(i1).getCodes())){
                        dfbm = true;
                    }
                }
            }
            for (int i1 = 0; i1 < voucherEntity.getVoucherDetailEntityList().size(); i1++) {
                VoucherDetailEntity voucherDetailEntity = voucherEntity.getVoucherDetailEntityList().get(i1);
                ProofDetailEntity proofDetailEntity1 = new ProofDetailEntity();
                proofDetailEntity1.setVoucher_id(i); //表单序号
                proofDetailEntity1.setAbstract_msg(voucherDetailEntity.getAbstract_msg()); //摘要
                proofDetailEntity1.setSubject(receiptConfEntity.getDebit_account()); //科目
                if(jfxm){
                    proofDetailEntity1.setProject(querydataType(voucherEntity.getProject(),"1")); //项目
                    proofDetailEntity1.setProject_flag("true"); //项目
                }else {
                    proofDetailEntity1.setProject_flag("false"); //项目
                }
                if(jfgys){
                    proofDetailEntity1.setSupplier(querydataType(voucherEntity.getSupplier(),"2")); //供应商
                    proofDetailEntity1.setSupplier_flag("true"); //供应商
                }else {
                    proofDetailEntity1.setSupplier_flag("false"); //供应商
                }

                if(jfkh){
                    proofDetailEntity1.setClient(querydataType(voucherEntity.getClient(),"3")); //客户
                    proofDetailEntity1.setClient_flag("true"); //客户
                }else {
                    proofDetailEntity1.setClient_flag("false"); //客户
                }

                if(jfywy){
                    proofDetailEntity1.setSalesman(querydataType(voucherEntity.getSalesman(),"4")); //业务员
                    proofDetailEntity1.setSalesman_flag("true"); //业务员
                }else {
                    proofDetailEntity1.setSalesman_flag("false"); //业务员
                }

                if(jfbm){
                    proofDetailEntity1.setSection(querydataType(voucherEntity.getSection(),"5")); //部门
                    proofDetailEntity1.setSection_flag("true"); //部门
                }else {
                    proofDetailEntity1.setSection_flag("false"); //部门
                }
                proofDetailEntity1.setCredit_amount(voucherDetailEntity.getAmount()); //金额
                proofDetailEntity1.setDirection("1"); //借方

                ProofDetailEntity proofDetailEntity = new ProofDetailEntity();
                proofDetailEntity.setVoucher_id(i); //表单序号
                proofDetailEntity.setAbstract_msg(voucherDetailEntity.getAbstract_msg()); //摘要
                proofDetailEntity.setSubject(receiptConfEntity.getCredit_account()); //科目
                if(dfxm){
                    proofDetailEntity.setProject(querydataType(voucherDetailEntity.getProject(),"1")); //项目
                    proofDetailEntity.setProject_flag("true"); //项目
                }else {
                    proofDetailEntity.setProject_flag("false"); //项目
                }
                if(dfgys){
                    proofDetailEntity.setSupplier(querydataType(voucherDetailEntity.getSupplier(),"2")); //供应商
                    proofDetailEntity.setSupplier_flag("true"); //供应商
                }else {
                    proofDetailEntity.setSupplier_flag("false"); //供应商
                }

                if(dfkh){
                    proofDetailEntity.setClient(querydataType(voucherDetailEntity.getClient(),"3")); //客户
                    proofDetailEntity.setClient_flag("true"); //客户
                }else {
                    proofDetailEntity.setClient_flag("false"); //客户
                }
                if(dfywy){
                    proofDetailEntity.setSalesman(querydataType(voucherDetailEntity.getSalesman(),"4")); //业务员
                    proofDetailEntity.setSalesman_flag("true"); //业务员
                }else {
                    proofDetailEntity.setSalesman_flag("false"); //业务员
                }
                if(dfbm){
                    proofDetailEntity.setSection(querydataType(voucherDetailEntity.getSection(),"5")); //部门
                    proofDetailEntity.setSection_flag("true"); //部门
                }else {
                    proofDetailEntity.setSection_flag("false"); //部门
                }
                proofDetailEntity.setDebit_amount(voucherDetailEntity.getAmount()); //金额
                proofDetailEntity.setDirection("2"); //贷方
                proofDetailEntityList.add(proofDetailEntity1);
                proofDetailEntityList.add(proofDetailEntity);
            }
        }

        return proofDetailEntityList;
    }

    /**
     * @Author lvleigang
     * @Description  查询数据
     * @Date 1:57 下午 2023/8/3
     * @param project
     * @param type
     * @return java.lang.String
     **/
    private String querydataType(String project, String type) {
        String code = null;
        PullVo dfpullVo = new PullVo();
        dfpullVo.setCodes(project);
        List<PullVo> df = null;
        if("1".equals(type)){ //项目
            df = proofDao.queryXM(dfpullVo);
        }
        if("2".equals(type)){//供应商
            df = proofDao.queryGys(dfpullVo);
        }
        if("3".equals(type)){//客户
            df = proofDao.queryKh(dfpullVo);
        }
        if("4".equals(type)){//业务员
            df = proofDao.queryYwy(dfpullVo);
        }
        if("5".equals(type)){//部门
            df = proofDao.queryBm(dfpullVo);
        }
        if(df != null && df.size() == 1){
            code = df.get(0).getCodes();
        }
        return code;
    }

    /**
     * @Author lvleigang
     * @Description  获取数据
     * @Date 9:52 下午 2023/8/2
     * @param ids
     * @return java.util.List<com.hzya.frame.sys.voucher.entity.VoucherEntity>
     **/
    private List<VoucherEntity> getVoucherList(List<String> ids) {
        List<VoucherEntity> voucherEntityList = null;
        VoucherEntity voucherEntity = new VoucherEntity();
        voucherEntity.setIds(ids);
        voucherEntityList = voucherDao.query(voucherEntity);
        if(voucherEntityList != null && voucherEntityList.size() > 0){
            for (int i = 0; i < voucherEntityList.size(); i++) {
                VoucherDetailEntity voucherDetailEntity = new VoucherDetailEntity();
                voucherDetailEntity.setFormmain_id(voucherEntityList.get(i).getId());
                List<VoucherDetailEntity> voucherDetailEntityList =  voucherdetailDao.query(voucherDetailEntity);
                voucherEntityList.get(i).setVoucherDetailEntityList(voucherDetailEntityList);
            }
        }
        return voucherEntityList;
    }

    /**
     * @Author lvleigang
     * @Description  判断根据摘要和科目分类时，辅助核算是否一样，不一样返回
     * @Date 9:40 下午 2023/8/2
     * @param consolidationEntries
     * @param ids
     * @return boolean
     **/
    private boolean checkConsolidationEntries(String consolidationEntries, List<String> ids) {
        return true;
    }

    /**
     * @Author lvleigang
     * @Description  判断帐套号是否一样,且只有一个
     * @Date 9:35 下午 2023/8/2
     * @param voucherEntityList
     * @return boolean
     **/
    private boolean checkSobNo(List<VoucherEntity> voucherEntityList,VoucherEntityDto entity) {
        ReceiptConfEntity receiptConfEntity = new ReceiptConfEntity();
        receiptConfEntity.setVoucherEntities(voucherEntityList);
        List<ReceiptConfEntity> receiptConfEntityList = receiptconfDao.query(receiptConfEntity);


        //查询帐套
        //直接代入单据类型查询帐套
        Set<String> sobs = new HashSet<>();

        if(receiptConfEntityList != null && receiptConfEntityList.size() > 0){
            //有单据类型不存在
            for (int i = 0; i < receiptConfEntityList.size(); i++) {
                if(receiptConfEntityList.get(i).getSob_no() != null){
                    sobs.add(receiptConfEntityList.get(i).getSob_no());
                }
            }
        }else {
            return false;
        }
        if(sobs == null || sobs.size() != 1 ){
            return false;
        }
        entity.setSobs(sobs.iterator().next());
        return true;
    }

    /**
     * @Author lvleigang
     * @Description  不制单
     * @Date 1:33 下午 2023/7/12
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity notProofPrepare(JSONObject object) {
        VoucherEntity entity = getData("jsonStr", object, VoucherEntity.class);
        if (entity.getIds() == null || entity.getIds().size() == 0) {
            return BaseResult.getFailureMessageEntity("请先传递不制单数据");
        }
        if (!checkStr(entity.getNot_receipt_reason())) {
            return BaseResult.getFailureMessageEntity("请先输入不制单原因");
        }
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_time(new Date());
        entity.setReceipt_status("3");
        voucherDao.VoucherEntity_update_ids(entity);
        return BaseResult.getSuccessMessageEntity("单据不制单成功");
    }

    /**
     * @Author lvleigang
     * @Description  重新制单
     * @Date 1:33 下午 2023/7/12
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity anewProofPrepare(JSONObject object) {
        VoucherEntity entity = getData("jsonStr", object, VoucherEntity.class);
        if (entity.getIds() == null || entity.getIds().size() == 0) {
            return BaseResult.getFailureMessageEntity("请先传递重新制单数据");
        }
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_time(new Date());
        entity.setReceipt_status("1");
        voucherDao.VoucherEntity_update_ids(entity);
        return BaseResult.getSuccessMessageEntity("单据重新制单成功");
    }

    /**
     * @Author lvleigang
     * @Description  根据科目查询是否显示
     * @Date 1:33 下午 2023/7/12
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity account(JSONObject object) {
        JSONObject entity = getData("jsonStr", object, JSONObject.class);
        PullVo jfpullVo = new PullVo();
        jfpullVo.setCodes(entity.getString("account"));
        List<PullVo> jf = proofDao.queryFzhs(jfpullVo);
        boolean jfxm = false;
        boolean jfgys = false;
        boolean jfkh = false;
        boolean jfywy = false;
        boolean jfbm = false;
        if(jf != null && jf.size() > 0){
            for (int i1 = 0; i1 < jf.size(); i1++) {
                if("xm".equals(jf.get(i1).getCodes())){
                    jfxm = true;
                }
                if("gys".equals(jf.get(i1).getCodes())){
                    jfgys = true;
                }
                if("kh".equals(jf.get(i1).getCodes())){
                    jfkh = true;
                }
                if("ywy".equals(jf.get(i1).getCodes())){
                    jfywy = true;
                }
                if("bm".equals(jf.get(i1).getCodes())){
                    jfbm = true;
                }
            }
        }
        ProofDetailEntity proofDetailEntity1 = new ProofDetailEntity();
        if(jfxm){
            proofDetailEntity1.setProject_flag("true"); //项目
        }else {
            proofDetailEntity1.setProject_flag("false"); //项目
        }
        if(jfgys){
            proofDetailEntity1.setSupplier_flag("true"); //供应商
        }else {
            proofDetailEntity1.setSupplier_flag("false"); //供应商
        }
        if(jfkh){
            proofDetailEntity1.setClient_flag("true"); //客户
        }else {
            proofDetailEntity1.setClient_flag("false"); //客户
        }
        if(jfywy){
            proofDetailEntity1.setSalesman_flag("true"); //业务员
        }else {
            proofDetailEntity1.setSalesman_flag("false"); //业务员
        }
        if(jfbm){
            proofDetailEntity1.setSection_flag("true"); //部门
        }else {
            proofDetailEntity1.setSection_flag("false"); //部门
        }
        return BaseResult.getSuccessMessageEntity("根据科目查询是否显示成功",proofDetailEntity1);
    }
}
