package com.yc.ship.module.resource.controller.admin.baseprice;

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.resource.controller.admin.baseprice.vo.ProductBasePricePageReqVO;
import com.yc.ship.module.resource.controller.admin.baseprice.vo.ProductBasePriceRespVO;
import com.yc.ship.module.resource.controller.admin.baseprice.vo.ProductBasePriceSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.baseprice.ProductBasePriceDO;
import com.yc.ship.module.resource.service.baseprice.ProductBasePriceService;
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("/resource/baseprice")
@Validated
public class ProductBasePriceController {

    @Resource
    private ProductBasePriceService basePriceService;

    @PostMapping("/create")
    @Operation(summary = "创建基础产品价格")
    @PreAuthorize("@ss.hasPermission('product:base-price:create')")
    public CommonResult<Long> createBasePrice(@Valid @RequestBody ProductBasePriceSaveReqVO createReqVO) {
        return success(basePriceService.createBasePrice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新基础产品价格")
    @PreAuthorize("@ss.hasPermission('product:base-price:update')")
    public CommonResult<Boolean> updateBasePrice(@Valid @RequestBody ProductBasePriceSaveReqVO updateReqVO) {
        basePriceService.updateBasePrice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除基础产品价格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:base-price:delete')")
    public CommonResult<Boolean> deleteBasePrice(@RequestParam("id") Long id) {
        basePriceService.deleteBasePrice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得基础产品价格")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:base-price:query')")
    public CommonResult<ProductBasePriceRespVO> getBasePrice(@RequestParam("id") Long id) {
        ProductBasePriceDO basePrice = basePriceService.getBasePrice(id);
        return success(BeanUtils.toBean(basePrice, ProductBasePriceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得基础产品价格分页")
    @PreAuthorize("@ss.hasPermission('product:base-price:query')")
    public CommonResult<PageResult<ProductBasePriceRespVO>> getBasePricePage(@Valid ProductBasePricePageReqVO pageReqVO) {
        PageResult<ProductBasePriceDO> pageResult = basePriceService.getBasePricePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductBasePriceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出基础产品价格 Excel")
    @PreAuthorize("@ss.hasPermission('product:base-price:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportBasePriceExcel(@Valid ProductBasePricePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductBasePriceDO> list = basePriceService.getBasePricePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "基础产品价格.xls", "数据", ProductBasePriceRespVO.class,
                        BeanUtils.toBean(list, ProductBasePriceRespVO.class));
    }

}