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.SurProjectSchedule;
import com.yanzhu.jh.project.service.ISurProjectScheduleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 形象进度Controller
 * 
 * @author Jiangdu
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/project/surProjectSchedule")
public class SurProjectScheduleController extends BaseController
{
    @Autowired
    private ISurProjectScheduleService surProjectScheduleService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询形象进度列表
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectSchedule:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurProjectSchedule surProjectSchedule)
    {
        startPage();
        surProjectSchedule.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(surProjectSchedule.getNowRole())){
            surProjectSchedule.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            surProjectSchedule.setNowDept(Convert.toStr(getDeptId()));
        }
        surProjectSchedule.setNowUser(Convert.toStr(getUserId()));
        surProjectSchedule.setCreateBy(Convert.toStr(getLoginUser().getUser().getUserId()));
        List<SurProjectSchedule> list =
                surProjectScheduleService.selectSurProjectScheduleList(surProjectSchedule);
        return getDataTable(list);
    }

    /**
     * 导出形象进度列表
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectSchedule:export')")
    @Log(title = "形象进度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurProjectSchedule surProjectSchedule)
    {
        surProjectSchedule.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(surProjectSchedule.getNowRole())){
            surProjectSchedule.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            surProjectSchedule.setNowDept(Convert.toStr(getDeptId()));
        }
        surProjectSchedule.setNowUser(Convert.toStr(getUserId()));
        surProjectSchedule.setCreateBy(Convert.toStr(getLoginUser().getUser().getUserId()));
        List<SurProjectSchedule> list = surProjectScheduleService.selectSurProjectScheduleList(surProjectSchedule);
        ExcelUtil<SurProjectSchedule> util = new ExcelUtil<SurProjectSchedule>(SurProjectSchedule.class);
        util.exportExcel(response, list, "形象进度数据");
    }

    /**
     * 获取形象进度详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectSchedule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surProjectScheduleService.selectSurProjectScheduleById(id));
    }

    /**
     * 新增形象进度
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectSchedule:add')")
    @Log(title = "形象进度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurProjectSchedule surProjectSchedule)
    {
        return toAjax(surProjectScheduleService.insertSurProjectSchedule(surProjectSchedule));
    }

    /**
     * 修改形象进度
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectSchedule:edit')")
    @Log(title = "形象进度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurProjectSchedule surProjectSchedule)
    {
        return toAjax(surProjectScheduleService.updateSurProjectSchedule(surProjectSchedule));
    }

    /**
     * 删除形象进度
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectSchedule:remove')")
    @Log(title = "形象进度", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surProjectScheduleService.deleteSurProjectScheduleByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('project:surProjectSchedule:query')")
    @GetMapping("/getLastOne")
    public AjaxResult getLastOne(long projectId){
        SurProjectSchedule surProjectSchedule=surProjectScheduleService.getProjectConstructionProgress(projectId);
        return AjaxResult.success(surProjectSchedule);
    }
}
