package com.huimv.beeboxs.service.impl;

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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.beeboxs.common.utils.Result;
import com.huimv.beeboxs.common.utils.ResultCode;
import com.huimv.beeboxs.entity.HiveBaseBeehive;
import com.huimv.beeboxs.mapper.HiveBaseBeehiveMapper;
import com.huimv.beeboxs.service.IHiveBaseBeehiveService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-04-21
 */
@Service
public class HiveBaseBeehiveServiceImpl extends ServiceImpl<HiveBaseBeehiveMapper, HiveBaseBeehive> implements IHiveBaseBeehiveService {

    @Override
    public Result searchList(Map map) {
        Integer current =  (Integer)map.get("current");
        Integer pageSize =   (Integer) map.get("pageSize");
        Integer farmId =(Integer)  map.get("farmId");
        String apUidHex =(String)  map.get("apUidHex");

        if (farmId == null || farmId <1 ){
            return new  Result(10001,"牧场错误",false);
        }
        if (current == null || current <1 ){
            current =1;
        }
        if (pageSize == null || pageSize <1 ){
            pageSize =10;
        }
        QueryWrapper<HiveBaseBeehive> wrapper = new QueryWrapper<>();

        wrapper.eq("farm_id",farmId);
        wrapper.eq(StringUtils.isNotBlank(apUidHex),"ap_uid_hex",apUidHex);
        Page<HiveBaseBeehive> page = this.page(new Page<>(current, pageSize),wrapper);

        return new  Result(ResultCode.SUCCESS,page);
    }

    @Override
    public Result listAll(Map map) {
        Integer current =  (Integer)map.get("current");
        Integer pageSize =   (Integer) map.get("pageSize");
        if (current == null || current <1 ){
            current =1;
        }
        if (pageSize == null || pageSize <1 ){
            pageSize =10;
        }
        Page<HiveBaseBeehive> page = this.page(new Page<>(current, pageSize));

        return new  Result(ResultCode.SUCCESS,page);
    }

    @Override
    public Result listCount(Map map) {
        Integer farmId = (Integer)map.get("farmId");
        String apUidHex = (String)map.get("apUidHex");
        QueryWrapper<HiveBaseBeehive> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id",farmId);
        wrapper.eq(StringUtils.isNotBlank(apUidHex),"ap_uid_hex",apUidHex);

        HashMap<String, Object> endMap = new HashMap<>();
        int stationNum = this.count(wrapper);


        wrapper.eq("tag_state",0);
        int onStationNum = this.count(wrapper);

        endMap.put("onStationNum",onStationNum);
        endMap.put("offStationNum",stationNum -  onStationNum);
        endMap.put("stationNum",stationNum);

        return new Result(ResultCode.SUCCESS,endMap);
    }
}
