Browse Source

Merge branch '20211122' of http://git.shengws.com/csx/XT_New into 20211122

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