package com.yc.ship.module.product.controller.admin.productTakeRule;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.product.controller.admin.productTakeRule.vo.ProductTakeRulePageReqVO;
import com.yc.ship.module.product.controller.admin.productTakeRule.vo.ProductTakeRuleRespVO;
import com.yc.ship.module.product.controller.admin.productTakeRule.vo.ProductTakeRuleSaveReqVO;
import com.yc.ship.module.product.dal.dataobject.productTakeRule.ProductTakeRuleDO;
import com.yc.ship.module.product.service.productTakeRule.ProductTakeRuleService;
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 com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 产品取票规则")
@RestController
@RequestMapping("/take-rule")
@Validated
public class ProductTakeRuleController {

    @Resource
    private ProductTakeRuleService takeRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建产品取票规则")
    @PreAuthorize("@ss.hasPermission('product:take-rule:create')")
    public CommonResult<Long> createTakeRule(@Valid @RequestBody ProductTakeRuleSaveReqVO createReqVO) {
        return success(takeRuleService.createTakeRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品取票规则")
    @PreAuthorize("@ss.hasPermission('product:take-rule:update')")
    public CommonResult<Boolean> updateTakeRule(@Valid @RequestBody ProductTakeRuleSaveReqVO updateReqVO) {
        takeRuleService.updateTakeRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品取票规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:take-rule:delete')")
    public CommonResult<Boolean> deleteTakeRule(@RequestParam("id") Long id) {
        takeRuleService.deleteTakeRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品取票规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:take-rule:query')")
    public CommonResult<ProductTakeRuleRespVO> getTakeRule(@RequestParam("id") Long id) {
        ProductTakeRuleDO takeRule = takeRuleService.getTakeRule(id);
        return success(BeanUtils.toBean(takeRule, ProductTakeRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品取票规则分页")
    @PreAuthorize("@ss.hasPermission('product:take-rule:query')")
    public CommonResult<PageResult<ProductTakeRuleRespVO>> getTakeRulePage(@Valid ProductTakeRulePageReqVO pageReqVO) {
        PageResult<ProductTakeRuleDO> pageResult = takeRuleService.getTakeRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductTakeRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品取票规则 Excel")
    @PreAuthorize("@ss.hasPermission('product:take-rule:export')")
    @OperateLog(type = EXPORT, enable = false)
    public void exportTakeRuleExcel(@Valid ProductTakeRulePageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductTakeRuleDO> list = takeRuleService.getTakeRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品取票规则.xls", "数据", ProductTakeRuleRespVO.class,
                BeanUtils.toBean(list, ProductTakeRuleRespVO.class));
    }

}