package com.yida.data.device.telephone.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.telephone.EduTelephoneMeal;
import com.yida.data.common.core.entity.telephone.EduTelephonePrice;
import com.yida.data.device.telephone.service.EduTelephoneMealService;
import com.yida.data.device.telephone.service.EduTelephonePriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 亲情电话-套餐 Controller
 *
 * @author zhanghaijun
 * @date 2021-08-10 17:00:06
 */
@Api(tags = "亲情电话-费率-套餐管理")
@Slf4j
@Validated
@RestController
@RequestMapping("/telephone/meal")
@RequiredArgsConstructor
public class EduTelephoneMealController {
    private final EduTelephonePriceService eduTelephonePriceService;
    private final EduTelephoneMealService eduTelephoneMealService;


    @ApiOperation("查询套餐")
    @GetMapping("/listMeal")
    public ResultBean<List<EduTelephoneMeal>> listMeal() {
        return ResultBean.buildSuccess(eduTelephoneMealService.list());
    }

    @ApiOperation("/添加套餐")
    @PostMapping("/saveMeal")
    public ResultBean saveMeal(@RequestBody EduTelephoneMeal meal) {
        eduTelephoneMealService.saveOrUpdate(meal);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询费率")
    @GetMapping("/listPrice")
    public ResultBean<List<EduTelephonePrice>> listPrice(@ApiParam("套餐类型") @RequestParam(required = false) Integer type,
                                                         @ApiParam("套餐名称") @RequestParam(required = false) String name) {
        LambdaQueryWrapper<EduTelephonePrice> condition =
                Wrappers.lambdaQuery(new EduTelephonePrice()).orderByDesc(EduTelephonePrice::getCreateDate);
        if (type != null) {
            condition.eq(EduTelephonePrice::getType, type);
        }
        if (StrUtil.isNotBlank(name)) {
            condition.like(EduTelephonePrice::getName, name);
        }
        return ResultBean.buildSuccess(eduTelephonePriceService.list(condition));
    }

    @ApiOperation("保存费率")
    @PostMapping("/savePrice")
    public ResultBean savePrice(@RequestBody EduTelephonePrice meal) {
        eduTelephonePriceService.saveOrUpdate(meal);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除费率")
    @PostMapping("/delPriceBatch")
    public ResultBean delPriceBatch(@RequestBody List<Long> ids) {
        eduTelephonePriceService.delPriceBatch(ids);
        return ResultBean.buildSuccess();
    }
}
