zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
package org.jeecg.modules.lims.controller;
 
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.lims.api.JeecgSystemClient;
import org.jeecg.modules.lims.config.WxProperties;
import org.jeecg.modules.lims.entity.*;
import org.jeecg.modules.lims.service.*;
import org.jeecg.modules.lims.vo.LimsAppointmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @Description: 预约
 * @Author: jeecg-boot
 * @Date: 2022-11-07
 * @Version: V1.0
 */
@Api(tags = "预约")
@RestController
@RequestMapping("/limsAppointment")
@Slf4j
public class LimsAppointmentController extends JeecgController<LimsAppointment, ILimsAppointmentService> {
    @Autowired
    private ILimsAppointmentService limsAppointmentService;
    @Autowired
    private ILimsInstrumentService limsInstrumentService;
 
    @Autowired
    private JeecgSystemClient jeecgSystemClient;
 
    @Autowired
    private ILimsAppointmentOptionService optionService;
 
    @Autowired
    private ILimsAppointmentAuditService auditService;
 
 
    @Autowired
    private IWxService wxService;
 
 
    /**
     * 分页列表查询
     *
     * @param limsAppointment
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "预约-分页列表查询")
    @ApiOperation(value = "预约-分页列表查询", notes = "预约-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(LimsAppointment limsAppointment,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) throws InvocationTargetException, IllegalAccessException {
        QueryWrapper<LimsAppointment> queryWrapper = QueryGenerator.initQueryWrapper(limsAppointment, req.getParameterMap());
        Page<LimsAppointment> page = new Page<LimsAppointment>(pageNo, pageSize);
        IPage<LimsAppointment> pageList = limsAppointmentService.page(page, queryWrapper);
 
        try {
            //查询用户真实姓名
            pageList.getRecords().forEach(item -> {
                SysUser user = jeecgSystemClient.queryByUserName(item.getCreateBy()).getResult();
                SysUser updateUser = jeecgSystemClient.queryByUserName(item.getUpdateBy()).getResult();
                List<DepartIdModel> departs = jeecgSystemClient.getUserDepartsListByName(item.getCreateBy()).getResult();
                if (departs != null && departs.size() > 0) {
                    String depart = departs.get(0).getTitle();
                    item.setDepart(depart);
                }
 
                if (user != null)
                    item.setCreateBy(user.getRealname());
                if (updateUser != null)
                    item.setUpdateBy(updateUser.getRealname());
 
 
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        Set<String> collect = pageList.getRecords().stream().map(LimsAppointment::getInstrument).collect(Collectors.toSet());
        Map<String, LimsInstrument> map = new HashMap<>();
        if (collect != null && collect.size() > 0) {
            LambdaQueryWrapper<LimsInstrument> in = new LambdaQueryWrapper<LimsInstrument>().in(LimsInstrument::getId, collect);
            List<LimsInstrument> list = limsInstrumentService.list(in);
            map = list.stream().collect(Collectors.toMap(LimsInstrument::getId, item -> item));
 
        }
 
//        Set<String> createUsers = pageList.getRecords().stream().map(LimsAppointment::getCreateBy).collect(Collectors.toSet());
//        Set<String> updateUsers = pageList.getRecords().stream().map(LimsAppointment::getUpdateBy).collect(Collectors.toSet());
//        createUsers.addAll(updateUsers);
//        Map<String, String> userMap = new HashMap<>();
//        if (createUsers != null && createUsers.size() > 0) {
//            Result<Collection<SysUser>> collectionResult = jeecgSystemClient.queryByIds(StringUtils.join(createUsers, ","));
//            userMap = collectionResult.getResult().stream().collect(Collectors.toMap(SysUser::getUsername, SysUser::getRealname));
//        }
 
 
        Page<LimsAppointmentVo> page1 = new Page<>();
        List<LimsAppointmentVo> listVo = new ArrayList<>();
        BeanUtils.copyProperties(page1, page);
        for (LimsAppointment appointment : pageList.getRecords()) {
            LimsAppointmentVo vo = new LimsAppointmentVo();
            try {
                BeanUtils.copyProperties(vo, appointment);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
            vo.setInstrumentObj(map.get(vo.getInstrument()));
//            vo.setCreateName(userMap.get(vo.getCreateBy()));
//            vo.setUpdateName(userMap.get(vo.getUpdateBy()));
            listVo.add(vo);
        }
 
        page1.setRecords(listVo);
 
        page1.setTotal(pageList.getTotal());
        return Result.OK(page1);
    }
 
    /**
     * 添加
     *
     * @param limsAppointment
     * @return
     */
    @AutoLog(value = "预约-添加")
    @ApiOperation(value = "预约-添加", notes = "预约-添加")
    @PostMapping(value = "/add")
    @Transactional
    public Result<?> add(@RequestBody LimsAppointment limsAppointment) throws ParseException {
 
        if (limsAppointment.getOptionid() != null) {
            if (limsAppointment.getStime() == null) {
                return Result.error("预约日期不能为空");
            }
            if (limsAppointment.getStatus() == null) {
                return Result.error("预约状态不能为空");
            }
            // 2.查询已预约时间段
            Map<String, LimsAppointment> map = limsAppointmentService.listInsAppByDate(limsAppointment.getInstrument(), limsAppointment.getStime());
            String date = DateUtils.formatDate(limsAppointment.getStime(), "yyyy-MM-dd");
            String[] strings = limsAppointment.getOptionid().split(",");
            if (strings == null || strings.length < 1) {
                return Result.error("预约参数错误");
            }
            LambdaQueryWrapper<LimsAppointmentOption> optionsQuery = new LambdaQueryWrapper<>();
            optionsQuery.in(LimsAppointmentOption::getId, strings);
            optionsQuery.orderByAsc(LimsAppointmentOption::getSort);
            List<LimsAppointmentOption> list = optionService.list(optionsQuery);
            int size = list.size();
 
            // 待添加预约列表
            List<LimsAppointment> addList = new ArrayList<>();
            int i = 0;
            while (i < size) {
                int si = i;
                // 获取最后一个连续时间的下标
                i = getI(list, i, map);
                int ei = i;
                LimsAppointment la = new LimsAppointment();
                String ids = "";
                for (int j = si; j <= ei; j++) {
                    ids += list.get(j).getId() + ",";
                }
                la.setName(list.get(si).getStime().toString().substring(0, 5) + "-" + list.get(ei).getEtime().toString().substring(0, 5));
                la.setOptionid(ids);
                la.setInstrument(limsAppointment.getInstrument());
                la.setStime(DateUtils.parseDate(date + list.get(si).getStime().toString(), "yyyy-MM-ddHH:mm:ss"));
                la.setRemark(limsAppointment.getRemark());
                la.setStatus(limsAppointment.getStatus());
                la.setEtime(DateUtils.parseDate(date + list.get(ei).getEtime().toString(), "yyyy-MM-ddHH:mm:ss"));
                addList.add(la);
                i += 1;
            }
 
            limsAppointmentService.saveBatch(addList);
 
 
            //长时间预约时需要添加一条审批记录 status == '5'
            if (limsAppointment.getStatus().equals("5") && addList.size() == 1) {
                LimsAppointmentAudit audit = new LimsAppointmentAudit();
                audit.setAppoId(addList.get(0).getId());
                audit.setStatus("0"); //设置为待审批
                audit.setDescription(limsAppointment.getRemark());
                auditService.save(audit);
 
                Map<String, String> params = new HashMap<>();
                LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                List<DepartIdModel> departs = jeecgSystemClient.getUserDepartsListByName(loginUser.getUsername()).getResult();
                if (departs != null && departs.size() > 0) {
                    String depart = departs.get(0).getTitle();
                    params.put("depart", depart);
                } else {
                    params.put("depart", "未设置部门");
                }
                LimsInstrument instrument = limsInstrumentService.getById(limsAppointment.getInstrument());
                params.put("realname", loginUser.getRealname());
                params.put("username", loginUser.getUsername());
                params.put("time", size + "小时");
                params.put("equ", instrument.getName());
 
                try {
                    wxService.sendMessage(params, audit);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException("微信消息推送异常");
                }
 
            }
 
 
//            for (String str: strings) {
//                if (map.get(str) != null) {
//                    throw new RuntimeException("预约失败,请刷新后重新提交");
//                }
//                LimsAppointmentOption option = optionService.getById(str);
//                LimsAppointment appointment = new LimsAppointment();
//                appointment.setOptionid(str);
//                appointment.setInstrument(limsAppointment.getInstrument());
//                appointment.setStime( DateUtils.parseDate(date + option.getStime().toString(),"yyyy-MM-ddHH:mm:ss"));
//                appointment.setEtime( DateUtils.parseDate(date + option.getEtime().toString(),"yyyy-MM-ddHH:mm:ss"));
//                appointment.setStatus("0");
//                limsAppointmentService.save(appointment);
//            }
        } else {
            return Result.error("参数错误");
        }
        return Result.OK("添加成功!");
    }
 
    // 返回列表中最后给定下标的最后一个连续项下标
    private int getI(List<LimsAppointmentOption> list, int i, Map<String, LimsAppointment> map) {
        if (map.get(list.get(i).getId()) != null) {
            throw new RuntimeException("预约失败,请刷新后重新提交");
        }
        if (i + 1 < list.size() && list.get(i).getEtime().equals(list.get(i + 1).getStime())) {
            i = getI(list, i + 1, map);
        }
        return i;
    }
 
    /**
     * 编辑
     *
     * @param limsAppointment
     * @return
     */
    @AutoLog(value = "预约-编辑")
    @ApiOperation(value = "预约-编辑", notes = "预约-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody LimsAppointment limsAppointment) {
        limsAppointmentService.updateById(limsAppointment);
        return Result.OK("编辑成功!");
    }
 
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "预约-通过id删除")
    @ApiOperation(value = "预约-通过id删除", notes = "预约-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        limsAppointmentService.removeById(id);
        return Result.OK("删除成功!");
    }
 
    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "预约-批量删除")
    @ApiOperation(value = "预约-批量删除", notes = "预约-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.limsAppointmentService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
 
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "预约-通过id查询")
    @ApiOperation(value = "预约-通过id查询", notes = "预约-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        LimsAppointment limsAppointment = limsAppointmentService.getById(id);
        if (limsAppointment == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(limsAppointment);
    }
 
    /**
     * 导出excel
     *
     * @param request
     * @param limsAppointment
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LimsAppointment limsAppointment) {
        return super.exportXls(request, limsAppointment, LimsAppointment.class, "预约");
    }
 
    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LimsAppointment.class);
    }
 
    /**
     * 通过设备id获取设备预约详情
     * 用于预约时展示和查看当天预约情况
     * 传入设备ID和要查询的日期时间
     */
    @ApiOperation(value = "查询设备预约详情", notes = "查询设备预约详情")
    @GetMapping("/appointmentDetail")
    public Result<?> appointmentDetail(LimsAppointment param) {
        // 1.查询预约字典项
        List<LimsAppointmentOption> options = optionService.list();
        // 2.查询已预约时间段
        Map<String, LimsAppointment> map = limsAppointmentService.listInsAppByDate(param.getInstrument(), param.getStime());
 
        // 3.组装到一块
        List<LimsAppointmentOption> collect = options.stream().map(item -> {
            if (map.get(item.getId()) != null) {
                item.setUsed(1);
                item.setStatus(map.get(item.getId()).getStatus());
                item.setUserName(map.get(item.getId()).getCreateBy());
                SysUser user = jeecgSystemClient.queryByUserName(item.getUserName()).getResult();
                if (user != null)
                    item.setRealName(user.getRealname());
 
                List<DepartIdModel> departs = jeecgSystemClient.getUserDepartsListByName(item.getUserName()).getResult();
                if (departs != null && departs.size() > 0) {
                    String depart = departs.get(0).getTitle();
                    item.setDepart(depart);
                }
            }
            return item;
        }).collect(Collectors.toList());
        return Result.OK(collect);
    }
 
}