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

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.suppliercategory.vo.SupplierCategoryPageReqVO;
import com.yc.ship.module.resource.controller.admin.suppliercategory.vo.SupplierCategoryRespVO;
import com.yc.ship.module.resource.controller.admin.suppliercategory.vo.SupplierCategorySaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.suppliercategory.SupplierCategoryDO;
import com.yc.ship.module.resource.service.suppliercategory.SupplierCategoryService;
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("/product/supplier-category")
@Validated
public class SupplierCategoryController {

    @Resource
    private SupplierCategoryService supplierCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建资源供应商与资源类型关联")
    @PreAuthorize("@ss.hasPermission('product:supplier-category:create')")
    public CommonResult<Long> createSupplierCategory(@Valid @RequestBody SupplierCategorySaveReqVO createReqVO) {
        return success(supplierCategoryService.createSupplierCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源供应商与资源类型关联")
    @PreAuthorize("@ss.hasPermission('product:supplier-category:update')")
    public CommonResult<Boolean> updateSupplierCategory(@Valid @RequestBody SupplierCategorySaveReqVO updateReqVO) {
        supplierCategoryService.updateSupplierCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源供应商与资源类型关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:supplier-category:delete')")
    public CommonResult<Boolean> deleteSupplierCategory(@RequestParam("id") Long id) {
        supplierCategoryService.deleteSupplierCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源供应商与资源类型关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:supplier-category:query')")
    public CommonResult<SupplierCategoryRespVO> getSupplierCategory(@RequestParam("id") Long id) {
        SupplierCategoryDO supplierCategory = supplierCategoryService.getSupplierCategory(id);
        return success(BeanUtils.toBean(supplierCategory, SupplierCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源供应商与资源类型关联分页")
    @PreAuthorize("@ss.hasPermission('product:supplier-category:query')")
    public CommonResult<PageResult<SupplierCategoryRespVO>> getSupplierCategoryPage(@Valid SupplierCategoryPageReqVO pageReqVO) {
        PageResult<SupplierCategoryDO> pageResult = supplierCategoryService.getSupplierCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源供应商与资源类型关联 Excel")
    @PreAuthorize("@ss.hasPermission('product:supplier-category:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportSupplierCategoryExcel(@Valid SupplierCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierCategoryDO> list = supplierCategoryService.getSupplierCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源供应商与资源类型关联.xls", "数据", SupplierCategoryRespVO.class,
                        BeanUtils.toBean(list, SupplierCategoryRespVO.class));
    }

}