package com.ruoyi.web.controller.oa;

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

import com.ruoyi.oa.domain.CmcProject;
import com.ruoyi.oa.service.ICmcPartyAService;
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.CmcManageDept;
import com.ruoyi.oa.service.ICmcManageDeptService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc领导分管Controller
 * 
 * @author cmc
 * @date 2024-07-04
 */
@RestController
@RequestMapping("/oa/manageDept")
public class CmcManageDeptController extends BaseController
{
    @Autowired
    private ICmcManageDeptService cmcManageDeptService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ICmcPartyAService partyAService;

    /**
     * 查询cmc领导分管列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcManageDept cmcManageDept)
    {
        startPage();
        List<CmcManageDept> list = cmcManageDeptService.selectCmcManageDeptList(cmcManageDept);
        for (CmcManageDept manageDept : list) {
            String deptId = manageDept.getDeptId();
            StringBuilder deptName = new StringBuilder();
            if (deptId != null && !deptId.equals("") && deptId.split(",").length > 0) {
                for (String dept : deptId.split(","))
                    deptName.append(deptService.selectDeptById(Long.parseLong(dept)).getDeptName()).append(",");
                manageDept.setDeptName(deptName.substring(0, deptName.length() - 1));
            }
            String partyAId = manageDept.getPartyAId();
            StringBuilder partyAName = new StringBuilder();
            if (partyAId != null && !partyAId.equals("") && partyAId.split(",").length > 0) {
                for (String partyA : partyAId.split(","))
                    partyAName.append(partyAService.selectCmcPartyAByPartyAId(partyA).getPartyAName()).append(",");
                manageDept.setPartyAName(partyAName.substring(0, partyAName.length() - 1));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出cmc领导分管列表
     */
    @Log(title = "cmc领导分管", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcManageDept cmcManageDept)
    {
        List<CmcManageDept> list = cmcManageDeptService.selectCmcManageDeptList(cmcManageDept);
        for (CmcManageDept manageDept : list) {
            String deptId = manageDept.getDeptId();
            StringBuilder deptName = new StringBuilder();
            if (deptId != null && !deptId.equals("") && deptId.split(",").length > 0) {
                for (String dept : deptId.split(","))
                    deptName.append(deptService.selectDeptById(Long.parseLong(dept)).getDeptName()).append(",");
                manageDept.setDeptName(deptName.substring(0, deptName.length() - 1));
            }
            String partyAId = manageDept.getPartyAId();
            StringBuilder partyAName = new StringBuilder();
            if (partyAId != null && !partyAId.equals("") && partyAId.split(",").length > 0) {
                for (String partyA : partyAId.split(","))
                    partyAName.append(partyAService.selectCmcPartyAByPartyAId(partyA).getPartyAName()).append(",");
                manageDept.setPartyAName(partyAName.substring(0, partyAName.length() - 1));
            }
        }
        ExcelUtil<CmcManageDept> util = new ExcelUtil<CmcManageDept>(CmcManageDept.class);
        util.exportExcel(response, list, "cmc领导分管数据");
    }

    /**
     * 获取cmc领导分管详细信息
     */
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(cmcManageDeptService.selectCmcManageDeptByUserId(userId));
    }

    /**
     * 新增cmc领导分管
     */
    @Log(title = "cmc领导分管", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcManageDept cmcManageDept)
    {
        return toAjax(cmcManageDeptService.insertCmcManageDept(cmcManageDept));
    }

    /**
     * 修改cmc领导分管
     */
    @Log(title = "cmc领导分管", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcManageDept cmcManageDept)
    {
        return toAjax(cmcManageDeptService.updateCmcManageDept(cmcManageDept));
    }

    /**
     * 删除cmc领导分管
     */
    @Log(title = "cmc领导分管", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(cmcManageDeptService.deleteCmcManageDeptByUserIds(userIds));
    }
}
