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

import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.module.api.shop.ShopApi;
import cn.newfeifan.mall.module.api.shop.dto.ShopDTO;
import cn.newfeifan.mall.module.distri.controller.admin.shopsettlement.excelvo.ShopSettlementExcelRespVO;
import cn.newfeifan.mall.module.distri.convert.shopsettlement.ShopSettlementConvert;
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.io.IOException;
import java.util.List;

import cn.newfeifan.mall.framework.common.pojo.PageParam;
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.shopsettlement.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.shopsettlement.ShopSettlementDO;
import cn.newfeifan.mall.module.distri.service.shopsettlement.ShopSettlementService;

@Tag(name = "管理后台 - 订单结算信息表，记录店铺的结算信息")
@RestController
@RequestMapping("/distri/shop-settlement")
@Validated
public class ShopSettlementController {

    @Resource
    private ShopSettlementService shopSettlementService;

    @Resource
    private ShopApi shopApi;

    @PostMapping("/create")
    @Operation(summary = "创建订单结算信息表，记录店铺的结算信息")
    @PreAuthorize("@ss.hasPermission('trade:shop-settlement:create')")
    public CommonResult<Long> createShopSettlement(@Valid @RequestBody ShopSettlementSaveReqVO createReqVO) {
        return success(shopSettlementService.createShopSettlement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单结算信息表，记录店铺的结算信息")
    @PreAuthorize("@ss.hasPermission('trade:shop-settlement:update')")
    public CommonResult<Boolean> updateShopSettlement(@Valid @RequestBody ShopSettlementSaveReqVO updateReqVO) {
        shopSettlementService.updateShopSettlement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单结算信息表，记录店铺的结算信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:shop-settlement:delete')")
    public CommonResult<Boolean> deleteShopSettlement(@RequestParam("id") Long id) {
        shopSettlementService.deleteShopSettlement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单结算信息表，记录店铺的结算信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:shop-settlement:query')")
    public CommonResult<ShopSettlementRespVO> getShopSettlement(@RequestParam("id") Long id) {
        ShopSettlementDO shopSettlement = shopSettlementService.getShopSettlement(id);
        return success(BeanUtils.toBean(shopSettlement, ShopSettlementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单结算信息表，记录店铺的结算信息分页")
    @PreAuthorize("@ss.hasPermission('trade:shop-settlement:query')")
    public CommonResult<PageResult<ShopSettlementRespVO>> getShopSettlementPage(@Valid ShopSettlementPageReqVO pageReqVO) {
        PageResult<ShopSettlementRespVO> pageResult = shopSettlementService.getShopSettlementPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单结算信息表，记录店铺的结算信息 Excel")
    @PreAuthorize("@ss.hasPermission('trade:shop-settlement:export')")
    @OperateLog(type = EXPORT)
    public void exportShopSettlementExcel(@Valid ShopSettlementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopSettlementExcelRespVO> list = ShopSettlementConvert.INSTANCE.convertList(shopSettlementService.getShopSettlementPage(pageReqVO).getList());

        for (ShopSettlementExcelRespVO shopSettlementExcelRespVO : list) {
            ShopDTO shop = shopApi.getShop(shopSettlementExcelRespVO.getShopId());
            shopSettlementExcelRespVO.setAccountName(shop.getAccountName());
            shopSettlementExcelRespVO.setAccountNumber(shop.getAccountNumber());
            shopSettlementExcelRespVO.setBankName(shop.getBankName());
        }

        // 导出 Excel
        ExcelUtils.write(response, "订单结算信息表，记录店铺的结算信息.xls", "数据", ShopSettlementExcelRespVO.class,
                        list);
    }

    @GetMapping("/settlementOrder")
    @Operation(summary = "结算订单")
    @PreAuthorize("@ss.hasPermission('trade:shop-settlement:query')")
    public CommonResult<String> settlementOrder() {
        shopSettlementService.settlementOrder();
        return success("true");
    }

}