package com.sundot.airport.item.service.impl;

import com.sundot.airport.common.utils.SecurityUtils;
import com.sundot.airport.item.domain.HkPersuadePowerBank;
import com.sundot.airport.item.mapper.HkPersuadePowerBankMapper;
import com.sundot.airport.item.service.IHkPersuadePowerBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class HkPersuadePowerBankServiceImpl implements IHkPersuadePowerBankService {

    @Autowired
    private HkPersuadePowerBankMapper mapper;

    @Override
    public List<HkPersuadePowerBank> selectHkPersuadePowerBankList(HkPersuadePowerBank query) {
        return mapper.selectHkPersuadePowerBankList(query);
    }

    @Override
    public HkPersuadePowerBank selectHkPersuadePowerBankById(Long id) {
        return mapper.selectHkPersuadePowerBankById(id);
    }

    @Override
    public int insertHkPersuadePowerBank(HkPersuadePowerBank data) {
        data.setCreateBy(SecurityUtils.getUsername());
        data.setCreateTime(new Date());
        return mapper.insertHkPersuadePowerBank(data);
    }

    @Override
    public int updateHkPersuadePowerBank(HkPersuadePowerBank data) {
        data.setUpdateBy(SecurityUtils.getUsername());
        data.setUpdateTime(new Date());
        return mapper.updateHkPersuadePowerBank(data);
    }

    @Override
    public int deleteHkPersuadePowerBankByIds(Long[] ids) {
        return mapper.deleteHkPersuadePowerBankByIds(ids);
    }

    @Override
    public String importData(List<HkPersuadePowerBank> list) {
        int successNum = 0;
        int updateNum = 0;
        StringBuilder failureMsg = new StringBuilder();
        String operator = SecurityUtils.getUsername();
        Date now = new Date();

        for (int i = 0; i < list.size(); i++) {
            HkPersuadePowerBank data = list.get(i);
            int rowNum = i + 2;
            if (data == null) continue;
            if (data.getRecordDate() == null) {
                failureMsg.append("<br/>第 ").append(rowNum).append(" 行日期不能为空");
                continue;
            }
            if (data.getTimeSlot() == null || data.getTimeSlot().isEmpty()) {
                failureMsg.append("<br/>第 ").append(rowNum).append(" 行时间段不能为空");
                continue;
            }
            try {
                HkPersuadePowerBank existing = mapper.selectHkPersuadePowerBankByDateAndSlot(data.getRecordDate(), data.getTimeSlot());
                if (existing != null) {
                    data.setId(existing.getId());
                    data.setUpdateBy(operator);
                    data.setUpdateTime(now);
                    mapper.updateHkPersuadePowerBank(data);
                    updateNum++;
                } else {
                    data.setCreateBy(operator);
                    data.setCreateTime(now);
                    mapper.insertHkPersuadePowerBank(data);
                    successNum++;
                }
            } catch (Exception e) {
                failureMsg.append("<br/>第 ").append(rowNum).append(" 行导入失败：").append(e.getMessage());
            }
        }
        if (failureMsg.length() > 0) {
            return "导入结果：新增 " + successNum + " 条，更新 " + updateNum + " 条，失败信息：" + failureMsg;
        }
        return "导入成功：新增 " + successNum + " 条，更新 " + updateNum + " 条";
    }
}
