package com.engine.htxc.tycgrwd.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.itcglc.cmd.ItcglcProjectInfoCmd;
import com.engine.htxc.tycgrwd.judgeTheProcessInTransit.JudgeTheProcessInTransit;
import com.engine.htxc.tycgrwd.judgeTheProcessInTransit.WhetherInTransit;
import com.engine.htxc.tycgrwd.service.TycgrwdService;
import com.engine.htxc.tycgrwd.service.cmd.TycgrwdFreeNodeCmd;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;

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

public class TycgrwdServiceImpl extends  Service implements TycgrwdService{

    @Override
    public Map<String, Object> getCgrwdProjectInfo(User user, Map<String, Object> params) {
        return commandExecutor.execute(new ItcglcProjectInfoCmd(user,params));
    }

    @Override
    public ArrayList<HashMap<String, Object>>  getCgxm(String id) {
        PropBean pb = new PropBean();
        String ufSql = "select * from uf_cgxq where zt = 1 and CGXMLX = "+id;
        RecordSet rs = new RecordSet();
        RecordSet dtRs = new RecordSet();
        RecordSet deptRs = new RecordSet();
        RecordSet hrmRs = new RecordSet();
        RecordSet cgzdRs = new RecordSet();
        rs.execute(ufSql);
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        while(rs.next()){
            String xmid = rs.getString("id");//采购项目id
//            手动发起采购任务单流程，选择项目名称，带出采购项目到明细表
//            采购项目需要完成通用采购申请流程（即在采购申请流程中为办结），并且没有在途的和已办结的通用采购任务单流程。
//            或者判断，采购项在采购任务单流程中被终止的。
            String tycgsqTable = pb.getPropName("tycgsq_table");
            String tycgrwdTablename = pb.getPropName("tycgrwd_tablename");
            JudgeTheProcessInTransit judge = new WhetherInTransit();
            //这一步是用来判断这个采购项目是不是已经做过采购项目
            boolean complete = judge.isComplete(tycgsqTable, xmid);
            if(complete){
                continue;
            }
            String sql = "select requestid from "+tycgrwdTablename +" where id = (select mainid from "+tycgrwdTablename+"_dt5 where xmid = "+xmid +")";
            dtRs.execute(sql);
            if(dtRs.next()){
                String requestid = dtRs.getString("requestid");
                //过滤已经办结的
                if(judge.isComplete(requestid)){
                    continue;
                }
                HashMap<String, Object> map = new HashMap<>();
                map.put("bz",rs.getString("bz"));
                String cgzdSql = "select * from  uf_cgzdsjzd where id = " + rs.getString("cgzd") ;
                cgzdRs.execute(cgzdSql);
                String cgzd = "";
                if(cgzdRs.next()){
                    cgzd =cgzdRs.getString("cgzdmc");
                }
                map.put("cgzd",cgzd);
                map.put("cgtk",rs.getString("cgtk"));
                map.put("xmbh",rs.getString("xmbh"));
                map.put("xmlb",rs.getString("xmlb"));
                map.put("cgfs",rs.getString("cgfs"));
                //预算部门改为浏览按钮格式
                ArrayList<HashMap<String, String>> deptList = new ArrayList<>();
                String deprtSql = "select * from  HRMDEPARTMENT where id in( "+rs.getString("ysssbm") + ")";
                deptRs.execute(deprtSql);
                while(deptRs.next()){
                    HashMap<String, String> dept = new HashMap<>();
                    dept.put("id",deptRs.getString("id"));
                    dept.put("name",deptRs.getString("departmentname"));
                    deptList.add(dept);
                }
                map.put("ysssbm",deptList);
                map.put("ysje",rs.getString("ysje"));
                map.put("cgnr",rs.getString("cgnr"));
                map.put("gys",rs.getString("gys"));
                map.put("cgsl",rs.getString("cgsl"));
                //采购负责人改为浏览按钮格式
                ArrayList<HashMap<String, String>> hrmList = new ArrayList<>();
                String hrmSql = "select * from hrmresource where id in ( "+rs.getString("xmfzr") + ") and status = 1";
                hrmRs.execute(hrmSql);
                while(hrmRs.next()){
                    HashMap<String, String> hrm = new HashMap<>();
                    hrm.put("id",hrmRs.getString("id"));
                    hrm.put("name",hrmRs.getString("lastname"));
                    hrmList.add(hrm);
                }
                map.put("xmfzr",hrmList);
                map.put("xmmc",rs.getString("xmmc"));
                map.put("id",rs.getString("id"));
                list.add(map);
            }else{
                HashMap<String, Object> map = new HashMap<>();
                map.put("bz",rs.getString("bz"));
                String cgzdSql = "select * from  uf_cgzdsjzd where id = " + rs.getString("cgzd") ;
                cgzdRs.execute(cgzdSql);
                String cgzd = "";
                if(cgzdRs.next()){
                    cgzd =cgzdRs.getString("cgzdmc");
                }
                map.put("cgzd",cgzd);
                map.put("cgtk",rs.getString("cgtk"));
                map.put("xmbh",rs.getString("xmbh"));
                map.put("xmlb",rs.getString("xmlb"));
                map.put("cgfs",rs.getString("cgfs"));
                //预算部门改为浏览按钮格式
                ArrayList<HashMap<String, String>> deptList = new ArrayList<>();
                String deprtSql = "select * from  HRMDEPARTMENT where id in( "+rs.getString("ysssbm") + ")";
                deptRs.execute(deprtSql);
                while(deptRs.next()){
                    HashMap<String, String> dept = new HashMap<>();
                    dept.put("id",deptRs.getString("id"));
                    dept.put("name",deptRs.getString("departmentname"));
                    deptList.add(dept);
                }
                map.put("ysssbm",deptList);
                map.put("ysje",rs.getString("ysje"));
                map.put("cgnr",rs.getString("cgnr"));
                map.put("gys",rs.getString("gys"));
                map.put("cgsl",rs.getString("cgsl"));
                //采购负责人改为浏览按钮格式
                ArrayList<HashMap<String, String>> hrmList = new ArrayList<>();
                String hrmSql = "select * from hrmresource where id in ( "+rs.getString("xmfzr") + ") and status = 1";
                hrmRs.execute(hrmSql);
                while(hrmRs.next()){
                    HashMap<String, String> hrm = new HashMap<>();
                    hrm.put("id",hrmRs.getString("id"));
                    hrm.put("name",hrmRs.getString("lastname"));
                    hrmList.add(hrm);
                }
                map.put("xmfzr",hrmList);
                map.put("xmmc",rs.getString("xmmc"));
                map.put("id",rs.getString("id"));
                list.add(map);
            }


        }
        return list;
    }
    @Override
    public Map<String, Object> createFreeNode(User user, Map<String, Object> params) {
        return commandExecutor.execute(new TycgrwdFreeNodeCmd(user,params));
    }

    @Override
    public ArrayList<HashMap<String, Object>> getRequestid(String xmids) {
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        ArrayList<String> requestids = new ArrayList<>();
        String tycgsqTable = pb.getPropName("tycgsq_table");
        if(StringUtils.isNotBlank(xmids)){
            String[] split = xmids.split(",");
            for (String xmid:split) {
                String sql = "select * from  (select  rownum num,  a.* from "+tycgsqTable+" a  where id = (select mainid from "+tycgsqTable+"_dt4 where xmid = "+xmid+") order by id desc ) where num =1";
                rs.execute(sql);
                if(rs.next()){
                    HashMap<String, Object> map = new HashMap<>();
                    String lcbt = rs.getString("lcbt");
                    String requestid = rs.getString("requestid");
                    String lcbh = rs.getString("lcbh");
                    if(!requestids.contains(requestid)){
                        requestids.add(requestid);
                        map.put("lcbt",lcbt);
                        map.put("requestid",requestid);
                        map.put("lcbh",lcbh);
                        list.add(map);
                    }
                }
            }

        }


        return list;
    }

}
