/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.base.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.base.entity.SysDict;
import com.codvision.base.entity.SysDictItem;
import com.codvision.base.entity.vo.SysDictItemVO;
import com.codvision.base.enums.DictTypeEnum;
import com.codvision.base.mapper.SysDictItemMapper;
import com.codvision.base.mapper.SysDictMapper;
import com.codvision.base.service.ISysDictItemService;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典项 服务实现类
 *
 * @author lingee
 * @date 2023/11/16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDictItemServiceImpl extends BaseServiceImpl<SysDictItemMapper, SysDictItem, SysDictItemVO>
        implements ISysDictItemService {

    @Resource
    private SysDictMapper sysDictMapper;

    @Override
    public List<SysDictItem> getItemList(SysDictItem queryBean) {
        LambdaQueryWrapper<SysDictItem> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(queryBean.getDictId()), SysDictItem::getDictId, queryBean.getDictType())
                .like(StrUtil.isNotBlank(queryBean.getLabel()), SysDictItem::getLabel, queryBean.getLabel())
                .eq(StrUtil.isNotBlank(queryBean.getItemValue()), SysDictItem::getItemValue, queryBean.getItemValue())
                .eq(ObjectUtil.isNotEmpty(queryBean.getDictType()), SysDictItem::getDictType, queryBean.getDictType());
        wrapper.orderByAsc(SysDictItem::getSortOrder);
        return this.list(wrapper);
    }

    @Override
    public IPage<SysDictItemVO> getPageResult(QueryPage<SysDictItem> queryPage) {
        SysDictItem queryBean = queryPage.getCondition();
        LambdaQueryWrapper<SysDictItem> queryWrapper = Wrappers.lambdaQuery();
        if (ObjectUtil.isNotEmpty(queryBean)) {
            Integer dictId = queryBean.getDictId();
            String label = queryBean.getLabel();
            String itemValue = queryBean.getItemValue();
            String dictType = queryBean.getDictType();
            queryWrapper.eq(ObjectUtil.isNotEmpty(dictId), SysDictItem::getDictId, dictId)
                    .like(StrUtil.isNotBlank(label), SysDictItem::getLabel, label)
                    .eq(StrUtil.isNotBlank(itemValue), SysDictItem::getItemValue, itemValue)
                    .eq(ObjectUtil.isNotEmpty(dictType), SysDictItem::getDictType, dictType);
        }
        queryWrapper.orderByAsc(SysDictItem::getSortOrder);
        return getBasePage(queryPage.getPage(), queryWrapper);
    }

    @Override
    public SysDictItem addDictItem(SysDictItem sysDictItem) {
        SysDict sysDict = sysDictMapper.selectById(sysDictItem.getDictId());
        Preconditions.checkArgument(sysDict != null, 30001);

        SysDictItem dictItem = this.getOne(Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getDictId, sysDict.getId())
                .eq(SysDictItem::getItemValue, sysDictItem.getItemValue()));
        Preconditions.checkArgument(dictItem == null, "数据值已存在！");

        sysDictItem.setDictType(sysDict.getDictType());
        this.save(sysDictItem);
        return sysDictItem;
    }

    @Override
    public SysDictItem updateDictItem(SysDictItem sysDictItem) {
        checkDictItem(sysDictItem.getId());

        this.updateById(sysDictItem);
        return this.getById(sysDictItem.getId());
    }

    @Override
    public void removeDictItem(Integer id) {
        checkDictItem(id);
        this.update(Wrappers.<SysDictItem>lambdaUpdate().setSql("del_flag = 1, del_key = id").eq(SysDictItem::getId, id));
    }


    private void checkDictItem(Integer itemId) {
        SysDictItem dictItem = this.getById(itemId);
        Preconditions.checkArgument(dictItem != null, 30001);

        SysDict sysDict = sysDictMapper.selectById(dictItem.getDictId());
        if (sysDict != null) {
            Preconditions.checkArgument(!DictTypeEnum.SYSTEM.getType().equals(sysDict.getSystemFlag()), "系统类字典禁止操作！");
        }
    }
}
