package com.yanzhu.jh.publics;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.enums.SysRoleEnum;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.FlowTaskEntity;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.yanzhu.jh.project.domain.SurProject;
import com.yanzhu.jh.project.domain.SurProjectAuditinfo;
import com.yanzhu.jh.project.domain.SurProjectUnitInfo;
import com.yanzhu.jh.project.domain.SurProjectUserinfo;
import com.yanzhu.jh.project.service.ISurProjectAuditinfoService;
import com.yanzhu.jh.project.service.ISurProjectService;
import com.yanzhu.jh.project.service.ISurProjectUnitInfoService;
import com.yanzhu.jh.project.service.ISurProjectUserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 公共服务Controller
 *
 * @author JiangYuQi
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/publics")
public class
PublicsController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISurProjectService surProjectService;

    @Autowired
    private ISurProjectUnitInfoService surProjectUnitInfoService;

    @Autowired
    private ISurProjectUserinfoService surProjectUserinfoService;

    @Autowired
    private ISurProjectAuditinfoService surProjectAuditinfoService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取部门列表
     */
    @GetMapping("/deptList")
    public AjaxResult list(SysDept dept)
    {
        dept.setStatus(PublicStateEnum.OK.getCode());
        List<SysDept> depts = deptService.selectDeptList(dept);
        return success(depts);
    }

    /**
     * 获取子公司部门列表
     */
    @GetMapping("/queryZgsDeptList")
    public AjaxResult queryZgsDeptList(SysDept dept)
    {
        String key="publics_queryZgsDeptList";
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return success(obj);
        }
        dept.setStatus(PublicStateEnum.OK.getCode());
        dept.setComFlag(PublicStateEnum.COMZGS.getCode());
        List<SysDept> list = deptService.selectAllDeptList(dept);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return success();
    }

    /**
     * 查询项目列表
     */
    @GetMapping("/projectList")
    public TableDataInfo projectList(SurProject surProject)
    {
        List<SurProject> list = surProjectService.selectSurProjectList(surProject);
        return getDataTable(list);
    }

    /**
     * 查询我的项目列表
     */
    @GetMapping("/findMyProjectList")
    public TableDataInfo findMyProjectList(SurProject surProject)
    {
        surProject.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(surProject.getNowRole())){
            surProject.setNowDept(Convert.toStr(deptService.getZGSDeptId(getDeptId())));
        }else{
            surProject.setNowDept(Convert.toStr(getDeptId()));
        }
        surProject.setNowUser(Convert.toStr(getUserId()));
        List<SurProject> list = surProjectService.selectSurProjectList(surProject);
        return getDataTable(list);
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/userList")
    public TableDataInfo list(SysUser user)
    {
        user.setDelFlag(PublicStateEnum.OK.getCode());
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 获取部门树列表
     */
    @GetMapping("/deptTree")
    public AjaxResult deptTree(SysDept dept)
    {
        return success(deptService.selectDeptTreeList(dept));
    }

    /**
     * 查询项目部门列表
     */
    @GetMapping("/queryUnitList")
    public TableDataInfo queryUnitList(SurProjectUnitInfo surProjectUnitInfo)
    {
        surProjectUnitInfo.setNowRole(Convert.toStr(getUserFirstRole()));
        if(Convert.toInt(surProjectUnitInfo.getNowRole())<5){
            //查询所有符合条件的单位
        }else{
            //查询当前登录人所在单位
            surProjectUnitInfo.setUnitId(getDeptId());
        }
        return getDataTable(surProjectUnitInfoService.selectSurProjectUnitInfoList(surProjectUnitInfo));
    }

    /**
     * 选择项目所有部门人员
     */
    @GetMapping("/selectProjectAllUnitUser")
    public AjaxResult selectProjectAllUnitUser(SurProjectUserinfo surProjectUserinfo)
    {
        String key="publics_selectProjectAllUnitUser_"+surProjectUserinfo.getProjectId();
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return success(obj);
        }
        List<SurProjectUnitInfo> list = surProjectUserinfoService.selectProjectUnitUser(surProjectUserinfo);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return success(list);
    }

    /**
     * 选择项目部门人员
     */
    @GetMapping("/selectProjectUnitUser")
    public AjaxResult selectProjectUnitUser(SurProjectUnitInfo surProjectUnitInfo)
    {
        String key="publics_selectProjectUnitUser_"+surProjectUnitInfo.getProjectId();
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return success(obj);
        }
        List<SurProjectUnitInfo> list = surProjectUserinfoService.selectProjectUnitUser(surProjectUnitInfo);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return success(list);
    }

    /**
     * 查询项目审核信息列表
     */
    @GetMapping("/selectProjectAuditinfo")
    public AjaxResult selectProjectAuditinfo(SurProjectAuditinfo surProjectAuditinfo)
    {
        return success(surProjectAuditinfoService.selectProjectAuditinfoList(surProjectAuditinfo));
    }

    /**
     * 查询代办任务
     */
    @GetMapping("/findTask")
    public AjaxResult findTask()
    {
        FlowTaskEntity flowTaskEntity = new FlowTaskEntity();
        flowTaskEntity.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(flowTaskEntity.getNowRole())){
            flowTaskEntity.setNowDept(Convert.toStr(deptService.getZGSDeptId(getDeptId())));
        }else{
            flowTaskEntity.setNowDept(Convert.toStr(getDeptId()));
        }
        flowTaskEntity.setNowUser(Convert.toStr(SecurityUtils.getUserId()));
        flowTaskEntity.setRoleIds(SecurityUtils.getLoginUser().getUser().getRoles().stream().map(role -> role.getRoleId()).collect(Collectors.toList()));
        flowTaskEntity.setNowUserName(getUsername());
        flowTaskEntity.setActiveName("await");
        return success(surProjectAuditinfoService.findMyTask(flowTaskEntity));
    }
}
