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.AppBaseDbMapper;
import org.wcs.mapper.intf.AppBaseDbService;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.dto.serve.siemensDb.QuerySiemensDbReq;
import org.wcs.model.po.app.AppBaseDb;
import org.wcs.utils.AppStringUtils;

import java.util.List;

@Service
public class AppBaseDbDao extends ServiceImpl<AppBaseDbMapper, AppBaseDb> implements AppBaseDbService {

    /**
     * 查询所有数据
     * @return 查询结果
     */
    @Override
    public List<AppBaseDb> queryAll() {
        try {
            return super.list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询数据
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @param request 查询参数
     * @return 查询结果
     */
    @Override
    public Tuple2<Long, List<AppBaseDb>> query(Integer pageIndex, Integer pageSize, QuerySiemensDbReq request) {
        try {
            LambdaQueryWrapper<AppBaseDb> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(request.getDbName() != null, AppBaseDb::getDbName, request.getDbName())
                    .like(request.getDbAddress() != null, AppBaseDb::getDbAddress, request.getDbAddress())
                    .eq(request.getPlcId() != null && request.getPlcId() > 0, AppBaseDb::getPlcId, request.getPlcId());
            wrapper.orderByAsc(AppBaseDb::getPlcId);
            Page<AppBaseDb> page = new Page<>(pageIndex, pageSize);
            Page<AppBaseDb> pageResult = super.page(page, wrapper);
            return new Tuple2<>(pageResult.getTotal(), pageResult.getRecords());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 新增数据
     * @param db 新增参数
     * @return 新增结果
     */
    @Override
    public int insert(AppBaseDb db) {
        try {
            return super.baseMapper.insert(db);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 修改数据
     * @param db 修改参数
     * @return 修改结果
     */
    @Override
    public int update(AppBaseDb db) {
        try {
            LambdaUpdateWrapper<AppBaseDb> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(AppBaseDb::getDbName, db.getDbName());
            return super.update(db, wrapper) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 根据名称查询数据
     * @param dbName 数据库名称
     * @return 查询结果
     */
    @Override
    public List<AppBaseDb> queryByName(String dbName) {
        try{
            LambdaQueryWrapper<AppBaseDb> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AppBaseDb::getDbName, dbName);
            return super.list(wrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据名称删除数据
     * @param dbName 删除参数
     * @return 删除结果
     */
    @Override
    public int deleteByName(String dbName) {
        try{
            LambdaQueryWrapper<AppBaseDb> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AppBaseDb::getDbName, dbName);
            return super.remove(wrapper) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }
}
