package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppEtagControllerInfoMapper;
import org.wcs.mapper.intf.AppEtagControllerInfoService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.etagBase.EtagControllerQueryReq;
import org.wcs.model.po.app.AppEtagControllerInfo;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 电子标签控制器数据访问层
 */
@Service
public class AppEtagControllerInfoDao extends ServiceImpl<AppEtagControllerInfoMapper, AppEtagControllerInfo> implements AppEtagControllerInfoService {

    /**
     * 查询电子标签控制器基础数据
     * @param request 查询参数
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public Tuple2<Long, List<AppEtagControllerInfo>> queryEtagControllerInfo(EtagControllerQueryReq request, Integer pageSize, Integer pageIndex) {
        if(request == null || pageSize == null || pageIndex == null || pageSize <= 0 || pageIndex <= 0) {
            return null;
        }
        LambdaQueryWrapper<AppEtagControllerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(request.getControllerName() != null, AppEtagControllerInfo::getControllerName, request.getControllerName())
                .like(request.getIp() != null, AppEtagControllerInfo::getIp, request.getIp());
        queryWrapper.orderByAsc(AppEtagControllerInfo::getControllerId);
        Page<AppEtagControllerInfo> page = new Page<>(pageIndex, pageSize);
        try {
            Page<AppEtagControllerInfo> selectedPage = baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(selectedPage.getTotal(), selectedPage.getRecords());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据ID查询电子标签控制器基础数据
     * @param etagControllerId 控制器ID
     * @return 响应
     */
    @Override
    public List<AppEtagControllerInfo> queryEtagControllerInfoById(Integer etagControllerId) {
        if(etagControllerId == null) {
            return null;
        }
        LambdaQueryWrapper<AppEtagControllerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppEtagControllerInfo::getControllerId, etagControllerId);
        queryWrapper.orderByAsc(AppEtagControllerInfo::getControllerId);
        try {
            return baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据状态查询电子标签控制器基础数据
     * @param status 状态
     * @return 响应
     */
    @Override
    public List<AppEtagControllerInfo> queryEtagControllerInfoByStatus(Integer status) {
        try {
            LambdaQueryWrapper<AppEtagControllerInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppEtagControllerInfo::getControllerStatus, status);
            queryWrapper.orderByAsc(AppEtagControllerInfo::getControllerId);
            return baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 添加电子标签控制器基础数据
     * @param appEtagControllerInfo 添加参数
     * @return 响应
     */
    @Override
    public int insertEtagControllerInfo(AppEtagControllerInfo appEtagControllerInfo) {
        if(appEtagControllerInfo == null) {
            return 0;
        }
        appEtagControllerInfo.setCreateTime(LocalDateTime.now());
        appEtagControllerInfo.setUpdateTime(LocalDateTime.now());
        try {
            return baseMapper.insert(appEtagControllerInfo);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 修改电子标签控制器基础数据
     * @param appEtagControllerInfo 修改参数
     * @return 响应
     */
    @Override
    public int updateEtagControllerInfo(AppEtagControllerInfo appEtagControllerInfo) {
        LambdaUpdateWrapper<AppEtagControllerInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppEtagControllerInfo::getControllerId, appEtagControllerInfo.getControllerId());
        appEtagControllerInfo.setUpdateTime(LocalDateTime.now());
        try {
            return baseMapper.update(appEtagControllerInfo, updateWrapper);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 删除电子标签控制器基础数据
     * @param etagControllerId 删除参数
     * @return 响应
     */
    @Override
    public int deleteEtagControllerInfoById(Integer etagControllerId) {
        LambdaQueryWrapper<AppEtagControllerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppEtagControllerInfo::getControllerId, etagControllerId);
        try {
            return baseMapper.delete(queryWrapper);
        } catch (Exception e) {
            return 0;
        }
    }
}
