drug_stock_api_contorller.go 63KB

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