/*
 * 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.voucher.ChanPinShouRu;
import vip.xiaonuo.erp.entity.voucher.ErpVoucher;
import vip.xiaonuo.erp.entity.voucher.LinJinHuanKuanQi;
import vip.xiaonuo.erp.entity.xiaoshou.*;
import vip.xiaonuo.erp.mapper.ErpBdOrgMapper;
import vip.xiaonuo.money.entity.MoneyBorrowWarning;
import vip.xiaonuo.money.entity.MoneyBusiness;
import vip.xiaonuo.money.entity.MoneyProduce;
import vip.xiaonuo.money.entity.MoneyProduceIncome;
import vip.xiaonuo.money.mapper.MoneyBorrowWarningMapper;
import vip.xiaonuo.money.mapper.MoneyBusinessMapper;
import vip.xiaonuo.money.mapper.MoneyProduceIncomeMapper;
import vip.xiaonuo.money.mapper.MoneyProduceMapper;
import vip.xiaonuo.sale.entity.*;
import vip.xiaonuo.sale.mapper.*;

import java.util.List;

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

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ErpBdOrgMapper erpBdOrgMapper;
    @Autowired
    private MoneyBusinessMapper businessMapper;

    @Autowired
    private MoneyBorrowWarningMapper borrowWarningMapper;
    @Autowired
    private MoneyProduceMapper produceMapper;
    @Autowired
    private MoneyProduceIncomeMapper produceIncomeMapper;


    @Test
    public void  test1(){
        ResponseEntity<List<ErpVoucher>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-voucher/getShouRuJinE?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpVoucher>>() {}
        );
        List<ErpVoucher> forObjects = response.getBody();
        System.out.println(forObjects);
        for (ErpVoucher forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFacctorgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            MoneyBusiness moneyBusiness = new MoneyBusiness();
            moneyBusiness.setOrgId(forgid.getOrgId());
            moneyBusiness.setCreateTime(forObject.getFDATE());
            moneyBusiness.setBusinessIncome(String.format("%.2f",forObject.getFcredit()));
            businessMapper.insert(moneyBusiness);
        }
    }

    @Test
    public void  test2(){
        ResponseEntity<List<LinJinHuanKuanQi>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-voucher/getLinJinHuanKuan?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<LinJinHuanKuanQi>>() {}
        );
        List<LinJinHuanKuanQi> forObjects = response.getBody();
        System.out.println(forObjects);
        for (LinJinHuanKuanQi forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getForgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            MoneyBorrowWarning moneyBorrowWarning = new MoneyBorrowWarning();
            moneyBorrowWarning.setOrgId(forgid.getOrgId());
            moneyBorrowWarning.setBorrowTime(forObject.getFdate());
            moneyBorrowWarning.setBorrowPrice(String.format("%.2f",forObject.getFcredit()));
            moneyBorrowWarning.setMoneyType(forObject.getFname());
            moneyBorrowWarning.setCompany(forObject.getOrgName());
            borrowWarningMapper.insert(moneyBorrowWarning);
        }
    }

    @Test
    public void  test3(){
        ResponseEntity<List<ErpVoucher>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-voucher/getDangQianYueYingYeE?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpVoucher>>() {}
        );
        List<ErpVoucher> forObjects = response.getBody();
        System.out.println(forObjects);
        for (ErpVoucher forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFacctorgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            MoneyBusiness moneyBusiness = new MoneyBusiness();
            moneyBusiness.setOrgId(forgid.getOrgId());
            moneyBusiness.setCreateTime(forObject.getFDATE());
            moneyBusiness.setBusinessCost(String.format("%.2f",forObject.getFcredit()));
            businessMapper.insert(moneyBusiness);
        }
    }
    @Test
    public void  test4(){
        ResponseEntity<List<ErpVoucher>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-voucher/getDangQianYueJingLiRun?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpVoucher>>() {}
        );
        List<ErpVoucher> forObjects = response.getBody();
        System.out.println(forObjects);
        for (ErpVoucher forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFacctorgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            MoneyBusiness moneyBusiness = new MoneyBusiness();
            moneyBusiness.setOrgId(forgid.getOrgId());
            moneyBusiness.setCreateTime(forObject.getFDATE());
            moneyBusiness.setProfit(String.format("%.2f",forObject.getFcredit()));
            businessMapper.insert(moneyBusiness);
        }
    }
    @Test
    public void  test5(){
        ResponseEntity<List<ErpVoucher>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-voucher/getDangQianZiJinYvE?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpVoucher>>() {}
        );
        List<ErpVoucher> forObjects = response.getBody();
        System.out.println(forObjects);
        for (ErpVoucher forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFacctorgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;
            MoneyBusiness moneyBusiness = new MoneyBusiness();
            moneyBusiness.setOrgId(forgid.getOrgId());
            moneyBusiness.setCreateTime(forObject.getFDATE());
            moneyBusiness.setBalance(String.format("%.2f",forObject.getFcredit()));
            businessMapper.insert(moneyBusiness);
        }
    }

    @Test
    public void  test6(){
        ResponseEntity<List<ChanPinShouRu>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-voucher/getChanPinShouRu?date=",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ChanPinShouRu>>() {}
        );
        //'50万IU/G饲料添加剂VD3微粒','NF级胆固醇','饲料级胆固醇91%',
        // '25-羟基VD3原','4000万IU/G胆钙化醇',
        // '维生素D3原','蜡丸','100万IU/G复配营养强化剂MO','饲料级胆固醇80%','化妆品级羊毛醇'
        List<ChanPinShouRu> forObjects = response.getBody();
        for (ChanPinShouRu forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFstockorgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;

            MoneyProduce moneyProduce = new MoneyProduce();
            moneyProduce.setOrgId(forgid.getOrgId());
            moneyProduce.setCreateTime(forObject.getFdate());
            MoneyProduceIncome moneyProduceIncome = new MoneyProduceIncome();
            moneyProduceIncome.setOrgId(forgid.getOrgId());
            moneyProduceIncome.setCreateTime(forObject.getFdate());
            if ("NF级胆固醇".equals(forObject.getFname())){
                moneyProduce.setNfCholesterol(String.format("%.2f",forObject.getFallamount()));
                moneyProduceIncome.setNfCholesterol(String.format("%.2f",forObject.getFallamount()));
            }else if ("饲料级胆固醇80".equals(forObject.getFname())||"饲料级胆固醇91".equals(forObject.getFname())){
                moneyProduce.setBreedCholesterol(String.format("%.2f",forObject.getFallamount()));
                moneyProduceIncome.setBreedCholesterol(String.format("%.2f",forObject.getFallamount()));
            }else if ("化妆品级羊毛醇".equals(forObject.getFname())){
                moneyProduce.setSheepAlcohol(String.format("%.2f",forObject.getFallamount()));
                moneyProduceIncome.setSheepAlcohol(String.format("%.2f",forObject.getFallamount()));
            }else {
                moneyProduce.setOther(String.format("%.2f",forObject.getFallamount()));
                moneyProduceIncome.setOther(String.format("%.2f",forObject.getFallamount()));
            }
            produceIncomeMapper.insert(moneyProduceIncome);
            produceMapper.insert(moneyProduce);
        }
    }

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

        //'国内销售收入','国内销售收入2','国外销售收入'
        List<ErpVoucher> forObjects = response.getBody();
        for (ErpVoucher forObject : forObjects) {
            ErpBdOrg forgid = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forObject.getFacctorgid()));
            if (ObjectUtil.isEmpty(forgid))
                continue;

            MoneyProduce moneyProduce = new MoneyProduce();
            moneyProduce.setOrgId(forgid.getOrgId());
            moneyProduce.setCreateTime(forObject.getFDATE());
            if ("国内销售收入".equals(forObject.getFNAME())||"国内销售收入2".equals(forObject.getFNAME())){
                moneyProduce.setInMoney(String.format("%.2f",forObject.getFcredit()));
            } else {
                moneyProduce.setOutMoney(String.format("%.2f",forObject.getFcredit()));
            }
            produceMapper.insert(moneyProduce);
        }
    }
}
