package com.wx.application.base;

import cn.hutool.core.map.MapUtil;
import com.wx.application.constant.CONSTANT;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import static com.baomidou.mybatisplus.extension.toolkit.SqlHelper.retBool;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class BaseSearchService<T extends BaseEntity<?>, M extends BaseMapper<T>>  {
    @Autowired
    protected M simpleMapper;

    
	protected DynamicSpecifications<T> dynamicSpecifications = new DynamicSpecifications();

    /**
     * 根据id查询一条
     * @param id
     * @return
     */
    public T queryOne(Serializable id){
        return (T) simpleMapper.selectById(id);
    }
    /**
     * 读取一个对象
     *
     * @return
     */
    @Transactional(readOnly = true)
    public T queryUnique(Map map) {
        AbstractWrapper wrapper = dynamicSpecifications.parseSearchParams(map);
        wrapper = (AbstractWrapper) wrapper.last("limit 1");
        return (T) simpleMapper.selectOne(wrapper);
    }

    /**
     * 读取一个对象
     *
     * @return
     */
    @Transactional(readOnly = true)
    public T queryUnique(Map map,Map other) {
        AbstractWrapper wrapper = dynamicSpecifications.parseSearchParams(map);
        wrapper = (AbstractWrapper) wrapper.last("limit 1");
        // 循环map
        for(Object key : other.keySet()){
            Object value = other.get(key);
            wrapper.eq(key,value);
        }
        return (T) simpleMapper.selectOne(wrapper);
    }

    /**
     * 查询（根据ID 批量查询）
     *
     * @return
     */
    @Transactional(readOnly = true)
    public List<T> queryBatchIds(List ids) {
        if(ids==null || ids.isEmpty()){
            return null;
        }
        return simpleMapper.selectBatchIds(ids);
    }

    /**
     * 分页查询
     *
     * @return
     */
    @Transactional(readOnly = true)
    public IPage<T> queryPage(Map map) {
        Long pageNum = MapUtil.getLong(map, CONSTANT.pageNumStr);
        Long pageSize = MapUtil.getLong(map, CONSTANT.pageSizeStr);
        if (null == pageNum) {
            pageNum = CONSTANT.pageNum;
        }
        if (null == pageSize) {
            pageSize = CONSTANT.pageSize;
        }
        return simpleMapper.selectPage(new Page(pageNum, pageSize), dynamicSpecifications.parseSearchParams(map));
    }

    /**
     * 分页查询
     *
     * @return
     */
    @Transactional(readOnly = true)
    public IPage<T> queryPage(Map map,Map other) {
        Long pageNum = MapUtil.getLong(map, CONSTANT.pageNumStr);
        Long pageSize = MapUtil.getLong(map, CONSTANT.pageSizeStr);
        if (null == pageNum) {
            pageNum = CONSTANT.pageNum;
        }
        if (null == pageSize) {
            pageSize = CONSTANT.pageSize;
        }
        AbstractWrapper wrapper = dynamicSpecifications.parseSearchParams(map);
        // 循环map
        for(Object key : other.keySet()){
            Object value = other.get(key);
            wrapper.eq(key,value);
        }
        return simpleMapper.selectPage(new Page(pageNum, pageSize), wrapper);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @Transactional(readOnly = true)
    public List<T> queryList(Map map) {
        return simpleMapper.selectList(dynamicSpecifications.parseSearchParams(map));
    }
    /**
     * 查询全部
     *
     * @return
     */
    @Transactional(readOnly = true)
    public List<T> queryList(Map map,Map other) {
        AbstractWrapper wrapper = dynamicSpecifications.parseSearchParams(map);
        // 循环map
        for(Object key : other.keySet()){
            Object value = other.get(key);
            wrapper.eq(key,value);
        }
        return simpleMapper.selectList(wrapper);
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @Transactional
    public synchronized boolean remove(Serializable id) {
        return retBool(simpleMapper.deleteById(id));
    }


    /**
     * 根据column-map删除
     * @param map
     * @return
     */
    @Transactional
    public synchronized boolean remove(Map map){
        AbstractWrapper wrapper = new QueryWrapper();
        // 循环map
        for(Object key : map.keySet()){
            Object value = map.get(key);
            wrapper.eq(key,value);
        }
        return retBool(simpleMapper.delete(wrapper));
    }

    /**
     * 修改数据
     * @param t
     * @return
     */
    @Transactional
    public synchronized boolean modify(T t) {
        if (null == t.getId()) {
            throw new RuntimeException("参数异常");
        }
        return retBool(simpleMapper.updateById(t));
    }

    /**
     * 添加数据
     * @param t
     * @return
     */
    @Transactional
    public synchronized boolean create(T t) {
        if (null != t.getId()) {
            throw new RuntimeException("参数异常");
        }
        return retBool(simpleMapper.insert(t));
    }
}
