package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.business.domain.vo.WxCommunityVo;
import com.propertyManagement.business.mapper.WxCommunityMapper;
import com.propertyManagement.common.enums.IsEnableType;
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.WxCommonPhoneBo;
import com.propertyManagement.business.domain.vo.WxCommonPhoneVo;
import com.propertyManagement.business.domain.WxCommonPhone;
import com.propertyManagement.business.mapper.WxCommonPhoneMapper;
import com.propertyManagement.business.service.IWxCommonPhoneService;

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

/**
 * 常用电话Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-14
 */
@RequiredArgsConstructor
@Service
public class WxCommonPhoneServiceImpl implements IWxCommonPhoneService {

    private final WxCommonPhoneMapper baseMapper;
    private final WxCommunityMapper communityMapper;

    /**
     * 查询常用电话
     */
    @Override
    public WxCommonPhoneVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询常用电话列表
     */
    @Override
    public TableDataInfo<WxCommonPhoneVo> queryPageList(WxCommonPhoneBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxCommonPhone> lqw = buildQueryWrapper(bo);
        Page<WxCommonPhoneVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询常用电话列表
     */
    @Override
    public List<WxCommonPhoneVo> queryList(WxCommonPhoneBo bo) {
        LambdaQueryWrapper<WxCommonPhone> lqw = buildQueryWrapper(bo);
        lqw.eq(WxCommonPhone::getIsEnable, IsEnableType.YES.getCode());
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxCommonPhone> buildQueryWrapper(WxCommonPhoneBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxCommonPhone> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommunityId() != null, WxCommonPhone::getCommunityId, bo.getCommunityId());
        lqw.like(StringUtils.isNotBlank(bo.getCommunityName()), WxCommonPhone::getCommunityName, bo.getCommunityName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WxCommonPhone::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), WxCommonPhone::getPhone, bo.getPhone());
        lqw.eq(bo.getIsEnable() != null, WxCommonPhone::getIsEnable, bo.getIsEnable());
        lqw.orderByAsc(WxCommonPhone::getCommunityName);
        lqw.orderByAsc(WxCommonPhone::getOrderNum);
        return lqw;
    }

    /**
     * 新增常用电话
     */
    @Override
    public Boolean insertByBo(WxCommonPhoneBo bo) {
        WxCommonPhone add = BeanUtil.toBean(bo, WxCommonPhone.class);
        validEntityBeforeSave(add);
        WxCommunityVo communityVo = communityMapper.selectVoById(add.getCommunityId());
        add.setCommunityName(communityVo.getCommunityName());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改常用电话
     */
    @Override
    public Boolean updateByBo(WxCommonPhoneBo bo) {
        WxCommonPhone update = BeanUtil.toBean(bo, WxCommonPhone.class);
        validEntityBeforeSave(update);
        WxCommunityVo communityVo = communityMapper.selectVoById(update.getCommunityId());
        update.setCommunityName(communityVo.getCommunityName());
        return baseMapper.updateById(update) > 0;
    }

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

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