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.UserService;
import org.jeecg.modules.employAuthenticationCompany.entity.EmployAuthenticationCompany;
import org.jeecg.modules.employAuthenticationCompany.service.IEmployAuthenticationCompanyService;
import org.jeecg.modules.employAuthenticationPerson.entity.EmployAuthenticationPerson;
import org.jeecg.modules.employAuthenticationPerson.service.IEmployAuthenticationPersonService;
import org.jeecg.modules.employJob.service.IEmployJobService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.springframework.stereotype.Service;

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

@Service
public class UserServiceImpl implements UserService {
    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

    //用户信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;

    //个人实名信息
    @Resource
    private IEmployAuthenticationPersonService employAuthenticationPersonService;

    //企业实名信息
    @Resource
    private IEmployAuthenticationCompanyService employAuthenticationCompanyService;
    /******************************************************************************************************************/

    //个人信息-查看个人信息
    @Override
    public Result<?> queryUser(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

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

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

        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);

        //返回信息
        Map map = new HashMap();

        try{
            //获取个人实名信息
            EmployAuthenticationPerson ahthenticationPerson = employAuthenticationPersonService
                    .lambdaQuery()
                    .eq(EmployAuthenticationPerson::getUserId, hanHaiMember.getId())
                    .one();

            //获取企业实名信息
            EmployAuthenticationCompany ahthenticationCompany = employAuthenticationCompanyService
                    .lambdaQuery()
                    .eq(EmployAuthenticationCompany::getUserId, hanHaiMember.getId())
                    .one();

            //组装个人基础数据
            map.put("amount",hanHaiMember.getPrice());//余额
            if(null != ahthenticationPerson){
                map.put("personAuthenticationStatus",ahthenticationPerson.getStatus());//个人实名状态
            }else {
                map.put("personAuthenticationStatus","-1");//个人实名状态
            }
            if(null != ahthenticationCompany){
                map.put("companyAuthenticationStatus",ahthenticationCompany.getStatus());//企业实名状态
            }else {
                map.put("companyAuthenticationStatus","-1");//企业实名状态
            }
            return Result.OK("获取个人中心基础数据信息",map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取个人中心基础数据信息失败",map);
        }
    }
}
