drug_stock_api_contorller.go 61KB

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