stock_api_controller.go 11KB

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