package com.yingpai.miniapp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yingpai.miniapp.domain.MiniappUser;
import com.yingpai.miniapp.domain.bo.MiniappUserBo;
import com.yingpai.miniapp.domain.vo.MiniappUserVo;
import com.yingpai.miniapp.mapper.MiniappUserMapper;
import com.yingpai.miniapp.service.IMiniappUserService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 小程序用户服务实现类
 */
@RequiredArgsConstructor
@Service
public class MiniappUserServiceImpl implements IMiniappUserService {

    private final MiniappUserMapper baseMapper;

    @Override
    public TableDataInfo<MiniappUserVo> queryPageList(MiniappUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MiniappUser> lqw = buildQueryWrapper(bo);
        Page<MiniappUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<MiniappUserVo> queryList(MiniappUserBo bo) {
        LambdaQueryWrapper<MiniappUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public MiniappUserVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public int insertUser(MiniappUserBo bo) {
        MiniappUser user = new MiniappUser();
        user.setNickname(bo.getNickname());
        user.setPhone(bo.getPhone());
        user.setStatus(bo.getStatus() != null ? bo.getStatus() : 0);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        return baseMapper.insert(user);
    }

    @Override
    public int updateUser(MiniappUserBo bo) {
        LambdaUpdateWrapper<MiniappUser> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(MiniappUser::getId, bo.getId())
                .set(StringUtils.isNotBlank(bo.getNickname()), MiniappUser::getNickname, bo.getNickname())
                .set(StringUtils.isNotBlank(bo.getPhone()), MiniappUser::getPhone, bo.getPhone())
                .set(ObjectUtil.isNotNull(bo.getStatus()), MiniappUser::getStatus, bo.getStatus())
                .set(MiniappUser::getUpdateTime, LocalDateTime.now());
        return baseMapper.update(null, updateWrapper);
    }

    @Override
    public int deleteUser(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<MiniappUser> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(MiniappUser::getId, id)
                .set(MiniappUser::getStatus, status)
                .set(MiniappUser::getUpdateTime, LocalDateTime.now());
        return baseMapper.update(null, updateWrapper);
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<MiniappUser> buildQueryWrapper(MiniappUserBo bo) {
        LambdaQueryWrapper<MiniappUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotNull(bo.getId()), MiniappUser::getId, bo.getId());
        lqw.like(StringUtils.isNotBlank(bo.getPhone()), MiniappUser::getPhone, bo.getPhone());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), MiniappUser::getNickname, bo.getNickname());
        lqw.eq(ObjectUtil.isNotNull(bo.getStatus()), MiniappUser::getStatus, bo.getStatus());
        lqw.orderByDesc(MiniappUser::getCreateTime);
        return lqw;
    }
}
