package com.xiaoniu.subsystem.business.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoniu.common.core.exception.ServiceException;
import com.xiaoniu.common.core.utils.StringUtils;
import com.xiaoniu.common.core.utils.bean.BeanValidators;
import com.xiaoniu.subsystem.business.domain.Budget;
import com.xiaoniu.subsystem.business.domain.Flight;
import com.xiaoniu.subsystem.business.domain.TransferStation;
import com.xiaoniu.subsystem.business.mapper.TransferStationMapper;
import com.xiaoniu.subsystem.business.service.IFlightService;
import com.xiaoniu.subsystem.business.service.ITransferStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoniu.subsystem.system.service.IUnitService;
import jakarta.validation.Validator;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.xiaoniu.common.security.utils.SecurityUtils.getUserId;

/**
 * <p>
 * 接送站信息统计表 服务实现类
 * </p>
 *
 * @author haoxuyang
 * @since 2024-12-23 05:36:11
 */
@Service
public class TransferStationServiceImpl extends ServiceImpl<TransferStationMapper, TransferStation> implements ITransferStationService {
    protected final Validator validator;
    protected final IUnitService unitService;
    protected final IFlightService flightService;

    public TransferStationServiceImpl(Validator validator, IUnitService unitService, IFlightService flightService) {
        this.validator = validator;
        this.unitService = unitService;
        this.flightService = flightService;
    }

    @Override
    public String importData(List<TransferStation> list, boolean updateSupport) {
        if (CollectionUtils.isEmpty(list)) {
            throw new ServiceException("导入接送站统计信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TransferStation d : list) {
            try {
                //查询班次是否存在
                Flight flight = flightService.getOne(new LambdaQueryWrapper<Flight>()
                        .eq(Flight::getFlightName,d.getFlightName())
                        .eq(Flight::getYear,d.getYear())
                        .eq(Flight::getTrainingTime,d.getTrainingTime())
                );
                if (flight != null){
                    d.setFlightId(flight.getFlightId());
                }
                // 验证是否存在这个数据
                TransferStation u = baseMapper.selectOne(new LambdaQueryWrapper<TransferStation>()
                        .eq(TransferStation::getUserName, d.getUserName())
                        .eq(TransferStation::getFlightName,d.getFlightName())
                        .eq(TransferStation::getTrainingTime,d.getTrainingTime())
                        .eq(TransferStation::getYear, d.getYear()));
                if (StringUtils.isNull(u)) {
                    BeanValidators.validateWithException(validator, d);
                    d.setCreateUser(getUserId());
                    this.save(d);
                    successNum++;
                    successMsg.append("<br/>" + successNum +"导入成功");
                } else if (updateSupport) {
                    BeanValidators.validateWithException(validator, d);
                    d.setUpdateUser(getUserId());
                    d.setStationId(u.getStationId());
                    this.updateById(d);
                    successNum++;
                    successMsg.append("<br/>" + successNum  + "更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + d.getFlightName() + "已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
