package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.business.domain.WxCommunity;
import com.propertyManagement.business.mapper.WxCommunityMapper;
import com.propertyManagement.common.utils.StringUtils;
import com.propertyManagement.common.core.page.TableDataInfo;
import com.propertyManagement.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.propertyManagement.business.domain.bo.WxRuleBo;
import com.propertyManagement.business.domain.vo.WxRuleVo;
import com.propertyManagement.business.domain.WxRule;
import com.propertyManagement.business.mapper.WxRuleMapper;
import com.propertyManagement.business.service.IWxRuleService;

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

/**
 * 规则说明Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-14
 */
@RequiredArgsConstructor
@Service
public class WxRuleServiceImpl implements IWxRuleService {

    private final WxRuleMapper baseMapper;
    private final WxCommunityMapper communityMapper;

    /**
     * 查询规则说明
     */
    @Override
    public WxRuleVo queryById(Long ruleId){
        return baseMapper.selectVoById(ruleId);
    }

    /**
     * 查询规则说明列表
     */
    @Override
    public TableDataInfo<WxRuleVo> queryPageList(WxRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxRule> lqw = buildQueryWrapper(bo);
        Page<WxRuleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询规则说明列表
     */
    @Override
    public List<WxRuleVo> queryList(WxRuleBo bo) {
        LambdaQueryWrapper<WxRule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxRule> buildQueryWrapper(WxRuleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxRule> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommunityId() != null, WxRule::getCommunityId, bo.getCommunityId());
        lqw.like(StringUtils.isNotBlank(bo.getCommunityName()), WxRule::getCommunityName, bo.getCommunityName());
        lqw.eq(StringUtils.isNotBlank(bo.getRuleContent()), WxRule::getRuleContent, bo.getRuleContent());
        lqw.eq(bo.getRuleType() != null, WxRule::getRuleType, bo.getRuleType());
        lqw.eq(bo.getIsEnable() != null, WxRule::getIsEnable, bo.getIsEnable());
        return lqw;
    }

    /**
     * 新增规则说明
     */
    @Override
    public Boolean insertByBo(WxRuleBo bo) {
        WxRule add = BeanUtil.toBean(bo, WxRule.class);
        validEntityBeforeSave(add);
        WxCommunity community = communityMapper.selectById(add.getCommunityId());
        add.setCommunityName(community.getCommunityName());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRuleId(add.getRuleId());
        }
        return flag;
    }

    /**
     * 修改规则说明
     */
    @Override
    public Boolean updateByBo(WxRuleBo bo) {
        WxRule update = BeanUtil.toBean(bo, WxRule.class);
        validEntityBeforeSave(update);
        WxCommunity community = communityMapper.selectById(update.getCommunityId());
        update.setCommunityName(community.getCommunityName());
        return baseMapper.updateById(update) > 0;
    }

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

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