package com.yuanchu.mom.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.dto.DeviceMaintenancePlanDetailsDto;
import com.yuanchu.mom.dto.DeviceMaintenancePlanDto;
import com.yuanchu.mom.mapper.DeviceMaintenancePlanDetailsMapper;
import com.yuanchu.mom.mapper.DeviceMaintenancePlanMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.DeviceMaintenancePlan;
import com.yuanchu.mom.pojo.DeviceMaintenancePlanDetails;
import com.yuanchu.mom.pojo.User;
import com.yuanchu.mom.service.DeviceMaintenancePlanDetailsService;
import com.yuanchu.mom.service.DeviceMaintenancePlanService;
import com.yuanchu.mom.utils.HackLoopTableRenderPolicy;
import com.yuanchu.mom.utils.UserUtils;
import com.yuanchu.mom.utils.QueryWrappers;
import com.yuanchu.mom.vo.Result;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 设备保养计划表 服务实现类
*
*
* @author 江苏鵷雏网络科技有限公司
* @since 2024-12-16 06:10:52
*/
@Service
public class DeviceMaintenancePlanServiceImpl extends ServiceImpl implements DeviceMaintenancePlanService {
@Resource
private DeviceMaintenancePlanDetailsService deviceMaintenancePlanDetailsService;
@Resource
private DeviceMaintenancePlanDetailsMapper deviceMaintenancePlanDetailsMapper;
@Resource
private GetLook getLook;
@Resource
private UserMapper userMapper;
/**
* 分页查询设备保养计划
*
* @param page
* @return
*/
@Override
public Result> selectDeviceMaintenancePlanByPage(IPage page, DeviceMaintenancePlanDto deviceMaintenancePlanDto) {
IPage iPage = baseMapper.selectDeviceParameterPage(page, QueryWrappers.queryWrappers(deviceMaintenancePlanDto));
return Result.success(iPage);
}
/**
* 新增设备保养计划
*
* @param deviceMaintenancePlanDto 设备保养计划
*/
@Override
public Result addMaintenancePlan(DeviceMaintenancePlanDto deviceMaintenancePlanDto) {
Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
User user = userMapper.selectById(userId);
deviceMaintenancePlanDto.setCompilerId(userId);
deviceMaintenancePlanDto.setCompiler(user.getName());
deviceMaintenancePlanDto.setDatePreparation(LocalDateTime.now());
// 查询审核人id
if (deviceMaintenancePlanDto.getAuditId() != null) {
User auditUser = userMapper.selectById(deviceMaintenancePlanDto.getAuditId());
deviceMaintenancePlanDto.setAudit(auditUser.getName());
}
this.saveOrUpdate(deviceMaintenancePlanDto);
// 详情赋值并保存
List deviceMaintenancePlanDetails = deviceMaintenancePlanDto.getDeviceMaintenancePlanDetails();
if (CollectionUtils.isNotEmpty(deviceMaintenancePlanDetails)) { // 详情不为空
List collect = deviceMaintenancePlanDetails.stream().map(deviceMaintenancePlanDetail -> { // 遍历详情
deviceMaintenancePlanDetail.setDeviceId(deviceMaintenancePlanDto.getDeviceId()); // 设备ID
deviceMaintenancePlanDetail.setMaintenancePlanId(deviceMaintenancePlanDto.getMaintenancePlanId()); // 保养计划ID
DeviceMaintenancePlanDetails planDetails = new DeviceMaintenancePlanDetails();
BeanUtils.copyProperties(deviceMaintenancePlanDetail, planDetails);
return planDetails;
}).collect(Collectors.toList());
deviceMaintenancePlanDetailsService.saveBatch(collect);
}
return Result.success();
}
/**
* 修改设备保养计划
* @param deviceMaintenancePlanDto 设备保养计划
*/
@Override
public Result updateMaintenancePlan(DeviceMaintenancePlanDto deviceMaintenancePlanDto) {
// 查询审核人id
if (deviceMaintenancePlanDto.getAuditId() != null) {
User auditUser = userMapper.selectById(deviceMaintenancePlanDto.getAuditId());
deviceMaintenancePlanDto.setAudit(auditUser.getName());
}
this.saveOrUpdate(deviceMaintenancePlanDto);
// 删除原本的详情
deviceMaintenancePlanDetailsService.remove(Wrappers.lambdaQuery().eq(DeviceMaintenancePlanDetails::getMaintenancePlanId, deviceMaintenancePlanDto.getMaintenancePlanId()));
// 详情赋值并保存
List deviceMaintenancePlanDetails = deviceMaintenancePlanDto.getDeviceMaintenancePlanDetails();
if (CollectionUtils.isNotEmpty(deviceMaintenancePlanDetails)) { // 详情不为空
List collect = deviceMaintenancePlanDetails.stream().map(deviceMaintenancePlanDetail -> { // 遍历详情
deviceMaintenancePlanDetail.setDeviceId(deviceMaintenancePlanDto.getDeviceId()); // 设备ID
deviceMaintenancePlanDetail.setMaintenancePlanId(deviceMaintenancePlanDto.getMaintenancePlanId()); // 保养计划ID
DeviceMaintenancePlanDetails planDetails = new DeviceMaintenancePlanDetails();
BeanUtils.copyProperties(deviceMaintenancePlanDetail, planDetails);
return planDetails;
}).collect(Collectors.toList());
deviceMaintenancePlanDetailsService.saveBatch(collect);
}
return Result.success();
}
/**
* 删除设备保养计划
*
* @param deviceMaintenancePlanDto 设备保养计划
*/
@Override
public Result deleteMaintenancePlan(DeviceMaintenancePlanDto deviceMaintenancePlanDto) {
this.removeById(deviceMaintenancePlanDto);
deviceMaintenancePlanDetailsService.remove(Wrappers.lambdaQuery().eq(DeviceMaintenancePlanDetails::getMaintenancePlanId, deviceMaintenancePlanDto.getMaintenancePlanId()));
return Result.success();
}
/**
* 导出设备保养计划
*
* @param maintenancePlanId 设备保养计划id
* @param response 响应
*/
@Override
public Result exportDeviceMaintenancePlanDto(Integer maintenancePlanId, HttpServletResponse response) {
// 查询设备保养计划
DeviceMaintenancePlanDto deviceMaintenancePlan = baseMapper.selectMaintenancePlanById(maintenancePlanId);
// 查询设备保养计划详情
List deviceMaintenancePlanDetailsDtoList = deviceMaintenancePlanDetailsMapper.deviceInspectionRecordDetailsList(maintenancePlanId);
// 设置序号
deviceMaintenancePlanDetailsDtoList.forEach(deviceInspectionRecordDetails -> {
deviceInspectionRecordDetails.setIndex(deviceMaintenancePlanDetailsDtoList.indexOf(deviceInspectionRecordDetails) + 1);
});
// 获取路径
InputStream inputStream = this.getClass().getResourceAsStream("/static/word/maintenance-plan.docx");
Configure configure = Configure.builder()
.bind("deviceMaintenancePlanDetailsDtoList", new HackLoopTableRenderPolicy())
.build();
XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
new HashMap() {{
put("deviceMaintenancePlan", deviceMaintenancePlan);
put("deviceMaintenancePlanDetailsDtoList", deviceMaintenancePlanDetailsDtoList);
// 编制人签名地址
put("compilerUrl", UserUtils.getFinalUserSignatureUrl(deviceMaintenancePlan.getCompilerId()));
// 审核人签名地址
put("auditUrl", UserUtils.getFinalUserSignatureUrl(deviceMaintenancePlan.getAuditId()));
}});
try {
response.setContentType("application/msword");
String fileName = URLEncoder.encode(
"设备保养计划表", "UTF-8");
response.setHeader("Content-disposition",
"attachment;filename=" + fileName + ".docx");
OutputStream os = response.getOutputStream();
template.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("导出失败");
}
return Result.success();
}
/**
* 查询设备保养计划详情
*
* @param maintenancePlanId 设备保养计划id
*/
@Override
public Result getMaintenancePlanDetail(Integer maintenancePlanId) {
// 查询设备保养计划
DeviceMaintenancePlan deviceMaintenancePlan = baseMapper.selectById(maintenancePlanId);
// 查询详情
DeviceMaintenancePlanDto deviceMaintenancePlanDto = new DeviceMaintenancePlanDto();
BeanUtils.copyProperties(deviceMaintenancePlan, deviceMaintenancePlanDto);
deviceMaintenancePlanDto.setDeviceMaintenancePlanDetails(deviceMaintenancePlanDetailsMapper.deviceInspectionRecordDetailsList(maintenancePlanId));
return Result.success(deviceMaintenancePlanDto);
}
/**
* 审核设备保养计划
*
* @param deviceMaintenancePlanDto 设备保养计划
*/
@Override
public Result reviewMaintenancePlanStatus(DeviceMaintenancePlanDto deviceMaintenancePlanDto) {
LambdaUpdateWrapper wrapper = Wrappers.lambdaUpdate()
.eq(DeviceMaintenancePlan::getMaintenancePlanId, deviceMaintenancePlanDto.getMaintenancePlanId())
.set(DeviceMaintenancePlan::getStatus, deviceMaintenancePlanDto.getStatus()) // 审核状态
.set(DeviceMaintenancePlan::getAuditRemark, deviceMaintenancePlanDto.getAuditRemark());// 审核备注
// 为0清除审核人
if (deviceMaintenancePlanDto.getStatus().equals(0)) {
wrapper.set(DeviceMaintenancePlan::getAuditId, null)
.set(DeviceMaintenancePlan::getAudit, null);
}
this.update(wrapper); // 更新
return Result.success();
}
}