package com.blueland.ebpm.rest;

import java.util.Date;
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.PathVariable;
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.Org;
import com.blueland.bpm.identity.model.User;
import com.blueland.ebpm.home.dao.OrgEntityDao;
import com.blueland.ebpm.home.dao.UserEntityDao;
import com.blueland.ebpm.home.model.UserEntity;
import com.blueland.ebpm.home.model.UserPostEntity;

@Controller
@RequestMapping(value = "/userpost")
public class UserPostSyncController {
	@Autowired
	private UserEntityDao userEntityDao;
	@Autowired
	private OrgEntityDao orgEntityDao;
	// 任岗类型
	private static String POSTTYPE = "parttime";
	

	/**
	 * 新增任岗信息
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "", method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public  Map<String,Object> saveUser(@RequestBody UserPostEntity 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.findUserByUserId(model.getStaffPostCode());
			Org org = orgEntityDao.findOrgByDeptId(model.getDeptCode());
			if(org==null){
				result.put("resultMsg", "操作失败：该部门不存在，deptCode:"+model.getDeptCode()+",deptName:"+model.getDeptName());
				result.put("resultCode", "500");
				result.put("result", null);
				return result;
			}
			// 如果能查到数据，就根据任岗编码更新数据
			User user = new User();
			if (obj != null ) {
				user = dealUser(model,obj,org);
				userEntityDao.updateByUserId(user);
			}
			// 任岗编码查不到，就查员工号 remark
			else {
				User userEmpno = userEntityDao.findUserByRemark(model.getStaffNum());
				// 查询员工号 不为空，就插入数据
				if(userEmpno != null){
					user = dealUser(model,userEmpno,org);
					userEntityDao.insertObj(user);
				}else{
					result.put("resultMsg", "操作失败,没有查到该用户信息：staffNum="+model.getStaffNum());
					result.put("resultCode", "500");
					result.put("result", null);
				}
			}
			result.put("resultMsg", "操作成功");
			result.put("resultCode", "200");
			result.put("result", user);
		}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", method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public  Map<String,Object> updateUser(@RequestBody UserPostEntity 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.findUserByUserId(model.getStaffPostCode());
			Org org = orgEntityDao.findOrgByDeptId(model.getDeptCode());
			if(org==null){
				result.put("resultMsg", "操作失败：该部门不存在，deptCode:"+model.getDeptCode()+",deptName:"+model.getDeptName());
				result.put("resultCode", "500");
				result.put("result", null);
				return result;
			}
			// 不为空 才更新数据
			if (obj != null) {
				User user = dealUser(model,obj,org);
				userEntityDao.updateByUserId(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 dealUser(UserPostEntity model, User obj, Org org) {
		if (model.getStaffPostType().equals(POSTTYPE)) { // 如果是任岗才更新userId
			obj.setUserId(model.getStaffPostCode());
		}
		obj.setUserCode(model.getStaffCode());
		obj.setRemark(model.getStaffNum());
		obj.setOrgId(org.getOrgId());
		obj.setOrgName(org.getOrgName());
		obj.setCompanyId(org.getCompanyId());
		obj.setCompanyName(org.getCompanyName());
		obj.setDeptId(model.getDeptCode());
		obj.setDeptName(model.getDeptName());
		obj.setUserType(model.getStaffPostType());
		obj.setPositionId(model.getPositionCode());
		obj.setUserStatus(model.getStatus().equals("enable")?"QY":"JY");
		obj.setUserUpdateTime(new Date());
		return obj;
	}
	
	/**
	 * 用户任岗删除接口
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "{userId}", method=RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public  Map<String,Object> deleteOrg(@PathVariable String userId){
		Map<String,Object> result=new HashMap<String,Object>();
		try {
			// 逻辑删除
			userEntityDao.updateStatusByUserId(userId);
			result.put("resultMsg", "操作成功");
			result.put("resultCode", "200");
			result.put("result", true);
		}catch(Exception e) {
			result.put("resultMsg", "操作失败："+e.getMessage());
			result.put("resultCode", "500");
			result.put("result", null);
		}
		return result;
	}
	
}
