package com.hzs.system.sys.controller.manage;

import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.domain.system.sys.SysUser;
import com.hzs.common.core.utils.TransactionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hzs.common.core.constant.UserConstants;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.sys.service.ISysUserService;

/**
 * 个人信息 业务处理
 *
 * @author hzs
 */
@RestController
@RequestMapping("/manage/profile")
public class SysProfileController extends BaseController {

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 个人信息
     */
    @Log(module = EOperationModule.HOME_PAGE, business = EOperationBusiness.SYS_INFO, method = EOperationMethod.SELECT)
    @GetMapping
    public AjaxResult profile() {
        String username = SecurityUtils.getUsername();
        SysUser user = iSysUserService.selectUserByUserName(username);
        return AjaxResult.success(user);
    }

    /**
     * 修改用户
     */
    @Log(module = EOperationModule.HOME_PAGE, business = EOperationBusiness.SYS_INFO, method = EOperationMethod.UPDATE)
    @PutMapping
    public AjaxResult updateProfile(@RequestBody SysUser user) {
        user.setUserName(SecurityUtils.getUsername());
        if (StringUtils.isNotEmpty(user.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(iSysUserService.checkPhoneUnique(user.getPhonenumber(), SecurityUtils.getUserId()))) {
            return AjaxResult.error("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        user.setUserId(SecurityUtils.getUserId());
        user.setPassword(null);
        if (iSysUserService.updateUserProfile(user) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("修改个人信息异常，请联系管理员");
    }

    /**
     * 重置密码
     */
    @Log(module = EOperationModule.HOME_PAGE, business = EOperationBusiness.SYS_INFO, method = EOperationMethod.UPDATE)
    @PutMapping("/updatePwd")
    public AjaxResult updatePwd(String oldPassword, String newPassword) {
        String username = SecurityUtils.getUsername();
        SysUser user = iSysUserService.selectUserByUserName(username);
        String password = user.getPassword();
        if (!SecurityUtils.matchesPassword(oldPassword, password)) {
            // 旧密码错误
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.PASSWORD_ERROR));
        }
        if (SecurityUtils.matchesPassword(newPassword, password)) {
            return AjaxResult.error("新密码不能与旧密码相同");
        }
        if (iSysUserService.resetUserPwd(username, SecurityUtils.encryptPassword(newPassword)) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("修改密码异常，请联系管理员");
    }

}
