package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaApplicationBo;
import com.ruoyi.oa.domain.vo.OaApplicationVo;
import com.ruoyi.oa.domain.OaApplication;
import com.ruoyi.oa.mapper.OaApplicationMapper;
import com.ruoyi.oa.service.IOaApplicationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 应用集成Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-14
 */
@RequiredArgsConstructor
@Service
public class OaApplicationServiceImpl implements IOaApplicationService {

    private final OaApplicationMapper baseMapper;

    /**
     * 查询应用集成
     */
    @Override
    public OaApplicationVo queryById(Long applicationId){
        return baseMapper.selectVoById(applicationId);
    }

    /**
     * 查询应用集成列表
     */
    @Override
    public TableDataInfo<OaApplicationVo> queryPageList(OaApplicationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaApplication> lqw = buildQueryWrapper(bo);
        Page<OaApplicationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询应用集成列表
     */
    @Override
    public List<OaApplicationVo> queryList(OaApplicationBo bo) {
        LambdaQueryWrapper<OaApplication> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaApplication> buildQueryWrapper(OaApplicationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaApplication> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getIcon()), OaApplication::getIcon, bo.getIcon());
        lqw.eq(StringUtils.isNotBlank(bo.getIp()), OaApplication::getIp, bo.getIp());
        lqw.eq(bo.getPort() != null, OaApplication::getPort, bo.getPort());
        lqw.like(StringUtils.isNotBlank(bo.getApplicationName()), OaApplication::getApplicationName, bo.getApplicationName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), OaApplication::getDescription, bo.getDescription());
        return lqw;
    }

    /**
     * 新增应用集成
     */
    @Override
    public Boolean insertByBo(OaApplicationBo bo) {
        OaApplication add = BeanUtil.toBean(bo, OaApplication.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setApplicationId(add.getApplicationId());
        }
        return flag;
    }

    /**
     * 修改应用集成
     */
    @Override
    public Boolean updateByBo(OaApplicationBo bo) {
        OaApplication update = BeanUtil.toBean(bo, OaApplication.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OaApplication entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除应用集成
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
