浏览代码

修改bug

mainqaq 3 年前
父节点
当前提交
352f3e27e2
共有 2 个文件被更改,包括 127 次插入27 次删除
  1. 105 17
      controllers/supply_order_api_contorller.go
  2. 22 10
      service/supply_service.go

+ 105 - 17
controllers/supply_order_api_contorller.go 查看文件

289
 	bank := dataBody["bank"].(string) //纳税人识别号
289
 	bank := dataBody["bank"].(string) //纳税人识别号
290
 	//bankAccount := this.GetString("bankAccount")     //银行账号
290
 	//bankAccount := this.GetString("bankAccount")     //银行账号
291
 	bankAccount := dataBody["bankaccount"].(string)
291
 	bankAccount := dataBody["bankaccount"].(string)
292
+	tmodify := this.GetAdminUserInfo().AdminUser.Id //修改者
292
 	//保存联系人
293
 	//保存联系人
293
 	if dataBody["contacts"] != nil && reflect.TypeOf(dataBody["contacts"]).String() == "[]interface {}" {
294
 	if dataBody["contacts"] != nil && reflect.TypeOf(dataBody["contacts"]).String() == "[]interface {}" {
294
 		thisStockIn, _ := dataBody["contacts"].([]interface{})
295
 		thisStockIn, _ := dataBody["contacts"].([]interface{})
295
 		if len(thisStockIn) > 0 {
296
 		if len(thisStockIn) > 0 {
296
-			if len(thisStockIn) == 1 {
297
+			for _, v := range thisStockIn { //这个循环是用来检测当填了手机号时联系人不能为空
298
+				vtem := v.(map[string]interface{})
299
+				if vtem["phone"].(string) != "" && vtem["name"].(string) == "" {
300
+					this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人不能为空")
301
+					return
302
+				}
303
+			}
304
+			if len(thisStockIn) == 1 { //当只有一条联系人时强制设为首要联系人
297
 				for _, item := range thisStockIn {
305
 				for _, item := range thisStockIn {
298
 					items := item.(map[string]interface{})
306
 					items := item.(map[string]interface{})
299
 					items["is_first"] = 1
307
 					items["is_first"] = 1
308
+					//如果电话和联系人都为空,不保存联系人
309
+					if items["phone"] == "" && items["name"] == "" {
310
+						upsupply := models.SpSupplierName{
311
+							ID:           supplierid,
312
+							SupplierCode: supplierCode,
313
+							SupplierName: supplierName,
314
+							SupplierType: supplierType,
315
+							VatRate:      vatRate,
316
+							Number:       number,
317
+							Bank:         bank,
318
+							BankAccount:  bankAccount,
319
+							Status:       1,
320
+							Mtime:        time.Now().Unix(),
321
+							Modify:       tmodify,
322
+						}
323
+						err = service.UpdateSupplyName(upsupply)
324
+						if err != nil {
325
+							this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
326
+							return
327
+						}
328
+					}
300
 				}
329
 				}
301
 			}
330
 			}
302
-			tmodify := this.GetAdminUserInfo().AdminUser.Id
303
 			err = service.UpdateSupplyAndContact(thisStockIn, supplierid, orgId, supplierType, tmodify, supplierCode, supplierName, number, bank, bankAccount, vatRate)
331
 			err = service.UpdateSupplyAndContact(thisStockIn, supplierid, orgId, supplierType, tmodify, supplierCode, supplierName, number, bank, bankAccount, vatRate)
304
 			if err != nil {
332
 			if err != nil {
305
 				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
333
 				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
306
 				return
334
 				return
307
 			}
335
 			}
308
 		} else {
336
 		} else {
309
-			this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人不能为空")
310
-			return
337
+			//如果联系人是空的,直接保存供应商
338
+			//更新供应商
339
+			upsupply := models.SpSupplierName{
340
+				ID:           supplierid,
341
+				SupplierCode: supplierCode,
342
+				SupplierName: supplierName,
343
+				SupplierType: supplierType,
344
+				VatRate:      vatRate,
345
+				Number:       number,
346
+				Bank:         bank,
347
+				BankAccount:  bankAccount,
348
+				Status:       1,
349
+				Mtime:        time.Now().Unix(),
350
+				Modify:       tmodify,
351
+			}
352
+			err = service.UpdateSupplyName(upsupply)
353
+			if err != nil {
354
+				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
355
+				return
356
+			}
357
+
311
 		}
358
 		}
312
 	}
359
 	}
313
 	this.ServeSuccessJSON(map[string]interface{}{
360
 	this.ServeSuccessJSON(map[string]interface{}{
520
 		dataBody["suppliertype"] = tmp
567
 		dataBody["suppliertype"] = tmp
521
 	}
568
 	}
522
 	if dataBody["vatrate"] == "" {
569
 	if dataBody["vatrate"] == "" {
523
-		dataBody["vatrate"] = 0
570
+		dataBody["vatrate"] = "0"
524
 	}
571
 	}
525
 	//supplierType, _ := this.GetInt64("suppliertype")
572
 	//supplierType, _ := this.GetInt64("suppliertype")
526
 	if dataBody["suppliertype"] == "" || dataBody["suppliertype"] == 0 {
573
 	if dataBody["suppliertype"] == "" || dataBody["suppliertype"] == 0 {
544
 	bank := dataBody["bank"].(string)
591
 	bank := dataBody["bank"].(string)
545
 	//bankAccount := this.GetString("bankAccount")
592
 	//bankAccount := this.GetString("bankAccount")
546
 	bankAccount := dataBody["bankaccount"].(string)
593
 	bankAccount := dataBody["bankaccount"].(string)
547
-
548
-	//contacts := this.Get("contacts")//联系人
549
-	//err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
550
-	//if err != nil {
551
-	//	utils.ErrorLog(err.Error())
552
-	//	this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
553
-	//	return
554
-	//}
594
+	tcreater := this.GetAdminUserInfo().AdminUser.Id
555
 	//保存联系人
595
 	//保存联系人
556
 	if dataBody["contacts"] != nil && reflect.TypeOf(dataBody["contacts"]).String() == "[]interface {}" {
596
 	if dataBody["contacts"] != nil && reflect.TypeOf(dataBody["contacts"]).String() == "[]interface {}" {
557
 		thisStockIn, _ := dataBody["contacts"].([]interface{})
597
 		thisStockIn, _ := dataBody["contacts"].([]interface{})
558
 		if len(thisStockIn) > 0 {
598
 		if len(thisStockIn) > 0 {
559
-			if len(thisStockIn) == 1 {
599
+			for _, v := range thisStockIn { //这个循环是用来检测当填了手机号时联系人不能为空
600
+				vtem := v.(map[string]interface{})
601
+				if vtem["phone"] != "" && vtem["name"] == nil {
602
+					this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人不能为空")
603
+					return
604
+				}
605
+			}
606
+			if len(thisStockIn) == 1 { //当只有一条联系人时强制设为首要联系人,
560
 				for _, item := range thisStockIn {
607
 				for _, item := range thisStockIn {
561
 					items := item.(map[string]interface{})
608
 					items := item.(map[string]interface{})
562
 					items["is_first"] = 1
609
 					items["is_first"] = 1
610
+					//如果电话和联系人都为空,不保存联系人
611
+					if items["phone"] == "" && items["name"] == "" {
612
+						supply := models.SpSupplierName{
613
+							SupplierCode: supplierCode,
614
+							SupplierName: supplierName,
615
+							SupplierType: supplierType,
616
+							VatRate:      vatRate,
617
+							Number:       number,
618
+							Bank:         bank,
619
+							BankAccount:  bankAccount,
620
+							UserOrgId:    orgId,
621
+							Status:       1,
622
+							Ctime:        time.Now().Unix(),
623
+							Mtime:        0,
624
+							Creater:      tcreater,
625
+							Modify:       tcreater,
626
+						}
627
+						err = service.SaveSupply(supply)
628
+						if err != nil {
629
+							this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
630
+							return
631
+						}
632
+					}
563
 				}
633
 				}
564
 			}
634
 			}
565
-			tcreater := this.GetAdminUserInfo().AdminUser.Id
566
 			err = service.SaveSupplyAndContact(thisStockIn, orgId, supplierType, tcreater, supplierCode, supplierName, number, bank, bankAccount, vatRate)
635
 			err = service.SaveSupplyAndContact(thisStockIn, orgId, supplierType, tcreater, supplierCode, supplierName, number, bank, bankAccount, vatRate)
567
 			if err != nil {
636
 			if err != nil {
568
 				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
637
 				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
569
 				return
638
 				return
570
 			}
639
 			}
571
 		} else {
640
 		} else {
572
-			this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人不能为空")
573
-			return
641
+			//如果联系人是空的,这里直接保存供应商
642
+			supply := models.SpSupplierName{
643
+				SupplierCode: supplierCode,
644
+				SupplierName: supplierName,
645
+				SupplierType: supplierType,
646
+				VatRate:      vatRate,
647
+				Number:       number,
648
+				Bank:         bank,
649
+				BankAccount:  bankAccount,
650
+				UserOrgId:    orgId,
651
+				Status:       1,
652
+				Ctime:        time.Now().Unix(),
653
+				Mtime:        0,
654
+				Creater:      tcreater,
655
+				Modify:       tcreater,
656
+			}
657
+			err = service.SaveSupply(supply)
658
+			if err != nil {
659
+				this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
660
+				return
661
+			}
574
 		}
662
 		}
575
 	}
663
 	}
576
 	this.ServeSuccessJSON(map[string]interface{}{
664
 	this.ServeSuccessJSON(map[string]interface{}{

+ 22 - 10
service/supply_service.go 查看文件

43
 	}()
43
 	}()
44
 	for _, item := range thisStockIn {
44
 	for _, item := range thisStockIn {
45
 		items := item.(map[string]interface{})
45
 		items := item.(map[string]interface{})
46
-		//查询是否
46
+		//判断联系人是否为新加的
47
 		if items["id"] == "" || items["id"] == 0 {
47
 		if items["id"] == "" || items["id"] == 0 {
48
 			var tmp float64 = 0
48
 			var tmp float64 = 0
49
 			items["id"] = tmp
49
 			items["id"] = tmp
76
 			Ctime:        0,
76
 			Ctime:        0,
77
 			Mtime:        time.Now().Unix(),
77
 			Mtime:        time.Now().Unix(),
78
 		}
78
 		}
79
-		if id == 0 {
79
+		if id == 0 { //表示这是新加的
80
 			spcontacts := models.SpSupplierContacts{
80
 			spcontacts := models.SpSupplierContacts{
81
 				Name:         name,
81
 				Name:         name,
82
 				Phone:        phone,
82
 				Phone:        phone,
87
 				Status:       1,
87
 				Status:       1,
88
 				Ctime:        time.Now().Unix(),
88
 				Ctime:        time.Now().Unix(),
89
 				Mtime:        0,
89
 				Mtime:        0,
90
-			}
90
+			} //保存一条联系人的数据
91
 			err = SaveContacts(spcontacts, tx)
91
 			err = SaveContacts(spcontacts, tx)
92
 			if err != nil {
92
 			if err != nil {
93
 				return err
93
 				return err
97
 		}
97
 		}
98
 		var tmpid int64
98
 		var tmpid int64
99
 		if isfirst == 1 {
99
 		if isfirst == 1 {
100
-			if id == 0 {
100
+			if id == 0 { //新加的首要联系人
101
 				var spconid []*models.SpSupplierContacts
101
 				var spconid []*models.SpSupplierContacts
102
 				spconid, err = SaveContactsId(tx, orgId)
102
 				spconid, err = SaveContactsId(tx, orgId)
103
 				if err != nil {
103
 				if err != nil {
123
 				Mtime:        time.Now().Unix(),
123
 				Mtime:        time.Now().Unix(),
124
 				Modify:       tcreater,
124
 				Modify:       tcreater,
125
 			}
125
 			}
126
-			err = UpdateSupplyName(upsupply, tx)
126
+			err = UpdateSupplyNameTX(upsupply, tx)
127
 			if err != nil {
127
 			if err != nil {
128
 				return err
128
 				return err
129
 			}
129
 			}
133
 	return err
133
 	return err
134
 }
134
 }
135
 
135
 
136
-//更新一条供应商的信息
137
-func UpdateSupplyName(upsupply models.SpSupplierName, tx *gorm.DB) error {
136
+//更新一条供应商的信息,添加了事务的
137
+func UpdateSupplyNameTX(upsupply models.SpSupplierName, tx *gorm.DB) error {
138
 	err := tx.Model(&models.SpSupplierName{}).Where("id = ? and status = 1", upsupply.ID).Updates(map[string]interface{}{"supplier_code": upsupply.SupplierCode, "supplier_name": upsupply.SupplierName, "supplier_type": upsupply.SupplierType, "vat_rate": upsupply.VatRate, "number": upsupply.Number, "bank": upsupply.Bank, "bank_account": upsupply.BankAccount, "user_org_id": upsupply.UserOrgId, "status": upsupply.Status, "contacts_id": upsupply.ContactsId, "mtime": upsupply.Mtime, "modify": upsupply.Modify}).Error
138
 	err := tx.Model(&models.SpSupplierName{}).Where("id = ? and status = 1", upsupply.ID).Updates(map[string]interface{}{"supplier_code": upsupply.SupplierCode, "supplier_name": upsupply.SupplierName, "supplier_type": upsupply.SupplierType, "vat_rate": upsupply.VatRate, "number": upsupply.Number, "bank": upsupply.Bank, "bank_account": upsupply.BankAccount, "user_org_id": upsupply.UserOrgId, "status": upsupply.Status, "contacts_id": upsupply.ContactsId, "mtime": upsupply.Mtime, "modify": upsupply.Modify}).Error
139
 	return err
139
 	return err
140
 }
140
 }
141
 
141
 
142
+//更新一条供应商的信息,没有添加事务
143
+func UpdateSupplyName(upsupply models.SpSupplierName) error {
144
+	err := XTWriteDB().Model(&models.SpSupplierName{}).Where("id = ? and status = 1", upsupply.ID).Updates(map[string]interface{}{"supplier_code": upsupply.SupplierCode, "supplier_name": upsupply.SupplierName, "supplier_type": upsupply.SupplierType, "vat_rate": upsupply.VatRate, "number": upsupply.Number, "bank": upsupply.Bank, "bank_account": upsupply.BankAccount, "user_org_id": upsupply.UserOrgId, "status": upsupply.Status, "contacts_id": upsupply.ContactsId, "mtime": upsupply.Mtime, "modify": upsupply.Modify}).Error
145
+	return err
146
+}
147
+
142
 //更新一条联系人的信息
148
 //更新一条联系人的信息
143
 func UpdateContact(updatecontacts models.SpSupplierContacts, tx *gorm.DB) error {
149
 func UpdateContact(updatecontacts models.SpSupplierContacts, tx *gorm.DB) error {
144
 	err := tx.Model(&models.SpSupplierContacts{}).Where("id = ? and status = 1", updatecontacts.ID).Updates(map[string]interface{}{"name": updatecontacts.Name, "phone": updatecontacts.Phone, "address": updatecontacts.Address, "is_first": updatecontacts.IsFirst, "supplier_code": updatecontacts.SupplierCode, "user_org_id": updatecontacts.UserOrgId, "status": updatecontacts.Status, "mtime": updatecontacts.Mtime}).Error
150
 	err := tx.Model(&models.SpSupplierContacts{}).Where("id = ? and status = 1", updatecontacts.ID).Updates(map[string]interface{}{"name": updatecontacts.Name, "phone": updatecontacts.Phone, "address": updatecontacts.Address, "is_first": updatecontacts.IsFirst, "supplier_code": updatecontacts.SupplierCode, "user_org_id": updatecontacts.UserOrgId, "status": updatecontacts.Status, "mtime": updatecontacts.Mtime}).Error
255
 				Creater:      tcreater,
261
 				Creater:      tcreater,
256
 				Modify:       tcreater,
262
 				Modify:       tcreater,
257
 			}
263
 			}
258
-			err = SaveSupply(supply, tx)
264
+			err = SaveSupplyTx(supply, tx)
259
 			if err != nil {
265
 			if err != nil {
260
 				return err
266
 				return err
261
 			}
267
 			}
309
 	return codebool, err
315
 	return codebool, err
310
 }
316
 }
311
 
317
 
312
-//保存一条供应商数据
313
-func SaveSupply(supply models.SpSupplierName, tx *gorm.DB) error {
318
+//保存一条供应商数据加事务的
319
+func SaveSupplyTx(supply models.SpSupplierName, tx *gorm.DB) error {
314
 	err := tx.Create(&supply).Error
320
 	err := tx.Create(&supply).Error
315
 	return err
321
 	return err
316
 }
322
 }
317
 
323
 
324
+//保存一条供应商数据没有事务的
325
+func SaveSupply(supply models.SpSupplierName) error {
326
+	err := XTWriteDB().Create(&supply).Error
327
+	return err
328
+}
329
+
318
 //获取联系人的id
330
 //获取联系人的id
319
 func SaveContactsId(tx *gorm.DB, orgid int64) (spconid []*models.SpSupplierContacts, err error) {
331
 func SaveContactsId(tx *gorm.DB, orgid int64) (spconid []*models.SpSupplierContacts, err error) {
320
 	err = tx.Model(&models.SpSupplierContacts{}).Select("id").Where("status = 1 and user_org_id = ?", orgid).Order("id desc").First(&spconid).Error
332
 	err = tx.Model(&models.SpSupplierContacts{}).Select("id").Where("status = 1 and user_org_id = ?", orgid).Order("id desc").First(&spconid).Error