package com.gree.mall.manager.controller.activity;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.activity.PromotionQuestionnaireAddBean;
import com.gree.mall.manager.bean.goods.PromotionGroupListVO;
import com.gree.mall.manager.bean.listvo.PromotionQuestionnairVO;
import com.gree.mall.manager.bean.listvo.param.PromotionZfireParamBean;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.activity.PromotionQuestionnaireLogic;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(value = "问卷活动API", tags ={"问卷活动API"} )
@RequestMapping(value = "/promotion/questionnaire", produces = "application/json; charset=utf-8")
public class PromotionQuestionnaireController {

    private final PromotionQuestionnaireLogic promotionQuestionnaireLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<IPage<PromotionQuestionnairVO>> list(
            @RequestBody PromotionZfireParamBean zfireParamBean
    ) {
        IPage<PromotionQuestionnairVO> list = promotionQuestionnaireLogic.list(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<PromotionQuestionnairVO>() {});
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public void listExport(@RequestBody PromotionZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //2.查询要导出的内容
        IPage<PromotionQuestionnairVO> page = promotionQuestionnaireLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public ResponseHelper add(@RequestBody PromotionQuestionnaireAddBean bean) throws Exception {
        promotionQuestionnaireLogic.add(bean);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "下载二维码")
    @GetMapping("/download")
    public void down(
           @ApiParam(value = "活动id",required = true) @RequestParam String id,
           @ApiParam(value = "活动二维码id",required = false) @RequestParam(required = false) String id2,
            HttpServletResponse response
    ) throws Exception {
        promotionQuestionnaireLogic.batchDownload(id,id2,response);
    }



    @ApiOperation(value = "显示/隐藏")
    @PostMapping("/show")
    public ResponseHelper show(@RequestParam String id,@RequestParam Boolean isShow) throws Exception {
        promotionQuestionnaireLogic.updateIsShow(id,isShow);
        return ResponseHelper.success();
    }


    @ApiOperation(value = "详情")
    @PostMapping("/detail")
    public ResponseHelper<PromotionQuestionnaireAddBean > detail(@RequestParam String id) throws Exception {
        PromotionQuestionnaireAddBean detail = promotionQuestionnaireLogic.detail(id);
        return ResponseHelper.success(detail);
    }


    @ApiOperation(value = "终止")
    @PostMapping("/stop")
    public ResponseHelper stop(@RequestParam String id) throws Exception {
        promotionQuestionnaireLogic.stop(id);
        return ResponseHelper.success();
    }


}
