package com.yanzhu.jh.publics;

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.text.Convert;
import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.enums.SysRoleEnum;
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.SurProjectChecking;
import com.yanzhu.jh.project.domain.SurProjectUnitInfo;
import com.yanzhu.jh.project.domain.SurProjectUserinfo;
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.security.access.prepost.PreAuthorize;
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;

/**
 * 公共服务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;

    /**
     * 获取部门列表
     */
    @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)
    {
        dept.setStatus(PublicStateEnum.OK.getCode());
        dept.setComFlag(PublicStateEnum.COMZGS.getCode());
        return success(deptService.selectAllDeptList(dept));
    }

    /**
     * 查询项目列表
     */
    @GetMapping("/projectList")
    public TableDataInfo list(SurProject surProject)
    {
        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(SysRoleEnum.ZBDW.getCode().equals(surProjectUnitInfo.getNowRole())){
            surProjectUnitInfo.setUnitId(getDeptId());
        }
        return getDataTable(surProjectUnitInfoService.selectSurProjectUnitInfoList(surProjectUnitInfo));
    }

    /**
     * 选择项目部门人员
     */
    @GetMapping("/selectProjectUnitUser")
    public AjaxResult selectProjectUnitUser(SurProjectUserinfo surProjectUserinfo)
    {
        return success(surProjectUserinfoService.selectProjectUnitUser(surProjectUserinfo));
    }
}
