import com.alibaba.excel.EasyExcel;
import com.bailuntec.domain.constant.CommonConstant;
import com.bailuntec.domain.entity.DcBaseStock;
import com.bailuntec.domain.entity.JobPointLog;
import com.bailuntec.domain.example.DcBaseStockExample;
import com.bailuntec.excel.DcAutoTurnoverExcel;
import com.bailuntec.excel.ExcelListener;
import com.bailuntec.job.AutoTurnoverJob;
import com.bailuntec.mapper.DcBaseStockMapper;
import com.bailuntec.utils.SessionUtil;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.executor.ShardingContexts;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

public class AutoTurnoverTest {
    //    @Test
    public void test1() throws Exception {
        LocalDate parse = LocalDate.parse("2018-01-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate parse1 = LocalDate.parse("2018-01-02", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(parse.isBefore(parse1));
        System.out.println(parse.isAfter(parse1));
        System.out.println(parse.isEqual(parse1));
        if (parse.isBefore(parse1)) {
            System.out.println(parse.compareTo(parse1));
        }
    }

    @Test
    public void test12() throws Exception {
        System.out.println(LocalDateTime.of(1990, 1, 1, 0, 0));
    }

    @Test
    public void test10() throws Exception {
        int interval = Period.between(LocalDate.now().minusDays(1), LocalDate.now()).getDays();
        int interval1 = Period.between(LocalDate.now(), LocalDate.now().minusDays(2)).getDays();
        int interval2 = Period.between(LocalDate.now(), LocalDate.now()).getDays();
        System.out.println(interval);
        System.out.println(interval1);
        System.out.println(interval2);
    }

    @Test
    public void test() throws Exception {
        // 生成周转
        String queryTime = DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT).format(LocalDate.now());
        AutoTurnoverJob autoTurnoverJob = new AutoTurnoverJob();
        DcBaseStock dcBaseStock = null;
        try {
            DcBaseStockMapper mapper = SessionUtil.getSession().getMapper(DcBaseStockMapper.class);
            dcBaseStock = mapper.selectOneByExample(DcBaseStockExample.newAndCreateCriteria().andBailunSkuEqualTo("229354801").andWarehouseCodeEqualTo("YHUSFBA").example());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SessionUtil.closeSession();
        }
        autoTurnoverJob.autoTurnoverFromStock(queryTime, dcBaseStock);
    }

    @Test
    public void test22() throws Exception {
        String queryTime = DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT).format(LocalDate.now());
        AutoTurnoverJob autoTurnoverJob = new AutoTurnoverJob();
        List<DcBaseStock> dcBaseStockList = null;
        try {
            DcBaseStockMapper mapper = SessionUtil.getSession().getMapper(DcBaseStockMapper.class);
            dcBaseStockList = mapper.listInStockNotInAutoTurnover();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SessionUtil.closeSession();
        }
        for (DcBaseStock dcBaseStock : dcBaseStockList) {
            autoTurnoverJob.autoTurnoverFromStock(queryTime, dcBaseStock);
        }
    }

    @Test
    public void test3() throws Exception {
        String queryTime = DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT).format(LocalDate.now());
        AutoTurnoverJob autoTurnoverJob = new AutoTurnoverJob();
        List<DcBaseStock> dcBaseStockList = null;
        try {
            DcBaseStockMapper mapper = SessionUtil.getSession().getMapper(DcBaseStockMapper.class);
            dcBaseStockList = mapper.selectByExample(DcBaseStockExample.newAndCreateCriteria().example());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SessionUtil.closeSession();
        }
        for (DcBaseStock dcBaseStock : dcBaseStockList) {
            autoTurnoverJob.autoTurnoverFromStock(queryTime, dcBaseStock);
        }
    }

    @Test
    public void test4() {
        AutoTurnoverJob autoTurnoverJob = new AutoTurnoverJob();
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "");
        ShardingContext shardingContext = new ShardingContext(new ShardingContexts("", "", 1, "INCREMENT", map), 0);
        JobPointLog jobPointLog = new JobPointLog("", 1, 400, 1, 1, LocalDateTime.now().minusSeconds(1), LocalDateTime.now());
        autoTurnoverJob.executeJob(shardingContext, jobPointLog);
    }


    @Test
    public void test55() {
        LocalDateTime recordTime = LocalDateTime.now();
        LocalDateTime finalEstimatedArrivalTime = LocalDateTime.now().minusDays(1);
        int interval = Period.between(recordTime.toLocalDate(), finalEstimatedArrivalTime.toLocalDate()).getDays();
        System.out.println(interval);
    }

    @Test
    public void test66() {

    }


    @Test
    public void testDcAutoTurnover() {
        String filePath = "/Users/huluobin/Library/Containers/com.tencent.WeWorkMac/Data/Library/Application Support/WXWork/Data/1688853793439460/Cache/File/2020-08/实际冗余测试815.xlsx";
        EasyExcel.read(filePath, DcAutoTurnoverExcel.class, new ExcelListener()).sheet().doRead();

    }

    //949406801 GZBLWH
    @Test
    public void testXX() {
        DcBaseStock dcBaseStock = SessionUtil.getSession().getMapper(DcBaseStockMapper.class)
                .selectOneByExample(DcBaseStockExample.newAndCreateCriteria()
                        .andBailunSkuEqualTo("950136209")
                        .andWarehouseCodeEqualTo("GZBLWH")
                        .example());

        try {
            AutoTurnoverJob autoTurnoverJob = new AutoTurnoverJob();
            autoTurnoverJob.autoTurnoverFromStock(DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT).format(LocalDate.now()), dcBaseStock);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testBigDecimal() {

        DcBaseStock dcBaseStock = new DcBaseStock();
        dcBaseStock.setInWarehouse(1);
        this.add(dcBaseStock);
        System.out.println(dcBaseStock.getInWarehouse());
    }

    public void add(DcBaseStock dcBaseStock) {
        dcBaseStock.setInWarehouse(2);
    }
}
