/*
 * 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.SysDictVO;
import com.codvision.base.enums.DictTypeEnum;
import com.codvision.base.mapper.SysDictMapper;
import com.codvision.base.service.ISysDictItemService;
import com.codvision.base.service.ISysDictService;
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;

/**
 * 字典 服务实现类
 *
 * @author lingee
 * @date 2023/11/16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDictServiceImpl extends BaseServiceImpl<SysDictMapper, SysDict, SysDictVO> implements ISysDictService {

    @Resource
    private ISysDictItemService sysDictItemService;

    @Override
    public IPage<SysDictVO> getPageResult(QueryPage<SysDict> queryPage) {
        LambdaQueryWrapper<SysDict> queryWrapper = Wrappers.lambdaQuery();
        SysDict queryBean = queryPage.getCondition();
        if (ObjectUtil.isNotEmpty(queryBean)) {
            String keyword = queryBean.getKeyword();
            if (StrUtil.isNotBlank(keyword)) {
                queryWrapper.nested(i -> i.like(SysDict::getDictType, keyword).or().like(SysDict::getDescription, keyword));
            }
            Integer systemFlag = queryBean.getSystemFlag();
            if (ObjectUtil.isNotEmpty(systemFlag)) {
                queryWrapper.eq(SysDict::getSystemFlag, systemFlag);
            }
        }
        queryWrapper.orderByDesc(SysDict::getSystemFlag, SysDict::getCreateTime).orderByAsc(SysDict::getId);
        IPage<SysDictVO> pageResult = this.getBasePage(queryPage.getPage(), queryWrapper);
        return pageResult;
    }

    @Override
    public void addDict(SysDict sysDict) {
        SysDict isExist = this.getOne(Wrappers.<SysDict>lambdaQuery().eq(SysDict::getDictType, sysDict.getDictType()));
        Preconditions.checkArgument(ObjectUtil.isEmpty(isExist), "类型已存在！");
        getBaseMapper().insert(sysDict);
    }

    @Override
    public void updateDict(SysDict sysDict) {
        SysDict checkDict = checkDict(sysDict.getId());
        if (!checkDict.getDictType().equals(sysDict.getDictType())) {
            this.update(Wrappers.<SysDict>lambdaUpdate().set(SysDict::getDictType, sysDict.getDictType()).eq(SysDict::getId, sysDict.getId()));
        }
        this.updateById(sysDict);
    }

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

    private SysDict checkDict(Integer dictId) {
        SysDict dict = this.getById(dictId);
        Preconditions.checkArgument(dict != null, 30001);
        Preconditions.checkArgument(!DictTypeEnum.SYSTEM.getType().equals(dict.getSystemFlag()), "系统类字典禁止操作！");
        return dict;
    }

}
