package com.fs.company.controller;

import com.fs.FSAdminApplication;
import com.fs.common.config.FSSysConfig;
import com.fs.common.core.domain.R;
import com.fs.erp.domain.ErpOrder;
import com.fs.erp.domain.ErpOrderItem;
import com.fs.erp.service.IErpOrderService;
import com.fs.erp.service.impl.ErpOrderServiceImpl;
import com.fs.pay.pay.util.PayUtil;
import com.fs.store.domain.FsStoreOrder;
import com.fs.store.dto.MedicalRecordDTO;
import com.fs.store.service.IFsPrescribeService;
import com.fs.store.service.IFsStoreOrderService;
import com.fs.store.service.MedicalRecordService;
import com.fs.wx.dto.*;
import com.fs.wx.service.OrderQueryService;
import com.fs.wx.service.ShippingService;
import com.fs.wx.service.WeChatAuthService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RunWith(org.springframework.test.context.junit4.SpringRunner.class)
@SpringBootTest(classes = FSAdminApplication.class)
@Slf4j
public class CompanyControllerTest {

    @Autowired
    private FSSysConfig fsSysConfig;

    @Autowired
    private IErpOrderService erpOrderService;

    @Autowired
    private WeChatAuthService weChatAuthService;

    @Autowired
    private ShippingService shippingService;

    @Autowired
    private IFsStoreOrderService fsStoreOrderService;

    @Autowired
    private IFsPrescribeService fsPrescribeService;

    @Autowired
    private MedicalRecordService medicalRecordService;

    @Test
    public void test6(){
//        MedicalRecordDTO medicalRecordDTO = new MedicalRecordDTO();
    }
    @Test
    public void test5(){
       fsPrescribeService.generateDealImg();
    }

    @Test
    public void test4() throws ParseException {
        FsStoreOrder order=fsStoreOrderService.selectFsStoreOrderByOrderCode("1935899736686985216");
        fsStoreOrderService.createOmsOrder(order.getId());
    }

    @Test
    public void test(){
        UploadShippingInfoRequest request = new UploadShippingInfoRequest();
        OrderKey orderKey = new OrderKey();
        orderKey.setTransactionId("4200002594202504182109677796");
        orderKey.setOrderNumberType(2);
        request.setOrderKey(orderKey);
        request.setLogisticsType(1);
        request.setDeliveryMode(1);
        request.setShippingList(Arrays.asList(ShippingItem.builder()
                        .expressCompany("SF")
                        .trackingNo("SF3172486548114")
                        .contact(Contact.builder().consignorContact("17749925835").build())
                .itemDesc("测试商品").build()));
        OffsetDateTime now = OffsetDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
        String formattedTimestamp = now.format(formatter);
        request.setUploadTime(formattedTimestamp);
        Payer payer = new Payer();
        payer.setOpenid("obmfC6_pHn2aLJ6Sm2PfQJvT2IIA");
        request.setPayer(payer);
        WeChatApiResponse weChatApiResponse = shippingService.uploadShippingInfo(request);

        log.info("上传物流信息: {} ", weChatApiResponse);
    }


    @Autowired
    private OrderQueryService orderQueryService;
    @Test
    public void test2(){
        OrderQueryRequest orderQueryRequest = new OrderQueryRequest();
        orderQueryRequest.setAccount("37519015371852");
        orderQueryRequest.setUpOrderId("91913098449039003648");
        orderQueryRequest.setIsNeedUpInfo("1");

        String sign = PayUtil.sign(orderQueryRequest.toSignMap());
        orderQueryRequest.setSign(sign);

        OrderQueryResponse orderQueryResponse = orderQueryService.queryOrder(orderQueryRequest);

        log.info("获取订单信息: {} ",orderQueryResponse);
    }
    @Test
    public void addOrder() {
        ErpOrder order=new ErpOrder();
        order.setShop_code(fsSysConfig.getErpShopCode());
        order.setVip_code("10000");
        order.setPlatform_code("202211111110");
        order.setWarehouse_code("CQDS001");
        order.setExpress_code("YZJD");
        order.setPost_fee(1.00);
        order.setSeller_memo("备注");
        // order.setCurrency_code("JCZD");
        // order.setCod_fee(2.00);//到 付服务费
        order.setBuyer_memo("备注");
        order.setOrder_type_code("order");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        order.setDeal_datetime(sdf.format(new Date()));
        List<ErpOrderItem> details=new ArrayList<>();
        ErpOrderItem item=new ErpOrderItem();
        item.setItem_code("105015");
        // item.setSku_code("1122");
        item.setOrigin_price("1.00");
        item.setPrice("12.00");
        item.setQty(1);
        item.setRefund(0);
        details.add(item);
        order.setDetails(details);
        order.setReceiver_name("张三");
        order.setReceiver_mobile("15193139333");
        order.setReceiver_province("重庆市");
        order.setReceiver_city("重庆市");
        order.setReceiver_district("江北区");
        order.setReceiver_address("观音桥街道中翰名苑");
        erpOrderService.addOrder(order);
    }
}

