zenglf
2023-08-17 7442a435d058bd17d6fc679e1b7956bf3a5d32ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="preLabel+'编码'" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" :placeholder="'系统自动生成'+preLabel+'编码'" :disabled="true" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="preLabel+'名称'" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" :placeholder="'请输入'+preLabel+'名称'"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="责任组织" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departId">
              <a-select v-model="model.departId"  placeholder="请选择责任组织">
                  <a-select-option :key="item.id" :value="item.id" v-for="item of departList">
                  {{item.orgCode+"/"+item.departName}}
                  </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否虚拟" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="virtualFlag">
              <a-radio-group  button-style="solid" v-model="model.virtualFlag">
                <a-radio-button :value="0" style="width:100px;text-align: center;">
                真  实
                </a-radio-button>
                <a-radio-button :value="1" style="width:100px;text-align: center;">
                虚  拟
                </a-radio-button>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>
 
<script>
 
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
import { putAction } from '../../../../api/manage'
 
  export default {
    name: 'FactoryModelForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      factoryElementCategoryId:{
        type: String,
        default: '',
        required: false
      },
      preLabel:{
        type: String,
        default: '',
        required: false
      },
      parentId:{
        type:String,
        default:'',
        required:false
      },
      enterpriseId:{
        type:String,
        default:'',
        required:false
      },
      version:{
        type:Number,
        required:false
      }
    },
    data () {
      return {
        departList:[],
        codeRule:'mom_base_factory_model_code_rule',
        tempName:'',
        isUsable:false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           code: [
              { required: true, message: '请输入'+this.preLabel+'编码!'},
           ],
           name: [
              { required: true, message: '请输入'+this.preLabel+'名称!'},
              { validator:this.validateName},
              {min:0,max:32,message:'长度超出最大限制,请缩减长度'}
           ],
           departId: [
              { required: true, message: '请选择责任组织'},
           ],
           virtualFlag:[
              { required: true, message: '请选择虚拟状态'},
           ],
           remark:[
            {min:0,max:100,message:'长度超出最大限制,请缩减长度'}
          ],
        },
        url: {
          add: "/base/factoryModel/add",
          edit: "/base/factoryModel/edit",
          queryById: "/base/factoryModel/queryById",
          getDepartList:"/base/depart/usableList",
          ruleBaseURL:"/sys/fillRule/executeRuleByCode",
          check:"/base/factoryModel/check",
          getVersion: "/base/factoryModel/getVersionList"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getDepartList();
      
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if(this.model.id){
          this.tempName=this.model.name;
        }
        this.visible = true;
        console.log(this.parentId+"=="+this.version+"=="+this.enterpriseId)
        if(!this.model.id){
          var ruleCodeParam={
            parentId:this.parentId,
            version:this.version?this.version:1,
            enterpriseId:this.enterpriseId
          }
          putAction(this.url.ruleBaseURL+"/"+this.codeRule,ruleCodeParam).then(res=>{
            this.model.code=res.result;
          });
          if(this.version){
                this.model.version = this.version;
            }
          else{
                this.model.version = 1;
          }
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
              this.model.versionStatus='0';
              this.model.enterpriseId = this.enterpriseId;
              this.model.parentId = this.parentId;
              this.model.factoryElementCategoryId = this.factoryElementCategoryId;
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              this.$emit('reloadUi')
              that.confirmLoading = false;
            })
          }else{
            this.$emit("changeAddLoading")
          }    
         
        })
      },
      validateName(rule, value, callback){
        var params = {
        name:value,
        version:this.version,
        parentId:this.parentId
      };
      getAction(this.url.check,params).then(res=>{
            if(res.result.length==0){
              callback();
            }
            else if(this.tempName==value){
              callback();
            }
            else{
              callback('名称重复,请重新输入');
            }
        })
      },
      clearInfo(){
        this.model.orgCode=''
        var ruleCodeParam={
            version:this.version,
            parentId:this.parentId,
            enterpriseId:this.enterpriseId
        }
        putAction(this.url.ruleBaseURL+"/"+this.codeRule,ruleCodeParam).then(res=>{
            this.model.code=res.result;
            this.model.name='';
            this.model.departId='';
            this.model.remark=''
            this.model.virtualFlag=''
        });
      },
      getDepartList(){
        this.confirmLoading=true;
        getAction(this.url.getDepartList,{enterpriseId:this.enterpriseId}).then(res=>{
          if(res.success){
             this.departList = res.result
             for(var i=0;i<this.departList.length;i++){
                 if(this.departList[i].id==this.model.departId){
                    this.isUsable=true;
                    break;
                 }
             }
             if(!this.isUsable&&this.model.id){
                this.model.departId='';
                this.$message.warning("当前所属的责任组织已经失效,请重新选择生效版本")
             }
          }else{
            that.$message.warning(res.message);
          }
        }).finally(res=>{
          this.confirmLoading=false
        })
      },
    },
  }
</script>