package com.fastbee.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastbee.common.extend.core.domin.mq.SubDeviceBo;
import com.fastbee.common.extend.utils.SecurityUtils;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.domain.ModbusParams;
import com.fastbee.iot.domain.SubGateway;
import com.fastbee.iot.mapper.SubGatewayMapper;
import com.fastbee.iot.model.gateWay.GateSubDeviceVO;
import com.fastbee.iot.model.gateWay.SubDeviceAddVO;
import com.fastbee.iot.model.gateWay.SubDeviceListVO;
import com.fastbee.iot.service.IModbusParamsService;
import com.fastbee.iot.service.ISubGatewayService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 网关与子设备关联Service业务层处理
 *
 * @author gsb
 * @date 2024-05-27
 */
@Service
public class SubGatewayServiceImpl extends ServiceImpl<SubGatewayMapper,SubGateway> implements ISubGatewayService
{
    @Resource
    private SubGatewayMapper gatewayMapper;
    @Resource
    private IModbusParamsService modbusParamsService;

    /**
     * 查询网关与子设备关联
     *
     * @param id 网关与子设备关联主键
     * @return 网关与子设备关联
     */
    @Override
    public SubGateway selectGatewayById(Long id)
    {
        return gatewayMapper.selectById(id);
    }

    /**
     * 查询网关与子设备关联列表
     *
     * @param gateway 网关与子设备关联
     * @return 网关与子设备关联
     */
    @Override
    public Page<SubDeviceListVO> selectGatewayList(SubGateway gateway)
    {
        return gatewayMapper.selectGatewayList(new Page<>(gateway.getPageNum(),gateway.getPageSize()), gateway);
    }

    /**
     * 新增网关与子设备关联
     *
     * @param gateway 网关与子设备关联
     * @return 结果
     */
    @Override
    public int insertGateway(SubGateway gateway)
    {
        gateway.setCreateTime(DateUtils.getNowDate());
        return gatewayMapper.insert(gateway);
    }

    /**
     * 修改网关与子设备关联
     *
     * @param gateway 网关与子设备关联
     * @return 结果
     */
    @Override
    public int updateGateway(SubGateway gateway)
    {
        gateway.setUpdateTime(DateUtils.getNowDate());
        return gatewayMapper.updateById(gateway);
    }

    /**
     * 批量删除网关与子设备关联
     *
     * @param ids 需要删除的网关与子设备关联主键
     * @return 结果
     */
    @Override
    public int deleteGatewayByIds(Long[] ids)
    {
        return gatewayMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除网关与子设备关联信息
     *
     * @param id 网关与子设备关联主键
     * @return 结果
     */
    @Override
    public int deleteGatewayById(Long id)
    {
        return gatewayMapper.deleteById(id);
    }

    /**
     * 获取可选的网关子设备列表
     * @return
     */
    @Override
    public Page<GateSubDeviceVO> getIsSelectGateSubDevice(GateSubDeviceVO subDeviceVO){
        return gatewayMapper.getIsSelectGateSubDevice(new Page<>(subDeviceVO.getPageNum(), subDeviceVO.getPageSize()), subDeviceVO);
    }

    /**
     * 批量添加子设备
     * @param subDeviceAddVO
     * @return
     */
    @Override
    public int insertSubDeviceBatch(SubDeviceAddVO subDeviceAddVO){
        Long gwDeviceId = subDeviceAddVO.getGwDeviceId();
        Long[] subDeviceIds = subDeviceAddVO.getSubDeviceIds();
        List<SubGateway> list = new ArrayList<>();
        if (subDeviceIds.length >0){
            for (Long subDeviceId : subDeviceIds) {
                ModbusParams modbusParams = modbusParamsService.getModbusParamsByDeviceId(subDeviceId);
                SubGateway subGateway = new SubGateway();
                subGateway.setSubDeviceId(subDeviceId);
                subGateway.setGwDeviceId(gwDeviceId);
                if (!Objects.isNull(modbusParams)) {
                    subGateway.setSlaveId(modbusParams.getSlaveId());
                }
                subGateway.setCreateBy(SecurityUtils.getUsername());
                list.add(subGateway);
            }
        }
        return gatewayMapper.insertBatch(list) ? 1 : 0;
    }

    /**
     * 批量更新子设备
     * @param list
     * @return
     */
    @Override
    public void updateSubDeviceBatch(List<SubGateway> list){
        assert !CollectionUtils.isEmpty(list) : "集合为空";
        for (SubGateway gateway : list) {
            this.updateGateway(gateway);
        }
    }

    /**
     * 根据网关设备编号查询子设备列表
     * @param gwSerialNumber
     * @return
     */
    @Override
    public List<SubDeviceBo> getSubDeviceListByGw(String gwSerialNumber){
        return gatewayMapper.getSubDeviceListByGw(gwSerialNumber);
    }

    private LambdaQueryWrapper<SubGateway> buildQueryWrapper(SubGateway query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<SubGateway> lqw = Wrappers.lambdaQuery();
        lqw.eq(query.getGwDeviceId() != null, SubGateway::getGwDeviceId, query.getGwDeviceId());
        lqw.eq(query.getSubDeviceId() != null, SubGateway::getSubDeviceId, query.getSubDeviceId());
        lqw.eq(query.getSlaveId() != null, SubGateway::getSlaveId, query.getSlaveId());

        if (!Objects.isNull(params.get("beginTime")) &&
                !Objects.isNull(params.get("endTime"))) {
            lqw.between(SubGateway::getCreateTime, params.get("beginTime"), params.get("endTime"));
        }
        return lqw;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SubGateway entity){
        //TODO 做一些数据校验,如唯一约束
    }
}
