package co.yixiang.yshop.module.distributor.controller.admin.distributorlevel;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;
import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import co.yixiang.yshop.module.distributor.controller.admin.distributorlevel.vo.*;
import co.yixiang.yshop.module.distributor.convert.distributorlevel.DistributorLevelConvert;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributorlevel.DistributorLevelDO;
import co.yixiang.yshop.module.distributor.service.distributorlevel.DistributorLevelFacade;
import co.yixiang.yshop.module.distributor.service.distributorlevel.DistributorLevelService;
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.Collection;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.common.util.collection.CollectionUtils.convertList;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 分销商等级")
@RestController
@RequestMapping("/distributor/level")
@Validated
public class DistributorLevelController {

    @Resource
    private DistributorLevelService levelService;

    @Resource
    private DistributorLevelFacade distributorLevelFacade;

    // 获取已有分销商等级
    @GetMapping("/get-all")
    @Operation(summary = "获取已有分销商等级")
    @PreAuthorize("@ss.hasPermission('distributor:level:query')")
    public CommonResult<List<Integer>> getAllLevel() {
        List<DistributorLevelDO> levelList = levelService.getLevelList(new DistributorLevelExportReqVO());
        List<Integer> levels = convertList(levelList, DistributorLevelDO::getLevel);
        return success(levels);
    }


    @PostMapping("/create")
    @Operation(summary = "创建分销商等级")
    @PreAuthorize("@ss.hasPermission('distributor:level:create')")
    public CommonResult<Long> createLevel(@Valid @RequestBody DistributorLevelCreateReqVO createReqVO) {
        Long level = distributorLevelFacade.createLevel(createReqVO);
        return success(level);
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商等级")
    @PreAuthorize("@ss.hasPermission('distributor:level:update')")
    public CommonResult<Boolean> updateLevel(@Valid @RequestBody DistributorLevelUpdateReqVO updateReqVO) {
        distributorLevelFacade.updateLevel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销商等级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distributor:level:delete')")
    public CommonResult<Boolean> deleteLevel(@RequestParam("id") Long id) {
        levelService.deleteLevel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销商等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distributor:level:query')")
    public CommonResult<DistributorLevelRespVO> getLevel(@RequestParam("id") Long id) {
        DistributorLevelDO level = levelService.getLevel(id);
        return success(DistributorLevelConvert.INSTANCE.convert(level));
    }

    @GetMapping("/list")
    @Operation(summary = "获得分销商等级列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('distributor:level:query')")
    public CommonResult<List<DistributorLevelRespVO>> getLevelList(@RequestParam("ids") Collection<Long> ids) {
        List<DistributorLevelDO> list = levelService.getLevelList(ids);
        return success(DistributorLevelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商等级分页")
    @PreAuthorize("@ss.hasPermission('distributor:level:query')")
    public CommonResult<PageResult<DistributorLevelRespVO>> getLevelPage(@Valid DistributorLevelPageReqVO pageVO) {
        return success(distributorLevelFacade.getLevelPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商等级 Excel")
    @PreAuthorize("@ss.hasPermission('distributor:level:export')")
    @OperateLog(type = EXPORT)
    public void exportLevelExcel(@Valid DistributorLevelExportReqVO exportReqVO,
                                 HttpServletResponse response) throws IOException {
        List<DistributorLevelDO> list = levelService.getLevelList(exportReqVO);
        // 导出 Excel
        List<DistributorLevelExcelVO> datas = DistributorLevelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "分销商等级.xls", "数据", DistributorLevelExcelVO.class, datas);
    }

    @PostMapping("/level-up")
    @Operation(summary = "分销商升级")
    public CommonResult<Boolean> levelUp(@RequestParam("id") Long id) {
        distributorLevelFacade.levelUp(id);
        return success(Boolean.TRUE);
    }

}
