package com.jiayue.insu.incloud.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.insu.incloud.constants.CommonStant;
import com.jiayue.insu.incloud.constants.enums.StatusEnum;
import com.jiayue.insu.incloud.entity.Record;
import com.jiayue.insu.incloud.entity.Station;
import com.jiayue.insu.incloud.mapper.StationMapper;
import com.jiayue.insu.incloud.service.RecordService;
import com.jiayue.insu.incloud.service.StationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 场站信息业务实现
 *
 * @author yh
 * @version 1.0
 * @since 2022/5/22 11:29
 */
@Service
@Slf4j
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements StationService {

    @Autowired
    private RecordService recordService;

    @Override
    public Station findByStationCode(String stationCode) {
        LambdaQueryWrapper<Station> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Station::getStationCode, stationCode);
        return this.getOne(lambdaQueryWrapper);

    }

    @Override
    public List<Station> findByInCode(String incode) {
        LambdaQueryWrapper<Station> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Station::getInCode, incode);
        return this.list(lambdaQueryWrapper);
    }

    /**
     * 校验场站编码并获取场站信息
     *
     * @param jobName
     * @return
     */
    @Override
    public Station checkAndFindStation(String jobName ,String recordType) {
        String[] split = jobName.split("-");
        Station station = new Station();
        if(split==null || split.length<=0){
            log.error("========== 一体化定时任务异常:  场站编码不正确  {}==========",jobName);
            return station;
        }
        String stationCode;
        // 如果定时任务名称根据"-"分割后是两半 例如J00156-QNHLPUSH
        if (split.length == 2) {
            stationCode = split[0];
            // 如果定时任务名称根据"-"分割后是三块 例如J00156-A-QNHLPUSH,则场站编码将前两个拼在一起
        } else if (split.length == 3) {
            stationCode = split[0] + "-" + split[1];
        } else {
            log.error("========== 一体化定时任务异常:  场站编码不正确  " + jobName + "==========");
            return station;
        }
        // 如果操作类型不为空，则获取操作吉林
        if(recordType!=null){
            // 获取操作记录
            Record successRecord = recordService.findTimeAndTypeAndStateAndStation(
                    LocalDateTimeUtil.beginOfDay(LocalDateTime.now()),
                    recordType,
                    StatusEnum.SUCCESS.getCode(), station.getStationCode());

            if (successRecord == null) {
                station = this.findByStationCode(stationCode);
            }else{
                log.info("========== 一体化定时任务异常:  定时任务已执行过，不再重复执行  " + jobName + "==========");
            }
        // 如果操作类型为空，则直接获取场站信息
        }else{
            station = this.findByStationCode(stationCode);
        }
        return station;
    }
}
