liurunyu
2024-11-29 8d87e04c9e5fb2fd54b25b045af6c60bf5641027
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
package com.dy.pipIrrBase.district;
 
import com.dy.pipIrrBase.district.qo.DistrictQO;
import com.dy.pipIrrGlobal.daoBa.AreaCode2023Mapper;
import com.dy.pipIrrGlobal.daoBa.BaDistrictMapper;
import com.dy.pipIrrGlobal.pojoBa.BaDistrict;
import com.dy.pipIrrGlobal.util.DistrictLevel;
import com.dy.pipIrrGlobal.voBa.VoDistrict;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
public class DistrictSv {
 
    private BaDistrictMapper dao;
    private AreaCode2023Mapper areaCode2023Dao;
 
    @Autowired
    private void setDao(BaDistrictMapper dao){
        this.dao = dao;
    }
 
    @Autowired
    private void setDao(AreaCode2023Mapper areaCode2023Dao){
        this.areaCode2023Dao = areaCode2023Dao;
    }
 
    /**
     * 从第一级行政区划开始,得到所有行政区划信息
     * @param firstLevel 第一级行下区 level
     * @return 所有行政区划集合
     */
    public List<BaDistrict> selectAllByLevel(byte firstLevel){
        List<BaDistrict> list = this.dao.selectByLevel(firstLevel) ;
        if(list != null && list.size() > 0){
            for(BaDistrict po : list){
                po.canDelete = false ;
                po.canAdd = true ;
                getSubDistrict(po) ;
            }
        }
        return list ;
    }
 
    /**
     * 得到一个行政区
     * @param id 行政区ID
     * @return 行政区实体
     */
    public BaDistrict selectById(Long id){
        return this.dao.selectByPrimaryKey(id) ;
    }
 
    /**
     * 保存实体
     * @param po 实体
     * @return 数量
     */
    @Transactional
    public int save(BaDistrict po){
        return this.dao.putin(po) ;
    }
 
    /**
     * 保存修改实体
     * @param po 实体
     * @return 数量
     */
    @Transactional
    public int update(BaDistrict po){
        return this.dao.updateByPrimaryKeySelective(po) ;
    }
 
    /**
     * 保存修改实体
     * @param id 实体ID
     * @return 数量
     */
    @Transactional
    public int delete(Long id){
        return this.dao.deleteByPrimaryKey(id) ;
    }
 
    /**
     * 得到下级行政区划
     * @param po 上级行政区划
     */
    private void getSubDistrict(BaDistrict po){
        if(po != null && po.id != null){
            po.subDistricts = this.dao.selectBySupperId(po.id) ;
            if(po.subDistricts != null && po.subDistricts.size() > 0){
                for(BaDistrict subPo : po.subDistricts){
                    getSubDistrict(subPo) ;
                }
            }else{
                //没有下级了,可以删除
                po.canDelete = false ;
                if(po.level.code.byteValue() == DistrictLevel.Village.code.byteValue()){
                    //到村级了,不可以再添加行政区了
                    po.canAdd = false ;
                }
            }
        }
    }
 
    /**
     * 根据级别获取行政区划列表
     * @param level
     * @return
     */
    public List<Map<String, Object>> getDistrictgsByLevel(Integer level) {
        return dao.getDistrictgsByLevel(level);
    }
 
    /**
     * 根据父ID获取行政区划列表
     * @param supperId
     * @return
     */
    public List<Map<String, Object>> getDistrictsBySupperId(Long supperId) {
        return dao.getDistrictsBySupperId(supperId);
    }
 
    /**
     * 根据区划代码查询指定级别行政区划
     * @param qo
     * @return
     */
    public List<VoDistrict> getDistrictS(DistrictQO qo) {
        return areaCode2023Dao.getDistrictS(qo.getAredCode(), qo.getLevel());
    }
}