package co.yixiang.yshop.module.member.controller.admin.burypoint;

import co.yixiang.yshop.module.member.dal.dataobject.burypoint.TrendChartRespVO;
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 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.member.controller.admin.burypoint.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.burypoint.BuryPointDO;
import co.yixiang.yshop.module.member.convert.burypoint.BuryPointConvert;
import co.yixiang.yshop.module.member.service.burypoint.BuryPointService;

@Tag(name = "管理后台 - 埋点")
@RestController
@RequestMapping("/member/bury-point")
@Validated
public class BuryPointController {

    @Resource
    private BuryPointService buryPointService;

    @PostMapping("/create")
    @Operation(summary = "创建埋点")
    @PreAuthorize("@ss.hasPermission('member:bury-point:create')")
    public CommonResult<Long> createBuryPoint(@Valid @RequestBody BuryPointCreateReqVO createReqVO) {
        return success(buryPointService.createBuryPoint(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新埋点")
    @PreAuthorize("@ss.hasPermission('member:bury-point:update')")
    public CommonResult<Boolean> updateBuryPoint(@Valid @RequestBody BuryPointUpdateReqVO updateReqVO) {
        buryPointService.updateBuryPoint(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除埋点")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:bury-point:delete')")
    public CommonResult<Boolean> deleteBuryPoint(@RequestParam("id") Long id) {
        buryPointService.deleteBuryPoint(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得埋点")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:bury-point:query')")
    public CommonResult<BuryPointRespVO> getBuryPoint(@RequestParam("id") Long id) {
        BuryPointDO buryPoint = buryPointService.getBuryPoint(id);
        return success(BuryPointConvert.INSTANCE.convert(buryPoint));
    }

    @GetMapping("/list")
    @Operation(summary = "获得埋点列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:bury-point:query')")
    public CommonResult<List<BuryPointRespVO>> getBuryPointList(@RequestParam("ids") Collection<Long> ids) {
        List<BuryPointDO> list = buryPointService.getBuryPointList(ids);
        return success(BuryPointConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得埋点分页")
    @PreAuthorize("@ss.hasPermission('member:bury-point:query')")
    public CommonResult<PageResult<BuryPointRespVO>> getBuryPointPage(@Valid BuryPointPageReqVO pageVO) {
        PageResult<BuryPointDO> pageResult = buryPointService.getBuryPointPage(pageVO);
        return success(BuryPointConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出埋点 Excel")
    @PreAuthorize("@ss.hasPermission('member:bury-point:export')")
    @OperateLog(type = EXPORT)
    public void exportBuryPointExcel(@Valid BuryPointExportReqVO exportReqVO,
                                     HttpServletResponse response) throws IOException {
        List<BuryPointDO> list = buryPointService.getBuryPointList(exportReqVO);
        // 导出 Excel
        List<BuryPointExcelVO> datas = BuryPointConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "埋点.xls", "数据", BuryPointExcelVO.class, datas);
    }

    @GetMapping("/get-trend-chart")
    @Operation(summary = "获取趋势图")
    public CommonResult<List<TrendChartRespVO>> getTrendChart() {
        return success(buryPointService.getTrendChart());
    }

}
