package com.reptileclient.jlifeapp.service;


import com.reptileclient.jlifeapp.dao.hm.HmBookOrderMapper;
import com.reptileclient.jlifeapp.dao.hm.HmBookTradeOrderMapper;
import com.reptileclient.jlifeapp.dao.hm.HmPaymentOrderMapper;
import com.reptileclient.jlifeapp.entity.hm.HmBookOrder;
import com.reptileclient.jlifeapp.entity.hm.HmBookTradeOrder;
import com.reptileclient.jlifeapp.entity.hm.HmPaymentOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

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

/**
 * 20230601因为盒马手机号加密 修复所有号码
 */
@EnableAsync
@Slf4j
@Service
public class HmEncryptPhoneRepairService {

    @Autowired
    private HmPhoneDecryptService phoneDecryptService;

    @Resource
    private HmPaymentOrderMapper paymentOrderMapper;

    @Resource
    private HmBookTradeOrderMapper bookTradeOrderMapper;

    @Resource
    private HmBookOrderMapper bookOrderMapper;

    //每日最大修复手机号数量
    private int maxRepairNum = 600;


    @Async
    public void repair() {


        log.info("开始修复支付单");
        // 修复支付单
        List<HmPaymentOrder> paymentOrderList = paymentOrderMapper.selectEncryptPhone();
        int size = 0;
        log.info("支付单数量:" + paymentOrderList.size());

        for (HmPaymentOrder paymentOrder : paymentOrderList) {
            log.info("修复支付单:" + paymentOrder.getPaymentNo());
            String phone = phoneDecryptService.getPhoneByTradeNo("buyerPhone", paymentOrder.getPaymentNo(), "SERVICE_PAYMENT_ORDER");
            if (phone != null) {
                paymentOrder.setBuyerPhone(phone);
                paymentOrderMapper.updateByPrimaryKeySelective(paymentOrder);
            }
            size++;
            if (size > maxRepairNum) {
                log.info("支付单数量解密手机号超过300 脚本停止");
                break;
            }
            log.info("修复支付单完成:" + paymentOrder.getPaymentNo());
        }

//        log.info("修复服务单");
//        List<HmBookOrder> bookOrders = bookOrderMapper.selectEncrypt();
//        for (HmBookOrder bookOrder : bookOrders) {
//            String phone = phoneDecryptService.getPhoneByTradeNo("phone", bookOrder.getBookOrderNo().toString(), "BOOK_ORDER");
//            if (phone != null) {
//                bookOrder.setPhone(phone);
//                bookOrderMapper.updateByPrimaryKeySelective(bookOrder);
//            }
//        }

        //修复技师作业单
        this.repairTradeOrder(size);
    }

    @Async
    public void repairTradeOrder(int num) {
        log.info("修复技师作业单-每日");
        List<HmBookTradeOrder> tradeOrders = bookTradeOrderMapper.selectEncrypt();

        for (HmBookTradeOrder tradeOrder : tradeOrders) {
            String phone = phoneDecryptService.getPhoneByTradeNo("userPhone", tradeOrder.getBookTradeNo().toString(), "BOOK_TRADE");
            if (phone != null) {
                tradeOrder.setUserPhone(phone);
                bookTradeOrderMapper.updateByPrimaryKeySelective(tradeOrder);
            }
            num++;
            if (num > maxRepairNum) {
                log.info("技师作业单解密手机号数量超过300 脚本停止");
                break;
            }

        }
    }

}
