package com.codvision.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.auth.entity.SysApp;
import com.codvision.auth.entity.SysMenu;
import com.codvision.auth.entity.vo.SysAppVO;
import com.codvision.auth.mapper.SysAppMapper;
import com.codvision.auth.mapper.SysMenuMapper;
import com.codvision.auth.service.ISysAppService;
import com.codvision.commoncore.enums.TrueOrFalse;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

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

/**
 * <p>
 * 系统应用 服务实现类
 * </p>
 *
 * @author lingee
 * @since 2024-04-29
 */
@Service
public class SysAppServiceImpl extends BaseServiceImpl<SysAppMapper, SysApp, SysAppVO> implements ISysAppService {

    @Resource
    private SysMenuMapper sysMenuMapper;

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

    @Override
    public IPage<SysAppVO> getPageResult(QueryPage<SysApp> queryPage) {
        LambdaQueryWrapper<SysApp> queryWrapper = Wrappers.lambdaQuery();
        SysApp queryBean = queryPage.getCondition();
        // 根据queryBean拼接查询条件
        if (null != queryBean) {
            queryWrapper.like(StrUtil.isNotBlank(queryBean.getName()), SysApp::getName, queryBean.getName());
        }
        queryWrapper.orderByDesc(SysApp::getId);
        IPage<SysAppVO> pageResult = this.getBasePage(queryPage.getPage(), queryWrapper);
        return pageResult;
    }

    @Override
    public List<SysAppVO> listAll(SysApp queryBean) {
        LambdaQueryWrapper<SysApp> queryWrapper = Wrappers.lambdaQuery();
        // 根据queryBean拼接查询条件
        if (null != queryBean) {

        }
        List<SysApp> list = this.list(queryWrapper);
        return BeanUtil.copyToList(list, SysAppVO.class);
    }

    @Override
    public boolean save(SysApp sysApp) {
        Assert.notNull(sysApp, "系统应用不能为空");
        getBaseMapper().insert(sysApp);
        return true;
    }

    @Override
    public boolean updateById(SysApp sysApp) {
        Assert.notNull(sysApp, "系统应用不能为空");
        Assert.notNull(sysApp.getId(), "系统应用id不能为空");
        getBaseMapper().updateById(sysApp);
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        SysApp sysApp = getById(id);
        Assert.notNull(sysApp, "系统应用不存在");
        if (sysMenuMapper.selectCount(Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getAppId, id)
                .eq(SysMenu::getDelFlag, TrueOrFalse.FALSE.getIntVal())) > 0) {
            throw new BusinessException("当前应用已关联菜单，无法删除");
        }
        getBaseMapper().deleteById(id);
        return true;
    }
}
