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.AlarmHydrantWater;
import com.usky.fire.domain.AlarmSmokeControl;
import com.usky.fire.domain.BaseUserCompany;
import com.usky.fire.mapper.AlarmSmokeControlMapper;
import com.usky.fire.service.AlarmSmokeControlService;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 防排烟隐患告警 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-08-18
 */
@Service
public class AlarmSmokeControlServiceImpl extends AbstractCrudService<AlarmSmokeControlMapper, AlarmSmokeControl> implements AlarmSmokeControlService {

    @Autowired
    private BaseUserCompanyService baseUserCompanyService;

    @Override
    public CommonPage<AlarmSmokeControl> alarmSmokeList(String companyName, Integer handlingStatus, String startDate, String endDate,
                                                        Integer pageNum, Integer pageSize) {
        IPage<AlarmSmokeControl> page = new Page<>(pageNum, pageSize);
        List<AlarmSmokeControl> 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<AlarmSmokeControl> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.select(AlarmSmokeControl::getId, AlarmSmokeControl::getDeviceCode,
                    AlarmSmokeControl::getDeviceName, AlarmSmokeControl::getAlarmType, AlarmSmokeControl::getAlarmTime,
                    AlarmSmokeControl::getCompanyId, AlarmSmokeControl::getCompanyName, AlarmSmokeControl::getHandlingStatus,
                    AlarmSmokeControl::getHandler, AlarmSmokeControl::getHandlingTime, AlarmSmokeControl::getHandlingContent,
                    AlarmSmokeControl::getFalseAlarm, AlarmSmokeControl::getSitePhoto, AlarmSmokeControl::getSystemType)
                    .in(AlarmSmokeControl::getCompanyId, companyIdList)
                    .like(StringUtils.isNotBlank(companyName), AlarmSmokeControl::getCompanyName, companyName)
                    .eq(handlingStatus != null, AlarmSmokeControl::getHandlingStatus, handlingStatus)
                    .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), AlarmSmokeControl::getAlarmTime, startDate, endDate);
            queryWrapperOne.orderByDesc(AlarmSmokeControl::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()));
                }
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }


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

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

    @Override
    public List<AlarmSmokeControl> alarmSmokeListOne(String companyName, Integer handlingStatus, String startDate, String endDate) {
        List<AlarmSmokeControl> 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<AlarmSmokeControl> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.select(AlarmSmokeControl::getId, AlarmSmokeControl::getDeviceCode,
                    AlarmSmokeControl::getDeviceName, AlarmSmokeControl::getAlarmType, AlarmSmokeControl::getAlarmTime,
                    AlarmSmokeControl::getCompanyId, AlarmSmokeControl::getCompanyName, AlarmSmokeControl::getHandlingStatus,
                    AlarmSmokeControl::getHandler, AlarmSmokeControl::getHandlingTime, AlarmSmokeControl::getHandlingContent,
                    AlarmSmokeControl::getFalseAlarm, AlarmSmokeControl::getSitePhoto)
                    .in(AlarmSmokeControl::getCompanyId, companyIdList)
                    .like(StringUtils.isNotBlank(companyName), AlarmSmokeControl::getCompanyName, companyName)
                    .eq(handlingStatus != null, AlarmSmokeControl::getHandlingStatus, handlingStatus)
                    .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), AlarmSmokeControl::getAlarmTime, startDate, endDate);
            queryWrapperOne.orderByDesc(AlarmSmokeControl::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()));
                }
            }
        }
        return list;
    }

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