package com.ruoyi.biz.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.ruoyi.biz.domain.bo.ArUserBo;
import com.ruoyi.biz.domain.vo.ArUserVo;
import com.ruoyi.biz.domain.ArUser;
import com.ruoyi.biz.mapper.ArUserMapper;
import com.ruoyi.biz.service.IArUserService;

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

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-27
 */
@RequiredArgsConstructor
@Service
public class ArUserServiceImpl implements IArUserService {

    private final ArUserMapper baseMapper;

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

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

    /**
     * 查询用户列表
     */
    @Override
    public List<ArUserVo> queryList(ArUserBo bo) {
        LambdaQueryWrapper<ArUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArUser> buildQueryWrapper(ArUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ArUser::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), ArUser::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), ArUser::getOpenId, bo.getOpenId());
        return lqw;
    }

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

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

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

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

    @Override
    public Map<String, Object> findUser(String name, String image, String openId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("flag", 1);
        Map<String, Object> user = baseMapper.findUser(openId);
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("name", name);
        paraMap.put("image", image);
        paraMap.put("openId", openId);
        if(user != null){
            paraMap.put("userId", user.get("userId"));
            baseMapper.updateUser(paraMap);
        }else{
            baseMapper.addUser(paraMap);
        }
        map.put("user", baseMapper.findUser(openId));
        return map;
    }
}
