package com.yc.ship.module.trade.controller.admin.shipSaleRule;

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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

import com.yc.ship.framework.excel.core.util.ExcelUtils;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
import static com.yc.ship.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yc.ship.module.trade.controller.admin.shipSaleRule.vo.*;
import com.yc.ship.module.trade.dal.dataobject.shipSaleRule.ShipSaleRuleDO;
import com.yc.ship.module.trade.service.shipSaleRule.ShipSaleRuleService;

@Tag(name = "管理后台 - 时间规则配置")
@RestController
@RequestMapping("/trade/ship-sale-rule")
@Validated
public class ShipSaleRuleController {

    @Resource
    private ShipSaleRuleService shipSaleRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建时间规则配置")
    @PreAuthorize("@ss.hasPermission('trade:ship-sale-rule:create')")
    public CommonResult<Long> createShipSaleRule(@Valid @RequestBody ShipSaleRuleSaveReqVO createReqVO) {
        return success(shipSaleRuleService.createShipSaleRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新时间规则配置")
    @PreAuthorize("@ss.hasPermission('trade:ship-sale-rule:update')")
    public CommonResult<Boolean> updateShipSaleRule(@Valid @RequestBody ShipSaleRuleSaveReqVO updateReqVO) {
        shipSaleRuleService.updateShipSaleRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除时间规则配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:ship-sale-rule:delete')")
    public CommonResult<Boolean> deleteShipSaleRule(@RequestParam("id") Long id) {
        shipSaleRuleService.deleteShipSaleRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得时间规则配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:ship-sale-rule:query')")
    public CommonResult<ShipSaleRuleRespVO> getShipSaleRule(@RequestParam("id") Long id) {
        ShipSaleRuleDO shipSaleRule = shipSaleRuleService.getShipSaleRule(id);
        return success(BeanUtils.toBean(shipSaleRule, ShipSaleRuleRespVO.class));
    }

    @GetMapping("/getOne")
    @Operation(summary = "获得时间规则配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ShipSaleRuleRespVO> getShipSaleRuleOne() {
        try {
            ShipSaleRuleDO shipSaleRule = shipSaleRuleService.getShipSaleRuleOne();
            return success(BeanUtils.toBean(shipSaleRule, ShipSaleRuleRespVO.class));
        }catch (Exception e){
            return success(new ShipSaleRuleRespVO());
        }
    }

    @GetMapping("/page")
    @Operation(summary = "获得时间规则配置分页")
    @PreAuthorize("@ss.hasPermission('trade:ship-sale-rule:query')")
    public CommonResult<PageResult<ShipSaleRuleRespVO>> getShipSaleRulePage(@Valid ShipSaleRulePageReqVO pageReqVO) {
        PageResult<ShipSaleRuleDO> pageResult = shipSaleRuleService.getShipSaleRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShipSaleRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出时间规则配置 Excel")
    @PreAuthorize("@ss.hasPermission('trade:ship-sale-rule:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShipSaleRuleExcel(@Valid ShipSaleRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShipSaleRuleDO> list = shipSaleRuleService.getShipSaleRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "时间规则配置.xls", "数据", ShipSaleRuleRespVO.class,
                        BeanUtils.toBean(list, ShipSaleRuleRespVO.class));
    }

}