package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.ServiceBase;
import com.ruoyi.business.domain.vo.ServiceBaseVo;
import com.ruoyi.business.mapper.ServiceBaseMapper;
import com.ruoyi.business.service.IServiceBaseService;
import com.ruoyi.common.core.domain.TreeSelectCommon;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.TreeUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.ruoyi.common.core.constant.Constants.DELETE_YES;

/**
 * 对外服务基础信息Service业务层处理
 * 
 * @author gx
 * @date 2024-02-04
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ServiceBaseServiceImpl extends ServiceImpl<ServiceBaseMapper, ServiceBase> implements IServiceBaseService {

    private final ServiceBaseMapper serviceBaseMapper;

    /**
     * 查询对外服务基础信息列表
     * 
     * @param serviceBase 对外服务基础信息
     * @return 对外服务基础信息
     */
    @Override
    public List<ServiceBase> selectServiceBaseList(ServiceBase serviceBase)
    {
        LambdaQueryWrapper<ServiceBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(serviceBase.getName()), ServiceBase::getName, serviceBase.getName());
        queryWrapper.like(ObjectUtils.isNotEmpty(serviceBase.getParentId()), ServiceBase::getParentId, serviceBase.getParentId());
        queryWrapper.eq(ObjectUtils.isNotEmpty(serviceBase.getBelongSystem()), ServiceBase::getBelongSystem, serviceBase.getBelongSystem());
//        queryWrapper.orderByDesc(ServiceBase::getCreateTime);
        queryWrapper.orderByAsc(ServiceBase::getOrderNum);
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询对外服务基础信息详情
     *
     * @param id 对外服务基础信息主键
     * @return 对外服务基础信息
     */
    @Override
    public ServiceBase selectServiceBaseById(Long id)
    {
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增对外服务基础信息
     * 
     * @param serviceBase 对外服务基础信息
     * @return 结果
     */
    @Override
    public int insertServiceBase(ServiceBase serviceBase)
    {
        serviceBase.setCreateTime(DateUtils.getNowDate());
        return this.baseMapper.insert(serviceBase);
    }

    /**
     * 修改对外服务基础信息
     * 
     * @param serviceBase 对外服务基础信息
     * @return 结果
     */
    @Override
    public Boolean updateServiceBase(ServiceBase serviceBase)
    {
        serviceBase.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(serviceBase);
    }

    /**
     * 批量删除对外服务基础信息
     * 
     * @param ids 需要删除的对外服务基础信息主键
     * @return 结果
     */
    @Override
    public int deleteServiceBaseByIds(Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        return updateByIds(list);
    }

    /**
     * 删除对外服务基础信息信息
     * 
     * @param id 对外服务基础信息主键
     * @return 结果
     */
    @Override
    public int deleteServiceBaseById(Long id)
    {
        List<Long> list = new ArrayList<>();
        list.add(id);
        return updateByIds(list);
    }

    /**
    * 批量更新
    * @param list ids
    */
    private int updateByIds(List<Long> list) {
        LambdaUpdateWrapper<ServiceBase> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ServiceBase::getDeleteBy, SecurityUtils.getUsername())
                .set(ServiceBase::getDeleteTime, DateUtils.getNowDate())
                .in(ServiceBase::getId, list)
                .set(ServiceBase::getDeleteFlag, DELETE_YES);
        return this.baseMapper.updateByIds(updateWrapper);
    }
    
    @Override
    public List<TreeSelectCommon> selecServiceBaseTreeList(ServiceBase serviceBase) {
        List<ServiceBase> list = this.selectServiceBaseList(serviceBase);
        return buildTreeSelect(list);
    }

    private List<TreeSelectCommon> buildTreeSelect(List<ServiceBase> list)
    {
        List<ServiceBase> treeList = TreeUtils.buildTree(list);
        return treeList.stream().map(ServiceBaseVo::new).collect(Collectors.toList());
    }
}
