package com.engine.htsc.itxqlc.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.itxqlc.service.ItDemandApplyService;
import com.engine.htsc.itxqlc.util.GetSupDepidUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ItDemandApplyServiceImpl extends Service implements ItDemandApplyService {
    GetSupDepidUtil getSupDepidUtil = new GetSupDepidUtil();

    @Override
    public Map<String, Object> getOperationRecords(String nodeid) {
        Map<String,Object> datas = new HashMap<>();
        try {
            RecordSet rs = new RecordSet();
            List<String> list = new ArrayList<>();
            rs.executeQuery("select operator from workflow_requestlog where nodeid = ?",nodeid);
            while (rs.next()){
                String operator = Util.null2String(rs.getString("operator"));
                list.add(operator);
            }
            datas.put("operator",list.stream().distinct().collect(Collectors.toList()).toString());

        }catch (Exception e){
            e.printStackTrace();
        }
        return datas;
    }

    @Override
    public Map<String, Object> detailSummary(Map<String, Object> params) {
        Map<String,Object> datas = new HashMap<>();
        try{
            RecordSet rs = new RecordSet();
            String tablename = "formtable_main_" + Util.null2String(params.get("formid"));
            String detailname = tablename +"_dt4";
            String requestid = Util.null2String(params.get("requestid"));
            String xqpgr = Util.null2String(params.get("xqpgr"));
            String xmfzr = Util.null2String(params.get("xmfzr"));
            String xqsm = Util.null2String(params.get("xqsm"));
            String id = "";
            rs.executeQuery("select id from "+tablename+" where requestid = ?",requestid);
            if (rs.next()){
                id = Util.null2String(rs.getString("id"));
            }
            String sql = "insert into "+detailname+"(mainid,xqpgr,xmfzr,xqsm) " +
                    " values(?,?,?,?)";
            rs.executeUpdate(sql,new Object[]{id,xqpgr,xmfzr,xqsm});

        }catch (Exception e){
            e.printStackTrace();
        }
        return datas;
    }

    @Override
    public Map<String, Object> dealWithPreson(String deptid) {
        Map<String,Object> datas = new HashMap<>();
        try {
            RecordSet rs = new RecordSet();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            List<String> noticeReadList = new ArrayList<>(); //通知阅读人集合
            noticeReadList.add("22801");
            noticeReadList.add("24003");//1、所有部门都需添加 黄寅001119,刘钰 003278
            List<String> demandReceiveList = new ArrayList<>(); //需求接收人集合
            rs.executeQuery("select deptTypeCode,deptDepth from hrmdepartmentdefined where deptid = ?",deptid);
            if (rs.next()) {
                String deptTypeCode = Util.null2String(rs.getString("deptTypeCode"));
                int deptDepth = Util.getIntValue(rs.getString("deptDepth"));
                //1.部门维度判断
                switch (deptTypeCode) {
                    case "JGLX01": case  "JGLX02":  //总部部门
                        //HTO-9003迭代需求
                        deptid = (deptDepth == 2 ?  getSupDepidUtil.getSupGeneral(deptid) : deptid);
                        break;
                    case "JGLX04": case "JGLX09":  //总部分公司部门
                        //所有分公司部门默认 安博 331
                        demandReceiveList.add("331");
                        break;
                    case "JGLX08": case "JGLX11": case "JGLX15": case "JGLX17": case "JGLX18":  //子公司部门
                        noticeReadList.add("23550");//所有子公司加上 孟德龙 003321
                        deptid = (deptDepth == 1 ? getSupDepidUtil.getSupGeneral(deptid) : getSupDepidUtil.getSupSpecial(deptid));
                        break;
                }
            }

            rs.executeQuery("select xqjsr,tzydr from uf_itxqgw where ngbm = ?",deptid);
            if (rs.next()) {
                String xqjsr = Util.null2String(rs.getString("xqjsr"));
                String tzydr = Util.null2String(rs.getString("tzydr"));
                demandReceiveList.add(xqjsr);
                noticeReadList.add(tzydr);
            }

            //2.需求接收人
            //3.通知阅读人
            String demandReceivePersons = StringUtils.join(demandReceiveList,",");
            String noticeReadPersons = StringUtils.join(noticeReadList,",");
            if (!StringUtils.isNotBlank(demandReceivePersons)){
                //若在表中查不到匹配的人，需求接收人赋值丁兴麒010396
                demandReceivePersons = "2152";
            }
            datas.put("demandReceivePersons",demandReceivePersons);
            datas.put("noticeReadPersons",noticeReadPersons);

        }catch (Exception e){
            e.printStackTrace();
        }
        return datas;
    }

    @Override
    public Map<String, Object> assignSuperiorDept(String deptid) {
        Map<String,Object> datas = new HashMap<>();
        try {
            RecordSet rs = new RecordSet();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            rs.executeQuery("select DeptTypeCode,deptDepth,DeptTypeName,deptScaleLevelCode,parentsalecode " +
                    " from  hrmdepartmentdefined where deptid = ?",deptid);
            if (rs.next()) {
                String deptTypeCode = Util.null2String(rs.getString("DeptTypeCode"));
                int deptDepth = Util.getIntValue(rs.getString("deptDepth"));
                String deptScaleLevelCode = Util.null2String(rs.getString("deptScaleLevelCode"));
                String parentsalecode = Util.null2String(rs.getString("parentsalecode"));


                if ("JGLX01".equals(deptTypeCode) && deptDepth == 2) { //总部部门且部门深度2
                    deptid = getSupDepidUtil.getSupGeneral(deptid);
                }else if ("JGLX02".equals(deptTypeCode)) { //总部直属营业部（即私行中心）
                    deptid = getSupDepidUtil.getSupGeneral(deptid);
                }else if("006".equals(deptScaleLevelCode) && StringUtils.isNotBlank(parentsalecode)){
                    deptid = getSupDepidUtil.getSecondaryDept(parentsalecode);
                }
            }
            String departmentName = departmentComInfo.getDepartmentName(deptid);
            datas.put("deptid",deptid);
            datas.put("departmentName",departmentName);

        }catch (Exception e) {
            e.printStackTrace();
        }


        return datas;
    }
}
