package nc.bs.uapbd.task;

import nc.bs.dao.BaseDAO;
import nc.bs.logging.Logger;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.bs.trade.business.HYSuperDMO;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.sscivm.ivsale.IVApplicationHeadVO;
import nc.vo.sscivm.ivsale.IVApplogVO;
import uap.mw.trans.TransactionFactory;

import java.util.ArrayList;
import java.util.HashMap;

public class InvoiceTaskPlugin implements IBackgroundWorkPlugin{

    @Override
    public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
        // TODO Auto-generated method stub
        Logger.error("---start----任务开始运行--");
        try {
            BaseDAO dao = new BaseDAO();
            String sql = " SELECT sscivm_invoice.pk_invoice,so_saleinvoice.csaleinvoiceid,so_saleinvoice.vbillcode billcode, sscivm_invoice.fphm, " +
                    "sscivm_invoice.Gmfmc, sscivm_invoice.Jshj, sscivm_invoice.viewurl, sscivm_invoice.billno " +
                    " FROM sscivm_invoice " +
                    " left join sscivm_invoice_relation on sscivm_invoice_relation.PK_INVOICE = sscivm_invoice.pk_invoice " +
                    " left join so_saleinvoice on so_saleinvoice.csaleinvoiceid = sscivm_invoice_relation.billid " +
                    " left join sscivm_ivapplog on sscivm_ivapplog.fphm = sscivm_invoice.fphm " +
                    " where " +
                    " sscivm_invoice_relation.billtype = '32' " +
                    " and (so_saleinvoice.vdef20 <> 'Y' OR so_saleinvoice.vdef13 IS NULL OR so_saleinvoice.vdef13='~' ) " +
                    " and sscivm_invoice.dr = 0 " +
                    " and sscivm_ivapplog.kpzt = 2 ";
//						 + "         so_saleinvoice.vbillcode = 'SI2024070400000738' ";
            ArrayList<HashMap<String, Object>> al = (ArrayList<HashMap<String, Object>>)dao.executeQuery(sql, new MapListProcessor());
            Logger.error("---alData------"+al);
            if(al.size() > 0) {
                for(HashMap<String, Object> sscivm_invoice : al) {
                    Object pk_invoice = sscivm_invoice.get("pk_invoice"); //bip发票主键 0001A1100000004Y7DWZ
                    Object csaleinvoiceid = sscivm_invoice.get("csaleinvoiceid"); //销售发票id
                    Object billcode = sscivm_invoice.get("billcode"); //销售发票编码
                    Object billno = sscivm_invoice.get("billno"); //bip单据编码编码
                    Object fphm = sscivm_invoice.get("fphm");
                    Object Gmfmc = sscivm_invoice.get("Gmfmc");
                    Object Jshj = sscivm_invoice.get("Jshj");
                    Object viewurl = sscivm_invoice.get("viewurl");
                    String ctcode="";
//                    HYSuperDMO dmo = new HYSuperDMO();
//                    IVApplogVO[] ivApplogVO=(IVApplogVO[]) dmo.queryByWhereClause(IVApplogVO.class, "fphm='"+fphm+"' and dr=0 ");
//                    Logger.error("---ivApplogVO------"+ivApplogVO);
//                    if(ivApplogVO!=null&&ivApplogVO.length>0) {
//                        ivApplogVO[0].getLyid();//开票申请单主键
//                        IVApplicationHeadVO ivApplicationHeadVO =(IVApplicationHeadVO) dmo.queryByPrimaryKey(IVApplicationHeadVO.class, ivApplogVO[0].getLyid());
//                        ctcode=ivApplicationHeadVO.getDef2();//合同号
//                    }
//                    Object url = ctcode+"_"+Gmfmc+"_"+fphm+"_"+Jshj + ".pdf";
                    /**
                     * 回写逻辑：
                     * 1.修改发票关联关系单据编码
                     * 2.ERP销售发票回传，根据销售发票id，回写发票号和单据编号
                     * 3.根据销售发票id 回写下游应收单表头表体发票号
                     * 4.查询销售发票下游单据，如果有将下游单据表体字段【来源单据号】更新成新的发票号
                     */
                    //手动开启事务
                    TransactionFactory.getTMProxy().begin(3,0);
                    //修改发票信息单据号
                    String sscivmSql = "UPDATE sscivm_invoice set billno = '" + fphm +"' where pk_invoice = '"+ pk_invoice +"'";
                    dao.executeUpdate(sscivmSql);
                    Logger.error("---sscivmSqlExecute------"+sscivmSql);
                    //修改发票关联关系单据编码
//                    String sirSql = "UPDATE sscivm_invoice_relation set billno = '" + fphm +"' where pk_invoice = '"+ pk_invoice +"' and billid = '"+ csaleinvoiceid +"'";
//                    dao.executeUpdate(sirSql);
//                    Logger.error("---sirSqlExecute------"+sirSql);
                    //ERP销售发票回传，根据销售发票id，回写发票号和单据编号
//                    String sSaleSql = "UPDATE so_saleinvoice set vdef20 = 'Y' , vdef13 = '" + fphm + "', vbillcode = '" + fphm +"'  where csaleinvoiceid = '" + csaleinvoiceid + "'";
//                    String sSaleSql = "UPDATE so_saleinvoice set vdef20 = 'Y' , vdef13 = '" + fphm + "'  where csaleinvoiceid = '" + csaleinvoiceid + "'";

//                    dao.executeUpdate(sSaleSql);
//                    Logger.error("---sSaleSqlExecute------"+sSaleSql);
                    //根据销售发票id 回写下游应收单表头发票号
                    String recBillSql =  "MERGE INTO ar_recbill a USING (select DISTINCT pk_recbill,top_billid,top_billtype from ar_recitem) b ON ( a.pk_recbill = b.pk_recbill AND b.top_billid = '" + csaleinvoiceid + "' AND b.top_billtype = '32' ) \n" +
                            "WHEN MATCHED THEN\n" +
                            "UPDATE \n" +
                            "SET a.invoiceno = '" + fphm + "'";
                    dao.executeUpdate(recBillSql);
                    Logger.error("---recBillSqlExecute------"+recBillSql);
                    //根据销售发票id 回写下游应收单表体发票号
                    String recItemSql =  "UPDATE ar_recitem \n" +
                            "SET invoiceno = '" + fphm + "' \n" +
                            "WHERE\n" +
                            "top_billid = '" + csaleinvoiceid + "'  \n" +
                            "AND top_billtype = '32'";
                    dao.executeUpdate(recItemSql);
                    Logger.error("---recItemSqlExecute------"+recItemSql);
                    //查询销售发票下游单据，如果有将下游单据表体字段【来源单据号】更新成新的发票号
//                    String saleOutSql = "UPDATE ic_saleout_b \n" +
//                            "SET vsourcebillcode = '"+ fphm +"' \n" +
//                            "WHERE\n" +
//                            "csourcetype = '32' \n" +
//                            "AND csourcebillhid = '" + csaleinvoiceid +"'";
//                    dao.executeUpdate(saleOutSql);
//                    Logger.error("---saleOutSqlExecute------"+saleOutSql);
                    //正常结束
                    TransactionFactory.getTMProxy().end(null);
                }


            }
        } catch(Exception e) {
            e.printStackTrace();
            //异常回滚
            TransactionFactory.getTMProxy().end(e);
            Logger.error("writeBack Error: ",e);
        }
        return null;
    }




}
