package com.engine.htsc.portal.util;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.upgradetool.wscheck.Util;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;


/**
 * 协同中心首页查询工具
 */
public class QueryTools {

    /**
     * 由我执行
     * @param userid
     * @param condition
     * @return
     */
    public static int getPerformCount(int userid,String condition){
        //1.协作
        int collaboration = generalQueryTool(userid, condition, "uf_xzsy", "jbrzzhyc","xzzt");
        //2.督办
        int oversee = generalQueryTool(userid, condition, "uf_xzsx", "jbrzzhyc","xzzt");
        //3.任务
        int task = generalQueryTool(userid, condition, "uf_rw", "jbrzzhyc","rwzt");

        return collaboration+oversee+task;
    }

    /**
     * 我参与的
     * @param userid
     * @param condition
     * @return
     */
    public static int getParticipateCount(int userid,String condition){
        //1.协作
        int collaboration = generalQueryTool(userid, condition, "uf_xzsy", "tzydzzhyc","xzzt");
        //2.督办
        int oversee = generalQueryTool(userid, condition, "uf_xzsx", "tzydzzhyc","xzzt");
        //3.任务
        int task = generalQueryTool(userid, condition, "uf_rw", "tzydzzhyc","rwzt");

        return collaboration+oversee+task;
    }

    /**
     * 我创建的
     * @param userid
     * @param condition
     * @return
     */
    public static int getCreateCount(int userid,String condition){
        //1.协作
        int collaboration = generalQueryTool(userid, condition, "uf_xzsy", "cjrzczhyc","xzzt");
        //2.督办
        int oversee = generalQueryTool(userid, condition, "uf_xzsx", "cjrzczhyc","xzzt");
        //3.任务
        int task = generalQueryTool(userid, condition, "uf_rw", "cjrzczhyc","rwzt");

        return collaboration+oversee+task;
    }

    /**
     * 全部
     * @param userid
     * @param condition
     * @return
     */
    public static int getAllCount(int userid,String condition){
        //1.协作
        int collaboration = allQueryTool(userid, condition, "uf_xzsy", "xzzt");
        //2.督办
        int oversee = allQueryTool(userid, condition, "uf_xzsx", "xzzt");
        //3.任务
        int task = allQueryTool(userid, condition, "uf_rw", "rwzt");

        return collaboration+oversee+task;
    }

    /**
     * 查询通用方法
     * @param userid
     * @param condition
     * @param tablename
     * @param field
     * @return
     */
    public static int generalQueryTool(int userid,String condition,String tablename,String field,
                                       String status) {
        int count = 0;
        try {
            RecordSet rs = new RecordSet();
            String value = "2";
            if ("cjrzczhyc".equals(field)){
                value = "0,2";
            }
            StringBuffer general = new StringBuffer("select count(1) as count from "+tablename+" where " +
                    "instr(','||"+field+"||',',',"+userid+",') > 0 and cjzt = 1 and "+status+" in ("+value+") ");

            LocalDate today = LocalDate.now();
            String startDate = "";
            String endDate = "";
            if (StringUtils.isNotEmpty(condition)) {
                switch (condition) {
                    case "0": //本周
                        startDate = today.with(DayOfWeek.MONDAY).toString();
                        endDate = today.with(DayOfWeek.SUNDAY).toString();
                        break;
                    case "1": //本月
                        startDate = LocalDate.of(today.getYear(),today.getMonth(),1).toString();
                        endDate = today.with(TemporalAdjusters.lastDayOfMonth()).toString();
                        break;
                    case "2": //本年
                        startDate = today.with(TemporalAdjusters.firstDayOfYear()).toString();
                        endDate = today.with(TemporalAdjusters.lastDayOfYear()).toString();
                        break;
                    default:  //自定义日期
                        startDate = condition.substring(0,condition.indexOf("/"));
                        endDate = condition.substring(condition.lastIndexOf("/")+1);
                }
                general.append(" and to_char(to_date(modedatacreatedate,'yyyy-MM-dd'),'yyyy-MM-dd') " +
                        "between '"+startDate+"' and '"+endDate+"'");
            }

            rs.executeQuery(general.toString());
            rs.next();
            count = Util.getIntValue(rs.getString("count"));

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

    /**
     * 已完成查询统计
     * @param userid
     * @param condition
     * @param tablename
     * @param status
     * @return
     */
    public static int allQueryTool(int userid,String condition,String tablename,String status) {
        int count = 0;
        try {
            RecordSet rs = new RecordSet();
            rs.executeQuery("select count(1) as count from (select jbrzzhyc||','||tzydzzhyc||','||cjrzczhyc as qb," +
                    " "+status+",cjzt from "+tablename+") where instr(','||qb||',',',"+userid+",') > 0  and "+status+" = 3 and cjzt = 1");
            rs.next();
            count = Util.getIntValue(rs.getString("count"));

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



}
