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.common.utils.StringUtils;
import com.ozs.entity.MsgAlarm;
import com.ozs.entity.MsgWebPush;
import com.ozs.entity.vo.MsgAlarmResp;
import com.ozs.entity.vo.MsgAlarmVo;
import com.ozs.mapper.MsgWebPushMapper;
import com.ozs.service.BaseCameraManagementService;
import com.ozs.service.BaseRailwayManagementService;
import com.ozs.service.MsgWebPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author : sunhh
 * @create 2023/3/15 14:34
 */
@Service
public class MsgWebPushServiceImpl extends ServiceImpl<MsgWebPushMapper, MsgWebPush> implements MsgWebPushService {
    @Autowired
    private MsgWebPushMapper msgWebPushMapper;
    @Autowired
    private BaseCameraManagementService baseCameraManagementService;
    @Autowired
    private BaseRailwayManagementService baseRailwayManagementService;

    @Override
    public int read(MsgWebPush msgWebPush) {
        // 状态：1未读；2已读
        msgWebPush.setStatus(2);
        return msgWebPushMapper.updateByPushId(msgWebPush);
    }

    @Override
    public Map webAlarm(String userId) {
        // List<String> userIds = baseCameraManagementService.getUserIds(userId);
        Map<String, Object> webAlarmMap = new HashMap<>();
        List<MsgAlarmResp> msgAlarmRespList = new ArrayList<>();
        Integer webAlarmNum = msgWebPushMapper.webAlarmNum(userId);
        List<MsgAlarm> webAlarm = msgWebPushMapper.webAlarm(userId);
        if (!StringUtils.isEmpty(webAlarm) && webAlarm.size() > 0) {
            for (MsgAlarm msgAlarm : webAlarm) {
//                LambdaQueryWrapper<BaseCameraManagement> lw = new LambdaQueryWrapper<BaseCameraManagement>();
//                if (!ObjectUtils.isEmpty(msgAlarm.getCameraCode())) {
//                    lw.eq(BaseCameraManagement::getCameraCode, msgAlarm.getCameraCode());
//                }
//                BaseCameraManagement baseCameraManagement = baseCameraManagementService.getOne(lw);
//                LambdaQueryWrapper<BaseRailwayManagement> wrapper = new LambdaQueryWrapper<BaseRailwayManagement>();
//                if (!ObjectUtils.isEmpty(baseCameraManagement.getRailwayCode())) {
//                    wrapper.eq(BaseRailwayManagement::getRailwayCode, baseCameraManagement.getRailwayCode());
//                }
//                BaseRailwayManagement baseRailwayManagement = baseRailwayManagementService.getOne(wrapper);
                MsgAlarmResp msgAlarmResp = new MsgAlarmResp();
//                BeanUtils.copyProperties(msgAlarm, msgAlarmResp);
//                msgAlarmResp.setRailwayName(baseRailwayManagement.getRailwayName());
//                msgAlarmResp.setDeptId(baseCameraManagement.getDeptId());
//                String begin = AppendUtils.stringAppend(baseCameraManagement.getBeginMile());
//                String end = AppendUtils.stringAppend(baseCameraManagement.getEndMile());
//                String endMile = begin + "-" + end;
//                msgAlarmResp.setBeginMile(endMile);
//                String mile = AppendUtils.stringAppend(baseCameraManagement.getInstallMile());
//                msgAlarmResp.setAlarmMile(mile);
//                msgAlarmResp.setInstallLongitude(baseCameraManagement.getInstallLongitude() + "," + baseCameraManagement.getInstallLatitude());
//                msgAlarmResp.setPushId(msgAlarm.getMsgId());
                msgAlarmRespList.add(msgAlarmResp);
            }
        }
        webAlarmMap.put("webAlarmNum", webAlarmNum);
        webAlarmMap.put("webAlarm", msgAlarmRespList);
        return webAlarmMap;
    }

    @Override
    public IPage<MsgAlarm> messageMore(MsgAlarmVo msgAlarmVo) {
        Page<MsgAlarm> page = PageHelper.startPage(msgAlarmVo.getPageNum().intValue(), msgAlarmVo.getPageSize().intValue())
                .doSelectPage(() -> msgWebPushMapper.messageMore(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());
        // Page<MsgAlarm> page = PageUtils.getPages(msgAlarmVo.getPageNum().intValue(), msgAlarmVo.getPageSize().intValue(), webAlarm);
        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 Map<String, Integer> messageMoreNum(String userId) {
        Map<String, Integer> map = msgWebPushMapper.messageMoreNum(userId);
        return map;
    }

    @Override
    public int readAll(String userId) {
        return msgWebPushMapper.readAll(userId);
    }
}
