package com.xhpc.activity.task;


import com.xhpc.activity.domain.XhpcClearingHistoryOrderDomain;
import com.xhpc.activity.domain.XhpcHistoryOrderDomain;
import com.xhpc.activity.mapper.XhpcClearingHistoryOrderMapper;
import com.xhpc.activity.mapper.XhpcHistoryOrderMapper;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.utils.bean.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


@Component
@RestController
@RequestMapping("/clearing/order")
public class SyncHistoryOrderTask {


    @Resource
    XhpcHistoryOrderMapper historyOrderMapper;

    @Resource
    XhpcClearingHistoryOrderMapper clearingHistoryOrderMapper;


    /**
     * 每小时自动同步一天之前所有未同步订单
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    //@Scheduled(cron = "0 0/3 * * * ?")
    @Transactional
    @GetMapping("/syncTask")
    public void ExpiredTenantStatusTask() {
        System.out.println(" ============== 自动同步订单到清分 开始 ==========================");

        try {
            String syncedOrderIds = historyOrderMapper.selectSyncedOrderIds();
            if (StringUtils.isNotEmpty(syncedOrderIds)) {
                historyOrderMapper.updateStatusBatchByOrderIds(syncedOrderIds, 3);
            }

            boolean isFinish = false;
            int count = 0;
            while (!isFinish) {
                List<XhpcHistoryOrderDomain> unClearingDomainList = historyOrderMapper.selectUnsyncOrderList();
                if (unClearingDomainList.size() > 0) {
                    List<XhpcClearingHistoryOrderDomain> clearingHistoryOrderDomainList = new ArrayList<>();
                    StringBuilder orderIds = new StringBuilder();
                    for (XhpcHistoryOrderDomain domain : unClearingDomainList) {

                        XhpcClearingHistoryOrderDomain clearingDomain = clearingHistoryOrderMapper.selectByPrimaryKey(domain.getHistoryOrderId());

                        if(clearingDomain == null){
                            clearingDomain = new XhpcClearingHistoryOrderDomain();

                            BeanUtils.copyProperties(domain, clearingDomain);
                            clearingDomain.setClearingOrderId(domain.getHistoryOrderId());
                            clearingDomain.setCheckStatus(0);
                            clearingHistoryOrderDomainList.add(clearingDomain);
                        } else {
                            // 如果订单未审核且未清分提现，则删除原订单，并重新插入
                            if(clearingDomain.getCheckStatus() == 0 && clearingDomain.getClearingCheckoutId() == null){
                                clearingHistoryOrderMapper.deleteByPrimaryKey(clearingDomain.getClearingOrderId());

                                clearingDomain = new XhpcClearingHistoryOrderDomain();
                                BeanUtils.copyProperties(domain, clearingDomain);
                                clearingDomain.setClearingOrderId(domain.getHistoryOrderId());
                                clearingDomain.setCheckStatus(0);
                                clearingHistoryOrderDomainList.add(clearingDomain);
                            }
                        }
                        orderIds.append(",").append(domain.getHistoryOrderId());
                    }
                    clearingHistoryOrderMapper.insertBatch(clearingHistoryOrderDomainList);
                    historyOrderMapper.updateStatusBatchByOrderIds(orderIds.toString(), 3);
                } else {
                    isFinish = true;
                }
                count += unClearingDomainList.size();
            }
            System.out.println(" ============== 自动同步订单到清分，结束; 总计同步数量：" + count + " ==========================");
        } catch (Exception e) {
            System.out.println(" ============= 同步异常 ============");
            e.printStackTrace();
        }
    }

}
