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.WxUserCommunityLedgerBo;
import com.propertyManagement.business.domain.vo.WxUserCommunityLedgerVo;
import com.propertyManagement.business.domain.WxUserCommunityLedger;
import com.propertyManagement.business.mapper.WxUserCommunityLedgerMapper;
import com.propertyManagement.business.service.IWxUserCommunityLedgerService;

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

/**
 * 用户、小区、台账关联报Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-14
 */
@RequiredArgsConstructor
@Service
public class WxUserCommunityLedgerServiceImpl implements IWxUserCommunityLedgerService {

    private final WxUserCommunityLedgerMapper baseMapper;

    /**
     * 查询用户、小区、台账关联报
     */
    @Override
    public WxUserCommunityLedgerVo queryById(Long userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询用户、小区、台账关联报列表
     */
    @Override
    public TableDataInfo<WxUserCommunityLedgerVo> queryPageList(WxUserCommunityLedgerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxUserCommunityLedger> lqw = buildQueryWrapper(bo);
        Page<WxUserCommunityLedgerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户、小区、台账关联报列表
     */
    @Override
    public List<WxUserCommunityLedgerVo> queryList(WxUserCommunityLedgerBo bo) {
        LambdaQueryWrapper<WxUserCommunityLedger> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxUserCommunityLedger> buildQueryWrapper(WxUserCommunityLedgerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxUserCommunityLedger> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommunityId() != null, WxUserCommunityLedger::getCommunityId, bo.getCommunityId());
        lqw.eq(bo.getProprietorId() != null, WxUserCommunityLedger::getProprietorId, bo.getProprietorId());
        return lqw;
    }

    /**
     * 新增用户、小区、台账关联报
     */
    @Override
    public Boolean insertByBo(WxUserCommunityLedgerBo bo) {
        WxUserCommunityLedger add = BeanUtil.toBean(bo, WxUserCommunityLedger.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户、小区、台账关联报
     */
    @Override
    public Boolean updateByBo(WxUserCommunityLedgerBo bo) {
        WxUserCommunityLedger update = BeanUtil.toBean(bo, WxUserCommunityLedger.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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