stock_api_controller.go 9.8KB

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