luqingyang
2023-10-24 cb642724c54d7d850aec5e5ee27fcc7186f352d7
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
package com.consum.base.service;
 
import com.consum.base.pojo.FinSysTenantParam;
import com.consum.model.po.FinSysTenant;
import com.iplatform.model.po.S_user_core;
import com.walker.infrastructure.utils.CollectionUtils;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.service.BaseServiceImpl;
import org.springframework.stereotype.Service;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @Description 系统机构
 * @Author 卢庆阳
 * @Date 2023/10/23
 */
@Service
public class FinSysTenantServiceImpl extends BaseServiceImpl {
 
    private static final String QUERY_BY_PARENT_CODE = "SELECT * FROM fin_sys_tenant WHERE CODE = ?";
 
    /**
     * @Description 根据机构id查询机构
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    public FinSysTenant selectByTenantId(String code) {
        List<FinSysTenant> select = this.select(QUERY_BY_PARENT_CODE, new Object[]{code}, new FinSysTenant());
        if (StringUtils.isEmptyList(select)) {
            return null;
        } else {
            return select.get(0);
        }
    }
 
    /**
     * 添加机构
     * @author 卢庆阳
     * @date 2023/10/4
     */
    public int addSysTenant(FinSysTenantParam param, S_user_core currentUser, int lv) {
        FinSysTenant sysTenant = new FinSysTenant();
 
        //id和tempId
        StringBuilder sql = new StringBuilder("SELECT max(id) FROM fin_sys_tenant WHERE 1=1");
        HashMap<String, Object> paramts = new HashMap<>();
 
        sql.append(" and parent_id =:parent_id");
        paramts.put("parent_id", param.getParentId());
 
        List<Map<String, Object>> list = this.select(sql.toString(), paramts);
        long id = 0L;
        if (!CollectionUtils.isEmpty(list) && list.get(0).get("max(id)") != null) {
            id = (Long) list.get(0).get("max(id)") + 1;
        } else {
            id = param.getParentId() * 1000 + 1;
        }
        sysTenant.setTempId(id);
        sysTenant.setId(id);
        sysTenant.setStatus(param.getStatus());
 
        sysTenant.setParentId(param.getParentId());
        sysTenant.setCode(param.getCode());
        sysTenant.setName(param.getName());
        sysTenant.setCreateTime(DateUtils.getDateTimeNumber(System.currentTimeMillis()));
        sysTenant.setSummary(param.getSummary());
        sysTenant.setCreateBy(currentUser.getUser_name());
        //设置各层级id及名称
        sysTenant.setLv(lv);
        setLvIdAndName(param, sysTenant, id, lv);
 
        return this.insert(sysTenant);
    }
 
    /**
     * 设置各层级id及名称
     *
     * @author 卢庆阳
     * @date 2023/10/4
     */
    private void setLvIdAndName(FinSysTenantParam param, FinSysTenant sysTenant, long id, int lv) {
        if (lv == 2) {    //新增市级机构
            //查询上一级 - 省级
            FinSysTenant finSysTenant1 = this.get(new FinSysTenant(param.getParentId()));
            if (finSysTenant1 != null) {
                sysTenant.setLv1Id(finSysTenant1.getId());
                sysTenant.setLv1Name(finSysTenant1.getName());
                sysTenant.setLv2Id(id);
                sysTenant.setLv2Name(param.getName());
            }
        } else if (lv == 3) {     //新增县级机构
            //查询上一级 - 市级
            FinSysTenant finSysTenant2 = this.get(new FinSysTenant(param.getParentId()));
            if (finSysTenant2 != null) {
                //查询上一级 - 省级
                FinSysTenant finSysTenant1 = this.get(new FinSysTenant(finSysTenant2.getParentId()));
 
                sysTenant.setLv1Id(finSysTenant1.getId());
                sysTenant.setLv1Name(finSysTenant1.getName());
                sysTenant.setLv2Id(finSysTenant2.getId());
                sysTenant.setLv2Name(finSysTenant2.getName());
                sysTenant.setLv3Id(id);
                sysTenant.setLv3Name(param.getName());
            }
        } else if (lv == 4) {      //新增支局机构
            //查询上一级 - 县级
            FinSysTenant finSysTenant3 = this.get(new FinSysTenant(param.getParentId()));
            if (finSysTenant3 != null) {
                //查询上一级 - 市级
                FinSysTenant finSysTenant2 = this.get(new FinSysTenant(finSysTenant3.getParentId()));
                if (finSysTenant2 != null) {
                    //查询上一级 - 省级
                    FinSysTenant sysTenant1 = this.get(new FinSysTenant(finSysTenant2.getParentId()));
                    if (sysTenant1 != null) {
                        sysTenant.setLv1Id(sysTenant1.getId());
                        sysTenant.setLv1Name(sysTenant1.getName());
                        sysTenant.setLv2Id(finSysTenant2.getId());
                        sysTenant.setLv2Name(finSysTenant2.getName());
                        sysTenant.setLv3Id(finSysTenant3.getId());
                        sysTenant.setLv3Name(finSysTenant3.getName());
                        sysTenant.setLv4Id(id);
                        sysTenant.setLv4Name(param.getName());
                    }
                }
            }
        }
    }
}