package com.ruoyi.web.controller.oa;

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

import com.ruoyi.oa.service.ICmcPartnerService;
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.CmcOutsource;
import com.ruoyi.oa.service.ICmcOutsourceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目委外Controller
 * 
 * @author cmc
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/oa/outsource")
public class CmcOutsourceController extends BaseController
{
    @Autowired
    private ICmcOutsourceService cmcOutsourceService;

    @Autowired
    private ICmcPartnerService cmcPartnerService;

    /**
     * 查询项目委外列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcOutsource cmcOutsource)
    {
        startPage();
        List<CmcOutsource> list = cmcOutsourceService.selectCmcOutsourceList(cmcOutsource);
        for (CmcOutsource outsource : list)
            partnerIdToName(outsource);
        return getDataTable(list);
    }

    /**
     * 导出项目委外列表
     */
    @Log(title = "项目委外", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcOutsource cmcOutsource)
    {
        List<CmcOutsource> list = cmcOutsourceService.selectCmcOutsourceList(cmcOutsource);
        for (CmcOutsource outsource : list)
            partnerIdToName(outsource);
        ExcelUtil<CmcOutsource> util = new ExcelUtil<CmcOutsource>(CmcOutsource.class);
        util.exportExcel(response, list, "项目委外数据");
    }

    /**
     * 获取项目委外详细信息
     */
    @GetMapping(value = "/{outsourceId}")
    public AjaxResult getInfo(@PathVariable("outsourceId") String outsourceId)
    {
        return success(cmcOutsourceService.selectCmcOutsourceByOutsourceId(outsourceId));
    }

    /**
     * 新增项目委外
     */
    @Log(title = "项目委外", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcOutsource cmcOutsource)
    {
        return toAjax(cmcOutsourceService.insertCmcOutsource(cmcOutsource));
    }

    /**
     * 修改项目委外
     */
    @Log(title = "项目委外", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcOutsource cmcOutsource)
    {
        return toAjax(cmcOutsourceService.updateCmcOutsource(cmcOutsource));
    }

    /**
     * 删除项目委外
     */
    @Log(title = "项目委外", businessType = BusinessType.DELETE)
	@DeleteMapping("/{outsourceIds}")
    public AjaxResult remove(@PathVariable String[] outsourceIds)
    {
        return success(cmcOutsourceService.deleteCmcOutsourceByOutsourceIds(outsourceIds));
    }

    public void partnerIdToName(CmcOutsource cmcOutsource) {
        String partnerId = cmcOutsource.getPartnerId();
        String partnerName = cmcOutsource.getPartnerName();
        StringBuilder partnerNames = new StringBuilder();
        if (partnerId != null && !partnerId.equals("") && (partnerName == null || partnerName.equals(""))) {
            for (String partner : partnerId.split(","))
                partnerNames.append(cmcPartnerService.selectCmcPartnerByPartnerId(partner).getPartnerName()).append(",");
            cmcOutsource.setPartnerName(partnerNames.substring(0, partnerNames.length() - 1));
        }
    }
}
