package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.business.domain.WxUser;
import com.propertyManagement.business.domain.bo.WxUserBo;
import com.propertyManagement.business.domain.vo.WxUserVo;
import com.propertyManagement.business.mapper.WxUserMapper;
import com.propertyManagement.business.service.IWxUserService;
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 java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 微信用户Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-13
 */
@RequiredArgsConstructor
@Service
public class WxUserServiceImpl implements IWxUserService {

    private final WxUserMapper baseMapper;

    /**
     * 查询微信用户
     */
    @Override
    public WxUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询微信用户列表
     */
    @Override
    public TableDataInfo<WxUserVo> queryPageList(WxUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxUser> lqw = buildQueryWrapper(bo);
        Page<WxUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询微信用户列表
     */
    @Override
    public List<WxUserVo> queryList(WxUserBo bo) {
        LambdaQueryWrapper<WxUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxUser> buildQueryWrapper(WxUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), WxUser::getNickName, bo.getNickName());
        lqw.eq(bo.getLatitude() != null, WxUser::getLatitude, bo.getLatitude());
        lqw.eq(bo.getLongitude() != null, WxUser::getLongitude, bo.getLongitude());
        lqw.eq(bo.getLocPrecision() != null, WxUser::getLocPrecision, bo.getLocPrecision());
        lqw.eq(StringUtils.isNotBlank(bo.getUserPermission()), WxUser::getUserPermission, bo.getUserPermission());
        lqw.eq(bo.getIsDelete() != null, WxUser::getIsDelete, bo.getIsDelete());
        return lqw;
    }

    /**
     * 新增微信用户
     */
    @Override
    public Boolean insertByBo(WxUserBo bo) {
        WxUser add = BeanUtil.toBean(bo, WxUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改微信用户
     */
    @Override
    public Boolean updateByBo(WxUserBo bo) {
        WxUser update = BeanUtil.toBean(bo, WxUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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