package com.ruoyi.web.controller.api;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TFoundationConfig;
import com.ruoyi.system.domain.TGoodsType;
import com.ruoyi.system.domain.TShop;
import com.ruoyi.system.service.TFoundationConfigService;
import com.ruoyi.system.service.TGoodsTypeService;
import com.ruoyi.system.service.TShopService;
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.List;
import java.util.Objects;
/**
*
* 商品分类 前端控制器
*
*
* @author xiaochen
* @since 2024-08-14
*/
@Api(tags = "商品分类")
@RestController
@RequestMapping("/t-goods-type")
public class TGoodsTypeController {
private final TGoodsTypeService goodsTypeService;
private final TokenService tokenService;
private final TShopService shopService;
private final TFoundationConfigService foundationConfigService;
@Autowired
public TGoodsTypeController(TGoodsTypeService goodsTypeService, TokenService tokenService, TShopService shopService, TFoundationConfigService foundationConfigService) {
this.goodsTypeService = goodsTypeService;
this.tokenService = tokenService;
this.shopService = shopService;
this.foundationConfigService = foundationConfigService;
}
/**
* 查询商品分类列表
*/
@ApiOperation( value = "查询商品分类列表")
@PostMapping(value = "/list")
public AjaxResult> list() {
Long shopId = tokenService.getLoginUser().getObjectId();
Integer roleType = tokenService.getLoginUser().getRoleType();
TShop shop = shopService.getById(shopId);
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
if(roleType != 1){
if(roleType == 2){
wrapper.eq(TGoodsType::getMealType, shop.getMealType());
}else {
wrapper.isNull(TGoodsType::getMealType);
}
}
return AjaxResult.success(goodsTypeService.list(wrapper));
}
/**
* 查询商品分类列表
*/
@ApiOperation( value = "查询商品分类列表-后台使用")
@GetMapping(value = "/getList")
public AjaxResult> getList(@RequestParam(value = "mealType",required = false) Integer mealType) {
// 获取餐饮分类店铺id
// TShop one = shopService.getOne(Wrappers.lambdaQuery(TShop.class)
// .eq(TShop::getMealType, mealType)
// .last("LIMIT 1"));
// if(Objects.isNull(one)){
// String str = mealType == 1 ? "中餐" : "火锅";
// return AjaxResult.error("无"+str+"类型店铺");
// }
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
if(Objects.nonNull(mealType)){
wrapper.eq(TGoodsType::getMealType, mealType);
}
return AjaxResult.success(goodsTypeService.list(wrapper));
}
/**
* 添加商品分类管理
*/
@ApiOperation( value = "添加商品分类")
@PostMapping(value = "/add")
public AjaxResult add(@RequestBody TGoodsType dto) {
Long shopId = tokenService.getLoginUser().getObjectId();
TShop shop = shopService.getById(shopId);
dto.setMealType(shop.getMealType());
dto.setShopId(shopId);
return AjaxResult.success(goodsTypeService.save(dto));
}
/**
* 修改商品分类
*/
@ApiOperation( value = "修改商品分类")
@PostMapping(value = "/update")
public AjaxResult update(@RequestBody TGoodsType dto) {
return AjaxResult.success(goodsTypeService.updateById(dto));
}
/**
* 查看商品分类详情
*/
@ApiOperation( value = "查看商品分类详情")
@GetMapping(value = "/getDetailById")
public AjaxResult getDetailById(@RequestParam("id") Long id) {
return AjaxResult.success(goodsTypeService.getById(id));
}
/**
* 删除商品分类
*/
@ApiOperation( value = "删除商品分类")
@DeleteMapping(value = "/deleteById")
public AjaxResult deleteById(@RequestParam("id") Long id) {
// 查询分类是否在生成配置里面存在
long count = foundationConfigService.count(Wrappers.lambdaQuery(TFoundationConfig.class)
.eq(TFoundationConfig::getTypeId, id));
if(count>0){
return AjaxResult.warn("该分类在基础设置中正在使用,无法删除");
}
return AjaxResult.success(goodsTypeService.removeById(id));
}
/**
* 批量删除商品分类
*/
@ApiOperation( value = "批量删除商品分类")
@DeleteMapping(value = "/deleteByIds")
public AjaxResult deleteByIds(@RequestBody List ids) {
return AjaxResult.success(goodsTypeService.removeByIds(ids));
}
}