package com.goldenwater.slgc.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goldenwater.common.utils.StringUtils;
import com.goldenwater.slgc.domain.AttResBase;
import com.goldenwater.slgc.mapper.AttResBaseMapper;
import com.goldenwater.slgc.service.ReservoirService;

/** 水库信息 业务实现 */
@Service
public class ReservoirServiceImpl implements ReservoirService {
    @Autowired
    private AttResBaseMapper attResBaseMapper;

    @Override
    public List<AttResBase> selectAttResBaseList(AttResBase attResBase) {
        return attResBaseMapper.selectAttResBaseList(attResBase);
    }

    @Override
    public AttResBase selectAttResBaseById(String resCode) {
        return attResBaseMapper.selectAttResBaseById(resCode);
    }

    @Override
    public int deleteAttResBaseByIds(String[] resCodes) {
        return attResBaseMapper.deleteAttResBaseByIds(resCodes);
    }

    @Override
    public int insertAttResBase(AttResBase attResBase) {
        if (attResBase.getCollDate() == null) {
            attResBase.setCollDate(new Date());
        }
        return attResBaseMapper.insertAttResBase(attResBase);
    }

    @Override
    public int updateAttResBase(AttResBase attResBase) {
        attResBase.setUpdDate(new Date());
        return attResBaseMapper.updateAttResBase(attResBase);
    }

    @Override
    public List<Map<String, Object>> countType() {
        return attResBaseMapper.countType();
    }

    @Override
    public String importReservoir(List<AttResBase> list) {
        if (list == null || list.isEmpty()) {
            return "导入数据为空";
        }
        int successNum = 0;
        int updateNum = 0;
        int skipNum = 0;
        int failureNum = 0;
        StringBuilder errorMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (AttResBase attResBase : list) {
            try {
                if (attResBase.getCollDate() == null) {
                    attResBase.setCollDate(new Date());
                }
                if (StringUtils.isEmpty(attResBase.getResCode())) {
                    skipNum++;
                    continue;
                }
                AttResBase existing = attResBaseMapper.selectAttResBaseById(attResBase.getResCode());
                if (existing != null) {
                    attResBase.setUpdDate(new Date());
                    attResBaseMapper.updateAttResBase(attResBase);
                    updateNum++;
                } else {
                    attResBaseMapper.insertAttResBase(attResBase);
                    successNum++;
                }
            } catch (Exception e) {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、" + attResBase.getResName() + " 导入失败：" + e.getMessage());
            }
        }
        errorMsg.append("导入成功：").append(successNum).append("条，更新：").append(updateNum).append("条");
        if (skipNum > 0) {
            errorMsg.append("，跳过（编码为空）：").append(skipNum).append("条");
        }
        if (failureNum > 0) {
            errorMsg.append("，失败：").append(failureNum).append("条");
            errorMsg.append("<br/>失败明细：").append(failureMsg);
        }
        return errorMsg.toString();
    }
}
