package com.ruoyi.ehsEnterprise.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.ehsRisk.domain.EhsRisk;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;
import java.util.List;
import java.util.Map;
import com.ruoyi.ehsEnterprise.mapper.EhsEnterpriseMapper;
import com.ruoyi.ehsEnterprise.domain.EhsEnterprise;
import com.ruoyi.ehsEnterprise.service.IEhsEnterpriseService;

import javax.validation.Validator;

/**
 * 企业基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-08
 */
@Service
public class EhsEnterpriseServiceImpl extends ServiceImpl<EhsEnterpriseMapper, EhsEnterprise> implements IEhsEnterpriseService {

	@Autowired
	private EhsEnterpriseMapper ehsEnterpriseMapper;
	@Autowired
	protected Validator validator;
	/**
	 * 查询企业基本信息
	 *
	 * @param enterpriseId 企业基本信息主键
	 * @return 企业基本信息
	 */
	@Override
	public EhsEnterprise selectEhsEnterpriseByEnterpriseId(Long enterpriseId)
	{
		return ehsEnterpriseMapper.selectEhsEnterpriseByEnterpriseId(enterpriseId);
	}

	/**
	 * 查询企业基本信息列表
	 *
	 * @param ehsEnterprise 企业基本信息
	 * @return 企业基本信息
	 */
	@Override
	public List<EhsEnterprise> selectEhsEnterpriseList(EhsEnterprise ehsEnterprise)
	{
		if(SecurityUtils.getLoginUser().getUser().getDept().getParentId()!=null &&
			SecurityUtils.getLoginUser().getUser().getDept().getParentId().longValue()!=0){
			ehsEnterprise.setDeptId(SecurityUtils.getLoginUser().getUser().getDept().getDeptId().longValue());
	}
		return ehsEnterpriseMapper.selectEhsEnterpriseList(ehsEnterprise);
	}

	/**
	 * 导入数据
	 *
	 * @param list  数据列表
	 * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
	 * @return 结果
	 */
	@Override
	public String importEnterprise(List<EhsEnterprise> list, Boolean isUpdateSupport )
	{
		if (StringUtils.isNull(list) || list.size() == 0)
		{
			throw new ServiceException("导入数据不能为空！");
		}
		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		int i =0;
		for (EhsEnterprise obj : list)
		{
			i++;
			try
			{
				BeanValidators.validateWithException(validator, obj);
				ehsEnterpriseMapper.insert(obj);
				successNum++;
				successMsg.append("<br/>" + successNum + "、第 " + i + " 行记录导入成功");
			}
			catch (Exception e)
			{
				failureNum++;
				String msg = "<br/>" + failureNum + "、第 " +i + " 行记录导入失败：";
				failureMsg.append(msg + e.getMessage());
				log.error(msg, e);
			}
		}
		if (failureNum > 0)
		{
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new ServiceException(failureMsg.toString());
		}
		else
		{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}
	private LambdaQueryWrapper<EhsEnterprise> buildQueryWrapper(EhsEnterprise query) {
		Map<String, Object> params = query.getParams();
		LambdaQueryWrapper<EhsEnterprise> lqw = Wrappers.lambdaQuery();
					lqw.like(StringUtils.isNotBlank(query.getEnterpriseCode()), EhsEnterprise::getEnterpriseCode, query.getEnterpriseCode());
					lqw.like(StringUtils.isNotBlank(query.getEnterpriseName()), EhsEnterprise::getEnterpriseName, query.getEnterpriseName());
					lqw.eq(StringUtils.isNotBlank(query.getEnterpriseType()), EhsEnterprise::getEnterpriseType, query.getEnterpriseType());
					lqw.like(StringUtils.isNotBlank(query.getEnterpriseAddr()), EhsEnterprise::getEnterpriseAddr, query.getEnterpriseAddr());
					lqw.like(StringUtils.isNotBlank(query.getEnterpriseLeader()), EhsEnterprise::getEnterpriseLeader, query.getEnterpriseLeader());
					lqw.like(StringUtils.isNotBlank(query.getLeaderTel()), EhsEnterprise::getLeaderTel, query.getLeaderTel());
					lqw.like(StringUtils.isNotBlank(query.getRegisteredCapital()), EhsEnterprise::getRegisteredCapital, query.getRegisteredCapital());
					lqw.like(StringUtils.isNotBlank(query.getBusinessScope()), EhsEnterprise::getBusinessScope, query.getBusinessScope());
					lqw.like(StringUtils.isNotBlank(query.getBusinessProjects()), EhsEnterprise::getBusinessProjects, query.getBusinessProjects());
					lqw.eq(StringUtils.isNotBlank(query.getEnterpriseStatus()), EhsEnterprise::getEnterpriseStatus, query.getEnterpriseStatus());
					lqw.like(StringUtils.isNotBlank(query.getSafetyManagerName()), EhsEnterprise::getSafetyManagerName, query.getSafetyManagerName());
					lqw.like(StringUtils.isNotBlank(query.getSafetyManagerTel()), EhsEnterprise::getSafetyManagerTel, query.getSafetyManagerTel());
				lqw.orderByDesc(EhsEnterprise::getCreateTime);
					lqw.eq(query.getDeptId() != null, EhsEnterprise::getDeptId, query.getDeptId());
					lqw.eq(query.getCreateUserId() != null, EhsEnterprise::getCreateUserId, query.getCreateUserId());
					lqw.eq(query.getUpdateUserId() != null, EhsEnterprise::getUpdateUserId, query.getUpdateUserId());
		return lqw;
	}

}
