package org.jeecg.modules.apiService.impl;

import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiService.CompanyService;
import org.jeecg.modules.carrentCompany.entity.CarrentCompany;
import org.jeecg.modules.carrentCompany.service.ICarrentCompanyService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CompanyServiceImpl implements CompanyService {

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

    //企业信息
    @Resource
    private ICarrentCompanyService carrentCompanyService;
    /*************************************************************************************/

    //企业信息-企业注册
    @Override
    public Result<?> addCompany(String token, CarrentCompany carrentCompany) {
        String message = "企业实名注册操作失败";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

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