package com.ruoyi.web.controller.oa;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.CmcArchive;
import com.ruoyi.oa.service.ICmcArchiveService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc成果归档Controller
 * 
 * @author cmc
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/oa/archive")
public class CmcArchiveController extends BaseController
{
    @Autowired
    private ICmcArchiveService cmcArchiveService;

    /**
     * 查询cmc成果归档列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcArchive cmcArchive)
    {
        startPage();
        List<CmcArchive> list = cmcArchiveService.selectCmcArchiveList(cmcArchive);
        return getDataTable(list);
    }

    /**
     * 导出cmc成果归档列表
     */
    @Log(title = "cmc成果归档", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcArchive cmcArchive)
    {
        List<CmcArchive> list = cmcArchiveService.selectCmcArchiveList(cmcArchive);
        ExcelUtil<CmcArchive> util = new ExcelUtil<CmcArchive>(CmcArchive.class);
        util.exportExcel(response, list, "cmc成果归档数据");
    }

    /**
     * 获取cmc成果归档详细信息
     */
    @GetMapping(value = "/{archiveId}")
    public AjaxResult getInfo(@PathVariable("archiveId") String archiveId)
    {
        return success(cmcArchiveService.selectCmcArchiveByArchiveId(archiveId));
    }

    /**
     * 新增cmc成果归档
     */
    @Log(title = "cmc成果归档", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcArchive cmcArchive)
    {
        cmcArchive.setProjectLeader(getLoginUser().getUserId());
        return toAjax(cmcArchiveService.insertCmcArchive(cmcArchive));
    }

    /**
     * 修改cmc成果归档
     */
    @Log(title = "cmc成果归档", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcArchive cmcArchive)
    {
        if (cmcArchive.getProduceComment() != null && cmcArchive.getQualityComment() == null) {
            cmcArchive.setProduceInspector(getLoginUser().getUserId());
            cmcArchive.setProduceTime(new Date());
        }
        else if (cmcArchive.getQualityComment() != null && cmcArchive.getDeptComment() == null) {
            cmcArchive.setQualityInspector(getLoginUser().getUserId());
            cmcArchive.setQualityTime(new Date());
        }
        else if (cmcArchive.getDeptComment() != null && cmcArchive.getArchiveComment() == null) {
            cmcArchive.setDeptLeader(getLoginUser().getUserId());
            cmcArchive.setDeptTime(new Date());
        }
        else if (cmcArchive.getArchiveComment() != null) {
            cmcArchive.setArchiver(getLoginUser().getUserId());
            cmcArchive.setArchiveTime(new Date());
        }
        return toAjax(cmcArchiveService.updateCmcArchive(cmcArchive));
    }

    /**
     * 删除cmc成果归档
     */
    @Log(title = "cmc成果归档", businessType = BusinessType.DELETE)
	@DeleteMapping("/{archiveIds}")
    public AjaxResult remove(@PathVariable String[] archiveIds)
    {
        return success(cmcArchiveService.deleteCmcArchiveByArchiveIds(archiveIds));
    }
}
