package com.qingyun.service.service.impl;

import com.qingyun.common.helper.LoginHelper;
import org.apache.commons.lang3.StringUtils;

import com.qingyun.common.utils.MapstructUtils;
import com.qingyun.mybatisplus.page.TableDataInfo;
import com.qingyun.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.qingyun.service.domain.bo.ContractRuleTemplateBo;
import com.qingyun.service.domain.vo.ContractRuleTemplateVo;
import com.qingyun.service.domain.ContractRuleTemplate;
import com.qingyun.service.mapper.ContractRuleTemplateMapper;
import com.qingyun.service.service.IContractRuleTemplateService;

import com.qingyun.mybatisplus.core.ServicePlusImpl;

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

/**
 * 规则要素模板Service业务层处理
 *
 * @author jianlu
 * @date 2025-07-11
 */
@RequiredArgsConstructor
@Service
public class ContractRuleTemplateServiceImpl extends ServicePlusImpl<ContractRuleTemplateMapper, ContractRuleTemplate, ContractRuleTemplateVo> implements IContractRuleTemplateService {


    /**
     * 查询规则要素模板
     */
    @Override
    public ContractRuleTemplateVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询规则要素模板列表
     */
    @Override
    public TableDataInfo<ContractRuleTemplateVo> queryPageList(ContractRuleTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ContractRuleTemplate> lqw = buildQueryWrapper(bo);
        Page<ContractRuleTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询规则要素模板列表
     */
    @Override
    public List<ContractRuleTemplateVo> queryList(ContractRuleTemplateBo bo) {
        LambdaQueryWrapper<ContractRuleTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ContractRuleTemplate> buildQueryWrapper(ContractRuleTemplateBo bo) {
        LambdaQueryWrapper<ContractRuleTemplate> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTemplateName()), ContractRuleTemplate::getTemplateName, bo.getTemplateName());
        lqw.eq(StringUtils.isNotBlank(bo.getReviewRuleJson()), ContractRuleTemplate::getReviewRuleJson, bo.getReviewRuleJson());
        lqw.eq(StringUtils.isNotBlank(bo.getReviewElementJson()), ContractRuleTemplate::getReviewElementJson, bo.getReviewElementJson());
        lqw.eq(ContractRuleTemplate::getStatus, 0);
        lqw.orderByDesc(ContractRuleTemplate::getCreateTime);
        return lqw;
    }

    /**
     * 新增规则要素模板
     */
    @Override
    public Boolean insertByBo(ContractRuleTemplateBo bo) {
        ContractRuleTemplate add = MapstructUtils.convert(bo, ContractRuleTemplate.class);
        if (add ==  null) {
            return false;
        }
        add.setCreateBy(String.valueOf(LoginHelper.getUserId()));;
        add.setCreateTime(new Date());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改规则要素模板
     */
    @Override
    public Boolean updateByBo(ContractRuleTemplateBo bo) {
        ContractRuleTemplate update = MapstructUtils.convert(bo, ContractRuleTemplate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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