package com.ylx.coupon.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.enums.BusinessType;
import com.ylx.coupon.domain.Coupon;
import com.ylx.coupon.domain.CouponReceive;
import com.ylx.common.core.domain.R;
import com.ylx.massage.domain.vo.ClaimCouponRequestVO;
import com.ylx.massage.domain.vo.CouponReceiveVo;
import com.ylx.massage.domain.vo.CouponReceivesVO;
import com.ylx.massage.domain.vo.ShareVolutionDetailVO;
import com.ylx.coupon.service.CouponReceiveService;
import com.ylx.massage.service.ShareVolutionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 优惠券领取表(CouponReceive)表控制层
 *
 * @author makejava
 * @since 2024-05-14 11:25:44
 */
@RestController
@RequestMapping("couponReceive")
@Slf4j
@Api(tags = { "优惠劵认领" })
public class CouponReceiveController {
    /**
     * 服务对象
     */
    @Resource
    private CouponReceiveService couponReceiveService;

    @Resource
    private ShareVolutionService shareVolutionService;

    /**
     * 分页查询所有数据
     *
     * @param page          分页对象
     * @param couponReceive 查询实体
     * @return 所有数据
     */
    @GetMapping("page")
    @ApiOperation("分页查询优惠认领列表")
    public R selectAll(Page<CouponReceive> page, CouponReceive couponReceive) {
        return R.ok(this.couponReceiveService.page(page, new QueryWrapper<>(couponReceive)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getById")
    @ApiOperation("通过主键查询优惠认领")
    public R selectOne(String id) {
        return R.ok(this.couponReceiveService.getById(id));
    }

    /**
     * 领取优惠卷
     *
     * @param couponReceive 实体对象
     * @return R 新增结果
     */
    @PostMapping("submit")
    @ApiOperation("领取优惠卷")
    public R insert(@RequestBody CouponReceive couponReceive) {
        try {
            return R.ok(this.couponReceiveService.submit(couponReceive));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 首页领取优惠卷(批量领取优惠券)
     *
     * @param couponReceive
     * @return R
     */
    @PostMapping("submits")
    @ApiOperation("首页领取优惠卷")
    @Log(title = "首页领取优惠卷", businessType = BusinessType.INSERT)
    public R insert(@RequestBody CouponReceivesVO couponReceive) {
        try {
            return R.ok(this.couponReceiveService.submits(couponReceive));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 用户领取劵中心的优惠劵
     *
     * @param claimCouponRequestVO
     * @return R<?>
     */
    @ApiOperation(value = "用户领取劵中心的优惠劵", notes = "用户领取劵中心的优惠劵")
    @PostMapping(value = "/receiveCoupon")
    public R<?> receiveCoupon(@Validated @RequestBody ClaimCouponRequestVO claimCouponRequestVO) {
        try {
            log.info("用户领取劵中心的优惠劵请求参数:{}", JSON.toJSONString(claimCouponRequestVO));
            return couponReceiveService.receiveVolution(claimCouponRequestVO);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询我的优惠卷
     *
     * @param couponReceive
     * @return R<List<CouponReceiveVo>>
     */
    @PostMapping("myCoupon")
    @ApiOperation("我的优惠卷")
    public R<List<CouponReceiveVo>> myCoupon(@RequestBody CouponReceive couponReceive) {
        try {
            return R.ok(this.couponReceiveService.getByOpenId(couponReceive.getReceiveOpenId()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    @ApiOperation("用户删除优惠卷")
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.couponReceiveService.removeByIds(idList));
    }

    @PostMapping("windows")
    @ApiOperation("优惠卷弹出")
    public R<List<Coupon>> couponWindows(@RequestBody CouponReceive couponReceive) {
        return R.ok(this.couponReceiveService.couponWindows(couponReceive));
    }

    /**
     * 通过优惠券ID查询优惠券详情（调用本地生活的接口）
     *
     * @param couponId
     * @return R<ShareVolutionDetailVO>
     */
    @GetMapping("getShareVolutionDetail")
    @ApiOperation("通过优惠券ID查询优惠券详情")
    public R<ShareVolutionDetailVO> getShareVolutionDetail(@RequestParam("couponId") String couponId) {
        try {
            log.info("通过优惠券ID查询优惠券详情, couponId: {}", couponId);
            return shareVolutionService.getVolutionDetailByCouponId(couponId);
        } catch (Exception e) {
            log.error("通过优惠券ID查询优惠券详情异常, couponId: {}", couponId, e);
            return R.fail(e.getMessage());
        }
    }

}
