/**
 * 文件：UserController.java
 * 版本：1.0.0
 * 日期：
 * Copyright &reg;
 * All right reserved.
 */
package com.mortals.xhx.base.system.user.web;


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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.xhx.common.code.UserStatus;
import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.common.code.UserType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.framework.annotation.Operlog;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.service.UserService;

/**
 * <p>Title: 用户信息</p>
 * <p>Description: UserController  </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 * @author
 * @version 1.0.0
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseCRUDJsonMappingController<UserService, UserForm, UserEntity, Long> {

    @Autowired
    private UserService userService;

    public UserController() {
        super.setFormClass(UserForm.class);
        super.setModuleDesc("用户信息");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, UserForm form,
                        Map<String, Object> model, Context context) {
        Map<String, Object> statsus = new HashMap<String, Object>();
        statsus.put("userType", IBaseEnum.getEnumMap(UserType.class));

        statsus.put("status", UserStatus.getEnumMap());
        model.put(KEY_RESULT_DICT, statsus);
        super.init(request, response, form, model, context);
    }

    @Override
    protected void doListBefore(HttpServletRequest request, HttpServletResponse response, UserForm form, Map<String, Object> model, Context context) throws AppException {
        if (!StringUtils.isEmpty(form.getQuery().getRealName())) {
            form.getQuery().setRealName("%".concat(form.getQuery().getRealName()).concat("%"));
        }
        if (!StringUtils.isEmpty(form.getQuery().getLoginName())) {
            form.getQuery().setLoginName("%".concat(form.getQuery().getLoginName()).concat("%"));
        }
    }

    @Override
    protected void editBefore(HttpServletRequest request, HttpServletResponse response, UserForm form,
                              Map<String, Object> model, Context context) throws AppException {
        form.getEntity().setLoginPwd(null);
        form.getEntity().setLoginPwd1(null);
        form.getEntity().setLoginPwd2(null);
        super.editBefore(request, response, form, model, context);
    }

    @Override
    protected int editAfter(HttpServletRequest request, HttpServletResponse response, UserForm form,
                            Map<String, Object> model, UserEntity entity, Context context) throws AppException {
        entity.setLoginPwd(null);
        entity.setLoginPwd1(null);
        entity.setLoginPwd2(null);
        return super.editAfter(request, response, form, model, entity, context);
    }

    @Override
    protected int saveAfter(HttpServletRequest request, HttpServletResponse response, UserForm form, Map<String, Object> model, Context context) throws AppException {

        if (form.getEntity().getId() == getCurUser().getId()) {
            saveCurrUserForSession(request, response, userService.get(form.getEntity().getId(), false));
        }
        return VALUE_RESULT_SUCCESS;
    }

    @Override
    protected void saveBefore(HttpServletRequest request, HttpServletResponse response, UserForm form,
                              Map<String, Object> model, Context context) throws AppException {
        if (service.existUser(form.getEntity().getLoginName(), form.getEntity().getId())) {
            throw new AppException("登录名已存在！");
        }
        super.saveBefore(request, response, form, model, context);
    }

    @RequestMapping(value = "change/password", method = RequestMethod.POST)
    @Operlog(msg = "密码修改成功!")
    public String changePassword(@RequestParam String oldPwd, @RequestParam String newPwd) {
        JSONObject ret = new JSONObject();
        try {
            service.updateUserPwd(super.getCurUser().getLoginName(), oldPwd, newPwd);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_MSG, "密码修改成功！");
        } catch (Exception e) {
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return ret.toJSONString();
    }



}