package com.product.file.service; import com.alibaba.fastjson.JSONObject; import com.product.common.collect.ListUtils; import com.product.common.collect.MapUtils; import com.product.common.lang.DateUtils; import com.product.core.config.Global; import com.product.core.dao.BaseDao; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldMetaEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.exception.BaseException; import com.product.core.service.support.AbstractBaseService; import com.product.core.service.support.QueryFilterService; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.core.transfer.Transactional; import com.product.file.config.CmnConst; import com.product.file.service.ide.IDocumentService; import com.product.module.sys.entity.SystemUser; import com.product.util.BaseUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; /** * Copyright lx * * @Title: ClaimExpenseController * @Project: lx-SERVER * @Date: 2020-11-18 11:30:48 * @Author: luoxin * @Description: 文件管理service */ @Component public class DocumentService extends AbstractBaseService implements IDocumentService { @Autowired public BaseDao baseDao; @Autowired public DocumentService documentService; @Autowired QueryFilterService queryFilterService; @Autowired DocumentDirectoryService documentDirectoryService; @Override @Transactional public String addDocument(FieldSetEntity fieldSetEntity) throws BaseException { //文件附件表uuid String uuIds = fieldSetEntity.getString(CmnConst.ATTACHMENTS_UUID); String directory_uuid = fieldSetEntity.getString(CmnConst.DIRECTORY_UUID); String[] uuid = uuIds.split(","); //获取附件信息 DataTableEntity attachments = baseDao.listTable(CmnConst.PRODUCT_SYS_ATTACHMENTS,BaseUtil.buildQuestionMarkFilter(CmnConst.UUID,uuid.length,true),uuid); List uuidList = ListUtils.newArrayList(); List fileNameList = ListUtils.newArrayList(); for (int i = 0,length=attachments.getRows(); i < length; i++) { FieldSetEntity fieldSet = new FieldSetEntity(); fieldSet.setTableName(CmnConst.product_oa_DOCUMENT); FieldSetEntity fse = attachments.getFieldSetEntity(i); //文件目录uuid fieldSet.setValue(CmnConst.DIRECTORY_UUID,directory_uuid); //文件大小 fieldSet.setValue(CmnConst.FILE_SIZE,fse.getString(CmnConst.ATTACHMENT_SIZE)); //文件名称 String file_name = fse.getString(CmnConst.FILE_NAME); fieldSet.setValue(CmnConst.FILE_NAME,file_name); //文件类型 String file_type = file_name.substring(file_name.lastIndexOf(".")+1); fieldSet.setValue(CmnConst.FILE_TYPE, file_type); //附件uuid fieldSet.setValue(CmnConst.ATTACHMENTS_UUID, fse.getString(CmnConst.UUID)); fieldSet.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());// 创建人 fieldSet.setValue(CmnConst.CREATED_UTC_DATETIME, DateUtils.getDateTime());// 创建时间 String file_uuid = baseDao.add(fieldSet); uuidList.add(file_uuid); fileNameList.add(file_name); } this.documentation(uuidList.toArray(), "新增", fileNameList.toArray(),null); return uuidList.toString(); } @Override @Transactional public boolean upDocumentName(FieldSetEntity fieldSetEntity) throws BaseException { String uuid = fieldSetEntity.getString(CmnConst.UUID); String file_name = fieldSetEntity.getString(CmnConst.FILE_NAME); fieldSetEntity.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());// 修改人 fieldSetEntity.setValue(CmnConst.UPDATED_UTC_DATETIME, DateUtils.getDateTime());// 修改时间 FieldSetEntity fie= baseDao.getFieldSetEntity(CmnConst.product_oa_DOCUMENT,uuid,false); String name = fie.getString(CmnConst.FILE_NAME); //每次修改都亲空已读人员 fieldSetEntity.setValue(CmnConst.READ_NUM,""); boolean flag = baseDao.update(fieldSetEntity); this.documentRepeatOrder(new Object[]{uuid}, "重命名",new Object[]{name},new Object[]{file_name}); return flag; } /** * 需要批量删除 * @param fieldSetEntity * @return * @throws BaseException */ @Override @Transactional public boolean delDocument(FieldSetEntity fieldSetEntity) throws BaseException { String uuids = fieldSetEntity.getString(CmnConst.UUID); String file_names = fieldSetEntity.getString(CmnConst.FILE_NAME); String[] file_name = file_names.split(","); String[] uuid = uuids.split(","); boolean flag = baseDao.delete(CmnConst.product_oa_DOCUMENT,uuid); this.documentation(uuid, "删除", file_name,null); return flag; } @Override public DataTableEntity documentAll(FieldSetEntity fieldSetEntity) throws BaseException { //文档目录uuid String directory_uuid = fieldSetEntity.getString(CmnConst.DIRECTORY_UUID); String queryFilter = queryFilterService.getQueryFilter(fieldSetEntity); if(!BaseUtil.strIsNull(queryFilter)){ queryFilter = " AND " + queryFilter; } StringBuffer sql = new StringBuffer(); sql.append(" SELECT ") .append(" a.*,CONCAT(CONVERT(a.file_size / 1024, decimal(10,2)), 'KB') file_size_kb,b.show_name name") // .append(" concat(c.attachment_domain,'/',c.attachment_container,'/',c.module_uuid,'/',c.attachment_title,?) path ") .append(" FROM ") .append(" (SELECT * FROM product_oa_document WHERE directory_uuid = ? ") .append(queryFilter) .append(" ) a LEFT JOIN product_sys_staffs b ON a.created_by = b.user_id ") .append(" LEFT JOIN product_sys_attachments c ON b.staff_avatar=c.uuid "); String sostoken = Global.getSystemConfig("upload.key", ""); DataTableEntity documentData = baseDao.listTable(sql.toString(), new String[]{directory_uuid},fieldSetEntity.getInteger(CmnConst.PAGESIZE), fieldSetEntity.getInteger(CmnConst.CPAGE)); baseDao.listInternationDataTable(documentData, null); return documentData; } @Override public FieldSetEntity documentInfo(FieldSetEntity fieldSetEntity) throws BaseException { String uuid = fieldSetEntity.getString(CmnConst.UUID); FieldSetEntity fieldSet = baseDao.getFieldSetEntity(CmnConst.product_oa_DOCUMENT, uuid, true); String file_name = fieldSet.getString(CmnConst.FILE_NAME); String user_ids = fieldSetEntity.getString(CmnConst.USER_IDS); String read = fieldSetEntity.getString(CmnConst.READ_NUM); if(!BaseUtil.strIsNull(user_ids)){ fieldSetEntity.setValue(CmnConst.REQUIRED_NUMBER,user_ids.split(",").length); fieldSetEntity.setValue(CmnConst.READ_NUM,read.split(",").length); } this.documentation(new Object[]{uuid}, "查看", new Object[]{file_name},null); return fieldSet; } @Override public void documentation(Object[] uuid, String opetype, Object[] file_name,String file_directory) throws BaseException { //获取文件信息存入日志表 SystemUser user = SpringMVCContextHolder.getCurrentUser(); DataTableEntity dataTableEntity = new DataTableEntity(); FieldMetaEntity fieldMeta = new FieldMetaEntity(); fieldMeta.setTableName(new String[]{CmnConst.product_oa_DOCUMENT_LOG}); dataTableEntity.setMeta(fieldMeta); for (int i = 0; i < uuid.length; i++) { FieldSetEntity fieldSetEntity = new FieldSetEntity(); fieldSetEntity.setTableName(CmnConst.product_oa_DOCUMENT_LOG); fieldSetEntity.setMeta(fieldMeta); fieldSetEntity.setValue(CmnConst.DOCUMENT_UUID,uuid[i]); //fieldSetEntity.setValue(CmnConst.RECORD,record); fieldSetEntity.setValue(CmnConst.OPETYPE,opetype); fieldSetEntity.setValue(CmnConst.OPE_DOCUMENT,file_name[i]); StringBuffer state = new StringBuffer(); state.append(opetype) .append("了[") .append(file_name[i]) .append("]"); if(!BaseUtil.strIsNull(file_directory)){ state.append("到").append(file_directory).append("目录下"); } fieldSetEntity.setValue(CmnConst.STATE,state.toString()); //说明 fieldSetEntity.setValue(CmnConst.CREATED_BY, user.getUser_id());// 创建人 fieldSetEntity.setValue(CmnConst.CREATED_UTC_DATETIME, DateUtils.getDateTime());// 创建时间 dataTableEntity.addFieldSetEntity(fieldSetEntity); } baseDao.add(dataTableEntity); } @Override public void documentRepeatOrder(Object[] uuid, String opetype, Object[] file_name,Object[] newFile_name) throws BaseException { //获取文件信息存入日志表 SystemUser user = SpringMVCContextHolder.getCurrentUser(); DataTableEntity dataTableEntity = new DataTableEntity(); FieldMetaEntity fieldMeta = new FieldMetaEntity(); fieldMeta.setTableName(new String[]{CmnConst.product_oa_DOCUMENT_LOG}); dataTableEntity.setMeta(fieldMeta); for (int i = 0; i < uuid.length; i++) { FieldSetEntity fieldSetEntity = new FieldSetEntity(); fieldSetEntity.setTableName(CmnConst.product_oa_DOCUMENT_LOG); fieldSetEntity.setMeta(fieldMeta); fieldSetEntity.setValue(CmnConst.DOCUMENT_UUID,uuid[i]); //fieldSetEntity.setValue(CmnConst.RECORD,record); fieldSetEntity.setValue(CmnConst.OPETYPE,opetype); fieldSetEntity.setValue(CmnConst.OPE_DOCUMENT,file_name[i]); StringBuffer state = new StringBuffer(); state.append("把[") .append(file_name[i]) .append("]") .append(opetype) .append("为[") .append(newFile_name[i]) .append("]"); fieldSetEntity.setValue(CmnConst.STATE,state.toString()); //说明 fieldSetEntity.setValue(CmnConst.CREATED_BY, user.getUser_id());// 创建人 fieldSetEntity.setValue(CmnConst.CREATED_UTC_DATETIME, DateUtils.getDateTime());// 创建时间 dataTableEntity.addFieldSetEntity(fieldSetEntity); } baseDao.add(dataTableEntity); } @Override public FieldSetEntity getButtonPermissions(FieldSetEntity fieldSetEntity) throws BaseException { String butt = ""; DataTableEntity dataTable = null; FieldSetEntity fse = new FieldSetEntity(); fse.setTableName(CmnConst.product_oa_DOCUMENT); if("1".equals(fieldSetEntity.getString(CmnConst.DIRECTORY_TYPE))) { //获取的user_id Integer user_id = SpringMVCContextHolder.getCurrentUser().getUser_id(); String key = "%," + user_id + ",%"; String directory_uuid = fieldSetEntity.getString(CmnConst.DIRECTORY_UUID); //String key = "%,140,%"; StringBuffer sql = new StringBuffer(); sql.append(" SELECT substring_index(substring_index(a.rigths,',',b.help_topic_id+1),',',-1) fun FROM product_oa_directory_rights a ") .append(" INNER join mysql.help_topic b on b.help_topic_id < (length(a.rigths) - length(replace(a.rigths,',',''))+1) ") .append(" WHERE a.directory_uuid = ? AND CONCAT(',',a.storage_uuid,',') LIKE ? "); //暂时不用 // sql.append(" SELECT ") // .append(" fun ") // .append(" FROM ") // .append(" ( SELECT ") // .append(" SUBSTRING_INDEX( SUBSTRING_INDEX( d.rigths, ',', 1 ), ',', - 1 ) fun, ") // .append(" d.staff_ids, ") // .append(" d.directory_uuid ") // .append(" FROM ") // .append(" (SELECT a.rigths, concat( ',', b.staff_ids, ',' ) staff_ids, a.directory_uuid FROM product_oa_directory_rights a LEFT JOIN product_sys_organizational_structure_storage b ON a.storage_uuid = b.uuid) d UNION ALL ") // .append(" SELECT ") // .append(" SUBSTRING_INDEX( SUBSTRING_INDEX( d.rigths, ',', 2 ), ',', - 1 ) fun, ") // .append(" d.staff_ids, ") // .append(" d.directory_uuid ") // .append(" FROM ") // .append(" (SELECT a.rigths, concat( ',', b.staff_ids, ',' ) staff_ids, a.directory_uuid FROM product_oa_directory_rights a LEFT JOIN product_sys_organizational_structure_storage b ON a.storage_uuid = b.uuid) d UNION ALL ") // .append(" SELECT ") // .append(" SUBSTRING_INDEX( SUBSTRING_INDEX( d.rigths, ',', 3 ), ',',- 1 ) fun, ") // .append(" d.staff_ids, ") // .append(" d.directory_uuid ") // .append(" FROM ") // .append(" (SELECT a.rigths, concat( ',', b.staff_ids, ',' ) staff_ids, a.directory_uuid FROM product_oa_directory_rights a LEFT JOIN product_sys_organizational_structure_storage b ON a.storage_uuid = b.uuid) d UNION ALL ") // .append(" SELECT ") // .append(" SUBSTRING_INDEX( SUBSTRING_INDEX( d.rigths, ',', 4 ), ',',- 1 ) fun, ") // .append(" d.staff_ids, ") // .append(" d.directory_uuid ") // .append(" FROM ") // .append(" (SELECT a.rigths, concat( ',', b.staff_ids, ',' ) staff_ids, a.directory_uuid FROM product_oa_directory_rights a LEFT JOIN product_sys_organizational_structure_storage b ON a.storage_uuid = b.uuid) d UNION ALL ") // .append(" SELECT ") // .append(" SUBSTRING_INDEX( SUBSTRING_INDEX( d.rigths, ',', 5 ), ',',- 1 ) fun, ") // .append(" d.staff_ids, ") // .append(" d.directory_uuid ") // .append(" FROM ") // .append(" (SELECT a.rigths, concat( ',', b.staff_ids, ',' ) staff_ids, a.directory_uuid FROM product_oa_directory_rights a LEFT JOIN product_sys_organizational_structure_storage b ON a.storage_uuid = b.uuid) d UNION ALL ") // .append(" SELECT ") // .append(" SUBSTRING_INDEX( SUBSTRING_INDEX( d.rigths, ',', 6 ), ',',- 1 ) fun, ") // .append(" d.staff_ids, ") // .append(" d.directory_uuid ") // .append(" FROM ") // .append(" (SELECT a.rigths, concat( ',', b.staff_ids, ',' ) staff_ids, a.directory_uuid FROM product_oa_directory_rights a LEFT JOIN product_sys_organizational_structure_storage b ON a.storage_uuid = b.uuid) d UNION ALL ") // .append(" SELECT ") // .append(" SUBSTRING_INDEX( SUBSTRING_INDEX( d.rigths, ',', 7 ), ',',- 1 ) fun, ") // .append(" d.staff_ids, ") // .append(" d.directory_uuid ") // .append(" FROM ") // .append(" (SELECT a.rigths, concat( ',', b.staff_ids, ',' ) staff_ids, a.directory_uuid FROM product_oa_directory_rights a LEFT JOIN product_sys_organizational_structure_storage b ON a.storage_uuid = b.uuid) d ") // .append(" ) e ") // .append(" WHERE ") // .append(" CONCAT(',',e.staff_ids,',') LIKE ? ") // .append(" AND e.directory_uuid = ? ") // .append(" GROUP BY ") // .append(" e.fun "); DataTableEntity dataTableEntity = baseDao.listTable(sql.toString(), new String[]{directory_uuid,key}); StringBuffer button = new StringBuffer(); if (!BaseUtil.dataTableIsEmpty(dataTableEntity)) { for (int i = 0, length = dataTableEntity.getRows(); i < length; i++) { FieldSetEntity field = dataTableEntity.getFieldSetEntity(i); String fun = field.getString("fun"); //权限1(查看),2(下载),3(新增上传),4(修改,重命名),5(删除),6(移动) button.append(fun).append(","); if ("1".equals(fun)) { //有查看权限就加载列表 dataTable = this.documentAll(fieldSetEntity); } } } if(!BaseUtil.strIsNull(button.toString())) { butt = button.substring(0, button.length() - 1); } }else { dataTable = this.documentAll(fieldSetEntity); } Map dataMap = MapUtils.newHashMap(); fse.setValue("button", butt); //加载文件路径 DataTableEntity file_list = documentDirectoryService.getDirectoryStructure(fieldSetEntity); dataMap.put("file_list", file_list); if (BaseUtil.dataTableIsEmpty(dataTable) || dataTable.getSqle() == null){ fse.setValue(CmnConst.PAGESIZE,0); fse.setValue(CmnConst.CPAGE, 0); fse.setValue("total", 0); }else{ dataMap.put("dataTable", dataTable); fse.setValue(CmnConst.PAGESIZE, dataTable.getSqle().getPsize()); fse.setValue(CmnConst.CPAGE, dataTable.getSqle().getPindex()); fse.setValue("total", dataTable.getSqle().getTotalCount()); } fse.setSubData(dataMap); return fse; } @Override public JSONObject popBoxGetFileDirectory(FieldSetEntity fse) throws BaseException { //文件目录uuid String directory_uuid = fse.getString(CmnConst.DIRECTORY_UUID); String type = fse.getString(CmnConst.DIRECTORY_TYPE); List param = ListUtils.newArrayList(); SystemUser user = SpringMVCContextHolder.getCurrentUser(); StringBuffer sql = new StringBuffer(); //1为公司文件目录 if("1".equals(type)){ //String staffId = user.getCurrentStaff().getString("staff_id"); sql.append(" select a.* from product_oa_directory a ") .append(" LEFT JOIN ( SELECT a.directory_uuid FROM product_oa_directory_rights a LEFT JOIN product_sys_organizational_structure_storage b ") .append(" on a.storage_uuid = b.uuid WHERE a.rigths like '%3%' AND CONCAT(',',b.staff_ids,',') like ? GROUP BY ") .append(" a.directory_uuid) b ON a.uuid = b.directory_uuid where a.directory_type = 1 and a.org_level_uuid = ? "); String staffId ="%" + user.getCurrentStaff().getString("staff_id") + "%"; param.add(staffId); param.add(user.getOrg_level_uuid()); //param.add("%,140,%"); }else { sql.append(" select * from product_oa_directory where directory_type = 2 AND created_by = ? "); param.add(user.getUser_id()); } JSONObject object = new JSONObject(); //directory_uuid为空就是未选择文件目录 获取当前根目录列表 if(BaseUtil.strIsNull(directory_uuid)){ sql.append(" and directory_tier = 1 "); //不为空就获取选择文件目录 }else { String triCode = fse.getString(CmnConst.TRICODE); param.add(triCode); sql.append(" and tricode_parent = ? "); //加载文件路径 DataTableEntity dataTableEntity = documentDirectoryService.getDirectoryStructure(fse); object.put("product_oa_directory_path",BaseUtil.dataTableEntityToJson(dataTableEntity)); } //sql.append(" GROUP BY tricode,tricode_parent,directory_name"); DataTableEntity data = baseDao.listTable(sql.toString(),param.toArray()); object.put("product_oa_directory_sub",BaseUtil.dataTableEntityToJson(data)); baseDao.listInternationDataTable(data, null); return object; } @Override @Transactional public void downloadFile(FieldSetEntity fieldSetEntity) throws BaseException { String uuid = fieldSetEntity.getString(CmnConst.UUID); FieldSetEntity fieldSet = baseDao.getFieldSetEntity(CmnConst.product_oa_DOCUMENT,uuid,false); String file_name = fieldSet.getString(CmnConst.FILE_NAME); this.documentation(new String[]{uuid},"下载", new String[]{file_name},null); this.markRead(fieldSet); } @Override public void markRead(FieldSetEntity fieldSetEntity) throws BaseException { //获取必读人员ID集 String user_ids = fieldSetEntity.getString(CmnConst.USER_IDS); String read = fieldSetEntity.getString(CmnConst.READ_NUM); SystemUser user = SpringMVCContextHolder.getCurrentUser(); user_ids = ","+user_ids+","; String rawRead = ","+read+","; String userId = ","+user.getUser_id()+","; //如果包含用户ID if(BaseUtil.strIsNull(read)){ fieldSetEntity.setValue(CmnConst.READ_NUM,user.getUser_id()); baseDao.update(fieldSetEntity); }else { if(user_ids.contains(userId) && !rawRead.contains(userId)){ read = read + "," + user.getUser_id(); fieldSetEntity.setValue(CmnConst.READ_NUM,read); baseDao.update(fieldSetEntity); } } } /** * 移动(文件批量移动)6 * @param * @return * @throws BaseException */ @Override @Transactional public boolean fileMoveTo(FieldSetEntity fse) throws BaseException { String directory_uuid = fse.getString(CmnConst.DIRECTORY_UUID); Integer directory_type = fse.getInteger(CmnConst.DIRECTORY_TYPE); Integer row = 0; //1为公司文件夹 if(directory_type == 1) { StringBuffer sql = new StringBuffer(); sql.append(" SELECT * FROM product_oa_directory_rights a ") .append(" LEFT JOIN product_sys_organizational_structure_storage b on a.storage_uuid = b.uuid ") .append(" WHERE a.directory_uuid = ? ") .append(" AND rigths like '%3%' AND concat(',',staff_ids,',') like ? ");//3为新增 String staffId = SpringMVCContextHolder.getCurrentUser().getCurrentStaff().getString("staff_id"); staffId = "%," + staffId + ",%"; DataTableEntity dataTableEntity = baseDao.listTable(sql.toString(), new String[]{directory_uuid, staffId}); row = dataTableEntity.getRows(); } //1为个人文件夹 if(directory_type == 2 || row > 0) { FieldSetEntity fieldSetEntity = baseDao.getFieldSetEntity(CmnConst.product_oa_DIRECTORY, directory_uuid, false); //获取移动到的目录名 String directory_name = fieldSetEntity.getString(CmnConst.DIRECTORY_NAME); String uuids = fse.getString(CmnConst.UUID); String[] uuid = uuids.split(","); DataTableEntity dataTable = baseDao.listTable(CmnConst.product_oa_DOCUMENT, BaseUtil.buildQuestionMarkFilter(CmnConst.UUID, uuid.length, true), uuid); List uuidList = ListUtils.newArrayList(); List fileNameList = ListUtils.newArrayList(); for (int i = 0, length = dataTable.getRows(); i < length; i++) { uuidList.add(dataTable.getFieldSetEntity(i).getString(CmnConst.UUID)); fileNameList.add(dataTable.getFieldSetEntity(i).getString(CmnConst.FILE_NAME)); } String params = directory_uuid + "," + uuids; String[] param = params.split(","); boolean flag = baseDao.executeUpdate("UPDATE product_oa_document SET directory_uuid = ? WHERE " + BaseUtil.buildQuestionMarkFilter(CmnConst.UUID, uuid.length, true), param); this.documentation(uuidList.toArray(), "移动", fileNameList.toArray(), directory_name); return flag; }else { return false; } } @Override public DataTableEntity getDocumentLog(FieldSetEntity fieldSetEntity) throws BaseException { return baseDao.listTable("SELECT CONCAT(b.show_name, a.state) log,a.created_utc_datetime FROM product_oa_document_log a LEFT JOIN product_sys_staffs b on a.created_by = b.user_id WHERE document_uuid = ? ORDER BY a.created_utc_datetime DESC", new String[]{fieldSetEntity.getString(CmnConst.DOCUMENT_UUID)},fieldSetEntity.getInteger(CmnConst.PAGESIZE), fieldSetEntity.getInteger(CmnConst.CPAGE)); } }