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.AppEtagTagInfoMapper;
import org.wcs.mapper.intf.AppEtagTagInfoService;
import org.wcs.model.bo.db.DataBaseActionResult;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.etagBase.EtagTagInfoQueryReq;
import org.wcs.model.po.app.AppEtagTagInfo;

import java.util.List;

/**
 * 电子标签小标签信息表操作类
 */
@Service
public class AppEtagTagInfoDao extends ServiceImpl<AppEtagTagInfoMapper, AppEtagTagInfo> implements AppEtagTagInfoService {

    /**
     * 查询电子标签小标签信息
     * @param request 查询参数
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return 响应
     */
    @Override
    public Tuple2<Long, List<AppEtagTagInfo>> queryEtagTagInfo(EtagTagInfoQueryReq request, Integer pageSize, Integer pageIndex) {
        LambdaQueryWrapper<AppEtagTagInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(request.getControllerId() != null, AppEtagTagInfo::getControllerId, request.getControllerId());
        queryWrapper.eq(request.getTagId() != null, AppEtagTagInfo::getTagId, request.getTagId());
        queryWrapper.eq(request.getTagType() != null, AppEtagTagInfo::getTagType, request.getTagType());
        queryWrapper.eq(request.getBusinessType() != null, AppEtagTagInfo::getBusinessType, request.getBusinessType());
        queryWrapper.like(request.getTagName() != null, AppEtagTagInfo::getTagName, request.getTagName());
        queryWrapper.like(request.getAreaId() != null, AppEtagTagInfo::getAreaId, request.getAreaId());
        queryWrapper.like(request.getTunnelNo() != null, AppEtagTagInfo::getTunnelNo, request.getTunnelNo());
        queryWrapper.orderByAsc(AppEtagTagInfo::getControllerId).orderByAsc(AppEtagTagInfo::getTagId);
        try{
            Page<AppEtagTagInfo> page = new Page<>(pageIndex, pageSize);
            Page<AppEtagTagInfo> appEtagTagInfoPage = baseMapper.selectPage(page, queryWrapper);
            return new Tuple2<>(appEtagTagInfoPage.getTotal(), appEtagTagInfoPage.getRecords());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据标签名称查询电子标签小标签信息
     * @param tagName 标签名称
     * @return 响应
     */
    @Override
    public List<AppEtagTagInfo> queryEtagTagInfoByTagName(String tagName) {
        if(tagName == null) {
            return null;
        }
        LambdaQueryWrapper<AppEtagTagInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppEtagTagInfo::getTagName, tagName);
        queryWrapper.orderByAsc(AppEtagTagInfo::getControllerId);
        try {
            return baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询所有电子标签小标签信息
     * @return 响应
     */
    @Override
    public List<AppEtagTagInfo> queryAll() {
        try {
            return super.list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 忽略null参数查询
     * @param appEtagTagInfo 查询参数
     * @return 响应
     */
    @Override
    public List<AppEtagTagInfo> queryIgnoreNull(AppEtagTagInfo appEtagTagInfo) {
        try {
            LambdaQueryWrapper<AppEtagTagInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(appEtagTagInfo.getTagName() != null, AppEtagTagInfo::getTagName, appEtagTagInfo.getTagName());
            queryWrapper.eq(appEtagTagInfo.getControllerId() != null, AppEtagTagInfo::getControllerId, appEtagTagInfo.getControllerId());
            queryWrapper.eq(appEtagTagInfo.getTagId() != null, AppEtagTagInfo::getTagId, appEtagTagInfo.getTagId());
            queryWrapper.eq(appEtagTagInfo.getTagType() != null, AppEtagTagInfo::getTagType, appEtagTagInfo.getTagType());
            queryWrapper.eq(appEtagTagInfo.getBusinessType() != null, AppEtagTagInfo::getBusinessType, appEtagTagInfo.getBusinessType());
            queryWrapper.eq(appEtagTagInfo.getAreaId() != null, AppEtagTagInfo::getAreaId, appEtagTagInfo.getAreaId());
            queryWrapper.eq(appEtagTagInfo.getTunnelNo() != null, AppEtagTagInfo::getTunnelNo, appEtagTagInfo.getTunnelNo());
            queryWrapper.eq(appEtagTagInfo.getRemark() != null, AppEtagTagInfo::getRemark, appEtagTagInfo.getRemark());
            return baseMapper.selectList(queryWrapper);

        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 添加电子标签小标签信息
     * @param appEtagTagInfo 添加参数
     * @return 响应
     */
    @Override
    public DataBaseActionResult<Integer> insert(AppEtagTagInfo appEtagTagInfo) {
        if(appEtagTagInfo == null) {
            return new DataBaseActionResult<>(null, new Exception("添加参数不能为空"));
        }
        try {
            int insert = baseMapper.insert(appEtagTagInfo);
            return new DataBaseActionResult<>(insert);
        } catch (Exception e) {
            return new DataBaseActionResult<>(null, e);
        }
    }

    /**
     * 修改电子标签小标签信息
     * @param appEtagTagInfo 修改参数
     * @return 响应
     */
    @Override
    public DataBaseActionResult<Integer> update(AppEtagTagInfo appEtagTagInfo) {
        if(appEtagTagInfo == null) {
            return new DataBaseActionResult<>(null, new Exception("修改参数不能为空"));
        }
        LambdaUpdateWrapper<AppEtagTagInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppEtagTagInfo::getTagName, appEtagTagInfo.getTagName());
        updateWrapper.set(AppEtagTagInfo::getControllerId, appEtagTagInfo.getControllerId());
        updateWrapper.set(AppEtagTagInfo::getTagType, appEtagTagInfo.getTagType());
        updateWrapper.set(AppEtagTagInfo::getBusinessType, appEtagTagInfo.getBusinessType());
        updateWrapper.set(AppEtagTagInfo::getTagId, appEtagTagInfo.getTagId());
        updateWrapper.set(AppEtagTagInfo::getAreaId, appEtagTagInfo.getAreaId());
        updateWrapper.set(AppEtagTagInfo::getTunnelNo, appEtagTagInfo.getTunnelNo());
        updateWrapper.set(AppEtagTagInfo::getRemark, appEtagTagInfo.getRemark());
        try {
            int update = baseMapper.update(appEtagTagInfo, updateWrapper);
            return new DataBaseActionResult<>(update);
        } catch (Exception e) {
            return new DataBaseActionResult<>(null, e);
        }
    }

    /**
     * 根据标签名称删除电子标签小标签信息
     * @param tagName 标签名称
     * @return 响应
     */
    @Override
    public DataBaseActionResult<Integer> deleteByTagName(String tagName) {
        if(tagName == null) {
            return new DataBaseActionResult<>(null, new Exception("标签名称不能为空"));
        }
        LambdaQueryWrapper<AppEtagTagInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppEtagTagInfo::getTagName, tagName);
        try {
            int delete = baseMapper.delete(queryWrapper);
            return new DataBaseActionResult<>(delete);
        } catch (Exception e) {
            return new DataBaseActionResult<>(null, e);
        }
    }
}
