package com.yida.data.school.transaction.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.apply.EduRealProduct;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.school.transaction.service.EduRealProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "交易-实际商品")
@AllArgsConstructor
@Slf4j
@RequestMapping("/transaction/realProduct")
@RestController
public class EduRealProductController {

    private final EduRealProductService eduRealProductService;

    @ApiOperation("根据类别查询查询实际商品")
    @GetMapping("/listRealProduct")
    public ResultBean<List<EduRealProduct>> listRealProduct(@ApiParam("查询一级类目") @RequestParam(required = false) String firstCategory,
                                                            @ApiParam("查询二级类目") @RequestParam(required = false) String secondCategory) {
        LambdaQueryWrapper<EduRealProduct> condition = Wrappers.<EduRealProduct>lambdaQuery();
        if (StrUtil.isNotBlank(firstCategory)) {
            condition.eq(EduRealProduct::getFirstProductCategory, firstCategory);
        }
        if (StrUtil.isNotBlank(secondCategory)) {
            condition.eq(EduRealProduct::getSecondProductCategory, secondCategory);
        }
        return ResultBean.buildSuccess(eduRealProductService.list(condition));
    }

    @ApiOperation("添加为商品")
    @PostMapping("/saveRealProduct")
    public ResultBean saveRealProduct(@RequestBody EduRealProduct product) {
        Asserts.isTrue(eduRealProductService.count(Wrappers.lambdaQuery(new EduRealProduct())
                .eq(EduRealProduct::getService, product.getService())
                .eq(EduRealProduct::getProductType, product.getProductType())
                .eq(EduRealProduct::getProductValue, product.getProductValue())) == 0, "已添加该商品");
        eduRealProductService.saveOrUpdate(product);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除商品")
    @PostMapping("/delRealProductBatch")
    public ResultBean delRealProductBatch(@RequestBody List<Long> ids) {
        eduRealProductService.removeByIds(ids);
        return ResultBean.buildSuccess();
    }
}
