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.SaasParkElevatorPerson;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorRecordPermissionRecordsEntity;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorRelativePersonElevator;
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.ElevatorRelativePersonElevatorRequest;
import com.yuchen.saas.park.api.vo.elevator.ElevatorRelativePersonElevatorVO;
import com.yuchen.saas.park.service.mapper.elevator.SaasParkElevatorPersonMapper;
import com.yuchen.saas.park.service.mapper.elevator.SaasParkElevatorRelativePersonElevatorMapper;
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.ISaasParkElevatorRelativePersonElevatorService;
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 javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 梯控人员关联电梯表 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-07-08
 */
@Service
public class SaasParkElevatorRelativePersonElevatorServiceImpl extends BaseServiceImpl<SaasParkElevatorRelativePersonElevatorMapper, SaasParkElevatorRelativePersonElevator> implements ISaasParkElevatorRelativePersonElevatorService {


    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;

    @Resource
    private PropertyBuildingsMapper propertyBuildingsMapper;

    @Resource
    private PropertyBuildingUnitMapper propertyBuildingUnitMapper;

    @Resource
    private SaasParkElevatorPersonMapper saasParkElevatorPersonMapper;


    @Override
    public R<IPage<ElevatorRelativePersonElevatorVO>> pageByParam(ElevatorRelativePersonElevatorRequest qryDTO, Query query) {

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

        LambdaQueryWrapper<SaasParkElevatorRelativePersonElevator> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getParkId()), SaasParkElevatorRelativePersonElevator::getParkId, qryDTO.getParkId());
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getBuildingId()), SaasParkElevatorRelativePersonElevator::getBuildingId, qryDTO.getBuildingId());
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getUnitId()), SaasParkElevatorRelativePersonElevator::getUnitId, qryDTO.getUnitId());

        queryWrapper.like(Func.isNotEmpty(qryDTO.getPersonName()), SaasParkElevatorRelativePersonElevator::getPersonName, qryDTO.getPersonName());
        queryWrapper.like(Func.isNotEmpty(qryDTO.getTel()), SaasParkElevatorRelativePersonElevator::getTel, qryDTO.getTel());
        queryWrapper.like(Func.isNotEmpty(qryDTO.getPointGroupName()), SaasParkElevatorRelativePersonElevator::getPointGroupName, qryDTO.getPointGroupName());
        queryWrapper.like(Func.isNotEmpty(qryDTO.getElevatorName()), SaasParkElevatorRelativePersonElevator::getElevatorName, qryDTO.getElevatorName());

        queryWrapper.eq(Func.isNotEmpty(qryDTO.getSendState()), SaasParkElevatorRelativePersonElevator::getSendState, qryDTO.getSendState());
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getPermissionStatus()), SaasParkElevatorRelativePersonElevator::getPermissionStatus, qryDTO.getPermissionStatus());

        queryWrapper.orderByDesc(SaasParkElevatorRelativePersonElevator::getUpdateTime);

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

        List<SaasParkElevatorRelativePersonElevator> saasParkElevatorRelativePersonElevatorList = pages.getRecords();
        if (saasParkElevatorRelativePersonElevatorList.isEmpty()) {
            return R.data(Condition.getPage(query));
        }

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

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

    @Override
    public List<SaasParkElevatorRelativePersonElevator> getPermissionListByPersonId(Long personId) {
        LambdaQueryWrapper<SaasParkElevatorRelativePersonElevator> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SaasParkElevatorRelativePersonElevator::getPersonId, personId);
        List<SaasParkElevatorRelativePersonElevator> listSaasParkElevatorRelativePersonElevator = baseMapper.selectList(queryWrapper);
        return listSaasParkElevatorRelativePersonElevator;
    }


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

    }


    public R<ElevatorRelativePersonElevatorVO> detail(Long id, SaasParkElevatorRelativePersonElevator saasParkElevatorRelativePersonElevator) {
        ElevatorRelativePersonElevatorVO elevatorRelativePersonElevatorVO = new ElevatorRelativePersonElevatorVO();
        BeanUtils.copyProperties(saasParkElevatorRelativePersonElevator, elevatorRelativePersonElevatorVO);


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

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

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

        SaasParkElevatorPerson saasParkElevatorPerson = saasParkElevatorPersonMapper.selectById(saasParkElevatorRelativePersonElevator.getPersonId());

        //人员类型 1:住户人员;2:工作人员;3:临时人员
        if(saasParkElevatorPerson.getUserType().equals(1)){
            elevatorRelativePersonElevatorVO.setPersonType("住户人员");
        }
        if(saasParkElevatorPerson.getUserType().equals(2)){
            elevatorRelativePersonElevatorVO.setPersonType("工作人员");
        }
        if(saasParkElevatorPerson.getUserType().equals(3)){
            elevatorRelativePersonElevatorVO.setPersonType("临时人员");
        }



        return R.data(elevatorRelativePersonElevatorVO);
    }
}
