package com.goldenwater.slgc.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goldenwater.common.utils.StringUtils;
import com.goldenwater.slgc.domain.SlgcWaterUnit;
import com.goldenwater.slgc.mapper.SlgcWaterUnitMapper;
import com.goldenwater.slgc.service.WaterUnitService;

/** 水管单位 业务实现 */
@Service
public class WaterUnitServiceImpl implements WaterUnitService {
    @Autowired
    private SlgcWaterUnitMapper slgcWaterUnitMapper;

    @Override
    public List<SlgcWaterUnit> selectWaterUnitList(SlgcWaterUnit slgcWaterUnit) {
        return slgcWaterUnitMapper.selectSlgcWaterUnitList(slgcWaterUnit);
    }

    @Override
    public SlgcWaterUnit selectWaterUnitById(String id) {
        return slgcWaterUnitMapper.selectSlgcWaterUnitById(id);
    }

    @Override
    public int insertWaterUnit(SlgcWaterUnit slgcWaterUnit) {
        if (slgcWaterUnit.getId() == null || slgcWaterUnit.getId().isEmpty()) {
            slgcWaterUnit.setIsDelete("0");
            slgcWaterUnit.setCreateTime(new Date());
            slgcWaterUnit.setUpdateTime(new Date());
            Integer maxId = slgcWaterUnitMapper.findMaxId();
            slgcWaterUnit.setId(String.valueOf(maxId));
        }
        return slgcWaterUnitMapper.insertSlgcWaterUnit(slgcWaterUnit);
    }

    @Override
    public int updateWaterUnit(SlgcWaterUnit slgcWaterUnit) {
        return slgcWaterUnitMapper.updateSlgcWaterUnit(slgcWaterUnit);
    }

    @Override
    public int deleteWaterUnitByIds(String[] ids) {
        return slgcWaterUnitMapper.deleteSlgcWaterUnitByIds(ids);
    }

    @Override
    public String importWaterUnit(List<SlgcWaterUnit> unitList) {
        if (StringUtils.isNull(unitList) || unitList.size() == 0) {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SlgcWaterUnit unit : unitList) {
            try {
                if (StringUtils.isEmpty(unit.getName())) {
                    throw new RuntimeException("单位名称不能为空");
                }
                // 已存在同名单位则更新，否则新增
                SlgcWaterUnit exist = slgcWaterUnitMapper.selectByUnitName(unit.getName());
                unit.setIsDelete("0");
                unit.setUpdateTime(new Date());
                if (exist != null) {
                    unit.setId(exist.getId());
                    slgcWaterUnitMapper.updateSlgcWaterUnit(unit);
                } else {
                    unit.setCreateTime(new Date());
                    Integer maxId = slgcWaterUnitMapper.findMaxId();
                    unit.setId(String.valueOf(maxId));
                    slgcWaterUnitMapper.insertSlgcWaterUnit(unit);
                }
                successNum++;
                successMsg.append("<br/>" + successNum + "、单位 " + unit.getName() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、单位 " + unit.getName() + " 导入失败：" + e.getMessage();
                failureMsg.append(msg);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        }
        return successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：").toString();
    }
}
