/*
 * 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.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.ErpBdMaterial;
import vip.xiaonuo.erp.entity.ErpBdOrg;
import vip.xiaonuo.erp.entity.voucher.ErpMaterial;
import vip.xiaonuo.erp.mapper.ErpBdMaterialMapper;
import vip.xiaonuo.erp.mapper.ErpBdOrgMapper;

import java.util.List;

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

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ErpBdOrgMapper erpBdOrgMapper;
    @Autowired
    private ErpBdMaterialMapper erpBdMaterialMapper;

    @Test
    public void  test1(){
        ResponseEntity<List<ErpMaterial>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-bd-org/getMaterial",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpMaterial>>() {}
        );
        List<ErpMaterial> forObjects = response.getBody();
        System.out.println(forObjects);

        for (ErpMaterial forObject : forObjects) {

            ResponseEntity<List<ErpMaterial>> response2 = restTemplate.exchange(
                    "http://127.0.0.1:9200/erp-bd-org/getMaterialDetail?fnumber="+forObject.getFnumber(),
                    HttpMethod.GET,
                    null,
                    new ParameterizedTypeReference<List<ErpMaterial>>() {}
            );
            //二
            List<ErpMaterial> forObjects2 = response2.getBody();
            System.out.println(forObjects2);

            boolean b = false;
            //判断有没有下级
            for (ErpMaterial erpMaterial : forObjects2) {
                if(erpMaterial.getFnumber().contains("0000")){

                    ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", erpMaterial.getFacctorgid()));
                    if (ObjectUtil.isEmpty(forgid))
                        continue;
                    if (erpBdMaterialMapper.exists(new QueryWrapper<ErpBdMaterial>().eq("erp_fnumber", erpMaterial.getFnumber()).eq("org_id",forgid.getOrgId()))){
                        continue;
                    }
                    ErpBdMaterial erpBaseMaterial = new ErpBdMaterial();
                    erpBaseMaterial.setErpFnumber(erpMaterial.getFnumber());
                    erpBaseMaterial.setOrgId(forgid.getOrgId());
                    erpBaseMaterial.setFname(erpMaterial.getFname());
                    erpBaseMaterial.setFspecification(erpMaterial.getFspecification());
                    erpBaseMaterial.setUnit("kg");
                    erpBaseMaterial.setConver("1");
                    erpBaseMaterial.setParent(0);
                    erpBdMaterialMapper.insert(erpBaseMaterial);
                    b =true;
                }
            }
            //有下级
            if (b){
                for (ErpMaterial erpMaterial : forObjects2) {
                    if(!erpMaterial.getFnumber().contains("0000")){

                        ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", erpMaterial.getFacctorgid()));
                        if (ObjectUtil.isEmpty(forgid))
                            continue;
                        if (erpBdMaterialMapper.exists(new QueryWrapper<ErpBdMaterial>().eq("erp_fnumber", erpMaterial.getFnumber()).eq("org_id",forgid.getOrgId()))){
                            continue;
                        }
                        ErpBdMaterial erpBaseMaterial = new ErpBdMaterial();
                        erpBaseMaterial.setErpFnumber(erpMaterial.getFnumber());
                        erpBaseMaterial.setOrgId(forgid.getOrgId());
                        erpBaseMaterial.setFname(erpMaterial.getFname()+"  "+erpMaterial.getFspecification());
                        erpBaseMaterial.setFspecification(erpMaterial.getFspecification());
                        ErpBdMaterial erpBdMaterial = erpBdMaterialMapper.selectOne(new QueryWrapper<ErpBdMaterial>().
                                eq("erp_fnumber", forObject.getFnumber() + "0000").eq("org_id", forgid.getOrgId()));
                        if (ObjectUtil.isEmpty(erpBdMaterial)){
                            continue;
                        }
                        erpBaseMaterial.setParent(erpBdMaterial.getId());
                        erpBaseMaterial.setUnit("kg");
                        erpBaseMaterial.setConver("1");
                        erpBdMaterialMapper.insert(erpBaseMaterial);
                    }
                }
            } else {
                for (ErpMaterial erpMaterial : forObjects2) {

                    ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", erpMaterial.getFacctorgid()));
                    if (ObjectUtil.isEmpty(forgid))
                        continue;
                    if (erpBdMaterialMapper.exists(new QueryWrapper<ErpBdMaterial>().eq("erp_fnumber", erpMaterial.getFnumber()).eq("org_id",forgid.getOrgId()))){
                        continue;
                    }
                    ErpBdMaterial erpBaseMaterial = new ErpBdMaterial();
                    erpBaseMaterial.setErpFnumber(erpMaterial.getFnumber());
                    erpBaseMaterial.setOrgId(forgid.getOrgId());
                    erpBaseMaterial.setFname(erpMaterial.getFname());
                    erpBaseMaterial.setFspecification(erpMaterial.getFspecification());
                    erpBaseMaterial.setParent(0);
                    erpBaseMaterial.setUnit("kg");
                    erpBaseMaterial.setConver("1");
                    erpBdMaterialMapper.insert(erpBaseMaterial);

                    ErpBdMaterial erpBaseMaterial1 = new ErpBdMaterial();
                    erpBaseMaterial1.setErpFnumber(erpMaterial.getFnumber());
                    erpBaseMaterial1.setOrgId(forgid.getOrgId());
                    erpBaseMaterial1.setFname(erpMaterial.getFname()+"  "+erpMaterial.getFspecification());
                    erpBaseMaterial1.setFspecification(erpMaterial.getFspecification());
                    erpBaseMaterial1.setParent(erpBaseMaterial.getId());
                    erpBaseMaterial.setUnit("kg");
                    erpBaseMaterial.setConver("1");
                    erpBdMaterialMapper.insert(erpBaseMaterial1);
                }
            }
        }
    }


}
