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.ExcelData;
import com.gree.mall.manager.bean.activity.PromotionActivityBatchBean;
import com.gree.mall.manager.bean.activity.PromotionActivityBean;
import com.gree.mall.manager.bean.activity.PromotionActivityVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.activity.PromotionActivityLogic;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Slf4j
@RestController
@Api(value = "扫码活动单API", tags ={"扫码活动单API"} )
@RequestMapping(value = "/promotion/activity", produces = "application/json; charset=utf-8")
public class PromotionActivityController {

    @Resource
    PromotionActivityLogic promotionActivityLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "活动单-列表")
    public ResponseHelper<IPage<PromotionActivityVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<PromotionActivityVO> page = promotionActivityLogic.list(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<PromotionActivityVO>() {});
    }

    @GetMapping("/export")
    @ApiOperation(value = "活动单-导出")
    public void export(
            @ApiParam(value = "开始时间") @RequestParam Date statDate,
            @ApiParam(value = "结束时间") @RequestParam Date endDate,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {

        ExcelData excelData = promotionActivityLogic.export(statDate, endDate);

        ExcelUtils.exportExcel(request,response,"活动单列表.xlsx",excelData);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "活动单-详情")
    public ResponseHelper<PromotionActivityBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String promotionActivityId
    ) throws RemoteServiceException {
        PromotionActivityBean bean = promotionActivityLogic.detail(promotionActivityId);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/add")
    @ApiOperation(value = "活动单-新增")
    public ResponseHelper add(
            HttpServletRequest request,
            @RequestBody PromotionActivityBean bean
    ) throws Exception {
        promotionActivityLogic.add(request, bean);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/follow")
    @ApiOperation(value = "活动单-批量跟进")
    public ResponseHelper batchFollow(
            @RequestBody PromotionActivityBatchBean bean
    ) throws Exception {
        promotionActivityLogic.batchFollow(bean);
        return ResponseHelper.success();
    }
}
