package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.constant.BusinessServiceEnum;
import com.ruoyi.business.domain.ServiceBase;
import com.ruoyi.business.domain.ServiceBusiness;
import com.ruoyi.business.domain.ServiceBusinessVo;
import com.ruoyi.business.domain.vo.ServiceBaseVo;
import com.ruoyi.business.mapper.ServiceBaseMapper;
import com.ruoyi.business.mapper.ServiceBusinessMapper;
import com.ruoyi.business.service.IServiceBusinessService;
import com.ruoyi.common.core.domain.TreeSelectCommon;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

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

/**
 * 对外业务服务Service业务层处理
 * 
 * @author wzq
 * @date 2024-01-10
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ServiceBusinessServiceImpl extends ServiceImpl<ServiceBusinessMapper, ServiceBusiness> implements IServiceBusinessService {

    private final ServiceBusinessMapper serviceBusinessMapper;

    @Autowired
    private ServiceBaseMapper serviceBaseMapper;

    /**
     * 查询对外业务服务列表
     * 
     * @param serviceBusiness 对外业务服务
     * @return 对外业务服务
     */
    @Override
    public List<ServiceBusiness> selectServiceBusinessList(ServiceBusiness serviceBusiness)
    {
        LambdaQueryWrapper<ServiceBusiness> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.apply(org.apache.commons.lang3.StringUtils.isNotEmpty(serviceBusiness.getType()), "FIND_IN_SET('" + serviceBusiness.getType() + "', type)");
        queryWrapper
                .eq(ServiceBusiness::getServiceId, serviceBusiness.getServiceId())
//                .eq(StringUtils.isNotEmpty(serviceBusiness.getType()),ServiceBusiness::getType,serviceBusiness.getType())
                .eq(StringUtils.isNotEmpty(serviceBusiness.getHasAudit()),ServiceBusiness::getHasAudit,serviceBusiness.getHasAudit())
                .like(StringUtils.isNotEmpty(serviceBusiness.getOutName()),ServiceBusiness::getOutName,serviceBusiness.getOutName())
                .like(StringUtils.isNotEmpty(serviceBusiness.getDatabaseName()),ServiceBusiness::getDatabaseName,serviceBusiness.getDatabaseName())
                .like(StringUtils.isNotEmpty(serviceBusiness.getTableRemark()),ServiceBusiness::getTableRemark,serviceBusiness.getTableRemark())
                .orderByDesc(ServiceBusiness::getCreateTime);
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询对外业务服务详情
     *
     * @param outName 对外业务服务主键
     * @return 对外业务服务
     */
    @Override
    public ServiceBusiness selectServiceBusinessByOutName(String outName)
    {
        LambdaQueryWrapper<ServiceBusiness> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(ServiceBusiness::getOutName,outName);
        List<ServiceBusiness> serviceBusinessList =  this.baseMapper.selectList(queryWrapper);
        if (serviceBusinessList.size() == 0){
            return null;
        }
        return serviceBusinessList.get(0);
    }


    /**
     * 查询对外业务服务详情
     *
     * @param id 对外业务服务主键
     * @return 对外业务服务
     */
    @Override
    public ServiceBusiness selectServiceBusinessById(Long id)
    {
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增对外业务服务
     * 
     * @param serviceBusiness 对外业务服务
     * @return 结果
     */
    @Override
    public int insertServiceBusiness(ServiceBusiness serviceBusiness)
    {
        serviceBusiness
                .setPublishStatus(BusinessServiceEnum.unpublished.getCode())
                .setEffectiveStatus(BusinessServiceEnum.not_effective.getCode())
                .setDeleteFlag(DELETE_NO)
                .setCreateTime(DateUtils.getNowDate());
        return this.baseMapper.insert(serviceBusiness);
    }

    /**
     * 修改对外业务服务
     * 
     * @param serviceBusiness 对外业务服务
     * @return 结果
     */
    @Override
    public Boolean updateServiceBusiness(ServiceBusiness serviceBusiness)
    {
        serviceBusiness.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(serviceBusiness);
    }

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

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

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

    /**
     * 查询对外业务服务树结构信息
     *
     * @param serviceBusiness 对外业务服务信息
     * @return 部门树信息集合
     */
    @Override
    public List<TreeSelectCommon> selectGrantTreeList(ServiceBusiness serviceBusiness)
    {
        List<ServiceBase> bases = serviceBaseMapper.selectList(new LambdaQueryWrapper<ServiceBase>()
                .eq(ObjectUtils.isNotEmpty(serviceBusiness.getBelongSystem()), ServiceBase::getBelongSystem, serviceBusiness.getBelongSystem())
                .orderByAsc(ServiceBase::getId));
        List<ServiceBusiness> businesses = this.baseMapper.selectList(new LambdaQueryWrapper<ServiceBusiness>()
                .eq(ObjectUtils.isNotEmpty(serviceBusiness.getBelongSystem()), ServiceBusiness::getBelongSystem, serviceBusiness.getBelongSystem())
                .orderByAsc(ServiceBusiness::getId));
        for (ServiceBusiness business : businesses){
            ServiceBase base = new ServiceBase();
            base.setId(business.getId());
            base.setParentId(business.getServiceId());
            base.setName(business.getOutName());
            base.setDirOrContent(business.getDirOrContent());
            bases.add(base);
        }
        return buildTreeSelect(bases);
    }

    @Override
    public List<ServiceBusiness> getServiceBusinessByIds(List<Long> list) {
        //查询id在list中的数据
        LambdaQueryWrapper<ServiceBusiness> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ServiceBusiness::getId, list);
        return this.baseMapper.selectList(queryWrapper);
    }


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