package cn.gov.jscd.experts.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.gov.jscd.experts.dao.CommonAccessor;
import cn.gov.jscd.experts.entity.OperateRecordEntity;
import cn.gov.jscd.experts.entity.UploadFilesEntity;
import cn.gov.jscd.experts.entity.UsersEntity;
import cn.gov.jscd.experts.service.CommonService;

@Service
public class CommonServiceImpl implements CommonService, InitializingBean {
	
	private Logger logger = Logger.getLogger(CommonServiceImpl.class);

	@Autowired
	private CommonAccessor commonAccessor;

	@Override
	public void saveFileRecord(UploadFilesEntity entity) {
		this.commonAccessor.saveOrUpdate(entity);
	}

	@Override
	public List<UploadFilesEntity> queryAttachments(Integer ownerId, Integer used) {
		return this.commonAccessor.queryAttachments(ownerId, used);
	}

	@Override
	public UploadFilesEntity queryAttachmentById(Integer id) {
		return this.commonAccessor.queryObjectById(UploadFilesEntity.class, id);
	}
	
	private Map<String, String> modelMap = new HashMap<>();

	@Override
	public void operate(UsersEntity user, String whatdo, String address) {
		if(null == user) {
			return;
		}
		try {
			OperateRecordEntity record = new OperateRecordEntity();
			record.setUserId(user.getId());
			record.setUserName(user.getRealName());
			record.setOperateContent(whatdo);
			record.setOperateTime(new Date());
			record.setAddress(address);
			
			StringBuffer buffer = new StringBuffer(user.getRealName());
			buffer.append("[").append(user.getId()).append(",").append(user.getUserCode()).append("],[");
			buffer.append(address).append("]").append(whatdo);
			this.logger.info(buffer.toString());
			
			this.commonAccessor.saveOrUpdate(record);
		} catch (Exception e) {
			this.logger.info("记录日志时发生异常");
		}
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		//1:专家库,2:专家审核,3:立项申请,4:立项审核,5:立项审查专家抽取,6:立项审查,7:评标专家抽取,8:评标,9:系统
		this.modelMap.put("1", "专家库");
		this.modelMap.put("2", "专家审核");
		this.modelMap.put("3", "立项申请");
		this.modelMap.put("4", "立项审核");
		this.modelMap.put("5", "立项审查专家抽取");
		this.modelMap.put("6", "立项审查");
		this.modelMap.put("7", "评标专家抽取");
		this.modelMap.put("8", "评标");
		this.modelMap.put("9", "系统");
	}
}
