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.PromotionActivity;
import com.ruoyi.system.domain.PromotionActivityRule;
import com.ruoyi.system.service.IPromotionActivityService;
import com.ruoyi.system.utils.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商家端促销活动管理
 */
@RestController
@RequestMapping("/system/shPromotionActivity")
public class ShPromotionActivityController extends BaseController
{
    @Autowired
    private IPromotionActivityService promotionActivityService;

    /**
     * 查询商家促销活动列表
     */
    @Anonymous
    @Auth
    @GetMapping("/list")
    public AjaxResult list(@RequestHeader String token,
                           @RequestParam Integer page,
                           @RequestParam Integer size,
                           @RequestParam Long storeId,
                           @RequestParam(required = false) Integer type,
                           @RequestParam(required = false) Integer status)
    {
        IPage<PromotionActivity> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<PromotionActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PromotionActivity::getStoreId, storeId);
        if (type != null)
        {
            queryWrapper.eq(PromotionActivity::getType, type);
        }
        if (status != null)
        {
            queryWrapper.eq(PromotionActivity::getStatus, status);
        }
        queryWrapper.orderByDesc(PromotionActivity::getCreateTime);
        IPage<PromotionActivity> result = promotionActivityService.page(pageParam, queryWrapper);
        return success(result);
    }

    /**
     * 获取促销活动详细信息（含规则）
     */
    @Anonymous
    @Auth
    @GetMapping("/{id}")
    public AjaxResult getInfo(@RequestHeader String token, @PathVariable("id") Long id)
    {
        PromotionActivity activity = promotionActivityService.selectActivityWithRules(id);
        return success(activity);
    }

    /**
     * 新增促销活动
     */
    @Anonymous
    @Auth
    @PostMapping
    public AjaxResult add(@RequestHeader String token, @RequestBody PromotionActivity activity)
    {
        List<PromotionActivityRule> rules = activity.getRules();
        return toAjax(promotionActivityService.createActivity(activity, rules));
    }

    /**
     * 结束促销活动
     */
    @Anonymous
    @Auth
    @PutMapping("/{id}/end")
    public AjaxResult end(@RequestHeader String token, @PathVariable("id") Long id)
    {
        return toAjax(promotionActivityService.endActivity(id));
    }

    /**
     * 修改促销活动（仅未开始）
     */
    @Anonymous
    @Auth
    @PutMapping
    public AjaxResult edit(@RequestHeader String token, @RequestBody PromotionActivity activity)
    {
        List<PromotionActivityRule> rules = activity.getRules();
        return toAjax(promotionActivityService.updateActivity(activity, rules));
    }

    /**
     * 删除促销活动（仅未开始）
     */
    @Anonymous
    @Auth
    @DeleteMapping("/{id}")
    public AjaxResult remove(@RequestHeader String token, @PathVariable("id") Long id)
    {
        return toAjax(promotionActivityService.deleteActivity(id));
    }
}
