package com.friday.custody.service.basicService.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.friday.custody.dao.DataDictionaryDirectoryDao;
import com.friday.custody.entity.DataDictionaryDirectoryEntity;
import com.friday.custody.enums.RetCodeEnum;
import com.friday.custody.req.QueryDictionaryDirectoryReq;
import com.friday.custody.service.basicService.DataDictionaryDirectoryService;
import com.friday.custody.utils.PageUtils;
import com.friday.custody.utils.Query;
import com.friday.custody.utils.R;
import com.friday.custody.vo.DataDictionaryDirectoryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("dataDictionaryDirectoryService")
@Slf4j
public class DataDictionaryDirectoryServiceImpl extends ServiceImpl<DataDictionaryDirectoryDao, DataDictionaryDirectoryEntity> implements DataDictionaryDirectoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DataDictionaryDirectoryEntity> page = this.page(
                new Query<DataDictionaryDirectoryEntity>().getPage(params),
                new QueryWrapper<DataDictionaryDirectoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R queryAll(String params) {
        try {
            QueryDictionaryDirectoryReq queryDictionaryDirectoryReq = JSON.parseObject(params, QueryDictionaryDirectoryReq.class);
            QueryWrapper<DataDictionaryDirectoryEntity> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(queryDictionaryDirectoryReq.getFieldName())){
                queryWrapper.like("dic_description",queryDictionaryDirectoryReq.getFieldName());
            }
            List<DataDictionaryDirectoryEntity> entities = baseMapper.selectList(queryWrapper);
            List<DataDictionaryDirectoryVo> vos =entities.stream().map(entity-> DataDictionaryDirectoryVo.builder()
                    .type(entity.getDicType())
                    .fieldName(entity.getDicDescription())
                    .build()).collect(Collectors.toList());
            return R.ok("查询字典目录成功！").put("data", vos);
        } catch (Exception e) {
            log.error("查询字典目录失败", e);
            return R.error(RetCodeEnum.SYS_ERROR.getCode(), "查询字典目录失败！");
        }
    }

    @Override
    public String queryByType(String type) {
        List<DataDictionaryDirectoryEntity> entities = baseMapper.selectList(new QueryWrapper<DataDictionaryDirectoryEntity>().eq("dic_type", type));
        if (!CollectionUtils.isEmpty(entities)) {
            return entities.get(0).getServiceName();
        }
        return null;
    }
}