package com.huimv.admin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.ProdFermentData;
import com.huimv.admin.entity.ProdFermentDevice;
import com.huimv.admin.mapper.ProdFermentDataMapper;
import com.huimv.admin.mapper.ProdFermentDeviceMapper;
import com.huimv.admin.service.IProdFermentDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.xdevapi.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 环保监测发酵罐数据表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-11-08
 */
@Service
public class ProdFermentDataServiceImpl extends ServiceImpl<ProdFermentDataMapper, ProdFermentData> implements IProdFermentDataService {

    @Autowired
    private ProdFermentDataMapper fermentDataMapper;
    @Autowired
    private ProdFermentDeviceMapper deviceMapper;

    @Override
    public Result listAll(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<ProdFermentDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        List<ProdFermentDevice> devices = deviceMapper.selectList(queryWrapper);
        List list = new ArrayList();
        for (ProdFermentDevice device : devices) {
            QueryWrapper<ProdFermentData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("device_code", device.getDeviceCode()).eq("farm_id", farmId)
                    .orderByDesc("create_time").last(" limit 1");
            ProdFermentData prodFermentData = fermentDataMapper.selectOne(queryWrapper1);
            list.add(prodFermentData);
        }
        return new Result(ResultCode.SUCCESS, list);
    }
}
