package com.ym.mec.student.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.page.SysCouponCodeQueryInfo;
import com.ym.mec.biz.service.SysCouponCodeService;
import com.ym.mec.biz.service.SysCouponIssueRecordService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.common.page.WrapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@RequestMapping("sysCouponCode")
@Api(tags = "优惠券明细")
@RestController
public class SysCouponCodeController extends BaseController {

    @Autowired
    private SysCouponCodeService sysCouponCodeService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private SysCouponIssueRecordService couponIssueRecordService;

    @ApiOperation("分页查询")
    @RequestMapping(value = "queryPage")
    public HttpResponseResult queryPage(SysCouponCodeQueryInfo queryInfo) {
        if (Objects.isNull(queryInfo.getUserId())) {
            SysUser sysUser = sysUserFeignService.queryUserInfo();
            if (Objects.isNull(sysUser.getId())) {
                return failed("请登录");
            }
            queryInfo.setUserId(sysUser.getId());
        }
        return succeed(sysCouponCodeService.querySysCouponUseList(queryInfo));
    }

    @ApiOperation("获取云教练活动跑马灯")
    @RequestMapping(value = "queryPageAll")
    public HttpResponseResult queryPageAll(QueryInfo queryInfo) {
        return succeed(sysCouponCodeService.queryHorseRaceLampDtoList(queryInfo));
    }

    @ApiOperation("兑换优惠券")
    @PostMapping(value = "exchangeCoupon")
    public HttpResponseResult exchangeCoupon(Integer couponId, Long paymentOrderId, Integer exchangeNum) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(sysCouponCodeService.exchangeCouponTest(sysUser.getId(), couponId, paymentOrderId, exchangeNum));
    }

    /**
     * 手动领取优惠券
     *
     * @param param userId 领取者id
     * @param param couponId 优惠券id
     */
    @ApiOperation("手动领取优惠券")
    @PostMapping(value = "/manualIssueCoupon")
    public HttpResponseResult manualIssueCoupon(@RequestBody Map<String, Object> param) {
        Integer couponId = WrapperUtil.toInt(param, "couponId", "传入参数错误!");
        couponIssueRecordService.manualIssueCoupon(getUserId(), couponId);
        return succeed();
    }

    @ApiOperation("查询优惠券发放记录")
    @GetMapping(value = "/queryCanBeGet")
    public HttpResponseResult queryCanBeGetCoupon() {
        return succeed(couponIssueRecordService.queryCanBeGetCoupon(getUserId()));
    }

    private Integer getUserId() {
        //修改机构基础信息
        return Optional.ofNullable(sysUserFeignService.queryUserInfo())
                .map(SysUser::getId)
                .orElseThrow(() -> new BizException("用户信息获取失败,重新登录!"));
    }

}
