stock_api_controller.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "github.com/astaxie/beego"
  8. "github.com/jinzhu/gorm"
  9. "strconv"
  10. "time"
  11. )
  12. type StockApiController struct {
  13. BaseAuthAPIController
  14. }
  15. func StockApiRegistRouters() {
  16. beego.Router("/api/stock/dealer/create", &StockApiController{}, "post:CreateDealer")
  17. beego.Router("/api/stock/dealer/modify", &StockApiController{}, "post:ModifyDealer")
  18. beego.Router("/api/stock/dealer/list", &StockApiController{}, "get:GetDealersList")
  19. beego.Router("/api/stock/dealer/getdealername", &StockApiController{}, "get:GetDealerName") //获取供应商名称
  20. beego.Router("/api/stock/dealer/delete", &StockApiController{}, "post:DeleteDealer")
  21. beego.Router("/api/stock/dealer/get", &StockApiController{}, "get:GetDealer")
  22. beego.Router("/api/stock/manufacturer/create", &StockApiController{}, "post:CreateManufacturer")
  23. beego.Router("/api/stock/manufacturer/modify", &StockApiController{}, "post:ModifyManufacturer")
  24. beego.Router("/api/stock/manufacturer/getmanufacturername", &StockApiController{}, "get:GetManufacturerName") //获取生产厂商名称
  25. beego.Router("/api/stock/manufacturer/list", &StockApiController{}, "get:GetManufacturerList")
  26. beego.Router("/api/stock/manufacturer/delete", &StockApiController{}, "post:DeleteManufacturer")
  27. beego.Router("/api/stock/manufacturer/get", &StockApiController{}, "get:GetManufacturer")
  28. beego.Router("/api/stock/dealer/all", &StockApiController{}, "get:GetAllDealer")
  29. beego.Router("/api/stock/manufacturer/all", &StockApiController{}, "get:GetAllManufacturer")
  30. }
  31. func (c *StockApiController) CreateDealer() {
  32. dealer_name := c.GetString("dealer_name")
  33. contact := c.GetString("contact")
  34. contact_phone := c.GetString("contact_phone")
  35. platform_number := c.GetString("platform_number")
  36. email := c.GetString("email")
  37. contact_address := c.GetString("contact_address")
  38. remark := c.GetString("remark")
  39. wubi := c.GetString("wubi")
  40. pinyin := c.GetString("pinyin")
  41. if len(dealer_name) <= 0 {
  42. utils.ErrorLog("len(dealer_name) == 0")
  43. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  44. return
  45. }
  46. adminUserInfo := c.GetAdminUserInfo()
  47. total := service.FindAllDealerTotal(adminUserInfo.CurrentOrgId)
  48. totalStr := strconv.FormatInt(total+1, 10)
  49. code := "1000" + totalStr
  50. dealer := models.Dealer{
  51. DealerName: dealer_name,
  52. Contact: contact,
  53. ContactPhone: contact_phone,
  54. ContactAddress: contact_address,
  55. DealerCode: code,
  56. Ctime: time.Now().Unix(),
  57. Mtime: time.Now().Unix(),
  58. Remark: remark,
  59. Creater: adminUserInfo.AdminUser.Id,
  60. Email: email,
  61. PlatformNumber: platform_number,
  62. OrgId: adminUserInfo.CurrentOrgId,
  63. Status: 1,
  64. WuBi: wubi,
  65. PinYin: pinyin,
  66. }
  67. err, dealers := service.AddSigleDealer(&dealer)
  68. if err == nil {
  69. c.ServeSuccessJSON(map[string]interface{}{
  70. "dealer": dealers,
  71. })
  72. } else {
  73. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  74. }
  75. }
  76. func (c *StockApiController) ModifyDealer() {
  77. id, _ := c.GetInt64("id", 0)
  78. dealer_name := c.GetString("dealer_name")
  79. contact := c.GetString("contact")
  80. contact_phone := c.GetString("contact_phone")
  81. platform_number := c.GetString("platform_number")
  82. dealer_code := c.GetString("dealer_code")
  83. email := c.GetString("email")
  84. contact_address := c.GetString("contact_address")
  85. remark := c.GetString("remark")
  86. wubi := c.GetString("wubi")
  87. pinyin := c.GetString("pinyin")
  88. if id <= 0 {
  89. utils.ErrorLog("id == 0")
  90. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  91. return
  92. }
  93. if len(dealer_name) <= 0 {
  94. utils.ErrorLog("len(dealer_name) == 0")
  95. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  96. return
  97. }
  98. adminUserInfo := c.GetAdminUserInfo()
  99. dealer := models.Dealer{
  100. ID: id,
  101. DealerName: dealer_name,
  102. Contact: contact,
  103. ContactPhone: contact_phone,
  104. ContactAddress: contact_address,
  105. Mtime: time.Now().Unix(),
  106. DealerCode: dealer_code,
  107. Remark: remark,
  108. Creater: adminUserInfo.AdminUser.Id,
  109. Email: email,
  110. PlatformNumber: platform_number,
  111. Modifier: adminUserInfo.AdminUser.Id,
  112. OrgId: adminUserInfo.CurrentOrgId,
  113. Status: 1,
  114. WuBi: wubi,
  115. PinYin: pinyin,
  116. }
  117. err := service.ModifyDealer(&dealer)
  118. if err == nil {
  119. c.ServeSuccessJSON(map[string]interface{}{
  120. "dealer": dealer,
  121. })
  122. } else {
  123. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  124. }
  125. }
  126. func (c *StockApiController) GetDealerName() {
  127. adminUserInfo := c.GetAdminUserInfo()
  128. list, _ := service.FindAllDealerName(adminUserInfo.CurrentOrgId)
  129. c.ServeSuccessJSON(map[string]interface{}{
  130. "list": list,
  131. })
  132. }
  133. func (c *StockApiController) GetDealersList() {
  134. page, _ := c.GetInt64("page", 1)
  135. limit, _ := c.GetInt64("limit", 7)
  136. key, _ := c.GetInt64("key", 0)
  137. adminUserInfo := c.GetAdminUserInfo()
  138. deales, total, err := service.FindAllDealerList(adminUserInfo.CurrentOrgId, page, limit, key)
  139. if err == nil {
  140. c.ServeSuccessJSON(map[string]interface{}{
  141. "dealer": deales,
  142. "total": total,
  143. })
  144. } else {
  145. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  146. }
  147. }
  148. func (c *StockApiController) DeleteDealer() {
  149. id, _ := c.GetInt64("id", 0)
  150. total, _ := service.FindStockInByDealerId(id)
  151. if total > 0 {
  152. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteDealerWrong)
  153. return
  154. }
  155. err := service.DeleteDealerById(id)
  156. if err == nil {
  157. c.ServeSuccessJSON(map[string]interface{}{
  158. "msg": "删除成功",
  159. })
  160. } else {
  161. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  162. }
  163. }
  164. func (c *StockApiController) GetDealer() {
  165. id, _ := c.GetInt64("id", 0)
  166. dealer, err := service.FindDealerById(id)
  167. if err == nil {
  168. c.ServeSuccessJSON(map[string]interface{}{
  169. "dealer": dealer,
  170. })
  171. } else {
  172. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  173. }
  174. }
  175. func (c *StockApiController) CreateManufacturer() {
  176. manufacturer_name := c.GetString("manufacturer_name")
  177. contact := c.GetString("contact")
  178. contact_phone := c.GetString("contact_phone")
  179. platform_number := c.GetString("platform_number")
  180. email := c.GetString("email")
  181. contact_address := c.GetString("contact_address")
  182. remark := c.GetString("remark")
  183. wubi := c.GetString("wubi")
  184. pinyin := c.GetString("pinyin")
  185. if len(manufacturer_name) <= 0 {
  186. utils.ErrorLog("len(manufacturer_name) == 0")
  187. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  188. return
  189. }
  190. adminUserInfo := c.GetAdminUserInfo()
  191. total := service.FindManufacturerTotal(adminUserInfo.CurrentOrgId)
  192. totalStr := strconv.FormatInt(total+1, 10)
  193. code := "2000" + totalStr
  194. manufacturer := models.Manufacturer{
  195. ManufacturerName: manufacturer_name,
  196. Contact: contact,
  197. ContactPhone: contact_phone,
  198. ContactAddress: contact_address,
  199. Ctime: time.Now().Unix(),
  200. Mtime: time.Now().Unix(),
  201. Remark: remark,
  202. Creater: adminUserInfo.AdminUser.Id,
  203. Email: email,
  204. PlatformNumber: platform_number,
  205. OrgId: adminUserInfo.CurrentOrgId,
  206. Status: 1,
  207. ManufacturerCode: code,
  208. WuBi: wubi,
  209. PinYin: pinyin,
  210. }
  211. _, errcode := service.GetManufacturerName(adminUserInfo.CurrentOrgId, manufacturer_name)
  212. if errcode == gorm.ErrRecordNotFound {
  213. err, manufacturers := service.AddSigleManufacturer(&manufacturer)
  214. if err == nil {
  215. c.ServeSuccessJSON(map[string]interface{}{
  216. "manufacturer": manufacturers,
  217. })
  218. } else {
  219. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  220. }
  221. } else if errcode == nil {
  222. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  223. }
  224. }
  225. func (c *StockApiController) ModifyManufacturer() {
  226. id, _ := c.GetInt64("id", 0)
  227. manufacturer_name := c.GetString("manufacturer_name")
  228. contact := c.GetString("contact")
  229. contact_phone := c.GetString("contact_phone")
  230. platform_number := c.GetString("platform_number")
  231. email := c.GetString("email")
  232. contact_address := c.GetString("contact_address")
  233. remark := c.GetString("remark")
  234. manufacturer_code := c.GetString("manufacturer_code")
  235. wubi := c.GetString("wubi")
  236. pinyin := c.GetString("pinyin")
  237. if id <= 0 {
  238. utils.ErrorLog("id == 0")
  239. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  240. return
  241. }
  242. if len(manufacturer_name) <= 0 {
  243. utils.ErrorLog("len(manufacturer_name) == 0")
  244. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  245. return
  246. }
  247. adminUserInfo := c.GetAdminUserInfo()
  248. manufacturer := models.Manufacturer{
  249. ID: id,
  250. ManufacturerName: manufacturer_name,
  251. Contact: contact,
  252. ContactPhone: contact_phone,
  253. ContactAddress: contact_address,
  254. Mtime: time.Now().Unix(),
  255. Remark: remark,
  256. Creater: adminUserInfo.AdminUser.Id,
  257. Email: email,
  258. PlatformNumber: platform_number,
  259. Modifier: adminUserInfo.AdminUser.Id,
  260. OrgId: adminUserInfo.CurrentOrgId,
  261. ManufacturerCode: manufacturer_code,
  262. Status: 1,
  263. WuBi: wubi,
  264. PinYin: pinyin,
  265. }
  266. _, errrcode := service.GetManufacturerName(adminUserInfo.CurrentOrgId, manufacturer_name)
  267. if errrcode == nil {
  268. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeIdCardNoExist)
  269. return
  270. }
  271. err := service.ModifyManufacturer(&manufacturer)
  272. if err == nil {
  273. c.ServeSuccessJSON(map[string]interface{}{
  274. "manufacturer": manufacturer,
  275. })
  276. } else {
  277. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  278. }
  279. }
  280. func (c *StockApiController) GetManufacturerName() {
  281. adminUserInfo := c.GetAdminUserInfo()
  282. list, _ := service.FindAllManufacturerName(adminUserInfo.CurrentOrgId)
  283. c.ServeSuccessJSON(map[string]interface{}{
  284. "list": list,
  285. })
  286. }
  287. func (c *StockApiController) GetManufacturerList() {
  288. page, _ := c.GetInt64("page", 1)
  289. limit, _ := c.GetInt64("limit", 10)
  290. key, _ := c.GetInt64("key", 0)
  291. adminUserInfo := c.GetAdminUserInfo()
  292. manufacturer, total, err := service.FindAllManufacturerList(adminUserInfo.CurrentOrgId, page, limit, key)
  293. if err == nil {
  294. c.ServeSuccessJSON(map[string]interface{}{
  295. "manufacturer": manufacturer,
  296. "total": total,
  297. })
  298. } else {
  299. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  300. }
  301. }
  302. func (c *StockApiController) DeleteManufacturer() {
  303. id, _ := c.GetInt64("id", 0)
  304. total, _ := service.FindStockInByManufacturerId(id)
  305. if total > 0 {
  306. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteManufacturerWrong)
  307. return
  308. }
  309. err := service.DeleteManufacturerById(id)
  310. if err == nil {
  311. c.ServeSuccessJSON(map[string]interface{}{
  312. "msg": "删除成功",
  313. })
  314. } else {
  315. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  316. }
  317. }
  318. func (c *StockApiController) GetManufacturer() {
  319. id, _ := c.GetInt64("id", 0)
  320. manufacturer, err := service.FindManufacturerById(id)
  321. if err == nil {
  322. c.ServeSuccessJSON(map[string]interface{}{
  323. "manufacturer": manufacturer,
  324. })
  325. } else {
  326. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  327. }
  328. }
  329. func (c *StockApiController) GetAllDealer() {
  330. adminUserInfo := c.GetAdminUserInfo()
  331. dealer, err := service.FindAllDealer(adminUserInfo.CurrentOrgId)
  332. if err == nil {
  333. c.ServeSuccessJSON(map[string]interface{}{
  334. "dealer": dealer,
  335. })
  336. } else {
  337. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  338. }
  339. }
  340. func (c *StockApiController) GetAllManufacturer() {
  341. adminUserInfo := c.GetAdminUserInfo()
  342. manufacturer, err := service.FindAllManufacturer(adminUserInfo.CurrentOrgId)
  343. if err == nil {
  344. c.ServeSuccessJSON(map[string]interface{}{
  345. "manufacturer": manufacturer,
  346. })
  347. } else {
  348. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  349. }
  350. }