package com.uas.platform.b2b.purc;

import com.uas.platform.b2b.BaseJunitTest;
import com.uas.platform.b2b.dao.PurchaseNoticeDao;
import com.uas.platform.b2b.dao.PurchaseOrderItemDao;
import com.uas.platform.b2b.erp.controller.PurchaseNotifyController;
import com.uas.platform.b2b.erp.model.PurchaseNotify;
import com.uas.platform.b2b.model.PurchaseNotice;
import com.uas.platform.b2b.model.PurchaseOrderItem;
import com.uas.platform.b2b.model.User;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class PurchaseNotifyTest extends BaseJunitTest {

    @Autowired
    private PurchaseOrderItemDao purchaseOrderItemDao;

    @Autowired
    private PurchaseNoticeDao purchaseNoticeDao;

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @PersistenceContext
    protected EntityManager em;

    @Autowired
    private PurchaseNotifyController purchaseNotifyController;

    @Transactional
    public void batchInsert(List list) {
        for(int i = 0; i < list.size(); i++) {
            em.persist(list.get(i));
            if(i % 30== 0) {
                em.flush();
                em.clear();
            }
        }
    }

    /**
     * 测试查询时间
     */
    @Test
    public void testFindTime() {
        Long orderItemId = 37053424L;
//        String orderCode = "B2BYBMP180100104";
//        Long enuu = 10041166L;
//        Short number = 1;

        String orderCode = "130000443-73";
        Long enuu = 10006098L;
        Short number = 2;
        long time1 = System.currentTimeMillis();
        List<Long> orderItemIds = purchaseOrderItemDao.findIdByEnUUAndOrderCodeAndNumber(enuu, orderCode, number);
        long time2 = System.currentTimeMillis();
        List<PurchaseOrderItem> orderItems = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(enuu, orderCode, number);
        long time3 = System.currentTimeMillis();
        System.out.println("time1: " + (time2 - time1) + " , time2: " + (time3 - time2));
    }

    /**
     * 测试保存时间
     */
    @Test
    public void testSaveTime() {
        String orderCode = "130000443-73";
        Long enuu = 10006098L;
        Short number = 2;
        List<PurchaseNotice> purchaseNotices = new ArrayList<>();
        for (int i = 0; i < 100; i ++) {
            purchaseNotices.add(createNotice());
        }

        List<PurchaseNotice> purchaseNotices2 = new ArrayList<>();
        for (int i = 0; i < 100; i ++) {
            purchaseNotices2.add(createNotice());
        }
        long time1 = System.currentTimeMillis();
        batchInsert(purchaseNotices);
        long time2 = System.currentTimeMillis();
        purchaseNoticeDao.save(purchaseNotices2);
        long time3 = System.currentTimeMillis();
        System.out.println("time1: " + (time2 - time1) + " , time2: " + (time3 - time2));
    }

    /**
     * 通过entityManager 保存实体
     * @param entities
     */
    private void persistByEntityManager(Iterable<?> entities) {
        EntityManager entityManager = null;
        EntityTransaction transaction = null;

        try {
            entityManager = entityManagerFactory.createEntityManager();
            transaction = entityManager.getTransaction();
            transaction.begin();

            Iterator iterator = entities.iterator();
            while(iterator.hasNext()){
                entityManager.persist(iterator.next());
            }
            transaction.commit();
        } catch (RuntimeException e) {
            if ( transaction != null &&
                    transaction.isActive()) {
                transaction.rollback();
            }
            throw e;
        } finally {
            if (entityManager != null) {
                entityManager.close();
            }
        }

    }

    /**
     * 创建notice
     * @return
     */
    private PurchaseNotice createNotice() {
        PurchaseNotice notice = new PurchaseNotice();
        notice.setQty((double) 1);
        notice.setDelivery(new Date());
        notice.setStatus((short) 201);
        notice.setSendStatus((short) 201);
        notice.setEndQty((double) 0);
        notice.setDate(new Date());
        notice.setEnUU(10041166L);
        notice.setErpDate(new Date());
        notice.setVendUU(10030994L);
        notice.setOrderItemId(37120085L);
        return notice;
    }

    /**
     * 测试保存100个发货提醒的全过程
     */
    @Test
    public void testSaveNotifies() {
        initSystemSession(200031316L, 10041120L);
        List<PurchaseNotify> notifies = createNotifies(100);
        String str = FlexJsonUtils.toJsonArray(notifies);
        System.out.println(str);
        long time1 = System.currentTimeMillis();
        try {
            purchaseNotifyController.saveNotifies(str);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        long time2 = System.currentTimeMillis();
        System.out.println("invoke time: " + (time2 - time1) + "ms.");
    }

    /**
     * 测试获取懒加载属性
     */
    @Test
    public void testLazyOrderItem() {
        List<PurchaseNotice> notices = purchaseNoticeDao.findByEnUUAndSourceId(10006098, 13265);
        PurchaseNotice notice = notices.get(0);
        System.out.println(notice.getOrderItem().getOrder().getVendUserUU());
    }

    /**
     * 模拟创建多个notify
     * @param size
     * @return
     */
    private List<PurchaseNotify> createNotifies(int size) {
        List<PurchaseNotify> notifies = new ArrayList<PurchaseNotify>();
        long venduu = 10000666L;
        String orderCode = "MP180500107";
        short orderDetno = 1;
        for (long i = 1; i <= size; i ++) {
            notifies.add(createNotify(i, venduu, orderCode, orderDetno));
        }
        return notifies;
    }

    /**
     * 模拟创建一个notify
     * @param id
     * @param venduu
     * @param orderCode
     * @param orderDetno
     * @return
     */
    private PurchaseNotify createNotify(Long id, long venduu, String orderCode, short orderDetno) {
        PurchaseNotify notify = new PurchaseNotify();
        notify.setPn_delivery(new Date());
        notify.setPn_endqty(0D);
        notify.setPn_id(id);
        notify.setPn_indate(new Date());
        notify.setPn_ordercode(orderCode);
        notify.setPn_orderdetno(orderDetno);
        notify.setPn_qty(1D);
        notify.setPr_zxbzs(1D);
        notify.setVe_uu(venduu);
        return notify;
    }

}
