package com.yongqi.xinrenli.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.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.yongqi.xinrenli.domain.bo.DbWxUserBo;
import com.yongqi.xinrenli.domain.vo.DbWxUserVo;
import com.yongqi.xinrenli.domain.DbWxUser;
import com.yongqi.xinrenli.mapper.DbWxUserMapper;
import com.yongqi.xinrenli.service.IDbWxUserService;

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

/**
 * 微信用户Service业务层处理
 *
 * @author zyf
 * @date 2023-03-09
 */
@RequiredArgsConstructor
@Service
public class DbWxUserServiceImpl implements IDbWxUserService {

    private final DbWxUserMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<DbWxUser> buildQueryWrapper(DbWxUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DbWxUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), DbWxUser::getOpenId, bo.getOpenId());
        lqw.eq(bo.getHeadOrNot() != null, DbWxUser::getHeadOrNot, bo.getHeadOrNot());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), DbWxUser::getNickname, bo.getNickname());
        lqw.eq(bo.getSex() != null, DbWxUser::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), DbWxUser::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), DbWxUser::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getCountry()), DbWxUser::getCountry, bo.getCountry());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadImgUrl()), DbWxUser::getHeadImgUrl, bo.getHeadImgUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getUnionId()), DbWxUser::getUnionId, bo.getUnionId());
        lqw.eq(StringUtils.isNotBlank(bo.getMobile()), DbWxUser::getMobile, bo.getMobile());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthMobile()), DbWxUser::getAuthMobile, bo.getAuthMobile());
        lqw.eq(StringUtils.isNotBlank(bo.getMallCode()), DbWxUser::getMallCode, bo.getMallCode());
        lqw.eq(StringUtils.isNotBlank(bo.getUserCode()), DbWxUser::getUserCode, bo.getUserCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSessionKey()), DbWxUser::getSessionKey, bo.getSessionKey());
        lqw.eq(bo.getWechatSubscribe() != null, DbWxUser::getWechatSubscribe, bo.getWechatSubscribe());
        lqw.eq(bo.getCancelSubscribe() != null, DbWxUser::getCancelSubscribe, bo.getCancelSubscribe());
        lqw.eq(StringUtils.isNotBlank(bo.getWechatOriginalId()), DbWxUser::getWechatOriginalId, bo.getWechatOriginalId());
        lqw.eq(bo.getOrderNum() != null, DbWxUser::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getCumulativeConsumption() != null, DbWxUser::getCumulativeConsumption, bo.getCumulativeConsumption());
        lqw.eq(bo.getLastConsumptionTime() != null, DbWxUser::getLastConsumptionTime, bo.getLastConsumptionTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateCode()), DbWxUser::getCreateCode, bo.getCreateCode());
        lqw.eq(bo.getModifyTime() != null, DbWxUser::getModifyTime, bo.getModifyTime());
        lqw.eq(StringUtils.isNotBlank(bo.getModifyBy()), DbWxUser::getModifyBy, bo.getModifyBy());
        lqw.eq(StringUtils.isNotBlank(bo.getModifyCode()), DbWxUser::getModifyCode, bo.getModifyCode());
        lqw.eq(bo.getDeleteFlag() != null, DbWxUser::getDeleteFlag, bo.getDeleteFlag());
        return lqw;
    }

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

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

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

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