935090232@qq.com
2021-11-21 59634aeabb04aae0e819bd4c5fe909bb9cdbeb28
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package com.matrix.system.common.service.impl;
 
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.system.app.authority.AppAuthorityManager;
import com.matrix.system.common.authority.DefaultAuthorityManager;
import com.matrix.system.common.bean.SysCompany;
import com.matrix.system.common.bean.SysRole;
import com.matrix.system.common.bean.SysRolePwoerFn;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.dao.SysCompanyDao;
import com.matrix.system.common.dao.SysRoleDao;
import com.matrix.system.common.dao.SysRolePwoerFnDao;
import com.matrix.system.common.init.LocalCache;
import com.matrix.system.common.service.SysCompanyService;
import com.matrix.system.common.service.SysUsersService;
import com.matrix.system.hive.bean.SysShopInfo;
import com.matrix.system.hive.bean.Warehouse;
import com.matrix.system.hive.dao.SysShopInfoDao;
import com.matrix.system.hive.dao.WarehouseDao;
import com.matrix.system.hive.plugin.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 公司管理
 *
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date Dec 11, 2017
 */
@Service
public class SysCompanyServiceImpl implements SysCompanyService {
 
    @Autowired
    private SysCompanyDao sysCompanyDao;
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRolePwoerFnDao rolePwoerFnDao;
 
    @Autowired
    private SysShopInfoDao shopInfoDao;
 
 
    @Autowired
    private InitCustomerDataDictionaryService initCustomerDataDictionaryService;
 
 
    @Autowired
    private InitShoppingGoodsCategoryService initShoppingGoodsCategoryService;
 
 
    @Autowired
    private InitRolePowerService initRolePowerService;
 
    @Autowired
    private InitGoodsTypeService initGoodsTypeService;
 
    @Autowired
    private InitShopProductCateService initShopProductCateService;
 
    @Autowired
    private InitBusParameterSettingService initBusParameterSettingService;
 
 
    @Transactional
    @Override
    public int add(SysCompany sysCompany) {
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        sysCompany.setCreateBy(user.getSuName());
        sysCompany.setUpdateBy(user.getSuName());
        int i = sysCompanyDao.insert(sysCompany);
        SysShopInfo zbShop = addZbShop(sysCompany);
        initCustomerDataDictionaryService.initCustomerDataDictionary(sysCompany);
        initShoppingGoodsCategoryService.initShoppingGoodsCategory(sysCompany, zbShop);
        initRolePowerService.initRolePower(sysCompany);
        initGoodsTypeService.initRolePower(sysCompany);
        initShopProductCateService.initShopProductCate(sysCompany);
        initBusParameterSettingService.initBusParameterSetting(sysCompany);
        initBusParameterSettingService.initBusParameterFenxiaoSetting(sysCompany);
 
        return i;
 
    }
 
 
    @Autowired
    private WarehouseDao warehouseDao;
 
    /**
     * 为新公司创建一个总部
     *
     * @param sysCompany
     */
    private SysShopInfo addZbShop(SysCompany sysCompany) {
        SysShopInfo zbShopInfo = new SysShopInfo();
        zbShopInfo.setShopType(SysShopInfo.SHOP_TYPE_ZONGBU);
        zbShopInfo.setShopName(sysCompany.getComName() + "(总部)");
        zbShopInfo.setCompanyId(sysCompany.getComId());
        zbShopInfo.setShopTel(sysCompany.getComTel());
        zbShopInfo.setShopShortName("总部");
        zbShopInfo.setIsOpenNet(SysShopInfo.CLOSE_NET);
        zbShopInfo.setShopNo(StringUtils.getRandomString(10));
        shopInfoDao.insert(zbShopInfo);
 
        //新增一个总部仓库
        Warehouse warehouse = new Warehouse();
        warehouse.setCompanyId(sysCompany.getComId());
        warehouse.setName("总部仓库");
        warehouse.setShopId(zbShopInfo.getId());
        warehouse.setSort("1");
        warehouseDao.insert(warehouse);
        return zbShopInfo;
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int modifyByMap(SysCompany oldSysCompany, SysCompany newSysCompany) {
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        newSysCompany.setUpdateBy(user.getSuName());
 
        updatePower(newSysCompany);
        Map<String, Object> modifyMap = null;
        try {
            if (!ModelUtils.isModified(oldSysCompany, newSysCompany)) {
                return MatrixConstance.DML_SUCCESSS;
            }
 
            modifyMap = ModelUtils.comparePojo2Map(oldSysCompany, newSysCompany);
        } catch (Exception e) {
            throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newSysCompany.getComName());
        }
        if (modifyMap.size() > 0) {
            modifyMap.put("comId", oldSysCompany.getComId());
            return sysCompanyDao.updateByMap(modifyMap);
        }
        return MatrixConstance.DML_SUCCESSS;
    }
 
    /**
     * 更新公司员工权限
     *
     * @param newSysCompany
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2017年12月5日
     */
    private void updatePower(SysCompany newSysCompany) {
        List<String> oldFunctions = null;
        String functions = sysCompanyDao.selectById(newSysCompany.getComId()).getComFunctions();
        if (functions == null || functions.equals("")) {
            oldFunctions = new ArrayList<>();
        } else {
            oldFunctions = StringUtils
                    .strToCollToString(sysCompanyDao.selectById(newSysCompany.getComId()).getComFunctions(), ",");
        }
        List<String> newFunctions = new ArrayList<>();
        if (StringUtils.isNotBlank(newSysCompany.getComFunctions())) {
            newFunctions = StringUtils.strToCollToString(newSysCompany.getComFunctions(), ",");
        }
 
        Collections.sort(newFunctions);
        Collections.sort(oldFunctions);
        if (Objects.equals(JSONUtil.toJsonStr(newFunctions), JSONUtil.toJsonStr(oldFunctions))) {
            //没有更新的权限
            return;
        }
 
        List<String> finalNewFunctions = newFunctions;
        List<String> deletedFn = oldFunctions.stream().map(old -> {
            if (finalNewFunctions.stream().noneMatch(newFn -> Objects.equals(newFn, old))) {
                return old;
            } else {
                return "";
            }
        }).filter(item->StringUtils.isNotBlank(item)).collect(Collectors.toList());
 
        if(CollectionUtils.isNotEmpty(deletedFn)){
            rolePwoerFnDao.deleteByFns(newSysCompany.getComId(),deletedFn);
            //有更新权限清空缓存
            SysUsers sysUsers = new SysUsers();
            sysUsers.setCompanyId(newSysCompany.getComId());
            sysUsers.setSuValid(AppConstance.RECORD_VALID);
            List<SysUsers> users = sysUsersService.findByModel(sysUsers);
            users.forEach(user -> {
                String userKey = SecureUtil.md5(user.getSuId() + "");
                LocalCache.remove(DefaultAuthorityManager.USER_POWER_REDISKEY_PC + userKey);
                LocalCache.remove(AppAuthorityManager.USER_POWER_REDISKEY_APP + userKey);
            });
        }
 
 
 
/*        for (String old : oldFunctions) {
            // 如果新权限中不包含这个老的功能,则要更新改企业下所有的角色权限
            if (!newFunctions.contains(old)) {
                SysRole role = new SysRole();
                role.setCompanyId(newSysCompany.getComId());
                List<SysRole> roles = sysRoleDao.selectByModel(role);
                // 获取该公司下所有的权限信息
                List<SysRolePwoerFn> compalyRolePwoer = new ArrayList<>();
                for (SysRole tempRole : roles) {
                    SysRolePwoerFn r = new SysRolePwoerFn();
                    r.setRoleId(tempRole.getRoleId());
                    compalyRolePwoer.addAll(rolePwoerFnDao.selectByModel(r));
                }
                // 记录要删除的id
                List<String> delRolePwoer = new ArrayList<>();
                for (SysRolePwoerFn rolePwoer : compalyRolePwoer) {
                    // 如果有一个老权限则删除这个权限
                    if (rolePwoer.getFnId() != null && rolePwoer.getFnId().equals(old)) {
                        delRolePwoer.add(rolePwoer.getRpfId() + "");
                    }
                }
                // 如果获取到了就删除
                if (!delRolePwoer.isEmpty()) {
                    rolePwoerFnDao.deleteByIds(delRolePwoer);
                }
            }
        }*/
 
 
 
    }
 
    @Autowired
    SysUsersService sysUsersService;
 
    @Override
    public int modifyByModel(SysCompany sysCompany) {
 
        return sysCompanyDao.updateByModel(sysCompany);
 
    }
 
    @Override
    public int remove(List<String> list) {
 
        return sysCompanyDao.deleteByIds(list);
 
    }
 
    @Override
    public int removeById(String comId) {
 
        return sysCompanyDao.deleteById(Long.parseLong(comId));
 
    }
 
    @Override
    public int removeByModel(SysCompany sysCompany) {
 
        return sysCompanyDao.deleteByModel(sysCompany);
 
    }
 
    @Override
    public List<SysCompany> findInPage(SysCompany sysCompany, PaginationVO pageVo) {
 
        return sysCompanyDao.selectInPage(sysCompany, pageVo);
 
    }
 
    @Override
    public List<SysCompany> findByModel(SysCompany sysCompany) {
 
        return sysCompanyDao.selectByModel(sysCompany);
 
    }
 
    @Override
    public int findTotal(SysCompany sysCompany) {
 
        return sysCompanyDao.selectTotalRecord(sysCompany);
 
    }
 
    @Override
    public SysCompany findById(String comId) {
 
        return sysCompanyDao.selectById(Long.parseLong(comId));
 
    }
 
 
}