package com.brilliance.isc.common.invchk.service;

import com.brilliance.isc.bo.*;
import com.brilliance.isc.bo.model.ImgInvmod;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.counter.service.CounterService;
import com.brilliance.isc.common.invchk.vo.InvlstVo;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.common.wfmmod.service.WfmmodService;
import com.brilliance.isc.mda.dao.*;
import com.brilliance.isc.utils.DateUtils;
import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.brilliance.mda.runtime.mda.Constants.TAB;

@Component(value = "invToolComponentCommon")
public class InvToolComponent {

    @Autowired
    private TransactionHelpService transactionHelpService;

    @Autowired
    private InvuseMapper invuseMapper;

    @Autowired
    private InvmonMapper invmonMapper;

    @Autowired
    private CounterService counterService;

    @Autowired
    private ImguseMapper imguseMapper;

    @Autowired
    private WfmmodService wfmmodService;

    @Autowired
    private InvMapper invMapper;

    @Autowired
    private ImfMapper imfMapper;

    @Autowired
    private OffcfgMapper offcfgMapper;

    /**
     * source:invchk.0002.script
     * 业务保存更新invuse，imguse，invmon,imglot
     */
    public void updateInv(ImgInvmod imgInvmod, String ownref, String pntref, BizInfoVo rec) {
        //save保存时，将业务编号，子业务编号，trn流水，begdat，enddat,占用金额，剩余金额,经办机构保存
        //更新修改后，对应的回滚trocorm\\invrbk里的回滚方法也要重新写
        String trninr = transactionHelpService.getTrninr();
        String frame = SettleContext.getTransName();
        //更新invuse表
        //跳过核验未勾选，save时更新invuse并插入invmon
        Inv inv;
        BigDecimal sum;
        BigDecimal balance;
        Invmon invmon;
        List<Imf> imfList = imfMapper.selectBySubref(ownref);
        if (CollectionUtils.isNotEmpty(imfList)) {
            for (Imf item : imfList) {
                Invmon dbitem = invmonMapper.selectByPrimaryKey(item.getInr());
                transactionHelpService.addBimdataUpdateModel(dbitem);
                item.setTrninr(trninr);
                item.setImfrefsta("D");//使用中
                imfMapper.updateByPrimaryKey(item);
            }
        }
        if (CollectionUtils.isNotEmpty(imgInvmod.getInvlst())) {
            if (MdaUtils.compareTo(imgInvmod.getSkipped(), "X") != 0) {
                for (InvlstVo item : imgInvmod.getInvlst()) {
                    String invcod = item.getInvcod();
                    String invnum = item.getInvnum();
                    //inr有值表示invuse已经存表，save时做更新invuse,更新inv表的剩余金额
                    if (StringUtils.isEmpty(item.getInr())) {
                        item.setInr(counterService.dbCounterFormat("INVUSE"));
                        item.setOwnref(ownref);
                        item.setPntref(pntref);
                        item.setTrninr(trninr);
                        item.setSkipped(imgInvmod.getSkipped());
                        item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                        invuseMapper.insert(item);
                        Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                        transactionHelpService.addBimdataDeleteModel(dbitem);
                    } else {
                        if (StringUtils.isEmpty(item.getSelfownref()) || "-BFTSND-BFTDRV-BPTOPN-".contains(frame)) {
                            Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                            transactionHelpService.addBimdataUpdateModel(dbitem);
                            item.setOwnref(ownref);
                            item.setPntref(pntref);
                            item.setTrninr(trninr);
                            item.setSkipped(imgInvmod.getSkipped());
                            if (StringUtils.isEmpty(item.getBranch())) {
                                item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                            }
                            invuseMapper.updateByPrimaryKey(item);
                        } else {
                            Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                            transactionHelpService.addBimdataUpdateModel(dbitem);
                            //自开自议
                            item.setSelfownref(ownref);
                            item.setTrninr(trninr);
                            item.setSkipped(imgInvmod.getSkipped());
                            if (StringUtils.isEmpty(item.getBranch())) {
                                item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                            }
                            invuseMapper.updateByPrimaryKey(item);
                        }
                    }

                    invmon = new Invmon();
                    invmon.setInr(counterService.dbCounterFormat("INVMON"));
                    invmon.setInvcod(invcod);
                    invmon.setInvnum(invnum);
                    invmon.setOwnref(ownref);
                    invmon.setWylsh(trninr);
                    String avbby = Utils.getFieldValue(rec, "avbby", "");
                    switch (frame) {
                        case "BFTSND":
                        case "BFTDRV":
                            //远期截止日期13个月后，即期取寄单日或到单日加一个月,D远期，P即期
                            if ("D".equalsIgnoreCase(avbby)) {
                                invmon.setEnddat(DateUtils.addMonths(Dates.today(), 13));
                            } else if ("P".equalsIgnoreCase(avbby)) {
                                Date orddat = Utils.getFieldValue(rec, "orddat", null);
                                invmon.setEnddat(DateUtils.addMonths(orddat, 1));
                            }
                            break;
                        case "DITDCK":
                        case "BDTDCK":
                            if ("D".equalsIgnoreCase(avbby)) {
                                invmon.setEnddat(DateUtils.addMonths(Dates.today(), 13));
                            } else if ("P".equalsIgnoreCase(avbby)) {
                                Date rcvdat = Utils.getFieldValue(rec, "rcvdat", null);
                                invmon.setEnddat(DateUtils.addMonths(rcvdat, 1));
                            }
                            break;
                        case "BPTOPN":
                            Date matdat = Utils.getFieldValue(rec, "matdat", null);
                            invmon.setEnddat(DateUtils.addDays(matdat, 15));
                            break;
                        default:
                            invmon.setEnddat(DateUtils.addDays(Dates.today(), 1));
                            break;
                    }
                    invmonMapper.insert(invmon);
                    Invmon dbitem = invmonMapper.selectByPrimaryKey(invmon.getInr());
                    transactionHelpService.addBimdataDeleteModel(dbitem);
                }
            } else {
                //如果勾选跳过核验，save时存invuse,和inv表
                for (InvlstVo item : imgInvmod.getInvlst()) {
                    //inr为空，表示还未存invuse表
                    if (StringUtils.isEmpty(item.getInr())) {
                        item.setInr(counterService.dbCounterFormat("INVUSE"));
                        item.setOwnref(ownref);
                        item.setPntref(pntref);
                        item.setTrninr(trninr);
                        item.setSkipped("X");
                        item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                        invuseMapper.insert(item);
                        Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                        transactionHelpService.addBimdataDeleteModel(dbitem);
                    } else {
                        if (StringUtils.isEmpty(item.getSelfownref()) || "-BFTSND-BFTDRV-BPTOPN-".contains(frame)) {
                            Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                            transactionHelpService.addBimdataUpdateModel(dbitem);
                            item.setOwnref(ownref);
                            item.setPntref(pntref);
                            item.setTrninr(trninr);
                            item.setSkipped(imgInvmod.getSkipped());
                            if (StringUtils.isEmpty(item.getBranch())) {
                                item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                            }
                            invuseMapper.updateByPrimaryKey(item);
                        } else {
                            Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                            transactionHelpService.addBimdataUpdateModel(dbitem);
                            //自开自议
                            item.setSelfownref(ownref);
                            item.setTrninr(trninr);
                            item.setSkipped(imgInvmod.getSkipped());
                            if (StringUtils.isEmpty(item.getBranch())) {
                                item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                            }
                            invuseMapper.updateByPrimaryKey(item);
                        }
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(imgInvmod.getInvlst())) {
                for (InvlstVo item : imgInvmod.getInvlst()) {
                    inv = invMapper.selectByCodeAndNum(item.getInvcod(), item.getInvnum());
                    if (!MdaUtils.isEmpty(inv)) {
                        transactionHelpService.addBimdataUpdateModel(inv);
                        sum = BigDecimal.ZERO;
                        List<Invuse> invlst1 = invuseMapper.selectBalanceByCondition(item.getInvcod(), item.getInvnum());
                        if (CollectionUtils.isNotEmpty(invlst1)) {
                            for (Invuse item1 : invlst1) {
                                sum = sum.add(item1.getAmountin());
                            }
                            balance = invlst1.get(0).getAmounttax().subtract(sum);
                            inv.setBalance(balance);
                            inv.setMonstatus("W");
                            invMapper.updateByCodeNum(inv);
                        }
                    } else {
                        inv = new Inv();
                        inv.setInr(counterService.dbCounterFormat("INV"));
                        inv.setOrgno("1");
                        inv.setIsmonitor("00");
                        inv.setMonstatus("W");
                        inv.setInvoicecode(item.getInvcod());
                        inv.setInvoicenumber(item.getInvnum());
                        inv.setBillingdate(item.getInvdat());
                        inv.setCheckcode(item.getCheckcode());
                        inv.setAmounttax(item.getAmounttax());
                        inv.setBalance(item.getAmounttax().subtract(item.getAmountin()));
                        invMapper.insert(inv);
                        transactionHelpService.addBimdataDeleteModel(inv);
                    }
                }
            }
        }
        if (CollectionUtils.isNotEmpty(imgInvmod.getOldInvlst())) {
            //valflg为VLID有效，INVD无效
            for (InvlstVo item : imgInvmod.getOldInvlst()) {
                if ("INVD".equalsIgnoreCase(item.getValflg()) && StringUtils.isNotEmpty(item.getInr())) {
                    Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                    transactionHelpService.addBimdataUpdateModel(dbitem);
                    item.setOwnref("");
                    item.setPntref("");
                    invuseMapper.updateByPrimaryKey(item);
                    //无效的发票解除监控
                    invmon = new Invmon();
                    invmon.setInr(counterService.dbCounterFormat("INVMON"));
                    invmon.setInvcod(item.getInvcod());
                    invmon.setInvnum(item.getInvnum());
                    invmon.setOwnref(ownref);
                    invmon.setWylsh(trninr);
                    invmon.setEnddat(DateUtils.addDays(Dates.today(), 1));
                    invmonMapper.insert(invmon);
                    Invmon dbitem1 = invmonMapper.selectByPrimaryKey(invmon.getInr());
                    transactionHelpService.addBimdataDeleteModel(dbitem1);
                }
            }
        }
        //如果勾选跳过核验，不存pdv工作流
        if (MdaUtils.compareTo(imgInvmod.getSkipped(), "X") != 0) {
            wfmmodService.wfeAdd("PDV", "PDV", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
        }
        //云平台来报监控发票、e结算来报
        if ("DETDRV".equalsIgnoreCase(frame) || "CPTHYF".equalsIgnoreCase(frame)) {
            int count = imgInvmod.getInvlst().size();
            if (MdaUtils.compareTo(imgInvmod.getSkipped(), "X") == 0 && count > 0) {
                wfmmodService.wfeAdd("PDV", "PDV", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
            }
        }
    }

    /**
     * @Description:发票修改交易更新invuse和inv
     */
    public void updateInvForAme(ImgInvmod imgInvmod, String ownref, String pntref) {
        //发票修改交易不能跳过核验
        String trninr = transactionHelpService.getTrninr();
        String frame = SettleContext.getTransName();
        //更新invuse表
        //跳过核验未勾选，save时更新invuse并插入invmon
        Inv inv;
        BigDecimal sum;
        BigDecimal balance;
        Invmon invmon;
        List<Imf> imfList = imfMapper.selectBySubref(ownref);
        if (CollectionUtils.isNotEmpty(imfList)) {
            for (Imf item : imfList) {
                Invmon dbitem = invmonMapper.selectByPrimaryKey(item.getInr());
                transactionHelpService.addBimdataUpdateModel(dbitem);
                item.setTrninr(trninr);
                item.setImfrefsta("D");//使用中
                imfMapper.updateByPrimaryKey(item);
            }
        }
        List<Invmon> invmonList1 = invmonMapper.selectByOwnref(ownref);
        if (CollectionUtils.isNotEmpty(imgInvmod.getInvlst())) {
            if (MdaUtils.compareTo(imgInvmod.getSkipped(), "X") != 0) {
                for (InvlstVo item : imgInvmod.getInvlst()) {
                    String invcod = item.getInvcod();
                    String invnum = item.getInvnum();
                    //inr有值表示invuse已经存表，save时做更新invuse,更新inv表的剩余金额
                    if (StringUtils.isEmpty(item.getInr())) {
                        item.setInr(counterService.dbCounterFormat("INVUSE"));
                        item.setOwnref(ownref);
                        item.setPntref(pntref);
                        item.setTrninr(trninr);
                        item.setSkipped(imgInvmod.getSkipped());
                        item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                        invuseMapper.insert(item);
                        Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                        transactionHelpService.addBimdataDeleteModel(dbitem);
                    } else {
                        if (StringUtils.isEmpty(item.getSelfownref()) || "-BFTINV-BPTINV-".contains(frame)) {
                            Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                            transactionHelpService.addBimdataUpdateModel(dbitem);
                            item.setOwnref(ownref);
                            item.setPntref(pntref);
                            item.setTrninr(trninr);
                            item.setSkipped(imgInvmod.getSkipped());
                            if (StringUtils.isEmpty(item.getBranch())) {
                                item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                            }
                            invuseMapper.updateByPrimaryKey(item);
                        } else {
                            Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                            transactionHelpService.addBimdataUpdateModel(dbitem);
                            //自开自议
                            item.setSelfownref(ownref);
                            item.setTrninr(trninr);
                            item.setSkipped(imgInvmod.getSkipped());
                            if (StringUtils.isEmpty(item.getBranch())) {
                                item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                            }
                            invuseMapper.updateByPrimaryKey(item);
                        }
                    }
                    invmon = new Invmon();
                    invmon.setInr(counterService.dbCounterFormat("INVMON"));
                    invmon.setInvcod(invcod);
                    invmon.setInvnum(invnum);
                    invmon.setOwnref(ownref);
                    invmon.setWylsh(trninr);
                    //监控截止日期和前手交易一致，如果前手交易跳过核验，invmonList1可能为空
                    if (CollectionUtils.isNotEmpty(invmonList1)) {
                        invmon.setEnddat(invmonList1.get(0).getEnddat());
                    } else {
                        invmon.setEnddat(DateUtils.addMonths(Dates.today(), 13));
                    }
                    invmonMapper.insert(invmon);
                    Invmon dbitem = invmonMapper.selectByPrimaryKey(invmon.getInr());
                    transactionHelpService.addBimdataDeleteModel(dbitem);
                }
            } else {
                //如果勾选跳过核验，save时存invuse,和inv表
                for (InvlstVo item : imgInvmod.getInvlst()) {
                    //inr为空，表示还未存invuse表
                    if (StringUtils.isEmpty(item.getInr())) {
                        item.setInr(counterService.dbCounterFormat("INVUSE"));
                        item.setOwnref(ownref);
                        item.setPntref(pntref);
                        item.setTrninr(trninr);
                        item.setSkipped(imgInvmod.getSkipped());
                        item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                        invuseMapper.insert(item);
                        Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                        transactionHelpService.addBimdataDeleteModel(dbitem);
                    } else {
                        if (StringUtils.isEmpty(item.getSelfownref()) || "-BFTINV-BPTINV-".contains(frame)) {
                            Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                            transactionHelpService.addBimdataUpdateModel(dbitem);
                            item.setOwnref(ownref);
                            item.setPntref(pntref);
                            item.setTrninr(trninr);
                            item.setSkipped(imgInvmod.getSkipped());
                            if (StringUtils.isEmpty(item.getBranch())) {
                                item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                            }
                            invuseMapper.updateByPrimaryKey(item);
                        } else {
                            Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                            transactionHelpService.addBimdataUpdateModel(dbitem);
                            //自开自议
                            item.setSelfownref(ownref);
                            item.setTrninr(trninr);
                            item.setSkipped(imgInvmod.getSkipped());
                            if (StringUtils.isEmpty(item.getBranch())) {
                                item.setBranch(SettleContext.getUserSession().getHldbch().getBranch());
                            }
                            invuseMapper.updateByPrimaryKey(item);
                        }
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(imgInvmod.getInvlst())) {
                for (InvlstVo item : imgInvmod.getInvlst()) {
                    inv = invMapper.selectByCodeAndNum(item.getInvcod(), item.getInvnum());
                    if (!MdaUtils.isEmpty(inv)) {
                        transactionHelpService.addBimdataUpdateModel(inv);
                        sum = BigDecimal.ZERO;
                        List<Invuse> invlst1 = invuseMapper.selectBalanceByCondition(item.getInvcod(), item.getInvnum());
                        if (CollectionUtils.isNotEmpty(invlst1)) {
                            for (Invuse item1 : invlst1) {
                                sum = sum.add(item1.getAmountin());
                            }
                            balance = invlst1.get(0).getAmounttax().subtract(sum);
                            inv.setBalance(balance);
                            inv.setMonstatus("W");
                            invMapper.updateByCodeNum(inv);
                        }
                    } else {
                        inv = new Inv();
                        inv.setInr(counterService.dbCounterFormat("INV"));
                        inv.setOrgno("1");
                        inv.setIsmonitor("00");
                        inv.setMonstatus("W");
                        inv.setInvoicecode(item.getInvcod());
                        inv.setInvoicenumber(item.getInvnum());
                        inv.setBillingdate(item.getInvdat());
                        inv.setCheckcode(item.getCheckcode());
                        inv.setAmounttax(item.getAmounttax());
                        inv.setBalance(item.getAmounttax().subtract(item.getAmountin()));
                        invMapper.insert(inv);
                        transactionHelpService.addBimdataDeleteModel(inv);
                    }
                }
            }
        }
        if (CollectionUtils.isNotEmpty(imgInvmod.getOldInvlst())) {
            //valflg为VLID有效，INVD无效
            for (InvlstVo item : imgInvmod.getOldInvlst()) {
                if ("INVD".equalsIgnoreCase(item.getValflg()) && StringUtils.isNotEmpty(item.getInr())) {
                    Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                    transactionHelpService.addBimdataUpdateModel(dbitem);
                    item.setOwnref("");
                    item.setPntref("");
                    invuseMapper.updateByPrimaryKey(item);
                    //无效的发票解除监控
                    invmon = new Invmon();
                    invmon.setInr(counterService.dbCounterFormat("INVMON"));
                    invmon.setInvcod(item.getInvcod());
                    invmon.setInvnum(item.getInvnum());
                    invmon.setOwnref("");
                    invmon.setWylsh(trninr);
                    invmon.setEnddat(DateUtils.addDays(Dates.today(), 1));
                    invmonMapper.insert(invmon);
                    Invmon dbitem1 = invmonMapper.selectByPrimaryKey(invmon.getInr());
                    transactionHelpService.addBimdataDeleteModel(dbitem1);
                }
            }
        }
        //如果勾选跳过核验，不存pdv工作流
        if (MdaUtils.compareTo(imgInvmod.getSkipped(), "X") != 0) {
            wfmmodService.wfeAdd("PDV", "PDV", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
        }
    }

    /**
     * @Description:承兑交易(BDTUDP,BFTACC)发票监控
     */
    public void updateInvForacc(String ownref, BizInfoVo rec) {
        List<Invuse> invlst = invuseMapper.selectInvuseByOwnrefSkipped(ownref);
        String trninr = transactionHelpService.getTrninr();
        Date matdat = Utils.getFieldValue(rec, "newmatdat", null);
        Invmon invmon;
        if (CollectionUtils.isNotEmpty(invlst)) {
            //承兑交易取远期到期日+15天
            for (Invuse item : invlst) {
                invmon = new Invmon();
                invmon.setInr(counterService.dbCounterFormat("INVMON"));
                invmon.setInvcod(item.getInvcod());
                invmon.setInvnum(item.getInvnum());
                invmon.setOwnref(ownref);
                invmon.setWylsh(trninr);
                invmon.setEnddat(DateUtils.addDays(matdat, 15));
                invmonMapper.insert(invmon);
                Invmon dbitem = invmonMapper.selectByPrimaryKey(invmon.getInr());
                transactionHelpService.addBimdataDeleteModel(dbitem);
            }
            //两个承兑交易加发票监控
            wfmmodService.wfeAdd("PDV", "PDV", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
        }
    }

    /**
     * @Description:单据注销、拒付退单解除发票占用和发票监控
     */
    public void updateInvForcan(String ownref) {
        String trninr = transactionHelpService.getTrninr();
        Invmon invmon;
        //查询该单据下所有已加入监控的发票
        List<Invuse> invlst2 = invuseMapper.selectInvuseByOwnrefSkipped(ownref);
        if (CollectionUtils.isNotEmpty(invlst2)) {
            for (Invuse item : invlst2) {
                //解除监控
                invmon = new Invmon();
                invmon.setInr(counterService.dbCounterFormat("INVMON"));
                invmon.setInvcod(item.getInvcod());
                invmon.setInvnum(item.getInvnum());
                invmon.setOwnref(ownref);
                invmon.setWylsh(trninr);
                invmon.setEnddat(DateUtils.addDays(Dates.today(), 1));
                invmonMapper.insert(invmon);
                Invmon dbitem = invmonMapper.selectByPrimaryKey(invmon.getInr());
                transactionHelpService.addBimdataDeleteModel(dbitem);
            }
            wfmmodService.wfeAdd("PDV", "PDV", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
        }
        //查询该单据下所有有效发票
        List<Invuse> invlst1 = invuseMapper.selectInvuseByOwnref(ownref);
        if (CollectionUtils.isNotEmpty(invlst1)) {
            for (Invuse item : invlst1) {
                //解除占用
                Invuse dbitem = invuseMapper.selectByPrimaryKey(item.getInr());
                transactionHelpService.addBimdataUpdateModel(dbitem);
                item.setPntref("");
                item.setOwnref("");
                item.setSelfownref("");
                item.setValflg("INVD");
                invuseMapper.updateByPrimaryKey(item);
            }
        }
    }

    /**
     * @Description:出口融资还款发票解除监控
     */
    public void updateInvForbptset(String ownref) {
        String trninr = transactionHelpService.getTrninr();
        Invmon invmon;
        //查询该单据下所有已加入监控的发票
        List<Invuse> invlst2 = invuseMapper.selectInvuseByOwnrefSkipped(ownref);
        if (CollectionUtils.isNotEmpty(invlst2)) {
            for (Invuse item : invlst2) {
                //解除监控
                invmon = new Invmon();
                invmon.setInr(counterService.dbCounterFormat("INVMON"));
                invmon.setInvcod(item.getInvcod());
                invmon.setInvnum(item.getInvnum());
                invmon.setOwnref(ownref);
                invmon.setWylsh(trninr);
                invmon.setEnddat(DateUtils.addDays(Dates.today(), 1));
                invmonMapper.insert(invmon);
                Invmon dbitem = invmonMapper.selectByPrimaryKey(invmon.getInr());
                transactionHelpService.addBimdataDeleteModel(dbitem);
            }
            //两个承兑交易加发票监控
            wfmmodService.wfeAdd("PDV", "PDV", Dates.add(Dates.today(), 1), BigDecimal.valueOf(21));
        }
    }

    /**
     * @Description: 业务保存更新imguse
     */
    public void updateImg(ImgInvmod imgInvmod, String ownref) {
        String trninr = transactionHelpService.getTrninr();
        String frame = SettleContext.getTransName();
        if (CollectionUtils.isNotEmpty(imgInvmod.getOldImguseList())) {
            for (Imguse item : imgInvmod.getOldImguseList()) {
                if ("INVD".equalsIgnoreCase(item.getValflg())) {
                    Imguse dbitem = imguseMapper.selectByPrimaryKey(item.getInr());
                    transactionHelpService.addBimdataUpdateModel(dbitem);
                    imguseMapper.updateByPrimaryKey(item);
                }
            }
        }
        if (CollectionUtils.isNotEmpty(imgInvmod.getImguseList())) {
            for (Imguse item : imgInvmod.getImguseList()) {
                if (StringUtils.isEmpty(item.getInr())) {
                    item.setInr(counterService.dbCounterFormat("IMGUSE"));
                    item.setOwnref(ownref);
                    item.setTrninr(trninr);
                    imguseMapper.insert(item);
                    Imguse dbitem = imguseMapper.selectByPrimaryKey(item.getInr());
                    transactionHelpService.addBimdataDeleteModel(dbitem);
                } else {
                    if (StringUtils.isEmpty(item.getSelfownref()) || !"-BDTDCK-BDTINV-BDTSET-DITAMC-DITAME-DITDCK-DITOPN-".contains(frame)) {
                        Imguse dbitem = imguseMapper.selectByPrimaryKey(item.getInr());
                        transactionHelpService.addBimdataUpdateModel(dbitem);
                        item.setOwnref(ownref);
                        item.setTrninr(trninr);
                        imguseMapper.updateByPrimaryKey(item);
                    } else {
                        Imguse dbitem = imguseMapper.selectByPrimaryKey(item.getInr());
                        transactionHelpService.addBimdataUpdateModel(dbitem);
                        //自开自议
                        item.setSelfownref(ownref);
                        item.setTrninr(trninr);
                        imguseMapper.updateByPrimaryKey(item);
                    }
                }
            }
        }
    }

    /**
     * @Description:Ditdck初始化
     */
    public ImgInvmod initImgInvForDitdck(String ownref, String sptinr) {
        ImgInvmod imgInvmod = new ImgInvmod();
        List<InvlstVo> invlst = new ArrayList<>();
        List<InvlstVo> oldInvlst = new ArrayList<>();
        List<Imguse> oldImguseList = new ArrayList<>();
        //获取跳过核验开关
        String isSkipped = this.isSkipped();
        imgInvmod.setIsSkipped(isSkipped);
        //从来报中获取影像列表和发票列表
        List<InvlstVo> invlst1 = invuseMapper.selectBySptinr(ownref, sptinr);
        Boolean isOneSelf = false;
        if (CollectionUtils.isNotEmpty(invlst1)) {
            for (InvlstVo item : invlst1) {
                if ("self".equals(item.getSelfownref())) {
                    isOneSelf = true;
                    break;
                }
            }
        }
        imgInvmod.setIsOneSelf(isOneSelf);
        invlst.addAll(invlst1);
        //获取自开自议的发票
        List<InvlstVo> invlst2 = invuseMapper.selectBySelfownref(ownref);
        invlst.addAll(invlst2);
        if (CollectionUtils.isNotEmpty(invlst)) {
            imgInvmod.setInvlst(invlst);
            imgInvmod.getPageTable().setTotal(invlst.size());
            int endIndex = imgInvmod.getPageTable().getPageSize() > invlst.size() ? invlst.size() : imgInvmod.getPageTable().getPageSize();
            imgInvmod.getPageTable().setTableData(invlst.subList(0, endIndex));
        }
        if (StringUtils.isNotEmpty(sptinr)) {
            List<Imguse> imguseList = imguseMapper.selectBySptinr(sptinr);
            imgInvmod.setImguseList(imguseList);
        }
        if (StringUtils.isNotEmpty(ownref)) {
            List<InvlstVo> oldInvlst1 = invuseMapper.selectByOwnref(ownref);
            oldInvlst.addAll(oldInvlst1);
            List<InvlstVo> oldInvlst2 = invuseMapper.selectBySelfSkip(ownref);
            oldInvlst.addAll(oldInvlst2);
            if (CollectionUtils.isNotEmpty(oldInvlst)) {
                imgInvmod.setOldInvlst(oldInvlst);
            }
            List<Imguse> oldImguseList1 = imguseMapper.selectByOwnref(ownref);
            oldImguseList.addAll(oldImguseList1);
            //获取自开自议寄单影像
            List<Imguse> oldImguseList2 = imguseMapper.selectBySelfownref(ownref);
            oldImguseList1.addAll(oldImguseList2);
            if (CollectionUtils.isNotEmpty(oldImguseList)) {
                imgInvmod.setOldImguseList(oldImguseList);
            }
        }
        return imgInvmod;
    }

    /**
     * @Description:init时根据ownref查询oldInvlst和oldImguseList
     */
    public ImgInvmod initImgInv(String ownref, String sptinr) {
        ImgInvmod imgInvmod = new ImgInvmod();
        List<InvlstVo> invlst = new ArrayList<>();
        List<InvlstVo> oldInvlst = new ArrayList<>();
        List<Imguse> oldImguseList = new ArrayList<>();
        //获取跳过核验开关
        String isSkipped = this.isSkipped();
        imgInvmod.setIsSkipped(isSkipped);
        //从来报中获取影像列表和发票列表
        List<InvlstVo> invlst1 = invuseMapper.selectBySptinr(ownref, sptinr);
        invlst.addAll(invlst1);
        //获取自开自议的发票
        List<InvlstVo> invlst2 = invuseMapper.selectBySelfownref(ownref);
        invlst.addAll(invlst2);
        if (CollectionUtils.isNotEmpty(invlst)) {
            imgInvmod.setInvlst(invlst);
            imgInvmod.getPageTable().setTotal(invlst.size());
            int endIndex = imgInvmod.getPageTable().getPageSize() > invlst.size() ? invlst.size() : imgInvmod.getPageTable().getPageSize();
            imgInvmod.getPageTable().setTableData(invlst.subList(0, endIndex));
        }
        if (StringUtils.isNotEmpty(sptinr)) {
            List<Imguse> imguseList = imguseMapper.selectBySptinr(sptinr);
            imgInvmod.setImguseList(imguseList);
        }
        if (StringUtils.isNotEmpty(ownref)) {
            List<InvlstVo> oldInvlst1 = invuseMapper.selectByOwnref(ownref);
            oldInvlst.addAll(oldInvlst1);
            List<InvlstVo> oldInvlst2 = invuseMapper.selectBySelfSkip(ownref);
            oldInvlst.addAll(oldInvlst2);
            if (CollectionUtils.isNotEmpty(oldInvlst)) {
                imgInvmod.setOldInvlst(oldInvlst);
            }
            List<Imguse> oldImguseList1 = imguseMapper.selectByOwnref(ownref);
            oldImguseList.addAll(oldImguseList1);
            //获取自开自议寄单影像
            List<Imguse> oldImguseList2 = imguseMapper.selectBySelfownref(ownref);
            oldImguseList1.addAll(oldImguseList2);
            if (CollectionUtils.isNotEmpty(oldImguseList)) {
                imgInvmod.setOldImguseList(oldImguseList);
            }
        }
        return imgInvmod;
    }

    /**
     * @Description:买方单据付款发票影像初始化
     */
    public ImgInvmod initImgInvForbdtset(String ownref) {
        ImgInvmod imgInvmod = new ImgInvmod();
        List<InvlstVo> invlst = new ArrayList<>();
        List<Imguse> oldImguseList = new ArrayList<>();
        //获取跳过核验开关
        String isSkipped = this.isSkipped();
        imgInvmod.setIsSkipped(isSkipped);
        //从来报中获取影像列表和发票列表
        List<InvlstVo> invlst1 = invuseMapper.selectInvlstByOwnref(ownref);
        invlst.addAll(invlst1);
        //获取自开自议的发票
        List<InvlstVo> invlst2 = invuseMapper.selectInvlsBySelfownref(ownref);
        invlst.addAll(invlst2);
        if (CollectionUtils.isNotEmpty(invlst)) {
            imgInvmod.setInvlst(invlst);
            imgInvmod.getPageTable().setTotal(invlst.size());
            int endIndex = imgInvmod.getPageTable().getPageSize() > invlst.size() ? invlst.size() : imgInvmod.getPageTable().getPageSize();
            imgInvmod.getPageTable().setTableData(invlst.subList(0, endIndex));
        }

        if (StringUtils.isNotEmpty(ownref)) {
            List<Imguse> oldImguseList1 = imguseMapper.selectByOwnref(ownref);
            oldImguseList.addAll(oldImguseList1);
            //获取自开自议寄单影像
            List<Imguse> oldImguseList2 = imguseMapper.selectBySelfownref(ownref);
            oldImguseList1.addAll(oldImguseList2);
            if (CollectionUtils.isNotEmpty(oldImguseList)) {
                imgInvmod.setOldImguseList(oldImguseList);
            }
        }
        return imgInvmod;
    }

    /**
     * @Description:发票修改交易初始化
     */
    public ImgInvmod initForInv(String ownref) {
        ImgInvmod imgInvmod = new ImgInvmod();
        //获取跳过核验开关
        String isSkipped = this.isSkipped();
        imgInvmod.setIsSkipped(isSkipped);
        if (StringUtils.isNotEmpty(ownref)) {
            //根据ownref和是否跳过核验
//            List<InvlstVo> invlst = invuseMapper.selectByOwnrefSkipped(ownref);
//            if (!MdaUtils.isEmpty(invlst)) {
//                imgInvmod.setInvlst(invlst);
//            }
            List<InvlstVo> oldInvlst = invuseMapper.selectByOwnref(ownref);
            if (!MdaUtils.isEmpty(oldInvlst)) {
                imgInvmod.setOldInvlst(oldInvlst);
            }
            List<Imguse> oldImguseList = imguseMapper.selectByOwnref(ownref);
            if (!MdaUtils.isEmpty(oldImguseList)) {
                imgInvmod.setOldImguseList(oldImguseList);
            }
        }
        return imgInvmod;
    }

    /**
     * @Description:BDTINV发票影像初始化
     */
    public ImgInvmod initForBdtinv(String ownref) {
        ImgInvmod imgInvmod = new ImgInvmod();
        List<InvlstVo> oldInvlst = new ArrayList<>();
        List<Imguse> oldImguseList = new ArrayList<>();
        //获取跳过核验开关
        String isSkipped = this.isSkipped();
        imgInvmod.setIsSkipped(isSkipped);
        if (StringUtils.isNotEmpty(ownref)) {
            List<InvlstVo> oldInvlst1 = invuseMapper.selectByOwnref(ownref);
            oldInvlst.addAll(oldInvlst1);
            List<InvlstVo> oldInvlst2 = invuseMapper.selectBySelfSkip(ownref);
            oldInvlst.addAll(oldInvlst2);
            if (CollectionUtils.isNotEmpty(oldInvlst)) {
                imgInvmod.setOldInvlst(oldInvlst);
            }
            List<Imguse> oldImguseList1 = imguseMapper.selectByOwnref(ownref);
            oldImguseList.addAll(oldImguseList1);
            //获取自开自议寄单影像
            List<Imguse> oldImguseList2 = imguseMapper.selectBySelfownref(ownref);
            oldImguseList1.addAll(oldImguseList2);
            if (CollectionUtils.isNotEmpty(oldImguseList)) {
                imgInvmod.setOldImguseList(oldImguseList);
            }
        }
        return imgInvmod;
    }

    public ImgInvmod initImg(String ownref, String sptinr) {
        ImgInvmod imgInvmod = new ImgInvmod();
        //从来报中获取影像列表和发票列表
        if (StringUtils.isNotEmpty(sptinr)) {
            List<Imguse> imguseList = imguseMapper.selectBySptinr(sptinr);
            imgInvmod.setImguseList(imguseList);
        }
        if (StringUtils.isNotEmpty(ownref)) {
            List<Imguse> oldImguseList = imguseMapper.selectByOwnref(ownref);
            if (!MdaUtils.isEmpty(oldImguseList)) {
                imgInvmod.setOldImguseList(oldImguseList);
            }
        }
        return imgInvmod;
    }

    /**
     * source:bftinv.0002.script
     */
    public void updateFZCInv(ImgInvmod imgInvmod) {
        //替换列表
        List<String> repList = new ArrayList<>();
        //忽略列表
        List<String> ignList = new ArrayList<>();
        //未操作列表
        List<String> undList = new ArrayList<>();
        String tmp;
        String hantyp;
//        String tmphantyp = NULLSTR;
//        Date tmpdat = null;
//        String tmpusr = NULLSTR;
        Invuse invuse;
        if (MdaUtils.compareTo(imgInvmod.getInvrep().getInvfunc(), "X") == 0) {
            for (int i = 0; i < imgInvmod.getInvrep().getZcinvlst().size(); i++) {
                if (MdaUtils.compareTo(imgInvmod.getInvrep().getZcinvlst().get(i).getRepflg(), "X") == 0) {
                    repList.add(imgInvmod.getInvrep().getZcinvlst().get(i).getInvcod() + TAB + imgInvmod.getInvrep().getZcinvlst().get(i).getInvnum());
                }
                if (MdaUtils.compareTo(imgInvmod.getInvrep().getZcinvlst().get(i).getIgnflg(), "X") == 0) {
                    ignList.add(imgInvmod.getInvrep().getZcinvlst().get(i).getInvcod() + TAB + imgInvmod.getInvrep().getZcinvlst().get(i).getInvnum());
                }
            }
            for (int i = 0; i < imgInvmod.getInvrep().getFzcinvlst().size(); i++) {
                if (MdaUtils.compareTo(imgInvmod.getInvrep().getFzcinvlst().get(i).getRepflg(), "X") == 0) {
                    repList.add(imgInvmod.getInvrep().getFzcinvlst().get(i).getInvcod() + TAB + imgInvmod.getInvrep().getFzcinvlst().get(i).getInvnum());
                }
                if (MdaUtils.compareTo(imgInvmod.getInvrep().getFzcinvlst().get(i).getIgnflg(), "X") == 0) {
                    ignList.add(imgInvmod.getInvrep().getFzcinvlst().get(i).getInvcod() + TAB + imgInvmod.getInvrep().getFzcinvlst().get(i).getInvnum());
                }
                if (MdaUtils.isEmpty(imgInvmod.getInvrep().getFzcinvlst().get(i).getRepflg()) && MdaUtils.isEmpty(imgInvmod.getInvrep().getFzcinvlst().get(i).getIgnflg())) {
                    undList.add(imgInvmod.getInvrep().getFzcinvlst().get(i).getInvcod() + TAB + imgInvmod.getInvrep().getFzcinvlst().get(i).getInvnum());
                }
            }
            for (int i = 0; i < imgInvmod.getInvrep().getCfinvlst().size(); i++) {
                if (MdaUtils.compareTo(imgInvmod.getInvrep().getCfinvlst().get(i).getRepflg(), "X") == 0) {
                    repList.add(imgInvmod.getInvrep().getCfinvlst().get(i).getInvcod() + TAB + imgInvmod.getInvrep().getCfinvlst().get(i).getInvnum());
                }
                if (MdaUtils.compareTo(imgInvmod.getInvrep().getCfinvlst().get(i).getIgnflg(), "X") == 0) {
                    ignList.add(imgInvmod.getInvrep().getCfinvlst().get(i).getInvcod() + TAB + imgInvmod.getInvrep().getCfinvlst().get(i).getInvnum());
                }
                if (MdaUtils.isEmpty(imgInvmod.getInvrep().getCfinvlst().get(i).getRepflg()) && MdaUtils.isEmpty(imgInvmod.getInvrep().getCfinvlst().get(i).getIgnflg())) {
                    undList.add(imgInvmod.getInvrep().getCfinvlst().get(i).getInvcod() + TAB + imgInvmod.getInvrep().getCfinvlst().get(i).getInvnum());
                }
            }
            for (int i = 0; i < imgInvmod.getInvrep().getInvtbl().size(); i++) {
                tmp = imgInvmod.getInvrep().getInvtbl().get(i).getInvcod() + TAB + imgInvmod.getInvrep().getInvtbl().get(i).getInvnum();
                hantyp = "";
                //A替换，B忽略，C未处理
                if (repList.contains(tmp)) {
                    hantyp = "A";
                } else {
                    if (ignList.contains(tmp)) {
                        hantyp = "B";
                    } else {
                        if (undList.contains(tmp)) {
                            hantyp = "C";
                        }
                    }
                }
                invuse = imgInvmod.getInvrep().getInvtbl().get(i);
                if ("B".equals(hantyp) || "C".equals(hantyp)) {
                    try {
                        transactionHelpService.addBimdataUpdateModel(invuse);
                        invuse.setHantyp(hantyp);
                        invuse.setHandat(Dates.today());
//                        invuse.setHanusr(SettleContext.getUserSession().getUsr().getExtkey());
                        invuseMapper.updateByPrimaryKey(invuse);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else if ("A".equals(hantyp)) {
                    try {
                        //替换的发票置为无效
                        transactionHelpService.addBimdataUpdateModel(invuse);
                        invuse.setHantyp(hantyp);
                        invuse.setHandat(Dates.today());
                        invuse.setOwnref("");
                        invuse.setPntref("");
                        invuse.setValflg("INVD");
//                        invuse.setHanusr(SettleContext.getUserSession().getUsr().getExtkey());
                        invuseMapper.updateByPrimaryKey(invuse);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public ImgInvmod queryIsSkipped() {
        ImgInvmod imgInvmod = new ImgInvmod();
        //获取跳过核验开关
        String isSkipped = this.isSkipped();
        imgInvmod.setIsSkipped(isSkipped);
        return imgInvmod;
    }

    private String isSkipped() {
        Offcfg offcfg = new Offcfg();
        offcfg.setSwitchcode("FPHY");
        offcfg = offcfgMapper.selectBySwitchcode(offcfg);
        if (!MdaUtils.isEmpty(offcfg) && "Y".equals(offcfg.getSwitchflg())){
            return "Y";
        }
        return "N";
    }
}
