drug_stock_api_contorller.go 64KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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. "reflect"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type StockDrugApiController struct {
  16. BaseAuthAPIController
  17. }
  18. func DrugStockManagerApiRegistRouters() {
  19. //入库管理和其他入库管理相关接口
  20. beego.Router("/api/drugwarehouse/create", &StockDrugApiController{}, "post:CreateDrugWarehouse")
  21. beego.Router("/api/drugwarehouse/list", &StockDrugApiController{}, "get:GetDrugWarehouseList")
  22. beego.Router("/api/drugwarehouse/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouse")
  23. beego.Router("/api/drugwarehouseinfo/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouseInfo")
  24. beego.Router("/api/drugwarehouseinfo/list", &StockDrugApiController{}, "get:GetDrugWarehouseInfoList")
  25. beego.Router("/api/drugwarehouse/edit", &StockDrugApiController{}, "post:EditDrugWarehouse")
  26. //退货管理和其他退货管理相关接口
  27. beego.Router("/api/drugsalesreturn/create", &StockDrugApiController{}, "post:CreateDrugSalesReturn")
  28. beego.Router("/api/drugsalesreturn/delete", &StockDrugApiController{}, "post:DeleteDrugSalesReturn")
  29. beego.Router("/api/drugsalesreturninfo/delete", &StockDrugApiController{}, "post:DeleteDrugSalesReturnInfo")
  30. beego.Router("/api/drugsalesreturninfo/list", &StockDrugApiController{}, "get:GetDrugSalesReturnInfoList")
  31. beego.Router("/api/drugsalesreturn/list", &StockDrugApiController{}, "get:GetDrugSalesReturnList")
  32. beego.Router("/api/drugsalesreturn/edit", &StockDrugApiController{}, "post:EditDrugReturnInfo")
  33. //出库管理和其他出库管理相关接口
  34. beego.Router("/api/drugwarehouseout/create", &StockDrugApiController{}, "post:CreateDrugWarehouseOut")
  35. beego.Router("/api/drugwarehouseout/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouseOut")
  36. beego.Router("/api/drugwarehouseoutinfo/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouseOutInfo")
  37. beego.Router("/api/drugwarehouseout/list", &StockDrugApiController{}, "get:GetDrugWarehouseOutList")
  38. beego.Router("/api/drugwarehouseout/edit", &StockDrugApiController{}, "post:EditDrugWarehouseOut")
  39. beego.Router("/api/drugwarehouseout/info", &StockDrugApiController{}, "get:GetDrugWarehouseOutInfoList")
  40. //出库退库管理和其他出库退库管理相关接口
  41. beego.Router("/api/drugcancelstock/create", &StockDrugApiController{}, "post:CreateDrugCancelStock")
  42. beego.Router("/api/drugcancelstock/list", &StockDrugApiController{}, "get:GetDrugCancelStockList")
  43. beego.Router("/api/drugcancelstock/info", &StockDrugApiController{}, "get:GetDrugCancelStockInfoList")
  44. beego.Router("/api/drugcancelstock/delete", &StockDrugApiController{}, "post:DeleteDrugCancelStock")
  45. beego.Router("/api/drugcancelstockinfo/delete", &StockDrugApiController{}, "post:DeleteDrugCancelStockInfo")
  46. beego.Router("/api/drugcancelstockinfo/edit", &StockDrugApiController{}, "post:EditDrugCancelStock")
  47. //beego.Router("/api/drugcancelstockinfo/config", &StockDrugApiController{}, "get:GetDrugCancelStockConfig")
  48. ////库存相关全局配置
  49. beego.Router("/api/drugstock/config", &StockDrugApiController{}, "get:GetAllConfig")
  50. //beego.Router("/api/drugreturn/config", &StockDrugApiController{}, "get:GetAllSalesReturnConfig")
  51. //
  52. ////库存查询
  53. beego.Router("/api/drugstock/query", &StockDrugApiController{}, "get:GetDrugQueryInfo")
  54. //
  55. //beego.Router("/api/drugstock/detail", &StockDrugApiController{}, "get:GetDetailInfo")
  56. //
  57. beego.Router("/api/drugwarehouseout/user", &StockDrugApiController{}, "get:GetUserDetailInfo")
  58. beego.Router("/api/drugstock/get", &StockDrugApiController{}, "get:GetAllDrugStock")
  59. }
  60. func (c *StockDrugApiController) CreateDrugWarehouse() {
  61. warehousing_time := c.GetString("warehousing_time")
  62. dealer_id, _ := c.GetInt64("dealer_id", 0)
  63. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  64. types, _ := c.GetInt64("type", 0)
  65. ctime := time.Now().Unix()
  66. adminUserInfo := c.GetAdminUserInfo()
  67. warehousingDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_time)
  68. if parseDateErr != nil {
  69. c.ErrorLog("日期(%v)解析错误:%v", warehousingDate, parseDateErr)
  70. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  71. return
  72. }
  73. timeStr := time.Now().Format("2006-01-02")
  74. timeArr := strings.Split(timeStr, "-")
  75. total, _ := service.FindAllWarehouseTotal(adminUserInfo.CurrentOrgId)
  76. total = total + 1
  77. warehousing_order := "YPRKD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  78. operation_time := time.Now().Unix()
  79. creater := adminUserInfo.AdminUser.Id
  80. warehousing := models.DrugWarehouse{
  81. WarehousingOrder: warehousing_order,
  82. OperationTime: operation_time,
  83. OrgId: adminUserInfo.CurrentOrgId,
  84. Creater: creater,
  85. Ctime: ctime,
  86. Status: 1,
  87. WarehousingTime: warehousingDate.Unix(),
  88. Dealer: dealer_id,
  89. Manufacturer: manufacturer_id,
  90. Type: types,
  91. }
  92. service.AddSigleDrugWarehouse(&warehousing)
  93. dataBody := make(map[string]interface{}, 0)
  94. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  95. if err != nil {
  96. utils.ErrorLog(err.Error())
  97. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  98. return
  99. }
  100. var warehousingInfo []*models.DrugWarehouseInfo
  101. if dataBody["stockIn"] != nil && reflect.TypeOf(dataBody["stockIn"]).String() == "[]interface {}" {
  102. thisStockIn, _ := dataBody["stockIn"].([]interface{})
  103. if len(thisStockIn) > 0 {
  104. for _, item := range thisStockIn {
  105. items := item.(map[string]interface{})
  106. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  107. utils.ErrorLog("drug_id")
  108. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  109. return
  110. }
  111. drug_id := int64(items["drug_id"].(float64))
  112. if items["warehousing_count"] == nil || reflect.TypeOf(items["warehousing_count"]).String() != "string" {
  113. utils.ErrorLog("warehousing_count")
  114. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  115. return
  116. }
  117. warehousing_count, _ := strconv.ParseInt(items["warehousing_count"].(string), 10, 64)
  118. if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  119. utils.ErrorLog("last_price")
  120. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  121. return
  122. }
  123. last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  124. total := float64(warehousing_count) * last_price
  125. //if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  126. // utils.ErrorLog("retail_price")
  127. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  128. // return
  129. //}
  130. //retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  131. retail_price_total := float64(warehousing_count) * last_price
  132. var productDates int64
  133. var expiryDates int64
  134. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  135. expiryDates = 0
  136. } else {
  137. if len(items["expiry_date"].(string)) == 0 {
  138. expiryDates = 0
  139. } else {
  140. expiryDate, _ := items["expiry_date"].(string)
  141. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  142. expiryDates = expiry_date.Unix()
  143. }
  144. }
  145. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  146. productDates = 0
  147. } else {
  148. if len(items["product_date"].(string)) == 0 {
  149. productDates = 0
  150. } else {
  151. productDate, _ := items["product_date"].(string)
  152. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  153. productDates = product_date.Unix()
  154. }
  155. }
  156. if items["lot_number"] == nil || reflect.TypeOf(items["lot_number"]).String() != "string" {
  157. utils.ErrorLog("lot_number")
  158. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  159. return
  160. }
  161. lot_number, _ := items["lot_number"].(string)
  162. if items["batch_number"] == nil || reflect.TypeOf(items["batch_number"]).String() != "string" {
  163. utils.ErrorLog("batch_number")
  164. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  165. return
  166. }
  167. batch_number, _ := items["batch_number"].(string)
  168. if items["packing_unit"] == nil || reflect.TypeOf(items["packing_unit"]).String() != "string" {
  169. utils.ErrorLog("packing_unit")
  170. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  171. return
  172. }
  173. packing_unit, _ := items["packing_unit"].(string)
  174. var remark string
  175. if items["remark"] == nil || reflect.TypeOf(items["remark"]).String() != "string" {
  176. remark = ""
  177. } else {
  178. remark = items["remark"].(string)
  179. }
  180. warehouseInfo := &models.DrugWarehouseInfo{
  181. WarehousingOrder: warehousing.WarehousingOrder,
  182. WarehousingId: warehousing.ID,
  183. DrugId: drug_id,
  184. Number: lot_number,
  185. ProductDate: productDates,
  186. ExpiryDate: expiryDates,
  187. WarehousingCount: warehousing_count,
  188. Price: last_price,
  189. TotalPrice: total,
  190. Status: 1,
  191. Ctime: ctime,
  192. Remark: remark,
  193. OrgId: adminUserInfo.CurrentOrgId,
  194. Type: types,
  195. Manufacturer: manufacturer_id,
  196. Dealer: dealer_id,
  197. //RetailPrice: retail_price,
  198. StockMaxNumber: warehousing_count,
  199. RetailTotalPrice: retail_price_total,
  200. BatchNumber: batch_number,
  201. MaxUnit: packing_unit,
  202. }
  203. warehousingInfo = append(warehousingInfo, warehouseInfo)
  204. }
  205. }
  206. }
  207. errs := service.CreateDrugWarehousingInfo(warehousingInfo)
  208. info, _ := service.FindLastDrugWarehousingInfo(warehousing.WarehousingOrder)
  209. if errs != nil {
  210. utils.ErrorLog(errs.Error())
  211. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockInFail)
  212. return
  213. }
  214. c.ServeSuccessJSON(map[string]interface{}{
  215. "msg": "入库成功",
  216. "warehousing_order": warehousing_order,
  217. "info": info,
  218. })
  219. //}
  220. //else {
  221. // warehouse, err1 := service.FindWareHouseByOrderNumber(warehousing_order)
  222. // if (err1 != nil) {
  223. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  224. // return
  225. // }
  226. //
  227. // dataBody := make(map[string]interface{}, 0)
  228. // err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  229. // if err != nil {
  230. // utils.ErrorLog(err.Error())
  231. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  232. // return
  233. // }
  234. //
  235. // var warehousingInfo []*models.WarehousingInfo
  236. //
  237. // if dataBody["stockIn"] != nil && reflect.TypeOf(dataBody["stockIn"]).String() == "[]interface {}" {
  238. // thisStockIn, _ := dataBody["stockIn"].([]interface{})
  239. // if len(thisStockIn) > 0 {
  240. // for _, item := range thisStockIn {
  241. // items := item.(map[string]interface{})
  242. //
  243. // if items["good_id"] == nil || reflect.TypeOf(items["good_id"]).String() != "float64" {
  244. // utils.ErrorLog("good_id")
  245. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  246. // return
  247. // }
  248. // good_id := int64(items["good_id"].(float64))
  249. //
  250. // if items["good_type_id"] == nil || reflect.TypeOf(items["good_type_id"]).String() != "float64" {
  251. // utils.ErrorLog("good_type_id")
  252. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  253. // return
  254. // }
  255. // good_type_id := int64(items["good_type_id"].(float64))
  256. //
  257. // if items["warehousing_count"] == nil || reflect.TypeOf(items["warehousing_count"]).String() != "string" {
  258. // utils.ErrorLog("warehousing_count")
  259. // c.ServeFailJSONWithSGJErrorCod Fe(enums.ErrorCodeParamWrong)
  260. // return
  261. // }
  262. //
  263. // warehousing_count, _ := strconv.ParseInt(items["warehousing_count"].(string), 10, 64)
  264. //
  265. // if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  266. // utils.ErrorLog("price")
  267. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  268. // return
  269. // }
  270. // price, _ := strconv.ParseFloat(items["price"].(string), 64)
  271. // total := float64(warehousing_count) * price
  272. //
  273. // if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  274. // utils.ErrorLog("expiry_date")
  275. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  276. // return
  277. // }
  278. //
  279. // expiryDate, _ := items["expiry_date"].(string)
  280. // if len(expiryDate) == 0 {
  281. // utils.ErrorLog("len(expiry_date) == 0")
  282. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  283. //
  284. // return
  285. // }
  286. //
  287. // expiry_date, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  288. // if parseDateErr != nil {
  289. // c.ErrorLog("日期(%v)解析错误:%v", expiry_date, parseDateErr)
  290. // }
  291. //
  292. // var productDates int64
  293. //
  294. // if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  295. // productDates = 0;
  296. // } else {
  297. // productDate, _ := items["product_date"].(string)
  298. // product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  299. // productDates = product_date.Unix()
  300. //
  301. // }
  302. //
  303. // if items["number"] == nil || reflect.TypeOf(items["number"]).String() != "string" {
  304. // utils.ErrorLog("number")
  305. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  306. // return
  307. // }
  308. // number, _ := items["number"].(string)
  309. //
  310. // var remark string
  311. // if items["number"] == nil || reflect.TypeOf(items["number"]).String() != "string" {
  312. // remark = ""
  313. // } else {
  314. // remark = items["remark"].(string)
  315. // }
  316. //
  317. // warehouseInfo := &models.WarehousingInfo{
  318. // WarehousingOrder: warehousing_order,
  319. // WarehousingId: warehouse.ID,
  320. // GoodId: good_id,
  321. // Number: number,
  322. // GoodTypeId: good_type_id,
  323. // ProductDate: productDates,
  324. // ExpiryDate: expiry_date.Unix(),
  325. // WarehousingCount: warehousing_count,
  326. // Price: price,
  327. // TotalPrice: total,
  328. // Status: 1,
  329. // Ctime: ctime,
  330. // Remark: remark,
  331. // OrgId: adminUserInfo.CurrentOrgId,
  332. // }
  333. // warehousingInfo = append(warehousingInfo, warehouseInfo)
  334. //
  335. // }
  336. // }
  337. // }
  338. // errs := service.CreateWarehousingInfo(warehousingInfo)
  339. // info, _ := service.FindLastWarehousingInfo(warehousing_order)
  340. //
  341. // if (errs != nil) {
  342. // utils.ErrorLog(errs.Error())
  343. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockInFail)
  344. // return
  345. // }
  346. //
  347. // c.ServeSuccessJSON(map[string]interface{}{
  348. // "msg": "入库成功",
  349. // "info": info,
  350. // })
  351. //
  352. //}
  353. }
  354. func (c *StockDrugApiController) GetDrugWarehouseList() {
  355. page, _ := c.GetInt64("page", -1)
  356. limit, _ := c.GetInt64("limit", -1)
  357. start_time := c.GetString("start_time")
  358. end_time := c.GetString("end_time")
  359. types, _ := c.GetInt64("type", 0)
  360. keywords := c.GetString("keywords")
  361. fmt.Println(start_time)
  362. fmt.Println(end_time)
  363. timeLayout := "2006-01-02"
  364. loc, _ := time.LoadLocation("Local")
  365. var startTime int64
  366. if len(start_time) > 0 {
  367. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  368. if err != nil {
  369. fmt.Println(err)
  370. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  371. return
  372. }
  373. startTime = theTime.Unix()
  374. }
  375. var endTime int64
  376. if len(end_time) > 0 {
  377. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  378. if err != nil {
  379. utils.ErrorLog(err.Error())
  380. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  381. return
  382. }
  383. endTime = theTime.Unix()
  384. }
  385. adminUserInfo := c.GetAdminUserInfo()
  386. warehouseList, total, err := service.FindAllDrugWarehousingList(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords)
  387. if err == nil {
  388. c.ServeSuccessJSON(map[string]interface{}{
  389. "list": warehouseList,
  390. "total": total,
  391. })
  392. } else {
  393. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  394. }
  395. }
  396. func (c *StockDrugApiController) GetDrugWarehouseInfoList() {
  397. id, _ := c.GetInt64("id", 0)
  398. if id <= 0 {
  399. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  400. return
  401. }
  402. admin := c.GetAdminUserInfo()
  403. warehousing, err := service.FindDrugWarehousingById(id, admin.CurrentOrgId)
  404. manufacturerList, _ := service.GetAllManufacturerList(admin.CurrentOrgId)
  405. if err != nil {
  406. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  407. return
  408. }
  409. if warehousing.ID <= 0 {
  410. return
  411. }
  412. warehousingInfo, err := service.FindDrugWarehousingInfoById(id, admin.CurrentOrgId)
  413. if err == nil {
  414. c.ServeSuccessJSON(map[string]interface{}{
  415. "info": warehousingInfo,
  416. "warehousing": warehousing,
  417. "manufacturerList": manufacturerList,
  418. })
  419. } else {
  420. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  421. }
  422. }
  423. func (c *StockDrugApiController) EditDrugWarehouse() {
  424. warehousing_time := c.GetString("warehousing_time")
  425. id, _ := c.GetInt64("id", 0)
  426. types, _ := c.GetInt64("type", 0)
  427. //manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  428. //dealer_id, _ := c.GetInt64("dealer_id", 0)
  429. if id == 0 {
  430. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  431. return
  432. }
  433. ctime := time.Now().Unix()
  434. mtime := time.Now().Unix()
  435. adminUserInfo := c.GetAdminUserInfo()
  436. warehousingDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_time)
  437. if parseDateErr != nil {
  438. c.ErrorLog("日期(%v)解析错误:%v", warehousingDate, parseDateErr)
  439. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  440. return
  441. }
  442. warehouse, _ := service.FindDrugWarehousingById(id, adminUserInfo.CurrentOrgId)
  443. warehousing := models.DrugWarehouse{
  444. WarehousingOrder: warehouse.WarehousingOrder,
  445. OrgId: adminUserInfo.CurrentOrgId,
  446. Modifier: adminUserInfo.AdminUser.Id,
  447. Mtime: mtime,
  448. Status: 1,
  449. WarehousingTime: warehousingDate.Unix(),
  450. Type: warehouse.Type,
  451. //Dealer: dealer_id,
  452. //Manufacturer: manufacturer_id,
  453. }
  454. service.EditDrugWarehousingOne(warehousing, id)
  455. dataBody := make(map[string]interface{}, 0)
  456. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  457. if err != nil {
  458. utils.ErrorLog(err.Error())
  459. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  460. return
  461. }
  462. var warehousingInfo []*models.DrugWarehouseInfo
  463. var upDateWarehousingInfo []*models.DrugWarehouseInfo
  464. if dataBody["stockIn"] != nil && reflect.TypeOf(dataBody["stockIn"]).String() == "[]interface {}" {
  465. thisStockIn, _ := dataBody["stockIn"].([]interface{})
  466. if len(thisStockIn) > 0 {
  467. for _, item := range thisStockIn {
  468. items := item.(map[string]interface{})
  469. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  470. utils.ErrorLog("drug_id")
  471. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  472. return
  473. }
  474. drug_id := int64(items["drug_id"].(float64))
  475. if items["warehousing_count"] == nil || reflect.TypeOf(items["warehousing_count"]).String() != "string" {
  476. utils.ErrorLog("warehousing_count")
  477. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  478. return
  479. }
  480. warehousing_count, _ := strconv.ParseInt(items["warehousing_count"].(string), 10, 64)
  481. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  482. utils.ErrorLog("price")
  483. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  484. return
  485. }
  486. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  487. total := float64(warehousing_count) * price
  488. //if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  489. // utils.ErrorLog("retail_price")
  490. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  491. // return
  492. //}
  493. //retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  494. //retail_price_total := float64(warehousing_count) * last_price
  495. if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  496. utils.ErrorLog("last_price")
  497. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  498. return
  499. }
  500. last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  501. retail_price_total := float64(warehousing_count) * last_price
  502. var productDates int64
  503. var expiryDates int64
  504. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  505. expiryDates = 0
  506. } else {
  507. if len(items["expiry_date"].(string)) == 0 {
  508. expiryDates = 0
  509. } else {
  510. expiryDate, _ := items["expiry_date"].(string)
  511. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  512. expiryDates = expiry_date.Unix()
  513. }
  514. }
  515. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  516. productDates = 0
  517. } else {
  518. if len(items["product_date"].(string)) == 0 {
  519. productDates = 0
  520. } else {
  521. productDate, _ := items["product_date"].(string)
  522. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  523. productDates = product_date.Unix()
  524. }
  525. }
  526. if items["lot_number"] == nil || reflect.TypeOf(items["lot_number"]).String() != "string" {
  527. utils.ErrorLog("lot_number")
  528. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  529. return
  530. }
  531. number, _ := items["lot_number"].(string)
  532. if items["batch_number"] == nil || reflect.TypeOf(items["batch_number"]).String() != "string" {
  533. utils.ErrorLog("batch_number")
  534. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  535. return
  536. }
  537. batch_number, _ := items["batch_number"].(string)
  538. var remark string
  539. if items["remark"] == nil || reflect.TypeOf(items["remark"]).String() != "string" {
  540. remark = ""
  541. } else {
  542. remark = items["remark"].(string)
  543. }
  544. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  545. utils.ErrorLog("id")
  546. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  547. return
  548. }
  549. id := int64(items["id"].(float64))
  550. //if items["dealer"] == nil || reflect.TypeOf(items["dealer"]).String() != "float64" {
  551. // utils.ErrorLog("dealer")
  552. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  553. // return
  554. //}
  555. //dealer := int64(items["dealer"].(float64))
  556. //
  557. if items["manufacturer"] == nil || reflect.TypeOf(items["manufacturer"]).String() != "float64" {
  558. utils.ErrorLog("manufacturer")
  559. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  560. return
  561. }
  562. manufacturer := int64(items["manufacturer"].(float64))
  563. if id == 0 {
  564. warehouseInfo := &models.DrugWarehouseInfo{
  565. WarehousingOrder: warehouse.WarehousingOrder,
  566. WarehousingId: warehouse.ID,
  567. DrugId: drug_id,
  568. Number: number,
  569. ProductDate: productDates,
  570. ExpiryDate: expiryDates,
  571. WarehousingCount: warehousing_count,
  572. Price: price,
  573. TotalPrice: total,
  574. Status: 1,
  575. Ctime: ctime,
  576. Remark: remark,
  577. OrgId: adminUserInfo.CurrentOrgId,
  578. Type: types,
  579. Manufacturer: manufacturer,
  580. //Dealer: dealer,
  581. RetailPrice: last_price,
  582. RetailTotalPrice: retail_price_total,
  583. BatchNumber: batch_number,
  584. }
  585. warehousingInfo = append(warehousingInfo, warehouseInfo)
  586. } else {
  587. warehouseInfo := &models.DrugWarehouseInfo{
  588. ID: id,
  589. WarehousingOrder: warehouse.WarehousingOrder,
  590. WarehousingId: warehouse.ID,
  591. DrugId: drug_id,
  592. Number: number,
  593. ProductDate: productDates,
  594. ExpiryDate: expiryDates,
  595. WarehousingCount: warehousing_count,
  596. Price: price,
  597. TotalPrice: total,
  598. Status: 1,
  599. Ctime: ctime,
  600. Remark: remark,
  601. OrgId: adminUserInfo.CurrentOrgId,
  602. Type: types,
  603. Manufacturer: manufacturer,
  604. //Dealer: dealer,
  605. RetailPrice: last_price,
  606. RetailTotalPrice: retail_price_total,
  607. StockMaxNumber: warehousing_count,
  608. }
  609. upDateWarehousingInfo = append(upDateWarehousingInfo, warehouseInfo)
  610. }
  611. }
  612. }
  613. }
  614. var errs error
  615. if len(warehousingInfo) > 0 {
  616. errs = service.CreateDrugWarehousingInfo(warehousingInfo)
  617. }
  618. if len(upDateWarehousingInfo) > 0 {
  619. for _, item := range upDateWarehousingInfo {
  620. errs = service.UpDateDrugWarehousingInfo(item)
  621. }
  622. }
  623. if errs != nil {
  624. utils.ErrorLog(errs.Error())
  625. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockInFail)
  626. return
  627. }
  628. c.ServeSuccessJSON(map[string]interface{}{
  629. "msg": "编辑成功",
  630. })
  631. }
  632. func (c *StockDrugApiController) DeleteDrugWarehouse() {
  633. ids := c.GetString("ids")
  634. if len(ids) == 0 {
  635. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  636. return
  637. }
  638. idArray := strings.Split(ids, ",")
  639. err := service.DeleteDrugWarehouse(idArray)
  640. if err != nil {
  641. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  642. } else {
  643. c.ServeSuccessJSON(map[string]interface{}{
  644. "msg": "删除成功",
  645. })
  646. }
  647. }
  648. func (c *StockDrugApiController) DeleteDrugWarehouseInfo() {
  649. id, _ := c.GetInt64("id", 0)
  650. if id == 0 {
  651. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  652. return
  653. }
  654. err := service.UpDateDrugWarehouseStatus(id)
  655. if err != nil {
  656. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  657. } else {
  658. c.ServeSuccessJSON(map[string]interface{}{
  659. "msg": "删除成功",
  660. })
  661. }
  662. }
  663. func (c *StockDrugApiController) CreateDrugSalesReturn() {
  664. dealer_id, _ := c.GetInt64("dealer_id", 0)
  665. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  666. sales_return_time := c.GetString("time")
  667. types, _ := c.GetInt64("type", 0)
  668. salesReturnDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", sales_return_time)
  669. if parseDateErr != nil {
  670. c.ErrorLog("日期(%v)解析错误:%v", salesReturnDate, parseDateErr)
  671. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  672. return
  673. }
  674. adminUserInfo := c.GetAdminUserInfo()
  675. operation_time := time.Now().Unix()
  676. creater := adminUserInfo.AdminUser.Id
  677. ctime := time.Now().Unix()
  678. timeStr := time.Now().Format("2006-01-02")
  679. timeArr := strings.Split(timeStr, "-")
  680. total, _ := service.FindAllSalesReturnTotal(adminUserInfo.CurrentOrgId)
  681. total = total + 1
  682. orderNumber := "YPTHD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  683. salesReturn := models.DrugSalesReturn{
  684. OrderNumber: orderNumber,
  685. OperaTime: operation_time,
  686. OrgId: adminUserInfo.CurrentOrgId,
  687. Creater: creater,
  688. Ctime: ctime,
  689. Status: 1,
  690. ReturnTime: salesReturnDate.Unix(),
  691. Dealer: dealer_id,
  692. Manufacturer: manufacturer_id,
  693. Type: types,
  694. }
  695. service.AddSigleDrugSalesReturn(&salesReturn)
  696. dataBody := make(map[string]interface{}, 0)
  697. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  698. if err != nil {
  699. utils.ErrorLog(err.Error())
  700. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  701. return
  702. }
  703. var salesReturnInfos []*models.DrugSalesReturnInfo
  704. if dataBody["salesReturn"] != nil && reflect.TypeOf(dataBody["salesReturn"]).String() == "[]interface {}" {
  705. thisStockIn, _ := dataBody["salesReturn"].([]interface{})
  706. if len(thisStockIn) > 0 {
  707. for _, item := range thisStockIn {
  708. items := item.(map[string]interface{})
  709. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  710. utils.ErrorLog("drug_id")
  711. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  712. return
  713. }
  714. drug_id := int64(items["drug_id"].(float64))
  715. return_count, _ := items["return_count"].(string)
  716. if len(return_count) == 0 {
  717. utils.ErrorLog("len(return_count) == 0")
  718. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  719. return
  720. }
  721. count, _ := strconv.ParseInt(return_count, 10, 64)
  722. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  723. utils.ErrorLog("price")
  724. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  725. return
  726. }
  727. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  728. total := float64(count) * price
  729. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  730. utils.ErrorLog("retail_price")
  731. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  732. return
  733. }
  734. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  735. retail_price_total := float64(count) * retail_price
  736. salesReturnInfo := &models.DrugSalesReturnInfo{
  737. OrderNumber: salesReturn.OrderNumber,
  738. SalesReturnId: salesReturn.ID,
  739. DrugId: drug_id,
  740. Count: count,
  741. Price: price,
  742. Total: total,
  743. Status: 1,
  744. Ctime: ctime,
  745. OrgId: adminUserInfo.CurrentOrgId,
  746. Type: types,
  747. Manufacturer: manufacturer_id,
  748. Dealer: dealer_id,
  749. RetailPrice: retail_price,
  750. RetailTotalPrice: retail_price_total,
  751. }
  752. salesReturnInfos = append(salesReturnInfos, salesReturnInfo)
  753. }
  754. }
  755. }
  756. errs := service.CreateDrugSalesReturnInfo(salesReturnInfos)
  757. if errs != nil {
  758. utils.ErrorLog(errs.Error())
  759. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateReturnFail)
  760. return
  761. }
  762. c.ServeSuccessJSON(map[string]interface{}{
  763. "msg": "退货成功",
  764. })
  765. }
  766. func (c *StockDrugApiController) GetDrugSalesReturnList() {
  767. page, _ := c.GetInt64("page", -1)
  768. limit, _ := c.GetInt64("limit", -1)
  769. start_time := c.GetString("start_time")
  770. end_time := c.GetString("end_time")
  771. types, _ := c.GetInt64("type", 0)
  772. keywords := c.GetString("keywords")
  773. timeLayout := "2006-01-02"
  774. loc, _ := time.LoadLocation("Local")
  775. var startTime int64
  776. if len(start_time) > 0 {
  777. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  778. if err != nil {
  779. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  780. return
  781. }
  782. startTime = theTime.Unix()
  783. }
  784. var endTime int64
  785. if len(end_time) > 0 {
  786. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  787. if err != nil {
  788. utils.ErrorLog(err.Error())
  789. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  790. return
  791. }
  792. endTime = theTime.Unix()
  793. }
  794. adminUserInfo := c.GetAdminUserInfo()
  795. returnList, total, err := service.FindAllDrugReturnList(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords)
  796. if err == nil {
  797. c.ServeSuccessJSON(map[string]interface{}{
  798. "list": returnList,
  799. "total": total,
  800. })
  801. } else {
  802. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  803. }
  804. }
  805. func (this *StockDrugApiController) DeleteDrugSalesReturn() {
  806. ids := this.GetString("ids")
  807. if len(ids) == 0 {
  808. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  809. return
  810. }
  811. idArray := strings.Split(ids, ",")
  812. err := service.DeleteDrugSalesReturn(idArray)
  813. if err != nil {
  814. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  815. } else {
  816. this.ServeSuccessJSON(map[string]interface{}{
  817. "msg": "删除成功",
  818. })
  819. }
  820. }
  821. func (this *StockDrugApiController) DeleteDrugSalesReturnInfo() {
  822. id, _ := this.GetInt64("id", 0)
  823. fmt.Println(id)
  824. if id == 0 {
  825. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  826. return
  827. }
  828. err := service.UpDateDrugSaleReturnStatus(id)
  829. if err != nil {
  830. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  831. } else {
  832. this.ServeSuccessJSON(map[string]interface{}{
  833. "msg": "删除成功",
  834. })
  835. }
  836. }
  837. func (this *StockDrugApiController) GetDrugSalesReturnInfoList() {
  838. id, _ := this.GetInt64("id", 0)
  839. adminUserInfo := this.GetAdminUserInfo()
  840. list, _ := service.FindAllDrugSalesReturnInfoById(id, adminUserInfo.CurrentOrgId)
  841. salesReturn, _ := service.FindAllDrugSalesReturnById(id, adminUserInfo.CurrentOrgId)
  842. this.ServeSuccessJSON(map[string]interface{}{
  843. "list": list,
  844. "salesReturn": salesReturn,
  845. })
  846. }
  847. func (c *StockDrugApiController) EditDrugReturnInfo() {
  848. return_time := c.GetString("return_time")
  849. id, _ := c.GetInt64("id", 0)
  850. types, _ := c.GetInt64("type", 0)
  851. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  852. dealer_id, _ := c.GetInt64("dealer_id", 0)
  853. if id == 0 {
  854. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  855. return
  856. }
  857. ctime := time.Now().Unix()
  858. mtime := time.Now().Unix()
  859. adminUserInfo := c.GetAdminUserInfo()
  860. returnDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", return_time)
  861. if parseDateErr != nil {
  862. c.ErrorLog("日期(%v)解析错误:%v", returnDate, parseDateErr)
  863. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  864. return
  865. }
  866. salesReturns, _ := service.FindDrugSalesReturnById(id)
  867. sales := models.DrugSalesReturn{
  868. ID: salesReturns.ID,
  869. Mtime: mtime,
  870. ReturnTime: returnDate.Unix(),
  871. Manufacturer: manufacturer_id,
  872. Dealer: dealer_id,
  873. }
  874. service.EditDrugSaleReturn(sales)
  875. dataBody := make(map[string]interface{}, 0)
  876. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  877. if err != nil {
  878. utils.ErrorLog(err.Error())
  879. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  880. return
  881. }
  882. var returnInfos []*models.DrugSalesReturnInfo
  883. var upDateReturnInfos []*models.DrugSalesReturnInfo
  884. if dataBody["salesReturn"] != nil && reflect.TypeOf(dataBody["salesReturn"]).String() == "[]interface {}" {
  885. thisStockIn, _ := dataBody["salesReturn"].([]interface{})
  886. if len(thisStockIn) > 0 {
  887. for _, item := range thisStockIn {
  888. items := item.(map[string]interface{})
  889. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  890. utils.ErrorLog("drug_id")
  891. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  892. return
  893. }
  894. drug_id := int64(items["drug_id"].(float64))
  895. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  896. utils.ErrorLog("count")
  897. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  898. return
  899. }
  900. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  901. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  902. utils.ErrorLog("id")
  903. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  904. return
  905. }
  906. id := int64(items["id"].(float64))
  907. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  908. utils.ErrorLog("price")
  909. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  910. return
  911. }
  912. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  913. total := float64(count) * price
  914. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  915. utils.ErrorLog("retail_price")
  916. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  917. return
  918. }
  919. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  920. retail_price_total := float64(count) * retail_price
  921. if id == 0 {
  922. returnInfo := &models.DrugSalesReturnInfo{
  923. DrugId: drug_id,
  924. Count: count,
  925. Status: 1,
  926. Ctime: ctime,
  927. OrgId: adminUserInfo.CurrentOrgId,
  928. OrderNumber: sales.OrderNumber,
  929. SalesReturnId: sales.ID,
  930. Mtime: time.Now().Unix(),
  931. Type: types,
  932. Manufacturer: manufacturer_id,
  933. Dealer: dealer_id,
  934. RetailPrice: retail_price,
  935. RetailTotalPrice: retail_price_total,
  936. Total: total,
  937. Price: price,
  938. }
  939. returnInfos = append(returnInfos, returnInfo)
  940. } else {
  941. returnInfo := &models.DrugSalesReturnInfo{
  942. ID: id,
  943. DrugId: drug_id,
  944. Count: count,
  945. Status: 1,
  946. Ctime: ctime,
  947. OrgId: adminUserInfo.CurrentOrgId,
  948. OrderNumber: sales.OrderNumber,
  949. SalesReturnId: sales.ID,
  950. Mtime: time.Now().Unix(),
  951. Type: types,
  952. Manufacturer: manufacturer_id,
  953. Dealer: dealer_id,
  954. RetailPrice: retail_price,
  955. RetailTotalPrice: retail_price_total,
  956. Total: total,
  957. Price: price,
  958. }
  959. upDateReturnInfos = append(upDateReturnInfos, returnInfo)
  960. }
  961. }
  962. }
  963. }
  964. var errs error
  965. if len(returnInfos) > 0 {
  966. errs = service.CreateDrugSalesReturnInfo(returnInfos)
  967. }
  968. if len(upDateReturnInfos) > 0 {
  969. for _, item := range upDateReturnInfos {
  970. errs = service.UpDateDrugSalesReturnInfo(item)
  971. }
  972. }
  973. if errs != nil {
  974. utils.ErrorLog(errs.Error())
  975. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateReturnFail)
  976. return
  977. }
  978. c.ServeSuccessJSON(map[string]interface{}{
  979. "msg": "编辑成功",
  980. })
  981. }
  982. func (c *StockDrugApiController) CreateDrugWarehouseOut() {
  983. dealer_id, _ := c.GetInt64("dealer_id", 0)
  984. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  985. types, _ := c.GetInt64("type", 0)
  986. ctime := time.Now().Unix()
  987. adminUserInfo := c.GetAdminUserInfo()
  988. warehousing_out_time := c.GetString("warehousing_out_time")
  989. warehousingOutDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_out_time)
  990. if parseDateErr != nil {
  991. c.ErrorLog("日期(%v)解析错误:%v", warehousingOutDate, parseDateErr)
  992. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  993. return
  994. }
  995. timeStr := time.Now().Format("2006-01-02")
  996. timeArr := strings.Split(timeStr, "-")
  997. total, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
  998. total = total + 1
  999. warehousing_out_order := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  1000. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  1001. number = number + total
  1002. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  1003. operation_time := time.Now().Unix()
  1004. creater := adminUserInfo.AdminUser.Id
  1005. warehouseOut := models.DrugWarehouseOut{
  1006. WarehouseOutOrderNumber: warehousing_out_order,
  1007. OperationTime: operation_time,
  1008. OrgId: adminUserInfo.CurrentOrgId,
  1009. Creater: creater,
  1010. Ctime: ctime,
  1011. Status: 1,
  1012. WarehouseOutTime: warehousingOutDate.Unix(),
  1013. Dealer: dealer_id,
  1014. Manufacturer: manufacturer_id,
  1015. Type: types,
  1016. }
  1017. service.AddSigleDrugWarehouseOut(&warehouseOut)
  1018. dataBody := make(map[string]interface{}, 0)
  1019. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1020. if err != nil {
  1021. utils.ErrorLog(err.Error())
  1022. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1023. return
  1024. }
  1025. var warehousingOutInfo []*models.DrugWarehouseOutInfo
  1026. if dataBody["stockOut"] != nil && reflect.TypeOf(dataBody["stockOut"]).String() == "[]interface {}" {
  1027. thisStockIn, _ := dataBody["stockOut"].([]interface{})
  1028. if len(thisStockIn) > 0 {
  1029. for _, item := range thisStockIn {
  1030. items := item.(map[string]interface{})
  1031. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1032. utils.ErrorLog("drug_id")
  1033. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1034. return
  1035. }
  1036. drug_id := int64(items["drug_id"].(float64))
  1037. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1038. utils.ErrorLog("count")
  1039. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1040. return
  1041. }
  1042. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1043. utils.ErrorLog("price")
  1044. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1045. return
  1046. }
  1047. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1048. if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  1049. utils.ErrorLog("last_price")
  1050. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1051. return
  1052. }
  1053. last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  1054. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1055. total := float64(count) * price
  1056. retail_price_total := float64(count) * last_price
  1057. remark := items["remark"].(string)
  1058. packing_unit := items["packing_unit"].(string)
  1059. fmt.Println("包装单位999999999999", packing_unit)
  1060. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1061. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1062. WarehouseOutId: warehouseOut.ID,
  1063. DrugId: drug_id,
  1064. Count: count,
  1065. Price: price,
  1066. TotalPrice: total,
  1067. Status: 1,
  1068. Ctime: ctime,
  1069. Remark: remark,
  1070. OrgId: adminUserInfo.CurrentOrgId,
  1071. Type: types,
  1072. Manufacturer: manufacturer_id,
  1073. Dealer: dealer_id,
  1074. RetailPrice: last_price,
  1075. RetailTotalPrice: retail_price_total,
  1076. CountUnit: packing_unit,
  1077. }
  1078. warehousingOutInfo = append(warehousingOutInfo, warehouseOutInfo)
  1079. }
  1080. }
  1081. }
  1082. //调用出库逻辑
  1083. for _, item := range warehousingOutInfo {
  1084. // 出库流程
  1085. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  1086. drup, _ := service.FindBaseDrugLibRecord(item.OrgId, item.DrugId)
  1087. if drup.ID > 0 {
  1088. prescribingNumber := item.Count
  1089. service.AutoDrugDeliverInfo(item.OrgId, prescribingNumber, &warehouseOut, &drup, item)
  1090. }
  1091. }
  1092. //errs := service.CreateDrugWarehousingOutInfo(warehousingOutInfo)
  1093. //info, _ := service.FindLastDrugWarehousingOutInfo(warehouseOut.WarehouseOutOrderNumber)
  1094. //if errs != nil {
  1095. // utils.ErrorLog(errs.Error())
  1096. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1097. // return
  1098. //}
  1099. c.ServeSuccessJSON(map[string]interface{}{
  1100. "msg": "出库成功",
  1101. "warehousing_out_order": warehousing_out_order,
  1102. })
  1103. }
  1104. func (c *StockDrugApiController) GetDrugWarehouseOutList() {
  1105. page, _ := c.GetInt64("page", -1)
  1106. limit, _ := c.GetInt64("limit", -1)
  1107. start_time := c.GetString("start_time")
  1108. end_time := c.GetString("end_time")
  1109. types, _ := c.GetInt64("type", 0)
  1110. keywords := c.GetString("keywords")
  1111. timeLayout := "2006-01-02"
  1112. loc, _ := time.LoadLocation("Local")
  1113. var startTime int64
  1114. if len(start_time) > 0 {
  1115. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  1116. if err != nil {
  1117. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1118. return
  1119. }
  1120. startTime = theTime.Unix()
  1121. }
  1122. var endTime int64
  1123. if len(end_time) > 0 {
  1124. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  1125. if err != nil {
  1126. utils.ErrorLog(err.Error())
  1127. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1128. return
  1129. }
  1130. endTime = theTime.Unix()
  1131. }
  1132. adminUserInfo := c.GetAdminUserInfo()
  1133. warehouseOutList, total, err := service.FindAllDrugWarehouseOutList(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords)
  1134. fmt.Println(err)
  1135. if err == nil {
  1136. c.ServeSuccessJSON(map[string]interface{}{
  1137. "list": warehouseOutList,
  1138. "total": total,
  1139. })
  1140. } else {
  1141. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  1142. }
  1143. }
  1144. func (c *StockDrugApiController) DeleteDrugWarehouseOut() {
  1145. ids := c.GetString("ids")
  1146. if len(ids) == 0 {
  1147. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1148. return
  1149. }
  1150. idArray := strings.Split(ids, ",")
  1151. err := service.DeleteDrugWarehouseOut(idArray)
  1152. if err != nil {
  1153. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1154. } else {
  1155. c.ServeSuccessJSON(map[string]interface{}{
  1156. "msg": "删除成功",
  1157. })
  1158. }
  1159. }
  1160. func (this *StockDrugApiController) DeleteDrugWarehouseOutInfo() {
  1161. id, _ := this.GetInt64("id", 0)
  1162. fmt.Println(id)
  1163. if id == 0 {
  1164. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1165. return
  1166. }
  1167. err := service.UpDateDrugWarehouseOutStatus(id)
  1168. if err != nil {
  1169. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1170. } else {
  1171. this.ServeSuccessJSON(map[string]interface{}{
  1172. "msg": "删除成功",
  1173. })
  1174. }
  1175. }
  1176. func (c *StockDrugApiController) GetDrugWarehouseOutInfoList() {
  1177. id, _ := c.GetInt64("id", 0)
  1178. adminInfo := c.GetAdminUserInfo()
  1179. warehouseOutInfo, _ := service.FindDrugWarehouseOutInfoById(id, adminInfo.CurrentOrgId)
  1180. warehouseOut, _ := service.FindDrugWareHouseOutById(id, adminInfo.CurrentOrgId)
  1181. c.ServeSuccessJSON(map[string]interface{}{
  1182. "list": warehouseOutInfo,
  1183. "info": warehouseOut,
  1184. })
  1185. }
  1186. func (c *StockDrugApiController) EditDrugWarehouseOut() {
  1187. warehouse_out_time := c.GetString("warehouse_out_time")
  1188. id, _ := c.GetInt64("id", 0)
  1189. types, _ := c.GetInt64("type", 0)
  1190. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  1191. dealer_id, _ := c.GetInt64("dealer_id", 0)
  1192. if id == 0 {
  1193. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1194. return
  1195. }
  1196. ctime := time.Now().Unix()
  1197. mtime := time.Now().Unix()
  1198. adminUserInfo := c.GetAdminUserInfo()
  1199. warehouseOutDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehouse_out_time)
  1200. if parseDateErr != nil {
  1201. c.ErrorLog("日期(%v)解析错误:%v", warehouseOutDate, parseDateErr)
  1202. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1203. return
  1204. }
  1205. warehouseOut, _ := service.FindDrugWareHouseOutById(id, adminUserInfo.CurrentOrgId)
  1206. tempWarehouseOut := models.DrugWarehouseOut{
  1207. ID: warehouseOut.ID,
  1208. Mtime: mtime,
  1209. WarehouseOutTime: warehouseOutDate.Unix(),
  1210. Manufacturer: manufacturer_id,
  1211. Dealer: dealer_id,
  1212. }
  1213. service.EditDrugWarehouseOut(tempWarehouseOut)
  1214. dataBody := make(map[string]interface{}, 0)
  1215. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1216. if err != nil {
  1217. utils.ErrorLog(err.Error())
  1218. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1219. return
  1220. }
  1221. var warehousingOutInfo []*models.DrugWarehouseOutInfo
  1222. var upDateWarehouseOutInfos []*models.DrugWarehouseOutInfo
  1223. if dataBody["stockOut"] != nil && reflect.TypeOf(dataBody["stockOut"]).String() == "[]interface {}" {
  1224. thisStockOut, _ := dataBody["stockOut"].([]interface{})
  1225. if len(thisStockOut) > 0 {
  1226. for _, item := range thisStockOut {
  1227. items := item.(map[string]interface{})
  1228. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1229. utils.ErrorLog("drug_id")
  1230. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1231. return
  1232. }
  1233. drug_id := int64(items["drug_id"].(float64))
  1234. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1235. utils.ErrorLog("count")
  1236. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1237. return
  1238. }
  1239. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1240. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1241. utils.ErrorLog("price")
  1242. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1243. return
  1244. }
  1245. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1246. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  1247. utils.ErrorLog("retail_price")
  1248. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1249. return
  1250. }
  1251. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  1252. total := float64(count) * price
  1253. retail_price_total := float64(count) * retail_price
  1254. remark := items["remark"].(string)
  1255. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  1256. utils.ErrorLog("id")
  1257. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1258. return
  1259. }
  1260. id := int64(items["id"].(float64))
  1261. if id == 0 {
  1262. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1263. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1264. WarehouseOutId: warehouseOut.ID,
  1265. DrugId: drug_id,
  1266. Count: count,
  1267. Price: price,
  1268. TotalPrice: total,
  1269. Status: 1,
  1270. Ctime: ctime,
  1271. Remark: remark,
  1272. OrgId: adminUserInfo.CurrentOrgId,
  1273. Type: types,
  1274. Manufacturer: manufacturer_id,
  1275. Dealer: dealer_id,
  1276. IsSys: 0,
  1277. SysRecordTime: 0,
  1278. RetailPrice: retail_price,
  1279. RetailTotalPrice: retail_price_total,
  1280. }
  1281. warehousingOutInfo = append(warehousingOutInfo, warehouseOutInfo)
  1282. } else {
  1283. if items["is_sys"] == nil || reflect.TypeOf(items["is_sys"]).String() != "float64" {
  1284. utils.ErrorLog("is_sys")
  1285. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1286. return
  1287. }
  1288. is_sys := int64(items["is_sys"].(float64))
  1289. if items["sys_record_time"] == nil || reflect.TypeOf(items["sys_record_time"]).String() != "float64" {
  1290. utils.ErrorLog("sys_record_time")
  1291. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1292. return
  1293. }
  1294. sys_record_time := int64(items["sys_record_time"].(float64))
  1295. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1296. ID: id,
  1297. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1298. WarehouseOutId: warehouseOut.ID,
  1299. DrugId: drug_id,
  1300. Count: count,
  1301. Price: price,
  1302. TotalPrice: total,
  1303. Status: 1,
  1304. Ctime: ctime,
  1305. Remark: remark,
  1306. OrgId: adminUserInfo.CurrentOrgId,
  1307. Mtime: time.Now().Unix(),
  1308. Type: types,
  1309. Manufacturer: manufacturer_id,
  1310. Dealer: dealer_id,
  1311. IsSys: is_sys,
  1312. SysRecordTime: sys_record_time,
  1313. RetailPrice: retail_price,
  1314. RetailTotalPrice: retail_price_total,
  1315. }
  1316. upDateWarehouseOutInfos = append(upDateWarehouseOutInfos, warehouseOutInfo)
  1317. }
  1318. }
  1319. }
  1320. }
  1321. var errs error
  1322. if len(warehousingOutInfo) > 0 {
  1323. errs = service.CreateDrugWarehousingOutInfo(warehousingOutInfo)
  1324. }
  1325. if len(upDateWarehouseOutInfos) > 0 {
  1326. for _, item := range upDateWarehouseOutInfos {
  1327. errs = service.UpDateDrugWarehouseOutInfo(item)
  1328. }
  1329. }
  1330. if errs != nil {
  1331. utils.ErrorLog(errs.Error())
  1332. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1333. return
  1334. }
  1335. c.ServeSuccessJSON(map[string]interface{}{
  1336. "msg": "编辑成功",
  1337. })
  1338. }
  1339. func (c *StockDrugApiController) CreateDrugCancelStock() {
  1340. dealer_id, _ := c.GetInt64("dealer_id", 0)
  1341. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  1342. cancel_stock_time := c.GetString("time")
  1343. types, _ := c.GetInt64("type", 0)
  1344. cancelStockDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", cancel_stock_time)
  1345. if parseDateErr != nil {
  1346. c.ErrorLog("日期(%v)解析错误:%v", cancelStockDate, parseDateErr)
  1347. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1348. return
  1349. }
  1350. adminUserInfo := c.GetAdminUserInfo()
  1351. operation_time := time.Now().Unix()
  1352. creater := adminUserInfo.AdminUser.Id
  1353. ctime := time.Now().Unix()
  1354. timeStr := time.Now().Format("2006-01-02")
  1355. timeArr := strings.Split(timeStr, "-")
  1356. total, _ := service.FindAllDrugCancelStockTotal(adminUserInfo.CurrentOrgId)
  1357. total = total + 1
  1358. orderNumber := "CKTKD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1359. cancelStock := models.DrugCancelStock{
  1360. OrderNumber: orderNumber,
  1361. OperaTime: operation_time,
  1362. OrgId: adminUserInfo.CurrentOrgId,
  1363. Creater: creater,
  1364. Ctime: ctime,
  1365. Status: 1,
  1366. ReturnTime: cancelStockDate.Unix(),
  1367. Dealer: dealer_id,
  1368. Manufacturer: manufacturer_id,
  1369. Type: types,
  1370. }
  1371. service.AddSigleDrugCancelStock(&cancelStock)
  1372. dataBody := make(map[string]interface{}, 0)
  1373. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1374. if err != nil {
  1375. utils.ErrorLog(err.Error())
  1376. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1377. return
  1378. }
  1379. var cancelStockInfos []*models.DrugCancelStockInfo
  1380. if dataBody["cancelStock"] != nil && reflect.TypeOf(dataBody["cancelStock"]).String() == "[]interface {}" {
  1381. thisStockIn, _ := dataBody["cancelStock"].([]interface{})
  1382. if len(thisStockIn) > 0 {
  1383. for _, item := range thisStockIn {
  1384. items := item.(map[string]interface{})
  1385. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1386. utils.ErrorLog("drug_id")
  1387. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1388. return
  1389. }
  1390. drug_id := int64(items["drug_id"].(float64))
  1391. return_count, _ := items["return_count"].(string)
  1392. if len(return_count) == 0 {
  1393. utils.ErrorLog("len(return_count) == 0")
  1394. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1395. return
  1396. }
  1397. count, _ := strconv.ParseInt(return_count, 10, 64)
  1398. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1399. utils.ErrorLog("price")
  1400. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1401. return
  1402. }
  1403. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1404. total := float64(count) * price
  1405. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  1406. utils.ErrorLog("retail_price")
  1407. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1408. return
  1409. }
  1410. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  1411. retail_price_total := float64(count) * retail_price
  1412. cancelStockInfo := &models.DrugCancelStockInfo{
  1413. OrderNumber: cancelStock.OrderNumber,
  1414. CancelStockId: cancelStock.ID,
  1415. DrugId: drug_id,
  1416. Count: count,
  1417. Status: 1,
  1418. Ctime: ctime,
  1419. OrgId: adminUserInfo.CurrentOrgId,
  1420. Type: types,
  1421. Manufacturer: manufacturer_id,
  1422. Dealer: dealer_id,
  1423. Total: total,
  1424. RetailPrice: retail_price,
  1425. RetailTotalPrice: retail_price_total,
  1426. Price: price,
  1427. }
  1428. cancelStockInfos = append(cancelStockInfos, cancelStockInfo)
  1429. }
  1430. }
  1431. }
  1432. errs := service.CreateDrugCancelStockInfo(cancelStockInfos)
  1433. if errs != nil {
  1434. utils.ErrorLog(errs.Error())
  1435. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  1436. return
  1437. }
  1438. c.ServeSuccessJSON(map[string]interface{}{
  1439. "msg": "退库成功",
  1440. })
  1441. }
  1442. func (c *StockDrugApiController) GetDrugCancelStockInfoList() {
  1443. id, _ := c.GetInt64("id", 0)
  1444. adminUserInfo := c.GetAdminUserInfo()
  1445. cancelStockInfoList, _ := service.FindDrugCancelStockInfoById(id, adminUserInfo.CurrentOrgId)
  1446. info, _ := service.FindCancelStockById(id, adminUserInfo.CurrentOrgId)
  1447. c.ServeSuccessJSON(map[string]interface{}{
  1448. "list": cancelStockInfoList,
  1449. "info": info,
  1450. })
  1451. }
  1452. func (c *StockDrugApiController) GetDrugCancelStockList() {
  1453. page, _ := c.GetInt64("page", -1)
  1454. limit, _ := c.GetInt64("limit", -1)
  1455. start_time := c.GetString("start_time")
  1456. end_time := c.GetString("end_time")
  1457. types, _ := c.GetInt64("type", 0)
  1458. keywords := c.GetString("keywords")
  1459. timeLayout := "2006-01-02"
  1460. loc, _ := time.LoadLocation("Local")
  1461. var startTime int64
  1462. if len(start_time) > 0 {
  1463. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  1464. if err != nil {
  1465. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1466. return
  1467. }
  1468. startTime = theTime.Unix()
  1469. }
  1470. var endTime int64
  1471. if len(end_time) > 0 {
  1472. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  1473. if err != nil {
  1474. utils.ErrorLog(err.Error())
  1475. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1476. return
  1477. }
  1478. endTime = theTime.Unix()
  1479. }
  1480. adminUserInfo := c.GetAdminUserInfo()
  1481. returnList, total, err := service.FindAllDrugCancelList(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords)
  1482. if err == nil {
  1483. c.ServeSuccessJSON(map[string]interface{}{
  1484. "list": returnList,
  1485. "total": total,
  1486. })
  1487. } else {
  1488. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  1489. }
  1490. }
  1491. func (this *StockDrugApiController) DeleteDrugCancelStock() {
  1492. ids := this.GetString("ids")
  1493. if len(ids) == 0 {
  1494. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1495. return
  1496. }
  1497. idArray := strings.Split(ids, ",")
  1498. err := service.DeleteDrugCancelStock(idArray)
  1499. if err != nil {
  1500. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1501. } else {
  1502. this.ServeSuccessJSON(map[string]interface{}{
  1503. "msg": "删除成功",
  1504. })
  1505. }
  1506. }
  1507. func (this *StockDrugApiController) DeleteDrugCancelStockInfo() {
  1508. id, _ := this.GetInt64("id", 0)
  1509. fmt.Println(id)
  1510. if id == 0 {
  1511. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1512. return
  1513. }
  1514. err := service.UpDateDrugCancleStockStatus(id)
  1515. if err != nil {
  1516. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1517. } else {
  1518. this.ServeSuccessJSON(map[string]interface{}{
  1519. "msg": "删除成功",
  1520. })
  1521. }
  1522. }
  1523. func (c *StockDrugApiController) EditDrugCancelStock() {
  1524. cancel_time := c.GetString("cancel_time")
  1525. id, _ := c.GetInt64("id", 0)
  1526. types, _ := c.GetInt64("type", 0)
  1527. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  1528. dealer_id, _ := c.GetInt64("dealer_id", 0)
  1529. if id == 0 {
  1530. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1531. return
  1532. }
  1533. ctime := time.Now().Unix()
  1534. mtime := time.Now().Unix()
  1535. adminUserInfo := c.GetAdminUserInfo()
  1536. cancelDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", cancel_time)
  1537. if parseDateErr != nil {
  1538. c.ErrorLog("日期(%v)解析错误:%v", cancelDate, parseDateErr)
  1539. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1540. return
  1541. }
  1542. cancelStock, _ := service.FindDrugCancelStockById(id)
  1543. tempCancelStock := models.DrugCancelStock{
  1544. ID: cancelStock.ID,
  1545. Mtime: mtime,
  1546. ReturnTime: cancelDate.Unix(),
  1547. Manufacturer: manufacturer_id,
  1548. Dealer: dealer_id,
  1549. }
  1550. service.EditDrugCancelStock(tempCancelStock)
  1551. dataBody := make(map[string]interface{}, 0)
  1552. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1553. if err != nil {
  1554. utils.ErrorLog(err.Error())
  1555. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1556. return
  1557. }
  1558. var cancelStockInfos []*models.DrugCancelStockInfo
  1559. var upDateCancelStockInfos []*models.DrugCancelStockInfo
  1560. if dataBody["cancelStock"] != nil && reflect.TypeOf(dataBody["cancelStock"]).String() == "[]interface {}" {
  1561. thisCancelStock, _ := dataBody["cancelStock"].([]interface{})
  1562. if len(thisCancelStock) > 0 {
  1563. for _, item := range thisCancelStock {
  1564. items := item.(map[string]interface{})
  1565. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1566. utils.ErrorLog("drug_id")
  1567. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1568. return
  1569. }
  1570. drug_id := int64(items["drug_id"].(float64))
  1571. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1572. utils.ErrorLog("count")
  1573. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1574. return
  1575. }
  1576. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1577. //count, _ := strconv.ParseInt(return_count, 10, 64)
  1578. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1579. utils.ErrorLog("price")
  1580. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1581. return
  1582. }
  1583. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1584. total := float64(count) * price
  1585. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  1586. utils.ErrorLog("retail_price")
  1587. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1588. return
  1589. }
  1590. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  1591. retail_price_total := float64(count) * retail_price
  1592. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  1593. utils.ErrorLog("id")
  1594. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1595. return
  1596. }
  1597. id := int64(items["id"].(float64))
  1598. if id == 0 {
  1599. cancelStockInfo := &models.DrugCancelStockInfo{
  1600. DrugId: drug_id,
  1601. Count: count,
  1602. Status: 1,
  1603. Ctime: ctime,
  1604. OrgId: adminUserInfo.CurrentOrgId,
  1605. OrderNumber: cancelStock.OrderNumber,
  1606. CancelStockId: cancelStock.ID,
  1607. Mtime: time.Now().Unix(),
  1608. Type: types,
  1609. Manufacturer: manufacturer_id,
  1610. Dealer: dealer_id,
  1611. RetailPrice: retail_price,
  1612. RetailTotalPrice: retail_price_total,
  1613. Price: price,
  1614. Total: total,
  1615. }
  1616. cancelStockInfos = append(cancelStockInfos, cancelStockInfo)
  1617. } else {
  1618. cancelStockInfo := &models.DrugCancelStockInfo{
  1619. ID: id,
  1620. DrugId: drug_id,
  1621. Count: count,
  1622. Status: 1,
  1623. Ctime: ctime,
  1624. OrgId: adminUserInfo.CurrentOrgId,
  1625. OrderNumber: cancelStock.OrderNumber,
  1626. CancelStockId: cancelStock.ID,
  1627. Mtime: time.Now().Unix(),
  1628. Type: types,
  1629. Manufacturer: manufacturer_id,
  1630. Dealer: dealer_id,
  1631. RetailPrice: retail_price,
  1632. RetailTotalPrice: retail_price_total,
  1633. Price: price,
  1634. Total: total,
  1635. }
  1636. upDateCancelStockInfos = append(upDateCancelStockInfos, cancelStockInfo)
  1637. }
  1638. }
  1639. }
  1640. }
  1641. var errs error
  1642. if len(cancelStockInfos) > 0 {
  1643. errs = service.CreateDrugCancelStockInfo(cancelStockInfos)
  1644. }
  1645. if len(upDateCancelStockInfos) > 0 {
  1646. for _, item := range upDateCancelStockInfos {
  1647. errs = service.UpDateDrugCancelStockInfo(item)
  1648. }
  1649. }
  1650. if errs != nil {
  1651. utils.ErrorLog(errs.Error())
  1652. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateCancelStockFail)
  1653. return
  1654. }
  1655. c.ServeSuccessJSON(map[string]interface{}{
  1656. "msg": "编辑成功",
  1657. })
  1658. }
  1659. func (c *StockDrugApiController) GetAllConfig() {
  1660. adminUserInfo := c.GetAdminUserInfo()
  1661. manufacturer, _ := service.FindAllManufacturer(adminUserInfo.CurrentOrgId)
  1662. dealer, _ := service.FindAllDealer(adminUserInfo.CurrentOrgId)
  1663. stockIns, _ := service.FindStockInNumber(adminUserInfo.CurrentOrgId)
  1664. drugs, _ := service.GetDrugs(adminUserInfo.CurrentOrgId)
  1665. c.ServeSuccessJSON(map[string]interface{}{
  1666. "manufacturer": manufacturer,
  1667. "dealer": dealer,
  1668. "drugs": drugs,
  1669. "numbers": stockIns,
  1670. })
  1671. }
  1672. func (c *StockDrugApiController) GetAllDrugStock() {
  1673. adminUserInfo := c.GetAdminUserInfo()
  1674. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  1675. dealer_id, _ := c.GetInt64("dealer_id", 0)
  1676. drugs, err := service.FindAllDrugByManufactureId(manufacturer_id, dealer_id, adminUserInfo.CurrentOrgId)
  1677. if err == nil {
  1678. c.ServeSuccessJSON(map[string]interface{}{
  1679. "drugs": drugs,
  1680. })
  1681. } else {
  1682. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  1683. }
  1684. }
  1685. func (c *StockDrugApiController) GetDrugQueryInfo() {
  1686. page, _ := c.GetInt64("page", -1)
  1687. limit, _ := c.GetInt64("limit", -1)
  1688. keyword := c.GetString("keyword")
  1689. drug_category, _ := c.GetInt64("drug_category")
  1690. start_time := c.GetString("start_time")
  1691. end_time := c.GetString("end_time")
  1692. timeLayout := "2006-01-02"
  1693. loc, _ := time.LoadLocation("Local")
  1694. var startTime int64
  1695. if len(start_time) > 0 {
  1696. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  1697. if err != nil {
  1698. fmt.Println(err)
  1699. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1700. return
  1701. }
  1702. startTime = theTime.Unix()
  1703. }
  1704. var endTime int64
  1705. if len(end_time) > 0 {
  1706. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  1707. if err != nil {
  1708. utils.ErrorLog(err.Error())
  1709. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1710. return
  1711. }
  1712. endTime = theTime.Unix()
  1713. }
  1714. adminUserInfo := c.GetAdminUserInfo()
  1715. list, total, err := service.FindAllDrugStockInfo(adminUserInfo.CurrentOrgId, page, limit, keyword, drug_category, startTime, endTime)
  1716. if err == nil {
  1717. c.ServeSuccessJSON(map[string]interface{}{
  1718. "list": list,
  1719. "total": total,
  1720. })
  1721. } else {
  1722. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  1723. }
  1724. }
  1725. func (this *StockDrugApiController) GetUserDetailInfo() {
  1726. order_id, _ := this.GetInt64("id")
  1727. if order_id <= 0 {
  1728. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1729. return
  1730. }
  1731. adminUserInfo := this.GetAdminUserInfo()
  1732. userDetails, err, total := service.FindDrugStockUserDetailById(adminUserInfo.CurrentOrgId, order_id)
  1733. if err == nil {
  1734. this.ServeSuccessJSON(map[string]interface{}{
  1735. "list": userDetails,
  1736. "total": total,
  1737. })
  1738. } else {
  1739. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  1740. return
  1741. }
  1742. }