package com.ruoyi.web.controller.oa;

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

import com.ruoyi.oa.domain.CmcProject;
import com.ruoyi.oa.service.ICmcProjectService;
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.oa.domain.CmcProjectProgress;
import com.ruoyi.oa.service.ICmcProjectProgressService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc项目进度Controller
 * 
 * @author cmc
 * @date 2024-04-02
 */
@RestController
@RequestMapping("/oa/projectProgress")
public class CmcProjectProgressController extends BaseController
{
    @Autowired
    private ICmcProjectProgressService cmcProjectProgressService;

    @Autowired
    private ICmcProjectService cmcProjectService;

    /**
     * 查询cmc项目进度列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcProjectProgress cmcProjectProgress)
    {
        startPage();
        List<CmcProjectProgress> list = cmcProjectProgressService.selectCmcProjectProgressList(cmcProjectProgress);
        return getDataTable(list);
    }

    /**
     * 导出cmc项目进度列表
     */
    @Log(title = "cmc项目进度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcProjectProgress cmcProjectProgress)
    {
        List<CmcProjectProgress> list = cmcProjectProgressService.selectCmcProjectProgressList(cmcProjectProgress);
        ExcelUtil<CmcProjectProgress> util = new ExcelUtil<CmcProjectProgress>(CmcProjectProgress.class);
        util.exportExcel(response, list, "cmc项目进度数据");
    }

    /**
     * 获取cmc项目进度详细信息
     */
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") String projectId)
    {
        return success(cmcProjectProgressService.selectCmcProjectProgressByProjectId(projectId));
    }

    /**
     * 新增cmc项目进度
     */
    @Log(title = "cmc项目进度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcProjectProgress cmcProjectProgress)
    {
        //如100%进度，则表示已完成；当项目流转流程结束，项目状态才结束
        //忽略从100调整到100以下，已完成状态未及时变为未完成的情况；如果考虑此情况，将导致每次执行都会产生修改项目表操作记录，意义不大，且占数据库资源
        if (cmcProjectProgress.getPercentage().equals("100")) {
            CmcProject cmcProject = cmcProjectService.selectCmcProjectByProjectId(cmcProjectProgress.getProjectId());
            cmcProject.setIsComplete("1");
            cmcProjectService.updateCmcProject(cmcProject);
        }
        return toAjax(cmcProjectProgressService.insertCmcProjectProgress(cmcProjectProgress));
    }

    /**
     * 修改cmc项目进度
     */
    @Log(title = "cmc项目进度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcProjectProgress cmcProjectProgress)
    {
        return toAjax(cmcProjectProgressService.updateCmcProjectProgress(cmcProjectProgress));
    }

    /**
     * 删除cmc项目进度
     */
    @Log(title = "cmc项目进度", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable String[] projectIds)
    {
        return success(cmcProjectProgressService.deleteCmcProjectProgressByProjectIds(projectIds));
    }
}
