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.CsIncomeDetails;
import com.ruoyi.cs.domain.template.CsIncomeDetailsTemplate;
import com.ruoyi.cs.service.ICsAnnexService;
import com.ruoyi.cs.service.ICsIncomeDetailsService;
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-07
 */
@RestController
@RequestMapping("/cs/csIncomeDetails")
public class CsIncomeDetailsController extends BaseController
{
    @Resource
    private ICsIncomeDetailsService csIncomeDetailsService;
    @Resource
    private ICsAnnexService csAnnexService;

    /**
     * 查询收入明细列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csIncomeDetails:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsIncomeDetails csIncomeDetails)
    {
        startPage();
        csIncomeDetails.setDelFlag("0");
        List<CsIncomeDetails> list = csIncomeDetailsService.selectCsIncomeDetailsList(csIncomeDetails);
        return getDataTable(list);
    }

    /**
     * 导出收入明细列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csIncomeDetails:export')")
    @Log(title = "收入明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsIncomeDetails csIncomeDetails)
    {
        List<CsIncomeDetails> list = csIncomeDetailsService.selectCsIncomeDetailsList(csIncomeDetails);
        if (list.size() > 0) {
            Integer serialNumber = 1;
            for (CsIncomeDetails incomeDetails : list) {
                // List<CsAnnexApplyVO> annexList = csAnnexService.getApplyFileList(incomeDetails.getIncomeDetailsId(), "2");
                // if (annexList.size() > 0) {
                //     incomeDetails.setAnnex(annexList.stream().map(CsAnnexApplyVO::getAnnexUrl).collect(Collectors.joining(",")));
                // }

                incomeDetails.setSerialNumber(serialNumber);
                serialNumber ++;
            }
        }
        ExcelUtil<CsIncomeDetails> util = new ExcelUtil<CsIncomeDetails>(CsIncomeDetails.class);
        util.exportExcel(response, list, "收入明细数据");
    }

    /**
     * 导出收入明细列表模板
     */
    @Log(title = "收入明细模板", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response)
    {
        ExcelUtil<CsIncomeDetails> util = new ExcelUtil<CsIncomeDetails>(CsIncomeDetails.class);
        util.exportExcel(response, null, "收入明细数据模板");
    }

    /**
     * 获取收入明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csIncomeDetails:query')")
    @GetMapping(value = "/{incomeDetailsId}")
    public AjaxResult getInfo(@PathVariable("incomeDetailsId") String incomeDetailsId)
    {
        return AjaxResult.success(csIncomeDetailsService.selectCsIncomeDetailsByIncomeDetailsId(incomeDetailsId));
    }

    /**
     * 新增收入明细
     */
    @PreAuthorize("@ss.hasPermi('cs:csIncomeDetails:add')")
    @Log(title = "收入明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsIncomeDetails csIncomeDetails)
    {
        return toAjax(csIncomeDetailsService.insertCsIncomeDetails(csIncomeDetails));
    }

    /**
     * 修改收入明细
     */
    @PreAuthorize("@ss.hasPermi('cs:csIncomeDetails:edit')")
    @Log(title = "收入明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsIncomeDetails csIncomeDetails)
    {
        return toAjax(csIncomeDetailsService.updateCsIncomeDetails(csIncomeDetails));
    }

    /**
     * 删除收入明细
     */
    @PreAuthorize("@ss.hasPermi('cs:csIncomeDetails:remove')")
    @Log(title = "收入明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{incomeDetailsIds}")
    public AjaxResult remove(@PathVariable String[] incomeDetailsIds)
    {
        return toAjax(csIncomeDetailsService.deleteCsIncomeDetailsByIncomeDetailsIds(incomeDetailsIds));
    }


    /**
     * 导入收入明细记录
     */
    @Log(title = "导入收入明细记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importCsIncomeDetails")
    public AjaxResult importCsIncomeDetails(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CsIncomeDetailsTemplate> util = new ExcelUtil<CsIncomeDetailsTemplate>(CsIncomeDetailsTemplate.class);
        List<CsIncomeDetailsTemplate> list = util.importExcel(file.getInputStream());
        String message = csIncomeDetailsService.importCsIncomeDetails(list, updateSupport);
        return AjaxResult.success(message);
    }
}
