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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorManageLocationEntity;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorPerson;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorRelativePark;
import com.yuchen.saas.park.api.vo.elevator.ElevatorPersonVO;
import com.yuchen.saas.park.api.vo.elevator.SaasParkElevatorRelativeParkVO;
import com.yuchen.saas.park.service.constant.ElevatorConstant;
import com.yuchen.saas.park.service.constant.TorchConstant;
import com.yuchen.saas.park.service.mapper.elevator.SaasParkElevatorRelativeParkMapper;
import com.yuchen.saas.park.service.service.elevator.ISaasParkElevatorManageLocationService;
import com.yuchen.saas.park.service.service.elevator.ISaasParkElevatorRelativeParkService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.utils.BeanUtil;
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.Date;
import java.util.List;

/**
 * <p>
 * 梯控-园区关联表 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-08-21
 */
@Service
public class SaasParkElevatorRelativeParkServiceImpl extends BaseServiceImpl<SaasParkElevatorRelativeParkMapper, SaasParkElevatorRelativePark> implements ISaasParkElevatorRelativeParkService {

    @Resource
    private ISaasParkElevatorManageLocationService iSaasParkElevatorManageLocationService;

    @Override
    @Transactional
    public SaasParkElevatorRelativeParkVO getDetailById(Long parkId) {
        SaasParkElevatorRelativeParkVO saasParkElevatorRelativeParkVO = new SaasParkElevatorRelativeParkVO();
        if (Func.isNotEmpty(parkId)) {
            SaasParkElevatorRelativePark saasParkElevatorRelativePark = getOne(Wrappers.<SaasParkElevatorRelativePark>lambdaQuery().eq(SaasParkElevatorRelativePark::getParkId, parkId));
            if (saasParkElevatorRelativePark == null) {
                SaasParkElevatorRelativePark saasParkElevatorRelativePark2 = new SaasParkElevatorRelativeParkVO();
                saasParkElevatorRelativePark2.setParkId(parkId);
                saasParkElevatorRelativePark2.setCreateTime(new Date());
                saasParkElevatorRelativePark2.setUpdateTime(new Date());
                saasParkElevatorRelativePark2.setIsOpen(false);
                saasParkElevatorRelativePark2.setLimitType("" + ElevatorConstant.ElevatorLimitType.LIMIT_FLOOR);
                saveOrUpdateByParam(saasParkElevatorRelativePark2);
                saasParkElevatorRelativePark = saasParkElevatorRelativePark2;
            }


            BeanUtil.copyProperties(saasParkElevatorRelativePark, saasParkElevatorRelativeParkVO);
        }
        return saasParkElevatorRelativeParkVO;
    }

    @Override
    @Transactional
    public boolean saveOrUpdateByParam(SaasParkElevatorRelativePark saasParkElevatorRelativePark) {
        Long id = saasParkElevatorRelativePark.getId();
        SaasParkElevatorRelativePark saasParkElevatorRelativeParkOld = new SaasParkElevatorRelativePark();

        if (Func.isNotEmpty(saasParkElevatorRelativePark.getParkId()) && Func.isEmpty(id)) {
            SaasParkElevatorRelativePark saasParkElevatorRelativePark1 = this.baseMapper.selectOne(new LambdaQueryWrapper<SaasParkElevatorRelativePark>().eq(SaasParkElevatorRelativePark::getParkId, saasParkElevatorRelativePark.getParkId()));
            if (saasParkElevatorRelativePark1 == null) {
                saasParkElevatorRelativeParkOld = saasParkElevatorRelativePark;
            } else {
                saasParkElevatorRelativeParkOld = saasParkElevatorRelativePark1;
                saasParkElevatorRelativeParkOld.setParkId(saasParkElevatorRelativePark.getParkId());
                saasParkElevatorRelativeParkOld.setLimitType(saasParkElevatorRelativePark.getLimitType());
                saasParkElevatorRelativeParkOld.setIsOpen(saasParkElevatorRelativePark.getIsOpen());
            }

            //如果是关闭,那么 调用IOT进行参数关闭设置
            if (!saasParkElevatorRelativePark.getIsOpen()) {
                LambdaQueryWrapper<SaasParkElevatorManageLocationEntity> queryWrapperItem = new LambdaQueryWrapper<>();
                queryWrapperItem.eq(SaasParkElevatorManageLocationEntity::getIsEnabled, true);
                List<SaasParkElevatorManageLocationEntity> list = iSaasParkElevatorManageLocationService.list(queryWrapperItem);
                for (SaasParkElevatorManageLocationEntity saasParkElevatorManageLocationEntity : list) {
                    saasParkElevatorManageLocationEntity.setIsEnabled(false);
                    iSaasParkElevatorManageLocationService.sendSetParameter(saasParkElevatorManageLocationEntity);
                }
            }

        }
        return saveOrUpdate(saasParkElevatorRelativeParkOld);
    }
}
