package com.engine.kq.cmd.balanceofleaverp;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQReportBiz;
import weaver.conn.RecordSet;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.resource.ResourceComInfo;

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

/**
 * 员工假期余额报表--获取分页信息
 */
public class GetPageInfoCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetPageInfoCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        /**
         * 时间范围选择的年份
         * dateScope：5-本年、8-上一年
         * selectedYear：指定年份
         */
        String dateScope = Util.null2String(params.get("dateScope"));
        String selectedYear = Util.null2String(params.get("selectedYear"));
        if (dateScope.equals("5") || dateScope.equals("8")) {
            selectedYear = TimeUtil.getDateByOption(dateScope, "0").substring(0, 4);
        }
        /**
         * 数据范围
         * dataScope：0-总部、1-分部、2-部门、3-人员、4-我的下属
         * subcomId：指定分部ID
         * deptId：指定部门ID
         * resourceId：指定人员ID
         * allLevel：是否包含下级下属：0-不包含、1-包含
         */
        String dataScope = Util.null2String(params.get("dataScope"));
        String subcomId = Util.null2String(params.get("subcomId"));
        String deptId = Util.null2String(params.get("deptId"));
        String resourceId = Util.null2String(params.get("resourceId"));
        String allLevel = Util.null2String(params.get("allLevel"));
        /**
         * isNoAccount：是否显示无账号人员：true-显示、false-不显示
         */
        String isNoAccount = Util.null2String(params.get("isNoAccount"));

        //人员状态
        String resourceStatus = Util.null2String(params.get("status"));

        try {
            /**
             * 获取考勤报表权限共享设置
             */
            KQReportBiz kqReportBiz = new KQReportBiz();
            String rightStr = kqReportBiz.getReportRight("4", "" + user.getUID(), "a");

            int total = 0;
            int pageSize = 10;

            String sql = "select count(*) total from HrmResource a where 1=1 ";
            if (dataScope.equals("0")) {
                //总部
            } else if (dataScope.equals("1")) {
                sql += " and a.subcompanyId1 in (" + subcomId + ") ";
            } else if (dataScope.equals("2")) {
                sql += " and a.departmentId in (" + deptId + ") ";
            } else if (dataScope.equals("3")) {
                sql += " and a.id in (" + resourceId + ")";
            } else if (dataScope.equals("4")) {
                if (allLevel.equals("1")) {
                    sql += " and (a.id=" + user.getUID() + " or a.managerStr like '%," + user.getUID() + ",%' )";
                } else {
                    sql += " and (a.id=" + user.getUID() + " or a.managerid = " + user.getUID() + ")";
                }
            }
            RecordSet recordSet = new RecordSet();
            if (isNoAccount.equals("false")) {
                if (recordSet.getDBType().equalsIgnoreCase("sqlserver")
                        || recordSet.getDBType().equalsIgnoreCase("mysql")) {
                    sql += " and (a.loginId is not null and a.loginId<>'')";
                } else {
                    sql += " and (a.loginId is not null)";
                }
            }

            if(resourceStatus.length()>0){
              if (!resourceStatus.equals("8") && !resourceStatus.equals("9")) {
                sql += " and a.status = "+resourceStatus+ "";
              }else if (resourceStatus.equals("8")) {
                sql += " and (a.status = 0 or a.status = 1 or a.status = 2 or a.status = 3) ";
              }
            }
            //考勤报表共享设置
            if (!rightStr.equals("") && !dataScope.equals("4")) {
                sql += rightStr;
            }
            recordSet.executeQuery(sql);
            if (recordSet.next()) {
                total = recordSet.getInt("total");
            }
            resultMap.put("total", total);
            resultMap.put("pageSize", pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}
