package com.tuoheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.common.utils.StringUtils;
import com.tuoheng.common.common.BaseQuery;
import com.tuoheng.common.common.BaseServiceImpl;
import com.tuoheng.system.entity.Dict;
import com.tuoheng.system.entity.DictData;
import com.tuoheng.system.mapper.DictDataMapper;
import com.tuoheng.system.mapper.DictMapper;
import com.tuoheng.system.query.DictDataQuery;
import com.tuoheng.system.query.DictQuery;
import com.tuoheng.system.service.IDictDataService;
import com.tuoheng.system.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 字典项管理表 服务实现类
 * </p>
 *
 * @author 拓恒
 * @since 2020-11-01
 */
@Service
public class DictDataServiceImpl extends BaseServiceImpl<DictDataMapper, DictData> implements IDictDataService {

    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private ITenantService tenantService;
    /**
     * 获取字典项列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(BaseQuery query) {
        DictDataQuery dictQuery = (DictDataQuery) query;
        // 查询条件
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        // 字典项父级id
        if (!StringUtils.isEmpty(dictQuery.getPCode())) {
            // 查询条件
            QueryWrapper<Dict> queryDict = new QueryWrapper<>();
            // 字典名称
            queryDict.eq("code", dictQuery.getPCode());
            queryDict.eq("mark", 1);
            queryDict.eq("tenant_id", tenantService.getCurrentTenantId());
            Dict dict = dictMapper.selectOne(queryDict);
            if(dict!=null) {
                queryWrapper.eq("dict_id", dict.getId());
            }
        }
        // 字典ID
        if (StringUtils.isNotNull(dictQuery.getDictId())) {
            queryWrapper.eq("dict_id", dictQuery.getDictId());
        }
        // 字典项名称
        if (!StringUtils.isEmpty(dictQuery.getName())) {
            queryWrapper.like("name", dictQuery.getName());
        }
        // 字典项编码
        if (!StringUtils.isEmpty(dictQuery.getCode())) {
            queryWrapper.like("code", dictQuery.getCode());
        }
        if(!StringUtils.isEmpty(dictQuery.getKeywords())){
            queryWrapper.and(wq->{
                wq.like("name",dictQuery.getKeywords()).or().like("code",dictQuery.getKeywords());
            });
        }
        queryWrapper.eq("tenant_id", tenantService.getCurrentTenantId());
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByAsc("sort");


        // 查询分页数据
        IPage<DictData> page = new Page<>(dictQuery.getPage(), dictQuery.getLimit());
        IPage<DictData> pageData = dictDataMapper.selectPage(page, queryWrapper);
        return JsonResult.success(pageData);
    }
}
