/*
 * Copyright (c) 2018-2028, AoKunSang All rights reserved.
 * 我问青山何时老，青山问我几时闲，
 * 不是闲人闲不得，能闲必非等闲人。
 */
package org.springblade.core.mp.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.mp.properties.MybatisPlusProperties;
import org.springblade.core.mp.util.BizIdInfo;
import org.springblade.core.mp.util.TableBizIdUtil;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Collection;

/**
 * APP应用[mybatis-plus]业务实现类
 *
 * @author wookvn
 * @datetime 2024-12-24 14:43:10
 */
public class BaseAppServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {

    /**
     * 租户配置文件
     */
    @Autowired
    private MybatisPlusProperties mybatisPlusProperties;

    /**
     * <p>根据appId和bizId查询唯一记录</p>
     * @author wookvn
     * @datetime 2024-12-24 14:46:19
     * @param bizId: 业务ID值
     * @return T
     **/
    public T getByBizId(Serializable bizId) {
        BizIdInfo bizIdInfo = TableBizIdUtil.getTableBizIdInfo(entityClass);
        QueryWrapper<T> queryWrapper = Wrappers.<T>query().eq(mybatisPlusProperties.getApp().getAppIdColumn(), AuthUtil.getAppId())
                .eq(bizIdInfo.getColumnName(), bizId);
        return getOne(queryWrapper);
    }

    /**
     * <p>根据appId和bizId删除记录</p>
     * @author wookvn
     * @datetime 2024-12-24 16:22:01
     * @param bizId: 业务ID值
     * @return boolean
     **/
    public boolean removeByBizId(Serializable bizId) {
        BizIdInfo bizIdInfo = TableBizIdUtil.getTableBizIdInfo(entityClass);
        QueryWrapper<T> queryWrapper = Wrappers.<T>query().eq(mybatisPlusProperties.getApp().getAppIdColumn(), AuthUtil.getAppId())
                .eq(bizIdInfo.getColumnName(), bizId);
        return remove(queryWrapper);
    }

    /**
     * <p>根据appId和bizId删除多条记录</p>
     * @author wookvn
     * @datetime 2024-12-24 16:23:37
     * @param list: 多个ID值列表
     * @return boolean
     **/
    public boolean removeByBizIds(Collection<?> list) {
        BizIdInfo bizIdInfo = TableBizIdUtil.getTableBizIdInfo(entityClass);
        QueryWrapper<T> queryWrapper = Wrappers.<T>query().eq(mybatisPlusProperties.getApp().getAppIdColumn(), AuthUtil.getAppId())
                .in(bizIdInfo.getColumnName(), list);
        return remove(queryWrapper);
    }

    /**
     * <p>根据appId和bizId更新记录</p>
     * @author wookvn
     * @datetime 2024-12-24 15:33:02
     * @param entity: 对象
     * @return boolean
     **/
    public boolean updateByBizId(T entity) {
        BizIdInfo bizIdInfo = TableBizIdUtil.getTableBizIdInfo(entityClass);
        QueryWrapper<T> updateWrapper = Wrappers.<T>query().eq(mybatisPlusProperties.getApp().getAppIdColumn(), AuthUtil.getAppId())
                .eq(bizIdInfo.getColumnName(), bizIdInfo.getBizId(entity));
        return update(entity, updateWrapper);
    }
}
