package com.czg.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.EntryManager;
import com.czg.PayCst;
import com.czg.dto.resp.QueryStatusResp;
import com.czg.order.entity.ShopDirectMerchant;
import com.czg.order.service.ShopMerchantService;
import com.czg.pay.AlipayAuthInfoDto;
import com.czg.pay.NativeMerchantDTO;
import com.czg.service.order.service.ShopDirectMerchantService;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 进件查询
 *
 * @author ww
 */
@Component
@Slf4j
public class EntryManagerTask {
    @Resource
    private ShopDirectMerchantService shopDirectMerchantService;
    @Resource
    private ShopMerchantService shopMerchantService;


    // TODO 暂时不用原生进件，使用第三方进件
    //每10分钟查一次
//    @Scheduled(cron = "0 0/10 * * * ? ")
    public void run() {
//        log.info("进件查询，定时任务执行");
//        long start = System.currentTimeMillis();
//        entryManager(null);
//        log.info("进件查询，定时任务执行完毕，耗时:{}ms", System.currentTimeMillis() - start);
    }

    /**
     * 查询状态为待处理、待签约、待审核的进件
     */
    public void entryManager(Long shopId) {
        List<ShopDirectMerchant> list = shopDirectMerchantService.list(QueryWrapper.create()
                .eq(ShopDirectMerchant::getShopId, shopId)
                .in(ShopDirectMerchant::getWechatStatus, PayCst.EntryStatus.NEED_QUERY_LIST)
                .or(ShopDirectMerchant::getAlipayStatus).in(PayCst.EntryStatus.NEED_QUERY_LIST));
        if (CollUtil.isEmpty(list)) {
            return;
        }
        for (ShopDirectMerchant shopDirectMerchant : list) {
            String wechatMerchantId = "";
            String alipayMerchantId = "";
            if (PayCst.EntryStatus.NEED_QUERY_LIST.contains(shopDirectMerchant.getWechatStatus()) && StrUtil.isNotBlank(shopDirectMerchant.getWechatApplyId())) {
                QueryStatusResp wechatStatus = EntryManager.queryWechatEntryStatus(shopDirectMerchant.getWechatApplyId());
                shopDirectMerchant.setWechatStatus(wechatStatus.getStatus());
                shopDirectMerchant.setWechatErrorMsg(wechatStatus.getFailReason());
                shopDirectMerchant.setWechatSignUrl("");
                shopDirectMerchant.setWechatMerchantId(wechatStatus.getThirdMerchantId());
                if (PayCst.EntryStatus.FINISH.equals(wechatStatus.getStatus())) {
                    wechatMerchantId = wechatStatus.getThirdMerchantId();
                }
            }
            if (PayCst.EntryStatus.NEED_QUERY_LIST.contains(shopDirectMerchant.getAlipayStatus()) && StrUtil.isNotBlank(shopDirectMerchant.getAlipayOrderId())) {
                QueryStatusResp alipayStatus = EntryManager.queryAlipayEntryStatus(shopDirectMerchant.getAlipayOrderId());
                shopDirectMerchant.setAlipayStatus(alipayStatus.getStatus());
                shopDirectMerchant.setAlipayErrorMsg(alipayStatus.getFailReason());
                shopDirectMerchant.setAlipaySignUrl("");
                shopDirectMerchant.setAlipayMerchantId(alipayStatus.getThirdMerchantId());
                if (PayCst.EntryStatus.FINISH.equals(alipayStatus.getStatus())) {
                    alipayMerchantId = alipayStatus.getThirdMerchantId();
                }
            }
            shopDirectMerchantService.updateById(shopDirectMerchant);
            if (StrUtil.isNotBlank(wechatMerchantId) || StrUtil.isNotBlank(alipayMerchantId)) {
//                ShopMerchantDTO shopMerchantDTO = new ShopMerchantDTO();
//                shopMerchantDTO.setShopId(shopId);
//                shopMerchantDTO.setChannel(PayChannelCst.NATIVE);
//                shopMerchantDTO.setRelatedId(shopDirectMerchant.getShopId());
                NativeMerchantDTO nativeMerchantDTO = new NativeMerchantDTO();
                nativeMerchantDTO.setWechatMerchantId(wechatMerchantId);
                nativeMerchantDTO.setAlipayMerchantId(alipayMerchantId);
                if (StrUtil.isNotBlank(shopDirectMerchant.getAlipayAuthInfo())) {
                    AlipayAuthInfoDto alipayAuthInfoDto = JSONObject.parseObject(shopDirectMerchant.getAlipayAuthInfo(), AlipayAuthInfoDto.class);
                    nativeMerchantDTO.setAlipayAuthInfo(alipayAuthInfoDto);
                }
//                shopMerchantDTO.setNativeMerchantDTO(nativeMerchantDTO);
//                shopMerchantService.editEntry(shopMerchantDTO, false);
                shopMerchantService.upMerchant(shopDirectMerchant.getShopId(), nativeMerchantDTO);
            }
        }
    }
}