package com.ruoyi.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.demo.entity.Equipment;
import com.ruoyi.demo.entity.Store;
import com.ruoyi.demo.entity.bo.StoreBo;
import com.ruoyi.demo.entity.vo.StoreVo;
import com.ruoyi.demo.mapper.EquipmentMapper;
import com.ruoyi.demo.mapper.StoreMapper;
import com.ruoyi.demo.service.StoreService;
import com.ruoyi.demo.utils.InitMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    StoreMapper storeMapper;

    @Autowired
    EquipmentMapper equipmentMapper;

    @Autowired
    InitMapUtil initMapUtil;

    @Override
    public void addStore(StoreVo storeVo) {
        Store store = new Store(storeVo);
        store.setAddrCodeInfo(initMapUtil.getInitAddrCodeMap(store.getAddrCode()));
        store.setCreateTime(LocalDateTime.now());
        storeMapper.insert(store);
    }

    @Override
    public void updateStore(StoreVo storeVo) {
        storeVo.setAddrCodeInfo(initMapUtil.getInitAddrCodeMap(storeVo.getAddrCode()));
        storeMapper.updateStore(storeVo);
    }

    @Override
    public void deleteStore(String[] storeIds) {
        storeMapper.deleteBatchIds(Arrays.asList(storeIds));
    }

    /*
    * 店铺管理设备（不支持设备上层没有店铺）
    * */
    @Override
    public HashMap getStoreList(StoreVo storeVo) {
        //1.找到所有店铺列表
        QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
        if(storeVo.getSearchText() != null && !storeVo.getStoreName().equals("")){
            queryWrapper.like("store_name",storeVo.getSearchText());
        }
        PageHelper.startPage(storeVo.getPageNum(),storeVo.getPageSize());
        List<Store> stores = storeMapper.selectList(queryWrapper);
        PageInfo<Store> pageInfo = new PageInfo<>(stores);

        //2.找到子设备
        List<String> collect = pageInfo.getList().stream().map(item -> {
            return item.getStoreId();
        }).collect(Collectors.toList());
        QueryWrapper<Equipment> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select("store_id","count(*) as state");
        queryWrapper1.in("store_id",collect);
        queryWrapper1.groupBy("store_id");
        List<Equipment> equipments = equipmentMapper.selectList(queryWrapper1);

        List<StoreBo> storeBos = new ArrayList<>();
        //组装
        for (Store store : pageInfo.getList()) {
            StoreBo storeBo = new StoreBo(store);
            storeBo.setCount(findEquipmentCount(store.getStoreId(),equipments));
            storeBos.add(storeBo);
        }

        pageInfo.setList(null);
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("page",pageInfo);
        objectObjectHashMap.put("data",stores);
        return objectObjectHashMap;
    }

    private int findEquipmentCount(String storeId,List<Equipment> equipments){
        for (Equipment equipment : equipments) {
            if (equipment.getStoreId().equals(storeId))
                return equipment.getState();
        }
        return 0;
    }

    @Override
    public Store getStore(String storeId) {
        Store store = storeMapper.selectById(storeId);
        return store;
    }
}
