xuekang
2024-05-13 15a0280ae9e7db96fdf0744c722d214d2cb5a0e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.nuvole.four.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.nuvole.base.domain.SysUser;
import com.nuvole.four.contants.Contants;
import com.nuvole.four.domain.StoreIndustryManage;
import com.nuvole.four.domain.query.StoreIndustryManageQuery;
import com.nuvole.four.mapper.StoreIndustryManageMapper;
import com.nuvole.four.service.StoreIndustryManageService;
import com.nuvole.four.util.SystemUtil;
import com.nuvole.util.IdGenerator;
import com.nuvole.util.TreeUtil;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * @Description 行业管理Service实现类
 * @Author dqh
 * @Date 2024-04-12 17:41:29
 */
@Service
@RequiredArgsConstructor
public class StoreIndustryManageServiceImpl implements StoreIndustryManageService {
 
    private final StoreIndustryManageMapper storeIndustryManageMapper;
 
 
 
    /**
     * 方法描述: 添加行业管理
     *
     * @date  2024-04-12 18:29
     **/
    @Override
    public int addSelective(StoreIndustryManage entity){
        entity.setId(IdGenerator.getId());
        if(entity.getPId() == null){
            entity.setPId(0L);
        }
        entity.setLevel(getLevel(entity.getPId()));
        //生成code
        String code = generateCode(entity);
        entity.setIndustryCode(code);
        entity.setIndustryCode(Contants.CODE + entity.getLevel() + Contants.CODE + entity.getLevel());
        SysUser user = SystemUtil.getLoginUser(Contants.LOGIN_TYPE_PC);
        entity.setCreateBy(user.getId());
        entity.setCreateTime(new Date());
        return storeIndustryManageMapper.insertSelective(entity);
    }
 
    /**
     * 方法描述: 生成本级code
     *
     * @date  2024-04-13 21:57
     **/
    private String generateCode(StoreIndustryManage entity){
        StoreIndustryManage pManage = storeIndustryManageMapper.selectByPrimaryKey(entity.getPId());
        String parentCode = "";//父级code
        if (pManage != null) {
            parentCode = pManage.getIndustryCode();
            parentCode = parentCode.substring(pManage.getIndustryCode().length());
        }
        String maxCode = storeIndustryManageMapper.getMaxCode(entity.getPId(),parentCode);   //本级最大code
        if (StrUtil.isNotBlank(maxCode)){
            maxCode = maxCode.substring(entity.getIndustryCode().length());
        }
        return maxCode;
    }
 
    /**
     * 方法描述:获取当前pId对应的level层级
     *
     **/
    private Integer getLevel(Long pId){
        Integer level = null;
        if(pId == null || pId == 0){
            level = 0;
        } else {
            //查询当前行业的level;根据pId的level + 1得出
            level = storeIndustryManageMapper.getLevel(pId);
        }
        return level;
    }
 
    /**
     * 方法描述:修改行业管理
     *
     * @date  2024-04-12 18:33
     **/
    @Override
    public int editSelective(StoreIndustryManage entity){
        if(entity.getPId() == null){
            entity.setPId(0L);
        }
        entity.setLevel(getLevel(entity.getPId()));
        SysUser user = SystemUtil.getLoginUser(Contants.LOGIN_TYPE_PC);
        entity.setUpdateBy(user.getId());
        entity.setUpdateTime(new Date());
        return storeIndustryManageMapper.updateByPrimaryKeySelective(entity);
    }
     @Override
     public StoreIndustryManage get(Long  id){
        return storeIndustryManageMapper.selectByPrimaryKey(id);
     }
     @Override
     public int del(Long  id){
        //校验是否存在子节点;存在则不允许删除
         int exists = storeIndustryManageMapper.selectByPid(id);
         if(exists > 0){
             throw new IllegalArgumentException("存在下级,不允许删除");
         }
        return storeIndustryManageMapper.deleteByPrimaryKey(id);
     }
 
 
     /**
      * 方法描述:查询行业管理列表
      *
      * @date  2024-04-12 18:52
      **/
    @Override
    public List<StoreIndustryManage> getList(StoreIndustryManageQuery query) {
        return storeIndustryManageMapper.selectList(query);
    }
 
    /**
     * 方法描述:查询列表默认方法
     *
     * @date  2024-04-12 18:52
     **/
    @Override
    public List<StoreIndustryManage> getList(Map map) {
        return storeIndustryManageMapper.selectAll(map);
    }
 
    /**
     * 方法描述: 查询max(sortNo)的序号信息
     * 返回 = max(sortNo) + 10
     *
     * @date  2024-04-12 18:17
     **/
    @Override
    public Integer getSort() {
        return storeIndustryManageMapper.getSort();
    }
 
    /**
     * 方法描述: 查询所有行业树
     *
     * @date  2024-04-12 18:47
     **/
    @Override
    public List<Map> getIndustryTree(Map map) {
        PageHelper.orderBy("p_id,sort_no");
        List<StoreIndustryManage> list = this.getList(map);
        return TreeUtil.convert2Tree(list);
    }
}