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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.bean.activity.PromotionGroupBean;
import com.gree.mall.manager.bean.activity.PromotionGroupDetail;
import com.gree.mall.manager.bean.activity.PromotionGroupList;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.activity.PromotionGroupLogic;
import com.gree.mall.manager.plus.entity.GoodsVisit;
import com.gree.mall.manager.plus.entity.OrderInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@Api(value = "团购活动管理", tags ={"团购活动管理"} )
@RequestMapping(value = "/promotion/group", produces = "application/json; charset=utf-8")
public class PromotionGroupController {

    @Autowired
    PromotionGroupLogic promotionGroupLogic;

    @GetMapping("/list")
    @ApiOperation(value = "团购活动列表")
    public ResponseHelper page(
            @ApiParam(value = "状态 true:开启 false:关闭") @RequestParam(required = false) Boolean status,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws Exception {
        IPage<PromotionGroupList> list = promotionGroupLogic.list(status, pageNum, pageSize,request);
        return ResponseHelper.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "团购活动详情")
    public ResponseHelper<PromotionGroupBean> detail(
            @ApiParam(value = "promotionGroupId") @RequestParam(required = true)String promotionGroupId
    ) throws Exception {
        PromotionGroupBean detail = promotionGroupLogic.detail(promotionGroupId);
        return ResponseHelper.success(detail);
    }

    @GetMapping("/count")
    @ApiOperation(value = "团购分类统计")
    public ResponseHelper count(
            HttpServletRequest request
    ) throws Exception {
        Map<String, Integer> count = promotionGroupLogic.count(request);
        return ResponseHelper.success(count);
    }


    @PostMapping("/add")
    @ApiOperation(value = "新增团购活动")
    public ResponseHelper<String> add(
            @ApiParam(value = "object") @RequestBody(required = false)PromotionGroupBean promotionGroupBean,
            HttpServletRequest request
            ) throws Exception {
        String promotionGroupId = promotionGroupLogic.add(promotionGroupBean,request);
        return ResponseHelper.success(promotionGroupId);
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改团购活动")
    public ResponseHelper update(
            HttpServletRequest request,
            @ApiParam(value = "object") @RequestBody(required = false)PromotionGroupBean promotionGroupBean
    ) throws Exception {
        promotionGroupLogic.update(request,promotionGroupBean);
        return ResponseHelper.success();
    }

    @PostMapping("/status/update")
    @ApiOperation(value = "修改团购状态")
    public ResponseHelper updateStatus(
            @ApiParam(value = "id") @RequestParam(required = false) String promotionGroupId,
            @ApiParam(value = "状态 true:开启 false:关闭") @RequestParam(required = false) Boolean status
    ) throws Exception {
        promotionGroupLogic.updateStatus(promotionGroupId,status);
        return ResponseHelper.success();
    }


    @GetMapping("/pt/detail")
    @ApiOperation(value = "拼团详情列表")
    public ResponseHelper<IPage<PromotionGroupDetail>> ptDetail(
            @ApiParam(value = "promotionGroupId") @RequestParam(required = false) String promotionGroupId,
            @ApiParam(value = "团长手机号",required = false) @RequestParam(required = false) String mobile,
            @ApiParam(value = "pageNum") @RequestParam(required = true)Integer pageNum,
            @ApiParam(value = "pageSize") @RequestParam(required = true)Integer pageSize
    ) throws Exception {
        IPage<PromotionGroupDetail> promotionGroupDetailIPage = promotionGroupLogic.ptDetail(promotionGroupId,mobile,pageNum, pageSize);
        return ResponseHelper.success(promotionGroupDetailIPage);
    }

    @GetMapping("/pt/orderlist")
    @ApiOperation(value = "拼团订单列表")
    public ResponseHelper<IPage<OrderInfo>> ptOrderlist(
            @ApiParam(value = "promotionGroupId") @RequestParam(required = false) String promotionGroupId,
            @ApiParam(value = "用户id") @RequestParam(required = true)String userId,
            @ApiParam(value = "pageNum") @RequestParam(required = true)Integer pageNum,
            @ApiParam(value = "pageSize") @RequestParam(required = true)Integer pageSize
    ) throws Exception {
        IPage<OrderInfo> orderInfoIPage = promotionGroupLogic.queryOrderList(promotionGroupId,userId, pageNum, pageSize);
        return ResponseHelper.success(orderInfoIPage);
    }

    @GetMapping("/pt/visitlist")
    @ApiOperation(value = "拼团订单列表")
    public ResponseHelper<IPage<GoodsVisit>> ptVisitlist(
            @ApiParam(value = "promotionGroupId") @RequestParam(required = false) String promotionGroupId,
            @ApiParam(value = "用户id") @RequestParam(required = true)String userId,
            @ApiParam(value = "pageNum") @RequestParam(required = true)Integer pageNum,
            @ApiParam(value = "pageSize") @RequestParam(required = true)Integer pageSize,
            HttpServletRequest request
    ) throws Exception {
        IPage<GoodsVisit> goodsVisitIPage = promotionGroupLogic.queryGoodsVisit(promotionGroupId,userId, pageNum, pageSize, request);
        return ResponseHelper.success(goodsVisitIPage);
    }

}
