package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.propertyManagement.business.domain.WxFamilyMember;
import com.propertyManagement.business.domain.WxProprietorLedger;
import com.propertyManagement.business.domain.WxUserCommunityLedger;
import com.propertyManagement.business.domain.bo.WxFamilyMemberBo;
import com.propertyManagement.business.domain.vo.LoginWxUser;
import com.propertyManagement.business.domain.vo.WxFamilyMemberVo;
import com.propertyManagement.business.mapper.WxFamilyMemberMapper;
import com.propertyManagement.business.mapper.WxProprietorLedgerMapper;
import com.propertyManagement.business.mapper.WxUserCommunityLedgerMapper;
import com.propertyManagement.business.service.IWxFamilyMemberService;
import com.propertyManagement.business.support.util.AuthUtil;
import com.propertyManagement.common.core.domain.PageQuery;
import com.propertyManagement.common.core.page.TableDataInfo;
import com.propertyManagement.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 家庭成员Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-14
 */
@RequiredArgsConstructor
@Service
public class WxFamilyMemberServiceImpl implements IWxFamilyMemberService {

    private final WxFamilyMemberMapper baseMapper;
    private final WxProprietorLedgerMapper proprietorLedgerMapper;
    private final WxUserCommunityLedgerMapper userCommunityLedgerMapper;

    /**
     * 查询家庭成员
     */
    @Override
    public WxFamilyMemberVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询家庭成员列表
     */
    @Override
    public TableDataInfo<WxFamilyMemberVo> queryPageList(WxFamilyMemberBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxFamilyMember> lqw = buildQueryWrapper(bo);
        Page<WxFamilyMemberVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询家庭成员列表
     */
    @Override
    public List<WxFamilyMemberVo> queryList(WxFamilyMemberBo bo) {
        LambdaQueryWrapper<WxFamilyMember> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxFamilyMember> buildQueryWrapper(WxFamilyMemberBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxFamilyMember> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, WxFamilyMember::getUserId, bo.getUserId());
        lqw.eq(bo.getCommunityId() != null, WxFamilyMember::getCommunityId, bo.getCommunityId());
        lqw.like(bo.getCommunityName() != null, WxFamilyMember::getCommunityName, bo.getCommunityName());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), WxFamilyMember::getAvatar, bo.getAvatar());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WxFamilyMember::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), WxFamilyMember::getPhone, bo.getPhone());
        lqw.eq(bo.getRelation() != null, WxFamilyMember::getRelation, bo.getRelation());
        lqw.eq(bo.getState() != null, WxFamilyMember::getState, bo.getState());
        return lqw;
    }

    /**
     * 新增家庭成员
     */
    @Override
    public Boolean insertByBo(WxFamilyMemberBo bo) {
        WxFamilyMember add = BeanUtil.toBean(bo, WxFamilyMember.class);
        validEntityBeforeSave(add);
        WxProprietorLedger proprietorLedger = proprietorLedgerMapper.selectById(bo.getProprietorId());
        add.setProprietorId(proprietorLedger.getProprietorId());
        add.setProprietorName(proprietorLedger.getName());
        add.setCommunityId(proprietorLedger.getCommunityId());
        add.setCommunityName(proprietorLedger.getCommunityName());
        add.setRoom(proprietorLedger.getRoom());
        add.setUserId(proprietorLedger.getUserId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改家庭成员
     */
    @Override
    public Boolean updateByBo(WxFamilyMemberBo bo) {
        WxFamilyMember update = BeanUtil.toBean(bo, WxFamilyMember.class);
        validEntityBeforeSave(update);
        WxProprietorLedger proprietorLedger = proprietorLedgerMapper.selectById(AuthUtil.getWxUser().getProprietorId());
        update.setProprietorId(proprietorLedger.getProprietorId());
        update.setProprietorName(proprietorLedger.getName());
        update.setCommunityId(proprietorLedger.getCommunityId());
        update.setCommunityName(proprietorLedger.getCommunityName());
        update.setRoom(proprietorLedger.getRoom());
        update.setUserId(proprietorLedger.getUserId());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 校验并删除家庭成员信息
     *
     * @param id
     */
    @Override
    public Boolean deleteWithValidById(Long id) {
        WxFamilyMember familyMember = baseMapper.selectById(id);
        LambdaQueryWrapper<WxUserCommunityLedger> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(WxUserCommunityLedger::getUserId, familyMember.getUserId())
            .eq(WxUserCommunityLedger::getCommunityId, familyMember.getCommunityId())
            .eq(WxUserCommunityLedger::getProprietorId, familyMember.getProprietorId());
        userCommunityLedgerMapper.delete(lambdaQueryWrapper);
        return baseMapper.deleteById(id) > 0;
    }

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

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