44323
2023-10-07 e6ee956618b3c1a6371a6c22dfbb70868dbd3f17
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
package com.dsh.guns.modular.system.controller.code;
 
 
import cn.hutool.crypto.SecureUtil;
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.extension.plugins.pagination.Page;
import com.dsh.course.feignClient.account.CityManagerClient;
import com.dsh.course.feignClient.account.CoachClient;
import com.dsh.course.feignClient.account.CoachTypeClient;
import com.dsh.course.feignClient.account.model.CityManager;
import com.dsh.course.feignClient.account.model.Coach;
import com.dsh.course.feignClient.account.model.CoachSerchVO;
import com.dsh.course.feignClient.account.model.CoachType;
import com.dsh.guns.config.UserExt;
import com.dsh.guns.core.base.controller.BaseController;
import com.dsh.guns.core.common.constant.factory.PageFactory;
import com.dsh.guns.core.util.SinataUtil;
import com.dsh.guns.modular.system.model.*;
import com.dsh.guns.modular.system.service.ICityService;
import com.dsh.guns.modular.system.service.IUserService;
import com.dsh.guns.modular.system.service.TOperatorCityService;
import com.dsh.guns.modular.system.service.TOperatorService;
import com.dsh.guns.modular.system.util.ResultUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
 
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
 
/**
 * 运营商管理
 */
@Controller
@RequestMapping("/operator")
public class OperatorController extends BaseController {
 
    private String PREFIX = "/system/operator/";
    @Autowired
    private TOperatorService operatorService;
    @Autowired
    private TOperatorCityService operatorCityService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICityService cityService;
    /**
     * 跳转运营商管理首页
     */
    @RequestMapping("")
    public String index(Model model) {
        return PREFIX + "Operator.html";
    }
 
    /**
     * 跳转运营商重置密码页面
     */
    @RequestMapping("/resetPassword")
    public String resetPassword(Model model) {
        return PREFIX + "Operator_resetPassword.html";
    }
    /**
     * 跳转运营商添加页面
     */
    @RequestMapping("/add")
    public String add(Model model) {
        List<TCity> list = cityService.list(new LambdaQueryWrapper<TCity>().eq(TCity::getParentId, 0));
        model.addAttribute("provinceList",list);
        return PREFIX + "Operator_add.html";
    }
    /**
     * 跳转运营商编辑页面
     */
    @RequestMapping("/update/{id}")
    public String update(Model model,@PathVariable("id") Integer id) {
        List<TCity> provinceList = cityService.list(new LambdaQueryWrapper<TCity>().eq(TCity::getParentId, 0));
        model.addAttribute("provinceList",provinceList);
        model.addAttribute("id",id);
        TOperator byId = operatorService.getById(id);
        // 运营商名称
        model.addAttribute(byId.getName());
        User byId1 = userService.getById(byId.getUserId());
        model.addAttribute("userName",byId1.getName());
        model.addAttribute("phone",byId1.getPhone());
        model.addAttribute("data",byId);
        // 获取全部的省
        List<TOperatorCity> list = operatorCityService.list(new QueryWrapper<TOperatorCity>().eq("operatorId", id).eq("pid",0));
        List<OperatorCityVO> result = new ArrayList<>();
 
        for (TOperatorCity tOperatorCity : list) {
            // 拿到省下面的所有市
            List<TOperatorCity> cities= operatorCityService.list(new QueryWrapper<TOperatorCity>().eq("pid", tOperatorCity.getId()));
            if (cities.size()==0){
                OperatorCityVO operatorCityVO = new OperatorCityVO();
                operatorCityVO.setProvince(tOperatorCity.getName());
                operatorCityVO.setProvinceCode(tOperatorCity.getCode());
                result.add(operatorCityVO);
            }
            for (TOperatorCity city : cities) {
                OperatorCityVO operatorCityVO = new OperatorCityVO();
                operatorCityVO.setProvince(tOperatorCity.getName());
                operatorCityVO.setProvinceCode(tOperatorCity.getCode());
                operatorCityVO.setCity(city.getName());
                operatorCityVO.setCityCode(city.getCode());
                result.add(operatorCityVO);
            }
        }
        model.addAttribute("list",result);
        return PREFIX + "Operator_edit.html";
    }
    /**
     * 获取运营商列表
     */
    @RequestMapping(value = "/listAll")
    @ResponseBody
    public Object listAll(String userName, String phone,Integer type) {
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        List<Map<String,Object>> list =  operatorService.listAll(page,userName,phone,type);
        page.setRecords(list);
        return super.packForBT(page);
    }
 
    /**
     * 批量重置密码
     * @return
     */
    @RequestMapping(value = "/pwd")
    @ResponseBody
    public Object pwd(@RequestBody OperatorRestPwd pwd  ) {
        try {
            List<TOperator> list = operatorService.list(new QueryWrapper<TOperator>().in("id",pwd.getIds()));
            List<Integer> userIds = list.stream()
                    .map(TOperator::getUserId)
                    .collect(Collectors.toList());
            String a123456 = SecureUtil.md5("123456");
            List<User> users = userService.list(new QueryWrapper<User>().in("id", userIds));
            for (User user : users) {
                user.setPassword(a123456);
            }
            userService.updateBatchById(users);
            return SUCCESS_TIP;
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }
    }
    /**
     *  1为解冻 2为冻结
     *
     * @return
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(@RequestBody CoachChangeStateVO vo){
        operatorService.changeState(vo);
        return ResultUtil.success();
    }
    /**
     *  添加运营商
     */
    @ResponseBody
    @RequestMapping(value = "/addOperator")
    public ResultUtil addOperator(String name,String userName,String phone,Integer type ,@RequestParam String comArr) {
        User one = userService.getOne(new QueryWrapper<User>().eq("name", name).eq("phone", phone));
        if (one!=null){
            return ResultUtil.error("当前管理员名称和电话已存在!");
        }
        User user = new User();
        user.setName(userName);
        user.setPhone(phone);
        user.setObjectType(2);
        userService.save(user);
        TOperator data = new TOperator();
        data.setUserId(user.getId());
        data.setName(name);
        data.setType(type);
        data.setStatus(1);
        data.setState(1);
        operatorService.save(data);
        if (SinataUtil.isNotEmpty(comArr)){
            JSONArray jsonArray = JSON.parseArray(comArr);
            int size = jsonArray.size();
            for (int i = 0; i < size; i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                TOperatorCity province = new TOperatorCity();
                TOperatorCity city = new TOperatorCity();
                TOperatorCity one1 = operatorCityService.getOne(new QueryWrapper<TOperatorCity>()
                        .eq("name", jsonObject.getString("province"))
                        .eq("operatorId", data.getId()));
                // 省
                if (one1==null){
                    province.setName(jsonObject.getString("province"));
                    province.setCode(jsonObject.getInteger("provinceCode"));
                    province.setPid(0);
                    province.setType(jsonObject.getInteger("areaType"));
                    province.setOperatorId(data.getId());
                    city.setPid(province.getId());
                    operatorCityService.save(province);
                    // 市
                    if (!jsonObject.getString("city").equals("")){
                        city.setName(jsonObject.getString("city"));
                        city.setCode(jsonObject.getInteger("cityCode"));
                        city.setPid(province.getId());
                        city.setType(jsonObject.getInteger("areaType"));
                        city.setOperatorId(data.getId());
                        operatorCityService.save(city);
                    }
                }else{
                    // 市
                    if (!jsonObject.getString("city").equals("")){
                        city.setName(jsonObject.getString("city"));
                        city.setCode(jsonObject.getInteger("cityCode"));
                        city.setPid(one1.getId());
                        city.setType(jsonObject.getInteger("areaType"));
                        city.setOperatorId(data.getId());
                        operatorCityService.save(city);
                    }
                }
            }
        }
        return ResultUtil.success("添加成功");
    }
    /**
     *  添加运营商
     */
    @ResponseBody
    @RequestMapping(value = "/updateOperator")
    public ResultUtil updateOperator(Integer id,String name,String userName,String phone,Integer type ,@RequestParam String comArr) {
        User one = userService.getOne(new QueryWrapper<User>().eq("name", name).eq("phone", phone));
        if (one!=null){
            return ResultUtil.error("当前管理员名称和电话已存在!");
        }
        operatorCityService.remove(new QueryWrapper<TOperatorCity>().eq("operatorId",id));
        User user = new User();
        user.setName(userName);
        user.setPhone(phone);
        user.setObjectType(2);
        String a123456 = SecureUtil.md5("a123456");
        user.setPassword(a123456);
        userService.save(user);
        TOperator data = new TOperator();
        data.setId(id);
        data.setUserId(user.getId());
        data.setName(name);
        data.setType(type);
        data.setStatus(1);
        data.setState(1);
        operatorService.updateById(data);
        if (type != 1){
        if (SinataUtil.isNotEmpty(comArr)){
            JSONArray jsonArray = JSON.parseArray(comArr);
            int size = jsonArray.size();
            for (int i = 0; i < size; i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                TOperatorCity province = new TOperatorCity();
                TOperatorCity city = new TOperatorCity();
                TOperatorCity one1 = operatorCityService.getOne(new QueryWrapper<TOperatorCity>()
                        .eq("name", jsonObject.getString("province"))
                        .eq("operatorId", data.getId()));
                // 省
                if (one1==null){
                    province.setName(jsonObject.getString("province"));
                    province.setCode(jsonObject.getInteger("provinceCode"));
                    province.setPid(0);
                    province.setType(jsonObject.getInteger("areaType"));
                    province.setOperatorId(data.getId());
                    city.setPid(province.getId());
                    operatorCityService.save(province);
                    // 市
                    if (!jsonObject.getString("city").equals("")){
                        city.setName(jsonObject.getString("city"));
                        city.setCode(jsonObject.getInteger("cityCode"));
                        city.setPid(province.getId());
                        city.setType(jsonObject.getInteger("areaType"));
                        city.setOperatorId(data.getId());
                        operatorCityService.save(city);
                    }
                }else{
                    // 市
                    if (!jsonObject.getString("city").equals("")){
                        city.setName(jsonObject.getString("city"));
                        city.setCode(jsonObject.getInteger("cityCode"));
                        city.setPid(one1.getId());
                        city.setType(jsonObject.getInteger("areaType"));
                        city.setOperatorId(data.getId());
                        operatorCityService.save(city);
                    }
                }
            }
        }
        }
        return ResultUtil.success("添加成功");
    }
}