| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- package com.ruoyi.project.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.ruoyi.common.core.domain.AjaxResult;
- import com.ruoyi.common.core.domain.entity.SysDept;
- import com.ruoyi.common.core.domain.entity.SysUser;
- import com.ruoyi.common.enums.UserRolesType;
- import com.ruoyi.common.exception.ServiceException;
- import com.ruoyi.common.utils.DateUtils;
- import com.ruoyi.common.utils.PageUtils;
- import com.ruoyi.common.utils.SecurityUtils;
- import com.ruoyi.common.utils.StringUtils;
- import com.ruoyi.common.utils.bean.BeanUtils;
- import com.ruoyi.common.utils.bean.BeanValidators;
- import com.ruoyi.common.utils.file.FileUtils;
- import com.ruoyi.project.bo.ProjectListInput;
- import com.ruoyi.project.bo.ProjectTaskListOut;
- import com.ruoyi.project.bo.ProjectTaskOut;
- import com.ruoyi.project.domain.ProjectStaff;
- import com.ruoyi.project.domain.ProjectStaffRecord;
- import com.ruoyi.project.domain.ProjectTask;
- import com.ruoyi.project.mapper.ProjectStaffMapper;
- import com.ruoyi.project.mapper.ProjectStaffRecordMapper;
- import com.ruoyi.project.mapper.ProjectTaskMapper;
- import com.ruoyi.project.service.ProjectTaskService;
- import com.ruoyi.system.mapper.SysDeptMapper;
- import com.ruoyi.system.mapper.SysUserMapper;
- import com.ruoyi.system.service.ISysDeptService;
- import org.apache.poi.xwpf.usermodel.XWPFDocument;
- import org.apache.poi.xwpf.usermodel.XWPFParagraph;
- import org.apache.poi.xwpf.usermodel.XWPFRun;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.HttpStatus;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletResponse;
- import javax.validation.Validator;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.*;
- import java.util.zip.ZipOutputStream;
- import static java.lang.Thread.sleep;
- @Service
- public class ProjectTaskServiceImpl implements ProjectTaskService {
- private static final Logger log = LoggerFactory.getLogger(ProjectTaskServiceImpl.class);
- @Resource
- private ProjectTaskMapper projectTaskMapper;
- @Resource
- private ProjectStaffMapper projectStaffMapper;
- @Resource
- private ISysDeptService deptService;
- @Resource
- private Validator validator;
- @Resource
- private SysUserMapper userMapper;
- @Resource
- private ProjectStaffRecordMapper projectStaffRecordMapper;
- @Resource
- private SysDeptMapper sysDeptMapper;
- @Resource
- private TianheService tianheService;
- @Value("${ruoyi.profile}")
- private String profile=null;
- @Override
- public AjaxResult addProjectTask(ProjectTask projectTask) {
- userMapper.updateUserProjectSum(projectTask.getAid(),0);
- projectTask.setCreateAid(SecurityUtils.getUserId());
- int i = projectTaskMapper.insertSelective(projectTask);
- if (StringUtils.isNotBlank(projectTask.getStaffObject())){
- String[] split = projectTask.getStaffObject().split(",");
- projectStaffMapper.updatePidByList(projectTask.getId(),split);
- projectTaskMapper.updateStaffName(projectTask.getId());
- }
- return AjaxResult.success(i);
- }
- @Override
- public AjaxResult deleteProject(ProjectTask in) {
- int i = projectTaskMapper.deleteByPrimaryKey(in.getId());
- userMapper.updateUserProjectSum(in.getAid(),0);
- if (i>0){
- projectStaffMapper.deleteBypid(in.getId());
- return AjaxResult.success();
- }
- return AjaxResult.error("项目编号不存在");
- }
- @Override
- public AjaxResult details(Long id) {
- ProjectTask projectTask = projectTaskMapper.selectByPrimaryKey(id);
- ProjectTaskOut out= new ProjectTaskOut();
- if (projectTask!=null){
- BeanUtils.copyBeanProp(out, projectTask);
- List<ProjectStaff> projectStaffs = projectStaffMapper.selectByPid(id);
- if (!projectStaffs.isEmpty())out.setList(projectStaffs);
- return AjaxResult.success(out);
- }
- return AjaxResult.error("项目不存在");
- }
- @Override
- public List<ProjectTaskListOut> list(ProjectListInput in) {
- pushRoleType(in);
- if (!SecurityUtils.isSuperAdmin()){
- in.setUid(SecurityUtils.getUserId());
- Long companyId = deptService.selectCompanyByDeptId();
- if (companyId!=null)in.setCompanyId(companyId);
- }
- if (in.getMyCreate()!=null&&in.getMyCreate()==1){
- in.setCreateAid(SecurityUtils.getUserId());
- }
- if (in.getEndTime()!=null)in.setEndTime(in.getEndTime()+" 23:59:59");
- PageUtils.startPage();
- return projectTaskMapper.selectList(in);
- }
- private void pushRoleType(ProjectListInput in) {
- if (in.getRoleType()==null){
- if (SecurityUtils.hashRoles(UserRolesType.CEO.getCode())){
- in.setRoleType(1);
- }else if (SecurityUtils.hashRoles(UserRolesType.ADMIN.getCode())){
- in.setRoleType(3);
- }else if (SecurityUtils.hashRoles(UserRolesType.MANAGE.getCode())){
- in.setRoleType(2);
- }else {
- in.setRoleType(0);
- }
- }
- }
- @Override
- public String importProject(List<ProjectTaskListOut> list, boolean updateSupport, String operName) {
- if (StringUtils.isNull(list) || list.size() == 0)
- {
- throw new ServiceException("导入用户数据不能为空!");
- }
- int successNum = 0;
- int failureNum = 0;
- StringBuilder successMsg = new StringBuilder();
- StringBuilder failureMsg = new StringBuilder();
- Long company = deptService.selectCompanyByDeptId();
- List<Long> pids = new ArrayList<Long>();
- for (ProjectTaskListOut p : list)
- {
- try
- {
- if (p.getProjectYear()==null||p.getProjectYear()==""){
- throw new ServiceException("项目年份不能为空");
- }else if (p.getCreateYear()==null||p.getCreateYear()==""){
- throw new ServiceException("创建年份不能为空");
- }
- // 验证是否存在这个用户
- ProjectTask projectTask = projectTaskMapper.selectByProjectNumber(p.getProjectNumber(),company);
- if (StringUtils.isNull(projectTask)){
- BeanValidators.validateWithException(validator, p);
- if (checkProjectNumber(p)){
- throw new ServiceException(String.format("项目编号[%s]已存在", p.getProjectNumber()));
- }
- if(StringUtils.isNotBlank(p.getAdminName())) {
- SysUser sysUser = userMapper.selectByNikeName(p.getAdminName(),company);
- if (sysUser == null) {
- throw new ServiceException(String.format("负责人[%s]不存在", p.getAdminName()));
- }
- p.setAid(sysUser.getUserId());
- p.setAdminName(sysUser.getNickName());
- }
- p.setProjectStatus(0);
- projectTaskMapper.insertSelective(p);
- pushProjectStaffName(p, company);
- successNum++;
- successMsg.append("<br/>" + successNum + "、项目: " + p.getName() + " 导入成功");
- }else if (updateSupport){
- BeanValidators.validateWithException(validator, p);
- p.setId(projectTask.getId());
- if(StringUtils.isNotBlank(p.getStaffName())) {
- addStaffAndDelete( p);
- }
- if (p.getAdminName()!=null&&!projectTask.getAdminName().equals(p.getAdminName())){
- SysUser sysUser = userMapper.selectByNikeName(p.getAdminName(),company);
- if (sysUser == null) {
- throw new ServiceException(String.format("负责人[%s]不存在", p.getAdminName()));
- }
- List<ProjectStaffRecord> projectStaffRecords = projectStaffRecordMapper.selectByPidAndAid(p.getId(), projectTask.getAid());
- if (projectStaffRecords.size()>0){
- throw new ServiceException(String.format("负责人[%s]存在打卡,不允许切换.", projectTask.getAdminName()));
- }
- p.setAid(sysUser.getUserId());
- p.setAdminName(sysUser.getNickName());
- }
- projectTaskMapper.updateByPrimaryKeySelective(p);
- if (!pids.contains(p.getId())){
- pids.add(p.getId());
- }
- successNum++;
- successMsg.append("<br/>" + successNum + "、项目: " + projectTask.getName() + " 更新成功");
- }
- else
- {
- failureNum++;
- failureMsg.append("<br/>" + failureNum + "、项目: " + projectTask.getName() + " 已存在");
- }
- }
- catch (Exception e)
- {
- failureNum++;
- String msg = "<br/>" + failureNum + "、项目: " + p.getName() + " 导入失败:";
- failureMsg.append(msg + e.getMessage());
- log.error(msg, e);
- }
- }
- if (failureNum > 0)
- {
- failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
- throw new ServiceException(failureMsg.toString());
- }
- else
- {
- successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
- }
- if (!pids.isEmpty()){
- for (Long pid : pids) {
- projectStaffRecordMapper.updateSumDurationProject(pid);
- projectStaffRecordMapper.updateSumDurationStaff(pid);
- }
- }
- return successMsg.toString();
- }
- private void pushProjectStaffName(ProjectTaskListOut p, Long company) {
- if(StringUtils.isNotBlank(p.getStaffName())) {
- String[] split = StringUtils.splitString(p.getStaffName());
- for (String s : split) {
- ProjectStaff newPs = new ProjectStaff();
- SysUser sysUser = userMapper.selectByNikeName(s, company);
- if (sysUser == null) {
- throw new ServiceException("[" + s + "]不存在");
- }
- newPs.setAid(sysUser.getUserId());
- newPs.setName(sysUser.getNickName());
- newPs.setPid(p.getId());
- projectStaffMapper.insertSelective(newPs);
- }
- }
- projectTaskMapper.updateStaffName(p.getId());
- }
- @Override
- public AjaxResult updateProjectTask(ProjectTask in) {
- ProjectTask use = projectTaskMapper.selectByPrimaryKey(in.getId());
- if (!in.getAid().equals(use.getAid())){
- List<ProjectStaffRecord> projectStaffRecords = projectStaffRecordMapper.selectByPidAndAid(in.getId(), use.getAid());
- if (projectStaffRecords.size()>0){
- throw new ServiceException(String.format("负责人[%s]存在打卡,不允许切换.", use.getAdminName()));
- }
- }
- return AjaxResult.success(projectTaskMapper.updateByPrimaryKeySelective(in));
- }
- @Override
- public AjaxResult selectProjectImg(Long pid) {
- return AjaxResult.success(projectTaskMapper.selectProjectImg(pid));
- }
- @Override
- public boolean checkProjectNumber(ProjectTask projectTask) {
- Long companyId = deptService.selectCompanyByDeptId();
- ProjectTask projectTask1 = projectTaskMapper.selectByProjectNumber(projectTask.getProjectNumber(),companyId);
- if (projectTask.getId()==null){
- if (projectTask1!=null){
- return true;
- }
- }else {
- if (projectTask1!=null&&!projectTask.getId().equals(projectTask1.getId())){
- return true;
- }
- }
- return false;
- }
- private void addStaffAndDelete( ProjectTaskListOut p) {
- String[] split = p.getStaffName().split(",");
- List<ProjectStaff> projectStaffs = projectStaffMapper.selectByPid(p.getId());
- deleteStaff( split, projectStaffs);
- addStaff( p, split, projectStaffs);
- //再从新获取统计名字
- projectTaskMapper.updateStaffName(p.getId());
- }
- private void deleteStaff( String[] split, List<ProjectStaff> projectStaffs) {
- //预设需要删除,如果找到相同名字的就无需删除
- for (ProjectStaff ps : projectStaffs) {
- boolean flag = true;
- for (String s : split) {
- s=s.trim();
- if (ps.getName().equals(s)) {
- flag = false;
- }
- }
- if (flag) {
- int sum = projectStaffRecordMapper.selectById(ps.getId());
- if (sum > 0) {
- throw new ServiceException("[" + ps.getName() + "]存在打卡不能删除");
- }
- projectStaffMapper.deleteByPrimaryKey(ps.getId());
- }
- }
- }
- private void addStaff( ProjectTaskListOut p, String[] split, List<ProjectStaff> projectStaffs) {
- Long company = deptService.selectCompanyByDeptId();
- for (String s : split) {
- s=s.trim();
- boolean flag = true;
- for (ProjectStaff ps : projectStaffs) {
- if (ps.getName().equals(s)) {
- flag = false;
- }
- }
- if (flag) {
- ProjectStaff newPs = new ProjectStaff();
- SysUser sysUser = userMapper.selectByNikeName(s,company);
- if (sysUser == null) {
- throw new ServiceException("[" + s + "]不存在");
- }
- newPs.setAid(sysUser.getUserId());
- newPs.setName(sysUser.getNickName());
- newPs.setPid(p.getId());
- projectStaffMapper.insertSelective(newPs);
- }
- }
- }
- @Override
- public void saveProjectTaskTianhe(String ids) {
- String[] split = ids.split(",");
- try {
- for (String id : split) {
- saveProjectText(Long.valueOf(id));
- Thread.sleep(1000);
- }
- } catch (InterruptedException e) {
- throw new RuntimeException("上链异常,请稍后再试");
- }
- }
- @Override
- public void saveProjectStaffRecordTianhe(String ids) {
- String[] split = ids.split(",");
- try {
- for (String id : split) {
- saveRecordsText(Long.valueOf(id));
- Thread.sleep(1000);
- }
- } catch (InterruptedException e) {
- throw new RuntimeException("上链异常,请稍后再试");
- }
- }
- @Override
- public void saveText(String content) {
- String address = getAddress();
- String operateId ="text_"+DateUtils.dateTimeNow();
- //文字导入
- String s = tianheService.seveText(address, content, operateId);
- JSONObject res = JSONObject.parseObject(s);
- Integer code = (Integer) res.get("code");
- String tianheCloudUrl=null;
- if (code== HttpStatus.OK.value()){
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- String certificate = tianheService.getCertificate(address, operateId);
- JSONObject certificateData = JSONObject.parseObject(certificate);
- Integer code2= (Integer) certificateData.get("code");
- if (code2==HttpStatus.OK.value()){
- log.debug(certificateData.toJSONString());
- tianheCloudUrl=certificateData.getString("data");
- }else {
- String msg = certificateData.getString("msg");
- log.error(msg);
- throw new RuntimeException(msg);
- }
- }else {
- String msg = res.getString("msg");
- log.error(msg);
- throw new RuntimeException(msg);
- }
- }
- public void saveRecordsText(Long id) throws InterruptedException {
- ProjectStaffRecord projectStaffRecord = projectStaffRecordMapper.selectByPrimaryKey(Long.valueOf(id));
- Map<String,Object> map=new HashMap<>();
- map.put("projectStaffRecord",projectStaffRecord);
- map.put("date", DateUtils.getTime());
- saveTianhe(map,null,null,projectStaffRecord);
- }
- public void saveProjectText(Long id) throws InterruptedException {
- ProjectTask projectTask = projectTaskMapper.selectByPrimaryKey(id);
- List<ProjectStaffRecord> projectStaffRecords = projectStaffRecordMapper.selectByPidAndAid(id, null);
- Map<String,Object> map=new HashMap<>();
- map.put("projectTask",projectTask);
- map.put("projectStaffRecords",projectStaffRecords);
- map.put("date", DateUtils.getTime());
- saveTianhe(map,projectTask,projectStaffRecords,null);
- }
- private void saveTianhe(Map<String, Object> map, ProjectTask projectTask,
- List<ProjectStaffRecord> projectStaffRecords,
- ProjectStaffRecord projectStaffRecord) throws InterruptedException {
- String json = JSONObject.toJSONString(map);
- String address = getAddress();
- String operateId =null;
- if (projectTask!=null){
- operateId = "yfdk_"+DateUtils.dateTimeNow();
- }else {
- operateId = "yfdk_records_"+DateUtils.dateTimeNow();
- }
- String docx = createDocx(json, operateId);
- File file = new File(docx);
- String s = tianheService.seveFile(address,operateId,file);
- //文字导入
- // String s = tianheService.seveText(address, json, operateId);
- JSONObject res = JSONObject.parseObject(s);
- Integer code = (Integer) res.get("code");
- String tianheCloudUrl=null;
- if (code== HttpStatus.OK.value()){
- sleep(1000);
- String certificate = tianheService.getCertificate(address, operateId);
- JSONObject certificateData = JSONObject.parseObject(certificate);
- Integer code2= (Integer) certificateData.get("code");
- if (code2==HttpStatus.OK.value()){
- log.debug(certificateData.toJSONString());
- tianheCloudUrl=certificateData.getString("data");
- if (projectTask!=null){
- if (StringUtils.isNotEmpty(tianheCloudUrl)){
- ProjectTask newPT=new ProjectTask();
- newPT.setTianheUrl(tianheCloudUrl);
- newPT.setId(projectTask.getId());
- projectTaskMapper.updateByPrimaryKeySelective(newPT);
- }
- pushProjectStaffRecord(projectStaffRecords);
- }
- if (projectStaffRecord!=null){
- ProjectStaffRecord newPSR=new ProjectStaffRecord();
- newPSR.setTianheUrl(tianheCloudUrl);
- newPSR.setId(projectStaffRecord.getId());
- projectStaffRecordMapper.updateByPrimaryKeySelective(newPSR);
- }
- FileUtils.deleteFile(docx);
- }else {
- String msg = certificateData.getString("msg");
- log.error(msg);
- throw new RuntimeException(msg);
- }
- }else {
- String msg = res.getString("msg");
- log.error(msg);
- throw new RuntimeException(msg);
- }
- }
- private String createDocx(String json, String operateId) {
- String url=profile+"/"+ operateId +".docx";
- XWPFDocument document = new XWPFDocument();
- XWPFParagraph paragraph = document.createParagraph();
- XWPFRun run = paragraph.createRun();
- run.setText(json);
- try {
- // 保存文档到指定路径
- FileOutputStream out = new FileOutputStream(url);
- document.write(out);
- out.close();
- System.out.println("Word文档已成功创建!");
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- // 关闭文档对象
- try {
- document.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return url;
- }
- /**
- * 处理日志跟随项目上链
- */
- private void pushProjectStaffRecord(List<ProjectStaffRecord> projectStaffRecords) {
- int count=50;
- int i=0;
- List<ProjectStaffRecord> newList=new ArrayList<>();
- for (ProjectStaffRecord projectStaffRecord : projectStaffRecords) {
- i++;
- newList.add(projectStaffRecord);
- if (newList.size()==count||i==projectStaffRecords.size()){
- projectStaffRecordMapper.updateTianheType(newList);
- newList.clear();
- }
- }
- }
- private String getAddress() {
- String address = null;
- SysUser user = SecurityUtils.getLoginUser().getUser();
- SysUser sysUser = userMapper.selectUserById(user.getUserId());
- if (sysUser.getTianheId()==null){
- try {
- String userId=user.getUserId().toString();
- String s = tianheService.addUser( userId, "123456");
- JSONObject jsonObject = JSONObject.parseObject(s);
- JSONObject data = (JSONObject) jsonObject.get("data");
- JSONObject pubKey = (JSONObject) data.get("pubKey");
- address= (String) pubKey.get("address");
- }catch (Exception e ){
- log.error("天河账号创建失败!");
- throw new RuntimeException("天河账号创建失败");
- }
- if (address!=null){
- user.setTianheId(address);
- userMapper.updateUser(user);
- }
- }else {
- address=sysUser.getTianheId();
- }
- if (address==null){
- throw new ServiceException("天河账号不存在,请稍后再试");
- }
- return address;
- }
- @Override
- public Object getCompanyData(Integer deptId) {
- //获取部门信息
- SysDept sysDept = new SysDept();
- sysDept.setAncestors(deptId.toString());
- List<SysDept> sysDepts = sysDeptMapper.selectDeptList(sysDept);
- sysDepts.stream().forEach(System.out::println);
- //获取部门下的用户信息
- List<SysUser> sysUsers = new ArrayList<>();
- List<ProjectTask> projectTasks=new ArrayList<>();
- sysDepts.stream().forEach(sysDept1 -> {
- sysUsers.addAll(userMapper.selectByDeptId(sysDept1.getDeptId()));
- });
- sysUsers.stream().forEach(sysUser -> {
- projectTasks.addAll(projectTaskMapper.selectByUserId(sysUser.getUserId()));
- });
- Map<String,Object> map=new HashMap();
- map.put("projectTasks",projectTasks);
- map.put("sysUsers",sysUsers);
- map.put("sysDepts",sysDepts);
- return map;
- }
- }
|