package com.ozs.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ozs.entity.MsgAlarm;
import com.ozs.entity.MsgAppPush;
import com.ozs.entity.vo.MsgAlarmVo;
import com.ozs.mapper.MsgAppPushMapper;
import com.ozs.service.BaseCameraManagementService;
import com.ozs.service.BaseRailwayManagementService;
import com.ozs.service.MsgAppPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wyy
 * @subject
 * @creat 2023/3/6
 */
@Service
public class MsgAppPushServiceImpl extends ServiceImpl<MsgAppPushMapper, MsgAppPush> implements MsgAppPushService {
    @Autowired
    MsgAppPushMapper msgAppPushMapper;
    @Autowired
    private BaseCameraManagementService baseCameraManagementService;
    @Autowired
    private BaseRailwayManagementService baseRailwayManagementService;

    @Override
    public IPage<MsgAlarm> alarmPushMsg(MsgAlarmVo msgAlarmVo) {
        Page<MsgAlarm> page = PageHelper.startPage(msgAlarmVo.getPageNum().intValue(), msgAlarmVo.getPageSize().intValue())
                .doSelectPage(() -> msgAppPushMapper.messageMoreApp(msgAlarmVo));
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<MsgAlarm> pageR =
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(msgAlarmVo.getPageNum().intValue(), msgAlarmVo.getPageSize().intValue());
        pageR.setSize(page.size());
        pageR.setTotal(page.getTotal());
        if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getResult())) {
            List<MsgAlarm> dto1 = page.getResult().stream().map(o -> {
//                String[] split = o.getImageUrl().split(";");
//                ArrayList<String> objects = new ArrayList<>(Arrays.asList(split));
//                o.setImageUrls(objects);
//                LambdaQueryWrapper<BaseCameraManagement> cameraManagementLambdaQueryWrapper = new LambdaQueryWrapper<BaseCameraManagement>();
//                if (!ObjectUtils.isEmpty(o.getCameraCode())) {
//                    cameraManagementLambdaQueryWrapper.eq(BaseCameraManagement::getCameraCode, o.getCameraCode());
//                }
//                BaseCameraManagement cameraManagement = baseCameraManagementService.getOne(cameraManagementLambdaQueryWrapper);
//                LambdaQueryWrapper<BaseRailwayManagement> queryWrapper = new LambdaQueryWrapper<BaseRailwayManagement>();
//                if (!ObjectUtils.isEmpty(cameraManagement.getRailwayCode())) {
//                    queryWrapper.eq(BaseRailwayManagement::getRailwayCode, cameraManagement.getRailwayCode());
//                }
//                BaseRailwayManagement baseRailwayManagement = baseRailwayManagementService.getOne(queryWrapper);
//                o.setRailwayName(baseRailwayManagement.getRailwayName());
//                String mils = AppendUtils.stringAppend(cameraManagement.getInstallMile());
//                o.setAlarmMiles(mils);
//                String end = AppendUtils.stringAppend(cameraManagement.getEndMile());
//                o.setEndMile(end);
//                String begin = AppendUtils.stringAppend(cameraManagement.getBeginMile());
//                o.setBeginMile(begin);
//                // o.setAlarmCount((int) msgAlarmFrequencyService.count()+1);
                return o;
            }).collect(Collectors.toList());
            pageR.setRecords(dto1);
        }
        return pageR;
    }

    @Override
    public int update(MsgAppPush msgAppPush) {
        int num = msgAppPushMapper.update(msgAppPush);
        return num;
    }
}
