package com.microservices.zone.member.service.impl;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.datascope.annotation.DataScope;
import com.microservices.zone.detail.service.IZoneDetailService;
import com.microservices.zone.member.domain.points.ZonePointsTransaction;
import com.microservices.zone.member.domain.vo.ZonePointsTransactionInputVo;
import com.microservices.zone.member.domain.vo.ZonePointsTransactionUpdateVo;
import com.microservices.zone.member.mapper.ZonePointsTransactionMapper;
import com.microservices.zone.member.service.IZonePointsTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 专区积分流水Service业务层处理（合并事件+流水）
 *
 * @author otto
 */
@Service
public class ZonePointsTransactionServiceImpl implements IZonePointsTransactionService {
    @Autowired
    private ZonePointsTransactionMapper zonePointsTransactionMapper;

    @Autowired
    private IZoneDetailService zoneDetailService;

    /**
     * 查询专区积分流水
     *
     * @param id 专区积分流水主键
     * @return 专区积分流水
     */
    @Override
    public ZonePointsTransaction selectZonePointsTransactionById(Long id) {
        ZonePointsTransaction zonePointsTransaction = zonePointsTransactionMapper.selectZonePointsTransactionById(id);
        if (zonePointsTransaction == null) {
            throw new ServiceException("该积分流水不存在或无权限访问(积分流水id[" + id + "])");
        }
        return zonePointsTransaction;
    }

    /**
     * 查询专区积分流水列表
     *
     * @param zonePointsTransaction 专区积分流水
     * @return 专区积分流水
     */
    @Override
    @DataScope(deptAlias = "zpt")
    public List<ZonePointsTransaction> selectZonePointsTransactionList(ZonePointsTransaction zonePointsTransaction) {
        return zonePointsTransactionMapper.selectZonePointsTransactionList(zonePointsTransaction);
    }

    /**
     * 新增专区积分流水
     *
     * @param inputVo 专区积分流水
     * @return 结果
     */
    @Override
    public int insertZonePointsTransaction(ZonePointsTransactionInputVo inputVo) {
        ZonePointsTransaction zonePointsTransaction = inputVo.toZonePointsTransaction();
        zonePointsTransaction.setCreateTime(DateUtils.getNowDate());
        return zonePointsTransactionMapper.insertZonePointsTransaction(zonePointsTransaction);
    }

    /**
     * 修改专区积分流水
     *
     * @param updateVo 专区积分流水
     * @return 结果
     */
    @Override
    public int updateZonePointsTransaction(ZonePointsTransactionUpdateVo updateVo) {
        selectZonePointsTransactionById(updateVo.getId());
        ZonePointsTransaction zonePointsTransaction = updateVo.toZonePointsTransaction();
        return zonePointsTransactionMapper.updateZonePointsTransaction(zonePointsTransaction);
    }

    /**
     * 删除专区积分流水信息
     *
     * @param id 专区积分流水主键
     * @return 结果
     */
    @Override
    public int deleteZonePointsTransactionById(Long id) {
        selectZonePointsTransactionById(id);
        return zonePointsTransactionMapper.deleteZonePointsTransactionById(id);
    }

    /**
     * 根据事件ID查询流水列表
     *
     * @param eventId 事件ID
     * @return 专区积分流水列表
     */
    @Override
    public List<ZonePointsTransaction> selectZonePointsTransactionByEventId(String eventId) {
        return zonePointsTransactionMapper.selectZonePointsTransactionByEventId(eventId);
    }

    /**
     * 根据账户ID查询流水列表
     *
     * @param accountId 积分账户ID
     * @return 专区积分流水列表
     */
    @Override
    public List<ZonePointsTransaction> selectZonePointsTransactionByAccountId(Long accountId) {
        return zonePointsTransactionMapper.selectZonePointsTransactionByAccountId(accountId);
    }

    /**
     * 根据用户ID和专区ID查询流水列表
     *
     * @param userId 用户ID
     * @param zoneId 专区ID
     * @return 专区积分流水列表
     */
    @Override
    public List<ZonePointsTransaction> selectZonePointsTransactionByUserIdAndZoneId(Long userId, Long zoneId) {
        return zonePointsTransactionMapper.selectZonePointsTransactionByUserIdAndZoneId(userId, zoneId);
    }

    /**
     * 根据专区ID获取当前用户积分明细（分页）
     *
     * @param zoneId 专区ID
     * @param userId 用户ID
     * @param transactionType 交易类型（可选）
     * @return 专区积分流水列表
     */
    @Override
    @DataScope(deptAlias = "zpt")
    public List<ZonePointsTransaction> selectMyPointsTransactionDetailsByZone(Long zoneId, Long userId, String transactionType) {
        return zonePointsTransactionMapper.selectMyPointsTransactionDetailsByZone(zoneId, userId, transactionType);
    }

    /**
     * 查询当前用户流水列表（合并后直接从流水表查询，所有业务字段已包含）
     *
     * @param userId 用户ID
     * @param zoneId 专区ID
     * @return 专区积分流水列表
     */
    @Override
    public List<ZonePointsTransaction> selectCurrentUserTransactionWithEvent(Long userId, Long zoneId) {
        return zonePointsTransactionMapper.selectCurrentUserTransactionWithEvent(userId, zoneId);
    }
}