package com.ruoyi.web.controller.api;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mysql.cj.xdevapi.Collection;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.dto.TShopDTO;
import com.ruoyi.system.query.TShopQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.TShopVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static com.ruoyi.common.core.domain.AjaxResult.error;
/**
*
* 店铺信息 前端控制器
*
*
* @author xiaochen
* @since 2024-08-14
*/
@Api(tags = "店铺信息")
@RestController
@RequestMapping("/t-shop")
public class TShopController {
private final TShopService shopService;
private final ISysUserService userService;
private final TBoardService boardService;
private final TokenService tokenService;
private final TGoodsService goodsService;
private final TGoodsTypeService goodsTypeService;
private final TOrderMealService mealService;
private final TOrderMealGoodsService mealGoodsService;
private final TOrderSaleService saleService;
private final TOrderSaleGoodsService saleGoodsService;
private final TOrderStockService stockService;
private final TOrderStockGoodsService stockGoodsService;
private final TStockDataSetService stockDataSetService;
private final TDataGeneratorService dataGeneratorService;
@Autowired
public TShopController(TShopService shopService, ISysUserService userService, TBoardService boardService, TokenService tokenService, TGoodsService goodsService, TGoodsTypeService goodsTypeService, TOrderMealService mealService, TOrderMealGoodsService mealGoodsService, TOrderSaleService saleService, TOrderSaleGoodsService saleGoodsService, TOrderStockService stockService, TOrderStockGoodsService stockGoodsService, TStockDataSetService stockDataSetService, TDataGeneratorService dataGeneratorService) {
this.shopService = shopService;
this.userService = userService;
this.boardService = boardService;
this.tokenService = tokenService;
this.goodsService = goodsService;
this.goodsTypeService = goodsTypeService;
this.mealService = mealService;
this.mealGoodsService = mealGoodsService;
this.saleService = saleService;
this.saleGoodsService = saleGoodsService;
this.stockService = stockService;
this.stockGoodsService = stockGoodsService;
this.stockDataSetService = stockDataSetService;
this.dataGeneratorService = dataGeneratorService;
}
/**
* 查询店铺信息分页列表
*/
@ApiOperation( value = "查询店铺信息分页列表")
@PostMapping(value = "/pageList")
public AjaxResult> pageList(@RequestBody TShopQuery query) {
return AjaxResult.success(shopService.pageList(query));
}
/**
* 添加店铺信息
*/
@ApiOperation( value = "添加店铺信息")
@PostMapping(value = "/add")
public AjaxResult add(@RequestBody TShopDTO dto) {
SysUser user = new SysUser();
user.setUserName(dto.getAccount());
user.setPhonenumber(dto.getAccount());
if (!userService.checkUserNameUnique(user))
{
return error("添加用户'" + user.getUserName() + "'失败,登录账号已存在");
}
shopService.save(dto);
// 添加账号
Long userId = userService.addAccount(dto);
dto.setUserId(userId);
shopService.updateById(dto);
return AjaxResult.success();
}
/**
* 编辑店铺信息
*/
@ApiOperation( value = "编辑店铺信息")
@PostMapping(value = "/edit")
public AjaxResult edit(@RequestBody TShopDTO dto) {
SysUser user = new SysUser();
user.setUserName(dto.getAccount());
user.setPhonenumber(dto.getAccount());
user.setUserId(dto.getUserId());
if (!userService.checkUserNameUnique(user))
{
return error("修改用户'" + user.getUserName() + "'失败,登录账号已存在");
}
shopService.updateById(dto);
userService.updateAccount(dto);
return AjaxResult.success();
}
/**
* 查看店铺信息详情
*/
@ApiOperation( value = "查看店铺信息详情")
@GetMapping(value = "/getDetailById")
public AjaxResult getDetailById(@RequestParam("id") Long id) {
TShop shop = shopService.getById(id);
TShopVO shopVO = new TShopVO();
BeanUtils.copyProperties(shop,shopVO);
List list = boardService.list(Wrappers.lambdaQuery(TBoard.class)
.eq(TBoard::getShopId, id));
shopVO.setBoards(list);
return AjaxResult.success(shopVO);
}
/**
* 用户查看店铺信息详情
*/
@ApiOperation( value = "用户查看店铺信息详情")
@GetMapping(value = "/getDetailByUserId")
public AjaxResult getDetailByUserId() {
Integer roleType = tokenService.getLoginUser().getRoleType();
if(roleType == 1){
return error("该用户不是商家账号");
}
return AjaxResult.success(shopService.getOne(Wrappers.lambdaQuery().eq(TShop::getUserId, tokenService.getLoginUser().getUserId())
.last("LIMIT 1")));
}
/**
* 删除店铺信息
*/
@ApiOperation( value = "删除店铺信息")
@DeleteMapping(value = "/deleteById")
public AjaxResult deleteById(@RequestParam("id") Long id) {
TShop shop = shopService.getById(id);
// 查询账号
SysUser sysUser = userService.selectUserByUserName(shop.getAccount());
if(Objects.nonNull(sysUser)){
userService.deleteUserById(sysUser.getUserId());
}
// 删除店铺下的桌子
long boardCount = boardService.count(Wrappers.lambdaQuery(TBoard.class)
.eq(TBoard::getShopId, id));
if(boardCount>0){
boardService.deleteByShopId(id);
}
// 删除店铺下的餐品分类
long goodsCount = goodsService.count(Wrappers.lambdaQuery(TGoods.class)
.eq(TGoods::getShopId, id));
if(goodsCount>0){
goodsService.deleteByShopId(id);
}
long goodsTypeCount = goodsTypeService.count(Wrappers.lambdaQuery(TGoodsType.class)
.eq(TGoodsType::getShopId, id));
if (goodsTypeCount>0){
goodsTypeService.deleteByShopId(id);
}
// 删除店铺下的餐饮订单
List mealList = mealService.list(Wrappers.lambdaQuery(TOrderMeal.class)
.eq(TOrderMeal::getShopId, id));
List mealIds = mealList.stream().map(TOrderMeal::getId).collect(Collectors.toList());
if(!CollectionUtils.isEmpty(mealIds)){
mealGoodsService.deleteByOrderId(mealIds);
mealService.deleteByShopId(id);
}
// 删除店铺下的销售数据
List saleList = saleService.list(Wrappers.lambdaQuery(TOrderSale.class)
.eq(TOrderSale::getShopId, id));
List saleIds = saleList.stream().map(TOrderSale::getId).collect(Collectors.toList());
if(!CollectionUtils.isEmpty(saleIds)){
saleGoodsService.deleteByOrderId(saleIds);
saleService.deleteByShopId(id);
}
// 删除店铺下的进货数据
List stockList = stockService.list(Wrappers.lambdaQuery(TOrderStock.class)
.eq(TOrderStock::getShopId, id));
List stockIds = stockList.stream().map(TOrderStock::getId).collect(Collectors.toList());
if(!CollectionUtils.isEmpty(stockIds)){
stockGoodsService.deleteByOrderId(stockIds);
stockService.deleteByShopId(id);
}
// 删除店铺下的生成数据
long dataGeneratorCount = dataGeneratorService.count(Wrappers.lambdaQuery(TDataGenerator.class)
.eq(TDataGenerator::getShopId, id));
if (dataGeneratorCount>0){
dataGeneratorService.deleteByShopId(id);
}
return AjaxResult.success(shopService.removeById(id));
}
/**
* 启用禁用店铺
*/
@ApiOperation( value = "启用禁用店铺")
@GetMapping(value = "/updateStatus")
public AjaxResult updateStatus(@RequestParam(value = "id") Integer id,
@RequestParam(value = "status") Integer status) {
TShop shop = shopService.getById(id);
shop.setStatus(status);
shopService.updateById(shop);
SysUser sysUser = userService.selectUserByUserName(shop.getAccount());
if(Objects.nonNull(sysUser)){
if(status == 1){
sysUser.setStatus("0");
}else {
sysUser.setStatus("1");
}
userService.updateUser(sysUser);
}
return AjaxResult.success();
}
}