规格前台回显,单规格查询

day-06
Saisai Liu 2024-03-10 11:13:34 +08:00
parent 0d68b51739
commit cbfe72810e
9 changed files with 71 additions and 29 deletions

View File

@ -42,7 +42,7 @@ public class Rule extends BaseEntity
public static Rule saveBuilder(RuleAttrReq req, Supplier<String> username){
return Rule.builder()
.name(req.getName())
.states(req.getStates())
.states(req.getStates()==null? null: req.getStates())
.createBy(username.get())
.createTime(new Date())
.build();

View File

@ -42,6 +42,8 @@ public class RuleAttr extends BaseEntity {
*/
@Excel(name = "规格属性")
private String attrName;
private String attrValue;

View File

@ -9,8 +9,7 @@ import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.util.Arrays;
import java.util.List;
import java.util.*;
/**
* @ClassName RuleAttrAddModel
@ -27,10 +26,20 @@ public class RuleAttrAddModel extends BaseEntity {
private String attrName;
private List<String> valueList;
public static RuleAttrAddModel saveBuilder(RuleAttr ruleAttr) {
return RuleAttrAddModel.builder()
.attrName(ruleAttr.getAttrName())
.valueList(Arrays.stream(ruleAttr.getAttrValue().split(",")).toList())
.build();
public static List<RuleAttrAddModel> saveBuilder(List<RuleAttr> rules) {
HashMap<String, List<String>> stringListHashMap = new HashMap<>();
for (RuleAttr rule : rules) {
if (stringListHashMap.containsKey(rule.getAttrName())){
stringListHashMap.get(rule.getAttrName()).add(rule.getAttrValue());
}else {
ArrayList<String> strings = new ArrayList<>();
strings.add(rule.getAttrValue());
stringListHashMap.put(rule.getAttrName(),strings);
}
}
return stringListHashMap.keySet().stream()
.map(key -> new RuleAttrAddModel
(key, stringListHashMap.get(key)))
.toList();
}
}

View File

@ -0,0 +1,26 @@
package com.muyu.product.domain.model;
import com.muyu.common.core.web.domain.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* @ClassName RuleAttrFindId
* @Description
* @Author SaiSai.Liu
* @Date 2024/3/9/0009 16:46
*/
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class RuleAttrFindId extends BaseEntity {
private Long id;
private String ruleName;
private String ruleValue;
}

View File

@ -2,6 +2,7 @@ package com.muyu.product.domain.resp;
import com.muyu.common.core.web.domain.BaseEntity;
import com.muyu.product.domain.Rule;
import com.muyu.product.domain.RuleAttr;
import com.muyu.product.domain.model.RuleAttrAddModel;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -25,13 +26,15 @@ public class RuleAttrResp extends BaseEntity {
private Long id;
private String name;
private String states;
private List<RuleAttrAddModel> ruleAttrAddModels;
private List<RuleAttrAddModel> ruleAttrList;
public static RuleAttrResp saveBuilder(Rule rule, Function<Long,List<RuleAttrAddModel>> ruleAttrs){
return RuleAttrResp.builder()
.id(rule.getId())
.name(rule.getName())
.ruleAttrAddModels(ruleAttrs.apply(rule.getId()))
.ruleAttrList(ruleAttrs==null? null : ruleAttrs.apply(rule.getId()))
.states(rule.getStates())
.build();
}

View File

@ -89,6 +89,7 @@ public class RuleController extends BaseController
@GetMapping(value = "/{id}")
public Result getInfo(@PathVariable("id") Long id)
{
return success(ruleService.selectRuleById(id));
}

View File

@ -22,7 +22,7 @@ public interface IRuleService extends IService<Rule>
* @param id
* @return
*/
Rule selectRuleById(Long id);
RuleAttrResp selectRuleById(Long id);
/**
*

View File

@ -18,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
/**
* Service
@ -43,8 +44,17 @@ public class RuleServiceImpl extends ServiceImpl<RuleMapper, Rule> implements IR
* @return
*/
@Override
public Rule selectRuleById(Long id) {
return ruleMapper.selectRuleById(id);
public RuleAttrResp selectRuleById(Long id) {
Rule byId = this.getById(id);
return RuleAttrResp.saveBuilder(byId, new Function<Long, List<RuleAttrAddModel>>() {
@Override
public List<RuleAttrAddModel> apply(Long id) {
LambdaQueryWrapper<RuleAttr> wrapper = new LambdaQueryWrapper<>();
List<RuleAttr> list = ruleAttrService.list(wrapper.eq(RuleAttr::getRuleId, id));
return RuleAttrAddModel.saveBuilder(list);
}
});
// return RuleAttrResp.saveBuilder(ruleMapper.selectRuleById(id), null);
}
/**
@ -121,23 +131,14 @@ public class RuleServiceImpl extends ServiceImpl<RuleMapper, Rule> implements IR
@Override
public List<RuleAttrResp> selectRuleAttrList(RuleAttrReq ruleAttrReq) {
//规格集合
List<Rule> rules = this.selectRuleList(Rule.saveBuilder(ruleAttrReq, SecurityUtils::getUsername));
//规格主键集合
List<Long> ruleIdList = rules.stream().map(Rule::getId).toList();
//创建响应值容器
List<RuleAttrResp> ruleAttrResps = new ArrayList<>();
//根据编号转换响应对象
ruleAttrResps = rules.stream().map(rule ->
return rules.stream().map(rule ->
RuleAttrResp.saveBuilder(rule, id -> {
LambdaQueryWrapper<RuleAttr> wrapper = new LambdaQueryWrapper<>();
List<RuleAttr> list = ruleAttrService.list(wrapper.eq(RuleAttr::getRuleId, rule.getId()));
return list.stream().map(RuleAttrAddModel::saveBuilder).toList();
return RuleAttrAddModel.saveBuilder(list);
})
).toList();
return ruleAttrResps;
}
}

View File

@ -16,7 +16,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</resultMap>
<sql id="selectRuleVo">
select id, name, remark, create_by, create_time, update_by, update_time from rule
select id, name,states, remark, create_by, create_time, update_by, update_time from rule
</sql>
<select id="selectRuleList" parameterType="com.muyu.product.domain.Rule" resultMap="RuleResult">