package org.jeecg.modules.apiService.impl;

import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiService.AuthenticationService;
import org.jeecg.modules.employAhthenticationCompany.entity.EmployAhthenticationCompany;
import org.jeecg.modules.employAhthenticationCompany.service.IEmployAhthenticationCompanyService;
import org.jeecg.modules.employAuthenticationPerson.entity.EmployAuthenticationPerson;
import org.jeecg.modules.employAuthenticationPerson.service.IEmployAuthenticationPersonService;
import org.jeecg.modules.employCompany.entity.EmployCompany;
import org.jeecg.modules.employCompany.service.IEmployCompanyService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AuthenticationServiceImpl implements AuthenticationService {

    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;
    //个人实名认证表
    @Resource
    private IEmployAuthenticationPersonService employAuthenticationPersonService;

    //企业实名认证表
    @Resource
    private IEmployAhthenticationCompanyService employAhthenticationCompanyService;

    //企业信息表
    @Resource
    private IEmployCompanyService employCompanyService;
    /******************************************************************************************************************/

    //会员中心-个人实名认证
    @Override
    public Result<?> addAuthenticationPerson(String token, EmployAuthenticationPerson employAuthenticationPerson) {
        String message = "";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

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

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

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

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

    }


    //lzx:我的服务-查询个人实名认证信息
    @Override
    public Result<?> getAuthenticationPerson(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        try{
            EmployAuthenticationPerson one = employAuthenticationPersonService.lambdaQuery()
                    .eq(EmployAuthenticationPerson::getUserId, hanHaiMember.getId())
                    .one();
            return Result.OK("个人实名认证信息", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.OK("个人实名认证信息查询失败");
        }
    }

    //会员中心-企业实名认证
    @Override
    public Result<?> addAuthenticationCompany(String token, EmployAhthenticationCompany employAhthenticationCompany) {
        String message = "企业实名认证操作失败";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            EmployAhthenticationCompany one = employAhthenticationCompanyService.lambdaQuery()
                    .eq(EmployAhthenticationCompany::getUserId, hanHaiMember.getId())
                    .one();
            //如果存在则修改信息
            if(null != one){
                if(!"1".equals(one.getStatus())){
                    employAhthenticationCompany.setId(one.getId());
                    employAhthenticationCompany.setStatus("0");
                    employAhthenticationCompanyService.updateById(employAhthenticationCompany);
                    message = "企业实名修改成功！";
                }else {
                    message = "审核已通过，不能修改！";
                }
            }else {
                employAhthenticationCompany.setUserId(hanHaiMember.getId());
                boolean result = employAhthenticationCompanyService.save(employAhthenticationCompany);
                message = "企业实名添加成功！";
            }
            return Result.OK(message);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(message);
        }
    }

    //我的服务-查询企业实名认证信息
    @Override
    public Result<?> getAuthenticationCompany(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        try{
            EmployAhthenticationCompany one = employAhthenticationCompanyService.lambdaQuery()
                    .eq(EmployAhthenticationCompany::getUserId, hanHaiMember.getId())
                    .one();
            return Result.OK("企业实名认证信息", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("企业实名认证信息查询失败");
        }
    }

}
