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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.activity.tree.mapper.SaTreeOrderMapper;
import com.hzs.activity.tree.param.TreeOrderParam;
import com.hzs.activity.tree.service.ISaTreeOrderService;
import com.hzs.activity.tree.vo.SaTreeOrderVO;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EOrderPrefix;
import com.hzs.common.core.enums.EPayStatus;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.core.utils.CommonUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.domain.activity.ext.SaTreeOrderExt;
import com.hzs.common.domain.activity.tree.SaTreeOrder;
import com.hzs.member.account.IMemberTradeServiceApi;
import com.hzs.member.account.dto.BusinessCommissionDTO;
import com.hzs.member.account.dto.MemberAmountDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 沙棘树活动购买订单表 服务实现类
 *
 * @author zhangjing
 * @since 2023-06-16
 */
@Service
public class SaTreeOrderServiceImpl extends ServiceImpl<SaTreeOrderMapper, SaTreeOrder> implements ISaTreeOrderService {

    @DubboReference
    private IMemberTradeServiceApi iMemberTradeServiceApi;

    /**
     * 查询种植公益列表
     *
     * @param treeOrderParam
     * @return
     */
    @Override
    public List<SaTreeOrderExt> selectByList(TreeOrderParam treeOrderParam) {
        return baseMapper.selectByList(treeOrderParam);
    }

    /**
     * @description: 查询生效的活动
     * @author: zhang jing
     * @date: 2023/6/17 10:37
     * @param: [saTreeOrderVO]
     * @return: com.hzs.activity.tree.vo.SaTreeOrderVO
     **/
    @Override
    public SaTreeOrderVO queryTreeActivity(SaTreeOrderVO saTreeOrderVO) {
        return baseMapper.queryTreeActivity(saTreeOrderVO);
    }

    /**
     * @description: 购买沙棘树
     * @author: zhang jing
     * @date: 2023/6/17 14:09
     * @param: [saTreeOrderVO]
     * @return: java.lang.Boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean shoppingTree(SaTreeOrderVO saTreeOrderVO, BigDecimal consumeBalance,
                                BigDecimal bonusBalance, BigDecimal cashBalance) {
        BusinessCommissionDTO businessCommissionDTO = new BusinessCommissionDTO();
        List<MemberAmountDTO> memberAmountDTOList = new ArrayList<>();
        MemberAmountDTO memberAmountDTO = new MemberAmountDTO();
        memberAmountDTO.setPkMember(saTreeOrderVO.getPkMember());
        memberAmountDTO.setTradeAmount(consumeBalance);
        memberAmountDTOList.add(memberAmountDTO);
        businessCommissionDTO.setPkCountry(saTreeOrderVO.getPkCountry());
        //扣消费
        businessCommissionDTO.setPkAccount(saTreeOrderVO.getConsume());
        businessCommissionDTO.setMemberAmountDTOList(memberAmountDTOList);
        R<Boolean> b1 = iMemberTradeServiceApi.businessCommissionSubmit(businessCommissionDTO);
        if (!b1.getData()) {
            throw new ServiceException(TransactionUtils.getContent(ConfigMsgConstants.DEDUCTION_FAILED));
        }
        if (bonusBalance.compareTo(BigDecimal.ZERO) > 0) {
            //扣奖金
            memberAmountDTO.setPkMember(saTreeOrderVO.getPkMember());
            memberAmountDTO.setTradeAmount(bonusBalance);
            memberAmountDTOList.clear();
            memberAmountDTOList.add(memberAmountDTO);
            businessCommissionDTO.setPkAccount(saTreeOrderVO.getBonus());
            businessCommissionDTO.setMemberAmountDTOList(memberAmountDTOList);
            R<Boolean> b2 = iMemberTradeServiceApi.businessCommissionSubmit(businessCommissionDTO);
            if (!b2.getData()) {
                throw new ServiceException(TransactionUtils.getContent(ConfigMsgConstants.DEDUCTION_FAILED));
            }
        }
        if (cashBalance.compareTo(BigDecimal.ZERO) > 0) {
            //扣现金
            memberAmountDTO.setPkMember(saTreeOrderVO.getPkMember());
            memberAmountDTO.setTradeAmount(cashBalance);
            memberAmountDTOList.clear();
            memberAmountDTOList.add(memberAmountDTO);
            businessCommissionDTO.setPkAccount(saTreeOrderVO.getCash());
            businessCommissionDTO.setMemberAmountDTOList(memberAmountDTOList);
            R<Boolean> b3 = iMemberTradeServiceApi.businessCommissionSubmit(businessCommissionDTO);
            if (!b3.getData()) {
                throw new ServiceException(TransactionUtils.getContent(ConfigMsgConstants.DEDUCTION_FAILED));
            }
        }

        SaTreeOrder saTreeOrder = BeanUtil.copyProperties(saTreeOrderVO, SaTreeOrder.class);
        // 生成订单编号
        String orderCode = CommonUtil.createSerialNumber(EOrderPrefix.ORDER_CODE);
        saTreeOrder.setOrderCode(orderCode);
        saTreeOrder.setQuantity(saTreeOrderVO.getShopQuantity());
        saTreeOrder.setOrderStatus(EPayStatus.PAID.getValue());
        saTreeOrder.setPayTime(DateUtils.currentDateTime());
        saTreeOrder.setCreationTime(DateUtils.currentDateTime());
        baseMapper.insert(saTreeOrder);
        return true;
    }

}
