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.AttWagaBase;
import com.goldenwater.slgc.mapper.AttWagaBaseMapper;
import com.goldenwater.slgc.service.WagaService;

/** 泵站信息 业务实现 */
@Service
public class WagaServiceImpl implements WagaService {
    @Autowired
    private AttWagaBaseMapper attWagaBaseMapper;

    @Override
    public List<AttWagaBase> selectAttWagaBaseList(AttWagaBase attWagaBase) {
        return attWagaBaseMapper.selectAttWagaBaseList(attWagaBase);
    }

    @Override
    public AttWagaBase selectAttWagaBaseById(String wagaCode) {
        return attWagaBaseMapper.selectAttWagaBaseById(wagaCode);
    }

    @Override
    public int deleteAttWagaBaseByIds(String[] wagaCodes) {
        return attWagaBaseMapper.deleteAttWagaBaseByIds(wagaCodes);
    }

    @Override
    public int insertAttWagaBase(AttWagaBase attWagaBase) {
        if (attWagaBase.getCollDate() == null) {
            attWagaBase.setCollDate(new Date());
        }
        return attWagaBaseMapper.insertAttWagaBase(attWagaBase);
    }

    @Override
    public int updateAttWagaBase(AttWagaBase attWagaBase) {
        attWagaBase.setUpdDate(new Date());
        return attWagaBaseMapper.updateAttWagaBase(attWagaBase);
    }

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

    @Override
    public String importWaga(List<AttWagaBase> 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 (AttWagaBase attWagaBase : list) {
            try {
                if (attWagaBase.getCollDate() == null) {
                    attWagaBase.setCollDate(new Date());
                }
                if (StringUtils.isEmpty(attWagaBase.getWagaCode())) {
                    skipNum++;
                    continue;
                }
                AttWagaBase existing = attWagaBaseMapper.selectAttWagaBaseById(attWagaBase.getWagaCode());
                if (existing != null) {
                    attWagaBase.setUpdDate(new Date());
                    attWagaBaseMapper.updateAttWagaBase(attWagaBase);
                    updateNum++;
                } else {
                    attWagaBaseMapper.insertAttWagaBase(attWagaBase);
                    successNum++;
                }
            } catch (Exception e) {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、" + attWagaBase.getWagaName() + " 导入失败：" + 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();
    }
}
