package co.yixiang.yshop.module.product.controller.admin.campaigninfo;

import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignProductReqVO;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignProductRespVO;
import co.yixiang.yshop.module.product.service.campaigninfo.AppCampaignInfoService;
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.product.controller.admin.campaigninfo.vo.*;
import co.yixiang.yshop.module.product.dal.dataobject.campaigninfo.CampaignInfoDO;
import co.yixiang.yshop.module.product.convert.campaigninfo.CampaignInfoConvert;
import co.yixiang.yshop.module.product.service.campaigninfo.CampaignInfoService;

@Tag(name = "管理后台 - 活动信息")
@RestController
@RequestMapping("/product/campaign-info")
@Validated
public class CampaignInfoController {

    @Resource
    private CampaignInfoService campaignInfoService;

    @Resource
    private AppCampaignInfoService appCampaignInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建活动信息")
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:create')")
    public CommonResult<Long> createCampaignInfo(@Valid @RequestBody CampaignInfoCreateReqVO createReqVO) {
        return success(campaignInfoService.createCampaignInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动信息")
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:update')")
    public CommonResult<Boolean> updateCampaignInfo(@Valid @RequestBody CampaignInfoUpdateReqVO updateReqVO) {
        campaignInfoService.updateCampaignInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:delete')")
    public CommonResult<Boolean> deleteCampaignInfo(@RequestParam("id") Long id) {
        campaignInfoService.deleteCampaignInfo(id);
        return success(true);
    }

    @GetMapping("/close")
    @Operation(summary = "结束活动信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:close')")
    public CommonResult<Boolean> closeCampaignInfo(@RequestParam("id") Long id) {
        campaignInfoService.closeCampaignInfo(id);
        return success(true);
    }

    @GetMapping("/start")
    @Operation(summary = "开始活动信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:start')")
    public CommonResult<Boolean> startCampaignInfo(@RequestParam("id") Long id) {
        campaignInfoService.startCampaignInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:query')")
    public CommonResult<CampaignInfoRespVO> getCampaignInfo(@RequestParam("id") Long id) {
        return success(campaignInfoService.getCampaignInfo(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动信息分页")
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:query')")
    public CommonResult<PageResult<CampaignInfoPageRespVO>> getCampaignInfoPage(@Valid CampaignInfoPageReqVO pageVO) {
        return success(campaignInfoService.getCampaignInfoPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动信息 Excel")
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:export')")
    @OperateLog(type = EXPORT)
    public void exportCampaignInfoExcel(@Valid CampaignInfoExportReqVO exportReqVO,
                                        HttpServletResponse response) throws IOException {
        List<CampaignInfoDO> list = campaignInfoService.getCampaignInfoList(exportReqVO);
        // 导出 Excel
        List<CampaignInfoExcelVO> datas = CampaignInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "活动信息.xls", "数据", CampaignInfoExcelVO.class, datas);
    }

    @GetMapping("/data")
    @Operation(summary = "获得活动数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:data')")
    public CommonResult<CampaignDataRespVO> getCampaignData(@RequestParam("id") Long id) {
        return success(campaignInfoService.getCampaignData(id));
    }

    @GetMapping("/data-detail")
    @Operation(summary = "获得活动信息分页")
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:data-detail')")
    public CommonResult<PageResult<CampaignDataDetailRespVO>> getCampaignDataDetail(@Valid CampaignDataDetailPageReqVO pageVO) {
        return success(campaignInfoService.getCampaignDataDetail(pageVO));
    }

    @GetMapping("/canvas/page")
    @Operation(summary = "获得画布活动分页")
    @PreAuthorize("@ss.hasPermission('yshop:campaign-info:canvas:page')")
    public CommonResult<PageResult<CanvasCampaignPageRespVO>> getCanvasCampaignPage(@Valid CanvasCampaignPageReqVO pageVO) {
        return success(campaignInfoService.getCanvasCampaignPage(pageVO));
    }

    @PostMapping("/canvas/product-page")
    @Operation(summary = "获得营销活动商品列表")
    public CommonResult<PageResult<CampaignProductRespVO>> getCampaignProductPage(@RequestBody CampaignProductReqVO reqVO) {
        return success(appCampaignInfoService.getCampaignProductPage(reqVO));
    }


}
