package com.ruoyi.web.controller.oa;

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

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 cmcPartyAService;

    /**
     * 查询cmc领导分管列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcManageDept cmcManageDept)
    {
        startPage();
        List<CmcManageDept> list = cmcManageDeptService.selectCmcManageDeptList(cmcManageDept);
        for (CmcManageDept manageDept : list) {
            deptIdToName(manageDept);
            partyAIdToName(manageDept);
        }
        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) {
            deptIdToName(manageDept);
            partyAIdToName(manageDept);
        }
        ExcelUtil<CmcManageDept> util = new ExcelUtil<CmcManageDept>(CmcManageDept.class);
        util.exportExcel(response, list, "cmc领导分管数据");
    }

    public void deptIdToName(CmcManageDept cmcManageDept) {
        String deptId = cmcManageDept.getDeptId();
        String deptName = cmcManageDept.getDeptName();
        StringBuilder deptNames = new StringBuilder();
        if (deptId != null && !deptId.equals("") && (deptName == null || deptName.equals(""))) {
            for (String dept : deptId.split(","))
                deptNames.append(deptService.selectDeptById(Long.parseLong(dept)).getDeptName()).append(",");
            cmcManageDept.setDeptName(deptNames.substring(0, deptNames.length() - 1));
        }
    }

    public void partyAIdToName(CmcManageDept cmcManageDept) {
        String partyAId = cmcManageDept.getPartyAId();
        String partyAName = cmcManageDept.getPartyAName();
        StringBuilder partyANames = new StringBuilder();
        if (partyAId != null && !partyAId.equals("") && (partyAName == null || partyAName.equals(""))) {
            for (String partyA : partyAId.split(","))
                partyANames.append(cmcPartyAService.selectCmcPartyAByPartyAId(partyA).getPartyAName()).append(",");
            cmcManageDept.setPartyAName(partyANames.substring(0, partyANames.length() - 1));
        }
    }

    /**
     * 获取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 success(cmcManageDeptService.deleteCmcManageDeptByUserIds(userIds));
    }
}
