puzhibing
2023-06-13 7860e5cb6db60aeb82c640651998f8294635df5b
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package com.dsh.guns.modular.system.controller.general;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsh.course.entity.City;
import com.dsh.course.feignClient.activity.CompanyCityClient;
import com.dsh.course.feignClient.app.OpenCityClinet;
import com.dsh.course.mapper.CityMapper;
import com.dsh.course.util.GoogleMap.GoogleMapUtil;
import com.dsh.guns.config.UserExt;
import com.dsh.guns.core.base.controller.BaseController;
import com.dsh.guns.core.base.tips.SuccessTip;
import com.dsh.guns.core.common.constant.factory.PageFactory;
import com.dsh.guns.core.log.LogObjectHolder;
import com.dsh.guns.core.util.SinataUtil;
import com.dsh.guns.modular.system.model.*;
import com.dsh.guns.modular.system.service.ITOpenCityBusinessService;
import com.dsh.guns.modular.system.service.ITOpenCityService;
import com.dsh.guns.modular.system.service.ITRegionService;
import com.dsh.guns.modular.system.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.util.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * 城市管理控制器
 *
 * @author fengshuonan
 * @Date 2020-06-02 11:40:31
 */
@Controller
@RequestMapping("/tOpenCity")
public class TOpenCityController extends BaseController {
 
    private String PREFIX = "/system/tOpenCity/";
 
    @Autowired
    private ITOpenCityService tOpenCityService;
 
    @Autowired
    private CityMapper cityMapper;
 
    @Autowired
    private ITRegionService tRegionService;
 
    @Autowired
    private ITOpenCityBusinessService tOpenCityBusinessService;
 
    @Autowired
    private GoogleMapUtil googleMapUtil;
 
    @Resource
    private OpenCityClinet openCityClinet;
    @Autowired
    private CompanyCityClient companyCityClient;
 
 
    /**
     * 跳转到城市管理首页
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("language",UserExt.getLanguage());
        return PREFIX + "tOpenCity.html";
    }
 
    /**
     * 跳转到添加城市管理
     */
    @RequestMapping("/tOpenCity_add")
    public String tOpenCityAdd(Model model) {
        List<City> pid = cityMapper.selectList(new QueryWrapper<City>().eq("pid", 0));
        Integer language = UserExt.getLanguage();
        if(language==2){
            for (City city : pid) {
                city.setChineseName(city.getEnglishName());
            }
        }
        if(language==3){
            for (City city : pid) {
                city.setChineseName(city.getIndonesianName());
            }
        }
        model.addAttribute("provinceList",pid);
        model.addAttribute("language",UserExt.getLanguage());
        return PREFIX + "tOpenCity_add.html";
    }
 
    /**
     * 跳转到修改城市管理
     */
    @RequestMapping("/tOpenCity_update/{tOpenCityId}")
    public String tOpenCityUpdate(@PathVariable Integer tOpenCityId, Model model) {
        TOpenCity tOpenCity = tOpenCityService.getById(tOpenCityId);
        model.addAttribute("item",tOpenCity);
        LogObjectHolder.me().set(tOpenCity);
        Integer language = UserExt.getLanguage();
        //省
        List<City> pid = cityMapper.selectList(new QueryWrapper<City>().eq("pid", 0));
        if(language==2){
            for (City city : pid) {
                city.setChineseName(city.getEnglishName());
            }
        }
        if(language==3){
            for (City city : pid) {
                city.setChineseName(city.getIndonesianName());
            }
        }
        model.addAttribute("provinceList",pid);
        //市
        List<City> pid1 = cityMapper.selectList(new QueryWrapper<City>().eq("pid", tOpenCity.getAreaId()));
        if(language==2){
            for (City city : pid1) {
                city.setChineseName(city.getEnglishName());
            }
        }
        if(language==3){
            for (City city : pid1) {
                city.setChineseName(city.getIndonesianName());
            }
        }
        model.addAttribute("cityList",pid1);
        //区
        List<TRegion> areaList = tRegionService.getBaseMapper().selectList(new QueryWrapper<TRegion>().eq("parent_id", tOpenCity.getCityId()));
        model.addAttribute("areaList",areaList);
        //经营业务
        List<Map<String,Object>> businessList = tOpenCityService.getBusinessByCityId(tOpenCityId);
        model.addAttribute("businessList",businessList);
        model.addAttribute("language",UserExt.getLanguage());
        return PREFIX + "tOpenCity_edit.html";
    }
 
    /**
     * 跳转到立即处理页面
     */
    @RequestMapping("/tOpenCity_immediately/{id}")
    public String sysCouponActivityImmediately(@PathVariable Integer id, Model model) {
        TOpenCity tOpenCity = tOpenCityService.getById(id);
        model.addAttribute("item",tOpenCity);
        LogObjectHolder.me().set(tOpenCity);
        Integer language = UserExt.getLanguage();
        //省
        List<City> pid = cityMapper.selectList(new QueryWrapper<City>().eq("pid", 0));
        if(language==2){
            for (City city : pid) {
                city.setChineseName(city.getEnglishName());
            }
        }
        if(language==3){
            for (City city : pid) {
                city.setChineseName(city.getIndonesianName());
            }
        }
        model.addAttribute("provinceList",pid);
        //市
        List<City> pid1 = cityMapper.selectList(new QueryWrapper<City>().eq("pid", tOpenCity.getAreaId()));
        if(language==2){
            for (City city : pid1) {
                city.setChineseName(city.getEnglishName());
            }
        }
        if(language==3){
            for (City city : pid1) {
                city.setChineseName(city.getIndonesianName());
            }
        }
        model.addAttribute("cityList",pid1);
        //区
        List<TRegion> areaList = tRegionService.getBaseMapper().selectList(new QueryWrapper<TRegion>().eq("parent_id", tOpenCity.getCityId()));
        model.addAttribute("areaList",areaList);
        //经营业务
        List<Map<String,Object>> businessList = tOpenCityService.getBusinessListByCityId(id);
        model.addAttribute("businessList",businessList);
        model.addAttribute("id",id);
        model.addAttribute("language", UserExt.getLanguage());
        return PREFIX + "tOpenCity_immediately.html";
    }
 
    /**
     * 立即处理操作
     */
    @RequestMapping(value = "/immediately")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public Object immediately(@RequestParam Integer id,@RequestParam Integer state,@RequestParam String remark) {
        TOpenCity tOpenCity = tOpenCityService.getById(id);
        if (SinataUtil.isNotEmpty(tOpenCity)){
            if(Objects.nonNull(tOpenCity.getSuperId()) && state == 3){
                // 查询出上一条老的数据
                TOpenCity byId = tOpenCityService.getById(tOpenCity.getSuperId());
                //删除老数据经营业务
                TOpenCityBusiness business = new TOpenCityBusiness();
                business.setFlag("3");
                tOpenCityBusinessService.update(business,new LambdaQueryWrapper<TOpenCityBusiness>().eq(TOpenCityBusiness::getOpenCityId,byId.getId()));
 
                Integer byIdId = byId.getId();
                // 审核通过的新数据覆盖老的数据
                tOpenCity.setId(null);
                BeanUtils.copyProperties(tOpenCity,byId);
                byId.setId(byIdId);
                byId.setStatus(state);
                byId.setRemark(remark);
                tOpenCityService.updateById(byId);
 
                //添加到mongodb
                openCityClinet.saveMongodb(byId);
 
 
                // 查询新数据的经营业务,指向老数据
                List<TOpenCityBusiness> list = tOpenCityBusinessService.list(Wrappers.lambdaQuery(TOpenCityBusiness.class)
                        .eq(TOpenCityBusiness::getOpenCityId, id));
                for (TOpenCityBusiness tOpenCityBusiness : list) {
                    tOpenCityBusiness.setOpenCityId(byId.getId());
                }
                tOpenCityBusinessService.updateBatchById(list);
 
                tOpenCityService.removeById(id);
            }else {
                tOpenCity.setStatus(state);
                tOpenCity.setRemark(remark);
                tOpenCityService.updateById(tOpenCity);
 
                //添加到mongodb
                openCityClinet.saveMongodb(tOpenCity);
            }
        }
        return SUCCESS_TIP;
    }
 
 
    @RequestMapping(value = "/change")
    @ResponseBody
    public Object change(@RequestParam Integer id) {
        List<City> list = new ArrayList<>();
        if (SinataUtil.isNotEmpty(id)){
            list = cityMapper.selectList(new QueryWrapper<City>().eq("pid", id));
        }
 
        Integer language = UserExt.getLanguage();
        if(language==2){
            for (City city : list) {
                city.setChineseName(city.getEnglishName());
            }
        }
        if(language==3){
            for (City city : list) {
                city.setChineseName(city.getIndonesianName());
            }
        }
        return list;
    }
 
    /**
     * 获取城市管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String insertTime,String name) {
        String beginTime = null;
        String endTime = null;
        if (SinataUtil.isNotEmpty(insertTime)){
            String[] timeArray = insertTime.split(" - ");
            beginTime = timeArray[0];
            endTime = timeArray[1];
        }
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        Integer language = UserExt.getLanguage();
        List<Map<String, Object>> openCityList = tOpenCityService.getOpenCityList(page, beginTime, endTime, name,language);
//        for (Map<String, Object> stringObjectMap : openCityList) {
//            Integer areaId = (Integer)stringObjectMap.get("areaId");
//            Integer cityId = (Integer)stringObjectMap.get("cityId");
//            City city = cityMapper.selectById(areaId);
//            City city1 = cityMapper.selectById(cityId);
//            if(UserExt.getLanguage()==1){
//                stringObjectMap.put("areaName",city.getChineseName());
//                stringObjectMap.put("cityName",city1.getChineseName());
//            }else if(UserExt.getLanguage()==2){
//                stringObjectMap.put("areaName",city.getEnglishName());
//                stringObjectMap.put("cityName",city1.getEnglishName());
//            }else {
//                stringObjectMap.put("areaName",city.getIndonesianName());
//                stringObjectMap.put("cityName",city1.getIndonesianName());
//            }
//            System.out.println(areaId);
//        }
        for (Map<String, Object> stringObjectMap : openCityList) {
            if(UserExt.getLanguage()==1){
                stringObjectMap.put("name",stringObjectMap.get("chineseName"));
            }else if(UserExt.getLanguage()==2){
                stringObjectMap.put("name",stringObjectMap.get("englishName"));
            }else {
                stringObjectMap.put("name",stringObjectMap.get("indonesianName"));
            }
        }
        page.setRecords(openCityList);
        return super.packForBT(page);
    }
 
    /**
     * 新增城市管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TOpenCity tOpenCity,@RequestParam String businessTypeStr,Integer areaId,Integer cityId,Integer provinceId) throws Exception {
        tOpenCity.setInsertTime(new Date());
        User user = UserExt.getUser();
        tOpenCity.setInsertUser(user.getId());
        tOpenCity.setAreaId(areaId);
        tOpenCity.setCityId(cityId);
        tOpenCity.setProvinceId(provinceId);
        tOpenCityService.save(tOpenCity);
 
        //添加经营业务
        testJSONStrToJSONArray(businessTypeStr,tOpenCity.getId());
        return SUCCESS_TIP;
    }
 
    public void testJSONStrToJSONArray(String subArr,Integer tOpenCityId){
        JSONArray jsonArray = JSON.parseArray(subArr);
        int size = jsonArray.size();
        for (int i = 0; i < size; i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            TOpenCityBusiness business = new TOpenCityBusiness();
            if (SinataUtil.isNotEmpty(jsonObject.getInteger("busId")) && jsonObject.getInteger("busId") != 0){
                business = tOpenCityBusinessService.getById(jsonObject.getInteger("busId"));
                business.setUpdateTime(new Date());
                business.setUpdateUser(UserExt.getUser().getId());
                business.setFlag("2");
            }else{
                business.setInsertTime(DateUtil.parse(jsonObject.getString("time")+" 00:00:00","yyyy-MM-dd HH:mm:ss"));
                business.setInsertUser(UserExt.getUser().getId());
            }
            business.setOpenCityId(tOpenCityId);
            String type = jsonObject.getString("type");
            Integer businessType = 0;
            if ("摩托车".equals(type)||"Motorcycle".equals(type) || "Motosikal".equals(type)){
                businessType = 1;
            }else if ("同城快送".equals(type)||"City express delivery".equals(type)||"Pengiriman ekspres kota".equals(type)){
                businessType = 4;
            }
            business.setBusinessType(businessType);
            business.setSort(jsonObject.getInteger("sort"));
            if (SinataUtil.isNotEmpty(jsonObject.getInteger("busId")) && jsonObject.getInteger("busId") != 0){
                tOpenCityBusinessService.updateById(business);
            }else{
                tOpenCityBusinessService.save(business);
            }
        }
    }
 
    /**
     * 删除城市管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tOpenCityId) {
        Boolean isBind = companyCityClient.isBindCompany(tOpenCityId);
        if(isBind){
            if(UserExt.getLanguage() == 1){
                return new SuccessTip(500,"该城市已绑定企业,无法删除!");
            }
            if(UserExt.getLanguage() == 2){
                return new SuccessTip(500,"This city is already bound to an enterprise and cannot be deleted!");
            }
            if(UserExt.getLanguage() == 3){
                return new SuccessTip(500,"Kota ini sudah terikat pada sebuah perusahaan dan tidak dapat dihapus!");
            }
        }
        TOpenCity tOpenCity = tOpenCityService.getById(tOpenCityId);
        tOpenCity.setFlag("3");
        tOpenCityService.updateById(tOpenCity);
        //添加到mongodb
        openCityClinet.delMongodb(tOpenCityId);
        return SUCCESS_TIP;
    }
 
    /**
     * 修改城市管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TOpenCity tOpenCity,@RequestParam String businessTypeStr,Integer areaId,Integer cityId,Integer provinceId) {
        Integer userId = UserExt.getUser().getId();
        tOpenCity.setUpdateTime(new Date());
        tOpenCity.setUpdateUser(userId);
        tOpenCityService.updateById(tOpenCity);
 
        TOpenCity city = new TOpenCity();
        BeanUtils.copyProperties(tOpenCity,city);
        city.setId(null);
        city.setInsertTime(new Date());
        city.setInsertUser(userId);
        city.setSuperId(tOpenCity.getId());
        tOpenCityService.save(city);
 
        testJSONStrToJSONArray(businessTypeStr,city.getId());
        return SUCCESS_TIP;
    }
 
    /**
     * 城市管理详情
     */
    @RequestMapping(value = "/detail/{tOpenCityId}")
    @ResponseBody
    public Object detail(@PathVariable("tOpenCityId") Integer tOpenCityId) {
        return tOpenCityService.getById(tOpenCityId);
    }
}