package com.ruoyi.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cs.domain.CsContractDetails;
import com.ruoyi.cs.domain.template.CsContractDetailsTemplate;
import com.ruoyi.cs.service.ICsAnnexService;
import com.ruoyi.cs.service.ICsContractDetailsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 合同信息详情Controller
 *
 * @author ruoyi
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/cs/csContractDetails")
public class CsContractDetailsController extends BaseController
{
    @Resource
    private ICsContractDetailsService csContractDetailsService;
    @Resource
    private ICsAnnexService csAnnexService;

    /**
     * 查询合同信息详情列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csContractDetails:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsContractDetails csContractDetails)
    {
        startPage();
        csContractDetails.setDelFlag("0");
        List<CsContractDetails> list = csContractDetailsService.selectCsContractDetailsList(csContractDetails);
        return getDataTable(list);
    }

    /**
     * 导出合同信息详情列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csContractDetails:export')")
    @Log(title = "合同信息详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsContractDetails csContractDetails)
    {
        csContractDetails.setDelFlag("0");
        List<CsContractDetails> list = csContractDetailsService.selectCsContractDetailsList(csContractDetails);
        if (list.size() > 0) {
            Integer serialNumber = 1;
            for (CsContractDetails contractDetails : list) {
                // List<CsAnnexApplyVO> annexList = csAnnexService.getApplyFileList(contractDetails.getContractDetailsId(), "2");
                // if (annexList.size() > 0) {
                //     contractDetails.setAnnexUrl(annexList.stream().map(CsAnnexApplyVO::getAnnexUrl).collect(Collectors.joining(",")));
                // }

                contractDetails.setSerialNumber(serialNumber);
                serialNumber ++;
            }
        }

        ExcelUtil<CsContractDetails> util = new ExcelUtil<CsContractDetails>(CsContractDetails.class);
        util.exportExcel(response, list, "合同信息详情数据");
    }

    /**
     * 导出合同信息详情列表模版
     */
    @Log(title = "合同信息详情模版", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response)
    {
        ExcelUtil<CsContractDetailsTemplate> util = new ExcelUtil<CsContractDetailsTemplate>(CsContractDetailsTemplate.class);
        util.exportExcel(response, null, "合同信息详情数据模版");
    }

    /**
     * 获取合同信息详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csContractDetails:query')")
    @GetMapping(value = "/{contractDetailsId}")
    public AjaxResult getInfo(@PathVariable("contractDetailsId") String contractDetailsId)
    {
        return AjaxResult.success(csContractDetailsService.selectCsContractDetailsByContractDetailsId(contractDetailsId));
    }

    /**
     * 新增合同信息详情
     */
    @PreAuthorize("@ss.hasPermi('cs:csContractDetails:add')")
    @Log(title = "合同信息详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsContractDetails csContractDetails)
    {
        return toAjax(csContractDetailsService.insertCsContractDetails(csContractDetails));
    }

    /**
     * 修改合同信息详情
     */
    @PreAuthorize("@ss.hasPermi('cs:csContractDetails:edit')")
    @Log(title = "合同信息详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsContractDetails csContractDetails)
    {
        return toAjax(csContractDetailsService.updateCsContractDetails(csContractDetails));
    }

    /**
     * 删除合同信息详情
     */
    @PreAuthorize("@ss.hasPermi('cs:csContractDetails:remove')")
    @Log(title = "合同信息详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contractDetailsIds}")
    public AjaxResult remove(@PathVariable String[] contractDetailsIds)
    {
        return toAjax(csContractDetailsService.deleteCsContractDetailsByContractDetailsIds(contractDetailsIds));
    }


    /**
     * 导入合同信息记录
     */
    @Log(title = "导入合同信息记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importCsContractDetails")
    public AjaxResult importCsContractDetails(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CsContractDetailsTemplate> util = new ExcelUtil<CsContractDetailsTemplate>(CsContractDetailsTemplate.class);
        List<CsContractDetailsTemplate> list = util.importExcel(file.getInputStream());
        String message = csContractDetailsService.importCsContractDetails(list, updateSupport);
        return AjaxResult.success(message);
    }
}
