package com.blueland.ebpm.rest;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blueland.bpm.identity.model.User;
import com.blueland.ebpm.home.dao.UserEntityDao;
import com.blueland.ebpm.home.model.UserEntity;

@Controller
@RequestMapping(value = "/staff")
public class UserSyncController {
	@Autowired
	private UserEntityDao userEntityDao;
	private static String PWD = "000000";
	// 任岗类型
	private static String POSTTYPE = "parttime";
	
	private static String STATUS_JY = "JY";

	/**
	 * 同步新增用户接口
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "", method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public  Map<String,Object> saveUser(@RequestBody UserEntity model){
		Map<String,Object> result=new HashMap<String,Object>();
		try {
			if(StringUtils.isEmpty(model.getStaffPostCode())){
				result.put("resultMsg", "操作失败：StaffPostCode不能为空");
				result.put("resultCode", "500");
				result.put("result", null);
				return result;
			}
			User obj = userEntityDao.findUserByRemark(model.getStaffNum());
			User user = dealUser(model);
			// 判断主岗是否存在
			if(obj == null){
				// 判断账号是否存在
				obj = userEntityDao.findUserByUserId(model.getStaffCode());
				if(obj == null){
					// 主岗、账号都不存在就新增
					userEntityDao.insertObj(user);
				}else{
					// 更新主岗信息
					userEntityDao.updateByUserId(user);
				}				
			}else{
				userEntityDao.updateByRemark(user);
			}			
			result.put("resultMsg", "操作成功");
			result.put("resultCode", "200");
			result.put("result", model);
		}catch(Exception e) {
			result.put("resultMsg", "操作失败："+e.getMessage());
			result.put("resultCode", "500");
			result.put("result", null);
			
		}
		return result;
	}
	/**
	 * 同步更新用户接口
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update/code", method=RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public  Map<String,Object> updateUser(@RequestBody UserEntity model){
		Map<String,Object> result=new HashMap<String,Object>();
		try {
			if(StringUtils.isEmpty(model.getStaffNum())){
				result.put("resultMsg", "操作失败：StaffNum不能为空");
				result.put("resultCode", "500");
				result.put("result", null);
				return result;
			}
			User obj = userEntityDao.findUserByUserId(model.getStaffCode());
			// 判断主岗是否存在
			if(obj == null){
				User user = dealUser(model);
				// 主岗、账号都不存在就新增
			    userEntityDao.insertObj(user);			
			}else{
				User user = dealUpdateUser(model);
				this.userEntityDao.updateByRemark(user);
			}			       
			// userEntityDao.updateByRemark(user);
			result.put("resultMsg", "操作成功");
			result.put("resultCode", "200");
			result.put("result", model);
		}catch(Exception e) {
			result.put("resultMsg", "操作失败："+e.getMessage());
			result.put("resultCode", "500");
			result.put("result", null);
		}
		return result;
	}
	private User dealUpdateUser(UserEntity model) {
		User user = new User();	
		user.setUserId(model.getStaffCode());
		user.setUserCode(model.getStaffCode());
		user.setUserName(model.getStaffName());
		user.setUserSex(model.getStaffSex());
		user.setUserMobile(model.getStaffMobile());
		user.setUserMail(model.getStaffMail());
		user.setUserStatus(model.getStaffStatus());
		user.setUserSort(model.getStaffSort());
		user.setRemark(model.getStaffNum());
		user.setCompanyId(model.getCompanyId());
		user.setCompanyName(model.getCompanyName());
		user.setOrgId(model.getOrgId());
		user.setOrgName(model.getOrgName());
		user.setDeptId(model.getDeptId());
		user.setDeptName(model.getDeptName());
		user.setPositionId(model.getPositionId());
		user.setUserType(model.getStaffPostType());
		return user;
	}
	// 拼装用户信息
	private User dealUser(UserEntity model) {
		User user = new User();
		user.setUserId(model.getStaffCode());
		if (StringUtils.isNotEmpty(model.getStaffPostType()) && model.getStaffPostType().equals(POSTTYPE)) { // 如果是任岗才更新userId
			user.setUserId(model.getStaffPostCode());
		}
		user.setUserCode(model.getStaffCode());
		user.setUserName(model.getStaffName());
		user.setUserSex(model.getStaffSex());
		user.setUserAge(0);
		user.setUserMobile(model.getStaffMobile());
		user.setUserMail(model.getStaffMail());
		user.setUserStatus(model.getStaffStatus());
		user.setUserSort(model.getStaffSort());
		user.setUserPwd(PWD);
		user.setRemark(model.getStaffNum());
		user.setCompanyId(model.getCompanyId());
		user.setCompanyName(model.getCompanyName());
		user.setOrgId(model.getOrgId());
		user.setOrgName(model.getOrgName());
		user.setDeptId(model.getDeptId());
		user.setDeptName(model.getDeptName());
		user.setPositionId(model.getPositionId());
		user.setUserType(model.getStaffPostType());
		return user;
	}
	
	
}
