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);
    }

    /**
     * 导入站点数据
     *
     * @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 failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GxExchangeStation station : stationList)
        {
            try
            {
                // 校验必填字段
                if (StringUtils.isEmpty(station.getStcd()))
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、站码为空，跳过导入");
                    continue;
                }
                // 根据站码校验是否已存在
                GxExchangeStation existStation = gxExchangeStationMapper.selectStationByStcd(station.getStcd());
                if (StringUtils.isNull(existStation))
                {
                    station.setCreateBy(operName);
                    gxExchangeStationMapper.insertGxExchangeStation(station);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、站码 " + station.getStcd() + " 导入成功");
                }
                else if (updateSupport)
                {
                    station.setId(existStation.getId());
                    station.setUpdateBy(operName);
                    gxExchangeStationMapper.updateGxExchangeStation(station);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、站码 " + station.getStcd() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、站码 " + station.getStcd() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、站码 " + station.getStcd() + " 导入失败：";
                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();
    }
}
