package com.ruoyi.web.controller.oa;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.system.service.ISysUserService;
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.CmcProjectComment;
import com.ruoyi.oa.service.ICmcProjectCommentService;
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/projectComment")
public class CmcProjectCommentController extends BaseController
{
    @Autowired
    private ICmcProjectCommentService cmcProjectCommentService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询cmc项目审批列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcProjectComment cmcProjectComment)
    {
        startPage();
        List<CmcProjectComment> list = cmcProjectCommentService.selectCmcProjectCommentList(cmcProjectComment);
        return getDataTable(list);
    }

    /**
     * 导出cmc项目审批列表
     */
    @Log(title = "cmc项目审批", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcProjectComment cmcProjectComment)
    {
        List<CmcProjectComment> list = cmcProjectCommentService.selectCmcProjectCommentList(cmcProjectComment);
        ExcelUtil<CmcProjectComment> util = new ExcelUtil<CmcProjectComment>(CmcProjectComment.class);
        util.exportExcel(response, list, "cmc项目审批数据");
    }

    /**
     * 获取cmc项目审批详细信息
     */
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") String projectId)
    {
        CmcProjectComment cmcProjectComment = cmcProjectCommentService.selectCmcProjectCommentByProjectId(projectId);
        if (cmcProjectComment != null) {
            String scApprove = cmcProjectComment.getScApprover();
            String scApproverName = cmcProjectComment.getScApproverName();
            StringBuilder scApproverNames = new StringBuilder();
            if (scApprove != null && !scApprove.equals("") && (scApproverName == null || scApproverName.equals(""))) {
                for (String user : scApprove.split(","))
                    scApproverNames.append(userService.selectUserById(Long.parseLong(user)).getNickName()).append(",");
                cmcProjectComment.setScApproverName(scApproverNames.substring(0, scApproverNames.length() - 1));
            }
        }
        return success(cmcProjectComment);
    }

    /**
     * 新增cmc项目审批
     */
    @Log(title = "cmc项目审批", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcProjectComment cmcProjectComment)
    {
        if (cmcProjectComment.getJyComment() != null && cmcProjectComment.getJyApprover() == null && cmcProjectComment.getJyApprovalTime() == null) {
            cmcProjectComment.setJyApprover(getLoginUser().getUserId());
            cmcProjectComment.setJyApprovalTime(new Date());
            return toAjax(cmcProjectCommentService.insertCmcProjectComment(cmcProjectComment));
        }
        else {
            return toAjax(cmcProjectCommentService.updateCmcProjectComment(cmcProjectComment));
        }
    }

    /**
     * 修改cmc项目审批
     */
    @Log(title = "cmc项目审批", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcProjectComment cmcProjectComment)
    {
        if (cmcProjectComment.getManageComment() != null && cmcProjectComment.getManageApprover() == null && cmcProjectComment.getManageApprovalTime() == null && cmcProjectComment.getScComment() == null) {
            cmcProjectComment.setManageApprover(getLoginUser().getUserId());
            cmcProjectComment.setManageApprovalTime(new Date());
        }
        if (cmcProjectComment.getScComment() != null) {
            CmcProjectComment projectComment = cmcProjectCommentService.selectCmcProjectCommentByProjectId(cmcProjectComment.getProjectId());
            StringBuilder scApprover = new StringBuilder();
            if (projectComment.getScApprover() == null)
                scApprover.append(cmcProjectComment.getScApprover());
            else
                scApprover.append(projectComment.getScApprover()).append(",").append(cmcProjectComment.getScApprover());
            cmcProjectComment.setScApprover(scApprover.toString());
            cmcProjectComment.setScApprovalTime(new Date());
        }
        return toAjax(cmcProjectCommentService.updateCmcProjectComment(cmcProjectComment));
    }

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