package com.sckw.system.service;

import com.alibaba.fastjson.JSON;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.RegularUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.model.EntCertificateInfo;
import com.sckw.core.web.model.LoginEntInfo;
import com.sckw.redis.utils.RedissonUtils;
import com.sckw.system.dao.KwsEnterpriseDao;
import com.sckw.system.model.KwsEnterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author czh
 * @desc 公共方法
 * @date 2023/6/30
 */
@Service
public class CommonService {

    @Autowired
    private KwsEnterpriseDao kwsEnterpriseDao;

    /**
     * @desc: 更新资质
     * @param: entId 企业id entCertificateInfoList 资质信息
     * @author: czh
     * @date 2023/7/23
     */
    public void updateEntCertificate(Long entId, List<EntCertificateInfo> entCertificateInfoList) {
        String string = RedissonUtils.getString(Global.REDIS_ENTERPRISE_PREFIX + entId);
        if (!StringUtils.isBlank(string)) {
            LoginEntInfo loginEntInfo = JSON.parseObject(string, LoginEntInfo.class);
            loginEntInfo.setCertificateInfo(entCertificateInfoList);
            RedissonUtils.putString(Global.REDIS_ENTERPRISE_PREFIX + entId, JSON.toJSONString(loginEntInfo), Global.COMMON_EXPIRE);
        }
    }

    /**
     * @desc: 校验企业是否重复
     * @param: entName 企业名  phone 联系电话
     * @author: czh
     * @date 2023/7/23
     */
    public void checkEntRepeat(String entName, String phone) {
        List<KwsEnterprise> list = kwsEnterpriseDao.checkEntRepeat(entName, phone);
        if (CollectionUtils.isNotEmpty(list)) {
            throw new SystemException(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.ENT_EXISTS);
        }
    }


    /**
     * @param phone 手机号
     * @desc: 校验手机号
     * @author: czh
     * @date: 2023/7/26
     */
    public void checkPhone(String phone) {
        if (!RegularUtils.matchs(RegularUtils.PHONE_REG, phone)) {
            throw new SystemException(HttpStatus.PARAMETERS_PATTERN_ERROR_CODE, HttpStatus.CONTACTS_PHONE_ERROR);
        }
    }


}
