diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/CommonController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/CommonController.java index 79ca76c..1c0f114 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/CommonController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/CommonController.java @@ -1,5 +1,6 @@ package com.chaozhanggui.system.cashierservice.controller; +import cn.hutool.core.util.StrUtil; import com.chaozhanggui.system.cashierservice.dao.TbPlatformDictMapper; import com.chaozhanggui.system.cashierservice.entity.TbPlatformDict; import com.chaozhanggui.system.cashierservice.entity.dto.WxMsgSubDTO; @@ -221,7 +222,7 @@ public class CommonController { } String msg = wxMsgSubDTO.getShopId().replace("msg", ""); String[] split = msg.split(","); - loginService.addShopId(wxMsgSubDTO.getOpenId(), split[0], split.length > 1 ? Integer.valueOf(split[1]) : null); + loginService.addShopId(wxMsgSubDTO.getOpenId(), split[0], split.length > 1 ? Integer.valueOf(split[1]) : null, wxMsgSubDTO.getNickname(), wxMsgSubDTO.getAvatar()); return Result.success(CodeEnum.SUCCESS); } } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopOpenIdMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopOpenIdMapper.java index d18bdf1..8d875e1 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopOpenIdMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopOpenIdMapper.java @@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.dao; import com.chaozhanggui.system.cashierservice.entity.TbShopOpenId; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; import java.util.List; @@ -30,4 +31,11 @@ public interface TbShopOpenIdMapper { @Select("select * from tb_shop_open_id where shop_id=#{shopId} and status=1") List selectByShopId(@Param("shopId") Integer shopId); + + + @Select("select * from tb_shop_open_id where open_id=#{openId} and status=1") + List selectByOpenId(@Param("openId") String openId); + + @Update("update tb_shop_open_id set nickname=#{nickName}, avatar=#{avatar} where open_id=#{openId} and status=1 ") + int updateBaseInfoByOpenId(@Param("openId") String openId, @Param("nickName") String nickName, @Param("avatar") String avatar); } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbShopOpenId.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbShopOpenId.java index 1afc92f..26adc94 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbShopOpenId.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbShopOpenId.java @@ -3,15 +3,17 @@ package com.chaozhanggui.system.cashierservice.entity; import java.io.Serializable; import java.util.Date; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 商家openid信息表 * @TableName tb_shop_open_id */ +@EqualsAndHashCode @Data public class TbShopOpenId implements Serializable { /** - * + * */ private Integer id; @@ -26,69 +28,24 @@ public class TbShopOpenId implements Serializable { private String openId; /** - * + * */ private Integer status; /** - * + * */ private Date createTime; /** - * + * */ private Date updateTime; + private String nickname; + private String avatar; + private Integer type; + private static final long serialVersionUID = 1L; - @Override - public boolean equals(Object that) { - if (this == that) { - return true; - } - if (that == null) { - return false; - } - if (getClass() != that.getClass()) { - return false; - } - TbShopOpenId other = (TbShopOpenId) that; - return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) - && (this.getShopId() == null ? other.getShopId() == null : this.getShopId().equals(other.getShopId())) - && (this.getOpenId() == null ? other.getOpenId() == null : this.getOpenId().equals(other.getOpenId())) - && (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus())) - && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) - && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime())); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); - result = prime * result + ((getShopId() == null) ? 0 : getShopId().hashCode()); - result = prime * result + ((getOpenId() == null) ? 0 : getOpenId().hashCode()); - result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode()); - result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); - result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode()); - return result; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName()); - sb.append(" ["); - sb.append("Hash = ").append(hashCode()); - sb.append(", id=").append(id); - sb.append(", shopId=").append(shopId); - sb.append(", openId=").append(openId); - sb.append(", status=").append(status); - sb.append(", createTime=").append(createTime); - sb.append(", updateTime=").append(updateTime); - sb.append(", serialVersionUID=").append(serialVersionUID); - sb.append("]"); - return sb.toString(); - } -} \ No newline at end of file +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/WxMsgSubDTO.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/WxMsgSubDTO.java index 63ef0f2..ecd97f4 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/WxMsgSubDTO.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/WxMsgSubDTO.java @@ -7,4 +7,6 @@ public class WxMsgSubDTO { private String shopId; private String openId; private Integer type; + private String nickname; + private String avatar; } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java index 80e9033..ada1443 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java @@ -3,9 +3,11 @@ package com.chaozhanggui.system.cashierservice.service; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.RandomUtil; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.chaozhanggui.system.cashierservice.dao.*; import com.chaozhanggui.system.cashierservice.entity.*; +import com.chaozhanggui.system.cashierservice.exception.MsgException; import com.chaozhanggui.system.cashierservice.redis.RedisCst; import com.chaozhanggui.system.cashierservice.redis.RedisUtil; import com.chaozhanggui.system.cashierservice.sign.CodeEnum; @@ -62,37 +64,33 @@ public class LoginService { this.shopOpenIdMapper = shopOpenIdMapper; } - public void addShopId(String openId, String shopId, Integer type) { - TbUserShopMsg shopMsg= tbUserShopMsgMapper.selectByShopIdAndOpenId(Integer.valueOf(shopId),openId); - if(Objects.isNull(shopMsg)){ - shopMsg=new TbUserShopMsg(); - shopMsg.setShopId(Integer.valueOf(shopId)); - shopMsg.setOpenId(openId); - shopMsg.setCreateTime(new Date()); - shopMsg.setStatus("1"); - tbUserShopMsgMapper.insert(shopMsg); - } - + public void addShopId(String openId, String shopId, Integer type, String nickName, String avatar) { // 为商家绑定openid TbShopOpenId tbShopOpenId = shopOpenIdMapper.countByOpenId(openId, Integer.valueOf(shopId), type); - if ( tbShopOpenId == null) { + if (tbShopOpenId == null) { TbShopOpenId shopOpenId = new TbShopOpenId(); shopOpenId.setOpenId(openId); shopOpenId.setCreateTime(DateUtil.date()); shopOpenId.setShopId(Integer.valueOf(shopId)); shopOpenId.setStatus(1); + shopOpenId.setNickname(nickName); + shopOpenId.setAvatar(avatar); + shopOpenId.setType(type == null ? -1 : type); shopOpenIdMapper.insert(shopOpenId); - }else { + } else { tbShopOpenId.setUpdateTime(DateUtil.date()); + tbShopOpenId.setNickname(nickName); + tbShopOpenId.setAvatar(avatar); shopOpenIdMapper.updateByPrimaryKeySelective(tbShopOpenId); } + } - public Result wxBusinessLogin(String openId,String shopId){ - TbUserShopMsg shopMsg= tbUserShopMsgMapper.selectByShopIdAndOpenId(Integer.valueOf(shopId),openId); - if(Objects.isNull(shopMsg)){ - shopMsg=new TbUserShopMsg(); + public Result wxBusinessLogin(String openId, String shopId) { + TbUserShopMsg shopMsg = tbUserShopMsgMapper.selectByShopIdAndOpenId(Integer.valueOf(shopId), openId); + if (Objects.isNull(shopMsg)) { + shopMsg = new TbUserShopMsg(); shopMsg.setShopId(Integer.valueOf(shopId)); shopMsg.setOpenId(openId); shopMsg.setCreateTime(new Date()); @@ -109,14 +107,14 @@ public class LoginService { shopOpenId.setStatus(1); // shopOpenIdMapper.insert(shopOpenId); } - return Result.success(CodeEnum.SUCCESS,shopMsg); + return Result.success(CodeEnum.SUCCESS, shopMsg); } - public Result wxCustomLogin(String openId,String headImage,String nickName,String telephone,String ip) { - TbUserInfo userInfo= tbUserInfoMapper.selectByOpenId(openId); - if(ObjectUtil.isNull(userInfo)){ - userInfo=new TbUserInfo(); + public Result wxCustomLogin(String openId, String headImage, String nickName, String telephone, String ip) { + TbUserInfo userInfo = tbUserInfoMapper.selectByOpenId(openId); + if (ObjectUtil.isNull(userInfo)) { + userInfo = new TbUserInfo(); userInfo.setAmount(BigDecimal.ZERO); userInfo.setChargeAmount(BigDecimal.ZERO); userInfo.setLineOfCredit(BigDecimal.ZERO); @@ -124,9 +122,9 @@ public class LoginService { userInfo.setConsumeAmount(BigDecimal.ZERO); userInfo.setTotalScore(0); userInfo.setLockScore(0); - userInfo.setHeadImg(ObjectUtil.isNotNull(headImage)?headImage:""); - userInfo.setNickName(ObjectUtil.isNotNull(nickName)?nickName:"微信用户"); - userInfo.setTelephone(ObjectUtil.isNotNull(telephone)?telephone:""); + userInfo.setHeadImg(ObjectUtil.isNotNull(headImage) ? headImage : ""); + userInfo.setNickName(ObjectUtil.isNotNull(nickName) ? nickName : "微信用户"); + userInfo.setTelephone(ObjectUtil.isNotNull(telephone) ? telephone : ""); userInfo.setMiniAppOpenId(openId); userInfo.setStatus(Byte.parseByte("1")); userInfo.setParentType("PERSON"); @@ -142,7 +140,7 @@ public class LoginService { userInfo.setLastLogInAt(System.currentTimeMillis()); userInfo.setUpdatedAt(System.currentTimeMillis()); tbUserInfoMapper.insert(userInfo); - }else { + } else { userInfo.setUpdatedAt(System.currentTimeMillis()); userInfo.setLastLogInAt(System.currentTimeMillis()); tbUserInfoMapper.updateByPrimaryKeySelective(userInfo); @@ -283,7 +281,7 @@ public class LoginService { * @param nickName * @return */ - public TbUserInfo register(String phone, String openId,String password, String nickName) { + public TbUserInfo register(String phone, String openId, String password, String nickName) { TbUserInfo userInfo = new TbUserInfo(); userInfo.setAmount(BigDecimal.ZERO); @@ -313,7 +311,7 @@ public class LoginService { if (StringUtils.isNotBlank(password)) { userInfo.setPassword(MD5Utils.MD5Encode(password, "UTF-8")); } - List tbShopUsers = tbShopUserMapper.selectByPhone(phone); + List tbShopUsers = tbShopUserMapper.selectByPhone(phone); for (TbShopUser tbShopUser : tbShopUsers) { tbShopUser.setUserId(userInfo.getId() + ""); tbShopUserMapper.updateByPrimaryKey(tbShopUser); @@ -342,13 +340,13 @@ public class LoginService { } @Transactional(rollbackFor = Exception.class) - public Result appLogin(String username,String openId, String password) { + public Result appLogin(String username, String openId, String password) { TbUserInfo userInfo = tbUserInfoMapper.selectByPhone(username); if (ObjectUtil.isNull(userInfo)) { if (StringUtils.isNotBlank(password)) { return Result.fail("暂未设置密码,请使用验证码登录"); } - userInfo = register(username,openId, password, username); + userInfo = register(username, openId, password, username); } else { if (StringUtils.isNotBlank(userInfo.getMiniAppOpenId())) { if (!userInfo.getMiniAppOpenId().equals(openId)) { @@ -423,7 +421,7 @@ public class LoginService { tbShopUserMapper.updateByPrimaryKeySelective(tbShopUser); - return Result.success(CodeEnum.SUCCESS, "46"+dynamicCode); + return Result.success(CodeEnum.SUCCESS, "46" + dynamicCode); } @@ -439,14 +437,14 @@ public class LoginService { public Result upPhone(String openId, String phone, String shopId) { TbUserInfo userInfo = tbUserInfoMapper.selectByOpenId(openId); // 同一个手机号 同一个店 userId 不为空 - TbShopUser tbShopUserPC = tbShopUserMapper.selectPCByPhoneAndShopId(userInfo.getTelephone(),shopId); + TbShopUser tbShopUserPC = tbShopUserMapper.selectPCByPhoneAndShopId(userInfo.getTelephone(), shopId); if (tbShopUserPC != null) { - TbShopUser tbShopUserSM = tbShopUserMapper.selectByUserIdAndShopId(userInfo.getId().toString(),shopId); + TbShopUser tbShopUserSM = tbShopUserMapper.selectByUserIdAndShopId(userInfo.getId().toString(), shopId); tbShopUserMapper.deleteByPrimaryKey(tbShopUserSM.getId()); tbShopUserPC.setUserId(userInfo.getId().toString()); tbShopUserMapper.updateByPrimaryKey(tbShopUserPC); - }else { - TbShopUser tbShopUserSM = tbShopUserMapper.selectByUserIdAndShopId(userInfo.getId().toString(),shopId); + } else { + TbShopUser tbShopUserSM = tbShopUserMapper.selectByUserIdAndShopId(userInfo.getId().toString(), shopId); if (tbShopUserSM != null) { tbShopUserSM.setIsVip(Byte.parseByte("1")); tbShopUserSM.setTelephone(phone); @@ -460,7 +458,7 @@ public class LoginService { return Result.success(CodeEnum.SUCCESS); } - public void registerShopUser(TbUserInfo userInfo,String shopId) { + public void registerShopUser(TbUserInfo userInfo, String shopId) { TbShopUser shopUser = new TbShopUser(); shopUser.setName(userInfo.getNickName()); shopUser.setSex(userInfo.getSex()); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java index 6f4c7ab..4cf1087 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java @@ -162,7 +162,7 @@ public class ProductService { return Result.success(CodeEnum.SUCCESS, concurrentMap); } - public JSONArray querySpec(QuerySpecDTO querySpecDTO) { + public Object querySpec(QuerySpecDTO querySpecDTO) { TbProduct tbProduct = tbProductMapper.selectById(querySpecDTO.getProductId()); if (tbProduct == null) { throw new MsgException("商品不存在"); @@ -171,71 +171,128 @@ public class ProductService { // 重组有效规格数据 String tagSnap = skuResult != null ? skuResult.getTagSnap() : null; + List tbProductSkus = tbProductSkuMapper.selectGroundingByProId(querySpecDTO.getProductId()); + JSONArray finalSnap = new JSONArray(); +// if (tagSnap != null) { +// JSONArray tagSnaps = JSONObject.parseArray(tagSnap); +// HashMap reverseTagSnapMap = new HashMap<>(); +// JSONObject snapJSON; +// for (Object snap : tagSnaps) { +// snapJSON = (JSONObject)snap; +// reverseTagSnapMap.put(snapJSON.getString("value"), snapJSON.getString("name")); +// } +// // 上下架对应的sku +// HashSet specSet = new HashSet<>(); +// for (TbProductSku item : tbProductSkus) { +// String specSnap = item.getSpecSnap(); +// +// ArrayList skuSpecList = new ArrayList<>(); +// if (specSnap != null) { +// String[] split = specSnap.split(","); +// for (String snap : split) { +// reverseTagSnapMap.forEach((k, v) -> { +// if (k.contains(snap)) { +// skuSpecList.add(new QuerySpecDTO.QuerySpec(v, snap)); +// } +// }); +// } +// +// boolean isHave = false; +// for (QuerySpecDTO.QuerySpec spec : querySpecDTO.getQuerySpecList()) { +// for (QuerySpecDTO.QuerySpec querySpec : skuSpecList) { +// isHave = querySpec.equals(spec); +// if (isHave) break; +// } +// +// } +// if (querySpecDTO.getQuerySpecList().isEmpty() || isHave) { +// specSet.addAll(Arrays.asList(split)); +// } +// } +// } +// +// +// for (Object snap : tagSnaps) { +// snapJSON = (JSONObject) snap; +// String values = snapJSON.getString("value"); +// +// ArrayList> data = new ArrayList<>(); +// if (StrUtil.isNotBlank(values)) { +// String[] valueList = values.split(","); +// for (String value : valueList) { +// LinkedHashMap finalValue = new LinkedHashMap<>(); +// finalValue.put("info", value); +// finalValue.put("isGrounding", specSet.contains(value)); +// data.add(finalValue); +// } +// if (!data.isEmpty()) { +// snapJSON.put("value", data); +// finalSnap.add(snapJSON); +// } +// +// } +// } +// } + List result = new ArrayList<>(); if (tagSnap != null) { JSONArray tagSnaps = JSONObject.parseArray(tagSnap); - HashMap reverseTagSnapMap = new HashMap<>(); - JSONObject snapJSON; - for (Object snap : tagSnaps) { - snapJSON = (JSONObject)snap; - reverseTagSnapMap.put(snapJSON.getString("value"), snapJSON.getString("name")); + List> valuesList = new ArrayList<>(); + + // 提取所有 value 的列表 + for (int i = 0; i < tagSnaps.size(); i++) { + JSONObject jsonObject = tagSnaps.getJSONObject(i); + String[] values = jsonObject.getString("value").split(","); + valuesList.add(Arrays.asList(values)); } - List tbProductSkus = tbProductSkuMapper.selectGroundingByProId(querySpecDTO.getProductId()); - // 上下架对应的sku - HashSet specSet = new HashSet<>(); - for (TbProductSku item : tbProductSkus) { - String specSnap = item.getSpecSnap(); + // 生成所有可能的排列组合 + generateCombinations(valuesList, 0, new ArrayList<>(), result); - ArrayList skuSpecList = new ArrayList<>(); - if (specSnap != null) { - String[] split = specSnap.split(","); - for (String snap : split) { - reverseTagSnapMap.forEach((k, v) -> { - if (k.contains(snap)) { - skuSpecList.add(new QuerySpecDTO.QuerySpec(v, snap)); - } - }); - } - boolean isHave = false; - for (QuerySpecDTO.QuerySpec spec : querySpecDTO.getQuerySpecList()) { - for (QuerySpecDTO.QuerySpec querySpec : skuSpecList) { - isHave = querySpec.equals(spec); - if (isHave) break; - } - - } - - if (querySpecDTO.getQuerySpecList().isEmpty() || isHave) { - specSet.addAll(Arrays.asList(split)); - } - } - } - - for (Object snap : tagSnaps) { - snapJSON = (JSONObject) snap; - String values = snapJSON.getString("value"); - - ArrayList> data = new ArrayList<>(); - if (StrUtil.isNotBlank(values)) { - String[] valueList = values.split(","); - for (String value : valueList) { - LinkedHashMap finalValue = new LinkedHashMap<>(); - finalValue.put("info", value); - finalValue.put("isGrounding", specSet.contains(value)); - data.add(finalValue); - } - if (!data.isEmpty()) { - snapJSON.put("value", data); - finalSnap.add(snapJSON); - } - - } - } } - return finalSnap; + ArrayList> specList = new ArrayList<>(); + tbProductSkus.forEach(item -> { + HashMap itemMap = new HashMap<>(); + itemMap.put("specSnap", item.getSpecSnap()); + itemMap.put("skuId", item.getId()); + specList.add(itemMap); + }); + + + ArrayList> otherVal = new ArrayList<>(); + for (String res : result) { + boolean found = false; + for (HashMap spec : specList) { + if (res.equals(spec.get("specSnap").toString())) { + spec.put("isGrounding", true); + found = true; + break; + } + } + if (!found) { + HashMap itemMap = new HashMap<>(); + itemMap.put("specSnap", res); + itemMap.put("skuId", null); + itemMap.put("isGrounding", false); + otherVal.add(itemMap); + } + } + specList.addAll(otherVal); + return specList; } + private static void generateCombinations(List> valuesList, int index, List current, List result) { + if (index == valuesList.size()) { + result.add(String.join(",", current)); + return; + } + + for (String value : valuesList.get(index)) { + current.add(value); + generateCombinations(valuesList, index + 1, current, result); + current.remove(current.size() - 1); // 回溯 + } + } public List handleDate(List products){ if (!CollectionUtils.isEmpty(products)) { diff --git a/src/main/resources/mapper/TbShopOpenIdMapper.xml b/src/main/resources/mapper/TbShopOpenIdMapper.xml index db80e80..64bcebb 100644 --- a/src/main/resources/mapper/TbShopOpenIdMapper.xml +++ b/src/main/resources/mapper/TbShopOpenIdMapper.xml @@ -41,10 +41,10 @@ insert into tb_shop_open_id ( id,shop_id,open_id - ,status,create_time,update_time + ,status,create_time,update_time, type, nickname, avatar ) values (#{id,jdbcType=INTEGER},#{shopId,jdbcType=INTEGER},#{openId,jdbcType=VARCHAR} - ,#{status,jdbcType=TINYINT},#{createTime,jdbcType=TIMESTAMP},#{updateTime,jdbcType=TIMESTAMP} + ,#{status,jdbcType=TINYINT},#{createTime,jdbcType=TIMESTAMP},#{updateTime,jdbcType=TIMESTAMP},#{type,jdbcType=INTEGER}, #{nickname,jdbcType=VARCHAR}, #{avatar,jdbcType=VARCHAR} ) @@ -84,6 +84,15 @@ update_time = #{updateTime,jdbcType=TIMESTAMP}, + + `type` = #{type,jdbcType=INTEGER}, + + + `nickname` = #{nickname,jdbcType=INTEGER}, + + + `avatar` = #{avatar,jdbcType=INTEGER}, + where id = #{id,jdbcType=INTEGER}