package com.codvision.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.base.entity.SysTag;
import com.codvision.base.entity.vo.SysTagVO;
import com.codvision.base.mapper.SysTagMapper;
import com.codvision.base.service.ISysTagService;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import com.codvision.webcore.util.PageUtil;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 系统标签 服务实现类
 * </p>
 *
 * @author lingee
 * @since 2023-11-17
 */
@Service
public class SysTagServiceImpl extends BaseServiceImpl<SysTagMapper, SysTag, SysTagVO> implements ISysTagService {

    @Override
    public SysTagVO getDetail(Integer id) {
        SysTagVO vo = new SysTagVO();
        SysTag po = this.getById(id);
        if (null != po) {
            BeanUtil.copyProperties(po, vo);
        }
        return vo;
    }

    @Override
    public IPage<SysTagVO> getPageResult(QueryPage<SysTag> queryPage) {
        QueryWrapper<SysTag> queryWrapper = queryPage.getWrapper();
        queryWrapper.orderByDesc("id");
        IPage<SysTagVO> pageResult = this.getBasePage(queryPage.getPage(), queryWrapper);
        return pageResult;
    }

    @Override
    public List<SysTagVO> listAll(SysTag queryBean) {
        QueryWrapper<SysTag> wrapper = PageUtil.initWrapper(queryBean);
        List<SysTag> list = this.list(wrapper);
        return BeanUtil.copyToList(list, SysTagVO.class);
    }

    @Override
    public boolean save(SysTag sysTag) {
        SysTag isExist = this.getByTagName(sysTag.getTagName());
        Preconditions.checkArgument(isExist == null, "标签名称已存在！");

        //填充新增标签的等级
        if (sysTag.getParentId() == null || sysTag.getParentId() == 0) {
            sysTag.setLevel((short) 1);
        } else {
            SysTag parentTagInfo = this.getById(sysTag.getParentId());
            Preconditions.checkArgument(parentTagInfo != null, "该标签所属父级标签不存在！");
            sysTag.setLevel((short) (parentTagInfo.getLevel() + 1));
        }

        if (sysTag.getSort() == null) {
            sysTag.setSort(0);
        }
        getBaseMapper().insert(sysTag);
        return true;
    }

    @Override
    public boolean updateById(SysTag sysTag) {
        Assert.notNull(sysTag, "系统标签不能为空");
        Assert.notNull(sysTag.getId(), "系统标签id不能为空");
        SysTag isExist = this.getByTagName(sysTag.getTagName());
        Preconditions.checkArgument(isExist == null || isExist.getId().equals(sysTag.getId()), "标签名称已存在！");
        getBaseMapper().updateById(sysTag);
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        SysTag sysTag = getById(id);
        Assert.notNull(sysTag, "系统标签不存在");
        getBaseMapper().deleteById(id);
        return true;
    }


    /**
     * 根据标签名称获取系统标签
     *
     * @param tagName 标签名称
     * @return 系统标签
     */
    private SysTag getByTagName(String tagName) {
        return this.getOne(Wrappers.<SysTag>lambdaQuery().eq(SysTag::getTagName, tagName).last("limit 1"));
    }
}
