package org.dromara.web.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import org.dromara.biz.domain.BizTask;
import org.dromara.biz.service.ICustomSQLService;
import org.dromara.biz.service.IStatDeptService;
import org.dromara.common.core.config.RuoYiConfig;
import org.dromara.common.core.constant.WflowContants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.dto.RoleDTO;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.Helper;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.wflow.workflow.service.RuansiMissionTaskService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 首页
 *
 * @author Lion Li
 */
@SaIgnore
@RequiredArgsConstructor
@RestController
@RequestMapping("home")
public class HomeController {

    /**
     * 系统基础配置
     */
    private final RuoYiConfig ruoyiConfig;

    private final IStatDeptService statDeptService;
    private final ICustomSQLService customSQLService;
    private final RuansiMissionTaskService ruansiMissionTaskService;

    /**
     * 访问首页，提示语
     */
    @GetMapping("/stat")
    public R<Map> stat() {
        Map result = new HashMap();

        LoginUser loginUser = LoginHelper.getLoginUser();
        Long userId = loginUser.getUserId();
        Long deptId = loginUser.getDeptId();

        Set<String> roles = loginUser.getRolePermission();
        if (roles.size()==1 && roles.contains("normal")){
            // 统计数字
            Map data = customSQLService.selectOne("SELECT  " +
                    "ifnull(count(id),0) rwzs, " +
                    "ifnull(count(id),0) wfqdrws, " +
                    "ifnull(sum(case when type = 1 then 1 else 0 end ),0) ajrws, " +
                    "ifnull(sum(case when type = 2 then 1 else 0 end ),0) jqrws, " +
                    "ifnull(sum(case when type = 3 then 1 else 0 end ),0) zxrws, " +
                    "ifnull(sum(case when type = 4 then 1 else 0 end ),0) qtrws, " +
                    "ifnull(sum(case when level = '普通任务' then 1 else 0 end ),0) ptrws, " +
                    "ifnull(sum(case when level = '指令任务' then 1 else 0 end ),0) jjrws, " +
                    "ifnull(sum(case when level = '线索核查' then 1 else 0 end ),0) zlrws, " +
                    "ifnull(sum(case when level = '敏感任务' then 1 else 0 end ),0) mgrws, " +
                    "ifnull(sum(case when level = '特殊任务' then 1 else 0 end ),0) tsrws, " +
                    "ifnull(sum(case when level = '情指任务' then 1 else 0 end ),0) qzrws, " +
                    "ifnull(sum(case when status = '10' or status = '11' then 1 else 0 end ),0) dsprws, " +
                    "ifnull(sum(case when status not in ('10','11','60') then 1 else 0 end ),0) jxzrws, " +
                    "ifnull(sum(case when status = '60' then 1 else 0 end ),0) ywjrws " +
                    "FROM `biz_task` where apply_user_id = " + userId);
            result.putAll(data);
            result.put("leader", 0);

            result.putAll(customSQLService.selectOne("SELECT ifnull(count(1),0) xzds FROM biz_task_work w " +
                    " left join biz_task t on w.task_no = t.task_no where t.apply_user_id = " + userId));
            result.putAll(customSQLService.selectOne("SELECT ifnull(count(1),0) fkds FROM biz_task_feedback f " +
                    " left join biz_task t on f.task_no = t.task_no where t.apply_user_id = " + userId));
            result.putAll(customSQLService.selectOne("select round(ifnull(avg(pjhs),0)) pjhs from ( " +
                    " select TIMESTAMPDIFF(SECOND, create_time, finish_time) pjhs from biz_task " +
                    " where apply_user_id = "+userId+" and finish_time is not null ) t"));
        }else{
            result.putAll(customSQLService.selectOne("select ifnull(count(id),0) wfqdrws from `biz_task` where apply_user_id = " + userId));
            result.putAll(customSQLService.selectOne("select  " +
                    "ifnull(sum(rwzs),0) rwzs, ifnull(sum(ajrws),0) ajrws, ifnull(sum(jqrws),0) jqrws, " +
                    "ifnull(sum(zxrws),0) zxrws, ifnull(sum(qtrws),0) qtrws, " +
                    "ifnull(sum(ptrws),0) ptrws, ifnull(sum(jjrws),0) jjrws, ifnull(sum(zlrws),0) zlrws, " +
                    "ifnull(sum(mgrws),0) mgrws, ifnull(sum(tsrws),0) tsrws, ifnull(sum(qzrws),0) qzrws," +
                    "ifnull(sum(xzds),0) xzds, ifnull(sum(fkds),0) fkds, ifnull(avg(pjhs),0) pjhs " +
                    "from stat_dept where dept_id = '"+deptId+"'"));

            result.put("leader", 1);
        }

        // 待我审批
        Map dwsp = customSQLService.selectOne("SELECT ifnull(count(1),0) dwsps FROM ACT_RU_TASK RES WHERE EXISTS ( SELECT 1 FROM ACT_RE_PROCDEF D " +
                " WHERE RES.PROC_DEF_ID_ = D.ID_ AND D.KEY_ = '"+ WflowContants.NORMAL_MISSION_DEFAULT_CODE +"' ) "+
                " AND RES.SUSPENSION_STATE_ = 1 AND ( RES.ASSIGNEE_ = "+userId+" OR ( RES.ASSIGNEE_ IS NULL AND EXISTS "+
                " ( SELECT LINK.ID_ FROM ACT_RU_IDENTITYLINK LINK WHERE LINK.TASK_ID_ = RES.ID_ AND LINK.TYPE_ = 'candidate' AND LINK.USER_ID_ = "+userId+" ))) ");
        result.putAll(dwsp);

        // 待我办理
        TableDataInfo<BizTask> table = ruansiMissionTaskService.getToHandleList(20,1,null,null,null,null, null, null);
        result.put("dwbls", table.getTotal());

        result.put("mrrwqs", customSQLService.selectList("select " +
                "rq.drq, ifnull(rwzs, dcount) rwzs, ifnull(ajrws,dcount) ajrws, ifnull(jqrws,dcount) jqrws, ifnull(zxrws,dcount) zxrws, " +
                "ifnull(qtrws,dcount) qtrws, ifnull(ptrws,dcount) ptrws, ifnull(jjrws,dcount) jjrws, ifnull(zlrws,dcount) zlrws, " +
                "ifnull(mgrws,dcount) mgrws, ifnull(tsrws,dcount) tsrws, ifnull(qzrws,dcount) qzrws from t_rq rq left join " +
                "(select * from stat_dept where dept_id = '"+deptId+"' and tjrq > CURDATE() - INTERVAL 7 DAY) tj " +
                "on rq.drq = tj.tjrq  where rq.drq > CURDATE() - INTERVAL 30 DAY and rq.drq <= CURDATE()"));

        result.put("rwdt", customSQLService.selectList("SELECT " +
                " l.*, t.title task_title, t.type task_type, t.level task_level, t.status task_status " +
                " FROM `biz_task_log` l left join biz_task t on l.task_no = t.task_no " +
                " where l.dept_id like '"+ Helper.PrefixDeptid(deptId.toString())+"%' order by optime desc limit 20"));
        return R.ok(result);
    }
}
