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

import com.sundot.airport.common.utils.SecurityUtils;
import com.sundot.airport.item.domain.HkOperationData;
import com.sundot.airport.item.mapper.HkOperationDataMapper;
import com.sundot.airport.item.service.IHkOperationDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class HkOperationDataServiceImpl implements IHkOperationDataService {

    @Autowired
    private HkOperationDataMapper mapper;

    @Override
    public List<HkOperationData> selectHkOperationDataList(HkOperationData query) {
        return mapper.selectHkOperationDataList(query);
    }

    @Override
    public HkOperationData selectHkOperationDataById(Long id) {
        return mapper.selectHkOperationDataById(id);
    }

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

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

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

    @Override
    public String importData(List<HkOperationData> 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++) {
            HkOperationData data = list.get(i);
            int rowNum = i + 2;
            if (data == null) continue;
            if (data.getRecordDate() == null) {
                failureMsg.append("<br/>第 ").append(rowNum).append(" 行日期不能为空");
                continue;
            }
            try {
                HkOperationData existing = mapper.selectHkOperationDataByDate(data.getRecordDate());
                if (existing != null) {
                    data.setId(existing.getId());
                    data.setUpdateBy(operator);
                    data.setUpdateTime(now);
                    mapper.updateHkOperationData(data);
                    updateNum++;
                } else {
                    data.setCreateBy(operator);
                    data.setCreateTime(now);
                    mapper.insertHkOperationData(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 + " 条";
    }
}
