package org.jeecg.modules.apiService.impl;

import com.alipay.api.domain.Car;
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.carrentCompany.entity.CarrentCompany;
import org.jeecg.modules.carrentCompany.service.ICarrentCompanyService;
import org.jeecg.modules.carrentUser.entity.CarrentUser;
import org.jeecg.modules.carrentUser.service.ICarrentUserService;
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.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    /*************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

    //个人信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;

    //个人注册信息
    @Resource
    private ICarrentUserService carrentUserService;

    //企业注册信息
    @Resource
    private ICarrentCompanyService carrentCompanyService;
    /*************************************************************************************/
    //用户信息-个人注册
    @Override
    public Result<?> addUser(String token, CarrentUser carrentUser) {
        //返回信息
        String message = "个人注册失败！";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //查询实名认证信息
            CarrentUser user = carrentUserService.lambdaQuery()
                    .eq(CarrentUser::getUserId, hanHaiMember.getId())
                    .one();

            //判断用户是否已实名认证通过
            CarrentUser cerNo = carrentUserService.lambdaQuery()
                    .eq(CarrentUser::getCerNo, carrentUser.getCerNo())
                    .eq(CarrentUser::getStatus, "1")
                    .one();

            if(null != cerNo){
                return Result.error("该身份证已认证过，不能重复使用！");
            }

            //如果存在则修改信息
            if(null != user){
                if(!"1".equals(user.getStatus())){
                    carrentUser.setId(user.getId());
                    carrentUser.setStatus("0");
                    carrentUserService.updateById(carrentUser);
                    message = "个人实名注册修改成功！";
                }else {
                    message = "审核已通过，不能修改！";
                }
            }else {
                carrentUser.setUserId(hanHaiMember.getId());
                boolean result = carrentUserService.save(carrentUser);
                message = "个人实名注册添加成功！";
            }
            return Result.OK(message);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(message);
        }

    }

    /******************************************************************************************************************/
    //查询个人信息
    @Override
    public Result<?> queryUserInfo(String token){
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        Map map = new HashMap();

        try{
            //用户信息
            HanHaiMember member = hanHaiMemberService.getById(hanHaiMember.getId());
            map.put("memberInfo", member);

            //获取个人实名信息
            CarrentUser personAuthenticationStatus = carrentUserService
                    .lambdaQuery()
                    .eq(CarrentUser::getUserId, hanHaiMember.getId())
                    .one();
            if(null != personAuthenticationStatus){
                map.put("personAuthenticationStatus",personAuthenticationStatus.getStatus());//个人实名状态
            }else {
                map.put("personAuthenticationStatus","-1");//个人实名状态
            }

            //获取企业实名信息
            CarrentCompany company = carrentCompanyService
                    .lambdaQuery()
                    .eq(CarrentCompany::getUserId, hanHaiMember.getId())
                    .one();
            if(null != company){
                map.put("companyAuthenticationStatus",company.getStatus());//企业实名状态
            }else {
                map.put("companyAuthenticationStatus","-1");//企业实名状态
            }

            return Result.OK("用户信息", map);

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

        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("修改失败");
        }
    }

}
