|
@@ -23,6 +23,11 @@ import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
|
|
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
|
|
|
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
|
|
import org.apache.poi.hssf.usermodel.*;
|
|
|
+import org.apache.poi.ss.usermodel.CellType;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.core.task.AsyncTaskExecutor;
|
|
@@ -31,11 +36,11 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.sql.Timestamp;
|
|
@@ -73,6 +78,8 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
|
|
|
@Value("${wx.app_secret}")
|
|
|
public String appSecret;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private ReportService reportService;
|
|
|
@Resource
|
|
|
ReportExtraDegreeMapper reportExtraDegreeMapper;
|
|
|
@Resource
|
|
@@ -1562,6 +1569,151 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
|
|
|
return httpRespMsg;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg importData(Integer companyId, MultipartFile multipartFile, HttpServletRequest request) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ String token = request.getHeader("TOKEN");
|
|
|
+ //然后处理文件
|
|
|
+ String fileName = multipartFile.getOriginalFilename();
|
|
|
+ File file = new File(fileName == null ? "file" : fileName);
|
|
|
+ InputStream inputStream = null;
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream = multipartFile.getInputStream();
|
|
|
+ outputStream = new FileOutputStream(file);
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int temp = 0;
|
|
|
+ while ((temp = inputStream.read(buffer, 0, 4096)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, temp);
|
|
|
+ }
|
|
|
+ inputStream.close();
|
|
|
+ outputStream.close();
|
|
|
+ //然后解析表格
|
|
|
+ XSSFWorkbook workbook = new XSSFWorkbook(file);
|
|
|
+
|
|
|
+ //导入员工薪资表
|
|
|
+ DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/M/d");
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d");
|
|
|
+ //获取公司全部成员
|
|
|
+ List<User> allUserList = userMapper.selectList(new QueryWrapper<User>().eq("company_id", companyId));
|
|
|
+
|
|
|
+ XSSFSheet sheet = workbook.getSheetAt(0);
|
|
|
+ //由于第一行需要指明列对应的标题
|
|
|
+ int rowNum = sheet.getLastRowNum();
|
|
|
+ List<String> projectList = new ArrayList<>();
|
|
|
+ List<Project> allProjectList = projectMapper.selectList(new QueryWrapper<Project>().eq("company_id", companyId));
|
|
|
+ List<Report> reportList = new ArrayList<>();
|
|
|
+ int projectNameStartIndex = 2;
|
|
|
+
|
|
|
+ for (int rowIndex = 0; rowIndex <= rowNum; rowIndex++) {
|
|
|
+ XSSFRow row = sheet.getRow(rowIndex);
|
|
|
+ if (row == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (ExcelUtil.isRowEmpty(row)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (rowIndex == 0) {
|
|
|
+ //第一行是标题,获取项目名称
|
|
|
+ int pIndex = projectNameStartIndex;
|
|
|
+ boolean projectNotExists = false;
|
|
|
+ String neProjectName = null;
|
|
|
+ while(pIndex < row.getLastCellNum() && row.getCell(pIndex).getCellTypeEnum() != CellType._NONE && row.getCell(pIndex).getCellTypeEnum() != CellType.BLANK) {
|
|
|
+ row.getCell(pIndex).setCellType(CellType.STRING);
|
|
|
+ String stringCellValue = row.getCell(pIndex).getStringCellValue().trim();
|
|
|
+ projectList.add(stringCellValue);
|
|
|
+ if (!allProjectList.stream().filter(p->p.getProjectName().equals(stringCellValue)).findAny().isPresent()) {
|
|
|
+ projectNotExists = true;
|
|
|
+ neProjectName = stringCellValue;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ pIndex++;
|
|
|
+ }
|
|
|
+ if (projectNotExists) {
|
|
|
+ //返回错误提示
|
|
|
+ msg.setError("项目["+neProjectName+"]不存在,请先在项目管理中添加或导入。");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //数据行
|
|
|
+ for (int i=1;i<2+projectList.size(); i++) {
|
|
|
+ row.getCell(i).setCellType(CellType.STRING);
|
|
|
+ }
|
|
|
+ String reportDate = sdf.format(row.getCell(0).getDateCellValue());
|
|
|
+ String username = row.getCell(1).getStringCellValue().trim();
|
|
|
+ //检查人员是否存在
|
|
|
+ Optional<User> any = allUserList.stream().filter(u -> u.getName().equals(username)).findAny();
|
|
|
+ if (!any.isPresent()) {
|
|
|
+ msg.setError("人员["+username+"]不存在,请先在组织结构中添加或者通过企业微信/钉钉同步导入");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ User reportCreator = any.get();
|
|
|
+ List<SimpleProjectime> timeCostList = new ArrayList<SimpleProjectime>();
|
|
|
+ double totalTime = 0;
|
|
|
+ for (int i=2; i < 2 + projectList.size(); i++) {
|
|
|
+ String stringCellValue = row.getCell(i).getStringCellValue();
|
|
|
+ double time = 0;
|
|
|
+ String pName = projectList.get(i-2);
|
|
|
+ Project project = allProjectList.stream().filter(p -> p.getProjectName().equals(pName)).findFirst().get();
|
|
|
+
|
|
|
+ if (!StringUtils.isEmpty(stringCellValue)) {
|
|
|
+ time = Double.parseDouble(stringCellValue);
|
|
|
+ totalTime += time;
|
|
|
+ Report report = new Report();
|
|
|
+ report.setCompanyId(companyId);
|
|
|
+ report.setCreatorId(reportCreator.getId());
|
|
|
+ report.setProjectId(project.getId());
|
|
|
+ report.setReportTimeType(1);
|
|
|
+ report.setWorkingTime(time);
|
|
|
+ report.setState(1);//导入的直接算审核通过
|
|
|
+ report.setCreateDate(LocalDate.parse(reportDate, dtf));
|
|
|
+ report.setCost(reportCreator.getCost().multiply(new BigDecimal(time)));
|
|
|
+ reportList.add(report);
|
|
|
+ }
|
|
|
+ //检查个人总工时不能为0
|
|
|
+ if (totalTime == 0) {
|
|
|
+ msg.setError(username + "的工时为0,请检查修改");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //先删除老数据
|
|
|
+ for (Report r : reportList) {
|
|
|
+ reportMapper.delete(new QueryWrapper<Report>().eq("company_id", companyId).eq("creator_id", r.getCreatorId()).eq("create_date", r.getCreateDate()));
|
|
|
+ }
|
|
|
+ //存储
|
|
|
+ reportService.saveBatch(reportList);
|
|
|
+ msg.data = rowNum;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ msg.setError("文件处理出错");
|
|
|
+ return msg;
|
|
|
+ } catch (NullPointerException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ msg.setError("数据格式有误或存在空数据 导入失败");
|
|
|
+ return msg;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ msg.setError("发生其他错误");
|
|
|
+ return msg;
|
|
|
+ } finally {
|
|
|
+ //关闭流
|
|
|
+ try {
|
|
|
+ if (outputStream != null && inputStream != null) {
|
|
|
+ outputStream.close();
|
|
|
+ inputStream.close();
|
|
|
+ System.out.println("流已关闭");
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+// file.deleteOnExit();//程序退出时删除临时文件
|
|
|
+ System.out.println(file.delete());
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
private void fillDeptUser(List<DepartmentVO> list, List<HashMap> userList) {
|
|
|
list.forEach(l->{
|
|
|
List<HashMap> collect = userList.stream().filter(u -> u.get("departmentId").equals(l.getId())).collect(Collectors.toList());
|