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);
|
}
|
|
}
|