package com.njzscloud.supervisory.dict.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.dict.mapper.SysDictMapper;
import com.njzscloud.supervisory.dict.pojo.SysDictEntity;
import com.njzscloud.supervisory.dict.pojo.SysDictItemEntity;
import com.njzscloud.supervisory.dict.pojo.ObtainDictDataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 字典表
 */
@Slf4j
@Service
public class SysDictService extends ServiceImpl<SysDictMapper, SysDictEntity> implements IService<SysDictEntity> {
    @Autowired
    private SysDictItemService sysDictItemService;

    /**
     * 新增
     *
     * @param sysDictEntity 数据
     */

    public void add(SysDictEntity sysDictEntity) {
        this.save(sysDictEntity);
    }

    /**
     * 修改
     *
     * @param sysDictEntity 数据
     */

    public void modify(SysDictEntity sysDictEntity) {
        this.updateById(sysDictEntity);
    }

    /**
     * 删除
     *
     * @param ids Ids
     */

    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return SysDictEntity 结果
     */

    public SysDictEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param sysDictEntity 筛选条件
     * @param pageParam     分页参数
     * @return PageResult&lt;SysDictEntity&gt; 分页结果
     */

    public PageResult<SysDictEntity> paging(PageParam pageParam, SysDictEntity sysDictEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<SysDictEntity>query(sysDictEntity)));
    }


    public List<ObtainDictDataResult> obtainDictData(String dictKey) {
        return sysDictItemService.list(Wrappers.<SysDictItemEntity>lambdaQuery().eq(SysDictItemEntity::getDictKey, dictKey))
                .stream().map(it -> BeanUtil.copyProperties(it, ObtainDictDataResult.class))
                .collect(Collectors.toList());
    }

}
