drug_stock_api_contorller.go 66KB

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