package com.engine.htsc.equipment.service;


import com.engine.core.impl.Service;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.text.SimpleDateFormat;
import java.util.*;

public class EquipmentPurchaseService extends Service {

    /**
     * 根据关联流程获取电子设备信息
     * @param gllc
     * @return
     */
    public Map<String, Object> getEquipmentInfoByRequestid(String gllc){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String, Object> resultMap = new HashMap<>();

        try{
            ResourceComInfo rc = new ResourceComInfo();
            DepartmentComInfo dc = new DepartmentComInfo();

            int sqsbsl = 0;
            double sqzjey = 0;
            List<Map<String,Object>> deptAmountMapList = new ArrayList<>();

            //电子设备信息
            List<Map<String,Object>> equipmentInfoMapList = new ArrayList<>();
            String sql = "select * from uf_dzsbxxcx where glsqlc in ("+gllc+")";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                Map<String,Object> equipmentInfoMap = new HashMap<>();
                equipmentInfoMap.put("dzsbxxid",Util.null2String(rs.getString("id")));
                equipmentInfoMap.put("sbmc",Util.null2String(rs.getString("sbmc")));
                equipmentInfoMap.put("xhjpz",Util.null2String(rs.getString("xhjpz")));
                equipmentInfoMap.put("sl",Util.null2String(rs.getString("sl")));
                equipmentInfoMap.put("yt",Util.null2String(rs.getString("yt")));
                equipmentInfoMap.put("cglx",Util.null2String(rs.getString("cglx")));
                equipmentInfoMap.put("dj",Util.null2String(rs.getString("dj")));
                equipmentInfoMap.put("xj",Util.null2String(rs.getString("xj")));

                String sqr = Util.null2String(rs.getString("sqr"));
                equipmentInfoMap.put("sqr",sqr);
                equipmentInfoMap.put("sqrName",rc.getLastname(sqr));
                equipmentInfoMap.put("lxdh",rc.getMobile(sqr));

                String szbm = Util.null2String(rs.getString("szbm"));
                equipmentInfoMap.put("szbm",szbm);
                equipmentInfoMap.put("szbmName",dc.getDepartmentmark(szbm));

                equipmentInfoMapList.add(equipmentInfoMap);

                int sl = Integer.parseInt(Util.null2String(rs.getString("sl")));
                sqsbsl += sl;
                double xj = Util.getDoubleValue(Util.null2String(rs.getString("xj")));
                sqzjey += xj;

                boolean contain = false;
                for(Map<String,Object> deptAmountMap:deptAmountMapList){
                    if(szbm.equals((String)deptAmountMap.get("jgmc"))){
                        double amount = (double)deptAmountMap.get("je");
                        amount += xj;
                        deptAmountMap.put("je",amount);
                        contain = true;
                        break;
                    }
                }
                if(!contain){
                    Map<String,Object> deptAmountMap = new HashMap<>();
                    deptAmountMap.put("jgmc",szbm);
                    deptAmountMap.put("jgmcName",dc.getDepartmentmark(szbm));
                    deptAmountMap.put("je",xj);
                    deptAmountMapList.add(deptAmountMap);
                }
            }
            resultMap.put("sqsbsl",sqsbsl);//申请设备数量
            resultMap.put("sqzjey",sqzjey);//申请总金额
            resultMap.put("jehz",deptAmountMapList);//各单位申请金额汇总
            resultMap.put("dzsb",equipmentInfoMapList);//各单位申请详细信息

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

        return resultMap;
    }

    /**
     * 获得上月办结的全部流程
     * @return
     */
    public Map<String, Object> getAllFinish(){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        Map<String, Object> resultMap = new HashMap<>();

        try{
            String workflowId = pb.getWorkflowIdsByUnNumber("dzsbsq");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar lastMonthFirstDay = Calendar.getInstance();
            lastMonthFirstDay.add(Calendar.MONTH, -1);
            lastMonthFirstDay.set(Calendar.DAY_OF_MONTH, 1);
            String firstDay = sdf.format(lastMonthFirstDay.getTime());
            Calendar lastMonthlastDay = Calendar.getInstance();
            lastMonthlastDay.set(Calendar.DAY_OF_MONTH, 1);
            lastMonthlastDay.add(Calendar.DATE, -1);
            String lastDay = sdf.format(lastMonthlastDay.getTime());

            List<String> requestidList = new ArrayList<>();
            List<Map<String,String>> requestMapList = new ArrayList<>();
            String sql = "select requestid,requestname,requestmark from workflow_requestbase where workflowid = '"+workflowId+"' and currentnodetype = '3' "
                    +"and lastoperatedate >= '"+firstDay+"' and lastoperatedate <= '"+lastDay+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                String requestid = Util.null2String(rs.getString("requestid"));
                String requestname = Util.null2String(rs.getString("requestname"));
                String requestmark = Util.null2String(rs.getString("requestmark"));
                requestidList.add(requestid);
                Map<String,String> requestMap = new HashMap<>();
                requestMap.put("requestid",requestid);
                requestMap.put("requestname",requestname);
                requestMap.put("requestmark",requestmark);
                requestMap.put("showname",requestname+"("+requestmark+")");
                requestMapList.add(requestMap);
            }
            String requestidStr = StringUtils.join(requestidList,",");
            resultMap = getEquipmentInfoByRequestid(requestidStr);
            resultMap.put("gllc",requestMapList);
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }

    /**
     * 采购流程终止后恢复电子设备信息
     * @param requestid
     * @return
     */
    public Map<String, Object> restoreEquipmentInfoAfterTerminate(String requestid){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String, Object> resultMap = new HashMap<>();

        try{
            String tableName = JAVATools.getMaintable(requestid);

            String mainid = "";
            String sql = "select id from "+tableName+" where requestid = '"+requestid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
            }

            sql = "select dzsbxxid from "+tableName+"_dt5 where mainid = '"+mainid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                String dzsbxxid = Util.null2String(rs.getString("dzsbxxid"));
                restoreEquipmentInfo(dzsbxxid);
            }

            resultMap.put("result","success");
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result","fail");
        }

        return resultMap;
    }

    public void restoreEquipmentInfo(String dzsbxxid){
        PropBean pb = new PropBean();
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();

        String workflowId = pb.getWorkflowIdsByUnNumber("dzsbsq");
        String tableName = "";
        String sql = "select t1.tablename from workflow_bill t1,workflow_base t2  where t2.formid = t1.id and t2.id = '"+workflowId+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            tableName = Util.null2String(rs.getString("tablename"));
        }

        sql = "select sbmc,xhjpz,sl,dj,xj from "+tableName+"_dt5 where id = '"+dzsbxxid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            StringBuffer update_sql = new StringBuffer("update uf_dzsbxxcx set ");
            update_sql.append(" sbmc = '"+Util.null2String(rs.getString("sbmc"))+"'");
            update_sql.append(", xhjpz = '"+Util.null2String(rs.getString("xhjpz"))+"'");
            update_sql.append(", sl = '"+Util.null2String(rs.getString("sl"))+"'");
            update_sql.append(", dj = '"+Util.null2String(rs.getString("dj"))+"'");
            update_sql.append(", xj = '"+Util.null2String(rs.getString("xj"))+"'");
            update_sql.append(" where id = '"+dzsbxxid+"'");
            bs.writeLog(update_sql);
            rs.executeUpdate(update_sql.toString());
        }

    }
}
