package com.sckw.report.controller;

import com.alibaba.fastjson2.JSON;
import com.sckw.core.exception.SystemException;
import com.sckw.core.web.response.HttpResult;
import com.sckw.mongo.model.SckwTradeOrder;
import com.sckw.report.dao.SckwTradeOrderRepository;
import com.sckw.stream.model.SckwBusSum;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
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.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @author zk
 * @desc TODO
 * @date 2023/7/8 0008
 */
@AllArgsConstructor
@RestController
@RequestMapping("/kwFleet")
public class KwFleetController {

    @Autowired
    SckwTradeOrderRepository tradeOrderRepository;

    @Resource
    StreamBridge streamBridge;

    @GetMapping("/detailV2")
    public HttpResult selectByKey2(Long id, int method) throws SystemException {
        SckwTradeOrder order = new SckwTradeOrder();
        order.setTOrderId(id);
        order.setTOrderNo("D" + id);
        order.setContractName(new Date()+"");

        SckwBusSum busSum = new SckwBusSum();
        busSum.setBusSumType("tradeOrder");
        busSum.setMethod(method);
        busSum.setObject(order);
        streamBridge.send("sckw-busSum", JSON.toJSONString(busSum));


        for (int i =0; i<10000; i++) {
            order.setTOrderId(System.currentTimeMillis());
            order.setTOrderNo("D" + order.getTOrderId());
            order.setContractName(new Date()+"");
            order.setActualAmount(new BigDecimal(0));
            order.setContractId(1L);
            order.setContractNo("11");
            order.setEndTime(LocalDate.now());
            order.setSupplyEntId(1L);

            busSum.setBusSumType("tradeOrder");
            busSum.setMethod(method);
            busSum.setObject(order);
            streamBridge.send("sckw-busSum", JSON.toJSONString(busSum));
        }


        //根据条件查询所有并排序，且分页
        Pageable pageable = PageRequest.of(0, 2);
        Page<SckwTradeOrder> all3 = tradeOrderRepository.findAll(pageable);
        List<SckwTradeOrder> content = all3.getContent();
        for (SckwTradeOrder stu : content) {
            System.out.println(stu);
        }

        return HttpResult.ok(content);
    }
}
