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.AlarmSprayWater;
import com.usky.fire.domain.BaseUserCompany;
import com.usky.fire.mapper.AlarmSprayWaterMapper;
import com.usky.fire.service.AlarmSprayWaterService;
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 AlarmSprayWaterServiceImpl extends AbstractCrudService<AlarmSprayWaterMapper, AlarmSprayWater> implements AlarmSprayWaterService {
    @Autowired
    private BaseUserCompanyService baseUserCompanyService;

    @Override
    public CommonPage<AlarmSprayWater> alarmSprayWaterList(String companyName, Integer handlingStatus, String startDate, String endDate,
                                                           Integer pageNum, Integer pageSize) {

        IPage<AlarmSprayWater> page = new Page<>(pageNum, pageSize);
        List<AlarmSprayWater> 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<AlarmSprayWater> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.select(AlarmSprayWater::getId, AlarmSprayWater::getDeviceCode,
                    AlarmSprayWater::getDeviceName, AlarmSprayWater::getAlarmType, AlarmSprayWater::getAlarmTime,
                    AlarmSprayWater::getCompanyId, AlarmSprayWater::getCompanyName, AlarmSprayWater::getHandlingStatus,
                    AlarmSprayWater::getHandler, AlarmSprayWater::getHandlingTime, AlarmSprayWater::getHandlingContent,
                    AlarmSprayWater::getFalseAlarm, AlarmSprayWater::getSitePhoto, AlarmSprayWater::getAttributeValue4,
                    AlarmSprayWater::getSystemType)
                    .in(CollectionUtils.isNotEmpty(companyIdList), AlarmSprayWater::getCompanyId, companyIdList)
                    .like(StringUtils.isNotBlank(companyName), AlarmSprayWater::getCompanyName, companyName)
                    .eq(handlingStatus != null, AlarmSprayWater::getHandlingStatus, handlingStatus)
                    .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), AlarmSprayWater::getAlarmTime, startDate, endDate);
            queryWrapperOne.orderByDesc(AlarmSprayWater::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() + "," + OnlineMethod.alarmName(list.get(i).getAlarmType()) + ",高警值：" + list.get(i).getAttributeValue4() + "" + OnlineMethod.company(list.get(i).getAlarmType()));
                }
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

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

    @Override
    public void updateAlarmSmokeControl(AlarmSprayWater alarmSprayWater) {
        if (alarmSprayWater.getId() == 0) {
            LambdaQueryWrapper<AlarmSprayWater> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(AlarmSprayWater::getAlarmType, alarmSprayWater.getAlarmType())
                    .eq(AlarmSprayWater::getDeviceCode, alarmSprayWater.getDeviceCode());
            List<AlarmSprayWater> 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(alarmSprayWater.getHandlingContent());
                    list.get(i).setHandlerPhone(SecurityUtils.getLoginUser().getSysPerson().getLinkPhone());
                    list.get(i).setFalseAlarm(alarmSprayWater.getFalseAlarm());
                    this.updateById(list.get(i));
                }
            }
        } else {
            alarmSprayWater.setHandler(SecurityUtils.getUsername());
            alarmSprayWater.setHandlingTime(LocalDateTime.now());
            alarmSprayWater.setHandlingStatus(1);
            alarmSprayWater.setHandlerPhone(SecurityUtils.getLoginUser().getSysPerson().getLinkPhone());
            this.updateById(alarmSprayWater);
        }
    }


    @Override
    public List<AlarmSprayWater> alarmSprayWaterListOne(String companyName, Integer handlingStatus, String startDate, String endDate) {
        List<AlarmSprayWater> 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<AlarmSprayWater> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.select(AlarmSprayWater::getId, AlarmSprayWater::getDeviceCode,
                    AlarmSprayWater::getDeviceName, AlarmSprayWater::getAlarmType, AlarmSprayWater::getAlarmTime,
                    AlarmSprayWater::getCompanyId, AlarmSprayWater::getCompanyName, AlarmSprayWater::getHandlingStatus,
                    AlarmSprayWater::getHandler, AlarmSprayWater::getHandlingTime, AlarmSprayWater::getHandlingContent,
                    AlarmSprayWater::getFalseAlarm, AlarmSprayWater::getSitePhoto, AlarmSprayWater::getAttributeValue4)
                    .in(CollectionUtils.isNotEmpty(companyIdList), AlarmSprayWater::getCompanyId, companyIdList)
                    .like(StringUtils.isNotBlank(companyName), AlarmSprayWater::getCompanyName, companyName)
                    .eq(handlingStatus != null, AlarmSprayWater::getHandlingStatus, handlingStatus)
                    .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), AlarmSprayWater::getAlarmTime, startDate, endDate);
            queryWrapperOne.orderByDesc(AlarmSprayWater::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() + "," + OnlineMethod.alarmName(list.get(i).getAlarmType()) + ",高警值：" + list.get(i).getAttributeValue4() + "" + OnlineMethod.company(list.get(i).getAlarmType()));
                }
            }
        }
        return list;
    }

    @Override
    public Integer sprayWaterCount(String companyId, String startTime, String endTime) {
        LambdaQueryWrapper<AlarmSprayWater> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AlarmSprayWater::getCompanyId, companyId)
                .between(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime), AlarmSprayWater::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<AlarmSprayWater> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.select(AlarmSprayWater::getAlarmTime, AlarmSprayWater::getDeviceCode,
                AlarmSprayWater::getAlarmType, AlarmSprayWater::getAddress,
                AlarmSprayWater::getHandlingStatus, AlarmSprayWater::getDeviceName,
                AlarmSprayWater::getAttributeValue4, AlarmSprayWater::getPortNum)
                .eq(AlarmSprayWater::getCompanyId, companyId)
                .orderByDesc(AlarmSprayWater::getId)
                .last(" limit 2");
        List<AlarmSprayWater> 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(OnlineMethod.alarmName(list1.get(i).getAlarmType()));
            dataVo.setType4(list1.get(i).getAddress());
            dataVo.setType5("-");
            dataVo.setType6(list1.get(i).getDeviceName() + "," + OnlineMethod.alarmName(list1.get(i).getAlarmType()) + ",高警值：" + list1.get(i).getAttributeValue4() + "" + OnlineMethod.company(list1.get(i).getAlarmType()));
            dataVo.setType7(list1.get(i).getHandlingStatus() == 1 ? "已处理" : "未处理");
            list.add(dataVo);
        }
        return list;
    }
}
