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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorRecordPermissionRecordsEntity;
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.ElevatorPermissionDownLoadListRequest;
import com.yuchen.saas.park.api.response.ElevatorPermissionDownLoadListResponse;
import com.yuchen.saas.park.service.mapper.elevator.SaasParkElevatorRecordPermissionRecordsMapper;
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.ISaasParkElevatorRecordPermissionRecordsService;
import com.yuchen.saas.parking.api.entity.VehicleMonthlyCardBillData;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * saas_park_elevator_record_permission_records 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-06-25
 */
@Service
public class SaasParkElevatorRecordPermissionRecordsServiceImpl extends BaseServiceImpl<SaasParkElevatorRecordPermissionRecordsMapper, SaasParkElevatorRecordPermissionRecordsEntity> implements ISaasParkElevatorRecordPermissionRecordsService {

    @Autowired
    private PmgBatParkInfoService pmgBatParkInfoService;

    @Resource
    private PropertyBuildingsMapper propertyBuildingsMapper;

    @Resource
    private PropertyBuildingUnitMapper propertyBuildingUnitMapper;

    @Override
    public R<IPage<ElevatorPermissionDownLoadListResponse>> permissionDownLoadList(ElevatorPermissionDownLoadListRequest qryDTO, Query query) {

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

        LambdaQueryWrapper<SaasParkElevatorRecordPermissionRecordsEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getParkId()), SaasParkElevatorRecordPermissionRecordsEntity::getParkId, qryDTO.getParkId());
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getPersonId()), SaasParkElevatorRecordPermissionRecordsEntity::getPersonId, qryDTO.getPersonId());
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getBuildingId()), SaasParkElevatorRecordPermissionRecordsEntity::getBuildingId, qryDTO.getBuildingId());
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getUnitId()), SaasParkElevatorRecordPermissionRecordsEntity::getUnitId, qryDTO.getUnitId());
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getElevatorName()), SaasParkElevatorRecordPermissionRecordsEntity::getElevatorName, qryDTO.getElevatorName());
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getPersonName()), SaasParkElevatorRecordPermissionRecordsEntity::getPersonName, qryDTO.getPersonName());
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getPersonType()), SaasParkElevatorRecordPermissionRecordsEntity::getPersonType, qryDTO.getPersonType());
        queryWrapper.ge(Func.isNotEmpty(qryDTO.getDownloadTime()), SaasParkElevatorRecordPermissionRecordsEntity::getDownloadTime, qryDTO.getDownloadTime());
        queryWrapper.orderByDesc(SaasParkElevatorRecordPermissionRecordsEntity::getDownloadTime);

        IPage<SaasParkElevatorRecordPermissionRecordsEntity> pages = this.page(page, queryWrapper);
        List<SaasParkElevatorRecordPermissionRecordsEntity> saasParkElevatorRecordPermissionRecordsEntity = pages.getRecords();

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

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

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



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

    }


    public R<ElevatorPermissionDownLoadListResponse> detail(Long id, SaasParkElevatorRecordPermissionRecordsEntity saasParkElevatorRecordPermissionRecordsEntity) {
        ElevatorPermissionDownLoadListResponse elevatorPermissionDownLoadListResponse = new ElevatorPermissionDownLoadListResponse();
        BeanUtils.copyProperties(saasParkElevatorRecordPermissionRecordsEntity, elevatorPermissionDownLoadListResponse);


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

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

        return R.data(elevatorPermissionDownLoadListResponse);
    }


}
