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.CmcTechnicalPlan;
import com.ruoyi.oa.service.ICmcTechnicalPlanService;
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/technicalPlan")
public class CmcTechnicalPlanController extends BaseController
{
    @Autowired
    private ICmcTechnicalPlanService cmcTechnicalPlanService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询cmc技术方案列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcTechnicalPlan cmcTechnicalPlan)
    {
        startPage();
        List<CmcTechnicalPlan> list = cmcTechnicalPlanService.selectCmcTechnicalPlanList(cmcTechnicalPlan);
        return getDataTable(list);
    }

    /**
     * 导出cmc技术方案列表
     */
    @Log(title = "cmc技术方案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcTechnicalPlan cmcTechnicalPlan)
    {
        List<CmcTechnicalPlan> list = cmcTechnicalPlanService.selectCmcTechnicalPlanList(cmcTechnicalPlan);
        for (CmcTechnicalPlan technical : list) {
            String qualityInspector = technical.getQualityInspector();
            String qualityInspectorName = technical.getQualityInspectorName();
            StringBuilder qualityInspectorNames = new StringBuilder();
            if (qualityInspector != null && !qualityInspector.equals("") &&  (qualityInspectorName == null || qualityInspectorName.equals(""))) {
                for (String inspector : qualityInspector.split(","))
                    qualityInspectorNames.append(userService.selectUserById(Long.parseLong(inspector)).getNickName()).append(",");
                technical.setQualityInspectorName(qualityInspectorNames.substring(0, qualityInspectorNames.length() - 1));
            }
        }
        ExcelUtil<CmcTechnicalPlan> util = new ExcelUtil<CmcTechnicalPlan>(CmcTechnicalPlan.class);
        util.exportExcel(response, list, "cmc技术方案数据");
    }

    /**
     * 获取cmc技术方案详细信息
     */
    @GetMapping(value = "/{technicalPlanId}")
    public AjaxResult getInfo(@PathVariable("technicalPlanId") String technicalPlanId)
    {
        return success(cmcTechnicalPlanService.selectCmcTechnicalPlanByTechnicalPlanId(technicalPlanId));
    }

    /**
     * 新增cmc技术方案
     */
    @Log(title = "cmc技术方案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcTechnicalPlan cmcTechnicalPlan)
    {
        cmcTechnicalPlan.setTechnicalTime(new Date());
        cmcTechnicalPlan.setTechnicalPlanner(getLoginUser().getUserId());
        return toAjax(cmcTechnicalPlanService.insertCmcTechnicalPlan(cmcTechnicalPlan));
    }

    /**
     * 修改cmc技术方案
     */
    @Log(title = "cmc技术方案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcTechnicalPlan cmcTechnicalPlan)
    {
        if (cmcTechnicalPlan.getTechnicalDocument() != null && cmcTechnicalPlan.getTechnicalComment() == null && cmcTechnicalPlan.getModifyDocument() == null) {
            cmcTechnicalPlan.setTechnicalDesigner(getLoginUser().getUserId());
            cmcTechnicalPlan.setTechnicalUploadTime(new Date());
        }
        if ((cmcTechnicalPlan.getModifyDocument() != null || cmcTechnicalPlan.getTechnicalComment() != null) && cmcTechnicalPlan.getTechDeptComment() == null) {
            cmcTechnicalPlan.setTechnicalAuditor(getLoginUser().getUserId());
            cmcTechnicalPlan.setTechApprovalTime(new Date());
        }
        if ((cmcTechnicalPlan.getReviseDocument() != null || cmcTechnicalPlan.getTechDeptComment() != null) && cmcTechnicalPlan.getManageComment() == null) {
            cmcTechnicalPlan.setTechDeptLeader(getLoginUser().getUserId());
            cmcTechnicalPlan.setTechDeptTime(new Date());
        }
        if (cmcTechnicalPlan.getManageComment() != null || cmcTechnicalPlan.getFinalDocument() != null) {
            cmcTechnicalPlan.setManagerUserId(getLoginUser().getUserId());
            cmcTechnicalPlan.setManageApprovalTime(new Date());
        }
        return toAjax(cmcTechnicalPlanService.updateCmcTechnicalPlan(cmcTechnicalPlan));
    }

    /**
     * 删除cmc技术方案
     */
    @Log(title = "cmc技术方案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{technicalPlanIds}")
    public AjaxResult remove(@PathVariable String[] technicalPlanIds)
    {
        return success(cmcTechnicalPlanService.deleteCmcTechnicalPlanByTechnicalPlanIds(technicalPlanIds));
    }
}
