package com.gxwebsoft.apps.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.apps.mapper.EquipmentMapper;
import com.gxwebsoft.apps.service.EquipmentService;
import com.gxwebsoft.apps.entity.Equipment;
import com.gxwebsoft.apps.param.EquipmentParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备管理Service实现
 *
 * @author 科技小王子
 * @since 2022-11-30 02:11:16
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {
    @Resource
    private UserService userService;

    @Override
    public PageResult<Equipment> pageRel(EquipmentParam param) {
        PageParam<Equipment, EquipmentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<Equipment> list = baseMapper.selectPageRel(page, param);
        // 查询绑定电池的用户
        for(Equipment equipment : list){
          if(!equipment.getUserId().equals(0)){
            equipment.setUser(userService.getById(equipment.getUserId()));
          }
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<Equipment> listRel(EquipmentParam param) {
        List<Equipment> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<Equipment, EquipmentParam> page = new PageParam<>();
        page.setDefaultOrder("create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public Equipment getByIdRel(Integer equipmentId) {
        EquipmentParam param = new EquipmentParam();
        param.setEquipmentId(equipmentId);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public Equipment getByEquipmentCode(String equipmentCode) {
      return query().eq("equipment_code", equipmentCode).one();
    }

}
