package com.ruoyi.web.controller.oa;

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

import com.ruoyi.common.utils.SnowFlake;
import com.ruoyi.oa.service.ICmcPartnerService;
import com.ruoyi.oa.service.ICmcPartyAService;
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.CmcBrandProject;
import com.ruoyi.oa.service.ICmcBrandProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc品牌项目Controller
 * 
 * @author cmc
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/oa/brandProject")
public class CmcBrandProjectController extends BaseController
{
    @Autowired
    private ICmcBrandProjectService cmcBrandProjectService;

    @Autowired
    private ICmcPartnerService cmcPartnerService;

    @Autowired
    private ICmcPartyAService cmcPartyAService;

    /**
     * 查询cmc品牌项目列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcBrandProject cmcBrandProject)
    {
        startPage();
        List<CmcBrandProject> list = cmcBrandProjectService.selectCmcBrandProjectList(cmcBrandProject);
        return getDataTable(list);
    }

    /**
     * 导出cmc品牌项目列表
     */
    @Log(title = "cmc品牌项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcBrandProject cmcBrandProject)
    {
        List<CmcBrandProject> list = cmcBrandProjectService.selectCmcBrandProjectList(cmcBrandProject);
        for (CmcBrandProject brandProject : list) {
            partyAIdToName(brandProject);
            partnerIdToName(brandProject);
        }
        ExcelUtil<CmcBrandProject> util = new ExcelUtil<CmcBrandProject>(CmcBrandProject.class);
        util.exportExcel(response, list, "cmc品牌项目数据");
    }

    public void partyAIdToName(CmcBrandProject cmcBrandProject) {
        String partyAId = cmcBrandProject.getPartyAId();
        String partyAName = cmcBrandProject.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(",");
            cmcBrandProject.setPartyAName(partyANames.substring(0, partyANames.length() - 1));
        }
    }

    public void partnerIdToName(CmcBrandProject cmcBrandProject) {
        String partnerId = cmcBrandProject.getPartnerId();
        String partnerName = cmcBrandProject.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(",");
            cmcBrandProject.setPartnerName(partnerNames.substring(0, partnerNames.length() - 1));
        }
    }

    /**
     * 获取cmc品牌项目详细信息
     */
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") String projectId)
    {
        return success(cmcBrandProjectService.selectCmcBrandProjectByProjectId(projectId));
    }

    /**
     * 新增cmc品牌项目
     */
    @Log(title = "cmc品牌项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcBrandProject cmcBrandProject)
    {
        cmcBrandProject.setProjectId(new SnowFlake().generateId());
        return toAjax(cmcBrandProjectService.insertCmcBrandProject(cmcBrandProject));
    }

    /**
     * 修改cmc品牌项目
     */
    @Log(title = "cmc品牌项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcBrandProject cmcBrandProject)
    {
        cmcBrandProject.setProjectRegistrant(getLoginUser().getUserId());
        cmcBrandProject.setRegisterTime(new Date());
        return toAjax(cmcBrandProjectService.updateCmcBrandProject(cmcBrandProject));
    }

    /**
     * 删除cmc品牌项目
     */
    @Log(title = "cmc品牌项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable String[] projectIds)
    {
        return success(cmcBrandProjectService.deleteCmcBrandProjectByProjectIds(projectIds));
    }
}
