aInicheRules.js 9.26 KB
(function (angular) {
  var reg = {
    priceReg: /^\d[\d\.]*$/,
    numReg: /^[1-9]\d*$/
  }
  // 属性名,新值,旧值,整数部分位数,小数部分位数
  const limitPrice = function (newValue, oldValue, intergerDigit, pointDigit, regRule) {
    var regRule = regRule || 'priceReg'
    if (newValue && reg[regRule].test(newValue + '')) {
      if ((/\./).test(newValue)) {
        var newValue = newValue + ''
        var interger = newValue.split('.')[0]
        var point = newValue.split('.')[1]
        var intergerNum = interger.length > intergerDigit ? oldValue.split('.')[0] : interger
        var pointNum = point.length > pointDigit ? point.slice(0, pointDigit) : point
        newValue = pointDigit ? intergerNum + '.' + pointNum : newValue.split('.')[0]
      } else {
        newValue = newValue.length > intergerDigit ? oldValue.slice(0, intergerDigit) : newValue
      }
      return newValue
    } else {
      return newValue ? oldValue : ''
    }
  }

  function aInicheRules($scope, $rootScope, aiNicheService, Utils, $uibModal, DTOptionsBuilder, DTColumnBuilder, $q, common, $resource) {

    $scope.isSuper = $rootScope.loginUser.isSuper
    $scope.searchForm = {}
    $scope.searchBrandForm = {}
    $scope.tableData = []

    // 表格
    $scope.dtColumns = [
      DTColumnBuilder.newColumn('id').withOption('width', 150).withTitle('编号ID'),
      DTColumnBuilder.newColumn('brandName').withOption('width', 150).withTitle('主品牌'),
      DTColumnBuilder.newColumn('childsBrandName').withOption('width', 150).withTitle('子品牌'),
      DTColumnBuilder.newColumn('firstMaintainDay').withTitle('首保时间间隔(天)'),
      DTColumnBuilder.newColumn('firstMaintainMileage').withTitle('首保里程间隔(公里)'),
      DTColumnBuilder.newColumn('regularMaintainDay').withTitle('定保时间间隔(天)'),
      DTColumnBuilder.newColumn('regularMaintainMileage').withTitle('定保里程间隔(公里)'),
      DTColumnBuilder.newColumn('').withTitle(function () {
        return '操作';
      }).withOption('width', 150).withClass('text-center').renderWith(function (e, dt, rowData, config) {
        // console.log(rowData)
        return '<span class="btn btn-link text-info btn-xs" ng-click="handleEdit(\'' + config.row + '\')" functional-contrl fc-menu="maintainRemind" fc-attrname="updateOfMaintainConfig">修改</span>' +
          '<span class="btn btn-link text-info btn-xs" ng-click="handleRulesReset(\'' + rowData.id + '\',\'' + config.row + '\')" functional-contrl fc-menu="maintainRemind" fc-attrname="resetMaintainConfig">重置</span>'
      })
    ]

    $scope.dtOptions = DTOptionsBuilder.newOptions().withOption('num', false)
    $scope.dtInstance = {
      serverData: function (param) {
        angular.extend(param, $scope.searchForm);
        return aiNicheService.getAlnicheList(param, function (res) {
          $scope.tableData = res.data.list
        });
      }
    }

    // 保存或编辑
    $scope.handleEdit = function (row) {
      $uibModal.open({
        templateUrl: 'editRulesModel',
        controller: 'editRulesModel',
        size: 'md',
        resolve: {
          params: function () {
            return {
              row: $scope.tableData[row]
            };
          }
        }
      }).result.then(function (isRefresh) {
        $scope.dtInstance.query({
          condition: $scope.searchForm
        })
      }, function () {});
    };

    // 删除
    $scope.handleRulesReset = function (id, row) {
      swal({
        title: '确定重置吗?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#DD6B55',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnConfirm: false
      }, function (isConfirm) {
        if (isConfirm) {
          aiNicheService.resetRules({
            id: id
          }, function (res) {
            if (res.returnCode == 0) {
              swal({
                type: 'success',
                title: '',
                html: true,
                text: '重置成功',
                showConfirmButton: false,
                timer: 1000
              });
              $scope.dtInstance.query({
                condition: $scope.searchForm
              })
            }
          })
        }
      })
    };

    //获取品牌车型
    function getAllVehicleBrand() {
      return new Promise(function (resolve, reject) {
        aiNicheService.getAllVehicleBrand(function (res) {
          $scope.carBrandOptions = res.data;
          resolve();
        });
      })
    }

    //车辆品牌值改变事件
    $scope.changeCarBrand = function (id) {
      for (var i = 0; i < $scope.carBrandOptions.length; i++) {
        if ($scope.carBrandOptions[i].id == id) {
          $scope.carSmallBrandOptions = JSON.parse(JSON.stringify($scope.carBrandOptions[i].childs));
          break;
        }
      }
      $scope.carSeriesOptions = [];
      $scope.carTypeOptions = [];
      $scope.searchBrandForm.carSmallBrand = '';
    };

    //车辆品牌值改变事件
    $scope.changeCarSmallBrand = function (id) {
      for (var i = 0; i < $scope.carSmallBrandOptions.length; i++) {
        if ($scope.carSmallBrandOptions[i].id == id) {
          $scope.carSeriesOptions = JSON.parse(JSON.stringify($scope.carSmallBrandOptions[i].childs));
          break;
        }
      }
      $scope.carTypeOptions = [];
    };

    /**
     * 下拉框绑定的值是一个对象,需要获取值
     * @param fieldName
     * @returns {*}
     */
    function getSelectValue(objName, id, tempObjName) {
      var obj = tempObjName ? $scope.tempModel[tempObjName] : $scope.searchBrandForm[objName];
      return (obj && obj[id]) ? obj[id] : '';
    }

    /**
     * 获取参数(所有查询都从此获取)
     */
    function getParams() {
      $scope.searchForm.brandId = getSelectValue('carBrand', 'id');
      $scope.searchForm.childsBrandId = getSelectValue('carSmallBrand', 'id');
    }

    $scope.handleSearch = function () {
      getParams()
      // console.log($scope.searchForm)
      $scope.dtInstance.query({
        condition: $scope.searchForm
      })
    };

    $scope.handleReset = function () {
      $scope.searchForm = {}
      $scope.searchBrandForm = {}
      $scope.dtInstance.query()
    };

    $scope.handleExport = function () {
      // console.log('导出--')
      var maintainTitle = '保养规则'
      // // url, params, filename, method
      Utils.exportExcel2('carMaintainConfig/export', $scope.searchForm, maintainTitle, 'GET')
    };

    $scope.handleAdd = function () {
      $uibModal.open({
        templateUrl: 'biz/AIniche/view/aInicheRules/selectBrandModel.html',
        controller: 'selectBrandModel',
        size: 'md',
        resolve: {
          merchant: function () {
            return {
              pageTitle: '添加车系', //弹框标题
              carBrandList: $scope.tableData
            };
          }
        }
      }).result.then(function (refresh) {
        if (refresh) {
          getAllVehicleBrand()
          $scope.dtInstance.query({
            condition: $scope.searchForm
          })
        }
      }, function () {});
    };

    $scope.handleImport = function () {
      getParams();
      $uibModal.open({
        templateUrl: 'biz/AIniche/view/aiImportModal.html',
        controller: 'aiImportModalCtrl',
        size: 'md',
        resolve: {
          importConfig: function () {
            return {
              pageTitle: '导入保养规则', //弹框标题
              ajaxUrl: 'carMaintainConfig/import', //导入服务地址
              downloadType: '22', //下载导入模板类型
              importTemplate: '导入保养规则模板', //下载导入模板文件名
              errorDown: '保养规则报错下载.xlsx',
              params: {
                brandId: $scope.searchForm.brandId,
                childsBrandId: $scope.searchForm.childsBrandId
              }
            }
          }
        }
      }).result.then(function () {}, function () {
        $scope.dtInstance.query({
          condition: $scope.searchForm
        })
      });
    };

    (function () {
      getAllVehicleBrand()
    })()

  }

  // 编辑-弹窗
  function editRulesModel($scope, params, aiNicheService, $uibModalInstance) {
    $scope.item = JSON.parse(JSON.stringify(params.row))

    $scope.submitted = false
    var checkAttr = ['firstMaintainDay', 'firstMaintainMileage', 'regularMaintainDay', 'regularMaintainMileage']

    checkAttr.forEach(function(i) {
      $scope.$watch('item.' + i, function (newValue, oldValue) {
        $scope.item[i] = limitPrice(newValue, oldValue, 9, 0)
      })
    })

    $scope.handleEditCancel = function () {
      $uibModalInstance.close();
    };

    function checkColumn() {
      var flag = true
      for (var i = 0; i < checkAttr.length; i++) {
        !$scope.item[checkAttr[i]] && (flag = false)
      }
      return flag
    }

    $scope.handleEditConfirm = function () {
      $scope.submitted = true
      var flag = checkColumn()
      if (flag) {
        aiNicheService.updateRules($scope.item, function (res) {
          if (res.returnCode == 0) {
            $uibModalInstance.close(true);
          }
        })
      }
    };

  }

  angular.module('AIniche').controller('aInicheRules', aInicheRules)
    .controller('editRulesModel', editRulesModel);

})(angular);