package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.business.domain.WxUser;
import com.propertyManagement.business.domain.WxUserCommunityLedger;
import com.propertyManagement.business.domain.vo.WxCommunityVo;
import com.propertyManagement.business.mapper.WxCommunityMapper;
import com.propertyManagement.business.mapper.WxUserCommunityLedgerMapper;
import com.propertyManagement.business.mapper.WxUserMapper;
import com.propertyManagement.common.core.domain.BaseEntity;
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.WxProprietorLedgerBo;
import com.propertyManagement.business.domain.vo.WxProprietorLedgerVo;
import com.propertyManagement.business.domain.WxProprietorLedger;
import com.propertyManagement.business.mapper.WxProprietorLedgerMapper;
import com.propertyManagement.business.service.IWxProprietorLedgerService;

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

/**
 * 小区业主台账信息Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-15
 */
@RequiredArgsConstructor
@Service
public class WxProprietorLedgerServiceImpl implements IWxProprietorLedgerService {

    private final WxProprietorLedgerMapper baseMapper;
    private final WxCommunityMapper communityMapper;
    private final WxUserMapper userMapper;
    private final WxUserCommunityLedgerMapper userCommunityLedgerMapper;

    /**
     * 查询小区业主台账信息
     */
    @Override
    public WxProprietorLedgerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询小区业主台账信息列表
     */
    @Override
    public TableDataInfo<WxProprietorLedgerVo> queryPageList(WxProprietorLedgerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxProprietorLedger> lqw = buildQueryWrapper(bo);
        Page<WxProprietorLedgerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询小区业主台账信息列表
     */
    @Override
    public List<WxProprietorLedgerVo> queryList(WxProprietorLedgerBo bo) {
        LambdaQueryWrapper<WxProprietorLedger> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxProprietorLedger> buildQueryWrapper(WxProprietorLedgerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxProprietorLedger> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, WxProprietorLedger::getUserId, bo.getUserId());
        lqw.eq(bo.getCommunityId() != null, WxProprietorLedger::getCommunityId, bo.getCommunityId());
        lqw.like(StringUtils.isNotBlank(bo.getCommunityName()), WxProprietorLedger::getCommunityName, bo.getCommunityName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WxProprietorLedger::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), WxProprietorLedger::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseType()), WxProprietorLedger::getHouseType, bo.getHouseType());
        lqw.eq(StringUtils.isNotBlank(bo.getRoom()), WxProprietorLedger::getRoom, bo.getRoom());
        lqw.eq(bo.getSqm() != null, WxProprietorLedger::getSqm, bo.getSqm());
        lqw.eq(bo.getSqmPrice() != null, WxProprietorLedger::getSqmPrice, bo.getSqmPrice());
        lqw.eq(bo.getMonthMoney() != null, WxProprietorLedger::getMonthMoney, bo.getMonthMoney());
        lqw.eq(bo.getYearMoney() != null, WxProprietorLedger::getYearMoney, bo.getYearMoney());
        lqw.eq(bo.getBirthDate() != null, WxProprietorLedger::getBirthDate, bo.getBirthDate());
        lqw.eq(StringUtils.isNotBlank(bo.getIdentityCard()), WxProprietorLedger::getIdentityCard, bo.getIdentityCard());
        lqw.eq(StringUtils.isNotBlank(bo.getRegisterAddress()), WxProprietorLedger::getRegisterAddress, bo.getRegisterAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getPresentAddress()), WxProprietorLedger::getPresentAddress, bo.getPresentAddress());
        lqw.orderByDesc(BaseEntity::getCreateTime);
        return lqw;
    }

    /**
     * 新增小区业主台账信息
     */
    @Override
    public Boolean insertByBo(WxProprietorLedgerBo bo) {
        WxProprietorLedger add = BeanUtil.toBean(bo, WxProprietorLedger.class);
        validEntityBeforeSave(add);
        WxCommunityVo communityVo = communityMapper.selectVoById(bo.getCommunityId());
        add.setCommunityName(communityVo.getCommunityName());
        boolean flag = baseMapper.insert(add) > 0;
        /**
         * 检查台账户主是否匹配已存在用户，存在需进行绑定
         */
        LambdaQueryWrapper<WxUser> wxUserLambdaQueryWrapper = Wrappers.lambdaQuery();
        wxUserLambdaQueryWrapper.eq(WxUser::getMobile, add.getPhone());
        List<WxUser> users = userMapper.selectList(wxUserLambdaQueryWrapper);
        if (!users.isEmpty()) {
            List<WxUserCommunityLedger> collect = users.stream().map(item -> {
                WxUserCommunityLedger communityLedger = new WxUserCommunityLedger();
                communityLedger.setUserId(item.getId());
                communityLedger.setCommunityId(add.getCommunityId());
                communityLedger.setProprietorId(add.getProprietorId());
                return communityLedger;
            }).collect(Collectors.toList());
            userCommunityLedgerMapper.insertBatch(collect);
        }
        if (flag) {
            bo.setProprietorId(add.getProprietorId());
        }
        return flag;
    }

    /**
     * 修改小区业主台账信息
     */
    @Override
    public Boolean updateByBo(WxProprietorLedgerBo bo) {
        WxProprietorLedger update = BeanUtil.toBean(bo, WxProprietorLedger.class);
        validEntityBeforeSave(update);
        WxCommunityVo communityVo = communityMapper.selectVoById(bo.getCommunityId());
        update.setCommunityName(communityVo.getCommunityName());
        return baseMapper.updateById(update) > 0;
    }

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

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