package com.sqx.modules.orders.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.orders.entity.PriceRule;
import com.sqx.modules.orders.service.PriceRuleService;
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.Date;


@RestController
@Api(value = "价格规则-管理端", tags = {"价格规则-管理端"})
@RequestMapping("/priceRule")
public class PriceRuleController {

    @Autowired
    private PriceRuleService priceRuleService;


    @PostMapping("/insertPriceRule")
    @ApiOperation("添加价格规则")
    public Result insertPriceRule(@RequestBody PriceRule priceRule){
        priceRule.setCreateTime(DateUtils.format(new Date()));
        priceRuleService.save(priceRule);
        return Result.success();
    }

    @PostMapping("/updatePriceRule")
    @ApiOperation("修改价格规则")
    public Result updatePriceRule(@RequestBody PriceRule priceRule){
        priceRuleService.updateById(priceRule);
        return Result.success();
    }

    @PostMapping("/deletePriceRule")
    @ApiOperation("删除价格规则")
    public Result deletePriceRule(Long priceRuleId){
        priceRuleService.removeById(priceRuleId);
        return Result.success();
    }

    @GetMapping("/selectPriceRuleList")
    @ApiOperation("查询价格规则")
    public Result selectPriceRuleList(Integer page,Integer limit,Integer peopleNum,Integer status){
        IPage<PriceRule> priceRuleIPage = priceRuleService.page(new Page<>(page, limit), new QueryWrapper<PriceRule>()
                .eq(peopleNum != null, "people_num", peopleNum).eq(status != null, "status", status).orderByAsc("start_num"));
        return Result.success().put("data",new PageUtils(priceRuleIPage));
    }




}