package com.njzscloud.dispose.sys.dict.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
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.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.dispose.sys.dict.mapper.DictMapper;
import com.njzscloud.dispose.sys.dict.pojo.entity.DictEntity;
import com.njzscloud.dispose.sys.dict.pojo.entity.DictItemEntity;
import com.njzscloud.dispose.sys.dict.pojo.result.ObtainDictDataResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 字典表
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DictService extends ServiceImpl<DictMapper, DictEntity> implements IService<DictEntity> {
    private final DictItemService dictItemService;

    /**
     * 新增
     */
    public void add(DictEntity dictEntity) {
        String dictKey = dictEntity.getDictKey();
        boolean exists = this.exists(Wrappers.<DictEntity>lambdaQuery().eq(DictEntity::getDictKey, dictKey));
        Assert.isFalse(exists, () -> Exceptions.clierr("字典标识重复：{}", dictKey));
        this.save(dictEntity);
    }

    /**
     * 修改
     */
    public void modify(DictEntity dictEntity) {
        this.updateById(dictEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public List<String> del(List<Long> ids) {
        List<DictEntity> dictEntities = listByIds(ids);
        List<String> list = dictEntities.stream().map(DictEntity::getDictKey).toList();
        this.removeBatchByIds(ids);
        dictItemService.remove(Wrappers.<DictItemEntity>lambdaQuery().in(DictItemEntity::getDictId, ids));
        return list;
    }

    /**
     * 详情
     */
    public DictEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<DictEntity> paging(PageParam pageParam, DictEntity dictEntity) {
        String dictName = dictEntity.getDictName();
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<DictEntity>lambdaQuery()
            .and(StrUtil.isNotBlank(dictName), it ->
                it.like(DictEntity::getDictKey, dictName)
                    .or().like(DictEntity::getDictName, dictName)
                    .or().like(DictEntity::getMemo, dictName)
            )
        ));
    }

    public List<ObtainDictDataResult> obtainDictData(String dictKey, String keywords) {
        return dictItemService.list(Wrappers.<DictItemEntity>lambdaQuery().eq(DictItemEntity::getDictKey, dictKey)
                .and(StrUtil.isNotBlank(keywords), it ->
                    it.like(DictItemEntity::getTxt, keywords)
                        .or().like(DictItemEntity::getVal, keywords)
                        .or().like(DictItemEntity::getMemo, keywords)
                )
                .orderByAsc(DictItemEntity::getSort, DictItemEntity::getVal, DictItemEntity::getId)
            )
            .stream().map(it -> BeanUtil.copyProperties(it, ObtainDictDataResult.class))
            .collect(Collectors.toList());
    }
}
