欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

两个大数字相乘

程序员文章站 2022-06-05 16:22:33
...

没什么只是.一个demo

 

 

package net.csdn.service.tag.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import love.cq.util.StringUtil;
import net.csdn.exception.ArgumentErrorException;
import net.csdn.pojo.tag.ObjTag;
import net.csdn.pojo.tag.Tag;
import net.csdn.service.tag.util.domain.MyTag;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

@IocBean
public class DocTagService {
	private static final Log log = Logs.getLog("DocTagService");
	@Inject
	private TagsManageService tagsManageService = null;

	@Inject
	private DocStoreRemoteService docStoreRemoteService;

	public boolean insert(String typeName, String jsonDate, String tagsStr) {
		if (StringUtils.isBlank(tagsStr)) {
			throw new ArgumentErrorException("tags 不可以为空");
		}
		ObjTag objTag = null;
		JSONObject fromObject = JSONObject.fromObject(jsonDate);
		log.info(fromObject.get("id") + "	" + tagsStr);
		String[] tags = tagsStr.split(",");
		String body = StringUtil.rmHtmlTag(fromObject.getString("body"));

		// /如果正文为空
		if (StringUtil.isBlank(body) || tags == null || tags.length == 0) {
			return false;
		}

		String tempTagStr = null;

		// 删除这条记录
		delete(typeName, fromObject.getInt("id"));

		for (int i = 0; i < tags.length; i++) {
			// 先查询tags是否存在于数据库中.如果不存在就创建一个
			tempTagStr = tags[i].toLowerCase().trim();
			if (StringUtil.isBlank(tempTagStr)) {
				continue;
			}
			Tag tag = tagsManageService.findTagByName(tempTagStr);

			if (tag == null) {
				tag = new Tag();
				tag.setName(tempTagStr);
				tagsManageService.saveTag(tag);
			}
			// 插入一条记录到数据库中
			objTag = new ObjTag();

			objTag.setObject_id(fromObject.getInt("id"));

			objTag.setTag_id(tag.getId());

			objTag.setCreated_at(fromObject.getLong("created_at"));

			try {
				objTag.setChannel_id(fromObject.getInt("channel_id"));
			} catch (Exception e) {
				log.info("channel get error!");
			}

			tagsManageService.saveAbstractObjTag(typeName, objTag);
		}
		return true;
	}

	public void setTagsManageService(TagsManageService tagsManageService) {
		this.tagsManageService = tagsManageService;
	}

	/**
	 * 在tag系统中删除文章
	 * 
	 * @param typeName
	 * @param objectId
	 */
	public void delete(String typeName, Integer objectId) {
		// TODO Auto-generated method stub
		if (objectId == null) {
			throw new ArgumentErrorException("待删除文章Id 不可以为空");
		}
		tagsManageService.deleteDocTag(typeName, objectId);
	}

	/**
	 * 超早tag的数量
	 * 
	 * @param typeName
	 * @param objectId
	 */
	public int findTagCount(String typeName, Integer objectId) {
		// TODO Auto-generated method stub
		if (objectId == null) {
			throw new ArgumentErrorException("待删除文章Id 不可以为空");
		}
		return tagsManageService.findTagCount(typeName, objectId);
	}

	/**
	 * tag补录,慎用会覆盖用户原本的tag
	 * 
	 * @param type
	 * @param fromId
	 * @param endId
	 * @return
	 */
	public void updateTagByDoc(String typeName, int fromId, int endId, RecommenderService rs, HashMap<String, Integer> tagMap, boolean needSuggestTag) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		fromId = Math.max(fromId, 1);
		for (int i = fromId; i <= endId; i++) {
			if (i % 100 == 0) {
				sb.append(i);
				try {
					insertTagsAndUpdateMongo(typeName, sb.toString(), rs, tagMap, needSuggestTag);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					log.error(e + " " + sb);
				}
				sb = new StringBuilder();
			} else {
				sb.append(i + ",");
			}
		}
		// 补录
		if (sb != null && sb.length() > 0) {
			sb.append(0);
			try {
				insertTagsAndUpdateMongo(typeName, sb.toString(), rs, tagMap, needSuggestTag);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error(e + " " + sb);
			}
		}
	}

	/**
	 * 插入tag
	 * 
	 * @param typeName
	 * @param ids
	 * @param rs
	 * @param tagMap
	 */
	private void insertTagsAndUpdateMongo(String typeName, String ids, RecommenderService rs, HashMap<String, Integer> tagMap, boolean needSuggestTag) {
		// TODO Auto-generated method stub

		JSONArray objs = docStoreRemoteService.findByIds(typeName, null, ids);
		JSONArray newObjs = new JSONArray();
		// 如果mongo那什么都没查出来就不走后面的了
		if (objs == null || objs.size() == 0) {
			return;
		}

		// 删除所有的关联
		tagsManageService.deleteDocTagByIds(typeName, ids);

		List<ObjTag> all = new ArrayList<ObjTag>();

		String title = null;
		String body = null;
		// 重新插入
		for (Object object : objs) {
			JSONObject job = (JSONObject) object;

			// 防止出现空hash
			if (job.size() <= 3) {
				continue;
			}

			List<MyTag> tag_suggest = null;

			try {

				StringBuilder sb = new StringBuilder();
				if (needSuggestTag) {

					try {
						title = job.getString("title");

					} catch (Exception e) {
					}

					try {
						body = job.getString("body");
					} catch (Exception e) {
					}

					tag_suggest = rs.tag_suggest(title, body, "csdn", 5);
					for (MyTag myTag : tag_suggest) {
						all.add(insertTagsAndUpdateMongo(tagMap, job, sb, myTag, typeName));
					}
				} else {
					String tag = job.getString("tag");
					if (StringUtil.isNotBlank(tag)) {
						String[] tags = tag.split(",");
						for (String string : tags) {
							if (StringUtil.isNotBlank(string)) {
								MyTag myTag = new MyTag(string.trim().toLowerCase(), 0, string.length());
								all.add(insertTagsAndUpdateMongo(tagMap, job, sb, myTag, typeName));
							}
						}
					} else {
						continue;
					}
				}
				if (StringUtil.isNotBlank(sb) && sb.length() > 1) {
					job.put("tag", sb.subSequence(0, sb.length() - 1));
					newObjs.add(job);
					log.info("insert " + job.getString("tag"));
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		try {
			// 批量保存关联
			if (all.size() == 0) {
				log.info("not have any change to skip! " + ids.substring(0, 10));
				return;
			}

			if (needSuggestTag && newObjs.size() > 0) {
				// 批量修改mongo中的tag
				log.info("update mongo ok" + newObjs.size());
				docStoreRemoteService.update(typeName, newObjs);
			}

			StringBuilder sb = new StringBuilder("INSERT INTO ");
			sb.append(tagsManageService.getTableName(typeName));
			ObjTag objTag = null;
			sb.append(" (tag_id,object_id,created_at,channel_id) values ");
			for (int i = 0; i < all.size(); i++) {
				objTag = all.get(i);
				sb.append("(");
				sb.append(objTag.getTag_id());
				sb.append(",");
				sb.append(objTag.getObject_id());
				sb.append(",");
				sb.append(objTag.getCreated_at());
				sb.append(",");
				sb.append(objTag.getChannel_id());
				if (i == all.size() - 1) {
					sb.append(");");
				} else {
					sb.append("),");
				}
			}

			tagsManageService.execute(sb.toString());
			log.info("batchSave mysql ok" + all.size());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("batchSave mysql error" + ids);
			e.printStackTrace();
		}
	}

	private ObjTag insertTagsAndUpdateMongo(HashMap<String, Integer> tagMap, JSONObject job, StringBuilder sb, MyTag myTag, String type)
			throws InstantiationException, IllegalAccessException {
		sb.append(myTag.getName() + ",");
		Integer tagId = tagMap.get(myTag.getName());
		// 如果不在hash表中就查数据库
		if (tagId == null) {
			Tag tag = tagsManageService.findTagByName(myTag.getName());
			// 如果为空就插入一个
			if (tag == null) {
				tag = new Tag();
				tag.setName(myTag.getName());
				tagsManageService.saveTag(tag);
			}
			// 设置到map中
			tagMap.put(tag.getName(), tag.getId());
			tagId = tag.getId();
		}
		// 插入一条记录到数据库中
		ObjTag objTag = new ObjTag();

		objTag.setObject_id(job.getInt("id"));

		objTag.setTag_id(tagId);

		objTag.setCreated_at(job.getLong("created_at"));

		try {
			objTag.setChannel_id(job.getInt("channel_id"));
		} catch (Exception e) {
			objTag.setChannel_id(-1);
			log.error("channel get error!");
		}

		return objTag;
	}

}