package com.yuchen.saas.park.service.service.property;

import com.yuchen.saas.manage.user.api.entity.User;
import com.yuchen.saas.manage.user.api.feign.IUserClient;
import com.yuchen.saas.park.api.enums.property.YesOrNoEnum;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PropertyAppCustomerService {
    @Resource
    private IPropertyCustomerArchivesService iPropertyCustomerArchivesService;
    @Resource
    private IUserClient userClient;

    public Long getAppPropertyCustomerArchivesId(Integer isPersonal) {
        if (Func.isEmpty(isPersonal)) {
            throw new ServiceException("isPersonal参数不能为空");
        }
        Long customerId;
        if (isPersonal == YesOrNoEnum.YES.getValue()) { // 小程序个人
            User user = userClient.userInfoById(AuthUtil.getUserId()).getData();
            customerId = iPropertyCustomerArchivesService.getPersonalCustomerArchivesId(user.getPhone());
            if (customerId == null) {
                throw new ServiceException("当前手机号未在物业建立客户档案信息");
            }
        } else if (isPersonal == YesOrNoEnum.NO.getValue()) {
            // 企业端
            String deptId = AuthUtil.getUser().getDeptId();
            customerId = iPropertyCustomerArchivesService.getOrgCustomerArchivesId(Long.parseLong(deptId));
            if (customerId == null) {
                throw new ServiceException("该组织未进行入驻企业审核登记");
            }
        } else {
            throw new ServiceException("获取物业客户id参数错误isPersonal="+isPersonal);
        }

        return customerId;
    }
}
