package com.yc.ship.module.ota.controller.admin.distributorcategory;

import com.yc.ship.module.ota.controller.admin.distributorcategory.vo.DistributorCategoryPageReqVO;
import com.yc.ship.module.ota.controller.admin.distributorcategory.vo.DistributorCategoryRespVO;
import com.yc.ship.module.ota.controller.admin.distributorcategory.vo.DistributorCategorySaveReqVO;
import com.yc.ship.module.ota.dal.dataobject.distributorcategory.DistributorCategoryDO;
import com.yc.ship.module.ota.service.distributorcategory.DistributorCategoryService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

import com.yc.ship.framework.excel.core.util.ExcelUtils;

import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 供应商分类")
@RestController
@RequestMapping("/distributor-category")
@Validated
public class DistributorCategoryController {

    @Resource
    private DistributorCategoryService distributorCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商分类")
    @PreAuthorize("@ss.hasPermission('ota:distributor-category:create')")
    public CommonResult<Long> createDistributorCategory(@Valid @RequestBody DistributorCategorySaveReqVO createReqVO) {
        return success(distributorCategoryService.createDistributorCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商分类")
    @PreAuthorize("@ss.hasPermission('ota:distributor-category:update')")
    public CommonResult<Boolean> updateDistributorCategory(@Valid @RequestBody DistributorCategorySaveReqVO updateReqVO) {
        distributorCategoryService.updateDistributorCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ota:distributor-category:delete')")
    public CommonResult<Boolean> deleteDistributorCategory(@RequestParam("id") Long id) {
        distributorCategoryService.deleteDistributorCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-category:query')")
    public CommonResult<DistributorCategoryRespVO> getDistributorCategory(@RequestParam("id") Long id) {
        DistributorCategoryDO distributorCategory = distributorCategoryService.getDistributorCategory(id);
        return success(BeanUtils.toBean(distributorCategory, DistributorCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商分类分页")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-category:query')")
    public CommonResult<PageResult<DistributorCategoryRespVO>> getDistributorCategoryPage(@Valid DistributorCategoryPageReqVO pageReqVO) {
        PageResult<DistributorCategoryDO> pageResult = distributorCategoryService.getDistributorCategoryPage(pageReqVO);
        pageResult.getList().sort(Comparator.comparing(DistributorCategoryDO::getSortNum));
        return success(BeanUtils.toBean(pageResult, DistributorCategoryRespVO.class));
    }

    @PutMapping("/update-status")
    @Operation(summary = "更改状态")
    @PreAuthorize("@ss.hasPermission('ota:distributor-category:update')")
    public CommonResult<Boolean> updateStatus(@Valid @RequestBody DistributorCategorySaveReqVO updateReqVO) {
        distributorCategoryService.updateStatus(updateReqVO.getId(),updateReqVO.getUseStatus());
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商分类 Excel")
    @PreAuthorize("@ss.hasPermission('ota:distributor-category:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportDistributorCategoryExcel(@Valid DistributorCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DistributorCategoryDO> list = distributorCategoryService.getDistributorCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商分类.xls", "数据", DistributorCategoryRespVO.class,
                        BeanUtils.toBean(list, DistributorCategoryRespVO.class));
    }

    @GetMapping({"/list-otc-category"})
    @Operation(summary = "获取分销商类型列表", description = "只包含被开启的类型，主要用于前端的下拉选项")
    public CommonResult<List<DistributorCategoryDO>> getCategoryList() {
        List<DistributorCategoryDO> list= distributorCategoryService.getCategoryList();
        return success(list);
    }
}