package org.jeecg.modules.api.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.api.service.UserInfoService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {

    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;
    //用户信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    /******************************************************************************************************************/

//查询个人信息
    @Override
    public Result<?> queryUserInfo(String token){
        log.info("开始查询个人信息");
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById(token);

        try{
            log.info("个人信息获取成功");
            return Result.OK("个人信息", hanHaiMember);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("个人信息获取失败", hanHaiMember);
        }
    }

    //修改个人信息
    @Override
    public Result<?> updateUserInfo(String token, HanHaiMember member){
        log.info("开始修改个人信息");
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById(token);

        try{
            //用户昵称不能为空
            if(StringUtils.isBlank(member.getNickName())){
                log.error("用户昵称不能为空");
                return Result.error("用户昵称不能为空");
            }
            if(StringUtils.isBlank(member.getNickName())){
                log.error("头像不能为空");
                return Result.error("头像不能为空");
            }
            if(StringUtils.isNotEmpty(member.getPhone())){
                HanHaiMember one = hanHaiMemberService
                        .lambdaQuery()
                        .eq(HanHaiMember::getPhone, member.getPhone())
                        .ne(HanHaiMember::getId, hanHaiMember.getId())
                        .one();
                if(null != one){
                    return Result.error("该号码已经有人使用，不能重复填写");
                }
            }

            hanHaiMember.setNickName(member.getNickName());
            hanHaiMember.setPhone(member.getPhone());
            hanHaiMember.setHeadImage(member.getHeadImage());
            boolean result = hanHaiMemberService.updateById(hanHaiMember);

            if(result){
                log.info("个人信息修改成功");
                return Result.OK("个人信息修改成功");
            }else {
                log.info("个人信息修改失败");
                return Result.error("个人信息修改失败");
            }

        }catch (Exception e){
            log.info("个人信息修改失败");
            e.printStackTrace();
            return Result.error("个人信息修改失败");
        }
    }

}
