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.AttDikeBase;
import com.goldenwater.slgc.mapper.AttDikeBaseMapper;
import com.goldenwater.slgc.service.DikeService;

/** 堤防信息 业务实现 */
@Service
public class DikeServiceImpl implements DikeService {
    @Autowired
    private AttDikeBaseMapper attDikeBaseMapper;

    @Override
    public List<AttDikeBase> selectAttDikeBaseList(AttDikeBase attDikeBase) {
        return attDikeBaseMapper.selectAttDikeBaseList(attDikeBase);
    }

    @Override
    public AttDikeBase selectAttDikeBaseById(String dikeCode) {
        return attDikeBaseMapper.selectAttDikeBaseById(dikeCode);
    }

    @Override
    public int deleteAttDikeBaseByIds(String[] dikeCodes) {
        return attDikeBaseMapper.deleteAttDikeBaseByIds(dikeCodes);
    }

    @Override
    public int insertAttDikeBase(AttDikeBase attDikeBase) {
        if (attDikeBase.getCollDate() == null) {
            attDikeBase.setCollDate(new Date());
        }
        return attDikeBaseMapper.insertAttDikeBase(attDikeBase);
    }

    @Override
    public int updateAttDikeBase(AttDikeBase attDikeBase) {
        attDikeBase.setUpdDate(new Date());
        return attDikeBaseMapper.updateAttDikeBase(attDikeBase);
    }

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

    @Override
    public String importDike(List<AttDikeBase> 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 (AttDikeBase attDikeBase : list) {
            try {
                if (attDikeBase.getCollDate() == null) {
                    attDikeBase.setCollDate(new Date());
                }
                if (StringUtils.isEmpty(attDikeBase.getDikeCode())) {
                    skipNum++;
                    continue;
                }
                AttDikeBase existing = attDikeBaseMapper.selectAttDikeBaseById(attDikeBase.getDikeCode());
                if (existing != null) {
                    attDikeBase.setUpdDate(new Date());
                    attDikeBaseMapper.updateAttDikeBase(attDikeBase);
                    updateNum++;
                } else {
                    attDikeBaseMapper.insertAttDikeBase(attDikeBase);
                    successNum++;
                }
            } catch (Exception e) {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、" + attDikeBase.getDikeName() + " 导入失败：" + 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();
    }
}
