package com.usky.transfer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.transfer.domain.DmpDeviceCommand;
import com.usky.transfer.mapper.DmpDeviceCommandMapper;
import com.usky.transfer.service.DmpDeviceCommandService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 创建下发命令历史记录表 服务实现类
 * </p>
 *
 * @author ya
 * @since 2024-09-24
 */
@Service
public class DmpDeviceCommandServiceImpl extends AbstractCrudService<DmpDeviceCommandMapper, DmpDeviceCommand> implements DmpDeviceCommandService {

    @Override
    public CommonPage<DmpDeviceCommand> deviceCommandRecord(Integer commandStatus,String productCode,String deviceUuid, String userName, String startTime, String endTime, Integer pageNum, Integer pageSize){
        IPage<DmpDeviceCommand> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<DmpDeviceCommand> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(commandStatus != null,DmpDeviceCommand::getCommandStatus,commandStatus)
                .like(StringUtils.isNotBlank(productCode),DmpDeviceCommand::getProductCode,productCode)
                .like(StringUtils.isNotBlank(deviceUuid),DmpDeviceCommand::getDeviceUuid,deviceUuid)
                .like(StringUtils.isNotBlank(userName),DmpDeviceCommand::getUserName,userName)
                .between(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime),DmpDeviceCommand::getCreatedTime,startTime,endTime)
                .eq(DmpDeviceCommand::getTenantId, SecurityUtils.getTenantId())
                .orderByDesc(DmpDeviceCommand::getId);
        page = this.page(page,queryWrapper);

        return new CommonPage<>(page.getRecords(),page.getTotal(),pageSize,pageNum);
    }
}