package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxOrder;
import com.iformall.domain.po.WxOrderGroup;
import com.iformall.domain.vo.WxOrderGroupMarketing;
import com.iformall.enums.EnumOrderStatus;
import com.iformall.service.WxCouponService;
import com.iformall.service.WxOrderGroupService;
import com.iformall.service.WxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/orderGroup")
@Api(description = "拼团接口")
public class WxOrderGroupController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxOrderService wxOrderService;

    @Autowired
    WxCouponService wxCouponService;

    @Autowired
    WxOrderGroupService wxOrderGroupService;


    @ApiOperation(value = "查询差1个的团购信息")
    @GetMapping("queryRemainOne")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "券ID", dataType = "Long", paramType = "query", required = true)
    })
    public ResultData queryRemainOne(@ModelAttribute WxOrderGroup wxOrderGroup) {
        if (wxOrderGroup.getCouponId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponId不能为空");
        }
        wxOrderGroup.setRemainPeople(1);
        wxOrderGroup.setStatus(EnumOrderStatus.ORDER_STATUS_COOPERATING.getCode());
        wxOrderGroup.setTenantId(getTenantId());
        return wxOrderGroupService.queryRemainOne(wxOrderGroup, getUserId());
    }


    @ApiOperation(value = "我的拼团")
    @GetMapping("queryOrderGroup")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
    })
    public ResultData queryOrderGroup(Integer pageNum, Integer pageSize) {
        WxOrder wxOrder = new WxOrder();
        wxOrder.setCUserId(getUserId());
        wxOrder.setTenantId(getTenantId());
        final PageInfo<Map<String, Object>> page = wxOrderGroupService.queryOrderGroup(wxOrder, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation(value = "去拼团")
    @PostMapping("toOrderGroup")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "orderId", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "couponId", value = "couponId", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true),
    })
    public ResultData toOrderGroup(@RequestBody WxOrderGroup wxOrderGroup) {
        if (wxOrderGroup.getId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "id不能为空");
        }
        if (wxOrderGroup.getCouponId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponId不能为空");
        }
        if (wxOrderGroup.getOrderId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId不能为空");
        }
        wxOrderGroup.setTenantId(getTenantId());
        return wxOrderGroupService.toOrderGroup(wxOrderGroup);
    }

    @ApiOperation(value = "查询拼团状态")
    @GetMapping("queryOrderGroupStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "orderId", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "couponId", value = "couponId", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true),
    })
    public ResultData queryOrderGroupStatus(@ModelAttribute WxOrderGroup wxOrderGroup) {
        if (wxOrderGroup.getId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "id不能为空");
        }
        if (wxOrderGroup.getCouponId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponId不能为空");
        }
        if (wxOrderGroup.getOrderId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId不能为空");
        }
        wxOrderGroup.setTenantId(getTenantId());
        return wxOrderGroupService.queryOrderGroupStatus(wxOrderGroup);
    }

    @ApiOperation(value = "查询是否已参团")
    @GetMapping("queryAttendStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true),
    })
    public ResultData queryAttendStatus(@ModelAttribute WxOrderGroup wxOrderGroup) {
        if (wxOrderGroup.getId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "id不能为空");
        }
        wxOrderGroup.setUserId(getUserId());
        wxOrderGroup.setTenantId(getTenantId());
        return wxOrderGroupService.queryAttendStatus(wxOrderGroup);
    }

    @ApiOperation(value = "拼团营销分析")
    @GetMapping("queryOrderGroupMarketingList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "couponId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
    })
    public ResultData queryOrderGroupMarketingList(@ModelAttribute WxOrderGroupMarketing wxOrderGroupMarketing, Integer pageNum, Integer pageSize) {
        final PageInfo<WxOrderGroupMarketing> page = wxOrderGroupService.queryOrderGroupMarketingList(wxOrderGroupMarketing, pageNum, pageSize);
        return new ResultData(page);
    }

}
