package weaver.interfaces.htxc.tycgsq.action;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-09-21 16:02
 * @Description:拟稿节点提交后将明细表保存到采购项
 * @ModifyLog:
 **/
@Slf4j
public class SaveCgxAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestId = requestInfo.getRequestid();
        //表单名称
        String tableName = requestInfo.getRequestManager().getBillTableName();
        int formid = requestInfo.getRequestManager().getFormid();
//        String requestId = "5915920";
//        String tableName = "formtable_main_493";
//        int formid = -493;
        RecordSet rs = new RecordSet();
        String sql = "select * from "+tableName+" where requestid = ?";
        rs.executeQuery(sql,requestId);
        if(rs.next()){
            //立项类型
            String lxlx = rs.getString("lxlx");
            String mainId = rs.getString("id");
            String ngr = rs.getString("ngr");
            String szbm = rs.getString("szbm");
            String lxly = rs.getString("lxly");
            String itlxsq = rs.getString("itlxsq");
            String zbcysxlc = rs.getString("zbcysxlc");
            String scmxbfzzd = rs.getString("scmxbfzzd");
            //根据立项类型保存明细表
//            if(Objects.equals("0",lxlx)){}else if(){}
            switch (lxlx){
                case "0":createCgxByDt5(tableName+"_dt5",mainId,requestId,lxlx,ngr,szbm,lxly,zbcysxlc,itlxsq,formid,scmxbfzzd);break;
                case "1":createCgxByDt9(tableName+"_dt9",mainId,requestId,lxlx,ngr,szbm,lxly,zbcysxlc,itlxsq,formid,scmxbfzzd);break;
                default: createCgxByDt10(tableName+"_dt10",mainId,requestId,lxlx,ngr,szbm,lxly,zbcysxlc,itlxsq,formid,scmxbfzzd);
            }
        }
//        System.out.println("***********结束*********************");
        return Action.SUCCESS;
    }

    public void createCgxByDt5(String dt,String mainid,String requestid,String lxlx,String ngr,String szbm,String lxly,String zbcysxlc,String itlxsq,int formid,String scmxbfzzd){
        PropBean pb = new PropBean();
        RecordSet rs1 = new RecordSet();
        RecordSet delRs = new RecordSet();
        RecordSet updateRs = new RecordSet();
        String cgsqModeId = pb.getPropName("cgsq_modeid");
        RecordSet rs = new RecordSet();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String format = now.format(formatter);
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format1 = now.format(formatter1);
        LocalTime time = LocalTime.now();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String sql = "select * from "+dt+" where mainid = ?";
        rs.executeQuery(sql,mainid);
        ArrayList<List> params = new ArrayList<>();
        while(rs.next()){
            String jmid = rs.getString("jmid");
            //判断当前的明细jmid是否为空，若为空则表明是新增的明细表，直接插入采购项建模
            if(StringUtils.isBlank(jmid)){
                ArrayList<String> list = new ArrayList<>();

                list.add(requestid);
                list.add(lxlx);
                //上期项目
                list.add(rs.getString("sqxm"));
                //采购类型
                list.add(rs.getString("cglx"));
                //采购单编号
//                if(Objects.equals("0",lxly)){
//                    String cgdbh = Util.null2String(rs.getString("cgdbh"));
//                    list.add(cgdbh);
//                }else{
//                    String cgfsSql ="select * from workflow_selectitem where fieldid = (select id from workflow_billfield where billid = ? and fieldname = 'cgfs' and detailtable like '%dt5' ) and selectvalue = ?";
//                    String cgfs = Util.null2String(rs.getString("cgfs"));
//                    rs1.executeQuery(cgfsSql,formid,Util.null2String(rs.getString("cgfs")));
//                    rs1.next();
//                    String cgdbh = Util.null2String(rs1.getString("selectname"))+format+generateRandomArray(6);
//                    list.add(cgdbh);
//                }
                //项目类型
                list.add(rs.getString("xmlx"));
                //项目开发形式
                list.add(rs.getString("xmkfxs"));
                //采购方式
                list.add(rs.getString("cgfs"));
                //供应商
                list.add(rs.getString("gys"));
                //采购内容
                list.add(rs.getString("cgnr"));
                //采购负责人
                list.add(rs.getString("cgfzr"));
                //预算金额(万元)
                list.add(String.valueOf(Integer.parseInt(rs.getString("ysje"))*10000));
                //预算部门
                list.add(rs.getString("ysbm"));
                //采购数量
                list.add(rs.getString("sl"));
                //备注
                list.add(rs.getString("bz"));
                //附件
                list.add(rs.getString("fj"));
                //采购制度
                list.add(rs.getString("cgzd"));
                //采购条款
                list.add(rs.getString("ejcgzd"));
                //状态
                list.add("0");
                //创建日期
                list.add(format1);
//            //紧急程度
//            list.add("0");
                //创建人
                list.add(ngr);
                //创建部门
                list.add(szbm);
                //来源
                list.add(lxly.equals("0") ? "0":"1");
                //明细表id
                list.add(rs.getString("id"));
                //立项流程
                list.add(itlxsq);
                //相关流程
                list.add(zbcysxlc);
                //所属中心
                list.add(rs.getString("sszx"));
                //所属团队
                list.add(rs.getString("sstd"));

                list.add(cgsqModeId);
                list.add(ngr);
                list.add(format1);
                list.add(time.format(timeFormatter));
                list.add("0");
                params.add(list);
            }else{
                //若jmid不为空，则表明，当前明细表已经存在采购项，则应该通过辅助字段判断当前的采购项是否要删除
                if(StringUtils.isNotBlank(scmxbfzzd)){
                    String[] split = scmxbfzzd.split(",");
                    for (String s : split) {
                        String selectSql = "select * from uf_cgxq where id = ?";
                        delRs.executeQuery(selectSql,s);
                        if(delRs.next()){
                            String delSql = "delete from uf_cgxq where id = ?";
                            boolean execute = delRs.executeUpdate(delSql,new Object[]{s});
//                            System.out.println(execute);
                        }

                    }


                }
            }

        }
        String cgxqsql="insert into uf_cgxq (gllc,xmlx1,sqxm,xmlb,xmlx,xmkfxs,cgfs,gys,cgnr,xmfzr,ysje,ysssbm,cgsl" +
                ",bz,fj,cgzd1,cgtk1,zt,cjrq,cjr,xmjlbm,ly,mxbid,lxlc,xglc,sszx,sstd,formmodeid,modedatacreater," +
                "modedatacreatedate,modedatacreatetime,modedatacreatertype) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        boolean b = rs.executeBatchSql(cgxqsql, params);
//        System.out.println(b);
        //权限重构
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        String rwmodeid = pb.getPropName("cgxm_modeuuid");
        JSONObject jsonObject_rw = pb.getModeInfoByUUID(rwmodeid);
        int modeid = Util.getIntValue(jsonObject_rw.getString("modeid"));
        modeRightInfo.editModeDataShare(1,modeid,Integer.parseInt(mainid));
//            System.out.println("结束");

        //方向插入建模id和采购单编号
        String cgxqSql = "select * from uf_cgxq where gllc = ?";
        ArrayList<String> strings = new ArrayList<>();
        strings.add(requestid);
        rs1.executeQuery(cgxqSql,strings);
        while(rs1.next()){
            String updateSql = "update "+dt+" set jmid = ? ,  xmid=?  where id = ?";
            updateRs.executeUpdate(updateSql,rs1.getString("id"), rs1.getString("id"), rs1.getString("mxbid"));
        }
    }
    public void createCgxByDt9(String dt,String mainid,String requestid,String lxlx,String ngr,String szbm,String lxly,String zbcysxlc,String itlxsq,int formid,String scmxbfzzd){
        PropBean pb = new PropBean();
        RecordSet rs1 = new RecordSet();
        RecordSet delRs = new RecordSet();
        RecordSet updateRs = new RecordSet();
        String cgsqModeId = pb.getPropName("cgsq_modeid");
        RecordSet rs = new RecordSet();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String format = now.format(formatter);
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format1 = now.format(formatter1);
        LocalTime time = LocalTime.now();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String sql = "select * from "+dt+" where mainid = ?";
        rs.executeQuery(sql,mainid);
        ArrayList<List> params = new ArrayList<>();
        while(rs.next()){
            String jmid = rs.getString("jmid");
            //判断当前的明细jmid是否为空，若为空则表明是新增的明细表，直接插入采购项建模
            if(StringUtils.isBlank(jmid)){
                ArrayList<String> list = new ArrayList<>();
                list.add(requestid);
                list.add(lxlx);
                //上期项目
                list.add(rs.getString("sqxm"));
                //采购类型
                list.add(rs.getString("cglx"));
                //采购单编号
//                if(Objects.equals("0",lxly)){
//                    String cgdbh = Util.null2String(rs.getString("cgdbh"));
//                    list.add(cgdbh);
//                }else{
//                    String cgfsSql ="select * from workflow_selectitem where fieldid = (select id from workflow_billfield where billid = ? and fieldname = 'cgfs' and detailtable like '%dt9' ) and selectvalue = ?";
//                    rs1.executeQuery(cgfsSql,formid,Util.null2String(rs.getString("cgfs")));
//                    rs1.next();
//                    String cgdbh = Util.null2String(rs1.getString("selectname"))+format+generateRandomArray(6);
//                    list.add(cgdbh);
//                }
                //项目类型
                list.add(rs.getString("xmlx"));

                //采购方式
                list.add(rs.getString("cgfs"));
                //供应商
                list.add(rs.getString("gys"));
                //采购内容
                list.add(rs.getString("cgnr"));
                //采购负责人
                list.add(rs.getString("cgfzr"));
                //预算金额(万元)
                list.add(String.valueOf(Integer.parseInt(rs.getString("ysje"))*10000));
                //预算部门
                list.add(rs.getString("ysbm"));
                //采购数量
                list.add(rs.getString("sl"));
                //备注
                list.add(rs.getString("bz"));
                //附件
                list.add(rs.getString("fj"));
                //采购制度
                list.add(rs.getString("cgzd"));
                //采购条款
                list.add(rs.getString("ejcgzd"));
                //状态
                list.add("0");
                //创建日期
                list.add(format1);
//            //紧急程度
//            list.add("0");
                //创建人
                list.add(ngr);
                //创建部门
                list.add(szbm);
                //来源
                list.add(lxly.equals("0") ? "0":"1");
                //明细表id
                list.add(rs.getString("id"));
                //立项流程
                list.add(itlxsq);
                //相关流程
                list.add(zbcysxlc);


                list.add(cgsqModeId);
                list.add(ngr);
                list.add(format1);
                list.add(time.format(timeFormatter));
                list.add("0");
                params.add(list);
            }else{
                //若jmid不为空，则表明，当前明细表已经存在采购项，则应该通过辅助字段判断当前的采购项是否要删除
                if(StringUtils.isNotBlank(scmxbfzzd)){
                    String[] split = scmxbfzzd.split(",");
                    for (String s : split) {
                        String selectSql = "select * from uf_cgxq where id = ?";
                        delRs.executeQuery(selectSql,s);
                        if(delRs.next()){
                            String delSql = "delete from uf_cgxq where id = ?";
                            delRs.executeUpdate(delSql,new Object[]{s});
                        }

                    }


                }
            }

        }

        String cgxqsql="insert into uf_cgxq (gllc,xmlx1,sqxm,xmlb,xmlx,cgfs,gys,cgnr,xmfzr,ysje,ysssbm,cgsl" +
                ",bz,fj,cgzd1,cgtk1,zt,cjrq,cjr,xmjlbm,ly,mxbid,lxlc,xglc,formmodeid,modedatacreater," +
                "modedatacreatedate,modedatacreatetime,modedatacreatertype) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                "?,?,?,?,?,?,?,?,?,?,?,?,?)";
        rs.executeBatchSql(cgxqsql,params);
        //权限重构
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        String rwmodeid = pb.getPropName("cgxm_modeuuid");
        JSONObject jsonObject_rw = pb.getModeInfoByUUID(rwmodeid);
        int modeid = Util.getIntValue(jsonObject_rw.getString("modeid"));
        modeRightInfo.editModeDataShare(1,modeid,Integer.parseInt(mainid));
//            System.out.println("结束");

        //方向插入建模id和采购单编号
        String cgxqSql = "select * from uf_cgxq where gllc = ?";
        ArrayList<String> strings = new ArrayList<>();
        strings.add(requestid);
        rs1.executeQuery(cgxqSql,strings);
        while(rs1.next()){
            String updateSql = "update "+dt+" set jmid = ? ,  xmid=?  where id = ?";
            updateRs.executeUpdate(updateSql,rs1.getString("id"), rs1.getString("id"), rs1.getString("mxbid"));
        }
    }
    public void createCgxByDt10(String dt,String mainid,String requestid,String lxlx,String ngr,String szbm,String lxly,String zbcysxlc,String itlxsq,int formid,String scmxbfzzd){
        PropBean pb = new PropBean();
        RecordSet rs1 = new RecordSet();
        RecordSet delRs = new RecordSet();
        RecordSet updateRs = new RecordSet();
        String cgsqModeId = pb.getPropName("cgsq_modeid");
        RecordSet rs = new RecordSet();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String format = now.format(formatter);
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format1 = now.format(formatter1);
        LocalTime time = LocalTime.now();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String sql = "select * from "+dt+" where mainid = ?";
        rs.executeQuery(sql,mainid);
        ArrayList<List> params = new ArrayList<>();
        while(rs.next()){
            String jmid = rs.getString("jmid");
            //判断当前的明细jmid是否为空，若为空则表明是新增的明细表，直接插入采购项建模
            if(StringUtils.isBlank(jmid)){
                ArrayList<String> list = new ArrayList<>();
                list.add(requestid);
                list.add(lxlx);
                //上期项目
                list.add(rs.getString("sqxm"));
                //采购类型
                list.add(rs.getString("cglx"));
                //采购单编号
//                if(Objects.equals("0",lxly)){
//                    String cgdbh = Util.null2String(rs.getString("cgdbh"));
//                    list.add(cgdbh);
//                }else{
//                    String cgfsSql ="select * from workflow_selectitem where fieldid = (select id from workflow_billfield where billid = ? and fieldname = 'cgfs' and detailtable like '%dt10' ) and selectvalue = ?";
//                    rs1.executeQuery(cgfsSql,formid,Util.null2String(rs.getString("cgfs")));
//                    rs1.next();
//                    String cgdbh = Util.null2String(rs1.getString("selectname"))+format+generateRandomArray(6);
//                    list.add(cgdbh);
//                }
                //项目类型
                list.add(rs.getString("xmlx"));

                //采购方式
                list.add(rs.getString("cgfs"));
                //供应商
                list.add(rs.getString("gys"));
                //采购内容
                list.add(rs.getString("cgnr"));
                //采购负责人
                list.add(rs.getString("cgfzr"));
                //预算金额(万元)
                list.add(String.valueOf(Integer.parseInt(rs.getString("ysje"))*10000));
                //预算部门
                list.add(rs.getString("ysbm"));
                //采购数量
                list.add(rs.getString("sl"));
                //备注
                list.add(rs.getString("bz"));
                //附件
                list.add(rs.getString("fj"));
                //采购制度
                list.add(rs.getString("cgzd"));
                //采购条款
                list.add(rs.getString("ejcgzd"));
                //状态
                list.add("0");
                //创建日期
                list.add(format1);
                //紧急程度
//            list.add("0");
                //创建人
                list.add(ngr);
                //创建部门
                list.add(szbm);
                //来源
                list.add(lxly.equals("0") ? "0":"1");
                //明细表id
                list.add(rs.getString("id"));
                //立项流程
                list.add(itlxsq);
                //相关流程
                list.add(zbcysxlc);
                list.add(cgsqModeId);
                list.add(ngr);
                list.add(format1);
                list.add(time.format(timeFormatter));
                list.add("0");
                list.add(rs.getString("xflb"));
                list.add(rs.getString("xmkzj"));
                params.add(list);
            }else{
                //若jmid不为空，则表明，当前明细表已经存在采购项，则应该通过辅助字段判断当前的采购项是否要删除
                if(StringUtils.isNotBlank(scmxbfzzd)){
                    String[] split = scmxbfzzd.split(",");
                    for (String s : split) {
                        String selectSql = "select * from uf_cgxq where id = ?";
                        delRs.executeQuery(selectSql,s);
                        if(delRs.next()){
                            String delSql = "delete from uf_cgxq where id = ?";
                            boolean execute = delRs.executeUpdate(delSql,new Object[]{s});
                        }
                    }
                }
            }

        }
        String cgxqsql="insert into uf_cgxq (gllc,xmlx1,sqxm,xmlb,xmlx,cgfs,gys,cgnr,xmfzr,ysje,ysssbm,cgsl" +
                ",bz,fj,cgzd1,cgtk1,zt,cjrq,cjr,xmjlbm,ly,mxbid,lxlc,xglc,formmodeid,modedatacreater," +
                "modedatacreatedate,modedatacreatetime,modedatacreatertype,xflb,xmkzj) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                "?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        rs.executeBatchSql(cgxqsql, params);
//        System.out.println(b);
//权限重构
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        String rwmodeid = pb.getPropName("cgxm_modeuuid");
        JSONObject jsonObject_rw = pb.getModeInfoByUUID(rwmodeid);
        int modeid = Util.getIntValue(jsonObject_rw.getString("modeid"));
        modeRightInfo.editModeDataShare(1,modeid,Integer.parseInt(mainid));
//            System.out.println("结束");

        //方向插入建模id和采购单编号
        String cgxqSql = "select * from uf_cgxq where gllc = ?";
        ArrayList<String> strings = new ArrayList<>();
        strings.add(requestid);
        rs1.executeQuery(cgxqSql,strings);
        while(rs1.next()){
            String updateSql = "update "+dt+" set jmid = ? ,  xmid=?  where id = ?";
            updateRs.executeUpdate(updateSql,rs1.getString("id"), rs1.getString("id"), rs1.getString("mxbid"));
        }
    }
    public  String generateRandomArray(int num) {
        String chars = "0123456789";

        char[] rands = new char[num];

        for (int i = 0; i < num; i++) {
            int rand = (int) (Math.random() * 10);

            rands[i] = chars.charAt(rand);

        }

        return new String(rands);

    }
}
