package com.huimv.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.admin.common.token.TokenSign;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.EnvDevice;
import com.huimv.admin.entity.WriteEnvData;
import com.huimv.admin.entity.vo.WriteEnvDataAddVo;
import com.huimv.admin.entity.vo.WriteEnvDataVo2;
import com.huimv.admin.mapper.EnvDeviceMapper;
import com.huimv.admin.mapper.WriteEnvDataMapper;
import com.huimv.admin.service.IWriteEnvDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 环境监测温湿度填报 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-11-17
 */
@Service
public class WriteEnvDataServiceImpl extends ServiceImpl<WriteEnvDataMapper, WriteEnvData> implements IWriteEnvDataService {

    @Autowired
    private WriteEnvDataMapper writeEnvDataMapper;

    @Autowired
    private EnvDeviceMapper deviceMapper;
    @Override
    public Result add(HttpServletRequest httpServletRequest,  WriteEnvDataAddVo writeEnvDataAddVo) {
        List<WriteEnvDataVo2> addVoList = writeEnvDataAddVo.getList();
        for (WriteEnvDataVo2 writeEnvDataVo2 : addVoList) {
            QueryWrapper<WriteEnvData> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("create_time", writeEnvDataVo2.getCreateTime())
                    .eq("unit_id", writeEnvDataAddVo.getUnitId())
                    .eq("farm_id", writeEnvDataAddVo.getFarmId());
            WriteEnvData envData = writeEnvDataMapper.selectOne(queryWrapper);
            WriteEnvData writeEnvData = new WriteEnvData();
            if (ObjectUtil.isEmpty(envData)) {
                writeEnvData.setUnitId(Integer.parseInt(writeEnvDataAddVo.getUnitId()));
                writeEnvData.setUnitName(writeEnvDataAddVo.getUnitName());
                writeEnvData.setFarmId(writeEnvDataAddVo.getFarmId());
                writeEnvData.setTemp(writeEnvDataVo2.getEnvTemp());
                writeEnvData.setHum(writeEnvDataVo2.getEnvHum());
                writeEnvData.setCreateTime(writeEnvDataVo2.getCreateTime());
                writeEnvData.setAq(writeEnvDataVo2.getAq());
                writeEnvDataMapper.insert(writeEnvData);
            } else {
                writeEnvData.setTemp(writeEnvDataVo2.getEnvTemp());
                writeEnvData.setHum(writeEnvDataVo2.getEnvHum());
                writeEnvData.setAq(writeEnvDataVo2.getAq());
                writeEnvDataMapper.updateById(writeEnvData);
            }

        }
        return new Result(ResultCode.SUCCESS);
    }

    @Override
    public Result listData(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String unitId = paramsMap.get("unitId");
        QueryWrapper<WriteEnvData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).eq("unit_id", unitId).orderByDesc("create_time");
        List<WriteEnvData> writeEnvData = writeEnvDataMapper.selectList(queryWrapper);
        return new Result(ResultCode.SUCCESS, writeEnvData);
    }

    @Override
    public Result envList(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        return new Result(ResultCode.SUCCESS, deviceMapper.selectList(queryWrapper));
    }
}
