package com.kym.service.miniapp.impl;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.github.pagehelper.PageHelper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.kym.common.IQuery;
import com.kym.common.utils.CommUtil;
import com.kym.entity.common.PageBean;
import com.kym.entity.miniapp.DataDict;
import com.kym.mapper.miniapp.DataDictMapper;
import com.kym.service.miniapp.DataDictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author zuy
 * @since 2023-08-13
 */
@Service
@DS("db-miniapp")
public class DataDictServiceImpl extends MPJBaseServiceImpl<DataDictMapper, DataDict> implements DataDictService {


    @Override
    public Object list(IQuery<DataDict> query) {
        QueryChainWrapper<DataDict> wrapper = getWrapper(query.query, DataDict.class);
//        PageHelper.startPage(query.pageNum, query.pageSize);
        List<DataDict> list = wrapper.list();
        return new PageBean<>(list);
    }


    @Override
    public Object listV2(IQuery<DataDict> query) {
        QueryChainWrapper<DataDict> wrapper = getWrapper(query.query, DataDict.class);
        wrapper.select("distinct(code)");
        Long count = wrapper.count();
        PageHelper.startPage(query.pageNum, query.pageSize);
        List<DataDict> list = wrapper.list();
        if (CommUtil.isNotEmptyAndNull(list)) {

            List<DataDict> list1 = lambdaQuery().in(CommUtil.isNotEmptyAndNull(list), DataDict::getCode, list.stream().map(DataDict::getCode).collect(Collectors.toList())).list();
            return IQuery.newPageBean(count, list1.stream().filter(CommUtil.distinctByKey(DataDict::getCode)).toList());
        } else {
            return IQuery.newPageBean(0, Collections.emptyList());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(List<DataDict> dictList) {
        saveOrUpdateBatch(dictList);
    }


    public <T> QueryChainWrapper<T> getWrapper(Object query, Class<T> clz) {
        QueryChainWrapper<T> wrapper = new QueryChainWrapper<>(clz);
        if (null == query) {
            return wrapper;
        }


        Field[] fields = ClassUtil.getDeclaredFields(query.getClass());
        if (!CommUtil.isEmptyOrNull(fields)) {
            for (Field field : fields) {
                int modifiers = field.getModifiers();
                if (Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers)) {
                    continue;
                }
                Object fieldValue = ReflectUtil.getFieldValue(query, field);
                if (!CommUtil.isEmptyOrNull(fieldValue)) {
                    Class<?> aClass = field.getType();

                    if (ClassUtil.isBasicType(aClass)) {
                        wrapper.eq(CommUtil.toDbColumn(field.getName()), fieldValue);
                    } else if (String.class.isAssignableFrom(aClass)) {
                        wrapper.like(CommUtil.toDbColumn(field.getName()), fieldValue);
                    }
                }
            }
        }

        return wrapper;
    }
}
