package com.goldenwater.gx.service.impl;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goldenwater.common.exception.ServiceException;
import com.goldenwater.common.utils.StringUtils;
import com.goldenwater.gx.domain.GxExchangeStation;
import com.goldenwater.gx.mapper.GxExchangeStationMapper;
import com.goldenwater.gx.service.IGxExchangeStationService;

/**
 * 数据交换站点Service业务层处理
 */
@Service
public class GxExchangeStationServiceImpl implements IGxExchangeStationService
{
    private static final Logger log = LoggerFactory.getLogger(GxExchangeStationServiceImpl.class);

    @Autowired
    private GxExchangeStationMapper gxExchangeStationMapper;

    @Override
    public GxExchangeStation selectGxExchangeStationById(Long id)
    {
        return gxExchangeStationMapper.selectGxExchangeStationById(id);
    }

    @Override
    public List<GxExchangeStation> selectGxExchangeStationList(GxExchangeStation station)
    {
        return gxExchangeStationMapper.selectGxExchangeStationList(station);
    }

    @Override
    public List<GxExchangeStation> selectStationByBizCode(String bizCode)
    {
        return gxExchangeStationMapper.selectStationByBizCode(bizCode);
    }

    @Override
    public int insertGxExchangeStation(GxExchangeStation station)
    {
        return gxExchangeStationMapper.insertGxExchangeStation(station);
    }

    @Override
    public int updateGxExchangeStation(GxExchangeStation station)
    {
        return gxExchangeStationMapper.updateGxExchangeStation(station);
    }

    @Override
    public int deleteGxExchangeStationByIds(Long[] ids)
    {
        return gxExchangeStationMapper.deleteGxExchangeStationByIds(ids);
    }

    @Override
    public int deleteGxExchangeStationById(Long id)
    {
        return gxExchangeStationMapper.deleteGxExchangeStationById(id);
    }

    /**
     * 导入站点数据
     * 使用STCD + BIZ_CODE + STNM作为联合主键判断测站是否存在
     * 存在则跳过导入，不存在则继续导入
     *
     * @param stationList 站点数据列表
     * @param updateSupport 是否更新已存在数据
     * @param operName 操作人
     * @return 结果信息
     */
    @Override
    public String importStation(List<GxExchangeStation> stationList, Boolean updateSupport, String operName)
    {
        if (StringUtils.isNull(stationList) || stationList.size() == 0)
        {
            throw new ServiceException("导入站点数据不能为空！");
        }
        int successNum = 0;
        int skipNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder skipMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GxExchangeStation station : stationList)
        {
            try
            {
                // 校验必填字段：STCD、BIZ_CODE、STNM
                if (StringUtils.isEmpty(station.getStcd()))
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、站码为空，跳过导入");
                    continue;
                }
                if (StringUtils.isEmpty(station.getBizCode()))
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、站码 " + station.getStcd() + " 的业务域为空，跳过导入");
                    continue;
                }
                if (StringUtils.isEmpty(station.getStnm()))
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、站码 " + station.getStcd() + " 的测站名称为空，跳过导入");
                    continue;
                }
                // 根据联合主键（STCD + BIZ_CODE + STNM）校验是否已存在
                GxExchangeStation existStation = gxExchangeStationMapper.selectStationByCompositeKey(
                    station.getStcd(), station.getBizCode(), station.getStnm());
                if (StringUtils.isNull(existStation))
                {
                    station.setCreateBy(operName);
                    gxExchangeStationMapper.insertGxExchangeStation(station);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、站码 " + station.getStcd() + " 导入成功");
                }
                else
                {
                    // 联合主键存在则跳过导入
                    skipNum++;
                    skipMsg.append("<br/>" + skipNum + "、站码 " + station.getStcd() + "（业务域：" + station.getBizCode() + "，测站名称：" + station.getStnm() + "）已存在，跳过导入");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、站码 " + station.getStcd() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        StringBuilder resultMsg = new StringBuilder();
        if (successNum > 0)
        {
            resultMsg.insert(0, "恭喜您，数据导入成功！共 " + successNum + " 条，数据如下：");
            resultMsg.append(successMsg);
        }
        if (skipNum > 0)
        {
            if (resultMsg.length() > 0)
            {
                resultMsg.append("<br/><br/>");
            }
            resultMsg.append("以下 " + skipNum + " 条数据因联合主键（站码+业务域+测站名称）已存在，跳过导入：");
            resultMsg.append(skipMsg);
        }
        if (failureNum > 0)
        {
            if (resultMsg.length() > 0)
            {
                resultMsg.append("<br/><br/>");
            }
            resultMsg.append("很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            resultMsg.append(failureMsg);
            throw new ServiceException(resultMsg.toString());
        }
        return resultMsg.toString();
    }
}
