package com.glxp.mipsdl.client.pzh;

import cn.hutool.core.util.StrUtil;
import com.glxp.mipsdl.client.CommonHttpClient;
import com.glxp.mipsdl.dao.pzhzd.YyUcloudV31SetDao;
import com.glxp.mipsdl.entity.pzhzd.YyUcloudV31SetEntity;
import com.glxp.mipsdl.entity.thrsys.ThrSystemBusApiEntity;
import com.glxp.mipsdl.enums.ResultEnum;
import com.glxp.mipsdl.http.UCloudHttpClient;
import com.glxp.mipsdl.req.base.*;
import com.glxp.mipsdl.req.post.PostFileThrOrderRequest;
import com.glxp.mipsdl.req.post.PostThrCorpRequest;
import com.glxp.mipsdl.req.post.PostThrInvProductsRequest;
import com.glxp.mipsdl.req.post.PostThrProductsRequest;
import com.glxp.mipsdl.req.pzh.USignRequest;
import com.glxp.mipsdl.req.ucloud.*;
import com.glxp.mipsdl.res.BaseResponse;
import com.glxp.mipsdl.res.PageSimpleResponse;
import com.glxp.mipsdl.res.ucloud.*;
import com.glxp.mipsdl.res.udiwms.*;
import com.glxp.mipsdl.util.DateUtil;
import com.glxp.mipsdl.util.ResultVOUtils;
import com.glxp.mipsdl.vo.ErpOrderEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 片仔癀用友erp Service
 */
@Slf4j
@Service(value = "100002HttpClient")  //bean注册名称，使用编码 + HttpClient，文件接口加 FileClient
public class PzhzdClient extends CommonHttpClient {

    @Resource
    private YyUcloudV31SetDao yyUcloudV31SetDao;
    @Resource
    private UCloudHttpClient uCloudHttpClient;

    private YyUcloudV31SetEntity setEntity;


    @Override
    public BaseResponse getUnit(UdiwmsUnitRequest udiwmsUnitRequest) {
        UCustmandocRequest uCustmandocRequest = new UCustmandocRequest(udiwmsUnitRequest.getPage(), udiwmsUnitRequest.getLimit());
        uCustmandocRequest.setCustshortname(udiwmsUnitRequest.getKey());
        uCustmandocRequest.setCustname(udiwmsUnitRequest.getName());
        uCustmandocRequest.setCustcode(udiwmsUnitRequest.getUnitId());
        uCustmandocRequest.setUnitcode(getSetEntity().getUnitCode());
        UBaseResponse<List<UCustmandocResponse>> response = uCloudHttpClient.custmandocQuery(uCustmandocRequest);
        if (response != null) {
            List<UCustmandocResponse> uCustmandocResponses = response.getData().getDatas();
            if (uCustmandocResponses != null && !uCustmandocResponses.isEmpty()) {
                List<UdiwmsUnitResponse> custmandocResponses = new ArrayList<>();
                for (UCustmandocResponse queryResponse : uCustmandocResponses) {
                    UdiwmsUnitResponse custmandocResponse = new UdiwmsUnitResponse();
                    custmandocResponse.setAddr(queryResponse.getParentvo().getConaddr());
                    custmandocResponse.setUnitId(queryResponse.getParentvo().getCustcode());
                    custmandocResponse.setName(queryResponse.getParentvo().getCustname());
                    custmandocResponse.setSpell(queryResponse.getParentvo().getMnecode());
                    custmandocResponse.setMobile(queryResponse.getParentvo().getPhone1());
                    custmandocResponse.setContact(queryResponse.getParentvo().getLinkman1());
                    custmandocResponses.add(custmandocResponse);
                }
                PageSimpleResponse<UdiwmsUnitResponse> pageSimpleResponse = new PageSimpleResponse<>();
                pageSimpleResponse.setTotal((long) response.getData().getAllcount());
                pageSimpleResponse.setList(custmandocResponses);
                return ResultVOUtils.success(pageSimpleResponse);
            }
        }
        return ResultVOUtils.error(500, "数据查询失败");
    }

    @Override
    public BaseResponse getProducts(UdiwmsProductRequest udiwmsProductRequest) {
        UInvmandocRequest uInvmandocRequest = new UInvmandocRequest(udiwmsProductRequest.getPage(), udiwmsProductRequest.getLimit());
        uInvmandocRequest.setInvname(udiwmsProductRequest.getName());
        uInvmandocRequest.setInvcode(udiwmsProductRequest.getCode());
        uInvmandocRequest.setUnitcode(getSetEntity().getUnitCode());
        UBaseResponse<List<UInvmandocQueryResponse>> response = uCloudHttpClient.invmandocQuery(uInvmandocRequest);
        if (response != null) {
            List<UInvmandocQueryResponse> uCustmandocResponses = response.getData().getDatas();
            if (uCustmandocResponses != null && !uCustmandocResponses.isEmpty()) {
                List<UdiwmsProductInfoResponse> custmandocResponses = new ArrayList<>();
                for (UInvmandocQueryResponse queryResponse : uCustmandocResponses) {
                    UdiwmsProductInfoResponse invmandocResponse = new UdiwmsProductInfoResponse();
                    invmandocResponse.setCode(queryResponse.getParentvo().getInvbasVo().getInvcode());
                    invmandocResponse.setName(queryResponse.getParentvo().getInvbasVo().getInvname());
                    invmandocResponse.setMeasname(queryResponse.getParentvo().getInvbasVo().getMeasname());
                    invmandocResponse.setSpec(queryResponse.getParentvo().getInvbasVo().getInvspec());
                    invmandocResponse.setStandard(invmandocResponse.getSpec());
                    invmandocResponse.setRegisterNo(queryResponse.getParentvo().getInvbasVo().getInvtype());
                    invmandocResponse.setManufactory(queryResponse.getParentvo().getInvmanVo().getProdarea());
                    custmandocResponses.add(invmandocResponse);
                }
                PageSimpleResponse<UdiwmsProductInfoResponse> pageSimpleResponse = new PageSimpleResponse<>();
                pageSimpleResponse.setTotal((long) response.getData().getAllcount());
                pageSimpleResponse.setList(custmandocResponses);
                return ResultVOUtils.success(pageSimpleResponse);
            }
        }
        return ResultVOUtils.success(response);
    }

    @Override
    public BaseResponse getOrderTypes(UdiwmsBusTypeRequest udiwmsBusTypeRequest) {
        BaseResponse<PageSimpleResponse<ThrSystemBusApiEntity>> baseResponse = new BaseResponse<>();
        PageSimpleResponse<ThrSystemBusApiEntity> pageSimpleResponse = new PageSimpleResponse<>();
        pageSimpleResponse.setTotal(4L);

        ThrSystemBusApiEntity thrSystemBusApiEntity = new ThrSystemBusApiEntity();
        thrSystemBusApiEntity.setCode("采购入库");
        thrSystemBusApiEntity.setName("采购入库");

        ThrSystemBusApiEntity thrSystemBusApiEntity1 = new ThrSystemBusApiEntity();
        thrSystemBusApiEntity1.setCode("采购入库");
        thrSystemBusApiEntity1.setName("采购入库");

        ThrSystemBusApiEntity thrSystemBusApiEntity2 = new ThrSystemBusApiEntity();
        thrSystemBusApiEntity2.setCode("采购入库");
        thrSystemBusApiEntity2.setName("采购入库");

        ThrSystemBusApiEntity thrSystemBusApiEntity3 = new ThrSystemBusApiEntity();
        thrSystemBusApiEntity3.setCode("采购入库");
        thrSystemBusApiEntity3.setName("采购入库");

        List<ThrSystemBusApiEntity> list = new ArrayList<>(4);
        list.add(thrSystemBusApiEntity);
        list.add(thrSystemBusApiEntity1);
        list.add(thrSystemBusApiEntity2);
        list.add(thrSystemBusApiEntity3);

        pageSimpleResponse.setList(list);
        baseResponse.setData(pageSimpleResponse);
        return baseResponse;
    }

    @Override
    public BaseResponse getOrders(UdiwmsOrderQueryRequest udiwmsOrderQueryRequest) {
        getSetEntity();
        if (null != udiwmsOrderQueryRequest) {
            if (StrUtil.isBlank(udiwmsOrderQueryRequest.getAction()) && StrUtil.isBlank(udiwmsOrderQueryRequest.getBillAction())) {
                return ResultVOUtils.error(500, "单据类型不能为空！");
            }
            if (StrUtil.isBlank(udiwmsOrderQueryRequest.getStartDate())) {
                return ResultVOUtils.error(500, "起始时间不能为空");
            }
            if (StrUtil.isBlank(udiwmsOrderQueryRequest.getEndDate())) {
                return ResultVOUtils.error(500, "结束时间不能为空");
            }
        } else {
            return ResultVOUtils.error(500, "参数不能为空");
        }
        if ("WareHouseIn".equals(udiwmsOrderQueryRequest.getAction()) || udiwmsOrderQueryRequest.getBillAction().equals("采购入库")
                || udiwmsOrderQueryRequest.getBillAction().equals("退货出库")) {
            UPurchaseinRequest uPurchaseinRequest = new UPurchaseinRequest();
            uPurchaseinRequest.setDate_end(udiwmsOrderQueryRequest.getEndDate());
            uPurchaseinRequest.setDate_begin(udiwmsOrderQueryRequest.getStartDate());
            uPurchaseinRequest.setPage_now(udiwmsOrderQueryRequest.getPage() + "");
            uPurchaseinRequest.setPage_size(udiwmsOrderQueryRequest.getLimit() + "");
            uPurchaseinRequest.setCorp(udiwmsOrderQueryRequest.getCorpName());
            // 3是已签字  2是未签字
//            if ("0".equals(udiwmsOrderRequest.getBillFlag())) {
//                uPurchaseinRequest.setFbillflag("2");
//            } else if (("1").equals(udiwmsOrderRequest.getBillFlag())) {
//                uPurchaseinRequest.setFbillflag("3");
//            }
            uPurchaseinRequest.setBillcode(udiwmsOrderQueryRequest.getBillNo());
            BaseResponse<PageSimpleResponse<UdiwmsOrderResponse>> baseResponse = getPurchasein(uPurchaseinRequest);
//            if (baseResponse.getCode() != 20000) {
//
//            }
            return baseResponse;
//            List<UdiwmsOrderResponse> udiwmsOrderResponses = baseResponse.getData().getList();
//            if (udiwmsOrderResponses != null && udiwmsOrderResponses.size() > 0) {
//                List<UdiwmsOrderResponse> returnsList = new ArrayList<>();
//                if ("采购入库".equals(udiwmsOrderQueryRequest.getBillAction())) {
//                    for (int i = 0; i < udiwmsOrderResponses.size(); i++) {
//                        if (udiwmsOrderResponses.get(i).getBillType().equals("采购入库")) {
//                            returnsList.add(udiwmsOrderResponses.get(i));
//                        }
//                    }
//                } else if ("退货出库".equals(udiwmsOrderQueryRequest.getBillAction())) {
//                    for (int i = 0; i < udiwmsOrderResponses.size(); i++) {
//                        if (udiwmsOrderResponses.get(i).getBillType().equals("退货出库")) {
//                            returnsList.add(udiwmsOrderResponses.get(i));
//                        }
//                    }
//                } else if ("WareHouseIn".equals(udiwmsOrderQueryRequest.getAction())) {
//                    returnsList.addAll(udiwmsOrderResponses);
//                }
//                PageSimpleResponse<UdiwmsOrderResponse> pageSimpleResponse = new PageSimpleResponse<>();
//                pageSimpleResponse.setTotal((long) returnsList.size());
//                pageSimpleResponse.setList(returnsList);
//                return ResultVOUtils.success(pageSimpleResponse);
//            } else
//                return ResultVOUtils.error(500, "未查询到数据");

        } else if (
                "WareHouseOut".equals(udiwmsOrderQueryRequest.getAction()) ||
                        udiwmsOrderQueryRequest.getBillAction().equals("销售出库") || udiwmsOrderQueryRequest.getBillAction().equals("退货入库")) {

            USaleoutQueryRequest uSaleoutQueryRequest = new USaleoutQueryRequest();
            uSaleoutQueryRequest.setDate_end(udiwmsOrderQueryRequest.getEndDate());
            uSaleoutQueryRequest.setDate_begin(udiwmsOrderQueryRequest.getStartDate());
            uSaleoutQueryRequest.setPage_now(udiwmsOrderQueryRequest.getPage() + "");
            uSaleoutQueryRequest.setPage_size(udiwmsOrderQueryRequest.getLimit() + "");
            uSaleoutQueryRequest.setCorp(udiwmsOrderQueryRequest.getCorpName());
            // 3是已签字  2是未签字
            if ("0".equals(udiwmsOrderQueryRequest.getBillFlag())) {
                uSaleoutQueryRequest.setFbillflag("2");
            } else if (("1").equals(udiwmsOrderQueryRequest.getBillFlag())) {
                uSaleoutQueryRequest.setFbillflag("3");
            }
            uSaleoutQueryRequest.setBillcode(udiwmsOrderQueryRequest.getBillNo());
            BaseResponse<PageSimpleResponse<UdiwmsOrderResponse>> baseResponse = getSaleout(uSaleoutQueryRequest);
            return baseResponse;
//            List<UdiwmsOrderResponse> udiwmsOrderResponses = baseResponse.getData().getList();
//            if (udiwmsOrderResponses != null && udiwmsOrderResponses.size() > 0) {
//                List<UdiwmsOrderResponse> returnsList = new ArrayList<>();
//                if ("销售出库".equals(udiwmsOrderQueryRequest.getBillAction())) {
//                    for (int i = 0; i < udiwmsOrderResponses.size(); i++) {
//                        if (udiwmsOrderResponses.get(i).getBillType().equals("销售出库")) {
//                            returnsList.add(udiwmsOrderResponses.get(i));
//                        }
//                    }
//                } else if ("退货入库".equals(udiwmsOrderQueryRequest.getBillAction())) {
//                    for (int i = 0; i < udiwmsOrderResponses.size(); i++) {
//                        if (udiwmsOrderResponses.get(i).getBillType().equals("退货入库")) {
//                            returnsList.add(udiwmsOrderResponses.get(i));
//                        }
//                    }
//                } else if ("WareHouseIn".equals(udiwmsOrderQueryRequest.getAction())) {
//                    returnsList.addAll(udiwmsOrderResponses);
//                }
//                PageSimpleResponse<UdiwmsOrderResponse> pageSimpleResponse = new PageSimpleResponse<>();
//                pageSimpleResponse.setTotal((long) returnsList.size());
//                pageSimpleResponse.setList(returnsList);
//                return ResultVOUtils.success(pageSimpleResponse);
//            } else
//                return ResultVOUtils.error(500, "未查询到数据");
        } else {
            return ResultVOUtils.error(500, "单据类型错误！");
        }
    }

    @Override
    public BaseResponse submitOrders(UdiwmsOrderRequest udiwmsOrderRequest) {
        return null;
    }

    @Override
    public BaseResponse getInvProducts(UdiwmsInvProductsRequest udiwmsInvProductsRequest) {
        if (udiwmsInvProductsRequest.getPage() > 1) {
            return ResultVOUtils.error(500, "超出最大数");
        }
        getSetEntity();
        UOnhandQueryRequest onhandQueryRequest = new UOnhandQueryRequest();
        if (udiwmsInvProductsRequest.getInventoryCode() != null) {
            onhandQueryRequest.setInventory(udiwmsInvProductsRequest.getInventoryCode());
        } else if (udiwmsInvProductsRequest.getInventoryCodes() != null) {
            onhandQueryRequest.setInventorys(udiwmsInvProductsRequest.getInventoryCodes());
        }
        onhandQueryRequest.setPage_size(udiwmsInvProductsRequest.getLimit() + "");
        onhandQueryRequest.setPage_now(udiwmsInvProductsRequest.getPage() + "");
        onhandQueryRequest.setBatchcode(udiwmsInvProductsRequest.getBatchNo());
        onhandQueryRequest.setWarehouse(udiwmsInvProductsRequest.getWarehouseCode());
        onhandQueryRequest.setCorp(setEntity.getUnitCode());
        onhandQueryRequest.setCalbody(setEntity.getUnitCode());
        UBaseResponse<List<UOnhandQueryResponse>> response = uCloudHttpClient.onhandQuery(onhandQueryRequest);
        if (response != null) {
            List<UOnhandQueryResponse> uOnhandQueryResponses;
            uOnhandQueryResponses = response.getData().getDatas();
            if (uOnhandQueryResponses != null && uOnhandQueryResponses.size() > 0) {
                List<UdiwmsInvProductResponse> invProductResponses = new ArrayList<>();
                for (UOnhandQueryResponse onhandQueryResponse : uOnhandQueryResponses) {
                    UdiwmsInvProductResponse invProductResponse = new UdiwmsInvProductResponse();
                    invProductResponse.setName(onhandQueryResponse.getCinventory_name());
                    invProductResponse.setBatchNo(onhandQueryResponse.getVbatchcode());
//                    if (StrUtil.isNotEmpty(onhandQueryResponse.getVbatchcode())) {
//                        String str = onhandQueryResponse.getVbatchcode();
//                        String regex = "\\(.*?\\)";
//                        str = str.replaceAll(regex, "");
//                        invProductResponse.setBatchNo(str);
//                    }
                    invProductResponse.setCode(onhandQueryResponse.getCinventory_code());
                    invProductResponse.setCount(onhandQueryResponse.getNnum());
                    invProductResponse.setSpec(onhandQueryResponse.getInvspec());
                    invProductResponse.setRegisterCertNo(onhandQueryResponse.getInvtype());
                    invProductResponse.setWarehouseName(onhandQueryResponse.getCwarehouse_name());
                    invProductResponse.setWarehouseCode(onhandQueryResponse.getCwarehouse_code());
                    invProductResponses.add(invProductResponse);
                }
                PageSimpleResponse<UdiwmsInvProductResponse> pageSimpleResponse = new PageSimpleResponse<>();
                pageSimpleResponse.setTotal(invProductResponses.size() + 0L);
                pageSimpleResponse.setList(invProductResponses);
                return ResultVOUtils.success(pageSimpleResponse);
            }

        }
        return ResultVOUtils.success(response);
    }

    @Override
    public BaseResponse getWarehouse(UdiwmsWarehouseRequest udiwmsWarehouseRequest) {
        return ResultVOUtils.success();
    }

    @Override
    public BaseResponse testConnect() {
        UdiwmsUnitRequest udiwmsUnitRequest = new UdiwmsUnitRequest();
        udiwmsUnitRequest.setPage(1);
        udiwmsUnitRequest.setLimit(2);
        BaseResponse baseResponse = getUnit(udiwmsUnitRequest);
        if (baseResponse.getCode() == 20000) {
            return ResultVOUtils.success("连接成功！");
        }
        return baseResponse;
    }

    @Override
    public BaseResponse queryUdiInvProducts(UdiwmsQueryUdiInvProductsRequest queryUdiInvProductsRequest) {
        return null;
    }

    @Override
    public BaseResponse submitProducts(UdiwmsAddProductsRequest udiwmsAddProductsRequest) {
        return null;
    }

    @Override
    public BaseResponse submitUnit(UdiwmsAddUnitRequest udiwmsAddUnitRequest) {
        return null;
    }

    @Override
    public BaseResponse submitPreInOrders(UdiwmsPreInOrderRequest udiwmsPreInOrderRequest) {
        return null;
    }

    @Override
    public BaseResponse postProducts(PostThrProductsRequest postThrProductsRequest) {
        return null;
    }

    @Override
    public BaseResponse postCorps(PostThrCorpRequest postThrCorpRequest) {
        return null;
    }

    @Override
    public BaseResponse postOrderDetail(PostFileThrOrderRequest postFileThrOrderRequest) {
        return null;
    }

    @Override
    public BaseResponse postInvProducts(PostThrInvProductsRequest postThrInvProductsRequest) {
        return null;
    }

    /**
     * 提交采购计划
     *
     * @param udiwmsOrderRequest
     * @return
     */
    public BaseResponse postOrder(UdiwmsOrderRequest udiwmsOrderRequest) {

        return null;
    }

    /**
     * 获取现存量
     *
     * @param udiwmsInvProductsRequest
     * @return
     */
    public BaseResponse getOnHand(UdiwmsInvProductsRequest udiwmsInvProductsRequest) {
        UOnhandQueryRequest onhandQueryRequest = new UOnhandQueryRequest();
        if (udiwmsInvProductsRequest.getInventoryCode() != null) {
            onhandQueryRequest.setInventory(udiwmsInvProductsRequest.getInventoryCode());
        } else if (udiwmsInvProductsRequest.getInventoryCodes() != null) {
            onhandQueryRequest.setInventorys(udiwmsInvProductsRequest.getInventoryCodes());
        }
        onhandQueryRequest.setPage_size(udiwmsInvProductsRequest.getLimit() + "");
        onhandQueryRequest.setPage_now(udiwmsInvProductsRequest.getPage() + "");
        onhandQueryRequest.setBatchcode(udiwmsInvProductsRequest.getBatchNo());
        onhandQueryRequest.setWarehouse(udiwmsInvProductsRequest.getWarehouseCode());
        onhandQueryRequest.setCorp(setEntity.getUnitCode());
        onhandQueryRequest.setCalbody(setEntity.getUnitCode());
        UBaseResponse<List<UOnhandQueryResponse>> response = uCloudHttpClient.onhandQuery(onhandQueryRequest);
        if (response != null) {
            List<UOnhandQueryResponse> uOnhandQueryResponses;
            uOnhandQueryResponses = response.getData().getDatas();
            if (uOnhandQueryResponses != null && uOnhandQueryResponses.size() > 0) {
                List<UdiwmsOnhandResponse> udiwmsOnhandResponses = new ArrayList<>();
                for (UOnhandQueryResponse onhandQueryResponse : uOnhandQueryResponses) {
                    UdiwmsOnhandResponse udiwmsOnhandResponse = new UdiwmsOnhandResponse();
                    udiwmsOnhandResponse.setBatchNo(onhandQueryResponse.getVbatchcode());
                    udiwmsOnhandResponse.setInventoryName(onhandQueryResponse.getCinventory_name());
                    udiwmsOnhandResponse.setInventoryCode(onhandQueryResponse.getCinventory_code());
                    udiwmsOnhandResponse.setCount(onhandQueryResponse.getNnum());
                    udiwmsOnhandResponse.setRegisterCertNo(onhandQueryResponse.getInvtype());
                    udiwmsOnhandResponse.setSpec(onhandQueryResponse.getInvspec());
                    udiwmsOnhandResponse.setWarehouseName(onhandQueryResponse.getCwarehouse_name());
                    udiwmsOnhandResponse.setWarehouseCode(onhandQueryResponse.getCwarehouse_code());
                    udiwmsOnhandResponses.add(udiwmsOnhandResponse);
                }

                PageSimpleResponse<UdiwmsOnhandResponse> pageSimpleResponse = new PageSimpleResponse<>();
                pageSimpleResponse.setTotal(udiwmsOnhandResponses.size() + 0L);
                pageSimpleResponse.setList(udiwmsOnhandResponses);
                return ResultVOUtils.success(pageSimpleResponse);
            }

        }
        return ResultVOUtils.success(response);
    }

    /**
     * 单据签名
     *
     * @param signRequest
     * @return
     */
    public BaseResponse signBill(USignRequest signRequest) {
        if (signRequest.getBillAction().equals("PurchaseWareHouseIn") || signRequest.getBillAction().equals("ReturnWareHouseOut")) {
            return purchaseinSign(signRequest);
        } else if (signRequest.getBillAction().equals("SalesWareHouseOut") || signRequest.getBillAction().equals("ReturnWareHouseIn")) {
            return saleSign(signRequest);
        } else {
            return ResultVOUtils.error(500, "单据类型错误！");
        }
    }

    /**
     * 修改erp订单
     *
     * @param udiwmsPostBillRequest
     * @return
     */
    public BaseResponse modifyBill(UdiwmsPostBillRequest udiwmsPostBillRequest) {
        Map<String, List<ErpOrderEntity>> postMap = udiwmsPostBillRequest.getPostMap();
        if (udiwmsPostBillRequest.getModifyType() != null && "sign".equals(udiwmsPostBillRequest.getModifyType())) {
            USignRequest signRequest = new USignRequest();
            signRequest.setBillcode(udiwmsPostBillRequest.getBillNo());
            signRequest.setBillAction(udiwmsPostBillRequest.getBillAction());
            if (signRequest.getBillAction().equals("PurchaseWareHouseIn") || signRequest.getBillAction().equals("ReturnWareHouseOut")) {
                return purchaseinSign(signRequest);
            } else if (signRequest.getBillAction().equals("SalesWareHouseOut") || signRequest.getBillAction().equals("ReturnWareHouseIn")) {
                return saleSign(signRequest);
            } else {
                return ResultVOUtils.error(500, "单据类型错误！");
            }
        } else {
            ModifyPruchaseinRequest modifyPruchaseinRequest = new ModifyPruchaseinRequest();
            modifyPruchaseinRequest.setBillAction(udiwmsPostBillRequest.getBillAction());
            List<ModifyPruchaseinRequest.GeneralBillVOBean> generalBillVOBeans = new ArrayList<>();
            for (List<ErpOrderEntity> entities : postMap.values()) {
                ModifyPruchaseinRequest.GeneralBillVOBean generalBillVOBean = new ModifyPruchaseinRequest.GeneralBillVOBean();
                ModifyPruchaseinRequest.GeneralBillVOBean.ParentVOBean parentVOBean = new ModifyPruchaseinRequest.GeneralBillVOBean.ParentVOBean();
                List<ModifyPruchaseinRequest.GeneralBillVOBean.ChildrenVOBean> subPurchases = new ArrayList<>();
                for (ErpOrderEntity erpOrderEntity : entities) {
                    if (erpOrderEntity.getErpOrderId() != null) {
                        parentVOBean.setVbillcode(erpOrderEntity.getErpOrderId());
                        ModifyPruchaseinRequest.GeneralBillVOBean.ChildrenVOBean childrenVOBean = new ModifyPruchaseinRequest.GeneralBillVOBean.ChildrenVOBean();
                        childrenVOBean.setCinventoryid(erpOrderEntity.getGoodsid());
                        if (erpOrderEntity.getCount() != 0) {
                            childrenVOBean.setNinnum(erpOrderEntity.getCount() + "");
                            childrenVOBean.setNoutnum(erpOrderEntity.getCount() + "");
                            subPurchases.add(childrenVOBean);
                        }
                    } else {
                        return ResultVOUtils.error(500, "批次号不匹配，请检查后修改！");
                    }
                }
                generalBillVOBean.setParentVO(parentVOBean);
                generalBillVOBean.setChildrenVO(subPurchases);
                generalBillVOBeans.add(generalBillVOBean);
            }
            modifyPruchaseinRequest.setGeneralBillVO(generalBillVOBeans);
            if (modifyPruchaseinRequest.getBillAction().equals("PurchaseWareHouseIn") || modifyPruchaseinRequest.getBillAction().equals("ReturnWareHouseOut")) {
                return modifyPurchasein(modifyPruchaseinRequest);
            } else if (modifyPruchaseinRequest.getBillAction().equals("SalesWareHouseOut") || modifyPruchaseinRequest.getBillAction().equals("ReturnWareHouseIn")) {
                return modifySale(modifyPruchaseinRequest);
            } else {
                return ResultVOUtils.error(500, "单据类型错误！");
            }
        }
    }

    private YyUcloudV31SetEntity getSetEntity() {
        if (null == setEntity) {
            setEntity = yyUcloudV31SetDao.findSetUp(null);
        }
        return setEntity;
    }

    public BaseResponse getPurchasein(UPurchaseinRequest uPurchaseinRequest) {
        uPurchaseinRequest.setCorp(setEntity.getUnitCode());
        UBaseResponse<List<UPurchaseinQueryResponse>> response = uCloudHttpClient.purchaseinQuery(uPurchaseinRequest);
        if (response != null) {
            List<UPurchaseinQueryResponse> uPurchaseinQueryResponses = response.getData().getDatas();
            if (uPurchaseinQueryResponses != null && !uPurchaseinQueryResponses.isEmpty()) {
                List<UdiwmsOrderResponse> purchaseinResponses = new ArrayList<>();
                for (UPurchaseinQueryResponse queryResponse : uPurchaseinQueryResponses) {
                    UdiwmsOrderResponse udiwmsOrderResponse = new UdiwmsOrderResponse();
                    UPurchaseinQueryResponse.ParentvoBean parentvoBean = queryResponse.getParentvo();
                    if (parentvoBean != null) {
                        udiwmsOrderResponse.setBilldate(parentvoBean.getDbilldate());
                        udiwmsOrderResponse.setCorpId(parentvoBean.getProvider_code());
                        udiwmsOrderResponse.setCorpName(parentvoBean.getProvider_name());
                        udiwmsOrderResponse.setBillNo(parentvoBean.getVbillcode());
                        udiwmsOrderResponse.setInvCode("1000100");
                        udiwmsOrderResponse.setOriginType("采购订单");
                        udiwmsOrderResponse.setSignFlag(parentvoBean.getFbillflag());
                        if (parentvoBean.isFreplenishflag()) {
                            udiwmsOrderResponse.setBillTypeName("退货出库");
                            udiwmsOrderResponse.setBillType("退货出库");
                        } else {
                            udiwmsOrderResponse.setBillType("采购入库");
                            udiwmsOrderResponse.setBillTypeName("采购入库");
                        }
                    }
                    List<UPurchaseinQueryResponse.ChildrenvoBean> childrenvoBeans = queryResponse.getChildrenvo();
                    if (childrenvoBeans != null && childrenvoBeans.size() > 0) {
                        List<UdiwmsOrderResponse.SubErpOrder> subPurchases = new ArrayList<>();
                        for (UPurchaseinQueryResponse.ChildrenvoBean childrenvoBean : childrenvoBeans) {
                            UdiwmsOrderResponse.SubErpOrder subErpOrder = new UdiwmsOrderResponse.SubErpOrder();
                            subErpOrder.setProductId(childrenvoBean.getInventory_code());
                            subErpOrder.setProductName(childrenvoBean.getInventory_name());
                            subErpOrder.setSpec(childrenvoBean.getInvspec());
                            subErpOrder.setCount(childrenvoBean.getNinnum());
                            subErpOrder.setReCount(childrenvoBean.getNshouldinnum());
                            subErpOrder.setBatchNo(childrenvoBean.getVbatchcode());
                            if (StrUtil.isNotEmpty(childrenvoBean.getVbatchcode())) {
                                String str = childrenvoBean.getVbatchcode();
                                String regex = "\\(.*?\\)";
                                str = str.replaceAll(regex, "");
                                subErpOrder.setBatchNo(str);
                            }
                            subErpOrder.setPrice(BigDecimal.valueOf(childrenvoBean.getNprice()));
                            subErpOrder.setExpireDate(childrenvoBean.getDvalidate());
                            subPurchases.add(subErpOrder);
                        }
                        udiwmsOrderResponse.setSubErpOrders(subPurchases);
                    }
                    purchaseinResponses.add(udiwmsOrderResponse);
                }

                PageSimpleResponse<UdiwmsOrderResponse> pageSimpleResponse = new PageSimpleResponse<>();
                pageSimpleResponse.setTotal(response.getData().getAllcount() + 0l);
                pageSimpleResponse.setList(purchaseinResponses);
                return ResultVOUtils.success(pageSimpleResponse);
            }
        }
        return ResultVOUtils.error(500, "获取单据失败!");
    }

    public BaseResponse getSaleout(USaleoutQueryRequest uSaleoutQueryRequest) {

        uSaleoutQueryRequest.setCorp(setEntity.getUnitCode());
        UBaseResponse<List<USaleoutQueryResponse>> response = uCloudHttpClient.saleoutQuery(uSaleoutQueryRequest);
        if (response != null) {
            List<USaleoutQueryResponse> uSaleoutQueryResponses = response.getData().getDatas();
            if (uSaleoutQueryResponses != null && !uSaleoutQueryResponses.isEmpty()) {
                List<UdiwmsOrderResponse> purchaseinResponses = new ArrayList<>();
                for (USaleoutQueryResponse queryResponse : uSaleoutQueryResponses) {
                    UdiwmsOrderResponse purchaseinResponse = new UdiwmsOrderResponse();
                    USaleoutQueryResponse.ParentvoBean parentvoBean = queryResponse.getParentvo();
                    if (parentvoBean != null) {
                        purchaseinResponse.setBilldate(parentvoBean.getDbilldate());
                        purchaseinResponse.setCorpId(parentvoBean.getCustomer_code());
                        purchaseinResponse.setCorpName(parentvoBean.getCustomer_name());
                        purchaseinResponse.setBillNo(parentvoBean.getVbillcode());
                        purchaseinResponse.setInvCode("1000100");
                        purchaseinResponse.setOriginType("销售订单");
                        purchaseinResponse.setSignFlag(parentvoBean.getFbillflag());
                        if (parentvoBean.isFreplenishflag()) {
                            purchaseinResponse.setBillType("退货入库");
                            purchaseinResponse.setBillTypeName("退货入库");
                        } else {
                            purchaseinResponse.setBillType("销售出库");
                            purchaseinResponse.setBillTypeName("销售出库");
                        }
                    }
                    List<USaleoutQueryResponse.ChildrenvoBean> childrenvoBeans = queryResponse.getChildrenvo();
                    if (childrenvoBeans != null && childrenvoBeans.size() > 0) {
                        List<UdiwmsOrderResponse.SubErpOrder> subPurchases = new ArrayList<>();
                        for (USaleoutQueryResponse.ChildrenvoBean childrenvoBean : childrenvoBeans) {
                            UdiwmsOrderResponse.SubErpOrder subPurchase = new UdiwmsOrderResponse.SubErpOrder();
                            subPurchase.setProductId(childrenvoBean.getInventory_code());
                            subPurchase.setProductName(childrenvoBean.getInventory_name());
                            subPurchase.setSpec(childrenvoBean.getInvspec());
                            subPurchase.setReCount(childrenvoBean.getNshouldoutnum());
                            subPurchase.setCount(childrenvoBean.getNoutnum());
                            subPurchase.setBatchNo(childrenvoBean.getVbatchcode());

                            if (StrUtil.isNotEmpty(childrenvoBean.getVbatchcode())) {
                                String str = childrenvoBean.getVbatchcode();
                                String regex = "\\(.*?\\)";
                                str = str.replaceAll(regex, "");
                                subPurchase.setBatchNo(str);
                            }

                            subPurchase.setPrice(BigDecimal.valueOf(childrenvoBean.getNsaleprice()));
                            subPurchases.add(subPurchase);
                        }
                        purchaseinResponse.setSubErpOrders(subPurchases);
                    }
                    purchaseinResponses.add(purchaseinResponse);
                }
                PageSimpleResponse<UdiwmsOrderResponse> pageSimpleResponse = new PageSimpleResponse<>();
                pageSimpleResponse.setTotal(response.getData().getAllcount() + 0l);
                pageSimpleResponse.setList(purchaseinResponses);
                return ResultVOUtils.success(pageSimpleResponse);
            }
        }
        return ResultVOUtils.error(500, "下载出库单据失败！");
    }

    //采购入库单签字
    public BaseResponse purchaseinSign(USignRequest uSignRequest) {
        UPurhaseinSignRequest uPurhaseinSignRequest = new UPurhaseinSignRequest();
        UPurhaseinSignRequest.QueryInfoBean queryInfoBean = new UPurhaseinSignRequest.QueryInfoBean();
        Map date = DateUtil.getYearTRange();
        queryInfoBean.setDate_begin((String) date.get("startDate"));
        queryInfoBean.setDate_end((String) date.get("endDate"));
        queryInfoBean.setCalbody(setEntity.getUnitCode());
        queryInfoBean.setCorp(setEntity.getUnitCode());
        queryInfoBean.setBillcode(uSignRequest.getBillcode());
        uPurhaseinSignRequest.setQueryInfo(queryInfoBean);
        UPurhaseinSignRequest.SignInfoBean signInfoBean = new UPurhaseinSignRequest.SignInfoBean();
        signInfoBean.setCoperator(setEntity.getCoperator());
        uPurhaseinSignRequest.setSignInfo(signInfoBean);
        UBaseResponse data = uCloudHttpClient.purchaseinSign(uPurhaseinSignRequest);
        if (data != null && data.getStatus().equals("success")) {
            return ResultVOUtils.success("修改成功");
        } else {
            if (data != null) {
                return ResultVOUtils.error(500, data.getErrormsg());
            }
            return ResultVOUtils.error(500, "连接U8云下载服务出错！");
        }
    }

    public BaseResponse saleSign(USignRequest uSignRequest) {
        USaleoutSignRequest uSaleoutSignRequest = new USaleoutSignRequest();
        USaleoutSignRequest.QueryInfoBean queryInfoBean = new USaleoutSignRequest.QueryInfoBean();
        Map date = DateUtil.getYearTRange();
        queryInfoBean.setDate_begin((String) date.get("startDate"));
        queryInfoBean.setDate_end((String) date.get("endDate"));
        queryInfoBean.setCalbody(setEntity.getUnitCode());
        queryInfoBean.setCorp(setEntity.getUnitCode());
        queryInfoBean.setBillcode(uSignRequest.getBillcode());
        uSaleoutSignRequest.setQueryInfo(queryInfoBean);
        USaleoutSignRequest.SignInfoBean signInfoBean = new USaleoutSignRequest.SignInfoBean();
        signInfoBean.setCoperator(setEntity.getCoperator());
        uSaleoutSignRequest.setSignInfo(signInfoBean);
        UBaseResponse data = uCloudHttpClient.saleOutSign(uSaleoutSignRequest);
        if (data != null && data.getStatus().equals("success")) {
            return ResultVOUtils.success("修改成功");
        } else {
            if (data != null) {
                return ResultVOUtils.error(500, data.getErrormsg());
            }
            return ResultVOUtils.error(500, "连接U8云下载服务出错！");
        }
    }

    public BaseResponse modifyPurchasein(ModifyPruchaseinRequest modifyPruchaseinRequest) {
        if (modifyPruchaseinRequest == null || modifyPruchaseinRequest.getGeneralBillVO() == null) {
            return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, "参数不能为null");
        }
        List<ModifyPruchaseinRequest.GeneralBillVOBean> generalBillVOBeans = modifyPruchaseinRequest.getGeneralBillVO();
        for (ModifyPruchaseinRequest.GeneralBillVOBean generalBillVOBean : generalBillVOBeans) {
            UPurchaseinRequest uPurchaseinRequest = new UPurchaseinRequest();
            uPurchaseinRequest.setBillcode(generalBillVOBean.getParentVO().getVbillcode());
            uPurchaseinRequest.setPage_size("1");
            uPurchaseinRequest.setPage_now("1");
            Map date = DateUtil.getYearTRange();
            uPurchaseinRequest.setDate_begin((String) date.get("startDate"));
            uPurchaseinRequest.setDate_end((String) date.get("endDate"));
            uPurchaseinRequest.setCorp(setEntity.getUnitCode());
            UBaseResponse<List<UPurchaseinQueryResponse>> response = uCloudHttpClient.purchaseinQuery(uPurchaseinRequest);

            List<UPurchaseinQueryResponse> uPurchaseinQueryResponses = response.getData().getDatas();
            if (uPurchaseinQueryResponses != null && !uPurchaseinQueryResponses.isEmpty()) {
                UPurchaseinQueryResponse uPurchaseinQueryResponse = uPurchaseinQueryResponses.get(0);
                UPurchaseinQueryResponse.ParentvoBean parentvoBean = uPurchaseinQueryResponse.getParentvo();
                generalBillVOBean.getParentVO().setCbiztype(parentvoBean.getBiztype_code());
                generalBillVOBean.getParentVO().setCoperatorid(parentvoBean.getOperator_code());
                generalBillVOBean.getParentVO().setCwarehouseid(parentvoBean.getWarehouseid_code());
                generalBillVOBean.getParentVO().setPk_calbody(setEntity.getUnitCode());
                generalBillVOBean.getParentVO().setPk_corp(setEntity.getUnitCode());
                generalBillVOBean.getParentVO().setVnote("扫码修改");
                generalBillVOBean.setChildrenVO(transChild(modifyPruchaseinRequest.getBillAction(), generalBillVOBean, uPurchaseinQueryResponse));
            }
        }
        modifyPruchaseinRequest.setGeneralBillVO(generalBillVOBeans);
        UBaseResponse data = uCloudHttpClient.purchaseinModify(modifyPruchaseinRequest);
        if (data != null && data.getStatus().equals("success")) {
            return ResultVOUtils.success("修改成功");
        } else {
            if (data != null) {
                return ResultVOUtils.error(500, data.getErrormsg());
            }
            return ResultVOUtils.error(500, "连接U8云下载服务出错！");
        }


    }

    public BaseResponse modifySale(ModifyPruchaseinRequest modifyPruchaseinRequest) {
        if (modifyPruchaseinRequest == null || modifyPruchaseinRequest.getGeneralBillVO() == null) {
            return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, "参数不能为null");
        }
        List<ModifyPruchaseinRequest.GeneralBillVOBean> generalBillVOBeans = modifyPruchaseinRequest.getGeneralBillVO();
        for (ModifyPruchaseinRequest.GeneralBillVOBean generalBillVOBean : generalBillVOBeans) {
            USaleoutQueryRequest uPurchaseinRequest = new USaleoutQueryRequest();
            uPurchaseinRequest.setBillcode(generalBillVOBean.getParentVO().getVbillcode());
            uPurchaseinRequest.setPage_size("1");
            uPurchaseinRequest.setPage_now("1");
            Map date = DateUtil.getYearTRange();
            uPurchaseinRequest.setDate_begin((String) date.get("startDate"));
            uPurchaseinRequest.setDate_end((String) date.get("endDate"));
            uPurchaseinRequest.setCorp("1110");
            UBaseResponse<List<USaleoutQueryResponse>> response = uCloudHttpClient.saleoutQuery(uPurchaseinRequest);

            List<USaleoutQueryResponse> uPurchaseinQueryResponses = response.getData().getDatas();
            if (uPurchaseinQueryResponses != null && !uPurchaseinQueryResponses.isEmpty()) {
                USaleoutQueryResponse uPurchaseinQueryResponse = uPurchaseinQueryResponses.get(0);
                USaleoutQueryResponse.ParentvoBean parentvoBean = uPurchaseinQueryResponse.getParentvo();
                generalBillVOBean.getParentVO().setCbiztype(parentvoBean.getBiztype_code());
                generalBillVOBean.getParentVO().setCoperatorid(parentvoBean.getOperator_code());
                generalBillVOBean.getParentVO().setCwarehouseid(parentvoBean.getWarehouseid_code());
                generalBillVOBean.getParentVO().setPk_calbody("1110");
                generalBillVOBean.getParentVO().setPk_corp("1110");
                generalBillVOBean.getParentVO().setVnote("校验修改");
                generalBillVOBean.getParentVO().setCcustomerid(parentvoBean.getCustomer_code());
                generalBillVOBean.setChildrenVO(transSaleChild(modifyPruchaseinRequest.getBillAction(), generalBillVOBean, uPurchaseinQueryResponse));
            }
        }
        modifyPruchaseinRequest.setGeneralBillVO(generalBillVOBeans);


        UBaseResponse data = uCloudHttpClient.saleModify(modifyPruchaseinRequest);
        if (data != null && data.getStatus().equals("success")) {
            return ResultVOUtils.success("修改成功");
        } else {
            if (data != null) {
                return ResultVOUtils.error(500, data.getErrormsg());
            }
            return ResultVOUtils.error(500, "连接U8云下载服务出错！");
        }
    }

    public List<ModifyPruchaseinRequest.GeneralBillVOBean.ChildrenVOBean> transChild(String action, ModifyPruchaseinRequest.GeneralBillVOBean generalBillVOBean, UPurchaseinQueryResponse uPurchaseinQueryResponse) {
        List<ModifyPruchaseinRequest.GeneralBillVOBean.ChildrenVOBean> childrenVOBeans = generalBillVOBean.getChildrenVO();
        List<UPurchaseinQueryResponse.ChildrenvoBean> temps = uPurchaseinQueryResponse.getChildrenvo();
        if (childrenVOBeans != null && temps != null) {
            for (ModifyPruchaseinRequest.GeneralBillVOBean.ChildrenVOBean childrenVOBean : childrenVOBeans) {
                for (int i = 0; i < temps.size(); i++) {
                    if (childrenVOBean.getCinventoryid().equals(temps.get(i).getInventory_code())) {
//                        childrenVOBean.setNprice(temps.get(i).getNprice() + "");
                        childrenVOBean.setCrowno(temps.get(i).getCrowno());
                        if (action.equals("ReturnWareHouseOut")) {
                            childrenVOBean.setNoutnum((-Integer.parseInt(childrenVOBean.getNinnum())) + "");
                        }
                    }
                }
            }
        }
        return childrenVOBeans;
    }

    public List<ModifyPruchaseinRequest.GeneralBillVOBean.ChildrenVOBean> transSaleChild(String action, ModifyPruchaseinRequest.GeneralBillVOBean generalBillVOBean, USaleoutQueryResponse uSaleoutQueryResponse) {
        List<ModifyPruchaseinRequest.GeneralBillVOBean.ChildrenVOBean> childrenVOBeans = generalBillVOBean.getChildrenVO();
        List<USaleoutQueryResponse.ChildrenvoBean> temps = uSaleoutQueryResponse.getChildrenvo();
        if (childrenVOBeans != null && temps != null) {
            for (ModifyPruchaseinRequest.GeneralBillVOBean.ChildrenVOBean childrenVOBean : childrenVOBeans) {
                for (int i = 0; i < temps.size(); i++) {
                    if (childrenVOBean.getCinventoryid().equals(temps.get(i).getInventory_code())) {
//                        childrenVOBean.setNprice(temps.get(i).getNsaleprice() + "");
//                        childrenVOBean.setNoutnum(temps.get(i).getNoutnum() + "");
                        childrenVOBean.setCrowno(temps.get(i).getCrowno());
                        if (action.equals("ReturnWareHouseIn")) {
                            childrenVOBean.setNoutnum((-Integer.parseInt(childrenVOBean.getNoutnum())) + "");
                        }

                    }

                }
            }
        }
        return childrenVOBeans;
    }


}
