package com.zd.security.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zd.common.core.annotation.Log;
import com.zd.common.core.annotation.PreAuthorize;
import com.zd.common.core.log.BusinessType;
import com.zd.common.core.utils.FileConfigUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.core.web.controller.AbstractController;
import com.zd.laboratory.api.entity.LabSubjectEntity;
import com.zd.laboratory.api.feign.RemoteSubQueryService;
import com.zd.model.domain.R;
import com.zd.model.domain.ResultData;
import com.zd.model.domain.per.PerFun;
import com.zd.model.domain.per.PerPrefix;
import com.zd.security.api.bo.CheckClapBo;
import com.zd.security.api.dto.UploadDto;
import com.zd.security.api.vo.CheckClapVo;
import com.zd.security.entity.CheckClap;
import com.zd.security.entity.CheckStaffUser;
import com.zd.security.entity.Upload;
import com.zd.security.scope.DataPermission;
import com.zd.security.scope.DataPermissionAspect;
import com.zd.security.service.CheckClapService;
import com.zd.security.service.CheckStaffUserService;
import com.zd.security.service.UploadService;
import com.zd.system.api.feign.RemoteUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
*
* 随手拍 前端控制器
*
*
* @author cyl
* @since 2023-04-19
*/
@RestController
@RequestMapping("/checkClap")
public class CheckClapController extends AbstractController {
@Autowired
private CheckClapService checkClapService;
@Autowired
private RemoteUserService remoteUserService;
@Autowired
private RemoteSubQueryService remoteSubQueryService;
@Autowired
private UploadService uploadService;
@Autowired
private FileConfigUtils fileConfigUtils;
@Autowired
private CheckStaffUserService checkStaffUserService;
/**
* 获取随手拍数据列表list
* @param checkClapBo
* @return
*/
// @DataPermission(tableAlias = "", permissionStr = PerPrefix.SECURITY_CLAP)
@DataPermission(roleKey="rectifyGentle")
@PreAuthorize(hasPermi = PerPrefix.SECURITY_CLAP + PerFun.LIST)
@GetMapping(value = "/list")
public ResultData list(CheckClapBo checkClapBo) {
//参数检查
List userList = DataPermissionAspect.threadLocal.get(); // 获取数据
checkClapBo.setUserList(userList);
DataPermissionAspect.threadLocal.remove();
Page page = checkClapService.selCheckClapList(checkClapBo);
return ResultData.success(page);
}
/**
* 获取随手拍数据列表list (小程序接口)
* @param checkClapBo
* @return
*/
@DataPermission(roleKey="rectifyGentle")
@GetMapping(value = "/appList")
public ResultData appList(CheckClapBo checkClapBo) {
//参数检查
List userList = DataPermissionAspect.threadLocal.get(); // 获取数据
checkClapBo.setUserList(userList);
DataPermissionAspect.threadLocal.remove();
checkClapBo.setMyRelated(1);
Page page = checkClapService.selCheckClapList(checkClapBo);
Optional.ofNullable(page.getRecords()).orElseGet(Collections::emptyList)
.stream()
.forEach(a->{
timeProcessing(a);
});
return ResultData.success(page);
}
public void timeProcessing(CheckClapVo a){
long nowTime = new Date().getTime()-1800000;
long timestamp = a.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
if(nowTime<=timestamp){
a.setTimeFlag("刚刚");
}else{
a.setTimeFlag(a.getCreateTime().get(ChronoField.HOUR_OF_DAY)+":"+a.getCreateTime().get(ChronoField.MINUTE_OF_HOUR));
long nowTime3 = new Date().getTime()-86400000;
long timestamp3 = a.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
if(nowTime3>=timestamp3){
//获取本条数据的年月日时间
String month3 = (a.getCreateTime().get(ChronoField.MONTH_OF_YEAR)+"").length()==1?"0"+a.getCreateTime().get(ChronoField.MONTH_OF_YEAR):a.getCreateTime().get(ChronoField.MONTH_OF_YEAR)+"";
a.setTimeFlag(a.getCreateTime().get(ChronoField.YEAR)+"-"+month3+"-"+a.getCreateTime().get(ChronoField.DAY_OF_MONTH));
}
//昨天的时间计算
recentTime(a,-1);
//前天的时间计算
recentTime(a,-2);
}
}
public void recentTime(CheckClapVo a,Integer time){
//time 是-1 就减一天,如果是-2就减两天
//昨天的时间计算
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");//设置格式
Calendar calendar1 = Calendar.getInstance(); //创建Calendar 的实例
calendar1.add(Calendar.DAY_OF_MONTH, time); //当前时间减去一天,即一天前的时间
String nowTime1 = simpleDateFormat1.format(calendar1.getTime());
//获取本条数据的年月日时间
String month1 = (a.getCreateTime().get(ChronoField.MONTH_OF_YEAR)+"").length()==1?"0"+a.getCreateTime().get(ChronoField.MONTH_OF_YEAR):a.getCreateTime().get(ChronoField.MONTH_OF_YEAR)+"";
String timestamp1 = a.getCreateTime().get(ChronoField.YEAR)+"-"+month1+"-"+a.getCreateTime().get(ChronoField.DAY_OF_MONTH);
if(nowTime1.equals(timestamp1)){
if(time.intValue()==-1){
a.setTimeFlag("昨天");
}
if(time.intValue()==-2){
a.setTimeFlag("前天");
}
}
}
/**
* 获取随手拍上报记录数据列表list
* @param checkClapBo
* @return
*/
@GetMapping(value = "/mylist")
public ResultData mylist(CheckClapBo checkClapBo) {
//查询条件
QueryWrapper wrapperClap = new QueryWrapper <>();
//整改状态
if(StringUtils.isNotNull(checkClapBo.getRectifyStatus())){
wrapperClap.lambda().eq(CheckClap::getRectifyStatus,checkClapBo.getRectifyStatus());
}
if(StringUtils.isNotNull(checkClapBo.getSearchValue())){
wrapperClap.lambda().and(wrapper->wrapper.like(CheckClap::getSubName,checkClapBo.getSearchValue())
.or().like(CheckClap::getRoomNum,checkClapBo.getSearchValue()).or().like(CheckClap::getCreateName,checkClapBo.getSearchValue()));
}
if(StringUtils.isNotNull(checkClapBo.getDeptId())){
wrapperClap.lambda().eq(CheckClap::getDeptId,checkClapBo.getDeptId());
}
wrapperClap.ge(StringUtils.isNotEmpty(checkClapBo.getBeginTime()),"create_time",checkClapBo.getBeginTime())
.le(StringUtils.isNotEmpty(checkClapBo.getEndTime()),"create_time",checkClapBo.getEndTime());
//过滤物理删除数据及倒叙
if(StringUtils.isNotNull(checkClapBo.getUpTimeOrder()) && 1==checkClapBo.getUpTimeOrder().intValue()){
wrapperClap.lambda().orderByAsc(CheckClap::getCreateTime);
}else if(StringUtils.isNotNull(checkClapBo.getUpTimeOrder()) && 2==checkClapBo.getUpTimeOrder().intValue()){
wrapperClap.lambda().orderByDesc(CheckClap::getCreateTime);
}else if(StringUtils.isNotNull(checkClapBo.getZgTimeOrder()) && 1==checkClapBo.getZgTimeOrder().intValue()){
wrapperClap.lambda().orderByAsc(CheckClap::getRectifyTime);
}else if(StringUtils.isNotNull(checkClapBo.getZgTimeOrder()) && 2==checkClapBo.getZgTimeOrder().intValue()){
wrapperClap.lambda().orderByDesc(CheckClap::getRectifyTime);
}else{
wrapperClap.lambda().orderByDesc(CheckClap::getId);
}
wrapperClap.lambda().eq(CheckClap::getIsDeleted, Boolean.FALSE).eq(CheckClap::getCreateBy,SecurityUtils.getUserId());
Page page = checkClapService.page(new Page<>(checkClapBo.getPageNum(), checkClapBo.getPageSize()), wrapperClap);
List checkClapVoList = com.zd.common.core.utils.BeanUtils.copyList2List(page.getRecords(), CheckClapVo.class);
BeanUtils.copyProperties(page.getRecords(),checkClapVoList);
Page pageVo = new Page <>();
BeanUtils.copyProperties(page,pageVo);
pageVo.setRecords(checkClapVoList);
Optional.ofNullable(pageVo.getRecords()).orElseGet(Collections::emptyList)
.stream()
.forEach(a->{
timeProcessing(a);
});
return ResultData.success(pageVo);
}
/**
* 添加数据
* @param checkClapBo
* @return
*/
@PreAuthorize(hasPermi = PerPrefix.SECURITY_CLAP + PerFun.ADD)
@Log(title = "安全服务-随手拍添加", businessType = BusinessType.INSERT)
@PostMapping(value = "/add")
public ResultData add(@RequestBody CheckClapBo checkClapBo) {
//参数检查
paramCheck.notNull(checkClapBo)
.notNull(checkClapBo.getDeptId())
.notNull(checkClapBo.getSubId())
.notNull(checkClapBo.getImgDtoList());
//请求实验室的服务接口,实验室的安全责任人用户id
List subIds = new ArrayList <>();
subIds.add(checkClapBo.getSubId());
R > subList = remoteSubQueryService.listByIds(subIds);
StringBuilder userNames = new StringBuilder();
Long[] adminInfo = new Long[2];
if(subList.getCode()==200 && subList.getData().size()>0){
adminInfo[0] = subList.getData().get(0).getId();
adminInfo[1] = subList.getData().get(0).getAdminId();
String[] safeUserIds = subList.getData().get(0).getSafeUserId().split(",");
for(int i=0;i> userInfo = remoteUserService.getUserMapInfoByUserId(Long.parseLong(safeUserIds[i]));
if(userInfo.getCode()==200){
userNames.append(",");
userNames.append(userInfo.getData().get("userName"));
}
}
}
}
CheckClap checkClap = new CheckClap();
BeanUtils.copyProperties(checkClapBo,checkClap);
checkClap.setRoomNum(subList.getData().get(0).getRoom());
if(userNames.length()>0){
checkClap.setSafetyPeople(userNames.substring(1));
}
//这里需要先去根据实验室id,查询检查人员配置的整改人员信息,如果有,就是查询回来的这个人整改,如果没有,就默认当前实验室的负责人
checkClap.setCanRectifyIds(rectUserInfo(adminInfo));
checkClap.setIsDeleted(Boolean.FALSE);
checkClap.setCreateName(SecurityUtils.getNickName());
if(checkClapService.save(checkClap)) {
//保存成功后,需要调用上传图片的功能
Optional.ofNullable(checkClapBo.getImgDtoList()).orElseGet(Collections::emptyList)
.stream()
.forEach(a->{
a.setKeyId(checkClap.getId());
a.setFileType(2);
a.setSource(4);
uploadService.addUpload(a);
});
return ResultData.success("操作成功");
}
return ResultData.fail("操作失败");
}
/**
* 添加数据(小程序接口)
* @param checkClapBo
* @return
*/
@Log(title = "安全服务-随手拍添加", businessType = BusinessType.INSERT)
@PostMapping(value = "/appAdd")
public ResultData appAdd(@RequestBody CheckClapBo checkClapBo) {
//参数检查
paramCheck.notNull(checkClapBo)
.notNull(checkClapBo.getDeptId())
.notNull(checkClapBo.getSubId())
.notNull(checkClapBo.getImgDtoList());
//请求实验室的服务接口,实验室的安全责任人用户id
List subIds = new ArrayList <>();
subIds.add(checkClapBo.getSubId());
R > subList = remoteSubQueryService.listByIds(subIds);
StringBuilder userNames = new StringBuilder();
Long[] adminInfo = new Long[2];
if(subList.getCode()==200 && subList.getData().size()>0){
adminInfo[0] = subList.getData().get(0).getId();
adminInfo[1] = subList.getData().get(0).getAdminId();
String[] safeUserIds = subList.getData().get(0).getSafeUserId().split(",");
for(int i=0;i> userInfo = remoteUserService.getUserMapInfoByUserId(Long.parseLong(safeUserIds[i]));
if(userInfo.getCode()==200){
userNames.append(",");
userNames.append(userInfo.getData().get("userName"));
}
}
}
}
CheckClap checkClap = new CheckClap();
BeanUtils.copyProperties(checkClapBo,checkClap);
checkClap.setRoomNum(subList.getData().get(0).getRoom());
if(userNames.length()>0){
checkClap.setSafetyPeople(userNames.substring(1));
}
//这里需要先去根据实验室id,查询检查人员配置的整改人员信息,如果有,就是查询回来的这个人整改,如果没有,就默认当前实验室的负责人
checkClap.setCanRectifyIds(rectUserInfo(adminInfo));
checkClap.setIsDeleted(Boolean.FALSE);
checkClap.setCreateName(SecurityUtils.getNickName());
if(checkClapService.save(checkClap)) {
//保存成功后,需要调用上传图片的功能
Optional.ofNullable(checkClapBo.getImgDtoList()).orElseGet(Collections::emptyList)
.stream()
.forEach(a->{
a.setKeyId(checkClap.getId());
a.setFileType(2);
a.setSource(4);
uploadService.addUpload(a);
});
return ResultData.success("操作成功");
}
return ResultData.fail("操作失败");
}
public String rectUserInfo(Long[] adminInfo){
//查询条件
QueryWrapper wrapperUser = new QueryWrapper <>();
//过滤物理删除数据及正序叙
wrapperUser.lambda().eq(CheckStaffUser::getIsDeleted, Boolean.FALSE).eq(CheckStaffUser::getStaffType,2)
.eq(CheckStaffUser::getSubId,adminInfo[0]);
List userList = checkStaffUserService.list(wrapperUser);
StringBuilder userIds = new StringBuilder();
if(userList!=null && !userList.isEmpty()){
Optional.ofNullable(userList).orElseGet(Collections::emptyList)
.stream()
.forEach(a->{
userIds.append(",");
userIds.append(a.getUserId());
});
}else{
userIds.append(adminInfo[1]);
}
if(userIds.length()>0){
return userIds.substring(1);
}
return "";
}
/**
* 通过ID获取
* @param id
* @return
*/
@PreAuthorize(hasPermi = PerPrefix.SECURITY_CLAP + PerFun.QUERY)
@GetMapping(value = "/findById")
public ResultData find(Long id) {
CheckClap checkClap = checkClapService.getById(id);
CheckClapVo checkClapVo = new CheckClapVo();
BeanUtils.copyProperties(checkClap,checkClapVo);
checkClapVo.setYhDtoList(funDto(checkClapVo.getId(),4));
checkClapVo.setZgDtoList(funDto(checkClapVo.getId(),5));
//这里需要校验一下,查看的时候,是否有身份整改当前的这条数据。
Predicate predicate = a->Arrays.asList(a.split(",")).stream().filter(b->b.equals(getCurrentUserId()+"")).count()>0;
if(StringUtils.isNotNull(checkClap.getCanRectifyIds()) && predicate.test(checkClap.getCanRectifyIds())){
checkClapVo.setRectifyGentle(Boolean.TRUE);
}else{
checkClapVo.setRectifyGentle(Boolean.FALSE);
}
return ResultData.success(checkClapVo);
}
/**
* 通过ID获取(小程序接口)
* @param id
* @return
*/
@GetMapping(value = "/appFindById")
public ResultData appFindById(Long id) {
CheckClap checkClap = checkClapService.getById(id);
CheckClapVo checkClapVo = new CheckClapVo();
BeanUtils.copyProperties(checkClap,checkClapVo);
checkClapVo.setYhDtoList(funDto(checkClapVo.getId(),4));
checkClapVo.setZgDtoList(funDto(checkClapVo.getId(),5));
//这里需要校验一下,查看的时候,是否有身份整改当前的这条数据。
Predicate predicate = a->Arrays.asList(a.split(",")).stream().filter(b->b.equals(getCurrentUserId()+"")).count()>0;
if(StringUtils.isNotNull(checkClap.getCanRectifyIds()) && predicate.test(checkClap.getCanRectifyIds())){
checkClapVo.setRectifyGentle(Boolean.TRUE);
}else{
checkClapVo.setRectifyGentle(Boolean.FALSE);
}
return ResultData.success(checkClapVo);
}
public List funDto(Long id,Integer source){
Function ,List> function = fun->Optional.ofNullable(fun).orElseGet(Collections::emptyList)
.stream()
.map(a->{
UploadDto uploadDto = new UploadDto();
BeanUtils.copyProperties(a,uploadDto);
uploadDto.setFileUrl(uploadDto.getFileUrl());
uploadDto.setViewFileUrl(fileConfigUtils.getFileDomainAdmin()+uploadDto.getFileUrl());
return uploadDto;
}).collect(Collectors.toList());
QueryWrapper uploadWrap = new QueryWrapper <>();
uploadWrap.lambda().eq(Upload::getSource,source).eq(Upload::getKeyId,id);
List uploadList = uploadService.list(uploadWrap);
return function.apply(uploadList);
}
@Log(title = "安全服务-随手拍审核", businessType = BusinessType.UPDATE)
@PostMapping(value = "/clapApprove")
public ResultData update(@RequestBody CheckClapBo checkClapBo) {
//参数检查
paramCheck.notNull(checkClapBo).notNull(checkClapBo.getRectifyStatus());
CheckClap checkClap = new CheckClap();
BeanUtils.copyProperties(checkClapBo,checkClap);
checkClap.setRectifyTime(LocalDateTime.now());
checkClap.setRectifyId(getCurrentUserId());
checkClap.setRectifyPeople(SecurityUtils.getNickName());
if(checkClapService.saveOrUpdate(checkClap)) {
//这里需要上传整改或无法整改照片。稍后补充
Optional.ofNullable(checkClapBo.getImgDtoList()).orElseGet(Collections::emptyList)
.stream()
.forEach(a->{
a.setKeyId(checkClap.getId());
a.setFileType(2);
a.setSource(5);
uploadService.addUpload(a);
});
return ResultData.success("操作成功");
}
return ResultData.fail("操作失败");
}
}