package com.uas.platform.b2b.mobile.service.impl;

import java.util.List;

import com.uas.sso.entity.UserView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.uas.sso.util.AccountUtils;
import com.uas.platform.b2b.dao.CommonDao;
import com.uas.platform.b2b.dao.EnterpriseDao;
import com.uas.platform.b2b.dao.UserDao;
import com.uas.platform.b2b.dao.UserPositionDao;
import com.uas.platform.b2b.mobile.service.AddUserService;
import com.uas.platform.b2b.model.Enterprise;
import com.uas.platform.b2b.model.User;
import com.uas.platform.b2b.model.UserPosition;
import com.uas.platform.b2b.support.UsageBufferedLogger;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.encry.Md5Utils;

@Service
public class AddUserServiceImpl implements AddUserService {

	static final String defaultPassword = "111111";

	private final static UsageBufferedLogger logger = BufferedLoggerManager.getLogger(UsageBufferedLogger.class);

	@Autowired
	private UserDao userDao;

	@Autowired
	private CommonDao commonDao;

	@Autowired
	private EnterpriseDao enterpriseDao;

	@Autowired
	private UserPositionDao userPositionDao;

	@Override
	public ModelMap addUser(User user, String dept, String role, Long enuu) {
		ModelMap map = new ModelMap();
		List<User> users = userDao.findUserByUserTel(user.getUserTel().trim());
		if (users.size() > 0) {
			map.put("error", "电话号码重复，建议输入您自己的手机号");
		}
		List<User> users2 = userDao.findUserByUserEmail(user.getUserEmail().trim());
		if (users2.size() > 0) {
			map.put("error", "邮箱地址重复，建议输入您自己的电子邮箱地址");
		}
		Long uu = commonDao.queryForObject("select users_seq.nextval from dual", Long.class);
		user.setUserUU(uu);
		Enterprise enter = enterpriseDao.findEnterpriseByUu(enuu);
		user.addEnterprise(enter);
		user = userDao.save(user);
		user.setUserPwd(Md5Utils.encode(defaultPassword, user.getUserUU()));
		try {
			postToAccountCenter(user, enuu, defaultPassword);
			user = userDao.save(user);
			if (user.getUserUU() != null) {
				UserPosition position = new UserPosition();
				position.setDepartment(dept);
				position.setEnuu(enuu);
				position.setEnname(enter.getEnName());
				position.setRole(role);
				position.setUsername(user.getUserName());
				position.setUseruu(user.getUserUU());
				userPositionDao.save(position);
				if (position.getId() != null) {
					logger.log("uu互联添加人员", enter.getEnName() + "添加了" + user.getUserName());
					map.put("success", "人员添加成功");
				} else {
					map.put("success", "职位设置失败");
				}
			} else {
				map.put("success", false);
			}
		} catch (Exception e) {
			userDao.delete(user);
			map.put("success", false);
		}
		return map;
	}

	/**
	 * 同步数据到账户中心
	 * 
	 * @param user
	 * @param password
	 * @throws Exception
	 */
	public void postToAccountCenter(User user, Long enuu, String password) throws Exception {
		UserView detail = new UserView();
		detail.setEmail(user.getUserEmail());
		detail.setIdCard(user.getUserIdcode());
		detail.setMobile(user.getUserTel());
		detail.setVipName(user.getUserName());
		detail.setSex(user.getUserSex());
		detail.setPassword(password);
		UserView userView = AccountUtils.addUser(enuu, detail);
		user.setUserUU(userView.getUserUU());
	}

}
