new_stock_api_controller.go 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package controllers
  2. import (
  3. "fmt"
  4. "math"
  5. "strconv"
  6. "time"
  7. "XT_New/enums"
  8. "XT_New/service"
  9. "XT_New/utils"
  10. "github.com/astaxie/beego"
  11. )
  12. type NewStockApiController struct {
  13. BaseAuthAPIController
  14. }
  15. func NewStockApiRegistRouters() {
  16. beego.Router("/api/drug/stock", &NewStockApiController{}, "Get:GetDrugStock")
  17. beego.Router("/api/drug/change", &NewStockApiController{}, "Get:GetDrugChange")
  18. //beego.Router("/api/drug/query", &NewStockApiController{}, "Get:GetDrugQuery")
  19. beego.Router("/api/drug/gethisdrugcodequery", &NewStockApiController{}, "Get:GetHisDrugCodeQuery")
  20. beego.Router("/api/drug/createuploaddrugcode", &NewStockApiController{}, "Get:CrateUploadDrugCode")
  21. beego.Router("/api/drug/getpatientdrugcode", &NewStockApiController{}, "Get:GetPatientDrugCode")
  22. }
  23. func (c *NewStockApiController) GetDrugStock() {
  24. start_time := c.GetString("start_time")
  25. end_time := c.GetString("end_time")
  26. timeLayout := "2006-01-02"
  27. loc, _ := time.LoadLocation("Local")
  28. var startTime int64
  29. if len(start_time) > 0 {
  30. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  31. if err != nil {
  32. fmt.Println(err)
  33. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  34. return
  35. }
  36. startTime = theTime.Unix()
  37. }
  38. var endTime int64
  39. if len(end_time) > 0 {
  40. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  41. if err != nil {
  42. utils.ErrorLog(err.Error())
  43. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  44. return
  45. }
  46. endTime = theTime.Unix()
  47. }
  48. admin := c.GetAdminUserInfo()
  49. is_pc, _ := c.GetInt64("is_pc", 0)
  50. is_bg, _ := c.GetInt64("is_bg", 0)
  51. //manufacturerList, _ := service.GetAllManufacturerList(admin.CurrentOrgId)
  52. //dealerList, _ := service.GetAllDealerList(admin.CurrentOrgId)
  53. fmt.Println(startTime)
  54. fmt.Println(endTime)
  55. warehousingInfo, err := service.FindAllDrugWarehousingInfo(admin.CurrentOrgId, is_pc, is_bg, startTime, endTime)
  56. //list, _ := service.GetAll/**/StoreHouseListThree(admin.CurrentOrgId)
  57. if err == nil {
  58. c.ServeSuccessJSON(map[string]interface{}{
  59. "info": warehousingInfo,
  60. //"manufacturerList": manufacturerList,
  61. //"dealerList": dealerList,
  62. //"list": list,
  63. })
  64. } else {
  65. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  66. }
  67. }
  68. func (c *NewStockApiController) GetDrugChange() {
  69. start_time := c.GetString("start_time")
  70. end_time := c.GetString("end_time")
  71. is_sale, _ := c.GetInt64("is_sale")
  72. is_bg, _ := c.GetInt64("is_bg")
  73. timeLayout := "2006-01-02"
  74. loc, _ := time.LoadLocation("Local")
  75. var startTime int64
  76. if len(start_time) > 0 {
  77. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  78. if err != nil {
  79. fmt.Println(err)
  80. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  81. return
  82. }
  83. startTime = theTime.Unix()
  84. }
  85. var endTime int64
  86. if len(end_time) > 0 {
  87. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  88. if err != nil {
  89. utils.ErrorLog(err.Error())
  90. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  91. return
  92. }
  93. endTime = theTime.Unix()
  94. }
  95. orgId := c.GetAdminUserInfo().CurrentOrgId
  96. page, _ := c.GetInt64("page")
  97. limit, _ := c.GetInt64("limit")
  98. list, total, _ := service.GetNewDrugFlow(orgId, limit, page, startTime, endTime, is_sale, is_bg)
  99. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  100. dealerList, _ := service.GetAllDealerList(orgId)
  101. houseList, _ := service.GetAllStoreHouseList(orgId)
  102. patientList, _ := service.GetAllPatientListSix(orgId)
  103. c.ServeSuccessJSON(map[string]interface{}{
  104. "info": list,
  105. "total": total,
  106. "manufacturerList": manufacturerList,
  107. "dealerList": dealerList,
  108. "houseList": houseList,
  109. "patientList": patientList,
  110. })
  111. }
  112. func (c *NewStockApiController) GetDrugQuery() {
  113. id, _ := c.GetInt64("id", 0)
  114. if id <= 0 {
  115. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  116. return
  117. }
  118. admin := c.GetAdminUserInfo()
  119. warehousing, err := service.FindDrugWarehousingById(id, admin.CurrentOrgId)
  120. manufacturerList, _ := service.GetAllManufacturerList(admin.CurrentOrgId)
  121. dealerList, _ := service.GetAllDealerList(admin.CurrentOrgId)
  122. if err != nil {
  123. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  124. return
  125. }
  126. if warehousing.ID <= 0 {
  127. return
  128. }
  129. warehousingInfo, err := service.FindDrugWarehousingInfoByIdThree(id, admin.CurrentOrgId)
  130. list, _ := service.GetAllStoreHouseListThree(admin.CurrentOrgId)
  131. if err == nil {
  132. c.ServeSuccessJSON(map[string]interface{}{
  133. "info": warehousingInfo,
  134. "warehousing": warehousing,
  135. "manufacturerList": manufacturerList,
  136. "dealerList": dealerList,
  137. "list": list,
  138. })
  139. } else {
  140. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  141. }
  142. }
  143. func (this *NewStockApiController) GetHisDrugCodeQuery() {
  144. start_time := this.GetString("start_time")
  145. end_time := this.GetString("end_time")
  146. limit, _ := this.GetInt64("limit")
  147. page, _ := this.GetInt64("page")
  148. is_sale, _ := this.GetInt64("is_sale")
  149. orgId := this.GetAdminUserInfo().CurrentOrgId
  150. timeLayout := "2006-01-02"
  151. loc, _ := time.LoadLocation("Local")
  152. var startTime int64
  153. if len(start_time) > 0 {
  154. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  155. if err != nil {
  156. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  157. return
  158. }
  159. startTime = theTime.Unix()
  160. }
  161. var endTime int64
  162. if len(end_time) > 0 {
  163. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  164. if err != nil {
  165. utils.ErrorLog(err.Error())
  166. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  167. return
  168. }
  169. endTime = theTime.Unix()
  170. }
  171. list, total, _ := service.GetHisDrugCodeQuery(orgId, startTime, endTime, limit, page, is_sale)
  172. drug, _ := service.GetAllDrugList(orgId)
  173. codeConfig, _ := service.GetDrugCodeConfig(orgId)
  174. patients, _ := service.GetAllpatient(orgId)
  175. this.ServeSuccessJSON(map[string]interface{}{
  176. "list": list,
  177. "total": total,
  178. "drug": drug,
  179. "patients": patients,
  180. "codeConfig": codeConfig,
  181. })
  182. return
  183. }
  184. func (this *NewStockApiController) CrateUploadDrugCode() {
  185. drug_id, _ := this.GetInt64("drug_id")
  186. patient_id, _ := this.GetInt64("patient_id")
  187. advice_date, _ := this.GetInt64("advice_date")
  188. orgId := this.GetAdminUserInfo().CurrentOrgId
  189. //查询该药品是否出库
  190. outinfo, _ := service.GetDrugWarehouseOutInfoByDrugCode(drug_id, patient_id, advice_date, orgId)
  191. if len(outinfo) == 0 {
  192. this.ServeDynamicFailJsonSend("该药品未出库,请先出库")
  193. return
  194. } else {
  195. this.ServeDynamicFailJsonSend("成功!")
  196. return
  197. }
  198. }
  199. func (this *NewStockApiController) GetPatientDrugCode() {
  200. drug_id, _ := this.GetInt64("drug_id")
  201. patient_id, _ := this.GetInt64("patient_id")
  202. advice_date, _ := this.GetInt64("advice_date")
  203. orgId := this.GetAdminUserInfo().CurrentOrgId
  204. advicelist, _ := service.FindeHisAdviceDocAdviceOne(orgId, patient_id, drug_id, advice_date)
  205. if len(advicelist) > 0 {
  206. var total int64
  207. var prescribing_number_total int64
  208. for _, item := range advicelist {
  209. //查询改药品信息
  210. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  211. //如果是药房发药的方式
  212. if medical.IsPharmacy == 1 {
  213. houseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  214. //查询该药品是否有库存
  215. list, _ := service.GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  216. //判断单位是否相等
  217. if medical.MaxUnit == item.PrescribingNumberUnit {
  218. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  219. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  220. //转化为最小单位
  221. total = list.Count*medical.MinNumber + list.StockMinNumber
  222. prescribing_number_total = count * medical.MinNumber
  223. }
  224. if medical.MinUnit == item.PrescribingNumberUnit {
  225. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  226. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  227. total = list.Count*medical.MinNumber + list.StockMinNumber
  228. prescribing_number_total = count
  229. }
  230. if medical.IsUse != 1 {
  231. //如果出库数量大于库存数量
  232. if prescribing_number_total > total {
  233. err := fmt.Errorf(service.FindDrugsName(item.DrugId) + "库存不足")
  234. if err != nil {
  235. utils.ErrorLog(err.Error())
  236. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  237. return
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }
  244. creater := this.GetAdminUserInfo().AdminUser.Id
  245. service.DispensingMedicineTwo(orgId, patient_id, advice_date, drug_id, creater)
  246. this.ServeSuccessJSON(map[string]interface{}{
  247. "msg": "发药成功!",
  248. })
  249. return
  250. }