package com.gci.boot.modules.gis.service.impl;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.common.util.DateUtils;
import com.gci.boot.enums.EventCharacteristicsType;
import com.gci.boot.enums.EventType;
import com.gci.boot.modules.gis.entity.TbGisActiveSafetyAlarm;
import com.gci.boot.modules.gis.mapper.TbGisActiveSafetyAlarmMapper;
import com.gci.boot.modules.gis.service.ITbGisActiveSafetyAlarmService;
import com.gci.boot.modules.statis.vo.AlarmTrendVO;
import com.gci.boot.modules.system.service.ISysDictService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 主动安全故障记录表 服务实现类
 * </p>
 *
 * @author 2464
 * @since 2023-09-01
 */
@Service
public class TbGisActiveSafetyAlarmServiceImpl extends ServiceImpl<TbGisActiveSafetyAlarmMapper, TbGisActiveSafetyAlarm> implements ITbGisActiveSafetyAlarmService {

    @Autowired
    private TbGisActiveSafetyAlarmMapper tbGisActiveSafetyAlarmMapper;
    @Autowired
    private ISysDictService sysDictService;

    @Override
    public IPage<TbGisActiveSafetyAlarm> pageBuild(Page<TbGisActiveSafetyAlarm> page, LambdaQueryWrapper<TbGisActiveSafetyAlarm> lambdaQueryWrapper) {
        Page<TbGisActiveSafetyAlarm> alarmPage = tbGisActiveSafetyAlarmMapper.pageBuild(page, lambdaQueryWrapper);
        List<TbGisActiveSafetyAlarm> records = alarmPage.getRecords();
        if (ObjectUtil.isNotEmpty(records)) {
            buildEventCharacteristics(records);
        }
        return alarmPage;
    }

    @Override
    public List<TbGisActiveSafetyAlarm> findListByTime(Date startTime, Date endTime) {
        LambdaQueryWrapper<TbGisActiveSafetyAlarm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(TbGisActiveSafetyAlarm::getAlertStartTime,startTime, endTime);
        List<TbGisActiveSafetyAlarm> list = tbGisActiveSafetyAlarmMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<AlarmTrendVO> findTrendListByTime(Date startTime, Date endTime) {
        return tbGisActiveSafetyAlarmMapper.findTrendListByTime(DateUtils.formatTime(startTime),DateUtils.formatTime(endTime));
    }

    /**
     * 补充下描述
     *
     * @param records
     */
    private void buildEventCharacteristics(List<TbGisActiveSafetyAlarm> records) {
        EventCharacteristicsType[] values = EventCharacteristicsType.values();
        StrBuilder strBuilder = new StrBuilder();
        for (TbGisActiveSafetyAlarm record : records) {
            strBuilder.clear();
            for (EventCharacteristicsType value : values) {
                List<EventType> eventTypeList = value.getEventTypeList();
                for (EventType eventType : eventTypeList) {
                    if (eventType.getDescription().equals(record.getEventName())) {
                        if (value.getDictCode() != null) {
                            if (StrUtil.isNotBlank(strBuilder)) {
                                strBuilder.append("/");
                            }
                            String format = StrUtil.format(value.getDesc(), sysDictService.queryDictTextByKey(value.getDictCode(), String.valueOf(ReflectUtil.getFieldValue(record, value.getFieldName()))));
                            if (StrUtil.isNotBlank(format)) {
                                strBuilder.append(format);
                            }
                        } else {
                            if (StrUtil.isNotBlank(strBuilder)) {
                                strBuilder.append("/");
                            }
                            String format = StrUtil.format(value.getDesc(), String.valueOf(ReflectUtil.getFieldValue(record, value.getFieldName())));
                            if (StrUtil.isNotBlank(format)) {
                                strBuilder.append(format);
                            }
                        }
                    }
                }
            }
            if (StrUtil.isNotBlank(strBuilder)) {
                record.setEventCharacteristics(strBuilder.toString());
            }
        }
    }
}
