package com.engine.attendance.attendanceanalysis.cmd;

import com.engine.attendance.enums.SystemItemEnum;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.common.util.DateUtil;
import com.engine.common.util.DbTools;
import com.engine.core.interceptor.CommandContext;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.icbc.api.internal.apache.http.impl.cookie.S;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 更新人员当天出勤结果
 */
public class UpdateAttendanceResultsCmd extends AbstractCommonCommand<Map<String,Object>> {


    @Override
    public BizLogContext getLogContext() {
        return null;
    }
    public UpdateAttendanceResultsCmd(Map<String,Object> params){
        this.params=params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        String userId = Util.null2String(params.get("userId"));
        String date = Util.null2String(params.get("date"));
        //项目
        String item = Util.null2String(params.get("item"));
        //项目时长
        String itemduration = params.get("itemduration") == null ? null:params.get("itemduration").toString();
        //是否异常
        String cqzt = params.get("cqzt") == null ? "0":Util.null2String(params.get("cqzt"));
        String formmodeid = Util.null2String(params.get("modeId"));
        Map<String,Object> resultMap = Maps.newHashMap();
        RecordSet rs = new RecordSet();
        List<Map<String,Object>> classInfo = (List<Map<String,Object>>)params.get("classInfo");

        String sql = "select ygid,rq,bc,xm1,xm2,xm3,xm4,xm5,xm6,xm7,xm8,j1,c1,j2,c2,j3,c3,j4,c4,j5,c5,j6,c6,j7,c7,j8,c8 from uf_jcl_kq_cqjg where ygid=? and rq=?";
        List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,userId,date);
        //班次信息
        if (params.get("classInfo") == null){
            //当天没有排班，计入异常
            noScheduling(params,rs,dataList);
            return resultMap;
        }
        if (dataList.size() >0){
            //有出勤结果
            int index = 0;
            int i = 0;
            while (index==0){
                i++;
                if (dataList.get(0).get("xm"+i) == null || "".equals(dataList.get(0).get("xm"+i))){
                    index = i;
                    break;
                }
            }
            sql = "update uf_jcl_kq_cqjg set xm"+index+"=?,sc"+index+"=? where ygid=? and rq=?";
            DbTools.update(rs,sql,item,itemduration,userId,date);

        }else {
            //没有出勤结果
            sql = "select subcompanyid1,departmentid,jobtitle from hrmresource  where id=?";
            List<Map<String,Object>> userList = DbTools.getSqlToList(sql,userId);
            sql = "insert into uf_jcl_kq_cqjg (szjg,ygid,fbid,bm,zw,rq,bc,bcbdxx,cqzt,cqsc,xm1,sc1,sjzt,fxrq,formmodeid,modeuuid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            DbTools.update(rs,sql,userList.get(0).get("subcompanyid1"),userId,userList.get(0).get("subcompanyid1"),userList.get(0).get("departmentid"),
                    userList.get(0).get("jobtitle"),date,classInfo.get(0).get("bcxx"),classInfo.get(0).get("bcsdxx"),1,0,item,itemduration,1, DateUtil.getCurrentDate(),formmodeid, UUID.randomUUID().toString(),1,0,DateUtil.getCurrentDate(),DateUtil.getCurrentTime().split(" ")[1]);


        }

        return resultMap;
    }

    /**
     * 没有排班处理
     * @param params
     */
    public void noScheduling(Map<String,Object> params, RecordSet rs,List<Map<String,Object>> dataList){
        String userId = Util.null2String(params.get("userId"));
        String date = Util.null2String(params.get("date"));
        String formmodeid = Util.null2String(params.get("modeId"));
        List<Map<String, Object>> scheduleList = params.get("scheduleList") == null? Lists.newArrayList():(List<Map<String, Object>>)params.get("scheduleList");
        String sql = "select subcompanyid1,departmentid,jobtitle from hrmresource  where id=?";
        String rqlx = "NULL";
        String sfxx = "NULL";
        String cqzt = "1";
        if (scheduleList.size()>0){
            rqlx = Util.null2String(scheduleList.get(0).get("rqlx"));
            sfxx = Util.null2String(scheduleList.get(0).get("sfxx"));
            cqzt = "0";
        }
        List<Map<String,Object>> userList = DbTools.getSqlToList(sql,userId);
        if (dataList.size() >0){
            if ("1".equals(sfxx)){
                sql = "update uf_jcl_kq_cqjg set bc=NULL,bcbdxx=NULL,cqzt="+cqzt+",cqsc=0,sjzt=1,rqlx="+rqlx+",sfxx="+sfxx+",fxrq='"+DateUtil.getCurrentTime()+"',";

                for (int i=1;i<9;i++){
                    sql = sql +"xm"+i+"=NULL,sc"+i+"=NULL,"+ "j"+i+"=NULL,c"+i+"=NULL,";
                }
                sql = sql.substring(0,sql.length()-1) +" where ygid=? and rq=?";
                DbTools.update(rs,sql,userId,date);
            }else {
                sql = "update uf_jcl_kq_cqjg set bc=NULL,bcbdxx=NULL,xm1=?,sc1=NULL,j1=NULL,c1=NULL,cqzt="+cqzt+",cqsc=0,sjzt=1,rqlx="+rqlx+",sfxx="+sfxx+",fxrq='"+DateUtil.getCurrentTime()+"',";
                for (int i=2;i<9;i++){
                    sql = sql +"xm"+i+"=NULL,sc"+i+"=NULL,"+ "j"+i+"=NULL,c"+i+"=NULL,";
                }
                sql = sql.substring(0,sql.length()-1) +" where ygid=? and rq=?";
                DbTools.update(rs,sql,SystemItemEnum.NO_SCHEDULING.getKey(),userId,date);
            }
        }else {
            if (scheduleList.size()>0){
                if ("1".equals(sfxx)){
                    sql = "insert into uf_jcl_kq_cqjg (cqsc,szjg,ygid,fbid,bm,zw,rq,cqzt,sjzt,fxrq,formmodeid,modeuuid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,rqlx,sfxx) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                    DbTools.update(rs,sql,0,userList.get(0).get("subcompanyid1"),userId,userList.get(0).get("subcompanyid1"),
                            userList.get(0).get("departmentid"),userList.get(0).get("jobtitle"),date,cqzt,1, DateUtil.getCurrentTime(),formmodeid, UUID.randomUUID().toString(),1,0,DateUtil.getCurrentDate(),DateUtil.getCurrentTime().split(" ")[1],rqlx,sfxx);
                }else {
                    sql = "insert into uf_jcl_kq_cqjg (cqsc,szjg,ygid,fbid,bm,zw,rq,xm1,cqzt,sjzt,fxrq,formmodeid,modeuuid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,rqlx,sfxx) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                    DbTools.update(rs,sql,0,userList.get(0).get("subcompanyid1"),userId,userList.get(0).get("subcompanyid1"),
                            userList.get(0).get("departmentid"),userList.get(0).get("jobtitle"),date, SystemItemEnum.NO_SCHEDULING.getKey(),cqzt,1, DateUtil.getCurrentTime(),formmodeid, UUID.randomUUID().toString(),1,0,DateUtil.getCurrentDate(),DateUtil.getCurrentTime().split(" ")[1],rqlx,sfxx);
                }
            }else {
                sql = "insert into uf_jcl_kq_cqjg (cqsc,szjg,ygid,fbid,bm,zw,rq,xm1,cqzt,sjzt,fxrq,formmodeid,modeuuid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                DbTools.update(rs,sql,0,userList.get(0).get("subcompanyid1"),userId,userList.get(0).get("subcompanyid1"),
                        userList.get(0).get("departmentid"),userList.get(0).get("jobtitle"),date, SystemItemEnum.NO_SCHEDULING.getKey(),cqzt,1, DateUtil.getCurrentTime(),formmodeid, UUID.randomUUID().toString(),1,0,DateUtil.getCurrentDate(),DateUtil.getCurrentTime().split(" ")[1]);

            }

        }

    }
}
