package org.jeecg.modules.apiService.impl;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiService.UserInfoService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.teambuyLevel.entity.TeambuyLevel;
import org.jeecg.modules.teambuyLevel.service.ITeambuyLevelService;
import org.jeecg.modules.teambuyTeamLeader.entity.TeambuyTeamLeader;
import org.jeecg.modules.teambuyTeamLeader.service.ITeambuyTeamLeaderService;
import org.jeecg.modules.teambuyTeamRelation.entity.TeambuyTeamRelation;
import org.jeecg.modules.teambuyTeamRelation.service.ITeambuyTeamRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;
    //用户信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    //佣金等级信息
    @Resource
    private ITeambuyLevelService teambuyLevelService;
    //团长团员关联
    @Resource
    private ITeambuyTeamRelationService teambuyTeamRelationService;
    //团长信息
    @Resource
    private ITeambuyTeamLeaderService teambuyTeamLeaderService;
    /******************************************************************************************************************/

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

        try{
            HanHaiMember byId = hanHaiMemberService.getById(hanHaiMember.getId());
            return Result.OK(byId);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("用户信息获取失败", null);
        }
    }

    //修改个人信息
    @Override
    public Result<?> updateUserInfo(String token, String nickName, String phone, String headImage){
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById("1919589727643111426");

        try{
            //用户昵称不能为空
            if(StringUtils.isBlank(nickName)){
                return Result.error("用户昵称不能为空");
            }
            if(StringUtils.isBlank(headImage)){
                return Result.error("头像不能为空");
            }

            hanHaiMember.setNickName(nickName);
            hanHaiMember.setPhone(phone);
            hanHaiMember.setHeadImage(headImage);
            hanHaiMemberService.updateById(hanHaiMember);

            return Result.OK("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("修改失败");
        }
    }

    //获取个人中心基础数据信息
    @Override
    public Result<?> getUserCenterData(String token){
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById("1919589727643111426");
        //返回信息
        Map map = new HashMap();

        try{

            //组装个人基础数据
            map.put("member",hanHaiMember);//用户基础信息

            map.put("score",hanHaiMember.getIntegerPrice());//积分
            map.put("balance",hanHaiMember.getBalance());//余额

            //团长等级
            TeambuyLevel level = teambuyLevelService.getById(hanHaiMember.getLeaderLevelId());
            map.put("level",level);

            //我的团长
            TeambuyTeamRelation relation = teambuyTeamRelationService
                    .lambdaQuery()
                    .eq(TeambuyTeamRelation::getMemberId, hanHaiMember.getId())
                    .one();
            TeambuyTeamLeader teamLeader = null;
            if(null != relation){
                teamLeader = teambuyTeamLeaderService.getById(relation.getLeaderId());
            }
            map.put("teamLeader", teamLeader);

            //推广佣金
            //优惠券

            return Result.OK("获取个人中心基础数据信息",map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取个人中心基础数据信息失败",map);
        }
    }

}
