package cn.pluss.platform.impl;

import cn.pluss.platform.SubMerchantApplyOrderService;
import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.entity.SubMerchantApplyOrder;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.SubMerchantApplyOrderMapper;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.vo.WxCertInfoVO;
import cn.pluss.platform.wx.WxCertService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 子渠道工单信息表 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2020-08-31
 */
@Service
public class SubMerchantApplyOrderServiceImpl extends ServiceImpl<SubMerchantApplyOrderMapper, SubMerchantApplyOrder> implements SubMerchantApplyOrderService {

    @Autowired
    private MerchantChannelStatusService mcsService;

    @Autowired
    private MerchantBaseInfoService mbiService;

    @Autowired @Lazy
    private WxCertService wxCertService;

    @Override
    public List<SubMerchantApplyOrder> listNoCertData() {
        return baseMapper.selectListNoCert();
    }

    @Override
    public Page<Map<String, Object>> pageMoreDetail(Page<Map<String, Object>> page, Map<String, String> condition) {
        page.getOrders().clear();
        return baseMapper.selectPageMoreDetail(page, condition);
    }

    @Override
    public SubMerchantApplyOrder apiGetWxAuditInfo(UserApp userApp) {
        MerchantChannelStatus validData = mcsService.getValidData(userApp.getMerchantCode());
        MsgException.checkNull(validData, "暂未进件");


        WxCertInfoVO wxCertInfoVO = wxCertService.wxCertInfo(userApp.getUserId() + "", validData.getVirChannelFlag());

        SubMerchantApplyOrder smApyOrder = new SubMerchantApplyOrder();
        smApyOrder.setMerchantCode(wxCertInfoVO.getMerchantCode());
        smApyOrder.setMerchantId(wxCertInfoVO.getMerchantId());
        smApyOrder.setSubMchType("WX");
        smApyOrder.setChannelId(validData.getChannel());
        smApyOrder.setRemark(wxCertInfoVO.getRemark());
        smApyOrder.setInfoQrcode(wxCertInfoVO.getInfoQrcode());
        smApyOrder.setResultStatus(wxCertInfoVO.getStatus());
        smApyOrder.setStatus(wxCertInfoVO.getStatus());

        return smApyOrder;
    }

    @Override
    public void complete(UserApp userApp) {
        final SubMerchantApplyOrder applyOrder = new SubMerchantApplyOrder();
        applyOrder.setMerchantCode(userApp.getMerchantCode());
        applyOrder.setStatus("1");
        applyOrder.setSubMchType("WX");
        final SubMerchantApplyOrder exist = getOne(new QueryWrapper<>(applyOrder));
        if (exist == null || exist.getInfoQrcode() == null ||exist.getInfoQrcode().isEmpty()) {
            throw new MsgException("当前还未获取到商户微信认证数据");
        }

        MerchantChannelStatus mcs = mcsService.getValidData(userApp.getMerchantCode());
        if (!MerchantChannelStatus.AUTHORIZATION_STATUS_AUTHORIZED.equals(mcs.getAuthorizationStatus())) {
            mcs.setAuthorizationStatus(MerchantChannelStatus.AUTHORIZATION_STATUS_AUTHORIZED);
            mcsService.updateById(mcs);
        }

        exist.setResultStatus(SubMerchantApplyOrder.RES_STATUS_COMPLETE);
        updateById(exist);
    }
}
