package cn.newfeifan.mall.module.distri.controller.admin.ptsettlement;

import cn.newfeifan.mall.module.distri.controller.admin.shopsettlement.vo.ShopSettlementPageReqVO;
import cn.newfeifan.mall.module.distri.dal.dataobject.shopsettlement.data.ShopSettlementErrorData;
import cn.newfeifan.mall.module.distri.service.shopsettlement.ShopSettlementService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.distri.controller.admin.ptsettlement.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptsettlement.PtSettlementDO;
import cn.newfeifan.mall.module.distri.service.ptsettlement.PtSettlementService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 平台每日结算信息表，记录店铺的结算信息")
@RestController
@RequestMapping("/distri/pt-settlement")
@Validated
public class PtSettlementController {

    @Resource
    private PtSettlementService ptSettlementService;

    @Resource
    private ShopSettlementService shopSettlementService;

    @PostMapping("/create")
    @Operation(summary = "创建平台每日结算信息表，记录店铺的结算信息")
    @PreAuthorize("@ss.hasPermission('distri:pt-settlement:create')")
    public CommonResult<Long> createPtSettlement(@Valid @RequestBody PtSettlementSaveReqVO createReqVO) {
        return success(ptSettlementService.createPtSettlement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新平台每日结算信息表，记录店铺的结算信息")
    @PreAuthorize("@ss.hasPermission('distri:pt-settlement:update')")
    public CommonResult<Boolean> updatePtSettlement(@Valid @RequestBody PtSettlementSaveReqVO updateReqVO) {
        ptSettlementService.updatePtSettlement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除平台每日结算信息表，记录店铺的结算信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:pt-settlement:delete')")
    public CommonResult<Boolean> deletePtSettlement(@RequestParam("id") Long id) {
        ptSettlementService.deletePtSettlement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得平台每日结算信息表，记录店铺的结算信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:pt-settlement:query')")
    public CommonResult<PtSettlementRespVO> getPtSettlement(@RequestParam("id") Long id) {
        PtSettlementDO ptSettlement = ptSettlementService.getPtSettlement(id);
        return success(BeanUtils.toBean(ptSettlement, PtSettlementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得平台每日结算信息表，记录店铺的结算信息分页")
    @PreAuthorize("@ss.hasPermission('distri:pt-settlement:query')")
    public CommonResult<PageResult<PtSettlementRespVO>> getPtSettlementPage(@Valid PtSettlementPageReqVO pageReqVO) {
        PageResult<PtSettlementDO> pageResult = ptSettlementService.getPtSettlementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PtSettlementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台每日结算信息表，记录店铺的结算信息 Excel")
    @PreAuthorize("@ss.hasPermission('distri:pt-settlement:export')")
    @OperateLog(type = EXPORT)
    public void exportPtSettlementExcel(@Valid PtSettlementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PtSettlementDO> list = ptSettlementService.getPtSettlementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "平台每日结算信息表，记录店铺的结算信息.xls", "数据", PtSettlementRespVO.class,
                        BeanUtils.toBean(list, PtSettlementRespVO.class));
    }

    @GetMapping("/export-transfer-template")
    @Operation(summary = "导出批量转账模板， Excel")
    @PreAuthorize("@ss.hasPermission('distri:pt-settlement:export')")
    @OperateLog(type = EXPORT)
    public void exportPtSettlementExcelTransferTemplate(@Valid ShopSettlementPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);

        List<TransferTemplateRespVO> list = shopSettlementService.excelShopSettlementPage(pageReqVO);

        if(!list.isEmpty()){
            PtSettlementDO ptSettlement = ptSettlementService.getPtSettlement(pageReqVO.getPtSettlementId());

            ptSettlement.setExportStatus(true);
            ptSettlementService.updatePtSettlement(BeanUtils.toBean(ptSettlement, PtSettlementSaveReqVO.class));
        }

        // 导出 Excel
        ExcelUtils.write(response, "批量转账，批量转账.xls", "数据", TransferTemplateRespVO.class,list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入pdf转账回执单")
    @PreAuthorize("@ss.hasPermission('distri:pt-settlement:import')")
    public CommonResult<List<ShopSettlementErrorData>> importPdf(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) throws IOException {

        return success(ptSettlementService.importPdf(file,id));
    }

}