package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.AlarmVideo;
import com.usky.fire.domain.BaseUserCompany;
import com.usky.fire.mapper.AlarmVideoMapper;
import com.usky.fire.service.AlarmVideoService;
import com.usky.fire.service.BaseUserCompanyService;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.DataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 视频监控隐患告警 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-08-18
 */
@Service
public class AlarmVideoServiceImpl extends AbstractCrudService<AlarmVideoMapper, AlarmVideo> implements AlarmVideoService {

    @Autowired
    private BaseUserCompanyService baseUserCompanyService;

    @Override
    public CommonPage<AlarmVideo> alarmVideoList(String companyName, Integer handlingStatus, String startDate, String endDate,
                                                 Integer pageNum, Integer pageSize) {
        IPage<AlarmVideo> page = new Page<>(pageNum, pageSize);
        List<AlarmVideo> list = new ArrayList<>();
        LambdaQueryWrapper<BaseUserCompany> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(BaseUserCompany::getCompanyId)
                .eq(BaseUserCompany::getUserId, SecurityUtils.getUserId());
        List<BaseUserCompany> comoanyList = baseUserCompanyService.list(QueryWrapper);
        if (CollectionUtils.isNotEmpty(comoanyList)) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < comoanyList.size(); i++) {
                companyIdList.add(comoanyList.get(i).getCompanyId());
            }
            LambdaQueryWrapper<AlarmVideo> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.select(AlarmVideo::getId, AlarmVideo::getDeviceCode,
                    AlarmVideo::getDeviceName, AlarmVideo::getAlarmType, AlarmVideo::getAlarmTime,
                    AlarmVideo::getCompanyId, AlarmVideo::getCompanyName, AlarmVideo::getHandlingStatus,
                    AlarmVideo::getHandler, AlarmVideo::getHandlingTime, AlarmVideo::getHandlingContent,
                    AlarmVideo::getFalseAlarm, AlarmVideo::getSitePhoto, AlarmVideo::getPortNum, AlarmVideo::getSystemType)
                    .in(AlarmVideo::getCompanyId, companyIdList)
                    .like(StringUtils.isNotBlank(companyName), AlarmVideo::getCompanyName, companyName)
                    .eq(handlingStatus != null, AlarmVideo::getHandlingStatus, handlingStatus)
                    .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), AlarmVideo::getAlarmTime, startDate, endDate);
            queryWrapperOne.orderByDesc(AlarmVideo::getId);
            page = this.page(page, queryWrapperOne);
            list = page.getRecords();
            if (CollectionUtils.isNotEmpty(list)) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setAlarmTypeName(OnlineMethod.alarmName(list.get(i).getAlarmType()));
                    list.get(i).setReportContent(list.get(i).getDeviceName() + ",端口号：" + list.get(i).getPortNum());
                }
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }


    @Override
    public Integer videoCount(List<String> companyIdList, Integer handlingStatus, String companyName, String startDate, String endDate) {
        LambdaQueryWrapper<AlarmVideo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(CollectionUtils.isNotEmpty(companyIdList), AlarmVideo::getCompanyId, companyIdList)
                .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), AlarmVideo::getAlarmTime, startDate, endDate)
                .like(StringUtils.isNotBlank(companyName), AlarmVideo::getCompanyName, companyName)
                .eq(handlingStatus != null, AlarmVideo::getHandlingStatus, handlingStatus);
        int total = this.count(queryWrapper);
        return total;
    }


    @Override
    public void updateAlarmVideo(AlarmVideo alarmVideo) {
        if (alarmVideo.getId() == 0) {
            LambdaQueryWrapper<AlarmVideo> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(AlarmVideo::getAlarmType, alarmVideo.getAlarmType())
                    .eq(AlarmVideo::getDeviceCode, alarmVideo.getDeviceCode());
            List<AlarmVideo> list = this.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setHandler(SecurityUtils.getUsername());
                    list.get(i).setHandlingTime(LocalDateTime.now());
                    list.get(i).setHandlingStatus(1);
                    list.get(i).setHandlingContent(alarmVideo.getHandlingContent());
                    list.get(i).setHandlerPhone(SecurityUtils.getLoginUser().getSysPerson().getLinkPhone());
                    list.get(i).setFalseAlarm(alarmVideo.getFalseAlarm());
                    this.updateById(list.get(i));
                }
            }
        } else {
            alarmVideo.setHandler(SecurityUtils.getUsername());
            alarmVideo.setHandlingTime(LocalDateTime.now());
            alarmVideo.setHandlingStatus(1);
            alarmVideo.setHandlerPhone(SecurityUtils.getLoginUser().getSysPerson().getLinkPhone());
            this.updateById(alarmVideo);
        }
    }

    @Override
    public List<AlarmVideo> alarmVideoListOne(String companyName, Integer handlingStatus, String startDate, String endDate) {
        List<AlarmVideo> list = new ArrayList<>();
        LambdaQueryWrapper<BaseUserCompany> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(BaseUserCompany::getCompanyId)
                .eq(BaseUserCompany::getUserId, SecurityUtils.getUserId());
        List<BaseUserCompany> comoanyList = baseUserCompanyService.list(QueryWrapper);
        if (CollectionUtils.isNotEmpty(comoanyList)) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < comoanyList.size(); i++) {
                companyIdList.add(comoanyList.get(i).getCompanyId());
            }
            LambdaQueryWrapper<AlarmVideo> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.select(AlarmVideo::getId, AlarmVideo::getDeviceCode,
                    AlarmVideo::getDeviceName, AlarmVideo::getAlarmType, AlarmVideo::getAlarmTime,
                    AlarmVideo::getCompanyId, AlarmVideo::getCompanyName, AlarmVideo::getHandlingStatus,
                    AlarmVideo::getHandler, AlarmVideo::getHandlingTime, AlarmVideo::getHandlingContent,
                    AlarmVideo::getFalseAlarm, AlarmVideo::getSitePhoto, AlarmVideo::getPortNum)
                    .in(AlarmVideo::getCompanyId, companyIdList)
                    .like(StringUtils.isNotBlank(companyName), AlarmVideo::getCompanyName, companyName)
                    .eq(handlingStatus != null, AlarmVideo::getHandlingStatus, handlingStatus)
                    .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), AlarmVideo::getAlarmTime, startDate, endDate);
            queryWrapperOne.orderByDesc(AlarmVideo::getId);
            list = this.list(queryWrapperOne);
            if (CollectionUtils.isNotEmpty(list)) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setAlarmTypeName(OnlineMethod.alarmName(list.get(i).getAlarmType()));
                    list.get(i).setReportContent(list.get(i).getDeviceName() + ",端口号：" + list.get(i).getPortNum());
                }
            }
        }
        return list;
    }

    @Override
    public Integer videoCount(String companyId, String startTime, String endTime) {
        LambdaQueryWrapper<AlarmVideo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AlarmVideo::getCompanyId, companyId)
                .between(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime), AlarmVideo::getAlarmTime, startTime, endTime);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public List<DataVo> Alarmlist() {
        List<DataVo> list = new ArrayList<>();
        String companyId = baseUserCompanyService.companyId();
        LambdaQueryWrapper<AlarmVideo> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.select(AlarmVideo::getAlarmTime, AlarmVideo::getDeviceCode,
                AlarmVideo::getAlarmType, AlarmVideo::getAddress,
                AlarmVideo::getHandlingStatus, AlarmVideo::getDeviceName,
                AlarmVideo::getAttributeValue4, AlarmVideo::getPortNum)
                .eq(AlarmVideo::getCompanyId, companyId)
                .orderByDesc(AlarmVideo::getId)
                .last(" limit 1");
        List<AlarmVideo> list1 = this.list(queryWrapperOne);
        for (int i = 0; i < list1.size(); i++) {
            DataVo dataVo = new DataVo();
            dataVo.setType1(list1.get(i).getAlarmTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            dataVo.setType2(list1.get(i).getDeviceCode());
            dataVo.setType3(list1.get(i).getAlarmType());
            dataVo.setType4(list1.get(i).getAddress());
            dataVo.setType5(list1.get(i).getPortNum());
            dataVo.setType6(list1.get(i).getDeviceName() + ",端口号：" + list1.get(i).getPortNum());
            dataVo.setType7(list1.get(i).getHandlingStatus() == 1 ? "已处理" : "未处理");
            list.add(dataVo);
        }
        return list;
    }
}
