package cn.pluss.platform.merchantOrderSplit.impl;

import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.MerchantOrderSplitMapper;
import cn.pluss.platform.mercSplitSetting.MerchantSplitSettingService;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.merchantOrder.BaseMercSplitService;
import cn.pluss.platform.merchantOrderSplit.MerchantOrderSplitDetailService;
import cn.pluss.platform.merchantOrderSplit.MerchantOrderSplitService;
import cn.pluss.platform.notify.NotifyMessageService;
import cn.pluss.platform.vo.SplitOrderVo;
import cn.pluss.platform.yinsheng.YsOldSrcMerchantNo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单分账信息表 服务实现类
 * </p>
 *
 * @author bzg
 * @since 2021-11-26
 */
@Service
@Slf4j
public class MerchantOrderSplitServiceImpl extends ServiceImpl<MerchantOrderSplitMapper, MerchantOrderSplit> implements MerchantOrderSplitService {

    @Setter(onMethod_ = {@Autowired, @Qualifier("ysMercSplitService")})
    private BaseMercSplitService ysMercSplitService;

    @Resource
    private MerchantSplitSettingService merchantSplitSettingService;

    @Resource
    private MerchantOrderSplitDetailService merchantOrderSplitDetailService;

    @Resource
    private MerchantChannelStatusService channelStatusService;

    @Resource
    private MerchantBaseInfoService merchantBaseInfoService;

    @Resource
    private NotifyMessageService notifyMessageService;
    @Override
    public void checkMerchantOrderSplit(MerchantOrder order) {
        log.info("银盛订单分账开始，待分账的订单号：{}",order.getOrderNumber());
        if(order.getOrderNumber().startsWith("YS") || "4".equals(order.getAisleSwitch())){
            String merchantCode = order.getMerchantCode();
            MerchantBaseInfo merchant = merchantBaseInfoService.getMerchantBaseInfoByMerchantCode(merchantCode);
            MerchantChannelStatus channel = channelStatusService.getByMerchantCode(order.getMerchantCode(), 4);
            //TODO 收银呗主体都需要走分账体系 判断是否自定义分账接口
            if(YsOldSrcMerchantNo.SYB.getValue().equals(channel.getSrcMerchantNo()) && merchant.getSplitFlag() == 1){
                QueryWrapper<MerchantSplitSetting> queryWrapper = new QueryWrapper<MerchantSplitSetting>()
                        .eq("chieMerchantCode",merchantCode);
                List<MerchantSplitSetting> list = merchantSplitSettingService.list(queryWrapper);
                try {
                    MerchantOrderSplit split = null;
                    if(list.isEmpty()){
                        if(channel != null){
                            List<MerchantSplitSetting> newList = new ArrayList<>(1);
                            MerchantSplitSetting setting = new MerchantSplitSetting(channel);
                            newList.add(setting);
                            split = ysMercSplitService.split(order, newList,false);
                        }
                    }else{
                        split = ysMercSplitService.split(order, list,true);
                    }
                    if(split != null){
                        split.setMerchantName(order.getMerchantName());
                        split.setMerchantId(channel.getMerchantId());
                        if(!list.isEmpty()){
                            split.setNotifyUrl(list.get(0).getNotifyUrl());
                        }
                        save(split);
                        List<MerchantOrderSplitDetail> detailList = split.getDetailList();
                        if(detailList != null && detailList.size() > 0){
                            for (MerchantOrderSplitDetail detail:detailList) {
                                MerchantBaseInfo divMerchant = merchantBaseInfoService.getMerchantBaseInfoByMerchantCode(detail.getMerchantCode());
                                if(divMerchant != null){
                                    detail.setMerchantName(divMerchant.getAlias());
                                }
                                if(split.getDivStatus() == -1){
                                    detail.setStatus(2);
                                }else{
                                    detail.setStatus(1);
                                }
                            }
                            merchantOrderSplitDetailService.saveBatch(detailList);
                            notifyMessageService.sendLowerSplitNotify(split);
                        }
                    }
                }catch (Exception e){
                    MsgException.throwException(e.getMessage());
                }
            }
        }
    }

    @Override
    public MerchantOrderSplit getByMercOrderNo(String mercOrderNo) {
        return baseMapper.getByMercOrderNo(mercOrderNo);
    }

    @Override
    public MerchantOrderSplit getByOrderNumber(String ordNo) {
        return baseMapper.getByOrderNumber(ordNo);
    }

    @Override
    public SplitOrderVo resultChannelParam(MerchantOrderSplit order, List<MerchantOrderSplitDetail> splitDetailList) {
        MerchantBaseInfo merchant = merchantBaseInfoService.getMerchantBaseInfoByMerchantCode(order.getMerchantCode());
        SplitOrderVo vo = new SplitOrderVo(order,splitDetailList);
        vo.setSign(merchant.getPlatformSign(vo));
        return vo;
    }
}
