package com.yonge.cooleshow.student.controller.coupon;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.coupon.CouponCategoryEnum;
import com.yonge.cooleshow.biz.dal.queryInfo.CouponInfoQuery;
import com.yonge.cooleshow.biz.dal.queryInfo.CouponIssueQueryInfo;
import com.yonge.cooleshow.biz.dal.service.CouponIssueService;
import com.yonge.cooleshow.biz.dal.vo.coupon.CouponInfoWrapper;
import com.yonge.cooleshow.biz.dal.vo.coupon.CouponIssueWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.student.io.request.CouponInfoVO;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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 java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 优惠券信息
 * Created by Eric.Shang on 2022/9/2.
 */
@RestController
@RequestMapping("/couponInfo")
@Api(value = "优惠券信息", tags = "优惠券信息")
public class CouponInfoController extends BaseController {

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private CouponIssueService couponIssueService;

    /**
     * 优惠券信息
     * @param request CouponInfoVO.RequestInfo
     * @return HttpResponseResult<PageInfo<CouponInfoVO.ResponseInfo>>
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询优惠券分页", notes = "传入CouponInfoVO.PageRequest")
    public HttpResponseResult<PageInfo<CouponInfoVO.CouponPageInfo>> queryCouponPageInfo(@RequestBody CouponInfoVO.PageRequest request) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        request.setUserId(user.getId());
        request.setClientType(ClientEnum.STUDENT.getCode());


        // 用户端的券类型 都包含全品类券
        if (request.getCouponCategory() != null) {
            List<CouponCategoryEnum> couponCategoryList = new ArrayList<>();
            couponCategoryList.add(CouponCategoryEnum.UNIVERSAL);
            couponCategoryList.add(request.getCouponCategory());
            request.setCouponCategory(null);
            request.setCouponCategoryList(couponCategoryList);
        }

        IPage<CouponIssueWrapper> couponIssueWrapperIPage = couponIssueService.queryCouponIssueInfo(
                PageUtil.getPage(request), CouponIssueQueryInfo.from(request.jsonString()));


        // 数据转换
        List<CouponInfoVO.CouponPageInfo> pageInfos = JSON.parseArray(JSON.toJSONString(couponIssueWrapperIPage.getRecords()),
                                                                      CouponInfoVO.CouponPageInfo.class);

        return succeed(PageUtil.getPageInfo(couponIssueWrapperIPage,pageInfos));
    }

    /**
     * 优惠券状态统计信息
     * @return List<CouponInfoVO.CouponIssueStateStat>
     */
    @GetMapping("/statInfo")
    @ApiOperation(value = "查询优惠券统计")
    public HttpResponseResult<List<CouponInfoVO.CouponStateStat>> findCouponStateStatInfo(
            @ApiParam(value = "优惠券类型") @RequestParam(value = "couponType", required = false) String couponType) {

        // 登录用户信息
        SysUser user = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(user) || Objects.isNull(user.getId())) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        // 优惠券统计
        List<CouponInfoWrapper.CouponStat> wrappers = couponIssueService.queryCouponStateStatInfo(user.getId(),
                CouponInfoQuery.CouponStateStatQuery.builder()
                        .clientType(ClientEnum.STUDENT)
                        .build().couponType(couponType));

        return succeed(JSON.parseArray(JSON.toJSONString(wrappers), CouponInfoVO.CouponStateStat.class));
    }
}
