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

import com.sundot.airport.common.utils.SecurityUtils;
import com.sundot.airport.item.domain.HkSeizureStat;
import com.sundot.airport.item.mapper.HkSeizureStatMapper;
import com.sundot.airport.item.service.IHkSeizureStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class HkSeizureStatServiceImpl implements IHkSeizureStatService {

    @Autowired
    private HkSeizureStatMapper mapper;

    @Override
    public List<HkSeizureStat> selectHkSeizureStatList(HkSeizureStat query) {
        return mapper.selectHkSeizureStatList(query);
    }

    @Override
    public HkSeizureStat selectHkSeizureStatById(Long id) {
        return mapper.selectHkSeizureStatById(id);
    }

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

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

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

    @Override
    public String importData(List<HkSeizureStat> 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++) {
            HkSeizureStat 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.getShift() == null || data.getShift().isEmpty()) {
                failureMsg.append("<br/>第 ").append(rowNum).append(" 行班次不能为空");
                continue;
            }
            try {
                HkSeizureStat existing = mapper.selectHkSeizureStatByDateAndShift(data.getRecordDate(), data.getShift());
                if (existing != null) {
                    data.setId(existing.getId());
                    data.setUpdateBy(operator);
                    data.setUpdateTime(now);
                    mapper.updateHkSeizureStat(data);
                    updateNum++;
                } else {
                    data.setCreateBy(operator);
                    data.setCreateTime(now);
                    mapper.insertHkSeizureStat(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 + " 条";
    }
}
