package com.huimv.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.EartagData2;
import com.huimv.admin.entity.EartagEartagRegister2;
import com.huimv.admin.entity.SysAccountMultilevel;
import com.huimv.admin.mapper.EartagData2Mapper;
import com.huimv.admin.mapper.EartagEartagRegister2Mapper;
import com.huimv.admin.mapper.SysAccountMultilevelMapper;
import com.huimv.admin.service.IEartagEartagRegister2Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-09-19
 */
@Service
public class EartagEartagRegister2ServiceImpl extends ServiceImpl<EartagEartagRegister2Mapper, EartagEartagRegister2> implements IEartagEartagRegister2Service {

    @Autowired
    SysAccountMultilevelMapper sysAccountMultilevelMapper;
    @Autowired
    private EartagData2Mapper eartagData2Mapper;

    @Override
    public Result listFarmUnitEartagData(Map<String, String> params) {
        String pageSize = params.get("pageSize");
        String pageNo = params.get("pageNo");
        String unitId = params.get("unitId");
        String deviceCode = params.get("deviceCode");
        String farmId = params.get("farmId");
        String earmark = params.get("earmark");
        String liveStatus = params.get("liveStatus");
        String userId = params.get("userId");
//        SysAccountMultilevel user = sysAccountMultilevelMapper.selectById(userId);
        if (StringUtils.isBlank(pageNo)){
            pageNo="1";
        }
        if (StringUtils.isBlank(pageSize)){
            pageSize="20";
        }
        Float maxEarTemp = 0F;
        Float minEarTemp = 100F;
        QueryWrapper<EartagEartagRegister2> wrapper = new QueryWrapper<>();
        //客户数据
//        if (user.getIsUser() == 0 && !"330727001".equals(farmId)){
//            wrapper.between("ear_temp1",30,40);
//            wrapper.le("act",10000);
//            wrapper.eq("live_status", 1);
//        }
        //设备
        wrapper.like(StringUtils.isNotBlank(deviceCode),"last_device",deviceCode);
        wrapper.eq("farm_id",farmId);
        wrapper.eq(StringUtils.isNotBlank(unitId),"unit_id",unitId);
        wrapper.like(StringUtils.isNotBlank(earmark),"earmark",earmark);
        wrapper.eq(StringUtils.isNotBlank(liveStatus),"live_status",liveStatus);
        wrapper.orderByDesc("ask_time");
        //大堰河牧场过滤
//        if ("330112004".equals(farmId)){
//            wrapper.eq("live_status", 1).and( i->i.likeRight("earmark","1332022109003").or().likeRight("earmark","1332022109004"));
//        }
//        if ("330727001".equals(farmId)){
//            wrapper.eq("live_status", 1).like("earmark","31111112222");
//        }
        Page<EartagEartagRegister2> page = this.page(new Page<>(Long.parseLong(pageNo), Long.parseLong(pageSize)), wrapper);
      /*  if (user.getIsUser() == 0){
            for (EartagEartagRegister2 record : page.getRecords()) {
                String earmark1 = record.getEarmark();
                EartagData2 eartagData2 = eartagData2Mapper.selectOne(new QueryWrapper<EartagData2>().eq("earmark",earmark1).orderByDesc("id").last("limit 1"));
//             EartagData2 eartagData2Temp = eartagData2Mapper.selectOne(new QueryWrapper<EartagData2>().eq("earmark",earmark1).ge("ask_time",DateUtil.beginOfDay(new Date())).orderByDesc("ear_temp1").last("limit 1"));
//             if (ObjectUtil.isNotEmpty(eartagData2Temp)){
//                 record.setEarTemp1(eartagData2Temp.getEarTemp1());
//             }
                if (ObjectUtil.isNotEmpty(eartagData2)){
                    record.setAct(eartagData2.getAct());
                }
                Float earTemp1 = record.getEarTemp1();
                Long act = record.getAct();
                if (earTemp1 >40){
                    record.setEarTemp1(40F);
                }
                if (earTemp1 <30){
                    double a= new Random().nextInt(32) % (2) + 29;
                    DecimalFormat df = new DecimalFormat( "0.00" );
                    String str=df.format( a );

                    record.setEarTemp1(Float.parseFloat(str));
                }
                if (act >300){
                    record.setAct(300L);
                }
                if (act<0){
                    record.setAct(0L);
                }
            }
        }*/
        Map map = new HashMap();
        long allCount = this.count(wrapper);
        //在线
        if (!"330112004".equals(farmId)){
            wrapper.eq("live_status", 1);
        }
        long onCount = this.count(wrapper);
        List<EartagEartagRegister2> list = this.list(wrapper);
        if (list.size() == 0){
            minEarTemp = 0F;
        }
        for (EartagEartagRegister2 record : list) {
            Float earTemp1 = record.getEarTemp1();
            //客户数据
         /*   if (user.getIsUser() == 0){
                if (earTemp1 >40){
                    earTemp1 = 40F;
                }
                if (earTemp1 <30){
                    earTemp1 = 30F;
                }
            }*/
            maxEarTemp = Math.max(earTemp1,maxEarTemp);
            minEarTemp = Math.min(earTemp1,minEarTemp);
        }
        map.put("countOff",allCount-onCount);
        map.put("countOn",onCount);
        map.put("countAll",allCount);
        map.put("maxEarTemp",maxEarTemp);
        map.put("minEarTemp",minEarTemp);
        map.put("page",page);
        return new Result(ResultCode.SUCCESS,map);
    }
}
