package com.huimv.beeboxs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.beeboxs.common.utils.Result;
import com.huimv.beeboxs.common.utils.ResultCode;
import com.huimv.beeboxs.entity.HiveBaseBeehive;
import com.huimv.beeboxs.entity.HiveBaseStation;
import com.huimv.beeboxs.entity.dto.HiveBaseBeeStationDao;
import com.huimv.beeboxs.entity.vo.HiveBaseStationVo;
import com.huimv.beeboxs.mapper.HiveBaseStationMapper;
import com.huimv.beeboxs.service.IHiveBaseBeehiveService;
import com.huimv.beeboxs.service.IHiveBaseStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-04-21
 */
@Service
public class HiveBaseStationServiceImpl extends ServiceImpl<HiveBaseStationMapper, HiveBaseStation> implements IHiveBaseStationService {
    @Autowired
    private IHiveBaseBeehiveService baseBeehiveService;

    @Override
    public Result searchList(Map map) {

        Integer current =  (Integer)map.get("current");
        Integer pageSize =   (Integer) map.get("pageSize");
        Integer farmId =(Integer)  map.get("farmId");

        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<HiveBaseStation> wrapper = new QueryWrapper<>();

        wrapper.eq("farm_id",farmId);
        Page<HiveBaseStation> page = this.page(new Page<>(current, pageSize));

        List<HiveBaseStation> records = page.getRecords();
        List list = new ArrayList();
        for (HiveBaseStation record : records) {
            HiveBaseBeeStationDao hiveBaseStationVo = new HiveBaseBeeStationDao();
            BeanUtil.copyProperties(record,hiveBaseStationVo);
            String apUidHex = record.getApUidHex();
            QueryWrapper<HiveBaseBeehive> hiveBaseBeehiveQueryWrapper = new QueryWrapper<>();
            hiveBaseBeehiveQueryWrapper.eq("ap_uid_hex",apUidHex);
            //总数
            hiveBaseStationVo.setBeeBoxNum(  baseBeehiveService.count(hiveBaseBeehiveQueryWrapper));
            hiveBaseBeehiveQueryWrapper.eq("tag_state",1);
            hiveBaseStationVo.setOffBeeBoxNum(  baseBeehiveService.count(hiveBaseBeehiveQueryWrapper));

            hiveBaseBeehiveQueryWrapper.clear();
            hiveBaseBeehiveQueryWrapper.eq("ap_uid_hex",apUidHex).eq("tag_state",0);
            hiveBaseStationVo.setOnBeeBoxNum(  baseBeehiveService.count(hiveBaseBeehiveQueryWrapper));
            list.add(hiveBaseStationVo);
        }
        page.setRecords(list);
        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;
        }
        QueryWrapper<HiveBaseStation> wrapper = new QueryWrapper<>();

        Page<HiveBaseStation> 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");
        QueryWrapper<HiveBaseStation> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id",farmId);

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


        wrapper.eq("ap_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);
    }
}
