drug_stock_api_contorller.go 65KB

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