package com.zhongshu.card.server.core.service.visitor;

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultMessage;
import com.zhongshu.card.client.model.base.IDsParam;
import com.zhongshu.card.client.model.visitor.setting.VisitorSettingDeviceModel;
import com.zhongshu.card.client.model.visitor.setting.VisitorSettingDeviceParam;
import com.zhongshu.card.client.model.visitor.setting.VisitorSettingDeviceSearch;
import com.zhongshu.card.server.core.dao.devices.DeviceInfoDao;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.scene.SceneComponentDao;
import com.zhongshu.card.server.core.dao.visitor.VisitorSettingDeviceDao;
import com.zhongshu.card.server.core.domain.devices.DeviceInfo;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.domain.scene.SceneComponent;
import com.zhongshu.card.server.core.domain.visitor.VisitorSettingDevice;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.devices.DeviceInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 组件关联的设备
 *
 * @author TRX
 * @date 2024/12/19
 */
@Slf4j
@Service
public class VisitorSettingDeviceService extends SuperService {

    @Autowired
    private OrganizationDao organizationDao;

    @Autowired
    private DeviceInfoDao deviceInfoDao;

    @Autowired
    private DeviceInfoServiceImpl deviceInfoService;

    @Autowired
    private VisitorSettingDeviceDao visitorSettingDeviceDao;

    @Autowired
    private SceneComponentDao sceneComponentDao;

    //-----------------------------分组关联的设备信息 start--------------------

    public ResultContent bindDevices(VisitorSettingDeviceParam param) {
        SceneComponent sceneComponent = sceneComponentDao.findTopById(param.getSceneComponentId());
        if (ObjectUtils.isEmpty(sceneComponent)) {
            return ResultContent.buildFail("组件不存在");
        }
        if (ObjectUtils.isEmpty(param.getDeviceIds())) {
            return ResultContent.buildFail("设备信息为空");
        }
        List<DeviceInfo> list = deviceInfoDao.findByDeviceIdIn(param.getDeviceIds());
        if (ObjectUtils.isEmpty(list)) {
            return ResultContent.buildFail("未查询到设备");
        }
        if (ObjectUtils.isNotEmpty(list)) {
            List<VisitorSettingDevice> saveList = new ArrayList<>();
            String projectOid = sceneComponent.getProjectOid();
            if (StringUtils.isEmpty(projectOid) && sceneComponent.getSceneInfo() != null) {
                projectOid = sceneComponent.getSceneInfo().getProjectOid();
            }
            if (StringUtils.isEmpty(projectOid)) {
                projectOid = param.getProjectOid();
            }

            if (StringUtils.isEmpty(projectOid)) {
                return ResultContent.buildFail("projectOid 信息为空");
            }
            for (DeviceInfo deviceInfo : list) {
                VisitorSettingDevice entity = visitorSettingDeviceDao.findTopBySceneComponentIdAndDeviceId(param.getSceneComponentId(), deviceInfo.getDeviceId());
                if (ObjectUtils.isEmpty(entity)) {
                    entity = new VisitorSettingDevice();
                    entity.setDeviceInfo(deviceInfo);
                    entity.setSceneComponentId(sceneComponent.getId());
                    entity.setProjectOid(projectOid);
                    entity.setDeviceId(deviceInfo.getDeviceId());
                    initEntity(entity);
                } else {
                    initUpdateEntity(entity);
                }
                entity.setDeviceName(deviceInfo.getDeviceName());
                entity.setDeviceType(deviceInfo.getDeviceType());
                saveList.add(entity);
            }
            if (ObjectUtils.isNotEmpty(saveList)) {
                visitorSettingDeviceDao.saveAll(saveList);
            }
        }
        return ResultContent.buildSuccess();
    }

    public ResultContent deleteComponentDeviceInfo(String id) {
        VisitorSettingDevice entity = visitorSettingDeviceDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        visitorSettingDeviceDao.delete(entity);
        return ResultContent.buildSuccess();
    }

    public ResultContent deleteComponentDevicesInfo(IDsParam param) {
        if (ObjectUtils.isEmpty(param.getIds())) {
            return ResultContent.buildFail("ids不能为空");
        }
        List<VisitorSettingDevice> list = visitorSettingDeviceDao.findByIdIn(param.getIds());
        if (ObjectUtils.isEmpty(list)) {
            return ResultContent.buildFail("数据不存在");
        }
        visitorSettingDeviceDao.deleteAllById(param.getIds());
        return ResultContent.buildSuccess();
    }

    public ResultContent<Page<VisitorSettingDeviceModel>> pageDevices(VisitorSettingDeviceSearch param, Pageable pageable) {
        String projectOid = param.getProjectOid();
        Organization organization = organizationDao.findTopByOid(projectOid);
        if (ObjectUtils.isEmpty(organization)) {
            return ResultContent.buildFail("projectOid不存在");
        }
        if (StringUtils.isEmpty(param.getSceneComponentId())) {
            return ResultContent.buildFail("sceneComponentId 不能为空");
        }
        Page<VisitorSettingDevice> page = visitorSettingDeviceDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toDeviceModel));
    }

    //-----------------------------分组关联的设备信息 end----------------------

    public VisitorSettingDeviceModel toDeviceModel(VisitorSettingDevice entity) {
        VisitorSettingDeviceModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new VisitorSettingDeviceModel();
            BeanUtils.copyProperties(entity, model);
            model.setId(entity.getId());
            model.setDeviceInfo(deviceInfoService.toModel(entity.getDeviceInfo()));
        }
        return model;
    }

}
