/**   
 * Copyright © 2018 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * 功能描述：
 * @Package: org.cs.webservice 
 * @author: <GZY>75166631@qq.com  
 * @date: 2018年6月20日 上午8:41:24 
 */
package org.cs.webservice.impl;

import javax.jws.WebParam;
import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;
import org.cs.mgr.user.model.MgmUser;
import org.cs.mgr.user.service.IMgmUserService;
import org.cs.util.Md5;
import org.cs.util.StringUtil;
import org.cs.webservice.IUserService;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;  
 
/** 
 * @author zhang 
 * @desc 实现IUserService接口 
 */  
@WebService  
public class UserServiceImpl implements IUserService {  
  
	
	@Autowired
	public IMgmUserService mgmUserService;
	
	/**   
	 * @Title: discontinuationMgmUser   
	 * @Description: 停用用户
	 * @param: @param id
	 * @param: @return         
	 * @see org.cs.webservice.IUserService#discontinuationMgmUser(long)   
	 */  
	@Override
	public JSONObject discontinuationMgmUser(@WebParam(name="code")String code) {
		MgmUser mgmUser=mgmUserService.getByPropertie(code, "personnelcode");
		if(mgmUser!=null){
			mgmUser.setIsDelete(true);
			mgmUserService.update(mgmUser);
			return getJson(1,null,"操作成功");
		}
		return getJson(0,null,"操作失败");
	}  
	
	/**   
	 * @Title: updateMgmUser   
	 * @Description: 修改用户信息
	 * @param: @param id
	 * @param: @return         
	 * @see org.cs.webservice.IUserService#discontinuationMgmUser(long)   
	 */  
	@Override
	public JSONObject updateMgmUser(@WebParam(name="code")String code, @WebParam(name="name")String name, @WebParam(name="idnumber")String idnumber,
									@WebParam(name="mail")String mail, @WebParam(name="qq")String qq, @WebParam(name="weixin")String weixin,
									@WebParam(name="province")String province, @WebParam(name="city")String city, @WebParam(name="area")String area,
									@WebParam(name="address")String address, @WebParam(name="deptcode")String deptcode, @WebParam(name="deptname")String deptname,
									@WebParam(name="deptpath")String deptpath) {
		MgmUser mgmUser=mgmUserService.getByPropertie(code,"personnelcode");
		if(mgmUser==null){
			return getJson(0,null,"不存在的用户！");
		}
		try {
			if(mgmUser!=null){
				if(StringUtil.isNotBlank(name)){
					mgmUser.setName(name);
				}
				if(StringUtil.isNotBlank(idnumber)){
					mgmUser.setIdnumber(idnumber);
				}
				if(StringUtil.isNotBlank(mail)){
					mgmUser.setMail(mail);
				}
				if(StringUtil.isNotBlank(qq)){
					mgmUser.setQq(qq);
				}
				if(StringUtil.isNotBlank(weixin)){
					mgmUser.setWeixin(weixin);
				}
				if(StringUtil.isNotBlank(province)){
					mgmUser.setProvince(province);
				}
				if(StringUtil.isNotBlank(city)){
					mgmUser.setCity(city);
				} 
				if(StringUtil.isNotBlank(area)){
					mgmUser.setArea(area);
				}  
				if(StringUtil.isNotBlank(address)){
					mgmUser.setAddress(address);
				}
				if(StringUtil.isNotBlank(deptcode)){
					mgmUser.setDeptCode(deptcode);
				}
				if(StringUtil.isNotBlank(deptname)){
					mgmUser.setDeptName(deptname);
				}
				if(StringUtil.isNotBlank(deptpath)){
					mgmUser.setDeptPath(deptpath);
				}
				mgmUserService.update(mgmUser);
				return getJson(1,null,"操作成功");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return getJson(0,null,"操作失败");
		}
		return getJson(0,null,"操作失败");
	}  
	
	/**   
	 * @Title: insertMgmUser   
	 * @Description: 新增用户
	 * @param: @param id
	 * @param: @return         
	 * @see org.cs.webservice.IUserService#discontinuationMgmUser(long)
	 */
	@Override
	public JSONObject insertMgmUser(@WebParam(name="code")String code, @WebParam(name="name")String name, @WebParam(name="idnumber")String idnumber,
									@WebParam(name="mail")String mail, @WebParam(name="qq")String qq, @WebParam(name="weixin")String weixin,
									@WebParam(name="province")String province, @WebParam(name="city")String city, @WebParam(name="area")String area,
									@WebParam(name="address")String address, @WebParam(name="deptcode")String deptcode, @WebParam(name="deptname")String deptname,
									@WebParam(name="deptpath")String deptpath) {
		MgmUser mgmUser=mgmUserService.getByPropertie(code,"personnelcode");
		if(mgmUser!=null){
			return getJson(0,null,"用户已存在！");
		}
		try {
			if(mgmUser==null){
				mgmUser = new MgmUser();
				if(StringUtil.isNotBlank(name)){
					mgmUser.setName(name);
				}
				if(StringUtil.isNotBlank(idnumber)){
					mgmUser.setIdnumber(idnumber);
				}
				if(StringUtil.isNotBlank(mail)){
					mgmUser.setMail(mail);
				}
				if(StringUtil.isNotBlank(qq)){
					mgmUser.setQq(qq);
				}
				if(StringUtil.isNotBlank(weixin)){
					mgmUser.setWeixin(weixin);
				}
				if(StringUtil.isNotBlank(province)){
					mgmUser.setProvince(province);
				}
				if(StringUtil.isNotBlank(city)){
					mgmUser.setCity(city);
				} 
				if(StringUtil.isNotBlank(area)){
					mgmUser.setArea(area);
				}  
				if(StringUtil.isNotBlank(address)){
					mgmUser.setAddress(address);
				}
				if(StringUtil.isNotBlank(deptcode)){
					mgmUser.setDeptCode(deptcode);
				}
				if(StringUtil.isNotBlank(deptname)){
					mgmUser.setDeptName(deptname);
				}
				if(StringUtil.isNotBlank(deptpath)){
					mgmUser.setDeptPath(deptpath);
				}

				String md5Pass = Md5.getMd5("123456" +code);
 
				mgmUser.setType("0");    //集团用户
				mgmUser.setPwd(md5Pass);
				mgmUser.setPersonnelcode(code);
				mgmUser.setInvitecode(code);
				mgmUser.setFirstLogin("0"); //首次登陆
				mgmUserService.add(mgmUser);
				return getJson(1,null,"操作成功");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return getJson(0,null,"操作失败");
		}
		return getJson(0,null,"操作失败");
	}  
	
	/**
	 * 
	 * @Title: getJson
	 * @Description: TODO
	 * @param ret
	 * @param result
	 * @param msg
	 * @return
	 * @return: JSONObject
	 */
	public JSONObject getJson(int ret, Object result, String msg) {
		JSONObject json = new JSONObject();
		json.put("ret", ret);
		json.put("data", result);
		json.put("msg", StringUtils.isBlank(msg)?"操作成功":msg);
		return json;
	}
}  
