forked from kidgrow-microservices-platform

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
package com.kidgrow.usercenter.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kidgrow.common.annotation.LoginUser;
import com.kidgrow.common.controller.BaseController;
import com.kidgrow.common.model.PageResult;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.common.model.SysOrganization;
import com.kidgrow.common.model.SysUser;
import com.kidgrow.common.utils.AesUtils;
import com.kidgrow.common.utils.StringUtils;
import com.kidgrow.usercenter.model.SysDepartment;
import com.kidgrow.usercenter.service.ISysDepartmentService;
import com.kidgrow.usercenter.service.ISysOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: 科室表
 * @Project: 用户中心
 * @CreateDate: Created in 2020-04-02 18:32:36 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/sysdepartment")
@Api(tags = "科室表")
public class SysDepartmentController extends BaseController {
    @Autowired
    private ISysDepartmentService sysDepartmentService;
    @Autowired
    private ISysOrganizationService sysOrganizationService;
 
    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping
    public ResultBody<PageResult> list(@RequestParam Map<String, Object> params) {
        if (params.size() == 0) {
            params.put("page", 1);
            params.put("limit", 10);
        }
        return ResultBody.ok().data(sysDepartmentService.findList(params));
    }
 
    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @GetMapping("/{id}")
    public ResultBody findById(@PathVariable Long id) {
        SysDepartment model = sysDepartmentService.getById(id);
        return ResultBody.ok().data(model).msg("查询成功");
    }
 
    /**
     * 查询
     */
    @ApiOperation(value = "更新支付功能的开通状态")
    @GetMapping("/updatePay")
    public ResultBody updatePay(Long departmentId, Boolean isPay) {
        boolean isPays = sysDepartmentService.updatePay(departmentId, isPay);
        return ResultBody.ok().data(isPays).msg("操作成功");
    }
 
    /**
     * 查询
     */
    @ApiOperation(value = "查看支付功能的开通状态")
    @GetMapping("/getPayMes")
    public ResultBody getPayMes(Long departmentId) {
        SysDepartment model = sysDepartmentService.getById(departmentId);
        return ResultBody.ok().data(model.getIsPay()).msg("操作成功");
    }
 
    /**
     * 根据SysDepartment当做查询条件进行查询
     */
    @ApiOperation(value = "根据SysDepartment当做查询条件进行查询")
    @PostMapping("/query")
    public ResultBody findByObject(@RequestBody SysDepartment sysDepartment) {
        SysDepartment model = sysDepartmentService.findByObject(sysDepartment);
        return ResultBody.ok().data(model).msg("查询成功");
    }
 
    /**
     * 检查科室名是否存在
     */
    @ApiOperation(value = "检查科室名是否存在")
    @GetMapping("/checkName")
    public ResultBody checkDepName(Long hospitalId, String departmentName) {
        if (departMentNameIsUsed(hospitalId, departmentName)) {
            return ResultBody.failed().msg(String.format("该医院下已经存在科室 %s", departmentName));
        }
        return ResultBody.ok().msg("");
    }
 
    /**
     * 检查科室名是否存在
     *
     * @param hosId
     * @param departmentName
     * @return
     */
    private Boolean departMentNameIsUsed(Long hosId, String departmentName) {
        String departName = sysDepartmentService.checkDepartmentName(hosId, departmentName);
        return StringUtils.isNotBlank(departName);
    }
 
    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping("/{hosId}")
    @Transactional(rollbackFor = {Exception.class})
    public ResultBody save(@Valid @RequestBody SysDepartment sysDepartment, @PathVariable Long hosId, BindingResult bindingResult, @LoginUser SysUser sysUser) {
        List<String> errMsg = new ArrayList<>();
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                errMsg.add(error.getDefaultMessage());
            }
            return ResultBody.failed().msg(errMsg.toString());
        } else {
            //先检查该医院该科室是否已经存在
            if (sysDepartment.getId() == null) {
 
                if (departMentNameIsUsed(hosId, sysDepartment.getDepartmentName())) {
                    return ResultBody.failed().msg(String.format("该医院下已经存在科室 %s", sysDepartment.getDepartmentName()));
                }
            }
            //先检查是否存在组织信息
            SysOrganization sysOrganization = new SysOrganization();
            if (sysDepartment.getOrgId() != null) {
                sysOrganization.setId(sysDepartment.getOrgId());
            } else {
                sysOrganization.setOrgName(sysDepartment.getDepartmentName());
                //临时暂用其它字段承载数据
                sysOrganization.setOrgParentId(sysDepartment.getUpdateUserId());
            }
            sysOrganization = sysOrganizationService.findByObject(sysOrganization);
            if (sysOrganization == null) {
                //创建一个组织
                sysOrganization = new SysOrganization();
                sysOrganization.setOrgName(sysDepartment.getDepartmentName());
                sysOrganization.setOrgAttr(2);
                sysOrganization.setOrgLevel(2);
                //临时暂用其它字段承载数据
                sysOrganization.setOrgParentId(sysDepartment.getUpdateUserId());
                if (!sysUser.getOrganizations().isEmpty()) {
                    sysOrganization.setCreateUserOrgCode(sysUser.getOrganizations().get(1).getOrgCode());
                }
            } else {
                sysOrganization.setOrgName(sysDepartment.getDepartmentName());
            }
            boolean org = sysOrganizationService.saveOrUpdateSer(sysOrganization);
            sysDepartment.setOrgId(sysOrganization.getId());
            boolean v = sysDepartmentService.saveOrUpdate(sysDepartment);
            if (v) {
                return ResultBody.ok().data(sysDepartment).msg("保存成功");
            } else {
                return ResultBody.failed().msg("保存失败");
            }
        }
    }
 
    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public ResultBody delete(@PathVariable Long id) {
        boolean v = sysDepartmentService.removeById(id);
        if (v) {
            return ResultBody.ok().msg("删除成功");
        } else {
            return ResultBody.failed().msg("删除失败");
        }
    }
 
    /**
     * 根据医院获取  组织,通过组织,获取科室;
     *
     * @param params
     * @return
     */
    @GetMapping("findListByHospitalId")
    public ResultBody findListByHospitalId(@RequestParam Map<String, Object> params) {
        return sysDepartmentService.findListByHospitalId(params);
    }
 
    @PostMapping
    public ResultBody update(@RequestBody SysDepartment sysDepartment) {
        if (sysDepartment.getId() == null) {
            return ResultBody.failed("请输入id");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", sysDepartment.getId());
        boolean update = sysDepartmentService.update(sysDepartment, queryWrapper);
        if (update) {
            return ResultBody.ok();
        } else {
            return ResultBody.failed("更新数据失败");
        }
    }
 
    /**
     * 运动处方调用 是否开通服务
     */
 
    @PostMapping("getHealth")
    public ResultBody getHealth(@RequestBody SysDepartment sysDepartment) {
        if (sysDepartment.getId() == null) {
            return ResultBody.failed("请输入id");
        }
        return sysDepartmentService.getHealth(sysDepartment);
    }
 
    /**
     * 下载私有云服务器License文件
     *
     * @param departmentId 科室ID
     * @param response     HttpServletResponse
     * @return void
     */
    @PostMapping("/downloadLicense")
    public void downloadLicense(@RequestParam Long departmentId, HttpServletResponse response) {
 
        SysDepartment department = sysDepartmentService.getById(departmentId);
        String serverLicense = "";
 
        OutputStream out = null;
        InputStream fileInputStream = null;
        try {
            if (department != null) {
                if (department.getIsPrivate() == 1) {
                    if ((department.getPrivateServerGuuid() != null) && (!department.getPrivateServerGuuid().isEmpty())) {
                        serverLicense = AesUtils.encrypt(department.getPrivateServerGuuid());
 
                    } else {
                        serverLicense = String.format("此科室【%s】未设置服务器GUUID,不能下载私有云服务器License", departmentId);
                        log.error(serverLicense);
                    }
                } else {
                    serverLicense = String.format("此科室【%s】非私有云客户,不能下载私有云服务器License", departmentId);
                    log.error(serverLicense);
                }
            } else {
                serverLicense = String.format("此科室【%s】不存在,不能下载私有云服务器License", departmentId);
                log.error(serverLicense);
            }
 
 
 
 
            byte[] arr = serverLicense.getBytes();
 
            // 设置输出的格式
            response.reset();
            response.setContentType("application/x-msdownload");
            response.addHeader("Content-Length", "" + arr.length);
            response.addHeader("Content-Disposition", "attachment; filename=\"" + department.getDepartmentName() + "License.txt\"");
 
 
            out = response.getOutputStream();
            fileInputStream = new ByteArrayInputStream(arr);
            byte[] buffer = new byte[1024 * 10];
            int len = 0;
            while ((len = fileInputStream.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //log.error("私有云服务器Guuid加密出错");
        } finally {
 
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    /**
     * 下载私有云本地数据库初始化数据
     * @param hospitalId
     * @param departmentId
     * @param response
     * @return void
     */
    @PostMapping("/downloadData")
    public void downloadPrivateData(@RequestParam("hospitalId") Long hospitalId,@RequestParam("departmentId") Long departmentId, HttpServletResponse response){
        String dataSql = "";
 
        dataSql = sysDepartmentService.initPrivateData(hospitalId,departmentId);
 
        OutputStream out = null;
        InputStream fileInputStream = null;
        try {
            byte[] arr = dataSql.getBytes();
 
            // 设置输出的格式
            response.reset();
            response.setContentType("application/x-msdownload");
            response.addHeader("Content-Length", "" + arr.length);
            response.addHeader("Content-Disposition", "attachment; filename=\"" + departmentId.toString() + ".sql\"");
 
 
            out = response.getOutputStream();
            fileInputStream = new ByteArrayInputStream(arr);
            byte[] buffer = new byte[1024 * 10];
            int len = 0;
            while ((len = fileInputStream.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
 
        } finally {
 
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
    }
 
}