package com.qianwen.smartman.modules.workinghour.service; import java.time.Duration; import java.util.List; import java.util.stream.Collectors; import org.apache.commons.beanutils.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.qianwen.core.excel.util.ExcelUtil; import com.qianwen.core.oss.model.BladeFile; import com.qianwen.core.tool.utils.DateUtil; import com.qianwen.smartman.common.enums.DefaultWcsEnum; import com.qianwen.smartman.common.utils.DurationUtil; import com.qianwen.smartman.modules.resource.builder.oss.OssBuilder; import com.qianwen.smartman.modules.smis.entity.Workstation; import com.qianwen.smartman.modules.smis.excel.WorkstationExcel; import com.qianwen.smartman.modules.smis.mapper.WorkstationMapper; import com.qianwen.smartman.modules.workinghour.convert.PartWorkingProcessConvert; import com.qianwen.smartman.modules.workinghour.entity.PartWorkingHour; import com.qianwen.smartman.modules.workinghour.entity.PartWorkingProcess; import com.qianwen.smartman.modules.workinghour.excel.PartWorkingProcessExcel; import com.qianwen.smartman.modules.workinghour.mapper.PartWorkingHourMapper; import com.qianwen.smartman.modules.workinghour.mapper.PartWorkingProcessMapper; import com.qianwen.smartman.modules.workinghour.vo.PartWorkingProcessVO; @Service public class PartWorkingProcessService{ @Autowired private PartWorkingProcessMapper partWorkingProcessMapper; @Autowired private WorkstationMapper workstationMapper; @Autowired private PartWorkingHourMapper partWorkingHourMapper; @Autowired private OssBuilder ossBuilder; @Transactional(readOnly=true) public List listByWorkinghourId(Long workinghourId) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().eq(PartWorkingProcess::getWorkinghourId, workinghourId).orderByAsc(PartWorkingProcess::getStartTime); return PartWorkingProcessConvert.INSTANCE.convert(partWorkingProcessMapper.selectList(wrapper)); } /** * 加工过程数据导出 * @param id * @return */ @Transactional(readOnly=true) public BladeFile export(long workinghourId) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().eq(PartWorkingProcess::getWorkinghourId, workinghourId).orderByAsc(PartWorkingProcess::getStartTime); List data = partWorkingProcessMapper.selectList(wrapper); List exportList = data.stream().map(p -> { PartWorkingProcessExcel e = new PartWorkingProcessExcel(); e.setProgName(p.getProgName()); e.setStartTime(p.getStartTime().toString()); e.setEndTime(p.getEndTime().toString()); Duration duration = Duration.between(p.getStartTime(), p.getEndTime()); e.setDuration(DurationUtil.toChineseDuration(duration)); e.setDeviceStateName(DefaultWcsEnum.of(p.getDeviceStatus()).getName()); return e; }).collect(Collectors.toList()); //xx工件在x机床 PartWorkingHour partWorkingHour = partWorkingHourMapper.selectById(workinghourId); Workstation ws = workstationMapper.selectById(partWorkingHour.getWorkstationId()); String fileName = String.format("%s的加工记录-%s.xlsx", ws.getName(), DateUtil.time()); MultipartFile multipartFile = ExcelUtil.exportToMultipartFile(fileName, "加工记录表", exportList, PartWorkingProcessExcel.class); BladeFile bladeFile = this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile); return bladeFile; } }