Browse Source

添加了供应商的接口

mainqaq 3 years ago
parent
commit
d5ab8199db
3 changed files with 250 additions and 2 deletions
  1. 121 1
      controllers/supply_order_api_contorller.go
  2. 18 1
      models/supply.models.go
  3. 111 0
      service/supply_service.go

+ 121 - 1
controllers/supply_order_api_contorller.go View File

@@ -20,7 +20,13 @@ type SupplyOrderApiController struct {
20 20
 
21 21
 func SupplyOrderApiRegistRouters() {
22 22
 
23
-	//初始化商品数据
23
+	//删除供应商及联系人
24
+	beego.Router("/api/supply/delsupply", &SupplyOrderApiController{}, "post:DelSupply")
25
+	//获取供应商编码
26
+	beego.Router("/api/supply/getsupplycode", &SupplyOrderApiController{}, "get:GetSupplyCode")
27
+	//保存供应商及联系人
28
+	beego.Router("/api/supply/savesupply", &SupplyOrderApiController{}, "post:SaveSupply")
29
+
24 30
 	beego.Router("/api/supply/getinitorder", &SupplyOrderApiController{}, "get:GetInitOrder")
25 31
 	//保存购货订单
26 32
 	beego.Router("/api/supply/savepurchaseorder", &SupplyOrderApiController{}, "post:SavePurchaseOrder")
@@ -54,6 +60,120 @@ func SupplyOrderApiRegistRouters() {
54 60
 	beego.Router("/api/supply/getreturnorder", &SupplyOrderApiController{}, "Get:GetReturnOrder")
55 61
 }
56 62
 
63
+//删除供应商及联系人
64
+func (this *SupplyOrderApiController) DelSupply() {
65
+	//拿到供应商的id
66
+	suid, _ := this.GetInt64("id")
67
+	if suid == 0 {
68
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "供应商id不能为空")
69
+		return
70
+	}
71
+	supply := models.SpSupplierName{
72
+		ID: suid,
73
+	}
74
+	err := service.DelSupply(supply)
75
+	if err != nil {
76
+		utils.ErrorLog(err.Error())
77
+		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
78
+		return
79
+	}
80
+	this.ServeSuccessJSON(map[string]interface{}{
81
+		"list": "删除成功",
82
+	})
83
+	return
84
+}
85
+
86
+//获取供应商编码
87
+func (this *SupplyOrderApiController) GetSupplyCode() {
88
+	supply, err := service.GetSuppliyCode()
89
+	if err != nil {
90
+		return
91
+	}
92
+	if len(supply) == 0 { //如果数据库中没有gys类型的编码则设置默认值
93
+		//supply[0].SupplierCode ="gys001"
94
+		supply = []*models.SpSupplierName{
95
+			{
96
+				SupplierCode: "gys001",
97
+			},
98
+		}
99
+	} else { //获取数据库中最大的编码值,并加一
100
+		tmp := supply[0].SupplierCode
101
+		tmp = tmp[3:]
102
+		var code int
103
+		code, err = strconv.Atoi(tmp)
104
+		code++
105
+		tmp = strconv.Itoa(code)
106
+		for len(tmp) < 3 {
107
+			tmp = "0" + tmp
108
+		}
109
+		tmp = "gys" + tmp
110
+		supply[0].SupplierCode = tmp
111
+	}
112
+	this.ServeSuccessJSON(map[string]interface{}{
113
+		"supplycode": supply,
114
+	})
115
+	return
116
+}
117
+
118
+//保存供应商
119
+func (this *SupplyOrderApiController) SaveSupply() {
120
+	supplierName := this.GetString("suppliername") //供应商名称
121
+	if supplierName == "" {
122
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "供应商名称不能为空")
123
+		return
124
+	}
125
+	//判断供应商名称是否有重复的
126
+	sbool, _ := service.FindSupplierName(supplierName)
127
+	if sbool { //有重复的
128
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "供应商名称重复")
129
+		return
130
+	}
131
+	supplierCode := this.GetString("suppliercode") //供应商编码
132
+	if supplierCode == "" {
133
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "供应商编号不能为空")
134
+		return
135
+	}
136
+	supplierType, _ := this.GetInt64("suppliertype") //供应商类别
137
+	vatRate, _ := this.GetFloat("vatrate")           //增值税税率
138
+	number := this.GetString("number")               //纳税人识别号
139
+	bank := this.GetString("bank")                   //开户银行
140
+	bankAccount := this.GetString("bankAccount")     //银行账号
141
+	orgId := this.GetAdminUserInfo().CurrentOrgId
142
+	//contacts := this.Get("contacts")//联系人
143
+	dataBody := make(map[string]interface{}, 0)
144
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
145
+	if err != nil {
146
+		utils.ErrorLog(err.Error())
147
+		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
148
+		return
149
+	}
150
+	//保存联系人
151
+	if dataBody["contacts"] != nil && reflect.TypeOf(dataBody["contacts"]).String() == "[]interface {}" {
152
+		thisStockIn, _ := dataBody["contacts"].([]interface{})
153
+		if len(thisStockIn) > 0 {
154
+			if len(thisStockIn) == 1 {
155
+				for _, item := range thisStockIn {
156
+					items := item.(map[string]interface{})
157
+					items["IsFirst"] = 1
158
+				}
159
+			}
160
+			tcreater := this.GetAdminUserInfo().AdminUser.Id
161
+			err = service.SaveSupplyAndContact(thisStockIn, orgId, supplierType, tcreater, supplierCode, supplierName, number, bank, bankAccount, vatRate)
162
+			if err != nil {
163
+				this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
164
+				return
165
+			}
166
+		} else {
167
+			this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人不能为空")
168
+			return
169
+		}
170
+	}
171
+	this.ServeSuccessJSON(map[string]interface{}{
172
+		"list": "保存成功",
173
+	})
174
+	return
175
+}
176
+
57 177
 func (this *SupplyOrderApiController) GetInitOrder() {
58 178
 
59 179
 	orgId := this.GetAdminUserInfo().CurrentOrgId

+ 18 - 1
models/supply.models.go View File

@@ -1,5 +1,22 @@
1 1
 package models
2 2
 
3
+//供应商联系人
4
+type SpSupplierContacts struct {
5
+	ID        int64  `gorm:"column:id" json:"id" from:"id"`
6
+	Name      string `gorm:"column:name" json:"name" from:"name"`
7
+	Phone     string `gorm:"column:phone" json:"phone" from:"phone"`
8
+	Address   string `gorm:"column:address" json:"address" from:"address"`
9
+	IsFirst   int64  `gorm:"column:is_first" json:"is_first" from:"is_first"`
10
+	UserOrgId int64  `gorm:"column:user_org_id" json:"user_org_id" from:"user_org_id"`
11
+	Status    int64  `gorm:"column:status" json:"status" from:"status"`
12
+	Ctime     int64  `gorm:"column:ctime" json:"ctime" from:"ctime"`
13
+	Mtime     int64  `gorm:"column:mtime" json:"mtime" from:"mtime"`
14
+}
15
+
16
+func (SpSupplierContacts) TableName() string {
17
+	return "xt_supplier_contacts"
18
+}
19
+
3 20
 type SpBaseDrug struct {
4 21
 	ID                int64                  `gorm:"column:id" json:"id" form:"id"`
5 22
 	DrugName          string                 `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
@@ -80,7 +97,7 @@ type SpSupplierName struct {
80 97
 	BankAccount  string  `gorm:"column:bank_account" json:"bank_account" form:"bank_account"`
81 98
 	UserOrgId    int64   `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
82 99
 	Status       int64   `gorm:"column:status" json:"status" form:"status"`
83
-	ContactsId   string  `gorm:"column:contacts_id" json:"contacts_id" form:"contacts_id"`
100
+	ContactsId   int64   `gorm:"column:contacts_id" json:"contacts_id" form:"contacts_id"`
84 101
 	Ctime        int64   `gorm:"column:ctime" json:"ctime" form:"ctime"`
85 102
 	Mtime        int64   `gorm:"column:mtime" json:"mtime" form:"mtime"`
86 103
 	Creater      int64   `gorm:"column:creater" json:"creater" form:"creater"`

+ 111 - 0
service/supply_service.go View File

@@ -2,9 +2,120 @@ package service
2 2
 
3 3
 import (
4 4
 	"XT_New/models"
5
+	"github.com/jinzhu/gorm"
6
+	"strconv"
5 7
 	"time"
6 8
 )
7 9
 
10
+//删除供应商及联系人
11
+func DelSupply(supply models.SpSupplierName) error {
12
+	err := XTWriteDB().Model(&supply).Update("status", 0).Error
13
+	return err
14
+}
15
+
16
+//保存供应商和联系人
17
+func SaveSupplyAndContact(thisStockIn []interface{}, orgId, supplierType, tcreater int64, supplierCode, supplierName, number, bank, bankAccount string, vatRate float64) error {
18
+	tx := XTWriteDB().Begin()
19
+	defer func() {
20
+		if err != nil {
21
+			tx.Rollback()
22
+		} else {
23
+			tx.Commit()
24
+		}
25
+	}()
26
+	for _, item := range thisStockIn {
27
+		items := item.(map[string]interface{})
28
+		name := items["name"].(string)
29
+		phone := items["phone"].(string)
30
+		address := items["address"].(string)
31
+		isfirst, _ := strconv.ParseInt(items["isfirst"].(string), 10, 64)
32
+
33
+		spcontacts := models.SpSupplierContacts{
34
+			Name:      name,
35
+			Phone:     phone,
36
+			Address:   address,
37
+			IsFirst:   isfirst,
38
+			UserOrgId: orgId,
39
+			Status:    1,
40
+			Ctime:     time.Now().Unix(),
41
+			Mtime:     0,
42
+		}
43
+		err = SaveContacts(spcontacts, tx)
44
+		if isfirst == 1 {
45
+			var spconid []*models.SpSupplierContacts
46
+			spconid, err = SaveContactsId(tx)
47
+
48
+			if err != nil {
49
+				return err
50
+			}
51
+			tmpid := spconid[0].ID
52
+			//保存供应商
53
+			supply := models.SpSupplierName{
54
+				SupplierCode: supplierCode,
55
+				SupplierName: supplierName,
56
+				SupplierType: supplierType,
57
+				VatRate:      vatRate,
58
+				Number:       number,
59
+				Bank:         bank,
60
+				BankAccount:  bankAccount,
61
+				UserOrgId:    orgId,
62
+				Status:       1,
63
+				ContactsId:   tmpid,
64
+				Ctime:        time.Now().Unix(),
65
+				Mtime:        0,
66
+				Creater:      tcreater,
67
+				Modify:       tcreater,
68
+			}
69
+			err = SaveSupply(supply, tx)
70
+			if err != nil {
71
+				return err
72
+			}
73
+
74
+		}
75
+		if err != nil {
76
+			return err
77
+		}
78
+	}
79
+	return err
80
+}
81
+
82
+//获取供应商编码
83
+func GetSuppliyCode() (spcode []*models.SpSupplierName, err error) {
84
+
85
+	err = XTReadDB().Model(&models.SpSupplierName{}).Select("supplier_code").Where("supplier_code like 'gys%' ").Order("supplier_code desc").First(&spcode).Error
86
+	return spcode, err
87
+}
88
+
89
+//查询供应商的名字是否有重复
90
+func FindSupplierName(supplierName string) (sbool bool, err error) {
91
+	var total int
92
+	err = XTReadDB().Model(&models.SpSupplierName{}).Where("supplier_name = ? and status = 1", supplierName).Count(&total).Error
93
+	if total != 0 {
94
+		sbool = true
95
+	} else {
96
+		sbool = false
97
+	}
98
+	return sbool, err
99
+}
100
+
101
+//保存一条供应商数据
102
+func SaveSupply(supply models.SpSupplierName, tx *gorm.DB) error {
103
+	err := tx.Create(&supply).Error
104
+	return err
105
+}
106
+
107
+//获取联系人的id
108
+func SaveContactsId(tx *gorm.DB) (spconid []*models.SpSupplierContacts, err error) {
109
+	err = tx.Model(&models.SpSupplierContacts{}).Select("id").Order("id desc").First(&spconid).Error
110
+	return
111
+}
112
+
113
+//保存一条联系人数据
114
+func SaveContacts(spcontacts models.SpSupplierContacts, tx *gorm.DB) error {
115
+	err := tx.Create(&spcontacts).Error
116
+	return err
117
+}
118
+
8 119
 func GetSupplyDrugList(orgid int64) (drug []*models.SpBaseDrug, err error) {
9 120
 
10 121
 	db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")