package com.ruoyi.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.demo.entity.Equipment;
import com.ruoyi.demo.entity.vo.EquipmentVo;
import com.ruoyi.demo.mapper.EquipmentMapper;
import com.ruoyi.demo.service.EquipmentService;
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.Arrays;
import java.util.List;

@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    EquipmentMapper equipmentMapper;
    @Autowired
    InitMapUtil initMapUtil;
    @Override
    public void addEquipment(EquipmentVo equipmentVo) {
        Equipment equipment = new Equipment(equipmentVo);
        //地区码映射
        equipment.setAddrCodeInfo(initMapUtil.getInitAddrCodeMap(equipment.getAddrCode()));
        //时间
        equipment.setEnterTime(LocalDateTime.now());
        equipmentMapper.insert(equipment);
    }

    @Override
    public void updateEquipment(EquipmentVo equipmentVo) {
        equipmentVo.setAddrCodeInfo(initMapUtil.getInitAddrCodeMap(equipmentVo.getAddrCode()));
        equipmentMapper.updateEquipment(equipmentVo);
    }

    @Override
    public Equipment getEquipment(String equipmentId) {
        return equipmentMapper.selectById(equipmentId);
    }

    @Override
    public void deleteEquipment(String[] equipmentIds) {
        equipmentMapper.deleteBatchIds(Arrays.asList(equipmentIds));
    }



    @Override
    public PageInfo<Equipment> getEquipmentBySelect(EquipmentVo equipmentVo) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("store_id");
        if (equipmentVo.getSearchText() != null && !equipmentVo.getSearchText().equals("")){
            queryWrapper.and(queryWrapper1 -> {
                queryWrapper1.likeRight("equipment_id",equipmentVo.getSearchText());
                queryWrapper1.or().like("equipment_name",equipmentVo.getSearchText());
            });
        }
        PageHelper.startPage(equipmentVo.getPageNum(),equipmentVo.getPageSize());
        List<Equipment> equipment = equipmentMapper.selectList(queryWrapper);
        PageInfo<Equipment> pageInfo = new PageInfo<>(equipment);
        return pageInfo;
    }

    @Override
    public void addStoreEquipment(EquipmentVo equipmentVo) {
        equipmentMapper.addStoreEquipment(equipmentVo);
    }

    @Override
    public PageInfo<Equipment> getEquipmentByStoreId(EquipmentVo equipmentVo) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store",equipmentVo.getStoreId());
        PageHelper.startPage(equipmentVo.getPageNum(),equipmentVo.getPageSize());
        List<Equipment> equipment = equipmentMapper.selectList(queryWrapper);
        PageInfo<Equipment> pageInfo = new PageInfo<>(equipment);
        return pageInfo;
    }
}
