package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.WxProtocolBo;
import com.propertyManagement.business.domain.vo.WxProtocolVo;
import com.propertyManagement.business.domain.WxProtocol;
import com.propertyManagement.business.mapper.WxProtocolMapper;
import com.propertyManagement.business.service.IWxProtocolService;

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

/**
 * 用户协议Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-24
 */
@RequiredArgsConstructor
@Service
public class WxProtocolServiceImpl implements IWxProtocolService {

    private final WxProtocolMapper baseMapper;

    /**
     * 查询用户协议
     */
    @Override
    public WxProtocolVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户协议列表
     */
    @Override
    public TableDataInfo<WxProtocolVo> queryPageList(WxProtocolBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxProtocol> lqw = buildQueryWrapper(bo);
        Page<WxProtocolVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户协议列表
     */
    @Override
    public List<WxProtocolVo> queryList(WxProtocolBo bo) {
        LambdaQueryWrapper<WxProtocol> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxProtocol> buildQueryWrapper(WxProtocolBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxProtocol> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), WxProtocol::getName, bo.getName());
        lqw.eq(bo.getType() != null, WxProtocol::getType, bo.getType());
        lqw.eq(bo.getIsEnable() != null, WxProtocol::getIsEnable, bo.getIsEnable());
        return lqw;
    }

    /**
     * 新增用户协议
     */
    @Override
    public Boolean insertByBo(WxProtocolBo bo) {
        WxProtocol add = BeanUtil.toBean(bo, WxProtocol.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户协议
     */
    @Override
    public Boolean updateByBo(WxProtocolBo bo) {
        WxProtocol update = BeanUtil.toBean(bo, WxProtocol.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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