package com.huimv.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.SysBaseConfig;
import com.huimv.admin.mapper.SysBaseConfigMapper;
import com.huimv.admin.service.ISysBaseConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-09-20
 */
@Service
public class SysBaseConfigServiceImpl extends ServiceImpl<SysBaseConfigMapper, SysBaseConfig> implements ISysBaseConfigService {
    @Resource
    private SysBaseConfigMapper sysBaseConfigMapper;
    @Override
    public Result listWarning(Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<SysBaseConfig> wrapper = new QueryWrapper<>();
        SysBaseConfig baseConfig = sysBaseConfigMapper.selectOne(wrapper.eq("config_value", farmId));
        Map endMap = new HashMap();

        if (ObjectUtil.isNotEmpty(baseConfig)){
            wrapper.clear();
            wrapper.eq("row_id",baseConfig.getRowId()).eq("config_key","eartagOfflineTime");
            SysBaseConfig eartagOfflineTime = sysBaseConfigMapper.selectOne(wrapper);
            endMap.put("eartagOfflineTime",eartagOfflineTime.getConfigValue());

            wrapper.clear();
            wrapper.eq("row_id",baseConfig.getRowId()).eq("config_key","deviceOfflineTime");
            SysBaseConfig deviceOfflineTime = sysBaseConfigMapper.selectOne(wrapper);
            endMap.put("deviceOfflineTime",deviceOfflineTime.getConfigValue());

            wrapper.clear();
            wrapper.eq("row_id",baseConfig.getRowId()).eq("config_key","eartagMaxTem");
            SysBaseConfig eartagMaxTem = sysBaseConfigMapper.selectOne(wrapper);
            endMap.put("eartagMaxTem",eartagMaxTem.getConfigValue());

            wrapper.clear();
            wrapper.eq("row_id",baseConfig.getRowId()).eq("config_key","eartagMinTem");
            SysBaseConfig eartagMinTem = sysBaseConfigMapper.selectOne(wrapper);
            endMap.put("eartagMinTem",eartagMinTem.getConfigValue());
        }else {
            endMap.put("eartagOfflineTime",120);
            endMap.put("deviceOfflineTime",1440);
            endMap.put("eartagMaxTem",40);
            endMap.put("eartagMinTem",0);
        }
        return new Result(ResultCode.SUCCESS,endMap);
    }
}
