package com.yuchen.saas.park.service.service.elevator.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.device.api.dto.IdDto;
import com.yuchen.saas.device.api.enums.NetAlarmRecordStatusEnum;
import com.yuchen.saas.park.api.dto.repair.SaasMatterRepairDispatchDto;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorMonitorAlarmRecords;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.entity.property.PropertyBuildingUnit;
import com.yuchen.saas.park.api.entity.property.PropertyBuildings;
import com.yuchen.saas.park.api.request.ElevatorMonitorAlarmRecordRequest;
import com.yuchen.saas.park.api.response.SaasParkElevatorMonitorAlarmRecordsResponse;
import com.yuchen.saas.park.api.vo.elevator.ElevatorAlarmRecordExportExcelVo;
import com.yuchen.saas.park.service.mapper.elevator.SaasParkElevatorMonitorAlarmRecordsMapper;
import com.yuchen.saas.park.service.mapper.property.PropertyBuildingUnitMapper;
import com.yuchen.saas.park.service.mapper.property.PropertyBuildingsMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.elevator.ISaasParkElevatorMonitorAlarmRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.excel.util.ExcelUtil;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 梯控-告警 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-06-24
 */
@Slf4j
@Service
public class SaasParkElevatorMonitorAlarmRecordsServiceImpl extends BaseServiceImpl<SaasParkElevatorMonitorAlarmRecordsMapper, SaasParkElevatorMonitorAlarmRecords> implements ISaasParkElevatorMonitorAlarmRecordsService {

    @Autowired
    private PmgBatParkInfoService pmgBatParkInfoService;


    @Autowired
    private PropertyBuildingsMapper propertyBuildingsMapper;

    @Autowired
    private PropertyBuildingUnitMapper propertyBuildingUnitMapper;

    @Override
    public R<IPage<SaasParkElevatorMonitorAlarmRecordsResponse>> alarmList(ElevatorMonitorAlarmRecordRequest qryDto, Query query) {

        Page<SaasParkElevatorMonitorAlarmRecords> page = new Page<>(query.getCurrent(), query.getSize());

        LambdaQueryWrapper<SaasParkElevatorMonitorAlarmRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Func.isNotEmpty(qryDto.getParkId()), SaasParkElevatorMonitorAlarmRecords::getParkId, qryDto.getParkId());

        queryWrapper.like(Func.isNotEmpty(qryDto.getManagementArea()), SaasParkElevatorMonitorAlarmRecords::getManageAreaIds, qryDto.getManagementArea());
        queryWrapper.eq(Func.isNotEmpty(qryDto.getLevel()), SaasParkElevatorMonitorAlarmRecords::getAlarmLevel, qryDto.getLevel());
        queryWrapper.like(Func.isNotEmpty(qryDto.getAlarmName()), SaasParkElevatorMonitorAlarmRecords::getAlarmName, qryDto.getAlarmName());
        queryWrapper.like(Func.isNotEmpty(qryDto.getDeviceName()), SaasParkElevatorMonitorAlarmRecords::getDeviceName, qryDto.getDeviceName());
        queryWrapper.like(Func.isNotEmpty(qryDto.getDeviceLocation()), SaasParkElevatorMonitorAlarmRecords::getLocation, qryDto.getDeviceLocation());
        queryWrapper.like(Func.isNotEmpty(qryDto.getSpatialPosition()), SaasParkElevatorMonitorAlarmRecords::getSpatialPosition, qryDto.getSpatialPosition());
        queryWrapper.eq(Func.isNotEmpty(qryDto.getStatus()), SaasParkElevatorMonitorAlarmRecords::getStatus, qryDto.getStatus());
        queryWrapper.ge(Func.isNotEmpty(qryDto.getStartAlarmTime()), SaasParkElevatorMonitorAlarmRecords::getAlarmTime, qryDto.getStartAlarmTime());
        queryWrapper.le(Func.isNotEmpty(qryDto.getEndAlarmTime()), SaasParkElevatorMonitorAlarmRecords::getAlarmTime, qryDto.getEndAlarmTime());
        queryWrapper.ge(Func.isNotEmpty(qryDto.getStartCloseTime()), SaasParkElevatorMonitorAlarmRecords::getCloseTime, qryDto.getStartCloseTime());
        queryWrapper.le(Func.isNotEmpty(qryDto.getEndCloseTime()), SaasParkElevatorMonitorAlarmRecords::getCloseTime, qryDto.getEndCloseTime());


        IPage<SaasParkElevatorMonitorAlarmRecords> pages = this.page(page, queryWrapper);

        List<SaasParkElevatorMonitorAlarmRecords> saasParkElevatorMonitorAlarmRecordsList = pages.getRecords();


        if (saasParkElevatorMonitorAlarmRecordsList.isEmpty()) {
            return R.data(Condition.getPage(query));
        }

        //组装返回response
        IPage<SaasParkElevatorMonitorAlarmRecordsResponse> pageVo = new Page(pages.getCurrent(), pages.getSize(), pages.getTotal());
        List<SaasParkElevatorMonitorAlarmRecordsResponse> records = this.listVO(pages.getRecords());

        pageVo.setRecords(records);
        return R.data(pageVo);


    }

    public List<SaasParkElevatorMonitorAlarmRecordsResponse> listVO(List<SaasParkElevatorMonitorAlarmRecords> listRecords) {
        return (List) listRecords.stream().map(entity -> {
            SaasParkElevatorMonitorAlarmRecordsResponse saasParkElevatorMonitorAlarmRecordsResponse = detail(entity.getId(), entity).getData();
            return saasParkElevatorMonitorAlarmRecordsResponse;
        }).collect(Collectors.toList());

    }


    public R<SaasParkElevatorMonitorAlarmRecordsResponse> detail(Long id, SaasParkElevatorMonitorAlarmRecords saasParkElevatorMonitorAlarmRecords) {
        SaasParkElevatorMonitorAlarmRecordsResponse saasParkElevatorMonitorAlarmRecordsResponse = new SaasParkElevatorMonitorAlarmRecordsResponse();
        BeanUtils.copyProperties(saasParkElevatorMonitorAlarmRecords, saasParkElevatorMonitorAlarmRecordsResponse);


        PmgBatParkInfo parkInfo = pmgBatParkInfoService.getById(saasParkElevatorMonitorAlarmRecords.getParkId());
        if (Func.isNotEmpty(parkInfo)) {
            saasParkElevatorMonitorAlarmRecordsResponse.setParkName(parkInfo.getParkName());
        }

        PropertyBuildings propertyBuildings = propertyBuildingsMapper.selectById(saasParkElevatorMonitorAlarmRecords.getBuildingId());
        if (propertyBuildings != null) {
            saasParkElevatorMonitorAlarmRecordsResponse.setBuildingName(propertyBuildings.getBuildingName());
        }

        PropertyBuildingUnit propertyBuildingUnit = propertyBuildingUnitMapper.selectById(saasParkElevatorMonitorAlarmRecords.getUnitId());
        if (propertyBuildingUnit != null) {
            saasParkElevatorMonitorAlarmRecordsResponse.setUnitName(propertyBuildingUnit.getUnitName());
        }

        return R.data(saasParkElevatorMonitorAlarmRecordsResponse);
    }


    @Override
    public void exportAlarmRecordsData(ElevatorMonitorAlarmRecordRequest qryDto, HttpServletResponse response) {

        /*qryDto.setCurrent(-1);
        qryDto.setSize(-1);
        List<SaasParkElevatorMonitorAlarmRecords> list = this.selectPage(qryDto).getRecords();
        String dateStr = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm");
        List<NetAlarmRecordExportExcelVo> excelVoList = BeanUtil.copyToList(list, NetAlarmRecordExportExcelVo.class);
        ExcelUtil.export(response, "告警中心" + dateStr, "告警中心", excelVoList, NetAlarmRecordExportExcelVo.class);*/

        Page<SaasParkElevatorMonitorAlarmRecords> page = new Page<>(-1, -1);

        LambdaQueryWrapper<SaasParkElevatorMonitorAlarmRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Func.isNotEmpty(qryDto.getParkId()), SaasParkElevatorMonitorAlarmRecords::getParkId, qryDto.getParkId());

        queryWrapper.like(Func.isNotEmpty(qryDto.getManagementArea()), SaasParkElevatorMonitorAlarmRecords::getManageAreaIds, qryDto.getManagementArea());
        queryWrapper.eq(Func.isNotEmpty(qryDto.getLevel()), SaasParkElevatorMonitorAlarmRecords::getAlarmLevel, qryDto.getLevel());
        queryWrapper.like(Func.isNotEmpty(qryDto.getAlarmName()), SaasParkElevatorMonitorAlarmRecords::getAlarmName, qryDto.getAlarmName());
        queryWrapper.like(Func.isNotEmpty(qryDto.getDeviceName()), SaasParkElevatorMonitorAlarmRecords::getDeviceName, qryDto.getDeviceName());
        queryWrapper.like(Func.isNotEmpty(qryDto.getDeviceLocation()), SaasParkElevatorMonitorAlarmRecords::getLocation, qryDto.getDeviceLocation());
        queryWrapper.like(Func.isNotEmpty(qryDto.getSpatialPosition()), SaasParkElevatorMonitorAlarmRecords::getSpatialPosition, qryDto.getSpatialPosition());
        queryWrapper.eq(Func.isNotEmpty(qryDto.getStatus()), SaasParkElevatorMonitorAlarmRecords::getStatus, qryDto.getStatus());
        queryWrapper.ge(Func.isNotEmpty(qryDto.getStartAlarmTime()), SaasParkElevatorMonitorAlarmRecords::getAlarmTime, qryDto.getStartAlarmTime());
        queryWrapper.le(Func.isNotEmpty(qryDto.getEndAlarmTime()), SaasParkElevatorMonitorAlarmRecords::getAlarmTime, qryDto.getEndAlarmTime());
        queryWrapper.ge(Func.isNotEmpty(qryDto.getStartCloseTime()), SaasParkElevatorMonitorAlarmRecords::getCloseTime, qryDto.getStartCloseTime());
        queryWrapper.le(Func.isNotEmpty(qryDto.getEndCloseTime()), SaasParkElevatorMonitorAlarmRecords::getCloseTime, qryDto.getEndCloseTime());


        IPage<SaasParkElevatorMonitorAlarmRecords> pages = this.page(page, queryWrapper);

        List<SaasParkElevatorMonitorAlarmRecords> saasParkElevatorMonitorAlarmRecordsList = pages.getRecords();


        //组装返回response
        IPage<SaasParkElevatorMonitorAlarmRecordsResponse> pageVo = new Page(pages.getCurrent(), pages.getSize(), pages.getTotal());
        List<SaasParkElevatorMonitorAlarmRecordsResponse> list = this.listVO(pages.getRecords());

        //qryDto.setCurrent(-1);
        //qryDto.setSize(-1);

        String dateStr = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm");
        List<ElevatorAlarmRecordExportExcelVo> excelVoList = BeanUtil.copyToList(list, ElevatorAlarmRecordExportExcelVo.class);
        ExcelUtil.export(response, "告警中心" + dateStr, "告警中心", excelVoList, ElevatorAlarmRecordExportExcelVo.class);


    }


    /**
     * 分页查询
     *
     * @param qryDto
     * @return
     */
    public Page<SaasParkElevatorMonitorAlarmRecords> selectPage(ElevatorMonitorAlarmRecordRequest qryDto) {
        Page<SaasParkElevatorMonitorAlarmRecords> page = new Page<>(qryDto.getCurrent(), qryDto.getSize());
        LambdaQueryWrapper<SaasParkElevatorMonitorAlarmRecords> qw = new LambdaQueryWrapper<>();

        qw.like(Func.isNotEmpty(qryDto.getManagementArea()), SaasParkElevatorMonitorAlarmRecords::getManageAreaIds, qryDto.getManagementArea());
        qw.eq(Func.isNotEmpty(qryDto.getLevel()), SaasParkElevatorMonitorAlarmRecords::getAlarmLevel, qryDto.getLevel());
        qw.like(Func.isNotEmpty(qryDto.getAlarmName()), SaasParkElevatorMonitorAlarmRecords::getAlarmName, qryDto.getAlarmName());
        qw.like(Func.isNotEmpty(qryDto.getDeviceName()), SaasParkElevatorMonitorAlarmRecords::getDeviceName, qryDto.getDeviceName());
        qw.like(Func.isNotEmpty(qryDto.getDeviceLocation()), SaasParkElevatorMonitorAlarmRecords::getLocation, qryDto.getDeviceLocation());
        qw.like(Func.isNotEmpty(qryDto.getSpatialPosition()), SaasParkElevatorMonitorAlarmRecords::getSpatialPosition, qryDto.getSpatialPosition());
        qw.eq(Func.isNotEmpty(qryDto.getStatus()), SaasParkElevatorMonitorAlarmRecords::getStatus, qryDto.getStatus());
        qw.ge(Func.isNotEmpty(qryDto.getStartAlarmTime()), SaasParkElevatorMonitorAlarmRecords::getAlarmTime, qryDto.getStartAlarmTime());
        qw.le(Func.isNotEmpty(qryDto.getEndAlarmTime()), SaasParkElevatorMonitorAlarmRecords::getAlarmTime, qryDto.getEndAlarmTime());
        qw.ge(Func.isNotEmpty(qryDto.getStartCloseTime()), SaasParkElevatorMonitorAlarmRecords::getCloseTime, qryDto.getStartCloseTime());
        qw.le(Func.isNotEmpty(qryDto.getEndCloseTime()), SaasParkElevatorMonitorAlarmRecords::getCloseTime, qryDto.getEndCloseTime());
        return this.page(page, qw);
    }


    @Override
    public void updateRepaire(SaasMatterRepairDispatchDto dispatchDto) {
        /*SaasMatterRepair matterRepair = getById(dispatchDto.getId());
        if (Func.isEmpty(matterRepair)) {
            throw new ServiceException("报事报修不存在");
        }
        SaasWorkOrderDto workOrderDto = new SaasWorkOrderDto();
        workOrderDto.setWorkOrderCategory(1);
        SaasWorkOrderMatterRepairDto workOrderMatterRepairDto = org.nest.springwrap.core.tool.utils.BeanUtil.copyProperties(dispatchDto, SaasWorkOrderMatterRepairDto.class);
        workOrderMatterRepairDto.setLinkNo(matterRepair.getId());
        workOrderDto.setWorkOrderMatterRepairDto(workOrderMatterRepairDto);
        SaasWorkOrder workOrder = workOrderService.add(workOrderDto);
        matterRepair.setRepairStatus(RepairStatusEnum.TO_HANDLE.getValue());
        updateById(matterRepair);
        // 添加处理记录
        SaasMatterRepairHandleRecord handleRecord = new SaasMatterRepairHandleRecord();
        handleRecord.setRepairId(matterRepair.getId());
        handleRecord.setOperateTime(new Date());
        handleRecord.setOperateType("派工");
        handleRecord.setOperateUserId(AuthUtil.getUser().getOrgUserId());
        handleRecord.setOperateContent(dispatchDto.getDispatchRemark());
        matterRepairHandleRecordService.save(handleRecord);
        return Boolean.TRUE;*/
    }

    @Override
    public void close(IdDto idDto) {
        closeBatch(List.of(idDto.getId()));
    }

    @Override
    public void closeBatch(List<Long> ids) {
        LambdaUpdateWrapper<SaasParkElevatorMonitorAlarmRecords> uw = new LambdaUpdateWrapper<>();
        uw.in(SaasParkElevatorMonitorAlarmRecords::getId, ids);
        uw.set(SaasParkElevatorMonitorAlarmRecords::getStatus, NetAlarmRecordStatusEnum.CLOSE.getValue());
        uw.set(SaasParkElevatorMonitorAlarmRecords::getCloseTime, new Date());
        uw.set(SaasParkElevatorMonitorAlarmRecords::getUpdateTime, new Date());
        uw.set(SaasParkElevatorMonitorAlarmRecords::getCloseUserId, AuthUtil.getUser().getUserId());
        uw.set(SaasParkElevatorMonitorAlarmRecords::getCloseUserName, AuthUtil.getUser().getUserName());
        this.update(uw);
    }


}
