package cn.iocoder.yudao.module.iot.service.devicepointrules;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.iot.controller.admin.devicepointrules.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicepointrules.DevicePointRulesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.iot.dal.mysql.devicepointrules.DevicePointRulesMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;

/**
 * 点位规则 Service 实现类
 *
 * @author 必硕科技
 */
@Service
@Validated
public class DevicePointRulesServiceImpl implements DevicePointRulesService {

    @Resource
    private DevicePointRulesMapper devicePointRulesMapper;

    @Override
    public Long createDevicePointRules(DevicePointRulesSaveReqVO createReqVO) {
        // 插入
        DevicePointRulesDO devicePointRules = BeanUtils.toBean(createReqVO, DevicePointRulesDO.class);
        devicePointRulesMapper.insert(devicePointRules);
        // 返回
        return devicePointRules.getId();
    }

    @Override
    public void updateDevicePointRules(DevicePointRulesSaveReqVO updateReqVO) {
        // 校验存在
        validateDevicePointRulesExists(updateReqVO.getId());
        // 更新
        DevicePointRulesDO updateObj = BeanUtils.toBean(updateReqVO, DevicePointRulesDO.class);
        if (!updateReqVO.getPointRulesVOList().isEmpty()){
            String jsonString = JSON.toJSONString(updateReqVO.getPointRulesVOList());
            updateObj.setFieldRule(jsonString);

        }

        devicePointRulesMapper.updateById(updateObj);
    }

    @Override
    public void deleteDevicePointRules(Long id) {
        // 校验存在
        validateDevicePointRulesExists(id);
        // 删除
        devicePointRulesMapper.deleteById(id);
    }

    private void validateDevicePointRulesExists(Long id) {
        if (devicePointRulesMapper.selectById(id) == null) {
            throw exception(DEVICE_POINT_RULES_NOT_EXISTS);
        }
    }

    @Override
    public DevicePointRulesDO getDevicePointRules(Long id) {
        return devicePointRulesMapper.selectById(id);
    }

    @Override
    public PageResult<DevicePointRulesDO> getDevicePointRulesPage(DevicePointRulesPageReqVO pageReqVO) {
        return devicePointRulesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DevicePointRulesDO> getList(Long id) {
        return devicePointRulesMapper.selectList(Wrappers.<DevicePointRulesDO>lambdaQuery()
                .eq(DevicePointRulesDO::getDeviceId,id).eq(DevicePointRulesDO::getIdentifier,"ALARM").orderByDesc(DevicePointRulesDO::getDeviceId));
    }

}