package com.mzl.flower.service.flower; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.mzl.flower.config.exception.ValidationException; import com.mzl.flower.config.security.SecurityUtils; import com.mzl.flower.dto.request.flower.*; import com.mzl.flower.dto.response.flower.FlowerParamDTO; import com.mzl.flower.dto.response.flower.FlowerParamItemDTO; import com.mzl.flower.dto.response.flower.FlowerParamListDTO; import com.mzl.flower.dto.response.flower.ParamItemDTO; import com.mzl.flower.entity.flower.FlowerCategory; import com.mzl.flower.entity.flower.FlowerParam; import com.mzl.flower.entity.flower.FlowerParamItem; import com.mzl.flower.mapper.flower.FlowerCategoryMapper; import com.mzl.flower.mapper.flower.FlowerParamItemMapper; import com.mzl.flower.mapper.flower.FlowerParamMapper; import com.mzl.flower.service.BaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; @Slf4j @Service @Transactional public class FlowerParamService extends BaseService { @Autowired private FlowerParamMapper paramMapper; @Autowired private FlowerParamItemMapper paramItemMapper; @Autowired private FlowerCategoryMapper categoryMapper; private final String table_name_pre = "flower_param_"; private final String table_column_pre = "field_"; public Long addParam(FlowerParamCreateDTO dto){ FlowerParam g = new FlowerParam(); BeanUtils.copyProperties(dto, g); g.create(SecurityUtils.getUserId()); paramMapper.insert(g); createTable(getTableName(g.getId()), "商品参数表_" + g.getName()); updateCategoryParam(g.getId(), dto.getCategoryIds()); return g.getId(); } private void updateCategoryParam(Long id, List categoryIds){ categoryMapper.clearParamByParamId(id); if(categoryIds != null && categoryIds.size() > 0) { categoryMapper.setParamById(id, categoryIds); } } private void createTable(String tableName, String tableComment){ Map tableMap = new HashMap<>(); tableMap.put("name", tableName); tableMap.put("label", tableComment); List columns = new ArrayList<>(); ParamTableColumn c = new ParamTableColumn(); c.setColumn("id"); c.setType("bigint(25)"); c.setPrimaryKey(true); c.setComment("商品主键"); columns.add(c); tableMap.put("fields", columns); paramMapper.createTable(tableMap); } public Long updateParam(FlowerParamUpdateDTO dto){ FlowerParam g = paramMapper.selectById(dto.getId()); BeanUtils.copyProperties(dto, g); g.update(SecurityUtils.getUserId()); paramMapper.updateById(g); updateCategoryParam(g.getId(), dto.getCategoryIds()); return g.getId(); } public FlowerParamDTO getParam(Long id){ FlowerParam g = paramMapper.selectById(id); FlowerParamDTO dto = new FlowerParamDTO(); BeanUtils.copyProperties(g, dto); List ls = categoryMapper.selectList(new QueryWrapper().eq("param_id", id)); List categoryIds = new ArrayList<>(); List categoryLs = new ArrayList<>(); if(ls != null && ls.size() > 0){ for(FlowerCategory c : ls){ categoryIds.add(c.getId()); categoryLs.add(c.getName()); } } dto.setCategoryIds(categoryIds); dto.setCategories(String.join(",", categoryLs)); return dto; } public Page selectParamList(Page page, FlowerParamQueryDTO dto){ List ls = paramMapper.selectParamList(page, dto); page.setRecords(ls); return page; } public void deleteParam(Long id){ paramMapper.deleteById(id); categoryMapper.clearParamByParamId(id); paramItemMapper.delete(new QueryWrapper().eq("param_id", id)); } private String getTableName(Long id){ return table_name_pre + id; } private String getTableColumn(Long id){ return table_column_pre + id; } public List getParamItems(Long id){ List ls = paramItemMapper.selectList( new QueryWrapper().eq("param_id", id).orderByAsc("sort_by")); List result = new ArrayList<>(); if(ls != null && ls.size() > 0){ for(FlowerParamItem t : ls){ FlowerParamItemDTO dto = new FlowerParamItemDTO(); BeanUtils.copyProperties(t, dto); result.add(dto); } } return result; } public FlowerParamItemDTO getParamItem(Long id){ FlowerParamItem t = paramItemMapper.selectById(id); FlowerParamItemDTO dto = new FlowerParamItemDTO(); BeanUtils.copyProperties(t, dto); return dto; } public Long addParamItem(FlowerParamItemCreateDTO dto){ FlowerParamItem t = new FlowerParamItem(); BeanUtils.copyProperties(dto, t); t.create(SecurityUtils.getUserId()); paramItemMapper.insert(t); ParamTableColumn c = new ParamTableColumn(); c.setColumn(getTableColumn(t.getId())); c.setType("varchar(255)"); c.setPrimaryKey(false); c.setComment(t.getName()); paramItemMapper.addTableColumn(getTableName(t.getParamId()), c); return t.getId(); } public Long updateParamItem(FlowerParamItemUpdateDTO dto){ FlowerParamItem t = paramItemMapper.selectById(dto.getId()); BeanUtils.copyProperties(dto, t); t.update(SecurityUtils.getUserId()); paramItemMapper.updateById(t); return t.getId(); } public void deleteParamItem(Long id){ paramItemMapper.deleteById(id); } public List getParamItemListByCategoryId(Long categoryId){ FlowerCategory fc = categoryMapper.selectById(categoryId); if(fc == null){ throw new ValidationException("分类不存在"); } if(fc.getParamId() == null){ return new ArrayList<>(); } return getParamItemListByParamId(fc.getParamId()); } public List getParamItemListByParamId(Long paramId){ List result = new ArrayList<>(); List ls = paramItemMapper.selectList( new QueryWrapper().eq("param_id", paramId).orderByAsc("sort_by")); if(ls != null && ls.size() > 0){ for(FlowerParamItem t : ls){ ParamItemDTO d = new ParamItemDTO(); d.setId(t.getId()); d.setColumn(getTableColumn(t.getId())); d.setName(t.getName()); d.setValues(splitParam(t.getContent())); result.add(d); } } return result; } public void insertTableData(Long paramId, Long id, List columns){ List columnList = new ArrayList<>(); List valueList = new ArrayList<>(); columnList.add("id"); valueList.add(id); for (ParamDTO f : columns){ Long fId = f.getId(); columnList.add(getTableColumn(fId)); valueList.add(f.getValue()); } String tableName = getTableName(paramId); paramMapper.insertTableData(tableName, columnList, valueList); } public void updateTableData(Long paramId, Long id, List columns){ List valueMapList = new ArrayList<>(); for (ParamDTO f : columns){ Long fId = f.getId(); Map map = new HashMap<>(); map.put("column", getTableColumn(fId)); map.put("value", f.getValue()); valueMapList.add(map); } String tableName = getTableName(paramId); paramMapper.updateTableDataById(tableName, valueMapList, "id", id); } public Map getTableDataById(Long paramId, Long id){ String tableName = getTableName(paramId); return paramMapper.getTableDataById(tableName, id); } public Long getParamIdByCategoryId(Long categoryId){ if(categoryId == null){ return null; } FlowerCategory c = categoryMapper.selectById(categoryId); return c.getParamId(); } public FlowerParamItem selectFlowerParamItemById(Long id) { return paramItemMapper.selectFlowerParamItemById(id); } public FlowerParam selectFlowerParamById(Long id) { return paramMapper.selectFlowerParamById(id); } }