package com.goafanti.admin.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.goafanti.admin.bo.InputAdmin;
import com.goafanti.admin.service.AdminService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.enums.AdminFields;
import com.goafanti.common.model.Admin;
import com.goafanti.common.utils.PasswordUtil;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.shiro.token.TokenManager;

@Controller
@RequestMapping(value = "/api/admin")
public class AdminSystemController extends BaseApiController {

	@Resource
	private AdminService					adminService;
	@Resource(name = "passwordUtil")
	private PasswordUtil					passwordUtil;
	
	
	/**
	 * 管理员修改本人信息
	 */
	@RequestMapping(value = "/updateAdminInfo", method = RequestMethod.POST)
	public Result updateAdminInfo(@Valid InputAdmin admin, String pwd, BindingResult bindingResult) {
		Result res = new Result();
		/*if (TokenManager.hasRole(AFTConstants.SUPERADMIN)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "超级管理员"));
			return res;
		}*/

		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					AdminFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}
		if (StringUtils.isBlank(admin.getId())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "用户id"));
			return res;
		}

		if (StringUtils.isBlank(admin.getMobile())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "登录帐号为空", "登录帐号"));
			return res;
		}

		if (StringUtils.isBlank(admin.getName())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "用户名为空", "用户名"));
			return res;
		}
		Admin a = adminService.selectByMobile(admin.getMobile().trim());
		if (!TokenManager.getAdminId().equals(a.getId())) {
			res.getError().add(buildError(ErrorConstants.USER_ALREADY_EXIST, "当前用户已注册！"));
			return res;
		}
		Admin ad = new Admin();
		BeanUtils.copyProperties(admin, ad);
		Admin aa = adminService.selectByPrimaryKey(TokenManager.getAdminId());
		ad.setMobile(ad.getMobile().trim());
		ad.setCreateTime(aa.getCreateTime());
		if (StringUtils.isNotBlank(ad.getPassword())) {
			if (StringUtils.isBlank(pwd)) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "原密码"));
				return res;
			}
			if (!aa.getPassword().equals(passwordUtil.getEncryptPwd(pwd, aa.getCreateTime()))) {
				res.getError().add(buildError(ErrorConstants.PWD_NOT_MATCH_ERROR, "", "原密码"));
				return res;
			}
			ad.setPassword(passwordUtil.getEncryptPwd(ad));
		} else {
			ad.setPassword(aa.getPassword());
		}
		res.setData(adminService.updateByPrimaryKey(ad));
		return res;
	}

}
