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.WxCarport;
import com.propertyManagement.business.domain.WxProprietorLedger;
import com.propertyManagement.business.domain.bo.WxCarportBo;
import com.propertyManagement.business.domain.vo.WxCarportVo;
import com.propertyManagement.business.mapper.WxCarportMapper;
import com.propertyManagement.business.mapper.WxCommunityMapper;
import com.propertyManagement.business.mapper.WxProprietorLedgerMapper;
import com.propertyManagement.business.service.IWxCarportService;
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-26
 */
@RequiredArgsConstructor
@Service
public class WxCarportServiceImpl implements IWxCarportService {

    private final WxCarportMapper baseMapper;

    private final WxCommunityMapper communityMapper;

    private final WxProprietorLedgerMapper proprietorLedgerMapper;

    /**
     * 查询业主车位
     */
    @Override
    public WxCarportVo queryById(Long carportId){
        return baseMapper.selectVoById(carportId);
    }

    /**
     * 查询业主车位列表
     */
    @Override
    public TableDataInfo<WxCarportVo> queryPageList(WxCarportBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxCarport> lqw = buildQueryWrapper(bo);
        Page<WxCarportVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询业主车位列表
     */
    @Override
    public List<WxCarportVo> queryList(WxCarportBo bo) {
        LambdaQueryWrapper<WxCarport> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxCarport> buildQueryWrapper(WxCarportBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxCarport> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommunityId() != null, WxCarport::getCommunityId, bo.getCommunityId());
        lqw.eq(bo.getProprietorId() != null, WxCarport::getProprietorId, bo.getProprietorId());
        lqw.like(StringUtils.isNotBlank(bo.getProprietorName()), WxCarport::getProprietorName, bo.getProprietorName());
        lqw.eq(StringUtils.isNotBlank(bo.getProprietorPhone()), WxCarport::getProprietorPhone, bo.getProprietorPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getRoom()), WxCarport::getRoom, bo.getRoom());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateNumber()), WxCarport::getPlateNumber, bo.getPlateNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleModel()), WxCarport::getVehicleModel, bo.getVehicleModel());
        lqw.eq(StringUtils.isNotBlank(bo.getCarportPhone()), WxCarport::getCarportPhone, bo.getCarportPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getCarportNumber()), WxCarport::getCarportNumber, bo.getCarportNumber());
        lqw.eq(bo.getType() != null, WxCarport::getType, bo.getType());
        lqw.orderByDesc(WxCarport::getCreateTime);
        return lqw;
    }

    /**
     * 新增业主车位
     */
    @Override
    public Boolean insertByBo(WxCarportBo bo) {
        WxCarport add = BeanUtil.toBean(bo, WxCarport.class);
        validEntityBeforeSave(add);
        WxProprietorLedger proprietorLedger = proprietorLedgerMapper.selectById(bo.getProprietorId());
        add.setCommunityId(proprietorLedger.getCommunityId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCarportId(add.getCarportId());
        }
        return flag;
    }

    /**
     * 修改业主车位
     */
    @Override
    public Boolean updateByBo(WxCarportBo bo) {
        WxCarport update = BeanUtil.toBean(bo, WxCarport.class);
        validEntityBeforeSave(update);
        WxProprietorLedger proprietorLedger = proprietorLedgerMapper.selectById(bo.getProprietorId());
        update.setCommunityId(proprietorLedger.getCommunityId());
        return baseMapper.updateById(update) > 0;
    }

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

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