/*
 * 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.alibaba.fastjson2.schema.ValidateResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.platform.commons.util.StringUtils;
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.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import vip.xiaonuo.erp.entity.ErpBdOrg;
import vip.xiaonuo.erp.entity.caigou.*;
import vip.xiaonuo.erp.mapper.ErpBdOrgMapper;
import vip.xiaonuo.purchase.entity.PurchaseContract;
import vip.xiaonuo.purchase.entity.PurchaseOrder;
import vip.xiaonuo.purchase.entity.PurchasePay;
import vip.xiaonuo.purchase.entity.PurchasePrice;
import vip.xiaonuo.purchase.mapper.PurchaseContractMapper;
import vip.xiaonuo.purchase.mapper.PurchaseOrderMapper;
import vip.xiaonuo.purchase.mapper.PurchasePayMapper;
import vip.xiaonuo.purchase.mapper.PurchasePriceMapper;

import java.text.SimpleDateFormat;
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 MainTest {


    @Autowired
    private RestTemplate restTemplate;

    //合同
    @Autowired
    private PurchaseContractMapper contractMapper;

    //订单
    @Autowired
    private PurchaseOrderMapper orderMapper;

    //付款
    @Autowired
    private PurchasePayMapper payMapper;

    //单价
    @Autowired
    private PurchasePriceMapper priceMapper;

    @Autowired
    private ErpBdOrgMapper erpBdOrgMapper;


    @Test
    public void test() {

        Date date = DateUtil.beginOfDay(new Date());  // 获取当前日期
        // 构建URL，使用格式化的日期
        String url = "http://127.0.0.1:9200/erp-caiGou/getCaiGouDingDanShuLiangJinE?date=";

        ResponseEntity<List<CaiGouDingDanShuLiangJinE>> response = restTemplate.exchange(
                    url,
                    HttpMethod.GET,
                    null,
                    new ParameterizedTypeReference<List<CaiGouDingDanShuLiangJinE>>() {}
            );
            List<CaiGouDingDanShuLiangJinE> forObjects = response.getBody();
        for (CaiGouDingDanShuLiangJinE forObject : forObjects) {

            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFPURCHASEORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
//            PurchasePrice purchasePrice = new PurchasePrice();
//            purchasePrice.setCreateTime(forObject.getFDATE());
//            purchasePrice.setMaterial(forObject.getFname());
//            purchasePrice.setOrgId(forgid.getOrgId());
//            purchasePrice.setPrice(String.format("%.2f",forObject.getDanJia()));
//            priceMapper.insert(purchasePrice);
//
            Date fdate = forObject.getFDATE();
            PurchaseOrder purchaseOrder = orderMapper.selectOne(new QueryWrapper<PurchaseOrder>().eq("org_id", forgid.getOrgId())
                    .eq("create_time",fdate)
                    .eq("material",forObject.getFname()));
            if (ObjectUtil.isEmpty(purchaseOrder)){
                purchaseOrder =new PurchaseOrder();
                purchaseOrder.setCreateTime(forObject.getFDATE());
                purchaseOrder.setPurchaseMoney(String.format("%.2f",forObject.getFALLAMOUNT()));
                purchaseOrder.setMaterial(forObject.getFname());
                purchaseOrder.setOrgId(forgid.getOrgId());
                purchaseOrder.setMaterialAcceptNumber(String.format("%.2f",forObject.getFQTY()));
                purchaseOrder.setMaterialAcceptPrice(String.format("%.2f",forObject.getFALLAMOUNT()));
                purchaseOrder.setSupplier(forObject.getGys());
                orderMapper.insert(purchaseOrder);
            }else {
//                purchaseOrder.setCreateTime(forObject.getFDATE());
//                purchaseOrder.setMaterial(forObject.getFname());
//                purchaseOrder.setOrgId(forgid.getOrgId());
                purchaseOrder.setSupplier(forObject.getGys());
//                purchaseOrder.setPurchaseMoney(String.format("%.2f",forObject.getFALLAMOUNT()));
//                purchaseOrder.setMaterialAcceptNumber(String.format("%.2f",forObject.getFQTY()));
//                purchaseOrder.setMaterialAcceptPrice(String.format("%.2f",forObject.getFALLAMOUNT()));
                orderMapper.updateById(purchaseOrder);
            }
        }
    }

    @Test
    public void  test2(){
        ResponseEntity<List<CaiGouRuKu>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getCaiGouRuku?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<CaiGouRuKu>>() {}
        );
        List<CaiGouRuKu> forObjects = response.getBody();
        for (CaiGouRuKu forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFDEMANDORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            List<PurchaseOrder> purchaseOrders = orderMapper.selectList(new QueryWrapper<PurchaseOrder>().eq("org_id", forgid.getOrgId())
                    .between("create_time",DateUtil.beginOfDay(forObject.getFDATE()),DateUtil.endOfDay(forObject.getFDATE())));
            if (ObjectUtil.isEmpty(purchaseOrders)){
                PurchaseOrder  purchaseOrder =new PurchaseOrder();
                purchaseOrder.setCreateTime(forObject.getFDATE());
                if (forObject.getFBILLAMOUNTLC()!=null){
                    purchaseOrder.setEnterPrice(String.format("%.2f",forObject.getFBILLAMOUNTLC()));
                }
                purchaseOrder.setOrgId(forgid.getOrgId());
                orderMapper.insert(purchaseOrder);
            }else {
                if (forObject.getFBILLAMOUNTLC()!=null){
                    PurchaseOrder    purchaseOrder = purchaseOrders.get(0);
                    purchaseOrder.setEnterPrice(String.format("%.2f",forObject.getFBILLAMOUNTLC()));
                    orderMapper.updateById(purchaseOrder);
                }

            }
        }

    }

    @Test
    public void  test3(){
        ResponseEntity<List<FaPiaoJinE>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getCaiGouFaPiao?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<FaPiaoJinE>>() {}
        );
        List<FaPiaoJinE> forObjects = response.getBody();

        for (FaPiaoJinE forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFPURCHASEORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            List<PurchaseOrder> purchaseOrders = orderMapper.selectList(new QueryWrapper<PurchaseOrder>().eq("org_id", forgid.getOrgId())
                    .between("create_time",DateUtil.beginOfDay(forObject.getFDATE()),DateUtil.endOfDay(forObject.getFDATE())));
            if (ObjectUtil.isEmpty(purchaseOrders)){
                PurchaseOrder purchaseOrder =new PurchaseOrder();
                purchaseOrder.setCreateTime(forObject.getFDATE());
                purchaseOrder.setInvoicePrice(String.format("%.2f",forObject.getFALLAMOUNT()));
//                purchaseOrder.setInvoiceNumber(String.format("%.2f",forObject.getFQTY()));
                purchaseOrder.setOrgId(forgid.getOrgId());
                orderMapper.insert(purchaseOrder);
            }else {
                PurchaseOrder purchaseOrder = purchaseOrders.get(0);
                purchaseOrder.setInvoicePrice(String.format("%.2f",forObject.getFALLAMOUNT()));
                orderMapper.updateById(purchaseOrder);
            }
        }

    }

    @Test
    public void  test4(){
        ResponseEntity<List<LeiJiRuKu>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getLeiJiRuKu?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<LeiJiRuKu>>() {}
        );
        List<LeiJiRuKu> forObjects = response.getBody();

        for (LeiJiRuKu forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFDEMANDORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            List<PurchaseOrder> purchaseOrders = orderMapper.selectList(new QueryWrapper<PurchaseOrder>().eq("org_id", forgid.getOrgId())
                    .between("create_time",DateUtil.beginOfDay(forObject.getFDATE()),DateUtil.endOfDay(forObject.getFDATE())));
            if (ObjectUtil.isEmpty(purchaseOrders)){
                PurchaseOrder purchaseOrder =new PurchaseOrder();
                purchaseOrder.setCreateTime(forObject.getFDATE());
                purchaseOrder.setEnterNumber(String.format("%.2f",forObject.getFREALQTY()));
                purchaseOrder.setOrgId(forgid.getOrgId());
                orderMapper.insert(purchaseOrder);
            }else {
                PurchaseOrder purchaseOrder = purchaseOrders.get(0);
                purchaseOrder.setEnterNumber(String.format("%.2f",forObject.getFREALQTY()));
                orderMapper.updateById(purchaseOrder);
            }
        }
    }

    @Test
    public void  test5(){
        ResponseEntity<List<ShouLiaoJinE>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getShouLiaoJinE?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ShouLiaoJinE>>() {}
        );
        List<ShouLiaoJinE> forObjects = response.getBody();

        for (ShouLiaoJinE forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFSTOCKORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            List<PurchaseOrder> purchaseOrders = orderMapper.selectList(new QueryWrapper<PurchaseOrder>().eq("org_id", forgid.getOrgId())
                    .between("create_time",DateUtil.beginOfDay(forObject.getFDATE()),DateUtil.endOfDay(forObject.getFDATE())));
            if (ObjectUtil.isEmpty(purchaseOrders)){
                PurchaseOrder purchaseOrder =new PurchaseOrder();
                purchaseOrder.setCreateTime(forObject.getFDATE());
                purchaseOrder.setMaterialAcceptPrice(String.format("%.2f",forObject.getFINSTOCKQTY()));
                purchaseOrder.setOrgId(forgid.getOrgId());
                orderMapper.insert(purchaseOrder);
            }else {
                PurchaseOrder purchaseOrder = purchaseOrders.get(0);
                purchaseOrder.setMaterialAcceptPrice(String.format("%.2f",forObject.getFINSTOCKQTY()));
                orderMapper.updateById(purchaseOrder);
            }
        }
    }

    @Test
    public void  test6(){
        ResponseEntity<List<JiaoHuoShuliang>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getJiaoHuoShuLiang?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<JiaoHuoShuliang>>() {}
        );
        List<JiaoHuoShuliang> forObjects = response.getBody();


        for (JiaoHuoShuliang forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFSTOCKORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            List<PurchaseOrder> purchaseOrders = orderMapper.selectList(new QueryWrapper<PurchaseOrder>().eq("org_id", forgid.getOrgId())
                    .between("create_time",DateUtil.beginOfDay(forObject.getFDATE()),DateUtil.endOfDay(forObject.getFDATE())));
            PurchaseOrder purchaseOrder =new PurchaseOrder();
            if (ObjectUtil.isEmpty(purchaseOrders)){

                purchaseOrder.setCreateTime(forObject.getFDATE());
                purchaseOrder.setJiaoHuo(forObject.getFACTRECEIVEQTY());
                purchaseOrder.setOrgId(forgid.getOrgId());
                orderMapper.insert(purchaseOrder);
            }else {
                 purchaseOrder = purchaseOrders.get(0);
                purchaseOrder.setJiaoHuo(forObject.getFACTRECEIVEQTY());
                orderMapper.updateById(purchaseOrder);
            }
            if (purchaseOrder.getJiaoHuo()!=null && purchaseOrder.getCaiGou()!=null){
                purchaseOrder.setRank(String.format("%.2f",purchaseOrder.getJiaoHuo() / purchaseOrder.getCaiGou()));
            }
            orderMapper.updateById(purchaseOrder);
        }
    }


    @Test
    public void  test7(){
        ResponseEntity<List<CaiGouShuLiang>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getCaiGouShuLiang?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<CaiGouShuLiang>>() {}
        );
        List<CaiGouShuLiang> forObjects = response.getBody();

        for (CaiGouShuLiang forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFPURCHASEORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            List<PurchaseOrder> purchaseOrders = orderMapper.selectList(new QueryWrapper<PurchaseOrder>().eq("org_id", forgid.getOrgId())
                    .between("create_time",DateUtil.beginOfDay(forObject.getFDATE()),DateUtil.endOfDay(forObject.getFDATE())));
            PurchaseOrder purchaseOrder =  new PurchaseOrder();;
            if (ObjectUtil.isEmpty(purchaseOrders)){
                purchaseOrder.setCreateTime(forObject.getFDATE());
                purchaseOrder.setCaiGou(forObject.getFQTY());
                purchaseOrder.setOrgId(forgid.getOrgId());
                orderMapper.insert(purchaseOrder);
            }else {
                 purchaseOrder = purchaseOrders.get(0);
                purchaseOrder.setCaiGou(forObject.getFQTY());
            }
            if (purchaseOrder.getJiaoHuo()!=null && purchaseOrder.getCaiGou()!=null){
                purchaseOrder.setRank(String.format("%.2f",purchaseOrder.getJiaoHuo() / purchaseOrder.getCaiGou()));
            }
            orderMapper.updateById(purchaseOrder);
        }
    }

    @Test
    public void  test8(){
        ResponseEntity<List<YingFuZhangKuan>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getYingFuZhangKuang?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<YingFuZhangKuan>>() {}
        );
        List<YingFuZhangKuan> forObjects = response.getBody();


        for (YingFuZhangKuan forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFPURCHASEORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;

            PurchasePay purchasePay = new PurchasePay();
            purchasePay.setCreateTime(forObject.getFDATE());
            purchasePay.setEndTime(forObject.getFBOOKINGDATE());
            purchasePay.setOrgId(forgid.getOrgId());
            purchasePay.setPayableNumber(String.format("%.2f",forObject.getFNOTVERIFICATEAMOUNT()));
            purchasePay.setPayableObject(forObject.getFBGLSHSJDW());
            purchasePay.setPurchaseMaterial(forObject.getWuLiaoName());
            purchasePay.setPurchaseNumber(String.format("%.2f",forObject.getFQTY()));
            if (forObject.getFQTY() != 0.0){
                purchasePay.setPrice(String.format("%.2f",forObject.getFNOTVERIFICATEAMOUNT() / forObject.getFQTY()));
            }
            payMapper.insert(purchasePay);

        }
    }
    @Test
    public void  test9(){
        ResponseEntity<List<CaiGouDingDanShuLiangJinE>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getCaiGouHeTongShuLiangJinE?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<CaiGouDingDanShuLiangJinE>>() {}
        );
        List<CaiGouDingDanShuLiangJinE> forObjects = response.getBody();
        System.out.println(forObjects);

        for (CaiGouDingDanShuLiangJinE forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFPURCHASEORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            PurchaseContract purchaseContract = new PurchaseContract();
            purchaseContract.setContractMoney(String.format("%.2f",forObject.getFALLAMOUNT()));
            purchaseContract.setCreateTime(forObject.getFDATE());
            purchaseContract.setMaterialNumber(String.format("%.2f",forObject.getFQTY()));
            purchaseContract.setMaterialType(forObject.getFname());
            purchaseContract.setOrgId(forgid.getOrgId());
            contractMapper.insert(purchaseContract);
        }

    }
    @Test
    public void  test10() {
        ResponseEntity<List<ZuiDiKuChun>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getZuiDiKuChun?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ZuiDiKuChun>>() {}
        );
        List<ZuiDiKuChun> forObjects = response.getBody();
        System.out.println(forObjects);
        for (ZuiDiKuChun forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFSTOCKORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;

        }

    }

}
