package org.dromara.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.redis.utils.CacheUtils;
import org.springframework.stereotype.Service;
import org.dromara.domain.bo.TblSensorBo;
import org.dromara.domain.vo.TblSensorVo;
import org.dromara.domain.TblSensor;
import org.dromara.mapper.TblSensorMapper;
import org.dromara.service.ITblSensorService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 传感器列Service业务层处理
 *
 * @author Lion Li
 * @date 2024-08-11
 */
@RequiredArgsConstructor
@Service
public class TblSensorServiceImpl implements ITblSensorService {

    private final TblSensorMapper baseMapper;

    /**
     * 查询传感器列
     *
     * @param id 主键
     * @return 传感器列
     */
    @Override
    public TblSensorVo queryById(Long id){
        TblSensorVo vo = baseMapper.selectVoById(id);

        JSONArray datapoints =JSONUtil.parseArray(vo.getDatapoints());
        for (Object obj : datapoints) {
            Object data = CacheUtils.get("sensorData","sensor_"+vo.getId()+((JSONObject)obj).getStr("name"));
            if(data!=null){
                ((JSONObject)obj).set("value",((JSONObject)data).get("value"));
                ((JSONObject)obj).set("vdate",((JSONObject)data).get("vdate"));
            }
        }
        vo.setDatapoints(JSONUtil.toJsonStr(datapoints));

        return vo;
    }

    /**
     * 分页查询传感器列列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 传感器列分页列表
     */
    @Override
    public TableDataInfo<TblSensorVo> queryPageList(TblSensorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblSensor> lqw = buildQueryWrapper(bo);
        Page<TblSensorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的传感器列列表
     *
     * @param bo 查询条件
     * @return 传感器列列表
     */
    @Override
    public List<TblSensorVo> queryList(TblSensorBo bo) {
        LambdaQueryWrapper<TblSensor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblSensor> buildQueryWrapper(TblSensorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblSensor> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TblSensor::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSn()), TblSensor::getSn, bo.getSn());
        lqw.eq(StringUtils.isNotBlank(bo.getSensorType()), TblSensor::getSensorType, bo.getSensorType());
        lqw.eq(bo.getProtocalType() != null, TblSensor::getProtocalType, bo.getProtocalType());
        lqw.eq(StringUtils.isNotBlank(bo.getSensorDesc()), TblSensor::getSensorDesc, bo.getSensorDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TblSensor::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDatapoints()), TblSensor::getDatapoints, bo.getDatapoints());
        lqw.eq(StringUtils.isNotBlank(bo.getEvents()), TblSensor::getEvents, bo.getEvents());
        lqw.eq(StringUtils.isNotBlank(bo.getFaults()), TblSensor::getFaults, bo.getFaults());
        return lqw;
    }

    /**
     * 新增传感器列
     *
     * @param bo 传感器列
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TblSensorBo bo) {
        TblSensor add = MapstructUtils.convert(bo, TblSensor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改传感器列
     *
     * @param bo 传感器列
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TblSensorBo bo) {
        TblSensor update = MapstructUtils.convert(bo, TblSensor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除传感器列信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
