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

import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;
import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import cn.newfeifan.mall.module.distri.controller.admin.shopsettlement.excelvo.ShopSettlementExcelRespVO;
import cn.newfeifan.mall.module.distri.controller.admin.shopsettlement.vo.ShopSettlementPageReqVO;
import cn.newfeifan.mall.module.distri.controller.admin.shopsettlement.vo.ShopSettlementRespVO;
import cn.newfeifan.mall.module.distri.controller.admin.shopsettlement.vo.ShopSettlementSaveReqVO;
import cn.newfeifan.mall.module.distri.convert.shopsettlement.ShopSettlementConvert;
import cn.newfeifan.mall.module.distri.dal.dataobject.shopsettlement.ShopSettlementDO;
import cn.newfeifan.mall.module.distri.service.shopsettlement.ShopSettlementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
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.List;

import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

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

    @Resource
    private ShopSettlementService shopSettlementService;

    @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());
        // 导出 Excel
        ExcelUtils.write(response, "订单结算信息表，记录店铺的结算信息.xls", "数据", ShopSettlementExcelRespVO.class,
                        list);
    }

}