package com.ruoyi.web.controller.oa;

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

import com.ruoyi.system.service.ISysDeptService;
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.CmcProjectChange;
import com.ruoyi.oa.service.ICmcProjectChangeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc项目变更Controller
 * 
 * @author cmc
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/oa/projectChange")
public class CmcProjectChangeController extends BaseController
{
    @Autowired
    private ICmcProjectChangeService cmcProjectChangeService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询cmc项目变更列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcProjectChange cmcProjectChange)
    {
        startPage();
        List<CmcProjectChange> list = cmcProjectChangeService.selectCmcProjectChangeList(cmcProjectChange);
        for (CmcProjectChange 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));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出cmc项目变更列表
     */
    @Log(title = "cmc项目变更", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcProjectChange cmcProjectChange)
    {
        List<CmcProjectChange> list = cmcProjectChangeService.selectCmcProjectChangeList(cmcProjectChange);
        for (CmcProjectChange 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));
            }
        }
        ExcelUtil<CmcProjectChange> util = new ExcelUtil<CmcProjectChange>(CmcProjectChange.class);
        util.exportExcel(response, list, "cmc项目变更数据");
    }

    /**
     * 获取cmc项目变更详细信息
     */
    @GetMapping(value = "/{changeId}")
    public AjaxResult getInfo(@PathVariable("changeId") String changeId)
    {
        return success(cmcProjectChangeService.selectCmcProjectChangeByChangeId(changeId));
    }

    /**
     * 新增cmc项目变更
     */
    @Log(title = "cmc项目变更", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcProjectChange cmcProjectChange)
    {
        return toAjax(cmcProjectChangeService.insertCmcProjectChange(cmcProjectChange));
    }

    /**
     * 修改cmc项目变更
     */
    @Log(title = "cmc项目变更", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcProjectChange cmcProjectChange)
    {
        return toAjax(cmcProjectChangeService.updateCmcProjectChange(cmcProjectChange));
    }

    /**
     * 删除cmc项目变更
     */
    @Log(title = "cmc项目变更", businessType = BusinessType.DELETE)
	@DeleteMapping("/{changeIds}")
    public AjaxResult remove(@PathVariable String[] changeIds)
    {
        return success(cmcProjectChangeService.deleteCmcProjectChangeByChangeIds(changeIds));
    }
}
