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

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.productSaleChannel.vo.ProductSaleChannelPageReqVO;
import com.yc.ship.module.product.controller.admin.productSaleChannel.vo.ProductSaleChannelRespVO;
import com.yc.ship.module.product.controller.admin.productSaleChannel.vo.ProductSaleChannelSaveReqVO;
import com.yc.ship.module.product.dal.dataobject.productSaleChannel.ProductSaleChannelDO;
import com.yc.ship.module.product.service.productSaleChannel.ProductSaleChannelService;
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("/sale-channel")
@Validated
public class ProductSaleChannelController {

    @Resource
    private ProductSaleChannelService saleChannelService;

    @PostMapping("/create")
    @Operation(summary = "创建产品销售渠道")
    @PreAuthorize("@ss.hasPermission('product:sale-channel:create')")
    public CommonResult<Long> createSaleChannel(@Valid @RequestBody ProductSaleChannelSaveReqVO createReqVO) {
        return success(saleChannelService.createSaleChannel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品销售渠道")
    @PreAuthorize("@ss.hasPermission('product:sale-channel:update')")
    public CommonResult<Boolean> updateSaleChannel(@Valid @RequestBody ProductSaleChannelSaveReqVO updateReqVO) {
        saleChannelService.updateSaleChannel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品销售渠道")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:sale-channel:delete')")
    public CommonResult<Boolean> deleteSaleChannel(@RequestParam("id") Long id) {
        saleChannelService.deleteSaleChannel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品销售渠道")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:sale-channel:query')")
    public CommonResult<ProductSaleChannelRespVO> getSaleChannel(@RequestParam("id") Long id) {
        ProductSaleChannelDO saleChannel = saleChannelService.getSaleChannel(id);
        return success(BeanUtils.toBean(saleChannel, ProductSaleChannelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品销售渠道分页")
    @PreAuthorize("@ss.hasPermission('product:sale-channel:query')")
    public CommonResult<PageResult<ProductSaleChannelRespVO>> getSaleChannelPage(@Valid ProductSaleChannelPageReqVO pageReqVO) {
        PageResult<ProductSaleChannelDO> pageResult = saleChannelService.getSaleChannelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductSaleChannelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品销售渠道 Excel")
    @PreAuthorize("@ss.hasPermission('product:sale-channel:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportSaleChannelExcel(@Valid ProductSaleChannelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductSaleChannelDO> list = saleChannelService.getSaleChannelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品销售渠道.xls", "数据", ProductSaleChannelRespVO.class,
                        BeanUtils.toBean(list, ProductSaleChannelRespVO.class));
    }

}