package com.yanzhu.jh.project.controller;

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

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.SysRoleEnum;
import com.ruoyi.system.service.ISysDeptService;
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.SurProjectMeasure;
import com.yanzhu.jh.project.service.ISurProjectMeasureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实测实量Controller
 * 
 * @author JiangYuQi
 * @date 2023-08-22
 */
@RestController
@RequestMapping("/project/projectMeasure")
public class SurProjectMeasureController extends BaseController
{
    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISurProjectMeasureService surProjectMeasureService;

    /**
     * 查询实测实量列表
     */
    @PreAuthorize("@ss.hasPermi('project:projectMeasure:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurProjectMeasure surProjectMeasure)
    {
        startPage();
        surProjectMeasure.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(surProjectMeasure.getNowRole())){
            surProjectMeasure.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            surProjectMeasure.setNowDept(Convert.toStr(getDeptId()));
        }
        surProjectMeasure.setNowUser(Convert.toStr(getUserId()));
        List<SurProjectMeasure> list = surProjectMeasureService.selectSurProjectMeasureList(surProjectMeasure);
        return getDataTable(list);
    }

    /**
     * 导出实测实量列表
     */
    @PreAuthorize("@ss.hasPermi('project:projectMeasure:export')")
    @Log(title = "实测实量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurProjectMeasure surProjectMeasure)
    {
        surProjectMeasure.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(surProjectMeasure.getNowRole())){
            surProjectMeasure.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            surProjectMeasure.setNowDept(Convert.toStr(getDeptId()));
        }
        surProjectMeasure.setNowUser(Convert.toStr(getUserId()));
        List<SurProjectMeasure> list = surProjectMeasureService.selectSurProjectMeasureList(surProjectMeasure);
        ExcelUtil<SurProjectMeasure> util = new ExcelUtil<SurProjectMeasure>(SurProjectMeasure.class);
        util.exportExcel(response, list, "实测实量数据");
    }

    /**
     * 获取实测实量详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:projectMeasure:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surProjectMeasureService.selectSurProjectMeasureById(id));
    }

    /**
     * 新增实测实量
     */
    @PreAuthorize("@ss.hasPermi('project:projectMeasure:add')")
    @Log(title = "实测实量", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurProjectMeasure surProjectMeasure)
    {
        return toAjax(surProjectMeasureService.insertSurProjectMeasure(surProjectMeasure));
    }

    /**
     * 修改实测实量
     */
    @PreAuthorize("@ss.hasPermi('project:projectMeasure:edit')")
    @Log(title = "实测实量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurProjectMeasure surProjectMeasure)
    {
        return toAjax(surProjectMeasureService.updateSurProjectMeasure(surProjectMeasure));
    }

    /**
     * 删除实测实量
     */
    @PreAuthorize("@ss.hasPermi('project:projectMeasure:remove')")
    @Log(title = "实测实量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surProjectMeasureService.deleteSurProjectMeasureByIds(ids));
    }
}
