package com.liquidnet.service.slime.controller;

import com.github.pagehelper.PageInfo;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.kylin.dto.param.KylinGroupBuyActivityCreateParam;
import com.liquidnet.service.kylin.dto.param.KylinGroupBuyActivityUpdateParam;
import com.liquidnet.service.kylin.dto.vo.groupbuy.KylinGroupBuyActivityVo;
import com.liquidnet.service.kylin.dto.vo.groupbuy.KylinGroupBuyPartnerOrderDetailVo;
import com.liquidnet.service.kylin.dto.vo.groupbuy.KylinGroupBuyPartnerOrderListVo;
import com.liquidnet.service.kylin.dto.vo.groupbuy.KylinGroupBuyPartnerOrderStatsVo;
import com.liquidnet.service.kylin.service.partner.IKylinGroupBuyPartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;

@Api(tags = "第三方-拼团", position = 1005)
@RestController
@RequestMapping("partner/groupBuy")
@Validated
public class KylinGroupBuyPartnerController {

    @Autowired
    private IKylinGroupBuyPartnerService groupBuyPartnerService;

    @GetMapping("")
    @ApiOperation(value = "拼团活动列表", position = 1)
    @ApiResponse(response = KylinGroupBuyActivityVo.class, code = 200, message = "接口返回对象参数")
    public ResponseDto<PageInfo<KylinGroupBuyActivityVo>> list(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        return groupBuyPartnerService.list(name, status, page, size);
    }

    @GetMapping("/stats")
    @ApiOperation(value = "拼团活动统计", position = 8)
    public ResponseDto<Map<String, Long>> stats() {
        return groupBuyPartnerService.stats();
    }

    @GetMapping("/details")
    @ApiOperation(value = "拼团活动详情", position = 2)
    public ResponseDto<KylinGroupBuyActivityVo> detail(
            @RequestParam("activityId") @NotNull(message = "活动ID不能为空") String activityId) {
        return groupBuyPartnerService.detail(activityId);
    }

    @PostMapping("")
    @ApiOperation(value = "创建拼团活动", position = 3)
    public ResponseDto<KylinGroupBuyActivityVo> create(@RequestBody @Valid KylinGroupBuyActivityCreateParam param) {
        return groupBuyPartnerService.create(param);
    }

    @PutMapping("")
    @ApiOperation(value = "编辑拼团活动", position = 4)
    public ResponseDto<KylinGroupBuyActivityVo> update(@RequestBody @Valid KylinGroupBuyActivityUpdateParam param) {
        return groupBuyPartnerService.update(param);
    }

    @DeleteMapping("")
    @ApiOperation(value = "删除拼团活动", position = 5)
    public ResponseDto<Boolean> delete(@RequestParam("activityId") @NotNull(message = "活动ID不能为空") String activityId) {
        return groupBuyPartnerService.delete(activityId);
    }

    @PostMapping("/toggle")
    @ApiOperation(value = "启停拼团活动", position = 6)
    public ResponseDto<Boolean> toggle(@RequestParam("activityId") @NotNull(message = "活动ID不能为空") String activityId,
                                       @RequestParam("targetStatus") @NotNull(message = "目标状态不能为空") Integer targetStatus) {
        return groupBuyPartnerService.toggle(activityId, targetStatus);
    }

    @GetMapping("/orders/stats")
    @ApiOperation(value = "拼团订单统计", position = 10)
    @ApiResponse(response = KylinGroupBuyPartnerOrderStatsVo.class, code = 200, message = "接口返回对象参数")
    public ResponseDto<KylinGroupBuyPartnerOrderStatsVo> orderStats() {
        return groupBuyPartnerService.orderStats();
    }

    @GetMapping("/orders")
    @ApiOperation(value = "拼团订单列表", position = 11)
    @ApiResponse(response = KylinGroupBuyPartnerOrderListVo.class, code = 200, message = "接口返回对象参数")
    public ResponseDto<PageInfo<KylinGroupBuyPartnerOrderListVo>> orderList(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        return groupBuyPartnerService.orderList(keyword, status, page, size);
    }

    @GetMapping("/orders/details")
    @ApiOperation(value = "拼团订单详情", position = 12)
    @ApiResponse(response = KylinGroupBuyPartnerOrderDetailVo.class, code = 200, message = "接口返回对象参数")
    public ResponseDto<KylinGroupBuyPartnerOrderDetailVo> orderDetail(
            @RequestParam("groupOrderId") @NotNull(message = "拼团订单号不能为空") String groupOrderId) {
        return groupBuyPartnerService.orderDetail(groupOrderId);
    }

    @GetMapping("/orders/export")
    @ApiOperation(value = "导出拼团订单", position = 13)
    public void exportOrders(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "status", required = false) Integer status,
            HttpServletResponse response) {
        groupBuyPartnerService.exportOrders(keyword, status, response);
    }
}
