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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.park.PmgBatParkCompany;
import com.yuchen.saas.park.api.entity.park.PmgBatParkPeople;
import com.yuchen.saas.park.api.entity.park.PmgBatParkVisitor;
import com.yuchen.saas.park.api.entity.park.PmgBtnDeviceOther;
import com.yuchen.saas.park.api.vo.park.DeviceAndOtherVo;
import com.yuchen.saas.park.api.vo.park.PmgBatParkCompanyVo;
import com.yuchen.saas.park.api.vo.park.PmgBatSmartEquipmentVo;
import com.yuchen.saas.park.service.mapper.PmgBtnDeviceOtherMapper;
import com.yuchen.saas.park.service.service.PmgBtnDeviceOtherService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 刘通
 * @date 2020/12/31 10:43
 */
@Service
public class PmgBtnDeviceOtherServiceImpl
        extends BaseServiceImpl<PmgBtnDeviceOtherMapper, PmgBtnDeviceOther>
        implements PmgBtnDeviceOtherService {


    /**
     * 园内人员列表接口(未与该设备绑定的人员)
     * @param deviceId
     * @return
     */
    @Override
    public List<DeviceAndOtherVo> getParkPeopleList(String deviceId) {
        return baseMapper.getParkPeopleList(deviceId);
    }
    @Override
    public List<PmgBatParkPeople> getParkPeopleListV2(String deviceId) {
        return baseMapper.getParkPeopleListV2(deviceId);
    }

    @Override
    public List<PmgBatSmartEquipment> getParkPeopleListV3(String peopleId) {
        return baseMapper.getParkPeopleListV3(peopleId);
    }

    @Override
    public List<PmgBatSmartEquipmentVo> getDeviceListByOtherId(String productKey, String otherId) {
        return baseMapper.getDeviceListByOtherId(productKey, otherId);
    }

    /*******************访客开始********************/
    @Override
    public List<DeviceAndOtherVo> getParkVisitorList(String deviceId) {
        return baseMapper.getParkVisitorList(deviceId);
    }
    @Override
    public List<PmgBatParkVisitor> getParkVisitorListV2(String deviceId) {
        return baseMapper.getParkVisitorListV2(deviceId);
    }

    @Override
    public List<PmgBatSmartEquipment> getParkVisitorListV3(String peopleId) {
        return baseMapper.getParkVisitorListV3(peopleId);
    }
    /*******************访客结束********************/
    /*******************公司开始********************/
    @Override
    public List<DeviceAndOtherVo> getParkCompanyList(String deviceId) {
        return baseMapper.getParkCompanyList(deviceId);
    }

    @Override
    public List<PmgBatParkCompany> getParkCompanyListV2(String deviceId) {
        return baseMapper.getParkCompanyListV2(deviceId);
    }

    @Override
    public List<PmgBatParkCompanyVo> getParkCompanyListV3(String productKey, String deviceName) {
        return baseMapper.getParkCompanyListV3(productKey, deviceName);
    }
    /*******************公司结束********************/
    /*******************设备结束********************/
    @Override
    public List<DeviceAndOtherVo> getParkDeviceList(String deviceId) {
        return baseMapper.getParkDeviceList(deviceId);
    }

    @Override
    public List<PmgBatSmartEquipmentVo> getParkDeviceListV2(String deviceId) {
        return baseMapper.getParkDeviceListV2(deviceId);
    }
    /*******************设备结束********************/

    /**
     * 园内人员和设备绑定的保存接口(批量)
     * @param pmgBtnDeviceOtherList
     * @return
     */
    @Override
    public boolean saveList(List<PmgBtnDeviceOther> pmgBtnDeviceOtherList) {
        if(Func.isNotEmpty(pmgBtnDeviceOtherList)
                && pmgBtnDeviceOtherList.size() > 0) {
            LambdaQueryWrapper<PmgBtnDeviceOther> deviceOtherWrapper = Wrappers.lambdaQuery();
            for(PmgBtnDeviceOther deviceOther : pmgBtnDeviceOtherList) {
                int deviceCount = count(deviceOtherWrapper.eq(PmgBtnDeviceOther::getDeviceId,
                        deviceOther.getDeviceId()).eq(PmgBtnDeviceOther::getOtherId, deviceOther.getOtherId()));
                if(deviceCount < 0) {
                    return false;
                } else if(deviceCount == 0) {
                    boolean saveDevice = save(deviceOther);
                    if(!saveDevice) {
                        return false;
                    }
                } else {
                    continue;
                }
            }
        }
        return true;
    }

    /**
     * (3)解绑接口(批量)
     */
    @Override
    public boolean deleteList(List<PmgBtnDeviceOther> pmgBtnDeviceOtherList) {
        if(Func.isNotEmpty(pmgBtnDeviceOtherList)
                && pmgBtnDeviceOtherList.size() > 0) {
            LambdaQueryWrapper<PmgBtnDeviceOther> deviceOtherWrapper = Wrappers.lambdaQuery();
            for(PmgBtnDeviceOther deviceOther : pmgBtnDeviceOtherList) {
                int del = baseMapper.delete(deviceOtherWrapper.eq(PmgBtnDeviceOther::getDeviceId,
                        deviceOther.getDeviceId()).eq(PmgBtnDeviceOther::getOtherId, deviceOther.getOtherId()));
                if(del < 0) {
                    return false;
                }
            }
        }
        return true;
    }
}
