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

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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.distributor.controller.admin.distributorwages.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributorwages.DistributorWagesDO;
import co.yixiang.yshop.module.distributor.convert.distributorwages.DistributorWagesConvert;
import co.yixiang.yshop.module.distributor.service.distributorwages.DistributorWagesService;

@Tag(name = "管理后台 - 分销商佣金")
@RestController
@RequestMapping("/distributor/wages")
@Validated
public class DistributorWagesController {

    @Resource
    private DistributorWagesService wagesService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商佣金")
    @PreAuthorize("@ss.hasPermission('distributor:wages:create')")
    public CommonResult<Long> createWages(@Valid @RequestBody DistributorWagesCreateReqVO createReqVO) {
        return success(wagesService.createWages(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商佣金")
    @PreAuthorize("@ss.hasPermission('distributor:wages:update')")
    public CommonResult<Boolean> updateWages(@Valid @RequestBody DistributorWagesUpdateReqVO updateReqVO) {
        wagesService.updateWages(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得分销商佣金")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distributor:wages:query')")
    public CommonResult<DistributorWagesRespVO> getWages(@RequestParam("id") Long id) {
        DistributorWagesDO wages = wagesService.getWages(id);
        return success(DistributorWagesConvert.INSTANCE.convert(wages));
    }

    @GetMapping("/list")
    @Operation(summary = "获得分销商佣金列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('distributor:wages:query')")
    public CommonResult<List<DistributorWagesRespVO>> getWagesList(@RequestParam("ids") Collection<Long> ids) {
        List<DistributorWagesDO> list = wagesService.getWagesList(ids);
        return success(DistributorWagesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商佣金分页")
    @PreAuthorize("@ss.hasPermission('distributor:wages:query')")
    public CommonResult<PageResult<DistributorWagesRespVO>> getWagesPage(@Valid DistributorWagesPageReqVO pageVO) {
        PageResult<DistributorWagesDO> pageResult = wagesService.getWagesPage(pageVO);
        return success(DistributorWagesConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商佣金 Excel")
    @PreAuthorize("@ss.hasPermission('distributor:wages:export')")
    @OperateLog(type = EXPORT)
    public void exportWagesExcel(@Valid DistributorWagesExportReqVO exportReqVO,
                                 HttpServletResponse response) throws IOException {
        List<DistributorWagesDO> list = wagesService.getWagesList(exportReqVO);
        // 导出 Excel
        List<DistributorWagesExcelVO> datas = DistributorWagesConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "分销商佣金.xls", "数据", DistributorWagesExcelVO.class, datas);
    }

}
