package com.yanzhu.jh.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.SysRoleEnum;
import com.ruoyi.system.service.ISysDeptService;
import com.yanzhu.jh.project.domain.SurProject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.yanzhu.jh.project.domain.SurProjectUserinfo;
import com.yanzhu.jh.project.service.ISurProjectUserinfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目用户Controller
 * 
 * @author JiangYuQi
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/project/surProjectUserInfo")
public class SurProjectUserinfoController extends BaseController
{
    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISurProjectUserinfoService surProjectUserinfoService;

    /**
     * 查询项目用户列表
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectUserInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurProjectUserinfo surProjectUserinfo)
    {
        startPage();
        surProjectUserinfo.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(surProjectUserinfo.getNowRole())){
            surProjectUserinfo.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            surProjectUserinfo.setNowDept(Convert.toStr(getDeptId()));
        }
        surProjectUserinfo.setNowUser(Convert.toStr(getUserId()));
        List<SurProjectUserinfo> list = surProjectUserinfoService.selectSurProjectUserinfoList(surProjectUserinfo);
        return getDataTable(list);
    }

    /**
     * 导出项目用户列表
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectUserInfo:export')")
    @Log(title = "项目用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurProjectUserinfo surProjectUserinfo)
    {
        surProjectUserinfo.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(surProjectUserinfo.getNowRole())){
            surProjectUserinfo.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            surProjectUserinfo.setNowDept(Convert.toStr(getDeptId()));
        }
        surProjectUserinfo.setNowUser(Convert.toStr(getUserId()));
        List<SurProjectUserinfo> list = surProjectUserinfoService.selectSurProjectUserinfoList(surProjectUserinfo);
        ExcelUtil<SurProjectUserinfo> util = new ExcelUtil<SurProjectUserinfo>(SurProjectUserinfo.class);
        util.exportExcel(response, list, "项目用户数据");
    }

    /**
     * 获取项目用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectUserInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surProjectUserinfoService.selectSurProjectUserinfoById(id));
    }

    /**
     * 新增项目用户
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectUserInfo:add')")
    @Log(title = "项目用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurProjectUserinfo surProjectUserinfo)
    {
        return toAjax(surProjectUserinfoService.insertSurProjectUserinfo(surProjectUserinfo));
    }

    /**
     * 修改项目用户
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectUserInfo:edit')")
    @Log(title = "项目用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurProjectUserinfo surProjectUserinfo)
    {
        return toAjax(surProjectUserinfoService.updateSurProjectUserinfo(surProjectUserinfo));
    }

    /**
     * 删除项目用户
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectUserInfo:remove')")
    @Log(title = "项目用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surProjectUserinfoService.deleteSurProjectUserinfoByIds(ids));
    }

    /**
     * 查询项目用户列表
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectUserInfo:list')")
    @GetMapping("/findUserByDeptId")
    public TableDataInfo findUserByDeptId(SurProject surProject)
    {
        List<SysUser> list = surProjectUserinfoService.selectProjectUserList(surProject);
        return getDataTable(list);
    }

}
