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

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorMaintenanceWarn;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorManageLocationEntity;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorPermissionElevatorGroup;
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.MaintenanceWarnRequest;
import com.yuchen.saas.park.api.response.ElevatorPointResponse;
import com.yuchen.saas.park.api.response.MaintenanceWarnResponse;
import com.yuchen.saas.park.service.mapper.elevator.SaasParkElevatorMaintenanceWarnMapper;
import com.yuchen.saas.park.service.service.elevator.ISaasParkElevatorMaintenanceWarnService;
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.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 梯控电梯维保提醒 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-08-29
 */
@Service
public class SaasParkElevatorMaintenanceWarnServiceImpl extends BaseServiceImpl<SaasParkElevatorMaintenanceWarnMapper, SaasParkElevatorMaintenanceWarn> implements ISaasParkElevatorMaintenanceWarnService {

    @Override
    public R<IPage<MaintenanceWarnResponse>> list(MaintenanceWarnRequest qryDTO, Query query)  {
        Page<SaasParkElevatorMaintenanceWarn> page = new Page<>(query.getCurrent(), query.getSize());

        LambdaQueryWrapper<SaasParkElevatorMaintenanceWarn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getElevatorId()), SaasParkElevatorMaintenanceWarn::getElevatorId, qryDTO.getElevatorId());
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getElevatorName()), SaasParkElevatorMaintenanceWarn::getElevatorName, qryDTO.getElevatorName());
        queryWrapper.ge(Func.isNotEmpty(qryDTO.getBeginTime()),SaasParkElevatorMaintenanceWarn::getOverTime,qryDTO.getBeginTime());
        queryWrapper.le(Func.isNotEmpty(qryDTO.getEndTime()),SaasParkElevatorMaintenanceWarn::getOverTime,qryDTO.getEndTime());

        queryWrapper.orderByDesc(SaasParkElevatorMaintenanceWarn::getCreateTime);

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

        List<SaasParkElevatorMaintenanceWarn> saasParkElevatorMaintenanceWarnList = pages.getRecords();


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

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

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

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

    }


    public R<MaintenanceWarnResponse> detail(Long id, SaasParkElevatorMaintenanceWarn saasParkElevatorMaintenanceWarn) {
        MaintenanceWarnResponse maintenanceWarnResponse = new MaintenanceWarnResponse();
        BeanUtils.copyProperties(saasParkElevatorMaintenanceWarn, maintenanceWarnResponse);

        //凑出其他数据信息,并把组信息返回
        /*List<SaasParkElevatorPermissionElevatorGroup> groupByElevatorId = saasParkElevatorPermissionElevatorGroupMapper.getGroupByElevatorId(id.toString());
        maintenanceWarnResponse.setGroups(groupByElevatorId);

        String levatorRangeStr = "";
        for (int i = 0; i < groupByElevatorId.size(); i++) {
            if (i != groupByElevatorId.size() - 1) {
                levatorRangeStr += groupByElevatorId.get(i).getGroupName() + ",";
            } else {
                levatorRangeStr += groupByElevatorId.get(i).getGroupName();
            }
        }
        maintenanceWarnResponse.setGroupNamesStr(levatorRangeStr);

        ElevatorPointResponse elevatorPoint = baseMapper.getElevatorPoint(SaasParkElevatorMaintenanceWarn);
        if (elevatorPoint != null) {
            maintenanceWarnResponse.setProductName(elevatorPoint.getProductName());
        }


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

        PropertyBuildingUnit propertyBuildingUnit = propertyBuildingUnitMapper.selectById(SaasParkElevatorMaintenanceWarn.getUnitId());
        if (propertyBuildingUnit != null) {
            maintenanceWarnResponse.setUnitName(propertyBuildingUnit.getUnitName());
        }*/

        return R.data(maintenanceWarnResponse);
    }



}
