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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.door.DoorDevicePoint;
import com.yuchen.saas.park.service.mapper.door.DoorDevicePointMapper;
import com.yuchen.saas.park.service.service.PmgBatSmartEquipmentService;
import com.yuchen.saas.park.service.service.door.DoorDevicePointService;
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.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 门禁点 服务实现类
 * </p>
 *
 * @author miaoqs
 * @since 2023-12-18
 */
@Service
public class DoorDevicePointServiceImpl extends BaseServiceImpl<DoorDevicePointMapper, DoorDevicePoint> implements DoorDevicePointService {

    @Resource
    private PmgBatSmartEquipmentService pmgBatSmartEquipmentService;
    @Resource
    private DoorDevicePointMapper doorDevicePointMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchSave(List<DoorDevicePoint> doorDevicePointList) {
        for (DoorDevicePoint devicePoint : doorDevicePointList) {
            save(devicePoint);
        }
        return true;
    }

    @Override
    public List<Long> getListBindId() {
        List<Long> idList = new ArrayList<>();
        List<DoorDevicePoint> list = list();
        if(Func.isNotEmpty(list)){
            idList = list.stream().map(DoorDevicePoint::getEquipmentId).collect(Collectors.toList());
        }
        return idList;
    }

    @Override
    public IPage<DoorDevicePoint> pageByparam(DoorDevicePoint doorDevicePoint, Query query) {
        IPage<DoorDevicePoint> pages = page(Condition.getPage(query),
                new LambdaQueryWrapper<DoorDevicePoint>()
                        .like(Func.isNotEmpty(doorDevicePoint.getDevicePointName()), DoorDevicePoint::getDevicePointName,
                                doorDevicePoint.getDevicePointName())
                        .eq(Func.isNotEmpty(doorDevicePoint.getParkId()), DoorDevicePoint::getParkId,
                                doorDevicePoint.getParkId())
                        /* .eq(Func.isNotEmpty(doorDevicePoint.getPlaceExplain()), DoorDevicePoint::getPlaceExplain,
                                doorDevicePoint.getPlaceExplain()) */
                        .apply(Func.isNotEmpty(doorDevicePoint.getPlaceExplain()),
                                "FIND_IN_SET('"+doorDevicePoint.getPlaceExplain()+"', place_explain) > 0")
                        .like(Func.isNotEmpty(doorDevicePoint.getDescription()), DoorDevicePoint::getDescription,
                                doorDevicePoint.getDescription())
                        .orderByDesc(DoorDevicePoint::getCreateTime)
        );

        if (Func.isNotEmpty(pages) && Func.isNotEmpty(pages.getRecords())){
            List<DoorDevicePoint> pointList = pages.getRecords();
            for (DoorDevicePoint devicePoint : pointList) {
                PmgBatSmartEquipment equipment = pmgBatSmartEquipmentService.getById(devicePoint.getEquipmentId());
                devicePoint.setDeviceName(null==equipment?"":equipment.getName());
            }
        }
        return pages;
    }

    @Override
    public List<DoorDevicePoint> listByparam(DoorDevicePoint doorDevicePoint) {
        return list(new LambdaQueryWrapper<DoorDevicePoint>()
                .eq(Func.isNotEmpty(doorDevicePoint.getParkId()), DoorDevicePoint::getParkId,doorDevicePoint.getParkId())
                .like(Func.isNotEmpty(doorDevicePoint.getDevicePointName()), DoorDevicePoint::getDevicePointName,doorDevicePoint.getDevicePointName()));
    }

    @Override
    public List<DoorDevicePoint> getListByparamAll() {
        return doorDevicePointMapper.getListByparamAll();
    }


}
