package com.ruoyi.app.mendian;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.PromotionCouponBatch;
import com.ruoyi.system.domain.PromotionCouponRule;
import com.ruoyi.system.service.IPromotionCouponBatchService;
import com.ruoyi.system.utils.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 商家端优惠券管理
 */
@RestController
@RequestMapping("/system/shPromotionCoupon")
public class ShPromotionCouponController extends BaseController
{
    @Autowired
    private IPromotionCouponBatchService promotionCouponBatchService;

    /**
     * 查询商家优惠券批次列表
     */
    @Anonymous
    @Auth
    @GetMapping("/list")
    public AjaxResult list(@RequestHeader String token,
                           @RequestParam Integer page,
                           @RequestParam Integer size,
                           @RequestParam Long storeId,
                           @RequestParam(required = false) Integer couponType,
                           @RequestParam(required = false) Integer status)
    {
        IPage<PromotionCouponBatch> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<PromotionCouponBatch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PromotionCouponBatch::getStoreId, storeId);
        if (couponType != null)
        {
            queryWrapper.eq(PromotionCouponBatch::getCouponType, couponType);
        }
        if (status != null)
        {
            queryWrapper.eq(PromotionCouponBatch::getStatus, status);
        }
        queryWrapper.orderByDesc(PromotionCouponBatch::getCreateTime);
        IPage<PromotionCouponBatch> result = promotionCouponBatchService.page(pageParam, queryWrapper);
        return success(result);
    }

    /**
     * 获取优惠券批次详细信息（含规则）
     */
    @Anonymous
    @Auth
    @GetMapping("/{id}")
    public AjaxResult getInfo(@RequestHeader String token, @PathVariable("id") Long id)
    {
        PromotionCouponBatch batch = promotionCouponBatchService.selectBatchWithRule(id);
        return success(batch);
    }

    /**
     * 新增优惠券批次
     */
    @Anonymous
    @Auth
    @PostMapping
    public AjaxResult add(@RequestHeader String token, @RequestBody PromotionCouponBatch batch)
    {
        List<PromotionCouponRule> rules = batch.getRules();
        if (rules != null && !rules.isEmpty())
        {
            return toAjax(promotionCouponBatchService.createBatch(batch, rules));
        }
        PromotionCouponRule rule = batch.getRule();
        return toAjax(promotionCouponBatchService.createBatch(batch, rule));
    }

    /**
     * 下架优惠券批次
     */
    @Anonymous
    @Auth
    @PutMapping("/{id}/offShelf")
    public AjaxResult offShelf(@RequestHeader String token, @PathVariable("id") Long id)
    {
        return toAjax(promotionCouponBatchService.offShelfBatch(id));
    }

    /**
     * 修改优惠券
     */
    @Anonymous
    @Auth
    @PutMapping
    public AjaxResult edit(@RequestHeader String token, @RequestBody PromotionCouponBatch batch)
    {
        List<PromotionCouponRule> rules = batch.getRules();
        if (rules != null && !rules.isEmpty())
        {
            return toAjax(promotionCouponBatchService.updateBatch(batch, rules));
        }
        PromotionCouponRule rule = batch.getRule();
        return toAjax(promotionCouponBatchService.updateBatch(batch, rule));
    }

    /**
     * 删除优惠券（仅未开始且无人领取）
     */
    @Anonymous
    @Auth
    @DeleteMapping("/{id}")
    public AjaxResult remove(@RequestHeader String token, @PathVariable("id") Long id)
    {
        return toAjax(promotionCouponBatchService.deleteBatch(id));
    }
}
