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

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.distributordynamics.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributordynamics.DistributorDynamicsDO;
import co.yixiang.yshop.module.distributor.convert.distributordynamics.DistributorDynamicsConvert;
import co.yixiang.yshop.module.distributor.service.distributordynamics.DistributorDynamicsService;

@Tag(name = "管理后台 - 分销商动态")
@RestController
@RequestMapping("/distributor/dynamics")
@Validated
public class DistributorDynamicsController {

    @Resource
    private DistributorDynamicsService dynamicsService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商动态")
    @PreAuthorize("@ss.hasPermission('distributor:dynamics:create')")
    public CommonResult<Long> createDynamics(@Valid @RequestBody DistributorDynamicsCreateReqVO createReqVO) {
        return success(dynamicsService.createDynamics(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商动态")
    @PreAuthorize("@ss.hasPermission('distributor:dynamics:update')")
    public CommonResult<Boolean> updateDynamics(@Valid @RequestBody DistributorDynamicsUpdateReqVO updateReqVO) {
        dynamicsService.updateDynamics(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得分销商动态")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distributor:dynamics:query')")
    public CommonResult<DistributorDynamicsRespVO> getDynamics(@RequestParam("id") Long id) {
        DistributorDynamicsDO dynamics = dynamicsService.getDynamics(id);
        return success(DistributorDynamicsConvert.INSTANCE.convert(dynamics));
    }

    @GetMapping("/list")
    @Operation(summary = "获得分销商动态列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('distributor:dynamics:query')")
    public CommonResult<List<DistributorDynamicsRespVO>> getDynamicsList(@RequestParam("ids") Collection<Long> ids) {
        List<DistributorDynamicsDO> list = dynamicsService.getDynamicsList(ids);
        return success(DistributorDynamicsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商动态分页")
    @PreAuthorize("@ss.hasPermission('distributor:dynamics:query')")
    public CommonResult<PageResult<DistributorDynamicsRespVO>> getDynamicsPage(@Valid DistributorDynamicsPageReqVO pageVO) {
        PageResult<DistributorDynamicsDO> pageResult = dynamicsService.getDynamicsPage(pageVO);
        return success(DistributorDynamicsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商动态 Excel")
    @PreAuthorize("@ss.hasPermission('distributor:dynamics:export')")
    @OperateLog(type = EXPORT)
    public void exportDynamicsExcel(@Valid DistributorDynamicsExportReqVO exportReqVO,
                                    HttpServletResponse response) throws IOException {
        List<DistributorDynamicsDO> list = dynamicsService.getDynamicsList(exportReqVO);
        // 导出 Excel
        List<DistributorDynamicsExcelVO> datas = DistributorDynamicsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "分销商动态.xls", "数据", DistributorDynamicsExcelVO.class, datas);
    }

}
