package com.bonus.canteen.core.device.manage.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.device.door.dto.terminal.DcTerminalVo;
import com.bonus.canteen.core.device.door.dto.terminal.QueryPageDcTerminalDto;
import com.bonus.canteen.core.device.manage.mapper.DeviceDcTerminalMapper;
import com.bonus.canteen.core.device.manage.model.DeviceDcTerminal;
import com.bonus.canteen.core.device.manage.model.DeviceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeviceDcTerminalService {
   @Autowired
   private DeviceDcTerminalMapper deviceDcTerminalMapper;

   public void insert(DeviceDcTerminal deviceDcTerminal) {
      this.deviceDcTerminalMapper.insert(deviceDcTerminal);
   }

   public void deleteByDeviceId(Long deviceId) {
      this.deviceDcTerminalMapper.delete(Wrappers.lambdaQuery(DeviceDcTerminal.class).eq(DeviceDcTerminal::getDeviceId, deviceId));
   }

   public DeviceDcTerminal getByDeviceId(Long deviceId) {
      return (DeviceDcTerminal)this.deviceDcTerminalMapper.selectOne(Wrappers.lambdaQuery(DeviceDcTerminal.class).eq(DeviceDcTerminal::getDeviceId, deviceId));
   }

   public void update(DeviceDcTerminal deviceDcTerminal) {
      this.deviceDcTerminalMapper.update(deviceDcTerminal, Wrappers.lambdaUpdate(DeviceDcTerminal.class).eq(DeviceDcTerminal::getDeviceId, deviceDcTerminal.getDeviceId()));
   }

   public void updateLastUpdateTime(Long deviceId) {
      this.deviceDcTerminalMapper.update(null, (Wrappers.lambdaUpdate(DeviceDcTerminal.class).set(DeviceDcTerminal::getLastPushDate, LocalDateTime.now())).eq(DeviceDcTerminal::getDeviceId, deviceId));
   }

   public List<DeviceInfo> getDoUpdateDevices(List<Long> terminalIds) {
      return this.deviceDcTerminalMapper.getDoUpdateDevices(terminalIds);
   }

   public void updateAllSystemDate() {
      this.deviceDcTerminalMapper.update(null, Wrappers.lambdaUpdate(DeviceDcTerminal.class).set(DeviceDcTerminal::getSystemDateUpdateDate, LocalDateTime.now()));
   }

   public Page<DcTerminalVo> pageByParams(Page<DcTerminalVo> page, QueryPageDcTerminalDto params) {
      return this.deviceDcTerminalMapper.pageByParams(page, params);
   }

}
