package com.jsh.erp.service.organization; import com.alibaba.fastjson.JSONObject; import com.jsh.erp.constants.BusinessConstants; import com.jsh.erp.constants.ExceptionConstants; import com.jsh.erp.datasource.entities.MaterialProperty; import com.jsh.erp.datasource.entities.Organization; import com.jsh.erp.datasource.entities.OrganizationExample; import com.jsh.erp.datasource.entities.User; import com.jsh.erp.datasource.mappers.OrganizationMapper; import com.jsh.erp.datasource.mappers.OrganizationMapperEx; import com.jsh.erp.datasource.vo.TreeNode; import com.jsh.erp.exception.BusinessRunTimeException; import com.jsh.erp.exception.JshException; import com.jsh.erp.service.log.LogService; import com.jsh.erp.service.user.UserService; import com.jsh.erp.utils.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Description * * @Author: cjl * @Date: 2019/3/6 15:10 */ @Service public class OrganizationService { private Logger logger = LoggerFactory.getLogger(OrganizationService.class); @Resource private OrganizationMapper organizationMapper; @Resource private OrganizationMapperEx organizationMapperEx; @Resource private UserService userService; @Resource private LogService logService; public Organization getOrganization(long id) throws Exception { return organizationMapper.selectByPrimaryKey(id); } public List getOrganizationListByIds(String ids)throws Exception { List idList = StringUtil.strToLongList(ids); List list = new ArrayList<>(); try{ OrganizationExample example = new OrganizationExample(); example.createCriteria().andIdIn(idList); list = organizationMapper.selectByExample(example); }catch(Exception e){ JshException.readFail(logger, e); } return list; } @Transactional(value = "transactionManager", rollbackFor = Exception.class) public int insertOrganization(JSONObject obj, HttpServletRequest request)throws Exception { Organization organization = JSONObject.parseObject(obj.toJSONString(), Organization.class); organization.setCreateTime(new Date()); organization.setUpdateTime(new Date()); int result=0; try{ result=organizationMapper.insertSelective(organization); logService.insertLog("机构", new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(organization.getOrgAbr()).toString(),request); }catch(Exception e){ JshException.writeFail(logger, e); } return result; } @Transactional(value = "transactionManager", rollbackFor = Exception.class) public int updateOrganization(JSONObject obj, HttpServletRequest request)throws Exception { Organization organization = JSONObject.parseObject(obj.toJSONString(), Organization.class); organization.setUpdateTime(new Date()); int result=0; try{ result=organizationMapperEx.editOrganization(organization); logService.insertLog("机构", new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(organization.getOrgAbr()).toString(), request); }catch(Exception e){ JshException.writeFail(logger, e); } return result; } @Transactional(value = "transactionManager", rollbackFor = Exception.class) public int deleteOrganization(Long id, HttpServletRequest request)throws Exception { return batchDeleteOrganizationByIds(id.toString()); } @Transactional(value = "transactionManager", rollbackFor = Exception.class) public int batchDeleteOrganization(String ids, HttpServletRequest request)throws Exception { return batchDeleteOrganizationByIds(ids); } @Transactional(value = "transactionManager", rollbackFor = Exception.class) public int batchDeleteOrganizationByIds(String ids) throws Exception{ StringBuffer sb = new StringBuffer(); sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE); List list = getOrganizationListByIds(ids); for(Organization organization: list){ sb.append("[").append(organization.getOrgAbr()).append("]"); } logService.insertLog("机构", sb.toString(), ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()); User userInfo=userService.getCurrentUser(); String [] idArray=ids.split(","); int result=0; List organList = organizationMapperEx.getOrganizationByParentIds(idArray); if(organList!=null && organList.size()>0) { //如果存在子机构则不能删除 logger.error("异常码[{}],异常提示[{}]", ExceptionConstants.ORGANIZATION_CHILD_NOT_ALLOWED_DELETE_CODE,ExceptionConstants.ORGANIZATION_CHILD_NOT_ALLOWED_DELETE_MSG); throw new BusinessRunTimeException(ExceptionConstants.ORGANIZATION_CHILD_NOT_ALLOWED_DELETE_CODE, ExceptionConstants.ORGANIZATION_CHILD_NOT_ALLOWED_DELETE_MSG); } else { result=organizationMapperEx.batchDeleteOrganizationByIds( new Date(),userInfo==null?null:userInfo.getId(),idArray); } return result; } public int checkIsNameExist(Long id, String name)throws Exception { OrganizationExample example = new OrganizationExample(); example.createCriteria().andIdNotEqualTo(id).andOrgAbrEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED); List list=null; try{ list= organizationMapper.selectByExample(example); }catch(Exception e){ JshException.readFail(logger, e); } return list==null?0:list.size(); } public List getOrganizationTree(Long id)throws Exception { List list=null; try{ list=organizationMapperEx.getNodeTree(id); }catch(Exception e){ JshException.readFail(logger, e); } return list; } public List findById(Long id) throws Exception{ OrganizationExample example = new OrganizationExample(); example.createCriteria().andIdEqualTo(id); List list=null; try{ list=organizationMapper.selectByExample(example); }catch(Exception e){ JshException.readFail(logger, e); } return list; } public List findByParentId(Long parentId)throws Exception { List list=null; if(parentId!=null){ OrganizationExample example = new OrganizationExample(); example.createCriteria().andIdEqualTo(parentId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED); try{ list=organizationMapper.selectByExample(example); }catch(Exception e){ JshException.readFail(logger, e); } } return list; } public List findByOrgNo(String orgNo)throws Exception { OrganizationExample example = new OrganizationExample(); example.createCriteria().andOrgNoEqualTo(orgNo).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED); List list=null; try{ list=organizationMapper.selectByExample(example); }catch(Exception e){ JshException.readFail(logger, e); } return list; } /** * create by: cjl * description: * 检查机构编号是否已经存在 * create time: 2019/3/7 10:01 * @Param: orgNo * @return void */ public void checkOrgNoIsExists(String orgNo,Long id)throws Exception { List orgList=findByOrgNo(orgNo); if(orgList!=null&&orgList.size()>0){ if(orgList.size()>1){ logger.error("异常码[{}],异常提示[{}],参数,orgNo[{}]", ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_CODE,ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_MSG,orgNo); //获取的数据条数大于1,机构编号已存在 throw new BusinessRunTimeException(ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_CODE, ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_MSG); } if(id!=null){ if(!orgList.get(0).getId().equals(id)){ //数据条数等于1,但是和编辑的数据的id不相同 logger.error("异常码[{}],异常提示[{}],参数,orgNo[{}],id[{}]", ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_CODE,ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_MSG,orgNo,id); throw new BusinessRunTimeException(ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_CODE, ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_MSG); } }else{ logger.error("异常码[{}],异常提示[{}],参数,orgNo[{}]", ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_CODE,ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_MSG,orgNo); //数据条数等于1,但此时是新增 throw new BusinessRunTimeException(ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_CODE, ExceptionConstants.ORGANIZATION_NO_ALREADY_EXISTS_MSG); } } } /** * 根据父级id递归获取子集组织id * @return */ public List getOrgIdByParentId(Long orgId) { List idList = new ArrayList<>(); OrganizationExample example = new OrganizationExample(); example.createCriteria().andIdEqualTo(orgId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED); List orgList = organizationMapper.selectByExample(example); if(orgList!=null && orgList.size()>0) { idList.add(orgId); getOrgIdByParentNo(idList, orgList.get(0).getId()); } return idList; } /** * 根据组织编号递归获取下级编号 * @param id * @return */ public void getOrgIdByParentNo(List idList,Long id) { OrganizationExample example = new OrganizationExample(); example.createCriteria().andParentIdEqualTo(id).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED); List orgList = organizationMapper.selectByExample(example); if(orgList!=null && orgList.size()>0) { for(Organization o: orgList) { idList.add(o.getId()); getOrgIdByParentNo(idList, o.getId()); } } } }