package cn.iocoder.yudao.module.bpm.controller.admin.financialpayment;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.UploadUserFile;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.bpm.controller.admin.financialpayment.vo.FinancialPaymentPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.financialpayment.vo.FinancialPaymentRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.financialpayment.vo.FinancialPaymentSaveReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.financialpayment.vo.FinancialPaymentSaveVO;
import cn.iocoder.yudao.module.bpm.controller.admin.financialpaymentitem.vo.FinancialPaymentItemRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.financialpayment.FinancialPaymentDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.financialpaymentitem.FinancialPaymentItemDO;
import cn.iocoder.yudao.module.bpm.service.financialpayment.FinancialPaymentService;
import cn.iocoder.yudao.module.bpm.service.financialpaymentitem.FinancialPaymentItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 财务支付管理")
@RestController
@RequestMapping("/bpm/financial-payment")
@Validated
public class FinancialPaymentController {

    @Resource
    private FinancialPaymentService financialPaymentService;
    @Resource
    private FinancialPaymentItemService financialPaymentItemService;

    @PostMapping("/create")
    @Operation(summary = "创建财务支付子表")
    public CommonResult<Long> createFinancialPayment(@Valid @RequestBody FinancialPaymentSaveVO vo) {
        return success(financialPaymentService.createFinancialPayment(vo));
    }

    @PutMapping("/update")
    @Operation(summary = "更新财务支付管理")
    public CommonResult<Boolean> updateFinancialPayment(@Valid @RequestBody FinancialPaymentSaveReqVO updateReqVO) {
        financialPaymentService.updateFinancialPayment(updateReqVO);
        return success(true);
    }

    @PostMapping("/receive")
    @Operation(summary = "领取财务支付管理")
    public CommonResult<Boolean> receive(@RequestBody List<Long> ids) {
        financialPaymentService.receive(ids);
        return success(true);
    }

    @PostMapping("/cancel")
    @Operation(summary = "取消领取")
    public CommonResult<Boolean> cancel(@RequestBody List<Long> ids) {
        financialPaymentService.cancel(ids);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除财务支付管理")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteFinancialPayment(@RequestParam("id") Long id) {
        financialPaymentService.deleteFinancialPayment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得财务支付管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<FinancialPaymentRespVO> getFinancialPayment(@RequestParam("id") Long id) {
        FinancialPaymentDO financialPayment = financialPaymentService.getFinancialPayment(id);
        FinancialPaymentRespVO vo = BeanUtils.toBean(financialPayment, FinancialPaymentRespVO.class);
        List<FinancialPaymentItemDO> list = financialPaymentItemService.getByFinancialPaymentId(id);
        List<FinancialPaymentItemRespVO> items = new ArrayList<>();
        for (FinancialPaymentItemDO financialPaymentItemDO : list) {
            FinancialPaymentItemRespVO item = new FinancialPaymentItemRespVO();
            item.setId(financialPaymentItemDO.getId());
            item.setNotes(financialPaymentItemDO.getNotes());
            item.setAmount(financialPaymentItemDO.getAmount());
            item.setFinancialPaymentId(financialPaymentItemDO.getFinancialPaymentId());
            item.setCreateTime(financialPaymentItemDO.getCreateTime());
            if (CollectionUtil.isNotEmpty(financialPaymentItemDO.getFileItems())) {
                List<UploadUserFile> uploadUserFiles = BeanUtils.toBean(financialPaymentItemDO.getFileItems(), UploadUserFile.class);
                item.setFileItems(uploadUserFiles);
            }
            items.add(item);
        }
        vo.setItems(items);
        return success(vo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得财务支付管理分页")
    public CommonResult<PageResult<FinancialPaymentRespVO>> getFinancialPaymentPage(@Valid FinancialPaymentPageReqVO pageReqVO) {
        PageResult<FinancialPaymentDO> pageResult = financialPaymentService.getFinancialPaymentPage(pageReqVO);
        pageResult.getList().forEach(item -> {
            if (StringUtils.isNotEmpty(item.getRecipientName())) {
                item.setRecipientName(item.getRecipientName().split(",")[0]);
            }
        });
        return success(BeanUtils.toBean(pageResult, FinancialPaymentRespVO.class));
    }

    @GetMapping("/total")
    @Operation(summary = "获得财务支付统计信息")
    public CommonResult<FinancialPaymentRespVO> getPaymentTotal(@Valid FinancialPaymentPageReqVO pageReqVO) {
        FinancialPaymentDO paymentDO = financialPaymentService.getPaymentTotal(pageReqVO);
        return success(BeanUtils.toBean(paymentDO, FinancialPaymentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出支付信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportFactoryInfoExcel(@Valid FinancialPaymentPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);

        financialPaymentService.exportList(response, pageReqVO);
    }
}
