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

import cn.hutool.core.lang.tree.TreeNode;
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.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.product.controller.admin.productBase.vo.ProductBasePageReqVO;
import com.yc.ship.module.product.controller.admin.productBase.vo.ProductBaseRespVO;
import com.yc.ship.module.product.controller.admin.productBase.vo.ProductBaseSaveReqVO;
import com.yc.ship.module.product.controller.admin.productBase.vo.ShipRespVO;
import com.yc.ship.module.product.service.productBase.ProductBaseService;
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;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.PRODUCT;


@Tag(name = "管理后台 - 基础产品")
@RestController
@RequestMapping("/product/base")
@Validated
public class ProductBaseController {

    @Resource
    private ProductBaseService baseService;

    @PostMapping("/create")
    @Operation(summary = "创建基础产品")
    @OperateLog(type = PRODUCT)
    @PreAuthorize("@ss.hasPermission('product:base:create')")
    public CommonResult<Long> createBase(@Valid @RequestBody ProductBaseSaveReqVO createReqVO) {
        return success(baseService.createBase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新基础产品")
    @OperateLog(type = PRODUCT)
    @PreAuthorize("@ss.hasPermission('product:base:update')")
    public CommonResult<Boolean> updateBase(@Valid @RequestBody ProductBaseSaveReqVO updateReqVO) {
        baseService.updateBase(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/page")
    @Operation(summary = "获得基础产品分页")
    @PreAuthorize("@ss.hasPermission('product:base:query')")
    public CommonResult<PageResult<ProductBaseRespVO>> getBasePage(@Valid ProductBasePageReqVO pageReqVO) {
        return success(baseService.queryBasePage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出基础产品 Excel")
    @PreAuthorize("@ss.hasPermission('product:base:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportBaseExcel(@Valid ProductBasePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductBaseRespVO> list = baseService.queryBasePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "基础产品.xls", "数据", ProductBaseRespVO.class, list);
    }

    @GetMapping("/list")
    @Operation(summary = "获得基础产品(分页)")
    public CommonResult<PageResult<ProductBaseRespVO>> list(@Valid ProductBasePageReqVO pageReqVO) {
        return success(baseService.selectBasePage(pageReqVO));
    }

    @GetMapping("/base-list")
    @Operation(summary = "获得基础产品(列表)")
    public CommonResult<List<ProductBaseRespVO>> baseList(@Valid ProductBasePageReqVO pageReqVO) {
        return success(baseService.selectBaseList(pageReqVO));
    }

    @GetMapping("/ship-list")
    @Operation(summary = "获得基础产品(列表)")
    public CommonResult<List<ShipRespVO>> shipList() {
        return success(baseService.selectShipList());
    }

    @GetMapping({"/base-tree-list"})
    @Operation(summary = "获取基础产品产品列表树。（带项目）", description = "获取基础产品产品列表树。（带项目）")
    public CommonResult<List<TreeNode<Long>>> getDistributorProductList() {
        return success( baseService.getProductBaseTreeList());
    }
}