new_stock_api_controller.go 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. keywords := this.GetString("keywords")
  148. page, _ := this.GetInt64("page")
  149. is_sale, _ := this.GetInt64("is_sale")
  150. is_settle, _ := this.GetInt64("is_settle")
  151. is_code, _ := this.GetInt64("is_code")
  152. is_type, _ := this.GetInt64("is_type")
  153. orgId := this.GetAdminUserInfo().CurrentOrgId
  154. timeLayout := "2006-01-02"
  155. loc, _ := time.LoadLocation("Local")
  156. var startTime int64
  157. if len(start_time) > 0 {
  158. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  159. if err != nil {
  160. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  161. return
  162. }
  163. startTime = theTime.Unix()
  164. }
  165. var endTime int64
  166. if len(end_time) > 0 {
  167. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  168. if err != nil {
  169. utils.ErrorLog(err.Error())
  170. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  171. return
  172. }
  173. endTime = theTime.Unix()
  174. }
  175. if is_type == 1 {
  176. list, total, _ := service.GetHisDrugCodeQuery(orgId, startTime, endTime, limit, page, is_sale, is_settle, keywords, is_code)
  177. drug, _ := service.GetAllDrugList(orgId)
  178. codeConfig, _ := service.GetDrugCodeConfig(orgId)
  179. patients, _ := service.GetAllpatient(orgId)
  180. this.ServeSuccessJSON(map[string]interface{}{
  181. "list": list,
  182. "total": total,
  183. "drug": drug,
  184. "patients": patients,
  185. "codeConfig": codeConfig,
  186. })
  187. }
  188. if is_type == 2 {
  189. advice, total, _ := service.GetBloodDrugCode(orgId, startTime, endTime, limit, page, is_sale)
  190. drug, _ := service.GetAllDrugList(orgId)
  191. codeConfig, _ := service.GetDrugCodeConfig(orgId)
  192. patients, _ := service.GetAllpatient(orgId)
  193. this.ServeSuccessJSON(map[string]interface{}{
  194. "list": advice,
  195. "total": total,
  196. "drug": drug,
  197. "patients": patients,
  198. "codeConfig": codeConfig,
  199. })
  200. }
  201. return
  202. }
  203. func (this *NewStockApiController) CrateUploadDrugCode() {
  204. drug_id, _ := this.GetInt64("drug_id")
  205. patient_id, _ := this.GetInt64("patient_id")
  206. advice_date, _ := this.GetInt64("advice_date")
  207. orgId := this.GetAdminUserInfo().CurrentOrgId
  208. //查询该药品是否出库
  209. outinfo, _ := service.GetDrugWarehouseOutInfoByDrugCode(drug_id, patient_id, advice_date, orgId)
  210. if len(outinfo) == 0 {
  211. this.ServeDynamicFailJsonSend("该药品未出库,请先出库")
  212. return
  213. } else {
  214. this.ServeDynamicFailJsonSend("成功!")
  215. return
  216. }
  217. }
  218. func (this *NewStockApiController) GetPatientDrugCode() {
  219. drug_id, _ := this.GetInt64("drug_id")
  220. patient_id, _ := this.GetInt64("patient_id")
  221. advice_date, _ := this.GetInt64("advice_date")
  222. orgId := this.GetAdminUserInfo().CurrentOrgId
  223. advicelist, _ := service.FindeHisAdviceDocAdviceOne(orgId, patient_id, drug_id, advice_date)
  224. if len(advicelist) > 0 {
  225. var total int64
  226. var prescribing_number_total int64
  227. for _, item := range advicelist {
  228. //查询改药品信息
  229. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  230. //如果是药房发药的方式
  231. if medical.IsPharmacy == 1 {
  232. houseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  233. //查询该药品是否有库存
  234. list, _ := service.GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
  235. //判断单位是否相等
  236. if medical.MaxUnit == item.PrescribingNumberUnit {
  237. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  238. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  239. //转化为最小单位
  240. total = list.Count*medical.MinNumber + list.StockMinNumber
  241. prescribing_number_total = count * medical.MinNumber
  242. }
  243. if medical.MinUnit == item.PrescribingNumberUnit {
  244. prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
  245. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  246. total = list.Count*medical.MinNumber + list.StockMinNumber
  247. prescribing_number_total = count
  248. }
  249. if medical.IsUse != 1 {
  250. //如果出库数量大于库存数量
  251. if prescribing_number_total > total {
  252. err := fmt.Errorf(service.FindDrugsName(item.DrugId) + "库存不足")
  253. if err != nil {
  254. utils.ErrorLog(err.Error())
  255. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, err.Error())
  256. return
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. creater := this.GetAdminUserInfo().AdminUser.Id
  264. service.DispensingMedicineTwo(orgId, patient_id, advice_date, drug_id, creater)
  265. this.ServeSuccessJSON(map[string]interface{}{
  266. "msg": "发药成功!",
  267. })
  268. return
  269. }