package com.ruoyi.web.controller.project;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.project.BmProjectReport;
import com.ruoyi.system.service.project.IBmProjectReportService;
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.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

/**
 * 项目上报Controller(游客上报,不需要权限)
 * 
 * @author ruoyi
 * @date 2021-05-31
 */
@RestController
@RequestMapping("/bmProjectReport/report")
public class BmProjectReportController extends BaseController
{
    @Autowired
    private IBmProjectReportService bmProjectReportService;

    /**
     * 查询项目上报列表
     */
    //@PreAuthorize("@ss.hasPermi('BmProjectReport:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmProjectReport bmProjectReport)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtil.isNotNull(loginUser)){
            List<SysRole> roles = loginUser.getUser().getRoles();
            boolean flag = false;
            if (roles.size() > 0){
                for (SysRole role : roles) {
                     String roleKey = role.getRoleKey();
                     if (StringUtils.isNotBlank(roleKey) && ("manage".equals(roleKey)||"admin".equals(roleKey))){
                         flag = true;
                         break;
                     }
                }
            }
            if (!flag){
                Long deptId = loginUser.getUser().getDeptId();
                bmProjectReport.setSysDeptId(deptId.toString());
                bmProjectReport.setReportStatus("1");
            }
        }
        List<BmProjectReport> list = bmProjectReportService.selectBmProjectReportList(bmProjectReport);
        return getDataTable(list);
    }

    /**
     * 导出项目上报列表
     */
    //@PreAuthorize("@ss.hasPermi('BmProjectReport:report:export')")
    @Log(title = "项目上报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmProjectReport bmProjectReport)
    {
        List<BmProjectReport> list = bmProjectReportService.selectBmProjectReportList(bmProjectReport);
        ExcelUtil<BmProjectReport> util = new ExcelUtil<BmProjectReport>(BmProjectReport.class);
        return util.exportExcel(list, "项目上报");
    }

    /**
     * 获取项目上报详细信息
     */
    //@PreAuthorize("@ss.hasPermi('BmProjectReport:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bmProjectReportService.selectBmProjectReportById(id));
    }

    /**
     * 新增项目上报
     */
    //@PreAuthorize("@ss.hasPermi('BmProjectReport:report:add')")
    @Log(title = "项目上报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmProjectReport bmProjectReport)
    {
        return toAjax(bmProjectReportService.insertBmProjectReport(bmProjectReport));
    }

    /**
     * 修改项目上报
     */
    //@PreAuthorize("@ss.hasPermi('BmProjectReport:report:edit')")
    @Log(title = "项目上报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmProjectReport bmProjectReport)
    {
        return toAjax(bmProjectReportService.updateBmProjectReport(bmProjectReport));
    }

    /**
     * 删除项目上报
     */
    //@PreAuthorize("@ss.hasPermi('BmProjectReport:report:remove')")
    @Log(title = "项目上报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bmProjectReportService.deleteBmProjectReportByIds(ids));
    }
}
