xuhy
2023-05-11 9c0566b86975f112a7f0cf044bab322b7ffe4f0a
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
package com.stylefeng.guns.modular.system.controller.general;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.controller.resp.TBranchOfficeResp;
import com.stylefeng.guns.modular.system.controller.resp.TDriverResp;
import com.stylefeng.guns.modular.system.enums.StatusEnum;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2023-02-21 11:17:18
 */
@Controller
@RequestMapping("/tBranchOffice")
public class TBranchOfficeController extends BaseController {
 
    private String PREFIX = "/system/tBranchOffice/";
 
    @Autowired
    private ITBranchOfficeService tBranchOfficeService;
    @Autowired
    private ITSystemConfigService tSystemConfigService;
 
    @Autowired
    private ITRegionService tRegionService;
    @Autowired
    private ITDriverService tDriverService;
    @Autowired
    private ITDriverWorkService tDriverWorkService;
    @Autowired
    private RedisUtil redisUtil;
 
    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tBranchOffice.html";
    }
 
    /**
     * 跳转到添加
     */
    @RequestMapping("/tBranchOffice_add")
    public String tBranchOfficeAdd() {
        return PREFIX + "tBranchOffice_add.html";
    }
 
    /**
     * 跳转到修改
     */
    @RequestMapping("/tBranchOffice_update/{tBranchOfficeId}")
    public String tBranchOfficeUpdate(@PathVariable Integer tBranchOfficeId, Model model) {
 
        TBranchOffice tBranchOffice = tBranchOfficeService.selectById(tBranchOfficeId);
        TBranchOfficeResp tBranchOfficeResp = new TBranchOfficeResp();
        BeanUtils.copyProperties(tBranchOffice,tBranchOfficeResp);
 
        // 查询区域
        TRegion city = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", tBranchOffice.getCityCode())
                .last("LIMIT 1"));
        TRegion district = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", tBranchOffice.getDistrictCode())
                .last("LIMIT 1"));
 
        if(StringUtils.hasLength(tBranchOffice.getDistrictName())){
            tBranchOfficeResp.setArea(tBranchOffice.getProvinceName()+"/"+tBranchOffice.getCityName()+"/"+tBranchOffice.getDistrictName());
        }else {
            tBranchOfficeResp.setArea(tBranchOffice.getProvinceName()+"/"+tBranchOffice.getCityName());
        }
 
        if(Objects.nonNull(city)){
            if(Objects.nonNull(district)){
                tBranchOfficeResp.setAreaId(city.getParentId()+"/"+city.getId()+"/"+district.getId());
            }else {
                tBranchOfficeResp.setAreaId(city.getParentId()+"/"+city.getId());
            }
        }
 
        model.addAttribute("item",tBranchOfficeResp);
        LogObjectHolder.me().set(tBranchOffice);
        return PREFIX + "tBranchOffice_edit.html";
    }
 
    /**
     * 跳转到详情
     */
    @RequestMapping("/tBranchOfficeDetail")
    public String tBranchOfficeDetail(Integer tBranchOfficeId, Model model) {
        tBranchOfficeService.tBranchOfficeDetail(tBranchOfficeId,model);
        TSystemConfig tSystemConfig = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 3));
        JSONObject jsonObject = JSONObject.parseObject(tSystemConfig.getContent());
        model.addAttribute("num2",jsonObject.getInteger("num2"));
        model.addAttribute("num3",jsonObject.getInteger("num3"));
        return PREFIX + "tBranchOfficeDetail.html";
    }
 
    /**
     * 跳转区域页面新增
     */
    @RequestMapping("/areaPageAdd")
    public String areaPageAdd(String area,String areaId,Model model) {
        String[] split1 = areaId.split("/");
        List<TRegion> tRegions = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
        model.addAttribute("provinceList",tRegions);
        List<Integer> provinceIds = tRegions.stream().map(TRegion::getId).collect(Collectors.toList());
        // 查询市
        List<TRegion> tRegions1 = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", split1[0]));
        model.addAttribute("cityList",tRegions1);
 
        // 查询区
        List<Integer> cityIds = tRegions1.stream().map(TRegion::getId).collect(Collectors.toList());
        List<TRegion> tRegions2 = tRegionService.selectList(new EntityWrapper<TRegion>().in("parent_id", cityIds));
        model.addAttribute("districtList",tRegions2);
 
        if(StringUtils.hasLength(area) && StringUtils.hasLength(areaId)){
            String[] split = area.split("/");
            model.addAttribute("provinceName",split[0]);
            model.addAttribute("cityName",split[1]);
            if(split.length>2){
                model.addAttribute("districtName",split[2]);
            }else {
                model.addAttribute("districtName","");
            }
 
            model.addAttribute("provinceId",split1[0]);
            model.addAttribute("cityId",split1[1]);
            if(split1.length>2) {
                model.addAttribute("districtId", split1[2]);
            }else {
                model.addAttribute("districtId", "");
            }
        }else {
            model.addAttribute("provinceName","");
            model.addAttribute("cityName","split[1]");
            model.addAttribute("districtName","");
 
            model.addAttribute("provinceId","");
            model.addAttribute("cityId","split1[1]");
            model.addAttribute("districtId", "");
        }
        return PREFIX + "tBranchOfficeAreaAdd.html";
    }
 
    /**
     * 跳转区域页面编辑
     */
    @RequestMapping("/areaPageUpdate")
    public String areaPageUpdate(String area,String areaId,Model model) {
 
        String[] split = area.split("/");
        model.addAttribute("provinceName",split[0]);
        model.addAttribute("cityName",split[1]);
        if(split.length>2){
            model.addAttribute("districtName",split[2]);
        }else {
            model.addAttribute("districtName","");
        }
 
        String[] split1 = areaId.split("/");
        String provinceId = split1[0];
        model.addAttribute("provinceId",split1[0]);
        model.addAttribute("cityId",split1[1]);
        if(split1.length>2) {
            model.addAttribute("districtId", split1[2]);
        }else {
            model.addAttribute("districtId", "");
        }
 
        List<TRegion> tRegions = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
        model.addAttribute("provinceList",tRegions);
//        List<Integer> provinceIds = tRegions.stream().map(TRegion::getId).collect(Collectors.toList());
        // 查询市
        List<TRegion> tRegions1 = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", provinceId));
        model.addAttribute("cityList",tRegions1);
 
        // 查询区
        List<Integer> cityIds = tRegions1.stream().map(TRegion::getId).collect(Collectors.toList());
        List<TRegion> tRegions2 = tRegionService.selectList(new EntityWrapper<TRegion>().in("parent_id", cityIds));
        model.addAttribute("districtList",tRegions2);
 
        return PREFIX + "tBranchOfficeAreaUpdate.html";
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String branchOfficeName,String principal,String principalPhone,Integer operatingBusiness ,Integer status) {
        List<TBranchOfficeResp> tBranchOfficeRespList = tBranchOfficeService.getPageList(branchOfficeName,principal,principalPhone,operatingBusiness,status);
        // 分公司查询优惠券,订单,司机等信息
        tBranchOfficeService.queryOtherInfo(tBranchOfficeRespList);
        return tBranchOfficeRespList;
    }
 
    @ApiOperation(value = "市区查询",notes="市区查询")
    @RequestMapping(value = "/areaCity")
    @ResponseBody
    public Object areaCity(Integer parentId,Model model) {
        List<TRegion> tRegions = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", parentId));
        /*if(CollectionUtils.isEmpty(tRegions) && StringUtils.hasLength(parentName)){
            TRegion parent = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("name", parentName));
            tRegions = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", parentId));
        }*/
        model.addAttribute("list",tRegions);
        return tRegions;
    }
 
    /**
     * 启用分公司
     */
    @RequestMapping(value = "/start")
    @ResponseBody
    public Object start(Integer id) {
        TBranchOffice tBranchOffice = tBranchOfficeService.selectById(id);
        tBranchOffice.setStatus(StatusEnum.NORMAL.getCode());
        tBranchOfficeService.updateById(tBranchOffice);
        return SUCCESS_TIP;
    }
 
    /**
     * 冻结分公司
     */
    @RequestMapping(value = "/stop")
    @ResponseBody
    public Object updateStatus(Integer id) {
        TBranchOffice tBranchOffice = tBranchOfficeService.selectById(id);
        tBranchOffice.setStatus(StatusEnum.FREEZE.getCode());
        tBranchOfficeService.updateById(tBranchOffice);
        List<TDriver> list = tDriverService.selectList(new EntityWrapper<TDriver>()
                .eq("branchOfficeId", tBranchOffice.getId()));
        for (TDriver tDriver : list) {
            String value = redisUtil.getValue("DRIVER_" + tDriver.getPhone());
            redisUtil.remove(value);
            redisUtil.remove("DRIVER_" + tDriver.getPhone());
            TDriverWork tDriverWork = tDriverWorkService.selectOne(new EntityWrapper<TDriverWork>()
                    .eq("driverId", tDriver.getId())
                    .eq("status", 1)
                    .orderBy("workTime", false)
                    .last("LIMIT 1"));
            if(Objects.nonNull(tDriverWork)){
                tDriverWork.setStatus(2);
                tDriverWork.setOffWorkTime(new Date());
                tDriverWorkService.updateById(tDriverWork);
            }
        }
        return SUCCESS_TIP;
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/list-back")
    @ResponseBody
    public Object listBack(String condition) {
        return tBranchOfficeService.selectList(null);
    }
 
    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TBranchOffice tBranchOffice) {
        int count = tBranchOfficeService.selectCount(new EntityWrapper<TBranchOffice>().eq("branchOfficeName", tBranchOffice.getBranchOfficeName()));
        if(count>0){
            return new SuccessTip(500,"该分公司名称已存在!");
        }
 
        Object o = tBranchOfficeService.addOrUpdate(tBranchOffice);
        if(Objects.nonNull(o)){
            return o;
        }
        tBranchOffice.setPrincipal(tBranchOffice.getPrincipal().replace(" ",""));
        tBranchOffice.setStatus(StatusEnum.NORMAL.getCode());
 
        tBranchOfficeService.insert(tBranchOffice);
        return SUCCESS_TIP;
    }
 
    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tBranchOfficeId) {
        TBranchOffice tBranchOffice = tBranchOfficeService.selectById(tBranchOfficeId);
        tBranchOffice.setStatus(StatusEnum.DELETE.getCode());
        tBranchOfficeService.updateById(tBranchOffice);
        return SUCCESS_TIP;
    }
 
    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TBranchOffice tBranchOffice) {
        TBranchOffice office = tBranchOfficeService.selectOne(new EntityWrapper<TBranchOffice>().eq("branchOfficeName", tBranchOffice.getBranchOfficeName())
                .last("LIMIT 1"));
        if(Objects.nonNull(office) && !tBranchOffice.getId().equals(office.getId())){
            return new SuccessTip(500,"该分公司名称已存在!");
        }
//        Object ocr = ocr("E:\\071bf986db0b00355c0ed190bbd3b16.png");
//        System.err.println(ocr);
        Object o = tBranchOfficeService.addOrUpdate(tBranchOffice);
        if(Objects.nonNull(o)){
            return o;
        }
        tBranchOffice.setPrincipal(tBranchOffice.getPrincipal().replace(" ",""));
        tBranchOfficeService.updateById(tBranchOffice);
        return SUCCESS_TIP;
    }
 
    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{tBranchOfficeId}")
    @ResponseBody
    public Object detail(@PathVariable("tBranchOfficeId") Integer tBranchOfficeId) {
        return tBranchOfficeService.selectById(tBranchOfficeId);
    }
}