package com.dsk.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsk.common.constant.TenantConstants;
import com.dsk.common.core.domain.PageQuery;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.exception.ServiceException;
import com.dsk.common.utils.StringUtils;
import com.dsk.system.domain.SysTenant;
import com.dsk.system.domain.SysTenantPackage;
import com.dsk.system.domain.bo.SysTenantPackageBo;
import com.dsk.system.domain.vo.SysTenantPackageSelectVo;
import com.dsk.system.domain.vo.SysTenantPackageVo;
import com.dsk.system.mapper.SysTenantMapper;
import com.dsk.system.mapper.SysTenantPackageMapper;
import com.dsk.system.service.ISysTenantPackageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author sxk
 * @date 2023.08.24
 */
@RequiredArgsConstructor
@Service
public class ISysTenantPackageServiceImpl implements ISysTenantPackageService {

    @Resource
    private final SysTenantPackageMapper baseMapper;
    @Resource
    private final SysTenantMapper tenantMapper;

    /**
     * 查询租户套餐列表
     */
    @Override
    public TableDataInfo<SysTenantPackageVo> queryPageList(SysTenantPackageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysTenantPackage> lqw = buildQueryWrapper(bo);
        pageQuery.setIsAsc("desc");
        pageQuery.setOrderByColumn("createTime");
        Page<SysTenantPackageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询租户套餐已启用列表
     */
    @Override
    public List<SysTenantPackageSelectVo> selectList() {
        List<SysTenantPackageVo> tenantPackageVo = baseMapper.selectVoList(new LambdaQueryWrapper<SysTenantPackage>()
                .eq(SysTenantPackage::getStatus, TenantConstants.NORMAL));
        return BeanUtil.copyToList(tenantPackageVo, SysTenantPackageSelectVo.class);
    }

//    /**
//     * 查询租户套餐列表
//     */
//    @Override
//    public List<SysTenantPackageVo> queryList(SysTenantPackageBo bo) {
//        LambdaQueryWrapper<SysTenantPackage> lqw = buildQueryWrapper(bo);
//        return baseMapper.selectVoList(lqw);
//    }

    /**
     * 查询租户套餐
     */
    @Override
    public SysTenantPackageVo queryById(Long packageId) {
        return baseMapper.selectVoById(packageId);
    }

    private LambdaQueryWrapper<SysTenantPackage> buildQueryWrapper(SysTenantPackageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysTenantPackage> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPackageName()), SysTenantPackage::getPackageName, bo.getPackageName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysTenantPackage::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增租户套餐
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(SysTenantPackageBo bo) {
        SysTenantPackage add = BeanUtil.toBean(bo, SysTenantPackage.class);
        // 保存菜单id
        List<Long> menuIds = Arrays.asList(bo.getMenuIds());
        if (CollUtil.isNotEmpty(menuIds)) {
            add.setMenuIds(StringUtils.join(menuIds, ","));
        } else {
            add.setMenuIds("");
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPackageId(add.getPackageId());
        }
        return flag;
    }

    /**
     * 修改租户套餐
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(SysTenantPackageBo bo) {
        SysTenantPackage update = BeanUtil.toBean(bo, SysTenantPackage.class);
        // 保存菜单id
        List<Long> menuIds = Arrays.asList(bo.getMenuIds());
        if (CollUtil.isNotEmpty(menuIds)) {
            update.setMenuIds(StringUtils.join(menuIds, ", "));
        } else {
            update.setMenuIds("");
        }
        return baseMapper.updateById(update) > 0;
    }

//    /**
//     * 修改套餐状态
//     */
//    @Override
//    public int updatePackageStatus(SysTenantPackageBo bo) {
//        SysTenantPackage tenantPackage = BeanUtil.toBean(bo, SysTenantPackage.class);
//        return baseMapper.updateById(tenantPackage);
//    }

    /**
     * 批量删除租户套餐
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(List<Long> ids, boolean isValid) {
        if (isValid) {
            boolean exists = tenantMapper.exists(new LambdaQueryWrapper<SysTenant>().in(SysTenant::getPackageId, ids));
            if (exists) {
                throw new ServiceException("租户套餐已被使用");
            }
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
