package com.casic.missiles.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.modular.system.dao.DeviceRuleMapper;
import com.casic.missiles.modular.system.model.DeviceRule;
import com.casic.missiles.modular.system.service.IDeviceRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 设备配置表 服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2019-05-16
 */
@Service
public class DeviceRuleServiceImpl extends ServiceImpl<DeviceRuleMapper, DeviceRule> implements IDeviceRuleService {

    @Resource
    private ICommonPermissionService permissionService;
    @Override
    public void updateRule(String devCode,Float highValue,Float lowValue) {
        List<DeviceRule> deviceRuleList=getDeviceRuleListByDevCode(devCode);
        deviceRuleList.forEach(deviceRule -> {
            this.deleteById(deviceRule.getId());
        });
        DeviceRule deviceRule = new DeviceRule();
        deviceRule.setDevcode(devCode);
        deviceRule.setHighvalue(highValue);
        deviceRule.setLowvalue(lowValue);
        deviceRule.setSensorcode(getSensorCodeByDevCode(devCode));
        deviceRule.setTs(new Date());
        deviceRule.setOperator( permissionService.getCurrLoginUser().getAccount());
        insert(deviceRule);
    }

    public List<DeviceRule> getDeviceRuleListByDevCode(String devCode) {
        EntityWrapper<DeviceRule> deviceRuleEntityWrapper = new EntityWrapper<>();
        deviceRuleEntityWrapper.eq("DEVCODE", devCode);
        return this.baseMapper.selectList(deviceRuleEntityWrapper);
    }

    public String getSensorCodeByDevCode(String devCode) {
        return this.baseMapper.getSensorCodeByDevCode(devCode);
    }
}
