package com.yiqi.app.service.impl;

import java.util.List;

import com.yiqi.app.mapper.AppUserMapper;
import com.yiqi.common.enums.MemberPointSourceEnum;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.app.mapper.AppUserPointLogMapper;
import com.yiqi.app.domain.AppUserPointLog;
import com.yiqi.app.service.IAppUserPointLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 积分日志Service业务层处理
 *
 * @author douya
 * @date 2024-09-04
 */
@Service
public class AppUserPointLogServiceImpl extends ServiceImpl<AppUserPointLogMapper, AppUserPointLog> implements IAppUserPointLogService {

    @Autowired
    private AppUserMapper appUserMapper;

    //<editor-folder desc="基础韩素">

    /**
     * 查询积分日志
     *
     * @param id 积分日志主键
     * @return 积分日志
     */
    @Override
    public AppUserPointLog selectAppUserPointLogById(Integer id) {
        return baseMapper.selectAppUserPointLogById(id);
    }

    /**
     * 查询积分日志列表
     *
     * @param appUserPointLog 积分日志
     * @return 积分日志
     */
    @Override
    public List<AppUserPointLog> selectAppUserPointLogList(AppUserPointLog appUserPointLog) {
        return baseMapper.selectAppUserPointLogList(appUserPointLog);
    }

    /**
     * 新增积分日志
     *
     * @param appUserPointLog 积分日志
     * @return 结果
     */
    @Override
    public int insertAppUserPointLog(AppUserPointLog appUserPointLog) {
        appUserPointLog.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(appUserPointLog);
    }

    /**
     * 修改积分日志
     *
     * @param appUserPointLog 积分日志
     * @return 结果
     */
    @Override
    public int updateAppUserPointLog(AppUserPointLog appUserPointLog) {
        appUserPointLog.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(appUserPointLog);
    }

    /**
     * 批量删除积分日志
     *
     * @param ids 需要删除的积分日志主键
     * @return 结果
     */
    @Override
    public int deleteAppUserPointLogByIds(Integer[] ids) {
        return baseMapper.deleteAppUserPointLogByIds(ids);
    }

    /**
     * 删除积分日志信息
     *
     * @param id 积分日志主键
     * @return 结果
     */
    @Override
    public int deleteAppUserPointLogById(Integer id) {
        return baseMapper.deleteAppUserPointLogById(id);
    }

    @Override
    public void addPoint(Long appUserId, Integer point, String relationId, MemberPointSourceEnum from, String remark) {
        if (point <= 0) {
            return;
        }
        AppUserPointLog appUserPointLog = new AppUserPointLog();
        appUserPointLog.setAppUserId(appUserId);
        appUserPointLog.setPoint(point);
        appUserPointLog.setType("1");
        appUserPointLog.setRelationId(relationId);
        appUserPointLog.setSourceType(from.getCode());
        appUserPointLog.setRemark(remark);
        appUserPointLog.buildCreateData();
        this.insertAppUserPointLog(appUserPointLog);
        // 更新用户积分
        appUserMapper.updateUserPoint(appUserId, point);
    }

    @Override
    public void reducePoint(Long appUserId, Integer point, String relationId, MemberPointSourceEnum from, String remark) {
        if (point <= 0) {
            return;
        }
        AppUserPointLog appUserPointLog = new AppUserPointLog();
        appUserPointLog.setAppUserId(appUserId);
        appUserPointLog.setPoint(point);
        appUserPointLog.setType("2");
        appUserPointLog.setRelationId(relationId);
        appUserPointLog.setSourceType(from.getCode());
        appUserPointLog.setRemark(remark);
        appUserPointLog.buildCreateData();
        this.insertAppUserPointLog(appUserPointLog);
        // 更新用户积分
        appUserMapper.updateUserPoint(appUserId, -point);
    }

}
