package org.jeecg.modules.itdmFirstPage.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.itdmFirstPage.entity.ItdmTask;
import org.jeecg.modules.itdmFirstPage.service.IItdmTaskService;
import org.jeecg.modules.itdmFirstPage.vo.ItdmFirstPageVO;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags="...首页")
@RestController
@RequestMapping("/itdmFirstPage")
@Slf4j
public class itdmFirstPageController {

    @Autowired
    private IItdmTaskService taskService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    private ISysRoleService roleService;

    @ApiOperation(value="任务各种数量", notes="所有任务，待办，进行中，完成")
    @GetMapping(value = "/getFrstPageTaskNum")
    public ItdmFirstPageVO getFrstPageTaskNum(){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String realName = loginUser.getRealname(); // 登录人姓名

        ItdmTask itdmTask = new ItdmTask();

        if(!realName.equals("管理员")){
            itdmTask.setTaskUser(realName);

            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(loginUser.getId());
            Wrapper<SysUserRole> userRoleWrapper = new QueryWrapper<>(userRole);
            List<SysUserRole> userRoleList = sysUserRoleService.list(userRoleWrapper);
            if(userRoleList != null && !userRoleList.isEmpty() && userRoleList.get(0)!=null){
                List<String> roles = userRoleList.stream().map(i->{
                    return roleService.getById(i.getRoleId()).getRoleName();
                }).collect(Collectors.toList());

                String role = roles.get(0); // 登录人角色，一般只有一个
                itdmTask.setTaskRole(role);

                if(role.equals("经理") || role.equals("计划员")) itdmTask.setTaskUser(null);
            }

        }

        Wrapper<ItdmTask> taskWrapper = new QueryWrapper<>(itdmTask);
        Long totalNum = taskService.count(taskWrapper);

        itdmTask.setTaskStatus(0);
        taskWrapper = new QueryWrapper<>(itdmTask);
        Long daibanNum = taskService.count(taskWrapper);

        itdmTask.setTaskStatus(1);
        taskWrapper = new QueryWrapper<>(itdmTask);
        Long jinxingNum = taskService.count(taskWrapper);

        itdmTask.setTaskStatus(2);
        taskWrapper = new QueryWrapper<>(itdmTask);
        Long wanchengNum =taskService.count(taskWrapper);

        ItdmFirstPageVO firstPageVO = new ItdmFirstPageVO();
        firstPageVO.setDaibanNum(daibanNum);
        firstPageVO.setJinxingNum(jinxingNum);
        firstPageVO.setWanchengNum(wanchengNum);
        firstPageVO.setTotalNum(totalNum);

        return firstPageVO;
    }


}
