/*
 * 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.ErpBdOrg;
import vip.xiaonuo.erp.entity.caigou.CaiGouRuKu;
import vip.xiaonuo.erp.entity.shengcan.ShiJiChanLiang;
import vip.xiaonuo.erp.mapper.ErpBdOrgMapper;
import vip.xiaonuo.production.entity.ProductionTarget;
import vip.xiaonuo.production.entity.ProductionWorkstopPlan;
import vip.xiaonuo.production.entity.ProductionYield;
import vip.xiaonuo.production.mapper.ProductionTargetMapper;
import vip.xiaonuo.production.mapper.ProductionWorkstopPlanMapper;
import vip.xiaonuo.production.mapper.ProductionYieldMapper;

import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class SengChanTest {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ErpBdOrgMapper erpBdOrgMapper;
    @Autowired
    private ProductionYieldMapper yieldMapper;

    @Autowired
    private ProductionWorkstopPlanMapper workstopPlanMapper;

    @Autowired
    private ProductionTargetMapper targetMapper;
    @Test
    public void  test1(){
        ResponseEntity<List<ShiJiChanLiang>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-shengChan/getShiJiChanLiang?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ShiJiChanLiang>>() {}
        );
        List<ShiJiChanLiang> forObjects = response.getBody();

        for (ShiJiChanLiang forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFSTOCKORGID()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            ProductionYield productionYield = new ProductionYield();
            productionYield.setActualProduction(String.format("%.2f",forObject.getFREALQTY()));
            productionYield.setCompanyName(forgid.getFname());
//            productionYield.setCompleteRate();
            productionYield.setCreateTime(forObject.getFDATE());
            productionYield.setLocationName(forObject.getCheJianName());
            productionYield.setOrgId(forgid.getOrgId());
            productionYield.setProduceName(forObject.getWuLiaoName());
            yieldMapper.insert(productionYield);

            ProductionWorkstopPlan productionWorkstopPlan  = new ProductionWorkstopPlan();
            productionWorkstopPlan.setActualProduction(String.format("%.2f",forObject.getFREALQTY()));
//            productionWorkstopPlan.setAvgValue();
            productionWorkstopPlan.setCreateTime(forObject.getFDATE());
            productionWorkstopPlan.setLocationId(forObject.getFWORKSHOPID()+"");
            productionWorkstopPlan.setLocationName(forObject.getCheJianName());
            productionWorkstopPlan.setOrgId(forgid.getOrgId());
//            productionWorkstopPlan.setPlanProduction();
            productionWorkstopPlan.setProduceName(forObject.getWuLiaoName());
//            productionWorkstopPlan.setUnitCost();
            workstopPlanMapper.insert(productionWorkstopPlan);

            ProductionTarget productionTarget = new ProductionTarget();
            productionTarget.setActualProduction(String.format("%.2f",forObject.getFREALQTY()));
            productionTarget.setCreateTime(forObject.getFDATE());
            productionTarget.setLocationName(forObject.getCheJianName());
            productionTarget.setOrgId(forgid.getOrgId());
//            productionTarget.setPlanCompleteRate();
//            productionTarget.setPlanProduction();
//            productionTarget.setProductionEnergy();
//            productionTarget.setReceiveRate();
            targetMapper.insert(productionTarget);
        }
    }

}
