/*
 * Copyright [2022] [https://www.baiduc.com]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.baiduc.com
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.baiduc.com
 */
package vip.xiaonuo;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import vip.xiaonuo.erp.entity.ErpBdOrg;
import vip.xiaonuo.erp.entity.caigou.CaiGouRuKu;
import vip.xiaonuo.erp.entity.kucun.ChenPinChuKu;
import vip.xiaonuo.erp.entity.kucun.WuLiaoKuMingXi;
import vip.xiaonuo.erp.entity.kucun.YuanCaiLiaoKuCun;
import vip.xiaonuo.erp.entity.xiaoshou.*;
import vip.xiaonuo.erp.mapper.ErpBdOrgMapper;
import vip.xiaonuo.money.entity.MoneyOverdue;
import vip.xiaonuo.money.entity.MoneyProduce;
import vip.xiaonuo.money.mapper.MoneyOverdueMapper;
import vip.xiaonuo.money.mapper.MoneyProduceMapper;
import vip.xiaonuo.sale.entity.*;
import vip.xiaonuo.sale.mapper.*;
import vip.xiaonuo.warehouse.entity.*;
import vip.xiaonuo.warehouse.mapper.*;

import java.util.Date;
import java.util.List;

/**
 * 主测试类
 *
 * @author xuyuxiang
 * @date 2022/9/17 17:09
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class XiaoShouTest {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private SaleDetailMapper detailMapper;
    @Autowired
    private ErpBdOrgMapper erpBdOrgMapper;

    @Autowired
    private SaleCustomerMapper customerMapper;
    @Autowired
    private SaleCostMapper costMapper;

    @Autowired
    private SaleDeliverMapper deliverMapper;
    @Autowired
    private SaleInvoiceMapper invoiceMapper;

    @Autowired
    private SaleOverdueMapper overdueMapper;
    @Autowired
    private SalePriceMapper priceMapper;
    @Autowired
    private SaleProduceMapper produceMapper;

    @Autowired
    private SaleOrderMapper orderMapper;


    @Autowired
    private SaleTargetMapper targetMapper;

    @Autowired
    private MoneyOverdueMapper moneyOverdueMapper;
    @Autowired
    private SaleSalesmanMapper salesmanMapper;


    @Test
    public void  test1(){
            ResponseEntity<List<XiaoShouWanChenLvJinE>> response = restTemplate.exchange(
                    "http://127.0.0.1:9200/erp-XiaoShou/getWanChengLv?date=",
                    HttpMethod.GET,
                    null,
                    new ParameterizedTypeReference<List<XiaoShouWanChenLvJinE>>() {}
            );
            List<XiaoShouWanChenLvJinE> forObjects = response.getBody();
            System.out.println(forObjects);
        for (XiaoShouWanChenLvJinE forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFSALEORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            SaleDetail saleDetail = new SaleDetail();
            saleDetail.setCreateTime(forObject.getFDATE());
            saleDetail.setCustomer(forObject.getKeHuName());
            saleDetail.setOrgId(forgid.getOrgId());
            saleDetail.setProduceName(forObject.getWuLiaoName());
            saleDetail.setSaleContraceOrder(forObject.getFBILLNO());
            saleDetail.setSaleMoney(String.format("%.2f",forObject.getFALLAMOUNTLC()));
            saleDetail.setSaleNumber(String.format("%.2f",forObject.getFQTY()));
            saleDetail.setSalesman(forObject.getFNAME());
            detailMapper.insert(saleDetail);


            SaleCustomer saleCustomer = new SaleCustomer();
            saleCustomer.setCreateTime(forObject.getFDATE());
            saleCustomer.setCustomer(forObject.getKeHuName());
            saleCustomer.setSaleMoney(String.format("%.2f",forObject.getFALLAMOUNTLC()));
            saleCustomer.setOrgId(forgid.getOrgId());
            customerMapper.insert(saleCustomer);

            SalePrice salePrice = new SalePrice();
            salePrice.setCreateTime(forObject.getFDATE());
            salePrice.setOrgId(forgid.getOrgId());
            salePrice.setPrice(String.format("%.4f",forObject.getXiaoShouDanJia()));
            salePrice.setProduceName(forObject.getWuLiaoName());
            priceMapper.insert(salePrice);

            SaleProduce saleProduce = new SaleProduce();
            saleProduce.setCreateTime(forObject.getFDATE());
            saleProduce.setOrgId(forgid.getOrgId());
            saleProduce.setPrice(String.format("%.4f",forObject.getXiaoShouDanJia()));
            saleProduce.setProduceName(forObject.getWuLiaoName());
            saleProduce.setSaleMoney(String.format("%.2f",forObject.getFALLAMOUNTLC()));
            produceMapper.insert(saleProduce);

             SaleOrder saleOrder = new SaleOrder();
            saleOrder.setCreateTime(forObject.getFDATE());
            saleOrder.setOrgId(forgid.getOrgId());
            saleOrder.setSaleMoney(String.format("%.2f",forObject.getFALLAMOUNTLC()));
            saleOrder.setSaleNumber(String.format("%.2f",forObject.getFQTY()));
            saleOrder.setSaleOrder(forObject.getFBILLNO());
            orderMapper.insert(saleOrder);

        }
    }
    @Test
    public void  test2(){
        ResponseEntity<List<XiaoShouFeiYong>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-XiaoShou/getXiaoShouFeiYong?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<XiaoShouFeiYong>>() {}
        );
        List<XiaoShouFeiYong> forObjects = response.getBody();
        System.out.println(forObjects);
        for (XiaoShouFeiYong forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFacctorgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            SaleCost saleCost = new SaleCost();
            saleCost.setCreateTime(forObject.getFDATE());
            saleCost.setOrgId(forgid.getOrgId());
            saleCost.setSaleCostMoney(String.format("%.2f",forObject.getFCREDIT()));
            saleCost.setSaleCostName(forObject.getFname());
            costMapper.insert(saleCost);
        }

    }

    @Test
    public void  test3(){
        ResponseEntity<List<XiaoShouFaHuoJinEKeHuGongXian>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-XiaoShou/getXiaoShouFaHuoJinEKeHuGongXian?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<XiaoShouFaHuoJinEKeHuGongXian>>() {}
        );
        List<XiaoShouFaHuoJinEKeHuGongXian> forObjects = response.getBody();
        for (XiaoShouFaHuoJinEKeHuGongXian forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFSTOCKORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            SaleDeliver saleDeliver = new SaleDeliver();
            saleDeliver.setCreateTime(forObject.getFDATE());
            saleDeliver.setOrgId(forgid.getOrgId());
            saleDeliver.setSaleDeliverMoney(String.format("%.2f",forObject.getFALLAMOUNTLC()));
            saleDeliver.setSaleDeliverNumber(String.format("%.2f",forObject.getFREALQTY()));
            saleDeliver.setSaleOrder(forObject.getFBILLNO());
            deliverMapper.insert(saleDeliver);
            SaleSalesman saleSalesman = new SaleSalesman();
            saleSalesman.setCreateTime(forObject.getFDATE());
            saleSalesman.setOrgId(forgid.getOrgId());
            saleSalesman.setSalesmanMoney(String.format("%.2f",forObject.getFALLAMOUNTLC()));
            saleSalesman.setSalesmanName(forObject.getXiaoShouName());
            salesmanMapper.insert(saleSalesman);
        }

    }

    @Test
    public void  test4(){
        ResponseEntity<List<XiaoShouFaPiaoJinE>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-XiaoShou/getXiaoShouFaPiaoJinE?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<XiaoShouFaPiaoJinE>>() {}
        );
        List<XiaoShouFaPiaoJinE> forObjects = response.getBody();
        for (XiaoShouFaPiaoJinE forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFSALEORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            SaleInvoice saleInvoice = new SaleInvoice();
            saleInvoice.setCreateTime(forObject.getFDATE());
            saleInvoice.setOrgId(forgid.getOrgId());
            saleInvoice.setSaleInvoiceMoney(String.format("%.2f",forObject.getFALLAMOUNT()));
            saleInvoice.setSaleInvoiceOrder(forObject.getFBILLNO());
            invoiceMapper.insert(saleInvoice);

        }
    }

    @Test
    public void  test5(){
        ResponseEntity<List<YvQiHeTongMingXi>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-XiaoShou/getYvQiHeTongMingXi?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<YvQiHeTongMingXi>>() {}
        );
        List<YvQiHeTongMingXi> forObjects = response.getBody();
        for (YvQiHeTongMingXi forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFSALEORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            SaleOverdue saleOverdue = new SaleOverdue();
            saleOverdue.setCreateTime(forObject.getFDATE());
            saleOverdue.setDeliveryDate(forObject.getChuKuDate());
            saleOverdue.setDeliveryNumber(forObject.getFQTY());
            saleOverdue.setOrgId(forgid.getOrgId());
//            saleOverdue.setOverdueDay();
            saleOverdue.setOverdueMoney(String.format("%.2f",forObject.getFALLAMOUNTLC()));
            saleOverdue.setOverdueOrder(forObject.getFBILLNO());
            overdueMapper.insert(saleOverdue);

            MoneyOverdue moneyProduce = new MoneyOverdue();
//            moneyProduce.setBalance();
            moneyProduce.setCollectPrice(String.format("%.2f",forObject.getFALLAMOUNTLC()));
            moneyProduce.setDateTime(forObject.getFDATE());
            moneyProduce.setDocumentNumber(forObject.getFBILLNO());
//            moneyProduce.setDocumentStatus();
            moneyProduce.setDocumentType(forObject.getFNAME());
            moneyProduce.setEndTime(forObject.getChuKuDate());
            moneyProduce.setIsOverdue(0);
            moneyProduce.setOrgId(forgid.getOrgId());
            moneyProduce.setOverduePrice(String.format("%.2f",forObject.getFALLAMOUNTLC()));
//            moneyProduce.setOverdueTime();
            moneyProduce.setUserName(forObject.getKeHuName());

            moneyOverdueMapper.insert(moneyProduce);
        }
    }

    @Test
    public void  test6() {

        ResponseEntity<List<BaiFangKeHu>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-XiaoShou/getBaiFangKeHu?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<BaiFangKeHu>>() {}
        );
        List<BaiFangKeHu> forObjects = response.getBody();
        System.out.println(forObjects);
        for (BaiFangKeHu forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFZCORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            SaleTarget saleTarget = new SaleTarget();
            saleTarget.setOrgId(forgid.getOrgId());
            saleTarget.setVisitNumber("1");
            saleTarget.setCreateTime(forObject.getFZCDATE());
            targetMapper.insert(saleTarget);
        }
    }


}
