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

import com.sundot.airport.common.utils.SecurityUtils;
import com.sundot.airport.item.domain.HkPendingConfiscate;
import com.sundot.airport.item.mapper.HkPendingConfiscateMapper;
import com.sundot.airport.item.service.IHkPendingConfiscateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class HkPendingConfiscateServiceImpl implements IHkPendingConfiscateService {

    @Autowired
    private HkPendingConfiscateMapper mapper;

    @Override
    public List<HkPendingConfiscate> selectHkPendingConfiscateList(HkPendingConfiscate query) {
        return mapper.selectHkPendingConfiscateList(query);
    }

    @Override
    public HkPendingConfiscate selectHkPendingConfiscateById(Long id) {
        return mapper.selectHkPendingConfiscateById(id);
    }

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

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

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

    @Override
    public String importData(List<HkPendingConfiscate> 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++) {
            HkPendingConfiscate 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 {
                HkPendingConfiscate existing = mapper.selectHkPendingConfiscateByDateAndSlot(data.getRecordDate(), data.getTimeSlot());
                if (existing != null) {
                    data.setId(existing.getId());
                    data.setUpdateBy(operator);
                    data.setUpdateTime(now);
                    mapper.updateHkPendingConfiscate(data);
                    updateNum++;
                } else {
                    data.setCreateBy(operator);
                    data.setCreateTime(now);
                    mapper.insertHkPendingConfiscate(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 + " 条";
    }
}
