package com.ruoyi.web.controller.oa;

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

import com.ruoyi.oa.service.ICmcPartyAService;
import com.ruoyi.oa.service.ICmcProjectWorkService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.CmcProject;
import com.ruoyi.oa.service.ICmcProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc项目Controller
 * 
 * @author cmc
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/oa/project")
public class CmcProjectController extends BaseController
{
    @Autowired
    private ICmcProjectService cmcProjectService;

    @Autowired
    private ICmcPartyAService cmcPartyAService;

    @Autowired
    private ICmcProjectWorkService cmcProjectWorkService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询cmc项目列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcProject cmcProject)
    {
        startPage();
        List<CmcProject> list = cmcProjectService.selectCmcProjectList(cmcProject);
        for (CmcProject project : list) {
            String underTakingDept = project.getUndertakingDept();
            StringBuilder underTakingDeptName = new StringBuilder();
            if (underTakingDept != null && !underTakingDept.equals("") && underTakingDept.split(",").length > 0) {
                for (String dept : underTakingDept.split(","))
                    underTakingDeptName.append(deptService.selectDeptById(Long.parseLong(dept)).getDeptName()).append(",");
                project.setUndertakingDeptName(underTakingDeptName.substring(0, underTakingDeptName.length() - 1));
            }
            String qualityInspector = project.getQualityInspector();
            StringBuilder qualityInspectorName = new StringBuilder();
            if (qualityInspector != null && !qualityInspector.equals("") && qualityInspector.split(",").length > 0) {
                for (String inspector : qualityInspector.split(","))
                    qualityInspectorName.append(userService.selectUserById(Long.parseLong(inspector)).getNickName()).append(",");
                project.setQualityInspectorName(qualityInspectorName.substring(0, qualityInspectorName.length() - 1));
            }
        }
        return getDataTable(list);
    }

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

    /**
     * 获取cmc项目详细信息
     */
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") String projectId)
    {
        CmcProject cmcProject = cmcProjectService.selectCmcProjectByProjectId(projectId);
        String underTakingDept = cmcProject.getUndertakingDept();
        StringBuilder underTakingDeptName = new StringBuilder();
        if (underTakingDept != null && !underTakingDept.equals("") && underTakingDept.split(",").length > 0) {
            for (String dept : underTakingDept.split(","))
                underTakingDeptName.append(deptService.selectDeptById(Long.parseLong(dept)).getDeptName()).append(",");
            cmcProject.setUndertakingDeptName(underTakingDeptName.substring(0, underTakingDeptName.length() - 1));
        }
        String qualityInspector = cmcProject.getQualityInspector();
        StringBuilder qualityInspectorName = new StringBuilder();
        if (qualityInspector != null && !qualityInspector.equals("") && qualityInspector.split(",").length > 0) {
            for (String inspector : qualityInspector.split(","))
                qualityInspectorName.append(userService.selectUserById(Long.parseLong(inspector)).getNickName()).append(",");
            cmcProject.setQualityInspectorName(qualityInspectorName.substring(0, qualityInspectorName.length() - 1));
        }
        return success(cmcProject);
    }

    /**
     * 获取项目负责人负责的项目详细信息
     */
    @GetMapping("/projectLeader")
    public AjaxResult getListByProjectLeader(String projectLeader)
    {
        return success(cmcProjectService.selectCmcProjectListByProjectLeader(projectLeader));
    }

    /**
     * 新增cmc项目
     */
    @Log(title = "cmc项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcProject cmcProject)
    {
        cmcProject.setRegisterTime(new Date());
        cmcProject.setIsFinished("0");
        return toAjax(cmcProjectService.insertCmcProject(cmcProject));
    }

    /**
     * 修改cmc项目
     */
    @Log(title = "cmc项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcProject cmcProject)
    {
        return toAjax(cmcProjectService.updateCmcProject(cmcProject));
    }

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