店铺扩展数据 初始化四条数据
This commit is contained in:
parent
2ca427f808
commit
489b3597d6
|
|
@ -1,7 +1,9 @@
|
|||
package cn.ysk.cashier.controller.shop;
|
||||
|
||||
import cn.ysk.cashier.exception.BadRequestException;
|
||||
import cn.ysk.cashier.mybatis.entity.TbShopExtend;
|
||||
import cn.ysk.cashier.mybatis.service.TbShopExtendService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
|
@ -51,8 +53,11 @@ public class TbShopExtendController {
|
|||
@PutMapping
|
||||
@ApiOperation("通过id修改")
|
||||
public ResponseEntity<Object> update(@RequestBody TbShopExtend tbShopExtend) {
|
||||
tbShopExtend.setUpdateTime(new Date());
|
||||
tbShopExtendService.updateById(tbShopExtend);
|
||||
if (tbShopExtend.getId() != null) {
|
||||
tbShopExtendService.updateById(tbShopExtend);
|
||||
} else {
|
||||
tbShopExtendService.saveInfo(tbShopExtend);
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package cn.ysk.cashier.mybatis.entity;
|
|||
import java.util.Date;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.activerecord.Model;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -93,5 +94,35 @@ public class TbShopExtend extends Model<TbShopExtend> {
|
|||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* index_bg:店铺首页背景图
|
||||
* my_bg: 我的页面背景图
|
||||
* member_bg:会员卡页面背景图
|
||||
* shopinfo_bg:商品列表顶部背景图
|
||||
* @param autokey
|
||||
*/
|
||||
public void initAutoKey(String autokey,Integer shopId) {
|
||||
if (StringUtils.isBlank(name)) {
|
||||
switch (autokey) {
|
||||
case "index_bg":
|
||||
this.name = "店铺首页背景图";
|
||||
break;
|
||||
case "my_bg":
|
||||
this.name = "我的页面背景图";
|
||||
break;
|
||||
case "member_bg":
|
||||
this.name = "会员卡页面背景图";
|
||||
break;
|
||||
case "shopinfo_bg":
|
||||
this.name = "商品列表顶部背景图";
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.type="img";
|
||||
this.shopId=shopId;
|
||||
this.autokey = autokey;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import cn.ysk.cashier.utils.PageUtil;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 店铺扩展信息(TbShopExtend)表服务实现类
|
||||
|
|
@ -40,6 +41,7 @@ public class TbShopExtendServiceImpl extends ServiceImpl<TbShopExtendMapper, TbS
|
|||
}
|
||||
wrapper.orderByDesc("create_time");
|
||||
Page<TbShopExtend> ipage = tbShopExtendmapper.selectPage(page, wrapper);
|
||||
checkAndInitialize(ipage,criteria.getShopId());
|
||||
return PageUtil.toPage(ipage.getRecords(), ipage.getTotal());
|
||||
}
|
||||
|
||||
|
|
@ -53,5 +55,43 @@ public class TbShopExtendServiceImpl extends ServiceImpl<TbShopExtendMapper, TbS
|
|||
}
|
||||
tbShopExtendmapper.insert(tbShopExtend);
|
||||
}
|
||||
|
||||
public Page<TbShopExtend> checkAndInitialize(Page<TbShopExtend> ipage, Integer shopId) {
|
||||
List<TbShopExtend> newRecords = new ArrayList<>();
|
||||
List<String> requiredAutokeys = Arrays.asList("index_bg", "my_bg", "member_bg", "shopinfo_bg");
|
||||
|
||||
if (ipage == null || ipage.getRecords() == null || ipage.getRecords().isEmpty()) {
|
||||
// ipage 为空,直接创建包含四种类型数据的新 Page 对象
|
||||
for (String key : requiredAutokeys) {
|
||||
TbShopExtend newRecord = new TbShopExtend();
|
||||
newRecord.initAutoKey(key, shopId);
|
||||
newRecords.add(newRecord);
|
||||
}
|
||||
return new Page<TbShopExtend>(1, newRecords.size(), newRecords.size()) // pageNum, pageSize, total
|
||||
.setRecords(newRecords);
|
||||
} else {
|
||||
// ipage 不为空,补充缺失的数据
|
||||
List<TbShopExtend> shopExtends = ipage.getRecords();
|
||||
Set<String> existingAutokeys = shopExtends.stream()
|
||||
.map(TbShopExtend::getAutokey)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<String> missingAutokeys = requiredAutokeys.stream()
|
||||
.filter(key -> !existingAutokeys.contains(key))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (String key : missingAutokeys) {
|
||||
TbShopExtend newRecord = new TbShopExtend();
|
||||
newRecord.initAutoKey(key, shopId);
|
||||
newRecords.add(newRecord);
|
||||
}
|
||||
|
||||
// 更新现有记录并将新记录添加到原来的列表中
|
||||
shopExtends.addAll(newRecords);
|
||||
|
||||
return new Page<TbShopExtend>(ipage.getCurrent(), ipage.getSize(), ipage.getTotal() + newRecords.size())
|
||||
.setRecords(shopExtends);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue