/*
 * 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.ErpBdDepartment;
import vip.xiaonuo.erp.entity.ErpBdMaterial;
import vip.xiaonuo.erp.entity.ErpBdOrg;
import vip.xiaonuo.erp.entity.ErpBdRawMaterial;
import vip.xiaonuo.erp.entity.voucher.ErpMaterial;
import vip.xiaonuo.erp.mapper.ErpBdDepartmentMapper;
import vip.xiaonuo.erp.mapper.ErpBdMaterialMapper;
import vip.xiaonuo.erp.mapper.ErpBdOrgMapper;
import vip.xiaonuo.erp.mapper.ErpBdRawMaterialMapper;
import vip.xiaonuo.production.entity.ProduceLocation;
import vip.xiaonuo.production.mapper.ProduceLocationMapper;
import vip.xiaonuo.quality.entity.ErpBdQuail;
import vip.xiaonuo.quality.entity.QualityFenxibaogao;
import vip.xiaonuo.quality.entity.QualityReagentStock;
import vip.xiaonuo.quality.entity.vo.ErpQualiQm;
import vip.xiaonuo.quality.mapper.ErpBdQuailMapper;
import vip.xiaonuo.quality.mapper.QualityFenxibaogaoMapper;
import vip.xiaonuo.quality.mapper.QualityReagentStockMapper;
import vip.xiaonuo.quality.service.IQualityReagentService;
import vip.xiaonuo.quality.service.IQualityReagentStockService;

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 Bdest {

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

    @Autowired
    private ErpBdDepartmentMapper erpBdDepartmentMapper;
    @Autowired
    private ProduceLocationMapper produceLocationMapper;
    @Autowired
    private QualityReagentStockMapper qualityReagentStockMapper;

    @Autowired
    private ErpBdQuailMapper erpBdQuailMapper;

    @Autowired
    private QualityFenxibaogaoMapper qualityFenxibaogaoMapper;

    @Autowired
    private ErpBdRawMaterialMapper erpBdRawMaterialMapper;

//    private IQualityReagentService produceLocationMapper;


    //获取质量 所有记录
    @Test
    public void  test6(){
        ResponseEntity<List<ErpQualiQm>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-bd-org/getQuailIbit",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpQualiQm>>() {}
        );
        List<ErpQualiQm> forObjects = response.getBody();
        for (ErpQualiQm erpMaterial : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", erpMaterial.getFINSPECTORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;

            QualityFenxibaogao qualityFenxibaogao = new QualityFenxibaogao();
            String fworkshopid = erpMaterial.getFWORKSHOPID();
            //车间
            ErpBdDepartment fdeptid = erpBdDepartmentMapper.selectOne(new QueryWrapper<ErpBdDepartment>().eq("fdeptid", fworkshopid));

            if(ObjectUtil.isNotEmpty(fdeptid)){
                qualityFenxibaogao.setCheJianName(fdeptid.getFname());
            }
            qualityFenxibaogao.setInspecDate( erpMaterial.getInspectStartDate());
            qualityFenxibaogao.setInspecItems(erpMaterial.getFbglremarks());
            qualityFenxibaogao.setInspecNum(erpMaterial.getFBILLNO());
            qualityFenxibaogao.setInspecResults("合格");
            qualityFenxibaogao.setInspecStandards(erpMaterial.getFbgltext16());
            qualityFenxibaogao.setManufBatchNum(erpMaterial.getFLOT());
            qualityFenxibaogao.setMaterialName(erpMaterial.getFname());
            qualityFenxibaogao.setOrgId(forgid.getOrgId());
            //生产单位
            ErpBdDepartment fdeptid1 = erpBdDepartmentMapper.selectOne(new QueryWrapper<ErpBdDepartment>().eq("fdeptid", erpMaterial.getFINSPECTDEPID()));
            if(ObjectUtil.isNotEmpty(fdeptid1)){
                qualityFenxibaogao.setProductionUnit(fdeptid1.getFname());
            }

            qualityFenxibaogao.setQualityType(0);
            qualityFenxibaogao.setQuantity(erpMaterial.getFINSPECTQTY());
            qualityFenxibaogao.setReportDate(erpMaterial.getInspectEndDate());
            qualityFenxibaogao.setSelfBatchNum(erpMaterial.getFBILLNO());
            qualityFenxibaogao.setSampDate(erpMaterial.getInspectStartDate());
//            qualityFenxibaogao.setSpec();
            qualityFenxibaogao.setSpecificOtation(erpMaterial.getFbgltext20());

            qualityFenxibaogaoMapper.insert(qualityFenxibaogao);

        }

    }

    //获取车间
    @Test
    public void  test2(){
        List<ErpBdDepartment> erpBdDepartments = erpBdDepartmentMapper.selectList(new QueryWrapper<ErpBdDepartment>().like("fname","车间"));
        for (ErpBdDepartment erpBdDepartment : erpBdDepartments) {
            ProduceLocation produceLocation = new ProduceLocation();
            produceLocation.setOrgId(erpBdDepartment.getOrgId());
            produceLocation.setLocationName(erpBdDepartment.getFname());
            produceLocation.setOrgName(erpBdDepartment.getFfullname());
            produceLocation.setLocationId(erpBdDepartment.getFdeptid());
            produceLocationMapper.insert(produceLocation);
        }
    }
    //获取质量检验项目
    @Test
    public void  test5(){
        ResponseEntity<List<ErpBdQuail>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-bd-org/getQuail",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpBdQuail>>() {}
        );
        List<ErpBdQuail> forObjects = response.getBody();
        for (ErpBdQuail erpMaterial : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", erpMaterial.getFuseorgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            erpMaterial.setOrgId(forgid.getOrgId());
            erpMaterial.setCreateTime(new Date());
//            qualityReagentStock.setStock();
            erpBdQuailMapper.insert(erpMaterial);

        }

    }

    //获取试剂
    @Test
    public void  test4(){
        ResponseEntity<List<ErpMaterial>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-bd-org/getReagent",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpMaterial>>() {}
        );
        List<ErpMaterial> forObjects = response.getBody();
        for (ErpMaterial erpMaterial : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", erpMaterial.getFacctorgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            QualityReagentStock qualityReagentStock = new QualityReagentStock();


            qualityReagentStock.setFnumber(erpMaterial.getFnumber());
            qualityReagentStock.setFspecification(erpMaterial.getFspecification());
            qualityReagentStock.setOrgId(forgid.getOrgId());
            qualityReagentStock.setOrgName(forgid.getFname());
            qualityReagentStock.setReagent(erpMaterial.getFname());
//            qualityReagentStock.setStock();
            qualityReagentStockMapper.insert(qualityReagentStock);

        }

    }

    @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());
                        erpBaseMaterial.setFspecification(erpMaterial.getFspecification());
                        erpBaseMaterial.setUnit("kg");
                        erpBaseMaterial.setConver("1");
                        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());
                        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());
                    erpBaseMaterial1.setFspecification(erpMaterial.getFspecification());
                    erpBaseMaterial1.setParent(erpBaseMaterial.getId());
                    erpBaseMaterial1.setUnit("kg");
                    erpBaseMaterial1.setConver("1");
                    erpBdMaterialMapper.insert(erpBaseMaterial1);
                }
            }
        }
    }

    //获取原材料 等
    @Test
    public void  test7(){
        ResponseEntity<List<ErpMaterial>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-bd-org/getMaterialRaw",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpMaterial>>() {}
        );
        List<ErpMaterial> forObjects = response.getBody();
        System.out.println(forObjects);
        for (ErpMaterial erpMaterial : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", erpMaterial.getFacctorgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            ErpBdRawMaterial erpBdRawMaterial = new ErpBdRawMaterial();
            erpBdRawMaterial.setFspecification(erpMaterial.getFspecification());
            erpBdRawMaterial.setOrgId(forgid.getOrgId());
            erpBdRawMaterial.setErpFnumber(erpMaterial.getFnumber());
            erpBdRawMaterial.setFname(erpMaterial.getFname());
            erpBdRawMaterial.setMaterialType(getType(erpBdRawMaterial.getErpFnumber()));
            erpBdRawMaterialMapper.insert(erpBdRawMaterial);
        }
    }

    private Integer getType(String erpFunmber) {
        if (erpFunmber.startsWith("01"))
            return 1;
        else if (erpFunmber.startsWith("10"))
            return 0;
        else if (erpFunmber.startsWith("11"))
            return 3;
        else if (erpFunmber.startsWith("15"))
            return 2;
        else
            return 4;
    }

}
