package com.hzs.activity.pick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hzs.activity.pick.mapper.AcRetailPickMapper;
import com.hzs.activity.pick.param.AcRetailPickParam;
import com.hzs.activity.pick.service.IAcRetailPickMemberLogService;
import com.hzs.activity.pick.service.IAcRetailPickService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.domain.activity.pick.AcRetailPick;
import com.hzs.common.domain.activity.pick.AcRetailPickMemberLog;
import com.hzs.common.domain.activity.pick.ext.AcRetailPickExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.common.domain.sale.order.SaOrderItems;
import com.hzs.retail.sale.controller.service.IRetailOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 新零售会员提货数量 服务实现类
 *
 * @author hzs
 * @since 2025-01-09
 */
@Service
public class AcRetailPickServiceImpl extends ServiceImpl<AcRetailPickMapper, AcRetailPick> implements IAcRetailPickService {

    @Autowired
    private IRetailOrderService iRetailOrderService;
    @Autowired
    private IAcRetailPickMemberLogService iAcRetailPickMemberLogService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String payOrder(AcRetailPick retailPick, SaOrder saOrder, List<SaOrderItems> saOrderItemsList) {
        if (baseMapper.updatePickQuantity(retailPick.getPkMember(), retailPick.getPkId(), retailPick.getUsedQuantity(), retailPick.getPkMember()) > 0) {
            // 更新提货数量成功

            // 保存订单、订单明细
            iRetailOrderService.savePickOrder(saOrder, saOrderItemsList);

            // 添加提货日志
            iAcRetailPickMemberLogService.save(AcRetailPickMemberLog.builder()
                    .pkMember(retailPick.getPkMember())
                    .pickQuantity(retailPick.getUsedQuantity())
                    .pickTime(saOrder.getCreationTime())
                    .pickOrder(saOrder.getPkId())
                    .pkCountry(saOrder.getPkCountry())
                    .pkRetailPick(retailPick.getPkId())
                    .build());

            return null;
        }
        return "确认购买失败，请重试";
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean cancelRetailPickOrder(Long pkOrder, Long userId) {
        LambdaQueryWrapper<AcRetailPickMemberLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AcRetailPickMemberLog::getPickOrder, pkOrder);
        AcRetailPickMemberLog retailPickMemberLog = iAcRetailPickMemberLogService.getOne(queryWrapper);
        if (null != retailPickMemberLog) {
            // 更新提货数量（此方法为统一的扣减，传入负数则为添加）
            if (baseMapper.updatePickQuantity(null, retailPickMemberLog.getPkRetailPick(), retailPickMemberLog.getPickQuantity() * -1, userId) > 0) {
                // 删除提货日志
                LambdaUpdateWrapper<AcRetailPickMemberLog> logUpdateWrapper = new LambdaUpdateWrapper<>();
                logUpdateWrapper.eq(AcRetailPickMemberLog::getPkId, retailPickMemberLog.getPkId());
                logUpdateWrapper.set(AcRetailPickMemberLog::getDelFlag, EDelFlag.DELETE.getValue());
                iAcRetailPickMemberLogService.update(logUpdateWrapper);
            }
            return true;
        }
        return false;
    }

    @Override
    public int handlePick(Long pkBaseId, Date nowDate) {
        return baseMapper.handlePick(pkBaseId, nowDate);
    }

    @Override
    public List<AcRetailPickExt> listData(AcRetailPickParam param) {
        return baseMapper.listData(param);
    }

}
