package com.pm.enterpriseInfo.service.impl;

import com.pm.common.config.PageData;
import com.pm.common.constant.UserConstants;
import com.pm.common.utils.DateUtils;
import com.pm.common.utils.StringUtils;
import com.pm.enterpriseInfo.mapper.EnterpriseMapper;
import com.pm.enterpriseInfo.service.IEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EnterpriseServiceImpl implements IEnterpriseService {

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Override
    public List<PageData> selectEnterpriseList(PageData pd) {
        return enterpriseMapper.selectEnterpriseList(pd);
    }

    @Override
    public PageData selectEnterpriseByEnterpriseId(Long enterpriseId) {
        return enterpriseMapper.selectEnterpriseByEnterpriseId(enterpriseId);
    }

    @Override
    public int insertEnterprise(PageData pd) {
        pd.put("createTime", DateUtils.getNowDate());
        // 根据信用代码查询企业信息
        PageData info = enterpriseMapper.selectEnterpriseByCreditCode(pd.getString("creditCode"));

        if (info != null) {
            throw new RuntimeException("修改企业'" + pd.getString("enterpriseName") + "'失败，统一社会信用代码已存在");
        }
        return enterpriseMapper.insertEnterprise(pd);
    }

    @Override
    public int updateEnterprise(PageData pd) {
        pd.put("updateTime", DateUtils.getNowDate());
        // 根据信用代码查询企业信息
        PageData info = enterpriseMapper.selectEnterpriseByCreditCode(pd.getString("creditCode"));
        if (info != null && !info.getString("enterpriseId").equals(pd.getString("enterpriseId"))) {
            throw new RuntimeException("修改企业'" + pd.getString("enterpriseName") + "'失败，统一社会信用代码已存在");
        }
        return enterpriseMapper.updateEnterprise(pd);
    }

    @Override
    public int deleteEnterpriseByEnterpriseIds(Long[] enterpriseIds) {
        return enterpriseMapper.deleteEnterpriseByEnterpriseIds(enterpriseIds);
    }

    @Override
    public int deleteEnterpriseByEnterpriseId(Long enterpriseId) {
        return enterpriseMapper.deleteEnterpriseByEnterpriseId(enterpriseId);
    }

    @Override
    public PageData getEnterpriseStatistics() {
        return enterpriseMapper.selectEnterpriseCount();
    }

}
