supplyItem.js 9.8 KB
(function () {
    "use strict";
    var demandSupplychainItemIndexCtrl = function ($scope, $modal, $http, customizationService,masterService, dialogService) {
        $scope.$emit('navShow',2);
        //分页传入的数值
        $scope.vm = {
            pageNumber: 1,
            pageSize: 10,
            isActive: true,
            totalCount: 0
        };
        //分页
        $scope.page = {
            pageSize: 10,
            pageNumber: 1
        };
        //获取供应链
        $scope.networks = [];
        $scope.getSupplychainNetworkList = function () {
            customizationService.apiCustomizationDemandFindSupplychainNetwork({pageSize: 0,demandIsActive:true, supplyIsActive:true}).success(function (data) {
                if (data.errors == null || data.errors.length > 0) {
                    dialogService.tip(data.errors);
                } else {
                    angular.forEach(data.result, function (network) {
                        network.showOperatingUnitName = network.demandTenantName + "  ——  " + network.supplyTenantName;
                        $scope.networks.push(network);
                    });
                }
            });
        };
        //列表 数生成
        $scope.getSupplychainNetworkList();

        $scope.doSearch = function () {
            if ($scope.vm.network == null) {
                dialogService.tip([{message: "请选择供应链"}]);
            } else {
                $scope.import.params = {supplyTenantId: 0,netWorkId :$scope.vm.network.id};
                $scope.vm.supplyTenantId = $scope.vm.network.supplyTenantId;
                $scope.import.params.supplyTenantId = $scope.vm.network.supplyTenantId;
                $scope.vm.tenantId = $scope.vm.network.supplyTenantId;
                $scope.vm.networkId = $scope.vm.network.id;
                $scope.vm.supplyIsActive = true;
                if($scope.vm.keyword && "" == $scope.vm.keyword) {
                    $scope.vm.keyword = undefined;
                }

                customizationService.apiCustomizationSupplychainItemDemandFind($scope.vm).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        $scope.vm.totalCount = data.totalCount;

                        //组建新的树
                        if (data.result != null && data.result.length > 0) {
                            var parentIndex = 0;
                            angular.forEach(data.result, function (result) {
                                if (parentIndex % 2 == 0) {
                                    result.color = "color-blue";
                                } else {
                                    result.color = "color-yellow";
                                }
                                parentIndex++;
                                result.level = 0;
                                result.visable = true;
                            });
                        }
                        $scope.trees = data.result;
                    }
                });
            }
        };


        //获取物料
        $scope.itemOptions = {
            isActive: true,
            pageSize: 10,
            pageNumber: 1,
            totalCount: 0,
            methodName: "doFindOwnerItem"
        };
        //自身开放物料
        $scope.doFindOwnerItem = function () {
            masterService.apiMasterItemFind($scope.itemOptions).success(function (data) {
                if (data.errors == null || data.errors.length > 0) {
                    dialogService.tip(data.errors);
                } else {
                    $scope.ownerItemList = data.result;
                    $scope.itemOptions.totalCount = data.totalCount;
                    angular.forEach($scope.ownerItemList, function (item) {
                        item.showName = item.name + "---" + item.number;
                        if(item.specCode) {
                            item.showName = item.showName + "---" + item.specCode;
                        }
                    });
                }
            });
        };

        $scope.selectOwnerItem = function (tree, ownerItem) {
            tree.demandItemId = ownerItem.id;
            tree.demandItemName = ownerItem.name;
            tree.demandItemNumber = ownerItem.number;
            tree.demandItemSpecCode = ownerItem.specCode;
            tree.demandItemSpecName = ownerItem.specName;
        };

        $scope.saveDemandItem = function(){
            angular.forEach($scope.trees, function (item) {
                if(item.demandItemNumber) {
                    //判断 当前自己物料是不是已经被使用
                    $scope.params = {};
                    $scope.params.supplyTenantId = $scope.vm.supplyTenantId;
                    $scope.params.networkId = $scope.vm.networkId;
                    $scope.params.demandItemId = item.demandItemId;
                    $scope.params.exitId = item.id;
                    customizationService.apiCustomizationSupplychainItemDemandFind($scope.params).success(function (data) {
                        if (data.errors == null || data.errors.length > 0) {
                            dialogService.tip(data.errors);
                            $scope.doSearch();
                        } else {
                            if (data.totalCount > 0) {
                                dialogService.tip([{message: "该物料已经存在此供应商映射关系"}]);
                                $scope.doSearch();
                            } else {
                                item.demandIsActive = true;
                                customizationService.apiCustomizationSupplychainItemUpdate(item).success(function (data) {
                                    if (data.errors == null || data.errors.length > 0) {
                                        dialogService.tip(data.errors);
                                        $scope.doSearch();
                                    } else {
                                        dialogService.tip([{message: "物料映射关系绑定成功"}]);
                                        $scope.doSearch();
                                    }
                                });
                            }
                        }
                    });
                } else {
                    item.demandItemId = null;
                    item.demandItemName = null;
                    item.demandItemNumber = null;
                    item.demandItemSpecCode = null;
                    item.demandItemSpecName = null;
                    item.demandIsActive = null;
                    item.demandActiveTime = null;
                    item.demandActiveUserId = null;
                    item.demandActiveUserName = null;

                    customizationService.apiCustomizationSupplychainItemUpdate(item).success(function (data) {
                        if (data.errors == null || data.errors.length > 0) {
                            dialogService.tip(data.errors);
                            $scope.doSearch();
                        } else {
                            dialogService.tip([{message: "物料映射关系绑定成功"}]);
                            $scope.doSearch();
                        }
                    });
                }
            });
        };

        $scope.import = {
            title: "导入映射关系",
            uploadMethod: "api.customization.supplychain.item.upload",
            downLoadUrl: "/customization/api.do",
            downLoadMethod: "api.customization.supplychain.itemTemp.export",
            params: {supplyTenantId: 0}
        };

        /**
         * 导出
         */
        $scope.doExport = function () {
            customizationService.apiCustomizationSupplyExportSupplychainItem($scope.vm).success(function (data) {
                
                if (data.errors != null && data.errors.length > 0) {
                    dialogService.tip(data.errors);
                } else {
                    window.open(data.url, "_self");
                }
            });
        };

        $scope.bing = function(tree){
            var req = {};
            req.id = tree.id;
            req.supplychainId = $scope.vm.network.id;
            req.autoNext = false;
            customizationService.apiCustomizationSupplychainItemCopy(req).success(function(data) {
                if (data.errors != null && data.errors.length > 0) {
                    dialogService.tip(data.errors);
                } else {
                    dialogService.tip([{message:"操作成功"}]);
                    $scope.doSearch();
                }
            })
        };

        $scope.bingAll = function(){
            var req = {};
            req.id = $scope.trees[0].id;
            req.supplychainId = $scope.vm.network.id;
            req.autoNext = true;
            req.syncPageNumber = 1;
            customizationService.apiCustomizationSupplychainItemCopy(req).success(function(data) {
                if (data.errors != null && data.errors.length > 0) {
                    dialogService.tip(data.errors);
                } else {
                    dialogService.tip([{message:"正在同步,请稍候"}]);
                    $scope.doSearch();
                }
            })
        };
        //监听
        $scope.$watch(function () {
            return $scope.import.data;
        }, function (newDate, oldDate) {
            if (oldDate == null && newDate != null) {
                if (newDate.isSuccessful){
                    location.reload(true);
                }
            }
        });
    };

    angular.module("xn.page", [])
        .controller("DemandSupplychainItemIndexCtrl", ["$scope", "$modal", "$http", "CustomizationService", "MasterService","dialogService", demandSupplychainItemIndexCtrl])
})();