提交 b5eb8629 作者: xiaowang

新增:商品新增详情接口

上级 a37943fb
......@@ -339,7 +339,7 @@ public enum ResultEnum implements BaseErrorInfoInterface {
GOODS_ON_SHELF_SUCCESS("30905", "商品上架成功"),
GOODS_DOWN_SHELF_SUCCESS("30906", "商品下架成功"),
GOODS_SKU_IS_NOT_NULL("30907", "商品规格不能为空"),
GOODS_PIC_IS_NOT_NULL("30908", "主图不能为空"),
GOODS_PIC_IS_NOT_NULL("30908", "商品主图不能为空"),
GOODS_DESC_IS_NOT_NULL("30910", "商品描述不能为空"),
GOODS_GROUP_IS_NOT_NULL("30911", "商品类型不能为空"),
GROUP_DONT_DELETE_BY_CHILD("30912", "该分类下尚有子分类存在"),
......
package com.mmc.pms.controller.mall;
import com.mmc.pms.common.ResultBody;
import com.mmc.pms.controller.BaseController;
import com.mmc.pms.model.mall.MallGoodsVO;
import com.mmc.pms.service.mall.MallGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* @Author LW
* @date 2023/7/24 16:56 概要:
*/
@RestController
@RequestMapping("/mall/goods")
@Api(tags = {"V1.0.3-商城商品-相关接口"})
public class MallGoodsController extends BaseController {
@Resource
private MallGoodsService mallGoodsService;
@ApiOperation(value = "新增商城商品")
@PostMapping("addMallGoods")
@ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
public ResultBody addMallGoods(@RequestBody MallGoodsVO mallGoodsVO, HttpServletRequest request) {
return mallGoodsService.addMallGoods(mallGoodsVO, this.getUserLoginInfoFromRedis(request).getUserAccountId());
}
@ApiOperation(value = "商城商品详情")
@GetMapping("mallGoodsDetails")
@ApiResponses({@ApiResponse(code = 200, message = "OK", response = MallGoodsVO.class)})
public ResultBody<MallGoodsVO> mallGoodsDetails(@RequestParam Long id) {
return mallGoodsService.mallGoodsDetails(id);
}
}
package com.mmc.pms.dao.mall;
import com.mmc.pms.entity.mall.GoodsSpecDO;
import com.mmc.pms.entity.mall.GoodsSpecValuesDO;
import com.mmc.pms.entity.mall.MallGoodsDO;
import com.mmc.pms.entity.mall.MallGoodsResourcesDO;
import com.mmc.pms.model.mall.MallGoodsVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @Author LW
* @date 2023/7/24 17:38 概要:
*/
@Mapper
public interface MallGoodsDao {
int countMallGoodsByName(@Param("mallGoodsVO") MallGoodsVO mallGoodsVO, @Param("userAccountId") Integer userAccountId);
int countMallGoods();
void insertMallGoodsBaseInfo(MallGoodsDO mallGoodsDO);
void insertGoodsSpec(GoodsSpecDO goodsSpecDO);
void batchInsertSpecValues(List<GoodsSpecValuesDO> goodsSpecValuesList);
void batchInsertMallGoodsResources(List<MallGoodsResourcesDO> mallGoodsResourcesList);
MallGoodsDO getMallGoodsBaseInfo(Long id);
List<MallGoodsResourcesDO> getMallGoodsResources(Long id);
List<GoodsSpecDO> getMallGoodsSpec(Long id);
List<GoodsSpecValuesDO> getMallGoodsSpecValues(List<Integer> ids);
}
package com.mmc.pms.entity.mall;
import com.mmc.pms.model.mall.GoodsSpecVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* (GoodsSpecDO)实体类
*
* @author makejava
* @since 2023-07-24 21:16:41
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GoodsSpecDO implements Serializable {
private static final long serialVersionUID = 381669128695968475L;
private Integer id;
/**
* 商品id
*/
private Long mallGoodsId;
/**
* 规格名称
*/
private String specName;
/**
* 0单选,1多选
*/
private Integer chooseType;
/**
* 0 非必选 1 必选
*/
private Integer must;
/**
* 单位id
*/
private Integer skuUnitId;
private Date createTime;
private Date updateTime;
public GoodsSpecDO(GoodsSpecVO goodsSpecVO) {
this.mallGoodsId = goodsSpecVO.getMallGoodsId();
this.specName = goodsSpecVO.getSpecName();
this.chooseType = goodsSpecVO.getChooseType();
this.must = goodsSpecVO.getMust();
this.skuUnitId = goodsSpecVO.getSkuUnitId();
}
public GoodsSpecVO buildGoodsSpecVO() {
return GoodsSpecVO.builder().id(id).mallGoodsId(mallGoodsId).specName(specName)
.chooseType(chooseType).must(must).skuUnitId(skuUnitId).build();
}
}
package com.mmc.pms.entity.mall;
import com.mmc.pms.model.mall.GoodsSpecValuesVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
/**
* (GoodsSpecValuesDO)实体类
*
* @author makejava
* @since 2023-07-24 21:18:28
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GoodsSpecValuesDO implements Serializable {
private static final long serialVersionUID = -35192084826222973L;
private Integer id;
/**
* 规格id
*/
private Integer goodsSpecId;
/**
* 选项名称
*/
private String specValueName;
/**
* 料号
*/
private String partNo;
/**
* 规格图片
*/
private String specValueImage;
/**
* 销售价是否显示 0:否 1:是
*/
private Integer showPrice;
/**
* 库存
*/
private Integer stock;
/**
* 销售价格
*/
private BigDecimal salePrice;
/**
* 渠道价格
*/
private BigDecimal channelPrice;
private Date createTime;
private Date updateTime;
public GoodsSpecValuesDO(GoodsSpecValuesVO d) {
this.goodsSpecId = d.getGoodsSpecId();
this.specValueName = d.getSpecValueName();
this.partNo = d.getPartNo();
this.specValueImage = d.getSpecValueImage();
this.showPrice = d.getShowPrice();
this.channelPrice = d.getChannelPrice();
this.salePrice = d.getSalePrice();
this.stock = d.getStock();
}
public GoodsSpecValuesVO buildGoodsSpecValuesVO() {
return GoodsSpecValuesVO.builder().id(id).goodsSpecId(goodsSpecId)
.specValueName(specValueName).specValueImage(specValueImage)
.partNo(partNo).showPrice(showPrice).stock(stock)
.salePrice(salePrice).channelPrice(channelPrice).build();
}
}
package com.mmc.pms.entity.mall;
import com.mmc.pms.model.mall.MallGoodsVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* 商品基本信息(MallGoodsDO)实体类
*
* @author makejava
* @since 2023-07-24 21:10:02
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MallGoodsDO implements Serializable {
private static final long serialVersionUID = 709941898403563905L;
private Long id;
/**
* 商品编号(ID12345678)
*/
private String goodsNo;
/**
* 商品名称
*/
private String tradeName;
/**
* 商品描述
*/
private String description;
/**
* 商品一级分类
*/
private Integer categoryPrimaryId;
/**
* 商品二级分类
*/
private Integer categorySubId;
/**
* 状态:0: 下架 1:上架
*/
private Integer shelfStatus;
/**
* 商品标签
*/
private String goodsLabel;
/**
* 标签是否显示0:否1:是
*/
private Integer labelShow;
/**
* 商品详情图 富文本
*/
private String goodsDetails;
/**
* 创建商品用户id
*/
private Integer userAccountId;
/**
* 排序字段
*/
private Integer sort;
private Date createTime;
private Date updateTime;
private Integer deleted;
public MallGoodsDO(MallGoodsVO mallGoodsVO) {
this.id = mallGoodsVO.getId();
this.tradeName = mallGoodsVO.getTradeName();
this.description = mallGoodsVO.getDescription();
this.categoryPrimaryId = mallGoodsVO.getCategoryPrimaryId();
this.categorySubId = mallGoodsVO.getCategorySubId();
this.shelfStatus = mallGoodsVO.getShelfStatus();
this.goodsLabel = mallGoodsVO.getGoodsLabel();
this.labelShow = mallGoodsVO.getLabelShow();
this.goodsDetails = mallGoodsVO.getGoodsDetails();
}
public MallGoodsVO buildMallGoodsVO() {
return MallGoodsVO.builder().id(id).tradeName(tradeName).description(description)
.categoryPrimaryId(categoryPrimaryId).categorySubId(categorySubId).shelfStatus(shelfStatus)
.goodsLabel(goodsLabel).labelShow(labelShow).goodsDetails(goodsDetails).build();
}
}
package com.mmc.pms.entity.mall;
import com.mmc.pms.model.mall.GoodsResourcesVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* (MallGoodsResourcesDO)实体类
*
* @author makejava
* @since 2023-07-24 21:13:15
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MallGoodsResourcesDO implements Serializable {
private static final long serialVersionUID = -98370711703270379L;
private Integer id;
private Long mallGoodsId;
/**
* 资源url
*/
private String url;
/**
* 类型:0主图 1副图 2视频
*/
private Integer type;
private Date createTime;
private Date updateTime;
public MallGoodsResourcesDO(GoodsResourcesVO d) {
this.type = d.getType();
this.id = d.getId();
this.url = d.getUrl();
}
public GoodsResourcesVO buildGoodsResourcesVO() {
return GoodsResourcesVO.builder().id(id).url(url).type(type).build();
}
}
package com.mmc.pms.model.mall;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author LW
* @date 2023/7/24 17:57
* 概要:
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class GoodsResourcesVO {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "url")
private String url;
@ApiModelProperty(value = "类型:0主图 1副图 2视频")
private Integer type;
}
package com.mmc.pms.model.mall;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @Author LW
* @date 2023/7/24 20:16
* 概要:
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class GoodsSpecVO {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "商品id")
private Long mallGoodsId;
@ApiModelProperty(value = "规格名称", example = "规格名称", required = true)
private String specName;
@ApiModelProperty(value = "选择方式 :0单选,1多选", example = "1", required = true)
private Integer chooseType;
@ApiModelProperty(value = "是否必选 : 0 非必选 1 必选", example = "0", required = true)
private Integer must;
@ApiModelProperty(value = "单位id", example = "1", required = true)
private Integer skuUnitId;
@ApiModelProperty(value = "规格值信息", required = true)
private List<GoodsSpecValuesVO> goodsSpecValuesList;
}
package com.mmc.pms.model.mall;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
/**
* @Author LW
* @date 2023/7/24 20:26
* 概要:
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class GoodsSpecValuesVO {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "规格id", example = "1", required = true)
private Integer goodsSpecId;
@ApiModelProperty(value = "选项名称", example = "规格值名称", required = true)
private String specValueName;
@ApiModelProperty(value = "料号", example = "料号", required = true)
private String partNo;
@ApiModelProperty(value = "规格值图片url", example = "1.jpg", required = true)
private String specValueImage;
@ApiModelProperty(value = "是否展示销售价格 0:否 1:是", example = "1", required = true)
private Integer showPrice;
@ApiModelProperty(value = "库存数量", example = "100", required = true)
private Integer stock;
@ApiModelProperty(value = "销售价格", example = "100.00", required = true)
private BigDecimal salePrice;
@ApiModelProperty(value = "渠道价格", example = "50.00", required = true)
private BigDecimal channelPrice;
}
package com.mmc.pms.model.mall;
import com.mmc.pms.model.group.Create;
import com.mmc.pms.model.group.Update;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* @Author LW
* @date 2023/7/24 17:44
* 概要:
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MallGoodsVO {
@ApiModelProperty(value = "id", example = "1")
@NotNull(message = "id不能为空", groups = {Update.class})
private Long id;
@ApiModelProperty(value = "商品名称", example = "这是商品名称", required = true)
@NotBlank(message = "商品名称不能为空", groups = {Update.class, Create.class})
private String tradeName;
@ApiModelProperty(value = "商品描述", example = "商品描述", required = true)
@NotBlank(message = "商品描述不能为空", groups = {Update.class, Create.class})
private String description;
@ApiModelProperty(value = "商品资源:图片or视频及其他")
@NotEmpty(message = "图片不能为空")
private List<GoodsResourcesVO> resourcesList;
@ApiModelProperty(value = "一级分类id", example = "1", required = true)
@NotNull(message = "一级分类id不能为空", groups = {Update.class, Create.class})
private Integer categoryPrimaryId;
@ApiModelProperty(value = "二级分类id", example = "2", required = true)
@NotNull(message = "二级分类id不能为空", groups = {Update.class, Create.class})
private Integer categorySubId;
@ApiModelProperty(value = "商品状态", example = "1", required = true)
@NotNull(message = "商品状态不能为空", groups = {Update.class, Create.class})
private Integer shelfStatus;
@ApiModelProperty(value = "商品标签")
private String goodsLabel;
@ApiModelProperty(value = "标签是否显示 0否 1是")
private Integer labelShow;
@ApiModelProperty(value = "规格")
@NotEmpty(message = "规格不能为空", groups = {Update.class, Create.class})
private List<GoodsSpecVO> goodsSpecList;
@ApiModelProperty(value = "商品详情 富文本")
private String goodsDetails;
}
package com.mmc.pms.model.sale.vo;
import com.mmc.pms.model.sale.dto.ProductSpecVO;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.List;
/**
* @Author small @Date 2023/5/16 15:27 @Version 1.0
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GoodsSpecVO implements Serializable {
private static final long serialVersionUID = -8681372139970849591L;
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "规格名称")
private String goodsSpecName;
@ApiModelProperty(value = "产品类型")
private Integer goodsTypeId;
@ApiModelProperty(value = "产品:自定义时此字段可不用填写")
private Integer skuId;
@ApiModelProperty(value = "规格来源 0:获取 1:自定义")
private Integer flag;
@ApiModelProperty(value = "产品名称(自定义的时候才需要传值)")
private String productName;
@ApiModelProperty(value = "选项来源")
private List<MallProductSpecVO> specIds;
@ApiModelProperty(value = "选择方式")
private Integer chooseType;
@ApiModelProperty(value = "规格单位")
private Integer skuUnitId;
@ApiModelProperty(value = "是否必选")
private Integer must;
@ApiModelProperty(value = "自定义的信息填充")
private List<ProductSpecVO> customizeInfo;
@ApiModelProperty(value = "删除的自定义规格id")
private List<Integer> delProductSpecId;
}
package com.mmc.pms.service.mall;
import com.mmc.pms.common.ResultBody;
import com.mmc.pms.model.mall.MallGoodsVO;
/**
* @Author LW
* @date 2023/7/24 17:37 概要:
*/
public interface MallGoodsService {
ResultBody addMallGoods(MallGoodsVO mallGoodsVO, Integer userAccountId);
ResultBody<MallGoodsVO> mallGoodsDetails(Long id);
}
package com.mmc.pms.service.mall.impl;
import com.mmc.pms.common.ResultBody;
import com.mmc.pms.common.ResultEnum;
import com.mmc.pms.dao.mall.MallGoodsDao;
import com.mmc.pms.entity.mall.GoodsSpecDO;
import com.mmc.pms.entity.mall.GoodsSpecValuesDO;
import com.mmc.pms.entity.mall.MallGoodsDO;
import com.mmc.pms.entity.mall.MallGoodsResourcesDO;
import com.mmc.pms.model.mall.GoodsResourcesVO;
import com.mmc.pms.model.mall.GoodsSpecVO;
import com.mmc.pms.model.mall.GoodsSpecValuesVO;
import com.mmc.pms.model.mall.MallGoodsVO;
import com.mmc.pms.service.mall.MallGoodsService;
import com.mmc.pms.util.CodeUtil;
import com.mmc.pms.util.SnowFlake;
import com.mmc.pms.util.TDateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Author LW
* @date 2023/7/24 17:37 概要:
*/
@Service
public class MallGoodsServiceImpl implements MallGoodsService {
@Resource
private MallGoodsDao mallGoodsDao;
@Override
@Transactional(rollbackFor = Exception.class)
public ResultBody addMallGoods(MallGoodsVO mallGoodsVO, Integer userAccountId) {
ResultBody resultError = checkInformation(mallGoodsVO, userAccountId);
if (resultError != null) return resultError;
int count = mallGoodsDao.countMallGoods();
// 使用雪花算法生成雪花id,用作商品id,及其他id
SnowFlake snowFlake = new SnowFlake(2, 3);
long id = snowFlake.nextId();
mallGoodsVO.setId(id);
MallGoodsDO mallGoodsDO = new MallGoodsDO(mallGoodsVO);
mallGoodsDO.setGoodsNo("MG" + TDateUtil.getDateStr(new Date(), "yyyyMMddHHmmss") + CodeUtil.getRandomNum(4));
mallGoodsDO.setUserAccountId(userAccountId);
mallGoodsDO.setSort(count + 1);
// 将基础信息存储入库
mallGoodsDao.insertMallGoodsBaseInfo(mallGoodsDO);
// 将商品图片等资源存入数据库中
List<MallGoodsResourcesDO> mallGoodsResourcesList = mallGoodsVO.getResourcesList().stream().map(d -> {
MallGoodsResourcesDO mallGoodsResourcesDO = new MallGoodsResourcesDO(d);
mallGoodsResourcesDO.setMallGoodsId(id);
return mallGoodsResourcesDO;
}).collect(Collectors.toList());
mallGoodsDao.batchInsertMallGoodsResources(mallGoodsResourcesList);
// 获取输入的规格信息
List<GoodsSpecVO> goodsSpecList = mallGoodsVO.getGoodsSpecList();
for (GoodsSpecVO goodsSpecVO : goodsSpecList) {
goodsSpecVO.setMallGoodsId(id);
GoodsSpecDO goodsSpecDO = new GoodsSpecDO(goodsSpecVO);
// 插入规格信息
mallGoodsDao.insertGoodsSpec(goodsSpecDO);
List<GoodsSpecValuesDO> goodsSpecValuesList = goodsSpecVO.getGoodsSpecValuesList().stream().map(d -> {
d.setGoodsSpecId(goodsSpecDO.getId());
return new GoodsSpecValuesDO(d);
}).collect(Collectors.toList());
// 批量插入规格值的信息
mallGoodsDao.batchInsertSpecValues(goodsSpecValuesList);
}
return ResultBody.success();
}
private ResultBody checkInformation(MallGoodsVO mallGoodsVO, Integer userAccountId) {
// 查询该账账号下是否有相同的商品名称存在
if (mallGoodsDao.countMallGoodsByName(mallGoodsVO, userAccountId) > 0) {
return ResultBody.error(ResultEnum.GOODS_CATEGORY_NAME_EXIST_ERROR);
}
// 判断主图是否为空
List<GoodsResourcesVO> resourcesList = mallGoodsVO.getResourcesList()
.stream().filter(d -> d.getType().equals(0))
.collect(Collectors.toList());
if (resourcesList.size() == 0) {
return ResultBody.error(ResultEnum.GOODS_PIC_IS_NOT_NULL);
}
return null;
}
@Override
public ResultBody<MallGoodsVO> mallGoodsDetails(Long id) {
MallGoodsDO mallGoodsDO = mallGoodsDao.getMallGoodsBaseInfo(id);
if (mallGoodsDO == null) {
return ResultBody.error("商品不存在或已删除!");
}
MallGoodsVO mallGoodsVO = mallGoodsDO.buildMallGoodsVO();
// 获取图片及其他资源信息
List<MallGoodsResourcesDO> mallGoodsResourcesList = mallGoodsDao.getMallGoodsResources(id);
mallGoodsVO.setResourcesList(mallGoodsResourcesList.stream()
.map(MallGoodsResourcesDO::buildGoodsResourcesVO).collect(Collectors.toList()));
// 获取规格信息
List<GoodsSpecDO> goodsSpecList = mallGoodsDao.getMallGoodsSpec(id);
List<GoodsSpecVO> goodsSpec = goodsSpecList.stream().map(GoodsSpecDO::buildGoodsSpecVO)
.collect(Collectors.toList());
// 获取规格值信息
List<GoodsSpecValuesDO> goodsSpecValuesList = mallGoodsDao.getMallGoodsSpecValues(goodsSpec.stream().map(GoodsSpecVO::getId).collect(Collectors.toList()));
List<GoodsSpecValuesVO> goodsSpecValues = goodsSpecValuesList.stream().map(GoodsSpecValuesDO::buildGoodsSpecValuesVO).collect(Collectors.toList());
Map<Integer, List<GoodsSpecValuesVO>> goodsSpecValuesVO = goodsSpecValues.stream().collect(Collectors.groupingBy(GoodsSpecValuesVO::getGoodsSpecId));
for (GoodsSpecVO goodsSpecVO : goodsSpec) {
goodsSpecVO.setGoodsSpecValuesList(goodsSpecValuesVO.get(goodsSpecVO.getId()));
}
mallGoodsVO.setGoodsSpecList(goodsSpec);
return ResultBody.success(mallGoodsVO);
}
}
package com.mmc.pms.util;
/**
* @author 作者 geDuo
* @version 创建时间:2022年2月21日 下午3:55:10
* https://www.cnblogs.com/sgh1023/p/14094592.html
* @explain 雪花算法-分布式id
*/
public class SnowFlake {
/**
* 起始的时间戳
*/
private final static long START_STMP = 1480166465631L;
/**
* 每一部分占用的位数
*/
private final static long SEQUENCE_BIT = 12; // 序列号占用的位数
private final static long MACHINE_BIT = 5; // 机器标识占用的位数
private final static long DATACENTER_BIT = 5;// 数据中心占用的位数
/**
* 每一部分的最大值
*/
private final static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT);
private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);
/**
* 每一部分向左的位移
*/
private final static long MACHINE_LEFT = SEQUENCE_BIT;
private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;
private long datacenterId; // 数据中心
private long machineId; // 机器标识
private long sequence = 0L; // 序列号
private long lastStmp = -1L;// 上一次时间戳
public SnowFlake(long datacenterId, long machineId) {
if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) {
throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
}
if (machineId > MAX_MACHINE_NUM || machineId < 0) {
throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0");
}
this.datacenterId = datacenterId;
this.machineId = machineId;
}
/**
* 产生下一个ID
*
* @return
*/
public synchronized long nextId() {
long currStmp = getNewstmp();
if (currStmp < lastStmp) {
throw new RuntimeException("Clock moved backwards. Refusing to generate id");
}
if (currStmp == lastStmp) {
// 相同毫秒内,序列号自增
sequence = (sequence + 1) & MAX_SEQUENCE;
// 同一毫秒的序列数已经达到最大
if (sequence == 0L) {
currStmp = getNextMill();
}
} else {
// 不同毫秒内,序列号置为0
sequence = 0L;
}
lastStmp = currStmp;
return (currStmp - START_STMP) << TIMESTMP_LEFT // 时间戳部分
| datacenterId << DATACENTER_LEFT // 数据中心部分
| machineId << MACHINE_LEFT // 机器标识部分
| sequence; // 序列号部分
}
private long getNextMill() {
long mill = getNewstmp();
while (mill <= lastStmp) {
mill = getNewstmp();
}
return mill;
}
private long getNewstmp() {
return System.currentTimeMillis();
}
public static void main(String[] args) {
SnowFlake snowFlake = new SnowFlake(2, 3);
for (int i = 0; i < (1 << 12); i++) {
System.out.println(snowFlake.nextId());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mmc.pms.dao.mall.MallGoodsDao">
<insert id="insertMallGoodsBaseInfo">
insert into mall_goods(id,
goods_no,
trade_name,
description,
category_primary_id,
category_sub_id,
shelf_status,
goods_label,
label_show,
goods_details,
user_account_id,
sort)
values ( #{id}, #{goodsNo}, #{tradeName}, #{description}, #{categoryPrimaryId}, #{categorySubId}
, #{shelfStatus}, #{goodsLabel}, #{labelShow}, #{goodsDetails}, #{userAccountId}, #{sort})
</insert>
<insert id="insertGoodsSpec" parameterType="com.mmc.pms.entity.mall.GoodsSpecDO"
keyProperty="id" useGeneratedKeys="true">
insert into goods_spec (mall_goods_id,
spec_name,
choose_type,
must,
sku_unit_id)
values (#{mallGoodsId}, #{specName}, #{chooseType}, #{must}, #{skuUnitId})
</insert>
<insert id="batchInsertSpecValues" parameterType="list">
insert into goods_spec_values (goods_spec_id,
spec_value_name,
part_no,
spec_value_image,
show_price,
stock,
sale_price,
channel_price)
values
<foreach collection="list" item="item" separator=",">
(#{item.goodsSpecId},#{item.specValueName},#{item.partNo},#{item.specValueImage}
,#{item.showPrice},#{item.stock},#{item.salePrice},#{item.channelPrice})
</foreach>
</insert>
<insert id="batchInsertMallGoodsResources">
insert into mall_goods_resources (mall_goods_id,
url,
`type`
)
values
<foreach collection="list" item="item" separator=",">
(#{item.mallGoodsId},#{item.url},#{item.type})
</foreach>
</insert>
<select id="countMallGoodsByName" resultType="java.lang.Integer">
select count(*)
from mall_goods
where is_deleted = 0
and trade_name = #{mallGoodsVO.tradeName}
and user_account_id = #{userAccountId}
<if test="mallGoodsVO.id!=null">
and id <![CDATA[<>]]> #{mallGoodsVO.id}
</if>
</select>
<select id="countMallGoods" resultType="java.lang.Integer">
select count(*)
from mall_goods
</select>
<select id="getMallGoodsBaseInfo" resultType="com.mmc.pms.entity.mall.MallGoodsDO">
SELECT id,
goods_no,
trade_name,
description,
category_primary_id,
category_sub_id,
shelf_status,
goods_label,
label_show,
goods_details,
user_account_id,
sort
FROM mall_goods
WHERE id = #{id}
and is_deleted = 0
</select>
<select id="getMallGoodsResources" resultType="com.mmc.pms.entity.mall.MallGoodsResourcesDO">
SELECT id,
mall_goods_id,
url,
`type`
from mall_goods_resources
where mall_goods_id = #{id}
</select>
<select id="getMallGoodsSpec" resultType="com.mmc.pms.entity.mall.GoodsSpecDO">
select id,
mall_goods_id,
spec_name,
choose_type,
must,
sku_unit_id
from goods_spec
where mall_goods_id = #{id}
</select>
<select id="getMallGoodsSpecValues" resultType="com.mmc.pms.entity.mall.GoodsSpecValuesDO">
select id,
goods_spec_id,
spec_value_name,
part_no,
spec_value_image,
show_price,
stock,
sale_price,
channel_price
from goods_spec_values
<where>
<foreach collection="list" separator="," item="item" open="goods_spec_id in (" close=")">
#{item}
</foreach>
</where>
</select>
</mapper>
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论