package vip.xiaonuo.erp.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
//import vip.xiaonuo.common.annotation.Test;
import vip.xiaonuo.erp.entity.ErpBdOrg;
import vip.xiaonuo.erp.entity.caigou.*;
import vip.xiaonuo.erp.entity.kucun.YangMaoZhiGongYingShang;
import vip.xiaonuo.erp.mapper.ErpBdOrgMapper;
import vip.xiaonuo.erp.service.IErpCaiGouService;
import vip.xiaonuo.erp.service.IErpShengChanService;
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.util.Date;
import java.util.List;


@Service
public class ErpCaiGouServiceImpl implements IErpCaiGouService {
    @Autowired
    private RestTemplate restTemplate;

    //合同
    @Autowired
    private PurchaseContractMapper contractMapper;

    //订单
    @Autowired
    private PurchaseOrderMapper orderMapper;

    //付款
    @Autowired
    private PurchasePayMapper payMapper;

    //单价
    @Autowired
    private PurchasePriceMapper priceMapper;

    @Autowired
    private ErpBdOrgMapper erpBdOrgMapper;
    @Override
    public void getCaiGouDingDanJinE() {
        Date date = DateUtil.beginOfDay(new Date());  // 获取当前日期
        // 构建URL，使用格式化的日期
        String url = "http://127.0.0.1:9200/erp-caiGou/getCaiGouDingDanShuLiangJinE?date=" +date;

        ResponseEntity<List<CaiGouDingDanShuLiangJinE>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<CaiGouDingDanShuLiangJinE>>() {}
        );
        List<CaiGouDingDanShuLiangJinE> forObjects = response.getBody();
        for (CaiGouDingDanShuLiangJinE forObject : forObjects) {
//        CaiGouDingDanShuLiangJinE forObject = forObjects.get(0);
            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())
                    .between("create_time",DateUtil.beginOfDay(fdate),DateUtil.endOfDay(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()));
                orderMapper.insert(purchaseOrder);
            }else {
                purchaseOrder.setCreateTime(forObject.getFDATE());
                purchaseOrder.setMaterial(forObject.getFname());
                purchaseOrder.setOrgId(forgid.getOrgId());
                purchaseOrder.setPurchaseMoney(String.format("%.2f",forObject.getFALLAMOUNT()));
                purchaseOrder.setMaterialAcceptNumber(String.format("%.2f",forObject.getFQTY()));
                purchaseOrder.setMaterialAcceptPrice(String.format("%.2f",forObject.getFALLAMOUNT()));
                orderMapper.updateById(purchaseOrder);
            }
        }
    }

    @Override
    public void getCaiGouRuKuJinE() {
        Date date = DateUtil.beginOfDay(new Date());  // 获取当前日期
        ResponseEntity<List<CaiGouRuKu>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getCaiGouRuku?date="+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);
                }

            }
        }
    }

    @Override
    public void getCaiGouFaPiaoJinE() {
        Date date = DateUtil.beginOfDay(new Date());  // 获取当前日期
        ResponseEntity<List<FaPiaoJinE>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getCaiGouFaPiao?date="+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.setOrgId(forgid.getOrgId());
                orderMapper.insert(purchaseOrder);
            }else {
                PurchaseOrder purchaseOrder = purchaseOrders.get(0);
                purchaseOrder.setInvoicePrice(String.format("%.2f",forObject.getFALLAMOUNT()));
                orderMapper.updateById(purchaseOrder);
            }
        }

    }

    @Override
    public void getLeiJiRuKu() {
        Date date = DateUtil.beginOfDay(new Date());
        ResponseEntity<List<LeiJiRuKu>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getLeiJiRuKu?date="+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);
            }
        }

    }

    @Override
    public void getShouLiaoJinE() {
        Date date = DateUtil.beginOfDay(new Date());
        ResponseEntity<List<ShouLiaoJinE>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getShouLiaoJinE?date="+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);
            }
        }

    }

    @Override
    public void getJiaoHuoShuLiang() {
        Date date = DateUtil.beginOfDay(new Date());
        ResponseEntity<List<JiaoHuoShuliang>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getJiaoHuoShuLiang?date="+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);
        }

    }

    @Override
    public void getCaiGouShuLiang() {
        Date date = DateUtil.beginOfDay(new Date());
        ResponseEntity<List<CaiGouShuLiang>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getCaiGouShuLiang?date="+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);
        }

    }

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

    }

    @Override
    public void getDaiJianShuLiang() {
        Date date = DateUtil.beginOfDay(new Date());
        ResponseEntity<List<DaiJianShuLiang>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getDaiJianShuLiang?date="+date,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<DaiJianShuLiang>>() {}
        );
        List<DaiJianShuLiang> forObjects = response.getBody();
        System.out.println(forObjects);

    }

    @Override
    public void getShangNianCaiGou() {
        Date date = DateUtil.beginOfDay(new Date());
        ResponseEntity<List<CaiGouYueJun>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getCaiGouYueJun?date="+date,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<CaiGouYueJun>>() {}
        );
        List<CaiGouYueJun> forObjects = response.getBody();
        System.out.println(forObjects);

    }

    @Override
    public void getYingFuZhangKuanMingXi() {
        Date date = DateUtil.beginOfDay(new Date());
        ResponseEntity<List<YingFuZhangKuan>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getYingFuZhangKuang?date="+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);

        }

    }

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

    }

    @Override
    public void getZuiDiKuCun() {
        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);

    }

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

    }

    @Override
    public void getCaiGouHeTongShuLiangJinE() {
        Date date = DateUtil.beginOfDay(new Date());
        ResponseEntity<List<CaiGouDingDanShuLiangJinE>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-caiGou/getCaiGouHeTongShuLiangJinE?date="+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);
        }

    }
}
