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.apiService.CategoryService;
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.employCategory.service.IEmployCategoryService;
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 IEmployAuthenticationCompanyService employAuthenticationCompanyService;

    //分类信息
    @Resource
    private IEmployCategoryService employCategoryService;
    /******************************************************************************************************************/

    //实名认证-查询个人实名认证信息
    @Override
    public Result<?> getAuthenticationPerson(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        try{
            EmployAuthenticationPerson one = employAuthenticationPersonService.lambdaQuery()
                    .eq(EmployAuthenticationPerson::getUserId, hanHaiMember.getId())
                    .one();
            if(null == one){
                return Result.OK("个人实名认证信息", null);
            }

            one.setIndustryName(employCategoryService.getById(one.getIndustry()).getName());



            return Result.OK("个人实名认证信息", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.OK("个人实名认证信息查询失败");
        }
    }

    //实名认证-个人实名认证
    @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 authentication = employAuthenticationPersonService.lambdaQuery()
                    .eq(EmployAuthenticationPerson::getStatus, "1")
                    .one();

            if(null != authentication){
                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);
        }
    }


    //实名认证-查询企业实名认证信息
    @Override
    public Result<?> getAuthenticationCompany(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        try{
            EmployAuthenticationCompany one = employAuthenticationCompanyService.lambdaQuery()
                    .eq(EmployAuthenticationCompany::getUserId, hanHaiMember.getId())
                    .one();
            if(null == one){
                return Result.OK("企业实名认证信息", null);
            }

            one.setIndustryName(employCategoryService.getById(one.getIndustry()).getName());

            return Result.OK("企业实名认证信息", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("企业实名认证信息查询失败");
        }
    }

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

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

}
