package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.business.domain.WxCommunity;
import com.propertyManagement.business.mapper.WxCommunityMapper;
import com.propertyManagement.common.constant.CacheConstants;
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 com.propertyManagement.common.utils.redis.RedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.propertyManagement.business.domain.bo.WxGuestBo;
import com.propertyManagement.business.domain.vo.WxGuestVo;
import com.propertyManagement.business.domain.WxGuest;
import com.propertyManagement.business.mapper.WxGuestMapper;
import com.propertyManagement.business.service.IWxGuestService;

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

/**
 * 访客Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-14
 */
@RequiredArgsConstructor
@Service
public class WxGuestServiceImpl implements IWxGuestService {

    private final WxGuestMapper baseMapper;
    private final WxCommunityMapper communityMapper;

    /**
     * 查询访客
     */
    @Override
    public WxGuestVo queryById(Long guestId){
        return baseMapper.selectVoById(guestId);
    }

    /**
     * 查询访客列表
     */
    @Override
    public TableDataInfo<WxGuestVo> queryPageList(WxGuestBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxGuest> lqw = buildQueryWrapper(bo);
        Page<WxGuestVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询访客列表
     */
    @Override
    public List<WxGuestVo> queryList(WxGuestBo bo) {
        LambdaQueryWrapper<WxGuest> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxGuest> buildQueryWrapper(WxGuestBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxGuest> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, WxGuest::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getGuestName()), WxGuest::getGuestName, bo.getGuestName());
        lqw.eq(bo.getGuestTime() != null, WxGuest::getGuestTime, bo.getGuestTime());
        lqw.eq(StringUtils.isNotBlank(bo.getGuestPhone()), WxGuest::getGuestPhone, bo.getGuestPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getGuestRoom()), WxGuest::getGuestRoom, bo.getGuestRoom());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateNumber()), WxGuest::getPlateNumber, bo.getPlateNumber());
        lqw.eq(bo.getCommunityId() != null, WxGuest::getCommunityId, bo.getCommunityId());
        lqw.like(StringUtils.isNotBlank(bo.getCommunityName()), WxGuest::getCommunityName, bo.getCommunityName());
        return lqw;
    }

    /**
     * 新增访客
     */
    @Override
    public Boolean add(WxGuestBo bo) {
        WxGuest add = BeanUtil.toBean(bo, WxGuest.class);
        String key = CacheConstants.GUEST_PHONE_CODES + bo.getGuestPhone();
        Object cacheObject = RedisUtils.getCacheObject(key);
        if (null == cacheObject) {
            throw new RuntimeException("提交失败，验证码不存在");
        }
        if (!bo.getPhoneCode().equals(cacheObject)) {
            throw new RuntimeException("提交失败，验证码错误");
        }
        WxCommunity wxCommunity = communityMapper.selectById(add.getCommunityId());
        add.setCommunityName(wxCommunity.getCommunityName());
        add.setGuestTime(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            RedisUtils.deleteObject(key);
        }
        return flag;
    }

    /**
     * 新增访客
     */
    @Override
    public Boolean insertByBo(WxGuestBo bo) {
        WxGuest add = BeanUtil.toBean(bo, WxGuest.class);
        validEntityBeforeSave(add);
        add.setGuestTime(new Date());
        WxCommunity community = communityMapper.selectById(bo.getCommunityId());
        add.setCommunityName(community.getCommunityName());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setGuestId(add.getGuestId());
        }
        return flag;
    }

    /**
     * 修改访客
     */
    @Override
    public Boolean updateByBo(WxGuestBo bo) {
        WxGuest update = BeanUtil.toBean(bo, WxGuest.class);
        validEntityBeforeSave(update);
        WxCommunity community = communityMapper.selectById(bo.getCommunityId());
        update.setCommunityName(community.getCommunityName());
        return baseMapper.updateById(update) > 0;
    }

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

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