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.AlarmFire;
import com.usky.fire.domain.AlarmHydrantWater;
import com.usky.fire.domain.BaseUserCompany;
import com.usky.fire.mapper.AlarmHydrantWaterMapper;
import com.usky.fire.service.AlarmHydrantWaterService;
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 AlarmHydrantWaterServiceImpl extends AbstractCrudService<AlarmHydrantWaterMapper, AlarmHydrantWater> implements AlarmHydrantWaterService {

    @Autowired
    private BaseUserCompanyService baseUserCompanyService;

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

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

    @Override
    public List<AlarmHydrantWater> alarmHydrantWaterListOne(String companyName, Integer handlingStatus, String startDate, String endDate) {
        List<AlarmHydrantWater> 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<AlarmHydrantWater> queryWrapperOne = Wrappers.lambdaQuery();
            queryWrapperOne.select(AlarmHydrantWater::getId, AlarmHydrantWater::getDeviceCode,
                    AlarmHydrantWater::getDeviceName, AlarmHydrantWater::getAlarmType, AlarmHydrantWater::getAlarmTime,
                    AlarmHydrantWater::getCompanyId, AlarmHydrantWater::getCompanyName, AlarmHydrantWater::getHandlingStatus,
                    AlarmHydrantWater::getHandler, AlarmHydrantWater::getHandlingTime, AlarmHydrantWater::getHandlingContent,
                    AlarmHydrantWater::getFalseAlarm, AlarmHydrantWater::getSitePhoto, AlarmHydrantWater::getAttributeValue4)
                    .in(AlarmHydrantWater::getCompanyId, companyIdList)
                    .like(StringUtils.isNotBlank(companyName), AlarmHydrantWater::getCompanyName, companyName)
                    .eq(handlingStatus != null, AlarmHydrantWater::getHandlingStatus, handlingStatus)
                    .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), AlarmHydrantWater::getAlarmTime, startDate, endDate);
            queryWrapperOne.orderByDesc(AlarmHydrantWater::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 hydrantWaterCount(String companyId, Integer handlingStatus, String startTime, String endTime) {
        LambdaQueryWrapper<AlarmHydrantWater> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AlarmHydrantWater::getCompanyId, companyId)
                .eq(handlingStatus != null, AlarmHydrantWater::getHandlingStatus, handlingStatus)
                .between(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime), AlarmHydrantWater::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<AlarmHydrantWater> queryWrapperOne = Wrappers.lambdaQuery();
        queryWrapperOne.select(AlarmHydrantWater::getAlarmTime, AlarmHydrantWater::getDeviceCode,
                AlarmHydrantWater::getAlarmType, AlarmHydrantWater::getAddress,
                AlarmHydrantWater::getHandlingStatus, AlarmHydrantWater::getDeviceName,
                AlarmHydrantWater::getAttributeValue4, AlarmHydrantWater::getPortNum)
                .eq(AlarmHydrantWater::getCompanyId, companyId)
                .orderByDesc(AlarmHydrantWater::getId)
                .last(" limit 2");
        List<AlarmHydrantWater> 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;
    }
}
