|
|
@@ -1,28 +1,38 @@
|
|
|
|
|
|
package org.sky.scientific.service.impl;
|
|
|
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.sky.core.excel.util.ExcelUtil;
|
|
|
import org.sky.core.log.exception.ServiceException;
|
|
|
import org.sky.core.mp.base.BaseServiceImpl;
|
|
|
-import org.sky.core.tool.utils.BeanUtil;
|
|
|
-import org.sky.core.tool.utils.CollectionUtil;
|
|
|
-import org.sky.core.tool.utils.DateUtil;
|
|
|
-import org.sky.core.tool.utils.StringUtil;
|
|
|
+import org.sky.core.secure.utils.SecureUtil;
|
|
|
+import org.sky.core.tool.utils.*;
|
|
|
import org.sky.scientific.excel.AssetExcel;
|
|
|
+import org.sky.scientific.excel.XmAssetHoursMonthlyDataListener;
|
|
|
+import org.sky.scientific.excel.XmAssetHoursMonthlyExcel;
|
|
|
import org.sky.scientific.mapper.AssetMapper;
|
|
|
+import org.sky.scientific.mapper.XmAssetHoursMapper;
|
|
|
+import org.sky.scientific.mapper.XmMapper;
|
|
|
import org.sky.scientific.pojo.dto.AssetDTO;
|
|
|
import org.sky.scientific.pojo.entity.AssetEntity;
|
|
|
+import org.sky.scientific.pojo.entity.XmAssetHoursEntity;
|
|
|
import org.sky.scientific.pojo.vo.AssetVO;
|
|
|
+import org.sky.scientific.pojo.vo.DailyAttendance;
|
|
|
+import org.sky.scientific.pojo.vo.XmAssetHoursVO;
|
|
|
import org.sky.scientific.service.IAssetService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
@@ -36,6 +46,11 @@ import java.util.Objects;
|
|
|
@Service
|
|
|
public class AssetServiceImpl extends BaseServiceImpl<AssetMapper, AssetEntity> implements IAssetService {
|
|
|
|
|
|
+ @Resource
|
|
|
+ private XmMapper xmMapper;
|
|
|
+ @Resource
|
|
|
+ private XmAssetHoursMapper xmAssetHoursMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public IPage<AssetVO> selectAssetPage(IPage<AssetVO> page, AssetVO asset) {
|
|
|
return page.setRecords(baseMapper.selectAssetPage(page, asset));
|
|
|
@@ -119,4 +134,105 @@ public class AssetServiceImpl extends BaseServiceImpl<AssetMapper, AssetEntity>
|
|
|
//});
|
|
|
return assetList;
|
|
|
}
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void importXmAssetHours(MultipartFile file, String yearAndMonth) {
|
|
|
+ XmAssetHoursMonthlyDataListener listener = new XmAssetHoursMonthlyDataListener();
|
|
|
+ listener.setYearAndMonth(yearAndMonth);
|
|
|
+ EasyExcel.read(new BufferedInputStream(file.getInputStream()))
|
|
|
+ .registerReadListener(listener)
|
|
|
+ .sheet(0)
|
|
|
+ .headRowNumber(3) // 重要:设置表头行数(0-based)
|
|
|
+ .doRead();
|
|
|
+ List<XmAssetHoursMonthlyExcel> records = listener.getRecords();
|
|
|
+ if (CollectionUtil.isEmpty(records)) {
|
|
|
+ throw new ServiceException("未能从模板中读取数据,请检查模板格式是否正确");
|
|
|
+ }
|
|
|
+
|
|
|
+ //需要新增的,以便后续批量新增
|
|
|
+ List<XmAssetHoursEntity> insertList = new ArrayList<>();
|
|
|
+ //需要更新的,以便后续批量修改
|
|
|
+ List<XmAssetHoursEntity> updateList = new ArrayList<>();
|
|
|
+
|
|
|
+ int batchCount = 20;
|
|
|
+
|
|
|
+ List<XmAssetHoursEntity> dbList = xmAssetHoursMapper.selectByYearAndMonth(yearAndMonth);
|
|
|
+ for (XmAssetHoursMonthlyExcel record : records) {
|
|
|
+ //保存
|
|
|
+ if(StringUtil.isAllBlank(record.getXmmc(), record.getXmbh())){
|
|
|
+ throw new ServiceException("研发项目编号、研发项目名称都为空,请检查数据");
|
|
|
+ }
|
|
|
+ Long xmId = xmMapper.getXmIdByXmmcOrXmbh(yearAndMonth, record.getXmmc(), record.getXmbh());
|
|
|
+ if (xmId == null) {
|
|
|
+ throw new ServiceException("序号为[" + record.getSerialNumber() + "]的研发项目编号和研发项目名称都不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(StringUtil.isBlank(record.getZcbm())){
|
|
|
+ throw new ServiceException("序号为[" + record.getSerialNumber() + "]的资产编码为空");
|
|
|
+ }
|
|
|
+ AssetEntity dbAsset = baseMapper.selectOne(Wrappers.<AssetEntity>lambdaQuery().eq(AssetEntity::getZcbm, record.getZcbm()).eq(AssetEntity::getYearAndMonth, yearAndMonth));
|
|
|
+ if (dbAsset == null) {
|
|
|
+ throw new ServiceException("序号为[" + record.getSerialNumber() + "]的资产不存在");
|
|
|
+ }
|
|
|
+ List<XmAssetHoursEntity> byXmAndAssetList = dbList.stream().filter(e -> e.getXmId().equals(xmId) && e.getAssetId().equals(dbAsset.getId())).toList();
|
|
|
+ for (DailyAttendance dailyAttendance : record.getDailyAttendances()) {
|
|
|
+
|
|
|
+ List<Long> primaryKeyList = byXmAndAssetList.stream().filter(e -> Objects.equals(e.getWorkDate(), dailyAttendance.getWorkDate())).map(XmAssetHoursEntity::getId).toList();
|
|
|
+ Long primaryKey = CollectionUtil.isEmpty(primaryKeyList) ? null : primaryKeyList.get(0);
|
|
|
+ XmAssetHoursEntity entity = new XmAssetHoursEntity();
|
|
|
+ if (primaryKey == null) {
|
|
|
+ entity.setXmId(xmId);
|
|
|
+ entity.setAssetId(dbAsset.getId());
|
|
|
+ entity.setWorkDate(dailyAttendance.getWorkDate());
|
|
|
+ entity.setWorkHours(dailyAttendance.getWorkHours());
|
|
|
+ entity.setTenantId(SecureUtil.getTenantId());
|
|
|
+ entity.setCreateUser(SecureUtil.getUserId());
|
|
|
+ entity.setCreateDept(Func.toLong(SecureUtil.getDeptId()));
|
|
|
+ insertList.add(entity);
|
|
|
+ } else {
|
|
|
+ entity.setId(primaryKey);
|
|
|
+ entity.setWorkHours(dailyAttendance.getWorkHours());
|
|
|
+ updateList.add(entity);
|
|
|
+ }
|
|
|
+ if (!insertList.isEmpty() && insertList.size() % batchCount == 0) {
|
|
|
+ int insertCount = xmAssetHoursMapper.batchInsert(insertList);
|
|
|
+ if (insertCount == batchCount) {
|
|
|
+ insertList.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (!updateList.isEmpty() && updateList.size() % batchCount == 0) {
|
|
|
+ int updateCount = xmAssetHoursMapper.batchUpdate(updateList);
|
|
|
+ if (updateCount == batchCount) {
|
|
|
+ updateList.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!insertList.isEmpty()) {
|
|
|
+ xmAssetHoursMapper.batchInsert(insertList);
|
|
|
+ }
|
|
|
+ if (!updateList.isEmpty()) {
|
|
|
+ xmAssetHoursMapper.batchUpdate(updateList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage<XmAssetHoursVO> selectXmAssetHoursPage(XmAssetHoursVO vo, IPage<XmAssetHoursVO> page) {
|
|
|
+ if (StringUtil.isBlank(vo.getYearAndMonth())) {
|
|
|
+ throw new ServiceException("参数yearAndMonth不能为空");
|
|
|
+ }
|
|
|
+ List<XmAssetHoursVO> list = xmAssetHoursMapper.selectXmAssetHoursPage(page, vo);
|
|
|
+// for (XmAssetHoursVO temp : list) {
|
|
|
+// List<DailyAttendance> dailyAttendanceList = xmAssetHoursMapper.selectHoursByXmIdAndAssetId(temp.getXmId(), temp.getAssetId(), vo.getYearAndMonth());
|
|
|
+// if(CollectionUtil.isEmpty(dailyAttendanceList)){
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// temp.setMonthlyHours(dailyAttendanceList.stream().mapToDouble(DailyAttendance::getWorkHours).sum());
|
|
|
+// temp.setDailyAttendances(dailyAttendanceList);
|
|
|
+// }
|
|
|
+ return page.setRecords(list);
|
|
|
+ }
|
|
|
}
|