/**
 * 文件：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.List;
import java.util.Map;
import java.util.stream.Collectors;

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

import com.mortals.framework.service.IUser;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.role.model.RoleEntity;
import com.mortals.xhx.base.system.role.service.RoleService;
import com.mortals.xhx.common.code.UserStatus;
import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.common.code.UserType;
import com.mortals.xhx.common.key.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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 BaseCRUDJsonBodyMappingController<UserService, UserEntity, Long> {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

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

    @Override
    protected void init(Map<String, Object> model, Context context) {

        this.addDict(model,"userType", IBaseEnum.getEnumMap(UserType.class));
        this.addDict(model,"status", UserStatus.getEnumMap());
        List<RoleEntity> roleList = roleService.find(new RoleEntity());
        this.addDict(model,"roleId", roleList.stream().collect(Collectors.toMap(e->e.getId().toString(), e->e.getName())));
        super.init(model, context);
    }

    @Override
    protected int editAfter(Long id, Map<String, Object> model, UserEntity entity, Context context) throws AppException {
        entity.setLoginPwd(null);
        entity.setLoginPwd1(null);
        entity.setLoginPwd2(null);
        entity.setLoginPwd3(null);
        return 1;
    }

    @RequestMapping(value = "change/password", method = RequestMethod.POST)
    public String changePassword(@RequestBody UserEntity entity) {

        IUser user = this.getCurUser();
        if(user == null){
            return this.createFailJsonResp("请先登录");
        }
        JSONObject ret = new JSONObject();
        try {
            service.updateUserPwd(super.getCurUser().getLoginName(), entity.getOldPassword(), entity.getNewPassword());
            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();
    }
}