package cn.iocoder.yudao.module.identity.controller.admin.diversionstats;

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.annotations.*;

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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.identity.controller.admin.diversionstats.vo.*;
import cn.iocoder.yudao.module.identity.dal.dataobject.diversionstats.DiversionStatsDO;
import cn.iocoder.yudao.module.identity.convert.diversionstats.DiversionStatsConvert;
import cn.iocoder.yudao.module.identity.service.diversionstats.DiversionStatsService;

@Api(tags = "管理后台 - 窜货统计")
@RestController
@RequestMapping("/identity/diversion-stats")
@Validated
public class DiversionStatsController {

    @Resource
    private DiversionStatsService diversionStatsService;

    @PostMapping("/create")
    @ApiOperation("创建窜货统计")
    @PreAuthorize("@ss.hasPermission('identity:diversion-stats:create')")
    public CommonResult<Long> createDiversionStats(@Valid @RequestBody DiversionStatsCreateReqVO createReqVO) {
        return success(diversionStatsService.createDiversionStats(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新窜货统计")
    @PreAuthorize("@ss.hasPermission('identity:diversion-stats:update')")
    public CommonResult<Boolean> updateDiversionStats(@Valid @RequestBody DiversionStatsUpdateReqVO updateReqVO) {
        diversionStatsService.updateDiversionStats(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除窜货统计")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:diversion-stats:delete')")
    public CommonResult<Boolean> deleteDiversionStats(@RequestParam("id") Long id) {
        diversionStatsService.deleteDiversionStats(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得窜货统计")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:diversion-stats:query')")
    public CommonResult<DiversionStatsRespVO> getDiversionStats(@RequestParam("id") Long id) {
        DiversionStatsDO diversionStats = diversionStatsService.getDiversionStats(id);
        return success(DiversionStatsConvert.INSTANCE.convert(diversionStats));
    }

    @GetMapping("/list")
    @ApiOperation("获得窜货统计列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('identity:diversion-stats:query')")
    public CommonResult<List<DiversionStatsRespVO>> getDiversionStatsList(@RequestParam("ids") Collection<Long> ids) {
        List<DiversionStatsDO> list = diversionStatsService.getDiversionStatsList(ids);
        return success(DiversionStatsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得窜货统计分页")
    @PreAuthorize("@ss.hasPermission('identity:diversion-stats:query')")
    public CommonResult<PageResult<DiversionStatsRespVO>> getDiversionStatsPage(@Valid DiversionStatsPageReqVO pageVO) {
        PageResult<DiversionStatsDO> pageResult = diversionStatsService.getDiversionStatsPage(pageVO);
        return success(DiversionStatsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出窜货统计 Excel")
    @PreAuthorize("@ss.hasPermission('identity:diversion-stats:export')")
    @OperateLog(type = EXPORT)
    public void exportDiversionStatsExcel(@Valid DiversionStatsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DiversionStatsDO> list = diversionStatsService.getDiversionStatsList(exportReqVO);
        // 导出 Excel
        List<DiversionStatsExcelVO> datas = DiversionStatsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "窜货统计.xls", "数据", DiversionStatsExcelVO.class, datas);
    }

}
