secondary_order_api_contorller.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/astaxie/beego"
  10. "github.com/jinzhu/gorm"
  11. "time"
  12. )
  13. type SecondaryOrderApiController struct {
  14. BaseAuthAPIController
  15. }
  16. func SecondaryOrderApiRegistRouters() {
  17. beego.Router("/api/secondary/getcode", &SecondaryOrderApiController{}, "get:GetStoreCode") //获取仓库编码
  18. beego.Router("/api/secondary/updatestatus", &SecondaryOrderApiController{}, "get:UpdateStatus") //修改仓库状态
  19. beego.Router("/api/secondary/deletestorehouse", &SecondaryOrderApiController{}, "get:DeleteStorehouse") //删除仓库
  20. beego.Router("/api/secondary/isstorehousename", &SecondaryOrderApiController{}, "get:IsStorehouseName") //仓库名称是否重复
  21. beego.Router("/api/secondary/isstorehouseaddress", &SecondaryOrderApiController{}, "get:IsStorehouseAddress") //仓库地址是否重复
  22. beego.Router("/api/secondary/storehouselist", &SecondaryOrderApiController{}, "get:StorehouseList") //分页
  23. beego.Router("/api/secondary/addstorehouse", &SecondaryOrderApiController{}, "post:AddStorehouse") //新增仓库
  24. beego.Router("/api/secondary/updatestorehouse", &SecondaryOrderApiController{}, "post:UpdateStorehouse") //修改
  25. beego.Router("/api/secondary/getonestorehouse", &SecondaryOrderApiController{}, "get:GetOneStorehouse") //查一条仓库的信息
  26. beego.Router("/api/secondary/getallstorehousename", &SecondaryOrderApiController{}, "get:GetAllStorehouseName") //获取当前机构的所有可用仓库名称
  27. beego.Router("/api/secondary/findstorehouseconfig", &SecondaryOrderApiController{}, "get:FindStorehouseConfig") //查询该机构的仓库配置
  28. beego.Router("/api/secondary/updateinfo", &SecondaryOrderApiController{}, "get:UpdateInfo") //更改耗材自动入库仓库
  29. beego.Router("/api/secondary/updateoutinfo", &SecondaryOrderApiController{}, "get:UpdateOutInfo") //更改耗材自动出库仓库
  30. beego.Router("/api/secondary/updatedruginfo", &SecondaryOrderApiController{}, "get:UpdateDrugInfo") //更改药品自动入库仓库
  31. beego.Router("/api/secondary/updatedrugout", &SecondaryOrderApiController{}, "get:UpdateDrugOut") //更改药品自动出库仓库
  32. beego.Router("/api/secondary/byliinit", &SecondaryOrderApiController{}, "get:Byliinit") //初始化旧数据
  33. }
  34. //获取仓库编码
  35. func (this *SecondaryOrderApiController) GetStoreCode() {
  36. orgId := this.GetAdminUserInfo().CurrentOrgId
  37. var code string
  38. for a := true; a == true; {
  39. code = service.CreateCode()
  40. tmp := service.FindStorehouseCode(orgId, code)
  41. //如果没有重复的编码结束循环
  42. if tmp == false {
  43. a = false
  44. }
  45. }
  46. this.ServeSuccessJSON(map[string]interface{}{
  47. "list": code,
  48. })
  49. return
  50. }
  51. //修改仓库状态
  52. func (this *SecondaryOrderApiController) UpdateStatus() {
  53. orgId := this.GetAdminUserInfo().CurrentOrgId
  54. check := map[string][]string{
  55. "id": {"must", "int", "id"},
  56. }
  57. _, err := checkParams(this, &check)
  58. if err != nil {
  59. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  60. return
  61. }
  62. id, _ := this.GetInt64("id")
  63. //判断该仓库的库存是否为零
  64. boolean := service.IsStorehouseNil(id, orgId)
  65. if boolean == false {
  66. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该仓库库存不为0,不支持该操作")
  67. return
  68. }
  69. //判断该仓库是否在仓库配置表中
  70. boolean = service.IsInConfig(orgId, id)
  71. if boolean == true {
  72. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "当前仓库是自动出入库仓库,请先取消自动出入库之后才能进行此操作")
  73. return
  74. }
  75. //修改仓库状态
  76. err = service.UpdateStorehouseStatus(id)
  77. if err != nil {
  78. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  79. return
  80. }
  81. this.ServeSuccessJSON(map[string]interface{}{
  82. "list": "修改成功",
  83. })
  84. return
  85. }
  86. //删除仓库
  87. func (this *SecondaryOrderApiController) DeleteStorehouse() {
  88. orgId := this.GetAdminUserInfo().CurrentOrgId
  89. check := map[string][]string{
  90. "id": {"must", "int", "id"},
  91. }
  92. _, err := checkParams(this, &check)
  93. if err != nil {
  94. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  95. return
  96. }
  97. id, _ := this.GetInt64("id")
  98. //判断该仓库的库存是否为零
  99. boolean := service.IsStorehouseNil(id, orgId)
  100. if boolean == false {
  101. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该仓库库存不为0,不支持该操作")
  102. return
  103. }
  104. //判断该仓库是否在仓库配置表中
  105. boolean = service.IsInConfig(orgId, id)
  106. if boolean == true {
  107. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "当前仓库是自动出入库仓库,请先取消自动出入库之后才能进行此操作")
  108. return
  109. }
  110. err = service.DeleteStorehouse(id)
  111. if err != nil {
  112. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  113. return
  114. }
  115. this.ServeSuccessJSON(map[string]interface{}{
  116. "list": "删除成功",
  117. })
  118. return
  119. }
  120. //仓库名称是否重复
  121. func (this *SecondaryOrderApiController) IsStorehouseName() {
  122. orgId := this.GetAdminUserInfo().CurrentOrgId
  123. storehouse_name := this.GetString("storehouse_name")
  124. check := map[string][]string{
  125. "storehouse_name": {"must", "string", "storehouse_name"},
  126. }
  127. _, err := checkParams(this, &check)
  128. if err != nil {
  129. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  130. return
  131. }
  132. var bo bool
  133. bo, err = service.IsStorehouseName(orgId, storehouse_name)
  134. if bo == true {
  135. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该仓库已存在,请重新输入")
  136. return
  137. }
  138. this.ServeSuccessJSON(map[string]interface{}{
  139. "list": "ok",
  140. })
  141. return
  142. }
  143. //仓库地址是否重复
  144. func (this *SecondaryOrderApiController) IsStorehouseAddress() {
  145. orgId := this.GetAdminUserInfo().CurrentOrgId
  146. storehouse_address := this.GetString("storehouse_address")
  147. check := map[string][]string{
  148. "storehouse_address": {"must", "string", "storehouse_address"},
  149. }
  150. _, err := checkParams(this, &check)
  151. if err != nil {
  152. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  153. return
  154. }
  155. var bo bool
  156. bo, err = service.IsStorehouseAddress(orgId, storehouse_address)
  157. if bo == true {
  158. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该地址已存在,请重新输入")
  159. return
  160. }
  161. this.ServeSuccessJSON(map[string]interface{}{
  162. "list": "ok",
  163. })
  164. return
  165. }
  166. //分页
  167. func (this *SecondaryOrderApiController) StorehouseList() {
  168. page, _ := this.GetInt64("page") //页码
  169. limit, _ := this.GetInt64("limit") //每一页查出来的条数
  170. check := map[string][]string{
  171. "page": {"must", "string", "page"},
  172. "limit": {"must", "string", "limit"},
  173. }
  174. _, err := checkParams(this, &check)
  175. keyword := this.GetString("keyword")
  176. orgId := this.GetAdminUserInfo().CurrentOrgId
  177. if err != nil {
  178. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  179. }
  180. //获取分页的数据
  181. list, total, err := service.StorehouseList(page, limit, orgId, keyword)
  182. this.ServeSuccessJSON(map[string]interface{}{
  183. "list": list,
  184. "total": total,
  185. })
  186. }
  187. //新增仓库
  188. func (this *SecondaryOrderApiController) AddStorehouse() {
  189. orgId := this.GetAdminUserInfo().CurrentOrgId
  190. dataBody := make(map[string]interface{}, 0)
  191. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  192. if err != nil {
  193. utils.ErrorLog(err.Error())
  194. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  195. return
  196. }
  197. var storehouse_status, admin_id int64
  198. var admin_name string
  199. tmpstatus := dataBody["storehouse_status"]
  200. tmpid := dataBody["storehouse_admin_id"]
  201. tmpname := dataBody["storehouse_admin_name"]
  202. if tmpstatus == nil {
  203. storehouse_status = 1
  204. } else {
  205. storehouse_status = int64(dataBody["storehouse_status"].(float64)) //状态
  206. }
  207. if tmpid == nil {
  208. admin_id = 0
  209. } else {
  210. admin_id = int64(dataBody["storehouse_status"].(float64)) //管理员id
  211. }
  212. if tmpname == nil {
  213. admin_name = "admin"
  214. } else {
  215. admin_name = dataBody["storehouse_status"].(string) //管理员名称
  216. }
  217. switch {
  218. case dataBody["storehouse_code"] == nil:
  219. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库编号不能为空")
  220. return
  221. case dataBody["storehouse_name"] == nil:
  222. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库名称不能为空")
  223. return
  224. case dataBody["storehouse_address"] == nil:
  225. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库地址不能为空")
  226. return
  227. }
  228. code := dataBody["storehouse_code"].(string) //仓库编号
  229. name := dataBody["storehouse_name"].(string) //仓库名称
  230. address := dataBody["storehouse_address"].(string) //地址
  231. switch {
  232. case name == "":
  233. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库名称不能为空")
  234. return
  235. case address == "":
  236. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库地址不能为空")
  237. return
  238. }
  239. //判断仓库名称是否重复
  240. var bo bool
  241. bo, err = service.IsStorehouseName(orgId, name)
  242. if bo == true {
  243. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该仓库已存在,请重新输入")
  244. return
  245. }
  246. //判断仓库地址是否重复
  247. bo, err = service.IsStorehouseAddress(orgId, address)
  248. if bo == true {
  249. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "该地址已存在,请重新输入")
  250. return
  251. }
  252. //storehouse_admin := int64(dataBody["storehouse_admin"].(float64))//仓库管理员,暂时不用管
  253. storehouse := models.Storehouse{
  254. StorehouseCode: code,
  255. StorehouseName: name,
  256. StorehouseAddress: address,
  257. StorehouseStatus: storehouse_status,
  258. UserOrgId: orgId,
  259. Status: 1,
  260. StorehouseAdminId: admin_id,
  261. StorehouseAdminName: admin_name,
  262. Ctime: time.Now().Unix(),
  263. }
  264. err = service.AddStroehouse(storehouse)
  265. if err != nil {
  266. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "保存失败")
  267. return
  268. }
  269. this.ServeSuccessJSON(map[string]interface{}{
  270. "list": "保存成功",
  271. })
  272. return
  273. }
  274. //修改仓库
  275. func (this *SecondaryOrderApiController) UpdateStorehouse() {
  276. dataBody := make(map[string]interface{}, 0)
  277. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  278. if err != nil {
  279. utils.ErrorLog(err.Error())
  280. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  281. return
  282. }
  283. var storehouse_status int64
  284. tmpstatus := dataBody["storehouse_status"]
  285. if tmpstatus == nil {
  286. storehouse_status = 1
  287. } else {
  288. storehouse_status = int64(dataBody["storehouse_status"].(float64)) //状态
  289. }
  290. switch {
  291. case dataBody["id"] == nil:
  292. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库id不能为空")
  293. return
  294. case dataBody["storehouse_name"] == nil:
  295. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库名称不能为空")
  296. return
  297. case dataBody["storehouse_address"] == nil:
  298. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "仓库地址不能为空")
  299. return
  300. }
  301. id := int64(dataBody["id"].(float64))
  302. name := dataBody["storehouse_name"].(string) //仓库名称
  303. address := dataBody["storehouse_address"].(string) //地址
  304. //storehouse_admin := int64(dataBody["storehouse_admin"].(float64))//仓库管理员,暂时不用管
  305. storehouse := models.Storehouse{
  306. ID: id,
  307. StorehouseName: name,
  308. StorehouseAddress: address,
  309. StorehouseStatus: storehouse_status,
  310. Mtime: time.Now().Unix(),
  311. }
  312. err = service.UpdateStroehouse(storehouse)
  313. if err != nil {
  314. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "保存失败")
  315. return
  316. }
  317. this.ServeSuccessJSON(map[string]interface{}{
  318. "list": "保存成功",
  319. })
  320. return
  321. }
  322. //查询一条仓库信息
  323. func (this *SecondaryOrderApiController) GetOneStorehouse() {
  324. orgId := this.GetAdminUserInfo().CurrentOrgId
  325. check := map[string][]string{
  326. "id": {"must", "int", "id"},
  327. }
  328. _, err := checkParams(this, &check)
  329. if err != nil {
  330. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  331. return
  332. }
  333. id, _ := this.GetInt64("id")
  334. var list models.Storehouse
  335. list, err = service.GetOneStorehouse(id, orgId)
  336. if err != nil {
  337. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  338. return
  339. }
  340. this.ServeSuccessJSON(map[string]interface{}{
  341. "list": list,
  342. })
  343. return
  344. }
  345. //获取当前机构所有可用仓库的名字
  346. func (this *SecondaryOrderApiController) GetAllStorehouseName() {
  347. orgId := this.GetAdminUserInfo().CurrentOrgId
  348. list, err := service.GetAllStorehouseName(orgId)
  349. if err != nil {
  350. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  351. return
  352. }
  353. this.ServeSuccessJSON(map[string]interface{}{
  354. "list": list,
  355. })
  356. }
  357. //根据机构id查询仓库配置
  358. func (this *SecondaryOrderApiController) FindStorehouseConfig() {
  359. orgId := this.GetAdminUserInfo().CurrentOrgId
  360. storehouse, err := service.FindStorehouseConfig(orgId)
  361. //如果没有仓库配置信息就新建一个
  362. if err == gorm.ErrRecordNotFound {
  363. err := service.GetDefaultStorehouse(orgId)
  364. if err != nil {
  365. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  366. return
  367. }
  368. storehouse, err = service.FindStorehouseConfig(orgId)
  369. if err != nil {
  370. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  371. return
  372. }
  373. }
  374. if err != nil && err.Error() != "record not found" {
  375. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  376. return
  377. }
  378. var storehouse_info, storehouse_out_info, drug_storehouse_info, drug_storehouse_out models.Storehouse
  379. storehouse_info, err = service.FindStorehouseName(storehouse.StorehouseInfo)
  380. if err != nil {
  381. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  382. return
  383. }
  384. storehouse_out_info, err = service.FindStorehouseName(storehouse.StorehouseOutInfo)
  385. if err != nil {
  386. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  387. return
  388. }
  389. drug_storehouse_info, err = service.FindStorehouseName(storehouse.DrugStorehouseInfo)
  390. if err != nil {
  391. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  392. return
  393. }
  394. drug_storehouse_out, err = service.FindStorehouseName(storehouse.DrugStorehouseOut)
  395. if err != nil {
  396. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  397. return
  398. }
  399. this.ServeSuccessJSON(map[string]interface{}{
  400. "storehouse_info": storehouse_info.StorehouseName,
  401. "storehouse_out_info": storehouse_out_info.StorehouseName,
  402. "drug_storehouse_info": drug_storehouse_info.StorehouseName,
  403. "drug_storehouse_out": drug_storehouse_out.StorehouseName,
  404. })
  405. return
  406. }
  407. //更改耗材自动入库仓库
  408. func (this *SecondaryOrderApiController) UpdateInfo() {
  409. orgId := this.GetAdminUserInfo().CurrentOrgId
  410. check := map[string][]string{
  411. "id": {"must", "int", "id"},
  412. }
  413. _, err := checkParams(this, &check)
  414. if err != nil {
  415. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  416. return
  417. }
  418. id, _ := this.GetInt64("id")
  419. err = service.UpdateInfo(orgId, id)
  420. if err != nil {
  421. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  422. return
  423. }
  424. this.ServeSuccessJSON(map[string]interface{}{
  425. "list": "修改成功",
  426. })
  427. return
  428. }
  429. //更改耗材自动出库仓库
  430. func (this *SecondaryOrderApiController) UpdateOutInfo() {
  431. orgId := this.GetAdminUserInfo().CurrentOrgId
  432. check := map[string][]string{
  433. "id": {"must", "int", "id"},
  434. }
  435. _, err := checkParams(this, &check)
  436. if err != nil {
  437. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  438. return
  439. }
  440. id, _ := this.GetInt64("id")
  441. err = service.UpdateOutInfo(orgId, id)
  442. if err != nil {
  443. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  444. return
  445. }
  446. this.ServeSuccessJSON(map[string]interface{}{
  447. "list": "修改成功",
  448. })
  449. return
  450. }
  451. //更改药品自动入库仓库
  452. func (this *SecondaryOrderApiController) UpdateDrugInfo() {
  453. orgId := this.GetAdminUserInfo().CurrentOrgId
  454. check := map[string][]string{
  455. "id": {"must", "int", "id"},
  456. }
  457. _, err := checkParams(this, &check)
  458. if err != nil {
  459. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  460. return
  461. }
  462. id, _ := this.GetInt64("id")
  463. err = service.UpdateDrugInfo2(orgId, id)
  464. if err != nil {
  465. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  466. return
  467. }
  468. this.ServeSuccessJSON(map[string]interface{}{
  469. "list": "修改成功",
  470. })
  471. return
  472. }
  473. //更改药品自动出库仓库
  474. func (this *SecondaryOrderApiController) UpdateDrugOut() {
  475. orgId := this.GetAdminUserInfo().CurrentOrgId
  476. check := map[string][]string{
  477. "id": {"must", "int", "id"},
  478. }
  479. _, err := checkParams(this, &check)
  480. if err != nil {
  481. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  482. return
  483. }
  484. id, _ := this.GetInt64("id")
  485. err = service.UpdateDrugOut(orgId, id)
  486. if err != nil {
  487. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  488. return
  489. }
  490. this.ServeSuccessJSON(map[string]interface{}{
  491. "list": "修改成功",
  492. })
  493. return
  494. }
  495. //判断前端参数是否为空
  496. func checkParams(this *SecondaryOrderApiController, m *map[string][]string) (map[string]string, error) {
  497. tmp := make(map[string]string)
  498. for k, v := range *m {
  499. t := this.GetString(k)
  500. if v[0] == "must" && t == "" {
  501. return nil, fmt.Errorf(v[2] + "不能为空")
  502. }
  503. tmp[k] = t
  504. }
  505. return tmp, nil
  506. }
  507. //兼容旧数据
  508. func (this *SecondaryOrderApiController) Byliinit() {
  509. err := service.Byliinit()
  510. if err != nil {
  511. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  512. return
  513. }
  514. this.ServeSuccessJSON(map[string]interface{}{
  515. "list": "初始化成功",
  516. })
  517. return
  518. }