mitao
2024-07-08 022a7ff7abf82cd2546e18071ade5228b4e2339f
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
package com.sinata.modular.system.controller;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sinata.common.enums.EnumIsDelete;
import com.sinata.core.base.controller.BaseController;
import com.sinata.core.base.tips.ErrorTip;
import com.sinata.core.common.annotion.BussinessLog;
import com.sinata.core.common.annotion.Permission;
import com.sinata.core.common.constant.factory.PageFactory;
import com.sinata.core.log.LogObjectHolder;
import com.sinata.modular.system.model.AreaCity;
import com.sinata.modular.system.model.TCityRegion;
import com.sinata.modular.system.service.IAreaCityService;
import com.sinata.modular.system.service.ITCityRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
import java.util.Map;
 
/**
 * 区域城市控制器
 * @author goku
 */
@Controller
@RequestMapping("/areaCity")
public class AreaCityController extends BaseController {
 
    private String PREFIX = "/system/areaCity/";
 
    @Autowired
    private IAreaCityService areaCityService;
 
    @Autowired
    private ITCityRegionService cityRegionService;
 
    /**
     * 跳转到区域城市首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "areaCity.html";
    }
 
    /**
     * 跳转到添加区域城市
     */
    @RequestMapping("/areaCity_add")
    public String areaCityAdd(Model model) {
        // 省市区三级联动
        List<TCityRegion> cityList = cityRegionService.selectList(
                new EntityWrapper<TCityRegion>()
                        .setSqlSelect("code, name")
                        .eq("parent_id", 0)
        );
        model.addAttribute("cityList", cityList);
        return PREFIX + "areaCity_add.html";
    }
 
    /**
     * 跳转到修改区域城市
     */
    @RequestMapping("/areaCity_update/{areaCityId}")
    public String areaCityUpdate(@PathVariable Integer areaCityId, Model model) {
        // 省市区三级联动
        List<TCityRegion> cityList = cityRegionService.selectList(
                new EntityWrapper<TCityRegion>()
                        .setSqlSelect("code, name")
                        .eq("parent_id", 0)
        );
        model.addAttribute("cityList", cityList);
 
        AreaCity areaCity = areaCityService.selectById(areaCityId);
        model.addAttribute("item",areaCity);
        LogObjectHolder.me().set(areaCity);
        return PREFIX + "areaCity_edit.html";
    }
 
    /**
     * 获取区域城市列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list(String beginTime, String endTime, String condition) {
        Page<Map<String, Object>> page = new PageFactory().defaultPage();
        Wrapper wrapper = new EntityWrapper<AreaCity>().eq("is_delete", 0).orderBy("id", false);
 
        // 时间搜索
        if(!StringUtils.isEmpty(beginTime)) {
            wrapper.ge("create_time", beginTime + " 00:00:00");
        }
        if(!StringUtils.isEmpty(endTime)) {
            wrapper.le("create_time", endTime + " 23:59:59");
        }
        if (StrUtil.isNotBlank(condition)) {
            wrapper.like("province_name", condition)
                    .or().like("city_name", condition)
                    .or().like("county_name", condition);
        }
 
        // 查询数据列表
        List<Map<String, Object>> list = areaCityService.selectMapsPage(page, wrapper).getRecords();
 
        page.setRecords(list);
        return super.packForBT(page);
    }
 
    /**
     * 新增区域城市
     */
    @Permission
    @ResponseBody
    @BussinessLog(value = "新增区域城市")
    @RequestMapping(value = "/add")
    public Object add(AreaCity areaCity) {
        // 获取城市有效信息
        int count = areaCityService.selectCount(
                new EntityWrapper<AreaCity>()
                        .eq("is_delete", 0)
                        .eq("county_code", areaCity.getCountyCode())
        );
        if (count > 0) {
            return new ErrorTip(500, "城市已存在!");
        }
 
        // 获取城市信息
        String[] city = cityRegionService.getProvinceCityCountyName(areaCity.getCountyCode());
        // 省市区赋值
        areaCity.setProvinceName(city[0]);
        areaCity.setCityName(city[1]);
        areaCity.setCountyName(city[2]);
 
        areaCityService.insert(areaCity);
        return SUCCESS_TIP;
    }
    /**
     * 删除/批量删除
     */
    @Permission
    @ResponseBody
    @BussinessLog(value = "删除/批量删除区域城市")
    @RequestMapping(value = "/delete")
    public Object delete(@RequestParam String ids) {
        // 逻辑删除
        areaCityService.updateForSet("is_delete = 1", new EntityWrapper<AreaCity>().in("id", ids.split(",")));
        return SUCCESS_TIP;
    }
 
    /**
     * 修改区域城市
     */
    @Permission
    @ResponseBody
    @BussinessLog(value = "修改区域城市")
    @RequestMapping(value = "/update")
    public Object update(AreaCity areaCity) {
        // 获取城市有效信息
        int count = areaCityService.selectCount(
                new EntityWrapper<AreaCity>()
                        .eq("is_delete", 0)
                        .eq("county_code", areaCity.getCountyCode())
                        .ne("id", areaCity.getId())
        );
        if (count > 0) {
            return new ErrorTip(500, "城市已存在!");
        }
 
        // 获取城市信息
        String[] city = cityRegionService.getProvinceCityCountyName(areaCity.getCountyCode());
        // 省市区赋值
        areaCity.setProvinceName(city[0]);
        areaCity.setCityName(city[1]);
        areaCity.setCountyName(city[2]);
 
        areaCityService.updateById(areaCity);
        return SUCCESS_TIP;
    }
 
    /**
     * 修改区域城市状态
     */
    @ResponseBody
    @BussinessLog(value = "修改区域城市状态")
    @RequestMapping(value = "/updateState")
    public Object updateState(Integer areaCityId, Integer state) {
        areaCityService.updateForSet("is_open = " + state, new EntityWrapper<AreaCity>().eq("id", areaCityId));
        return SUCCESS_TIP;
    }
 
    /**
     * 跳转区域城市详情
     */
    @RequestMapping(value = "/detail/{areaCityId}")
    public Object detail(@PathVariable("areaCityId") Integer areaCityId, Model model) {
        AreaCity areaCity = areaCityService.selectById(areaCityId);
        model.addAttribute("item",areaCity);
        return PREFIX + "areaCity_detail.html";
    }
 
    /**
     * 查询开通城市
     */
    @ResponseBody
    @GetMapping("/getListByCode")
    public Object getListByParentId(String code1, String code2) {
        Wrapper<AreaCity> wrapper = new EntityWrapper<AreaCity>()
                .eq("is_open", 1)
                .eq("is_delete", EnumIsDelete.EXISTED.index);
        if (StrUtil.isNotBlank(code1) && !code1.equals("-1")) {
            wrapper.eq("province_code", code1)
                    .like("city_code", code1.substring(0, 2) + "%" + "00", SqlLike.CUSTOM)
                    .groupBy("city_code");
            return areaCityService.selectList(wrapper);
        } else if (StrUtil.isNotBlank(code2) && !code2.equals("-1")) {
            wrapper.eq("city_code", code2)
                    .like("county_code", code2.substring(0, 4), SqlLike.RIGHT)
                    .groupBy("county_code");
            return areaCityService.selectList(wrapper);
        }
        return null;
    }
}