package cn.newfeifan.mall.module.distri.service.integral;

import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofitlog.PtProfitLogDO;
import cn.newfeifan.mall.module.distri.service.sharepath.SharePathService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.newfeifan.mall.module.distri.controller.admin.integral.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.integral.IntegralDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.integral.IntegralMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 推荐用户积分 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class IntegralServiceImpl implements IntegralService {

    @Resource
    private IntegralMapper integralMapper;

    @Resource
    private SharePathService sharePathService;

    @Override
    public Long createIntegral(IntegralSaveReqVO createReqVO) {
        // 插入
        IntegralDO integral = BeanUtils.toBean(createReqVO, IntegralDO.class);
        integralMapper.insert(integral);
        // 返回
        return integral.getId();
    }

    @Override
    public void updateIntegral(IntegralSaveReqVO updateReqVO) {
        // 校验存在
        validateIntegralExists(updateReqVO.getId());
        // 更新
        IntegralDO updateObj = BeanUtils.toBean(updateReqVO, IntegralDO.class);
        integralMapper.updateById(updateObj);
    }

    @Override
    public void deleteIntegral(Long id) {
        // 校验存在
        validateIntegralExists(id);
        // 删除
        integralMapper.deleteById(id);
    }

    private void validateIntegralExists(Long id) {
        if (integralMapper.selectById(id) == null) {
            throw exception(INTEGRAL_NOT_EXISTS);
        }
    }

    @Override
    public IntegralDO getIntegral(Long id) {
        return integralMapper.selectById(id);
    }

    @Override
    public PageResult<IntegralDO> getIntegralPage(IntegralPageReqVO pageReqVO) {
        return integralMapper.selectPage(pageReqVO);
    }

    @Override
    public IntegralDO selectByUser(Long descendant) {
        return integralMapper.selectOne(new LambdaQueryWrapper<IntegralDO>().eq(IntegralDO::getUserId, descendant));
    }

    @Override
    public void saveBatch(List<IntegralSaveReqVO> integralSaveReqVOS) {
        List<IntegralDO> integralDOS = BeanUtils.toBean(integralSaveReqVOS, IntegralDO.class);
        integralMapper.insertBatch(integralDOS);
    }

    @Override
    public void updateBatch(List<IntegralSaveReqVO> integralSaveReqVOS) {

        integralSaveReqVOS.forEach(k -> {
            // 校验存在
            validateIntegralExists(k.getId());
            // 更新
            IntegralDO updateObj = BeanUtils.toBean(k, IntegralDO.class);
            integralMapper.updateById(updateObj);
        });

    }

    @Override
    public IntegralDO selectUserParent(Long userId) {

        Long parentId = sharePathService.queryParentBySonUserId(userId);
        return integralMapper.selectOne(new LambdaQueryWrapperX<IntegralDO>().eqIfPresent(IntegralDO::getUserId, parentId));
    }

}