package com.jy.business.capital.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.business.capital.model.dto.CapitalTransactionsDto;
import com.jy.business.capital.model.dto.CapitalTransactionsSelectDto;
import com.jy.business.capital.model.vo.CapitalTransactionsExportVo;
import com.jy.business.capital.model.vo.CapitalTransactionsVo;
import com.jy.business.capital.service.CapitalTransactionsService;
import com.jy.framework.model.base.BaseSelectDto;
import com.jy.framework.model.validation.AddGroup;
import com.jy.framework.model.validation.BatchDeleteGroup;
import com.jy.framework.model.validation.DetailGroup;
import com.jy.framework.model.validation.EditGroup;
import com.jy.framework.utils.excel.ExcelUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 资金流水 前端控制器
 * </p>
 *
 * @author
 * @since 2024-10-16
 */
@RestController
@RequestMapping("/capitalTransactions")
public class CapitalTransactionsController {

    @Resource
    private HttpServletResponse response;

    @Resource
    private CapitalTransactionsService capitalTransactionsService;

    /**
     * 资金流水分页
     */
    @GetMapping("/getPage")
    public Page<CapitalTransactionsVo> getPage(CapitalTransactionsSelectDto dto) {
        return capitalTransactionsService.getPage(dto);
    }

    /**
     * 导出
     */
    @GetMapping("/excelExport")
    public void excelExport(CapitalTransactionsSelectDto dto) {
        dto.setSearchAll(true);
        Page<CapitalTransactionsVo> page = getPage(dto);
        List<CapitalTransactionsVo> records = page.getRecords();

        List<CapitalTransactionsExportVo> list = BeanUtil.copyToList(records, CapitalTransactionsExportVo.class);
        ExcelUtil.exportExcel(list, "资金流水", CapitalTransactionsExportVo.class, response);
    }

    /**
     * 资金流水明细
     */
    @GetMapping("/getDetail")
    public CapitalTransactionsVo getDetail(@Validated(DetailGroup.class) BaseSelectDto dto) {
        return capitalTransactionsService.getDetail(dto.getId());
    }

    /**
     * 资金流水新增
     */
    @PostMapping("/add")
    public void add(@Validated(AddGroup.class) @RequestBody CapitalTransactionsDto dto) {
        capitalTransactionsService.add(dto);
    }

    /**
     * 资金流水编辑
     */
    @PostMapping("/edit")
    public void edit(@Validated(EditGroup.class) @RequestBody CapitalTransactionsDto dto) {
        capitalTransactionsService.edit(dto);
    }

    /**
     * 资金流水删除
     */
    @PostMapping("/delete")
    public void delete(@Validated(BatchDeleteGroup.class) @RequestBody BaseSelectDto dto) {
        capitalTransactionsService.delete(dto.getIdList());
    }

}
