package cn.pluss.platform.transfer.impl;

import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.entity.MerchantOrder;
import cn.pluss.platform.entity.MerchantStore;
import cn.pluss.platform.entity.MerchantTransfer;
import cn.pluss.platform.mapper.MerchantTransferMapper;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.merchantStore.MerchantStoreService;
import cn.pluss.platform.transfer.MerchantTransferService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Djh
 * @since 2020-09-04
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MerchantTransferServiceImpl extends ServiceImpl<MerchantTransferMapper, MerchantTransfer> implements MerchantTransferService {

    private final MerchantChannelStatusService merchantChannelStatusService;
    private final MerchantStoreService merchantStoreService;
    private final MerchantOrderService merchantOrderService;

    /**
     * 保存打款记录
     * @param order
     * @param fee
     */
    @Override
    public void saveTransferInfo(MerchantOrder order,BigDecimal fee) {
        log.info("===============>进入保存营销转账打款记录操作，转账商户简称：{}，营销转账金额：{}元<==============", order.getMerchantName(),fee);
        log.info("===============>备注：营销补贴金额不足0.01元，不支持打款<==============");
        if(fee.doubleValue() >= 0.01d){
            String merchantCode = order.getMerchantCode();
            MerchantStore merchantStore = new MerchantStore().setMerchantCode(merchantCode);
            merchantStore = merchantStoreService.queryMerchantStore(merchantStore);
            if(!"1".equals(merchantStore.getIsMarket()) || order.getConsumeFee() > 100d){
                order.setMarketAmt(new BigDecimal("0.0"));
                merchantOrderService.saveOrUpdate(order);
                log.info("=====不符合活动营销的条件=====");
                return;
            }

            MerchantChannelStatus channel = merchantChannelStatusService.getByMerchantCode(merchantCode);
            if(channel != null){
                String orderNo = "ZZ000"+ StringUtil.getBillno();
                MerchantTransfer transfer = new MerchantTransfer();
                transfer.setMerchantCode(merchantCode);
                transfer.setAlias(order.getMerchantName());
                transfer.setChannelNo(channel.getMerchantId());
                transfer.setChannelId(channel.getChannel());
                transfer.setAmount(fee);
                transfer.setContent("营销转账");
                transfer.setCreateDt(new Date());
                transfer.setOrderNo(orderNo);
                //Map<String, Object> resultMap = null;
                /*try {
                    resultMap  = sxfPayService.orgTransfer(transfer);
                    if("0".equals(resultMap.get("code"))){
                        transfer.setRemark(resultMap.get("msg").toString());
                    }
                    Map<String,String> data = (Map<String,String>)resultMap.get("data");
                    transfer.setTransNo(data.get("transactionNo"));
                    if(data.get("transactionTime") != null){
                        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                        transfer.setTransTime(format.parse(data.get("transactionTime")));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("===================>请求随行付打款接口异常，异常信息:{}",e.getMessage());
                }*/
                this.baseMapper.insert(transfer);
            }
        }
    }
}
