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.OaOperationRuleBo;
import com.ruoyi.oa.domain.vo.OaOperationRuleVo;
import com.ruoyi.oa.domain.OaOperationRule;
import com.ruoyi.oa.mapper.OaOperationRuleMapper;
import com.ruoyi.oa.service.IOaOperationRuleService;

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

/**
 * 运营中心制度管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-22
 */
@RequiredArgsConstructor
@Service
public class OaOperationRuleServiceImpl implements IOaOperationRuleService {

    private final OaOperationRuleMapper baseMapper;

    /**
     * 查询运营中心制度管理
     */
    @Override
    public OaOperationRuleVo queryById(Long ruleId){
        return baseMapper.selectVoById(ruleId);
    }

    /**
     * 查询运营中心制度管理列表
     */
    @Override
    public TableDataInfo<OaOperationRuleVo> queryPageList(OaOperationRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaOperationRule> lqw = buildQueryWrapper(bo);
        Page<OaOperationRuleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询运营中心制度管理列表
     */
    @Override
    public List<OaOperationRuleVo> queryList(OaOperationRuleBo bo) {
        LambdaQueryWrapper<OaOperationRule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaOperationRule> buildQueryWrapper(OaOperationRuleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaOperationRule> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRuleCategory()), OaOperationRule::getRuleCategory, bo.getRuleCategory());
        lqw.like(StringUtils.isNotBlank(bo.getRuleName()), OaOperationRule::getRuleName, bo.getRuleName());
        lqw.eq(StringUtils.isNotBlank(bo.getCoverage()), OaOperationRule::getCoverage, bo.getCoverage());
        lqw.eq(StringUtils.isNotBlank(bo.getResponsibleDepartment()), OaOperationRule::getResponsibleDepartment, bo.getResponsibleDepartment());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessory()), OaOperationRule::getAccessory, bo.getAccessory());
        return lqw;
    }

    /**
     * 新增运营中心制度管理
     */
    @Override
    public Boolean insertByBo(OaOperationRuleBo bo) {
        OaOperationRule add = BeanUtil.toBean(bo, OaOperationRule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRuleId(add.getRuleId());
        }
        return flag;
    }

    /**
     * 修改运营中心制度管理
     */
    @Override
    public Boolean updateByBo(OaOperationRuleBo bo) {
        OaOperationRule update = BeanUtil.toBean(bo, OaOperationRule.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除运营中心制度管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
