package weaver.interfaces.htxc.tycgsq.action;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Objects;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-09-23 10:05
 * @Description:这个action这个action是用来办结更新建模明细表
 * @ModifyLog:
 **/
public class UpdateDtAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        //请求ID
        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 id = rs.getString("id");
            String itlxsq = rs.getString("itlxsq");
            String zbcysxlc = rs.getString("zbcysxlc");
            String lxlx = rs.getString("lxlx");
            String lxly = rs.getString("lxly");
            switch (lxlx){
                case "0":updateDt(tableName+"_dt5",id,zbcysxlc,itlxsq,lxly,formid);break;
                case "1":updateDt(tableName+"_dt9",id,zbcysxlc,itlxsq,lxly,formid);break;
                default:updateDt(tableName+"_dt10",id,zbcysxlc,itlxsq,lxly,formid);break;
            }
        }
        return Action.SUCCESS;
    }

    public void updateDt(String dt,String mainid,String zbcysxlc,String itlxsq,String lxly,int formid){
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String format = now.format(formatter);
        RecordSetTrans updateRs = new RecordSetTrans();
        String dt5Sql = "select * from "+dt+" where mainid =?";
        rs.executeQuery(dt5Sql,mainid);
        while(rs.next()){
            String id = rs.getString("id");
            ArrayList<String> list = new ArrayList<>();
            //上期项目
            list.add(rs.getString("sqxm"));
            //采购类型
            list.add(rs.getString("cglx"));
            //项目类型
            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(rs.getString("ysje"));
            //预算部门
            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("1");
            //立项流程
            list.add(itlxsq);
            //相关流程
            list.add(zbcysxlc);
            //所属中心
            list.add(rs.getString("sszx"));
            //所属团队
            list.add(rs.getString("sstd"));
            //采购单编号
            String cgdbh = "";
            if(Objects.equals("0",lxly)){
                 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 = ?";
                rs1.executeQuery(cgfsSql,formid,Util.null2String(rs.getString("cgfs")));
                rs1.next();
                 cgdbh = Util.null2String(rs1.getString("selectname"))+format+generateRandomArray(6);
                list.add(cgdbh);
            }
            String jmid = rs.getString("jmid");
            list.add(jmid);
            //更新建模表
            String updateSql = "update uf_cgxq set sqxm=?,xmlb=?,xmlx=?,cgfs=?,gys=?,cgnr=?,xmfzr=?,ysje=?,ysssbm=?,cgsl=?,bz=?,fj=?,cgzd1=?," +
                    "cgtk1=?,zt=?,lxlc=?,xglc=?,sszx=?,sstd=?,xmbh=? where id = ?";
//            updateRs.executeUpdate(updateSql,list);
            //更新流程明细表
            String dtSql = "update "+dt+" set cgdbh = ? where id = ?";
            try {
                 updateRs.executeUpdate(updateSql, list);
                 updateRs.executeUpdate(dtSql, cgdbh, id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    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);

    }
}
