package com.ruoyi.web.controller.oa;

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

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnowFlake;
import com.ruoyi.oa.domain.CmcProjectWork;
import com.ruoyi.oa.service.ICmcProjectWorkService;
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 ICmcProjectWorkService cmcProjectWorkService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询cmc项目列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcProject cmcProject)
    {
        startPage();
        List<CmcProject> list = new ArrayList<>();
        if (cmcProject.getProjectLeaderName() != null && !cmcProject.getProjectLeaderName().equals("")) {
            SysUser user = new SysUser();
            user.setNickName(cmcProject.getProjectLeaderName());
            List<SysUser> userList = userService.selectUserList(user);
            if (userList.size() > 0)
                for (SysUser sysUser : userList) {
                    cmcProject.setProjectLeader(sysUser.getUserId().toString());
                    list.addAll(cmcProjectService.selectCmcProjectList(cmcProject));
                }
            else
                list = cmcProjectService.selectCmcProjectList(cmcProject);
        }
        else
            list = cmcProjectService.selectCmcProjectList(cmcProject);
        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)
    {
        return success(cmcProjectService.selectCmcProjectByProjectId(projectId));
    }

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

    /**
     * 新增cmc项目
     */
    @Log(title = "cmc项目", businessType = BusinessType.INSERT)
    @PostMapping( "/submit")
    public AjaxResult submit(@RequestParam("form")String formData)
    {
        JSONObject formDataJson = JSONObject.parse(formData);
        CmcProject cmcProject = new CmcProject();
        cmcProject.setProjectName(formDataJson.getString("projectName"));
        cmcProject.setProjectNumber(formDataJson.getString("projectNumber"));
        cmcProject.setProjectLeader(formDataJson.getString("projectLeader"));
        cmcProject.setIsFinished(formDataJson.getString("isFinished"));
        cmcProject.setProjectSource(formDataJson.getString("projectSource"));
        cmcProject.setPartyA(formDataJson.getString("partyA"));
        cmcProject.setContactPerson(formDataJson.getString("contactPerson"));
        cmcProject.setTelephone(formDataJson.getString("telephone"));
        cmcProject.setProjectType(formDataJson.getString("projectType"));
        cmcProject.setProjectLevel(formDataJson.getString("projectLevel"));
        cmcProject.setProjectRegistrant(formDataJson.getString("projectRegistrant"));
        cmcProject.setCreateTime(DateUtils.getNowDate());
        cmcProject.setIsFinished("0");
        cmcProject.setRemark(formDataJson.getString("remark"));
        cmcProjectService.insertCmcProject(cmcProject);
        String workList = formDataJson.getString("workList");
        workList = workList.substring(1, workList.length() - 1);
        if (workList.split("}").length > 1) {
            for (int i = 0; i < workList.split("}").length; i++) {
                String workArri = workList.split("}")[i] + "}";
                if (i > 0)
                    workArri = workArri.substring(1);
                JSONObject workArrObject = JSONObject.parse(workArri);
                CmcProjectWork cmcProjectWork = new CmcProjectWork();
                cmcProjectWork.setWorkId(new SnowFlake().generateId());
                cmcProjectWork.setProjectId(cmcProject.getProjectId());
                cmcProjectWork.setContent(workArrObject.getString("content"));
                cmcProjectWork.setScale(workArrObject.getString("scale"));
                cmcProjectWork.setUnit(workArrObject.getString("unit"));
                cmcProjectWork.setWorkload(workArrObject.getString("workload"));
                cmcProjectWork.setDeadline(DateUtils.dateTime("yyyy-MM-dd", workArrObject.getString("deadline")));
                cmcProjectWork.setRemark(workArrObject.getString("remark"));
                cmcProjectWorkService.insertCmcProjectWork(cmcProjectWork);
            }
        }
        else {
            JSONObject workArrObject = JSONObject.parse(workList);
            CmcProjectWork cmcProjectWork = new CmcProjectWork();
            cmcProjectWork.setWorkId(new SnowFlake().generateId());
            cmcProjectWork.setProjectId(cmcProject.getProjectId());
            cmcProjectWork.setContent(workArrObject.getString("content"));
            cmcProjectWork.setScale(workArrObject.getString("scale"));
            cmcProjectWork.setUnit(workArrObject.getString("unit"));
            cmcProjectWork.setWorkload(workArrObject.getString("workload"));
            cmcProjectWork.setDeadline(DateUtils.dateTime("yyyy-MM-dd", workArrObject.getString("deadline")));
            cmcProjectWork.setRemark(workArrObject.getString("remark"));
            cmcProjectWorkService.insertCmcProjectWork(cmcProjectWork);
        }
        return success("新增成功");
    }

    /**
     * 修改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.UPDATE)
    @PutMapping( "/modify")
    public AjaxResult modify(@RequestParam("form") String formData)
    {
        JSONObject formDataJson = JSONObject.parse(formData);
        CmcProject cmcProject = cmcProjectService.selectCmcProjectByProjectId(formDataJson.getString("projectId"));
        cmcProject.setProjectName(formDataJson.getString("projectName"));
        cmcProject.setProjectNumber(formDataJson.getString("projectNumber"));
        cmcProject.setProjectLeader(formDataJson.getString("projectLeader"));
        cmcProject.setIsFinished(formDataJson.getString("isFinished"));
        cmcProject.setProjectSource(formDataJson.getString("projectSource"));
        cmcProject.setPartyA(formDataJson.getString("partyA"));
        cmcProject.setContactPerson(formDataJson.getString("contactPerson"));
        cmcProject.setTelephone(formDataJson.getString("telephone"));
        cmcProject.setProjectType(formDataJson.getString("projectType"));
        cmcProject.setProjectLevel(formDataJson.getString("projectLevel"));
        cmcProject.setProjectRegistrant(formDataJson.getString("projectRegistrant"));
        cmcProject.setCreateTime(DateUtils.getNowDate());
        cmcProject.setIsFinished("0");
        cmcProject.setRemark(formDataJson.getString("remark"));
        String workList = formDataJson.getString("workList");
        workList = workList.substring(1, workList.length() - 1);
        if (workList.split("}").length > 1) {
            cmcProjectWorkService.deleteCmcProjectWorkByProjectId(formDataJson.getString("projectId"));
            for (int i = 0; i < workList.split("}").length; i++) {
                String workArri = workList.split("}")[i] + "}";
                if (i > 0)
                    workArri = workArri.substring(1);
                JSONObject workArrObject = JSONObject.parse(workArri);
                CmcProjectWork cmcProjectWork = new CmcProjectWork();
                cmcProjectWork.setWorkId(new SnowFlake().generateId());
                cmcProjectWork.setProjectId(formDataJson.getInteger("projectId"));
                cmcProjectWork.setContent(workArrObject.getString("content"));
                cmcProjectWork.setScale(workArrObject.getString("scale"));
                cmcProjectWork.setUnit(workArrObject.getString("unit"));
                cmcProjectWork.setWorkload(workArrObject.getString("workload"));
                cmcProjectWork.setDeadline(DateUtils.dateTime("yyyy-MM-dd", workArrObject.getString("deadline")));
                cmcProjectWork.setRemark(workArrObject.getString("remark"));
                cmcProjectWorkService.insertCmcProjectWork(cmcProjectWork);
            }
        }
        else {
            JSONObject workArrObject = JSONObject.parse(workList);
            cmcProjectWorkService.deleteCmcProjectWorkByProjectId(formDataJson.getString("projectId"));
            CmcProjectWork cmcProjectWork = new CmcProjectWork();
            cmcProjectWork.setWorkId(new SnowFlake().generateId());
            cmcProjectWork.setProjectId(formDataJson.getInteger("projectId"));
            cmcProjectWork.setContent(workArrObject.getString("content"));
            cmcProjectWork.setScale(workArrObject.getString("scale"));
            cmcProjectWork.setUnit(workArrObject.getString("unit"));
            cmcProjectWork.setWorkload(workArrObject.getString("workload"));
            cmcProjectWork.setDeadline(DateUtils.dateTime("yyyy-MM-dd", workArrObject.getString("deadline")));
            cmcProjectWork.setRemark(workArrObject.getString("remark"));
            cmcProjectWorkService.insertCmcProjectWork(cmcProjectWork);
        }
        return toAjax(cmcProjectService.updateCmcProject(cmcProject));
    }

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