stock_service.go 96KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  11. err := writeDb.Create(&dealer).Error
  12. return err, dealer
  13. }
  14. func ModifyDealer(dealer *models.Dealer) error {
  15. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  16. "dealer_name": dealer.DealerName,
  17. "contact": dealer.Contact,
  18. "contact_phone": dealer.ContactPhone,
  19. "platform_number": dealer.PlatformNumber,
  20. "email": dealer.Email,
  21. "contact_address": dealer.ContactAddress,
  22. "modifier": dealer.Modifier,
  23. "mtime": time.Now().Unix(),
  24. "remark": dealer.Remark,
  25. "wubi": dealer.WuBi,
  26. "pinyin": dealer.PinYin,
  27. }).Error
  28. return err
  29. }
  30. func FindAllDealerTotal(orgId int64) (total int64) {
  31. db := readDb.Model(&models.Dealer{})
  32. db = db.Where("org_id = ? ", orgId).Count(&total)
  33. return
  34. }
  35. func FindAllDealerList(orgId int64, page int64, limit int64) (list []*models.Dealer, total int64, err error) {
  36. offset := (page - 1) * limit
  37. db := readDb.Model(&models.Dealer{})
  38. db = db.Where("org_id = ? AND status = 1", orgId)
  39. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  40. return
  41. }
  42. func DeleteDealerById(id int64) error {
  43. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  44. return err
  45. }
  46. func FindDealerById(id int64) (*models.Dealer, error) {
  47. dealer := &models.Dealer{}
  48. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  49. return dealer, err
  50. }
  51. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  52. err := writeDb.Create(&manufacturer).Error
  53. return err, manufacturer
  54. }
  55. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  56. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  57. "manufacturer_name": manufacturer.ManufacturerName,
  58. "contact": manufacturer.Contact,
  59. "contact_phone": manufacturer.ContactPhone,
  60. "platform_number": manufacturer.PlatformNumber,
  61. "email": manufacturer.Email,
  62. "contact_address": manufacturer.ContactAddress,
  63. "modifier": manufacturer.Modifier,
  64. "mtime": time.Now().Unix(),
  65. "remark": manufacturer.Remark,
  66. "wubi": manufacturer.WuBi,
  67. "pinyin": manufacturer.PinYin,
  68. }).Error
  69. return err
  70. }
  71. func FindAllManufacturerList(orgId int64, page int64, limit int64) (list []*models.Manufacturer, total int64, err error) {
  72. offset := (page - 1) * limit
  73. db := readDb.Model(&models.Manufacturer{})
  74. db = db.Where("org_id = ? AND status = 1", orgId)
  75. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  76. return
  77. }
  78. func DeleteManufacturerById(id int64) error {
  79. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  80. return err
  81. }
  82. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  83. dealer := &models.Manufacturer{}
  84. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  85. return dealer, err
  86. }
  87. func FindManufacturerTotal(orgId int64) (total int64) {
  88. db := readDb.Model(&models.Manufacturer{})
  89. db = db.Where("org_id = ?", orgId).Count(&total)
  90. return
  91. }
  92. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  93. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  94. return dealers, err
  95. }
  96. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  97. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  98. return manufacturer, err
  99. }
  100. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  101. db := readDb.Model(&models.GoodsType{})
  102. db = db.Where("org_id = ?", orgId).Count(&total)
  103. return
  104. }
  105. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  106. err := writeDb.Create(&goodType).Error
  107. return err, goodType
  108. }
  109. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  110. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  111. "modifier": goodType.Modifier,
  112. "mtime": time.Now().Unix(),
  113. "remark": goodType.Remark,
  114. "type_name": goodType.TypeName,
  115. "out_stock": goodType.OutStock,
  116. "stock_attribute": goodType.StockAttribute,
  117. }).Error
  118. return err, goodType
  119. }
  120. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  121. offset := (page - 1) * limit
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id=? AND status = 1", orgId)
  124. if len(keyword) > 0 {
  125. likeKey := "%" + keyword + "%"
  126. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  127. }
  128. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  129. return
  130. }
  131. func DeleteGoodTypeById(id int64, operateId int64) error {
  132. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  133. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  134. return err
  135. }
  136. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  137. goodType := &models.GoodsType{}
  138. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  139. return goodType, err
  140. }
  141. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  142. db := readDb.Model(&models.GoodInfo{})
  143. db = db.Where("org_id = ?", orgId).Count(&total)
  144. return
  145. }
  146. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  147. err := writeDb.Create(&goodInfo).Error
  148. return err, goodInfo
  149. }
  150. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  151. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  152. "good_type_id": goodInfo.GoodTypeId,
  153. "modifier": goodInfo.Modifier,
  154. "mtime": time.Now().Unix(),
  155. "remark": goodInfo.Remark,
  156. "specification_name": goodInfo.SpecificationName,
  157. "good_unit": goodInfo.GoodUnit,
  158. "buy_price": goodInfo.BuyPrice,
  159. "sell_price": goodInfo.SellPrice,
  160. "manufacturer": goodInfo.Manufacturer,
  161. "dealer": goodInfo.Dealer,
  162. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  163. "stock_warn_count": goodInfo.StockWarnCount,
  164. "is_reuse": goodInfo.IsReuse,
  165. "good_name": goodInfo.GoodName,
  166. "pinyin": goodInfo.Pinyin,
  167. "wubi": goodInfo.Wubi,
  168. "good_kind": goodInfo.GoodKind,
  169. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  170. "retail_price": goodInfo.RetailPrice,
  171. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  172. "is_special_diseases": goodInfo.IsSpecialDiseases,
  173. "is_record": goodInfo.IsRecord,
  174. "statistics_category": goodInfo.StatisticsCategory,
  175. "good_status": goodInfo.GoodStatus,
  176. "default_count": goodInfo.DefaultCount,
  177. "sign": goodInfo.Sign,
  178. "is_default": goodInfo.IsDefault,
  179. "is_charge_use": goodInfo.IsChargeUse,
  180. "is_charge_predict": goodInfo.IsChargePredict,
  181. "is_statistics_work": goodInfo.IsStatisticsWork,
  182. "sort": goodInfo.Sort,
  183. "is_doctor_use": goodInfo.IsDoctorUse,
  184. "agent": goodInfo.Agent,
  185. "good_number": goodInfo.GoodNumber,
  186. }).Error
  187. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  188. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  189. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  190. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  191. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  192. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  193. return err, goodInfo
  194. }
  195. func FindGoodInfoList(orgId int64, page int64, limit int64, keyword string, is_use int64, is_charge int64, good_kind int64) (list []*models.GoodInfo, total int64, err error) {
  196. offset := (page - 1) * limit
  197. db := readDb.Model(&models.GoodInfo{})
  198. db = db.Where("org_id = ? AND status = 1", orgId)
  199. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  200. if len(keyword) > 0 {
  201. likeKey := "%" + keyword + "%"
  202. db = db.Where("good_code LIKE ? OR specification_name LIKE ?", likeKey, likeKey)
  203. } else {
  204. if is_use > 0 {
  205. if is_use == 1 {
  206. db = db.Where("find_in_set('停用',good_status) = 0")
  207. } else {
  208. db = db.Where("find_in_set('停用',good_status) > 0")
  209. }
  210. }
  211. if is_charge > 0 {
  212. if is_charge == 1 {
  213. db = db.Where("find_in_set('收费',good_status) > 0")
  214. } else {
  215. db = db.Where("find_in_set('收费',good_status) = 0")
  216. }
  217. }
  218. if good_kind > 0 {
  219. db = db.Where(" good_kind = ?", good_kind)
  220. }
  221. }
  222. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  223. return
  224. }
  225. func DeleteGoodInfoById(id int64, operateId int64) error {
  226. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  227. return err
  228. }
  229. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  230. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  231. return list, err
  232. }
  233. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  234. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  235. return list, err
  236. }
  237. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  238. goodInfo := &models.GoodInfo{}
  239. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  240. return goodInfo, err
  241. }
  242. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  243. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  244. return goodType, err
  245. }
  246. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  247. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  248. return goodType, err
  249. }
  250. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  251. err = readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  252. return goodInfo, err
  253. }
  254. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  255. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  256. return total, err
  257. }
  258. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  259. err := writeDb.Create(&warehouse).Error
  260. return err
  261. }
  262. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  263. err := writeDb.Create(&warehouse).Error
  264. return err
  265. }
  266. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  267. err := writeDb.Create(&salesReturn).Error
  268. return err
  269. }
  270. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  271. err := writeDb.Create(&salesReturn).Error
  272. return err
  273. }
  274. func UpDateGoodReturnStatus(ids []string) {
  275. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  276. }
  277. func UpDateCanCelStockStatus(ids []string) {
  278. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  279. }
  280. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  281. if len(warehousingInfo) > 0 {
  282. utx := writeDb.Begin()
  283. if len(warehousingInfo) > 0 {
  284. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type) VALUES "
  285. insertParams := make([]string, 0)
  286. insertData := make([]interface{}, 0)
  287. for _, info := range warehousingInfo {
  288. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  289. insertData = append(insertData, info.WarehousingId)
  290. insertData = append(insertData, info.GoodId)
  291. insertData = append(insertData, info.GoodTypeId)
  292. insertData = append(insertData, info.Number)
  293. insertData = append(insertData, info.ProductDate)
  294. insertData = append(insertData, info.ExpiryDate)
  295. insertData = append(insertData, info.WarehousingCount)
  296. insertData = append(insertData, info.Price)
  297. insertData = append(insertData, info.TotalPrice)
  298. insertData = append(insertData, info.Dealer)
  299. insertData = append(insertData, info.Manufacturer)
  300. insertData = append(insertData, info.Remark)
  301. insertData = append(insertData, info.Ctime)
  302. insertData = append(insertData, info.Mtime)
  303. insertData = append(insertData, info.Status)
  304. insertData = append(insertData, info.OrgId)
  305. insertData = append(insertData, info.WarehousingOrder)
  306. insertData = append(insertData, info.Type)
  307. }
  308. thisSQL += strings.Join(insertParams, ", ")
  309. err = utx.Exec(thisSQL, insertData...).Error
  310. if err != nil {
  311. utx.Rollback()
  312. return
  313. }
  314. }
  315. utx.Commit()
  316. }
  317. return
  318. }
  319. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  320. if len(warehousingInfo) > 0 {
  321. utx := writeDb.Begin()
  322. if len(warehousingInfo) > 0 {
  323. thisSQL := "INSERT INTO xt_drug_warehouse_info (warehousing_id, drug_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,retail_price,retail_total_price) VALUES "
  324. insertParams := make([]string, 0)
  325. insertData := make([]interface{}, 0)
  326. for _, info := range warehousingInfo {
  327. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  328. insertData = append(insertData, info.WarehousingId)
  329. insertData = append(insertData, info.DrugId)
  330. insertData = append(insertData, info.Number)
  331. insertData = append(insertData, info.ProductDate)
  332. insertData = append(insertData, info.ExpiryDate)
  333. insertData = append(insertData, info.WarehousingCount)
  334. insertData = append(insertData, info.Price)
  335. insertData = append(insertData, info.TotalPrice)
  336. insertData = append(insertData, info.Dealer)
  337. insertData = append(insertData, info.Manufacturer)
  338. insertData = append(insertData, info.Remark)
  339. insertData = append(insertData, info.Ctime)
  340. insertData = append(insertData, info.Mtime)
  341. insertData = append(insertData, info.Status)
  342. insertData = append(insertData, info.OrgId)
  343. insertData = append(insertData, info.WarehousingOrder)
  344. insertData = append(insertData, info.Type)
  345. insertData = append(insertData, info.RetailPrice)
  346. insertData = append(insertData, info.RetailTotalPrice)
  347. }
  348. thisSQL += strings.Join(insertParams, ", ")
  349. err = utx.Exec(thisSQL, insertData...).Error
  350. if err != nil {
  351. utx.Rollback()
  352. return
  353. }
  354. }
  355. utx.Commit()
  356. }
  357. return
  358. }
  359. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  360. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  361. return info, err
  362. }
  363. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  364. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  365. return info, err
  366. }
  367. func FindAllWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  368. db := readDb.Model(&models.Warehousing{})
  369. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  370. if len(keywords) > 0 {
  371. likeKey := "%" + keywords + "%"
  372. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  373. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse.manufacturer")
  374. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_warehouse.warehousing_order LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse.id")
  375. }
  376. if startTime > 0 {
  377. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  378. }
  379. if endTime > 0 {
  380. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  381. }
  382. db = db.Count(&total)
  383. offset := (page - 1) * limit
  384. err = db.Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  385. return
  386. }
  387. type VMDrugWarehouse struct {
  388. ID int64 `gorm:"column:id" json:"id" form:"id"`
  389. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  390. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  391. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  392. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  393. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  394. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  395. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  396. Status int64 `gorm:"column:status" json:"status" form:"status"`
  397. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  398. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  399. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  400. Type int64 `gorm:"column:type" json:"type" form:"type"`
  401. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  402. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  403. }
  404. func (VMDrugWarehouse) TableName() string {
  405. return "xt_drug_warehouse"
  406. }
  407. type VMDrugWarehouseInfo struct {
  408. ID int64 `gorm:"column:id" json:"id" form:"id"`
  409. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  410. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  411. Number string `gorm:"column:number" json:"number" form:"number"`
  412. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  413. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  414. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  415. Price float64 `gorm:"column:price" json:"price" form:"price"`
  416. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  417. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  418. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  419. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  420. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  421. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  422. Status int64 `gorm:"column:status" json:"status" form:"status"`
  423. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  424. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  425. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  426. Type int64 `gorm:"column:type" json:"type" form:"type"`
  427. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  428. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  429. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  430. }
  431. func (VMDrugWarehouseInfo) TableName() string {
  432. return "xt_drug_warehouse_info"
  433. }
  434. type Drug struct {
  435. ID int64 `gorm:"column:id" json:"id" form:"id"`
  436. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  437. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  438. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  439. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  440. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  441. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  442. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  443. Status int64 `gorm:"column:status" json:"status" form:"status"`
  444. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  445. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  446. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  447. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  448. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  449. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  450. Total float64 `gorm:"column:total" json:"total" form:"total"`
  451. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  452. }
  453. func (Drug) TableName() string {
  454. return "xt_base_drug"
  455. }
  456. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  457. db := readDb.Model(&VMDrugWarehouse{})
  458. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  459. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  460. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  461. if len(keywords) > 0 {
  462. likeKey := "%" + keywords + "%"
  463. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  464. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  465. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse.warehousing_order LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_warehouse.id")
  466. }
  467. if startTime > 0 {
  468. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  469. }
  470. if endTime > 0 {
  471. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  472. }
  473. db = db.Count(&total)
  474. offset := (page - 1) * limit
  475. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  476. return
  477. }
  478. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  479. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  480. return list, err
  481. }
  482. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  483. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ? AND find_in_set('停用',drug_status) = 0", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  484. return list, err
  485. }
  486. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  487. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  488. return warehousing, err
  489. }
  490. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  491. err = readDb.Model(&VMDrugWarehouse{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  492. return warehousing, err
  493. }
  494. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  495. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  496. // return warehousing, err
  497. //
  498. //}
  499. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  500. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  501. return
  502. }
  503. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  504. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  505. return
  506. }
  507. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  508. if len(salesReturnInfo) > 0 {
  509. utx := writeDb.Begin()
  510. if len(salesReturnInfo) > 0 {
  511. thisSQL := "INSERT INTO xt_sales_return_info (good_id, sales_return_id, good_type_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer) VALUES "
  512. insertParams := make([]string, 0)
  513. insertData := make([]interface{}, 0)
  514. for _, info := range salesReturnInfo {
  515. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  516. insertData = append(insertData, info.GoodId)
  517. insertData = append(insertData, info.SalesReturnId)
  518. insertData = append(insertData, info.GoodTypeId)
  519. insertData = append(insertData, info.Count)
  520. insertData = append(insertData, info.Price)
  521. insertData = append(insertData, info.Total)
  522. insertData = append(insertData, info.Ctime)
  523. insertData = append(insertData, info.Status)
  524. insertData = append(insertData, info.OrgId)
  525. insertData = append(insertData, info.OrderNumber)
  526. insertData = append(insertData, info.Type)
  527. insertData = append(insertData, info.Dealer)
  528. insertData = append(insertData, info.Manufacturer)
  529. }
  530. thisSQL += strings.Join(insertParams, ", ")
  531. err = utx.Exec(thisSQL, insertData...).Error
  532. if err != nil {
  533. utx.Rollback()
  534. return
  535. }
  536. }
  537. utx.Commit()
  538. }
  539. return
  540. }
  541. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  542. if len(salesReturnInfo) > 0 {
  543. utx := writeDb.Begin()
  544. if len(salesReturnInfo) > 0 {
  545. thisSQL := "INSERT INTO xt_drug_sales_return_info (drug_id,sales_return_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer,retail_price,retail_total_price) VALUES "
  546. insertParams := make([]string, 0)
  547. insertData := make([]interface{}, 0)
  548. for _, info := range salesReturnInfo {
  549. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  550. insertData = append(insertData, info.DrugId)
  551. insertData = append(insertData, info.SalesReturnId)
  552. insertData = append(insertData, info.Count)
  553. insertData = append(insertData, info.Price)
  554. insertData = append(insertData, info.Total)
  555. insertData = append(insertData, info.Ctime)
  556. insertData = append(insertData, info.Status)
  557. insertData = append(insertData, info.OrgId)
  558. insertData = append(insertData, info.OrderNumber)
  559. insertData = append(insertData, info.Type)
  560. insertData = append(insertData, info.Dealer)
  561. insertData = append(insertData, info.Manufacturer)
  562. insertData = append(insertData, info.RetailPrice)
  563. insertData = append(insertData, info.RetailTotalPrice)
  564. }
  565. thisSQL += strings.Join(insertParams, ", ")
  566. err = utx.Exec(thisSQL, insertData...).Error
  567. if err != nil {
  568. utx.Rollback()
  569. return
  570. }
  571. }
  572. utx.Commit()
  573. }
  574. return
  575. }
  576. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  577. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  578. return total, err
  579. }
  580. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  581. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  582. return total, err
  583. }
  584. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  585. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  586. return total, err
  587. }
  588. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  589. db := readDb.Model(&models.SalesReturn{})
  590. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  591. if len(keywords) > 0 {
  592. likeKey := "%" + keywords + "%"
  593. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  594. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  595. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_sales_return.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_sales_return.id")
  596. }
  597. if startTime > 0 {
  598. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  599. }
  600. if endTime > 0 {
  601. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  602. }
  603. db = db.Count(&total)
  604. offset := (page - 1) * limit
  605. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  606. return
  607. }
  608. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  609. db := readDb.Model(&models.DrugSalesReturn{})
  610. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  611. if len(keywords) > 0 {
  612. likeKey := "%" + keywords + "%"
  613. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  614. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  615. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_sales_return.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_sales_return.id")
  616. }
  617. if startTime > 0 {
  618. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  619. }
  620. if endTime > 0 {
  621. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  622. }
  623. db = db.Count(&total)
  624. offset := (page - 1) * limit
  625. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  626. return
  627. }
  628. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  629. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  630. return list, err
  631. }
  632. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  633. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  634. return list, err
  635. }
  636. type DrugCancelStockInfo struct {
  637. ID int64 `gorm:"column:id" json:"id" form:"id"`
  638. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  639. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  640. Count int64 `gorm:"column:count" json:"count" form:"count"`
  641. Price float64 `gorm:"column:price" json:"price" form:"price"`
  642. Total float64 `gorm:"column:total" json:"total" form:"total"`
  643. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  644. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  645. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  646. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  647. Status int64 `gorm:"column:status" json:"status" form:"status"`
  648. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  649. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  650. Type int64 `gorm:"column:type" json:"type" form:"type"`
  651. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  652. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  653. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  654. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  655. Number string `gorm:"column:number" json:"number" form:"number"`
  656. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  657. }
  658. func (DrugCancelStockInfo) TableName() string {
  659. return "xt_drug_cancel_stock_info"
  660. }
  661. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  662. err = readDb.Model(&DrugCancelStockInfo{}).Preload("Drug", "status = 1 AND org_id = ? ", org_id).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  663. return list, err
  664. }
  665. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  666. db := readDb.Model(&models.WarehousingInfo{})
  667. db = db.Where("org_id = ? AND status = 1", orgId)
  668. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  669. if startTime > 0 {
  670. db = db.Where("ctime >=?", startTime)
  671. }
  672. if endTime > 0 {
  673. db = db.Where("ctime<= ?", endTime)
  674. }
  675. db = db.Count(&total)
  676. offset := (page - 1) * limit
  677. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  678. return list, total, err
  679. }
  680. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  681. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  682. return total, err
  683. }
  684. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  685. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  686. return total, err
  687. }
  688. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  689. err := writeDb.Create(&warehouseOut).Error
  690. return err
  691. }
  692. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  693. err := writeDb.Create(&warehouseOut).Error
  694. return err
  695. }
  696. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  697. if len(warehouseOutInfo) > 0 {
  698. utx := writeDb.Begin()
  699. if len(warehouseOutInfo) > 0 {
  700. thisSQL := "INSERT INTO xt_warehouse_out_info (warehouse_out_id, good_id, good_type_id, product_date,expiry_date,count,price,total_price,remark,ctime,status,org_id,warehouse_out_order_number,type,dealer,manufacturer) VALUES "
  701. insertParams := make([]string, 0)
  702. insertData := make([]interface{}, 0)
  703. for _, info := range warehouseOutInfo {
  704. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  705. insertData = append(insertData, info.WarehouseOutId)
  706. insertData = append(insertData, info.GoodId)
  707. insertData = append(insertData, info.GoodTypeId)
  708. insertData = append(insertData, info.ProductDate)
  709. insertData = append(insertData, info.ExpiryDate)
  710. insertData = append(insertData, info.Count)
  711. insertData = append(insertData, info.Price)
  712. insertData = append(insertData, info.TotalPrice)
  713. insertData = append(insertData, info.Remark)
  714. insertData = append(insertData, info.Ctime)
  715. insertData = append(insertData, info.Status)
  716. insertData = append(insertData, info.OrgId)
  717. insertData = append(insertData, info.WarehouseOutOrderNumber)
  718. insertData = append(insertData, info.Type)
  719. insertData = append(insertData, info.Dealer)
  720. insertData = append(insertData, info.Manufacturer)
  721. }
  722. thisSQL += strings.Join(insertParams, ", ")
  723. err = utx.Exec(thisSQL, insertData...).Error
  724. if err != nil {
  725. utx.Rollback()
  726. return
  727. }
  728. }
  729. utx.Commit()
  730. }
  731. return
  732. }
  733. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  734. if len(warehouseOutInfo) > 0 {
  735. utx := writeDb.Begin()
  736. if len(warehouseOutInfo) > 0 {
  737. thisSQL := "INSERT INTO xt_drug_warehouse_out_info (warehouse_out_id, drug_id, product_date,expiry_date,count,price,total_price,remark,ctime,status,org_id,warehouse_out_order_number,type,dealer,manufacturer,retail_price,retail_total_price) VALUES "
  738. insertParams := make([]string, 0)
  739. insertData := make([]interface{}, 0)
  740. for _, info := range warehouseOutInfo {
  741. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  742. insertData = append(insertData, info.WarehouseOutId)
  743. insertData = append(insertData, info.DrugId)
  744. insertData = append(insertData, info.ProductDate)
  745. insertData = append(insertData, info.ExpiryDate)
  746. insertData = append(insertData, info.Count)
  747. insertData = append(insertData, info.Price)
  748. insertData = append(insertData, info.TotalPrice)
  749. insertData = append(insertData, info.Remark)
  750. insertData = append(insertData, info.Ctime)
  751. insertData = append(insertData, info.Status)
  752. insertData = append(insertData, info.OrgId)
  753. insertData = append(insertData, info.WarehouseOutOrderNumber)
  754. insertData = append(insertData, info.Type)
  755. insertData = append(insertData, info.Dealer)
  756. insertData = append(insertData, info.Manufacturer)
  757. insertData = append(insertData, info.RetailPrice)
  758. insertData = append(insertData, info.RetailTotalPrice)
  759. }
  760. thisSQL += strings.Join(insertParams, ", ")
  761. err = utx.Exec(thisSQL, insertData...).Error
  762. if err != nil {
  763. utx.Rollback()
  764. return
  765. }
  766. }
  767. utx.Commit()
  768. }
  769. return
  770. }
  771. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  772. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  773. return info, err
  774. }
  775. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  776. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  777. return info, err
  778. }
  779. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  780. db := readDb.Model(&models.WarehouseOut{})
  781. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  782. if len(keywords) > 0 {
  783. likeKey := "%" + keywords + "%"
  784. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  785. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  786. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out.warehouse_out_order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_out.id")
  787. }
  788. if startTime > 0 {
  789. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  790. }
  791. if endTime > 0 {
  792. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  793. }
  794. db = db.Count(&total)
  795. offset := (page - 1) * limit
  796. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  797. return list, total, err
  798. }
  799. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  800. db := readDb.Model(&models.DrugWarehouseOut{})
  801. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  802. if len(keywords) > 0 {
  803. likeKey := "%" + keywords + "%"
  804. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  805. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  806. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse_out.warehouse_out_order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_warehouse_out.id")
  807. }
  808. if startTime > 0 {
  809. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  810. }
  811. if endTime > 0 {
  812. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  813. }
  814. db = db.Count(&total)
  815. offset := (page - 1) * limit
  816. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  817. return list, total, err
  818. }
  819. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  820. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? AND status = 1 AND count <> 0 AND good_id <> 0", id).Order("good_type_id desc").Find(&list).Error
  821. return list, err
  822. }
  823. type DrugWarehouseOutInfo struct {
  824. ID int64 `gorm:"column:id" json:"id" form:"id"`
  825. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  826. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  827. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  828. Count int64 `gorm:"column:count" json:"count" form:"count"`
  829. Price float64 `gorm:"column:price" json:"price" form:"price"`
  830. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  831. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  832. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  833. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  834. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  835. Status int64 `gorm:"column:status" json:"status" form:"status"`
  836. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  837. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  838. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  839. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  840. Type int64 `gorm:"column:type" json:"type" form:"type"`
  841. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  842. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  843. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  844. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  845. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  846. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  847. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  848. }
  849. func (DrugWarehouseOutInfo) TableName() string {
  850. return "xt_drug_warehouse_out_info"
  851. }
  852. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  853. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  854. return list, err
  855. }
  856. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  857. info := &models.WarehousingInfo{}
  858. type WarehouseInfoCountAmount struct {
  859. Total int64
  860. }
  861. var result WarehouseInfoCountAmount
  862. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  863. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  864. return result.Total, info
  865. }
  866. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  867. type SalesReturnCountAmount struct {
  868. Total int64
  869. }
  870. var result SalesReturnCountAmount
  871. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  872. return result.Total, err
  873. }
  874. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  875. type WarehouseOutInfoCountAmount struct {
  876. Total int64
  877. }
  878. var result WarehouseOutInfoCountAmount
  879. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  880. return result.Total, err
  881. }
  882. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  883. type CancleStockoCountAmount struct {
  884. Total int64
  885. }
  886. var result CancleStockoCountAmount
  887. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  888. return result.Total, err
  889. }
  890. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  891. info := models.WarehousingInfo{}
  892. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  893. return info, err
  894. }
  895. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  896. err := writeDb.Create(&cancelStock).Error
  897. return err
  898. }
  899. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  900. err := writeDb.Create(&cancelStock).Error
  901. return err
  902. }
  903. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  904. if len(cancelStockInfo) > 0 {
  905. utx := writeDb.Begin()
  906. if len(cancelStockInfo) > 0 {
  907. thisSQL := "INSERT INTO xt_cancel_stock_info (good_id, cancel_stock_id, good_type_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer) VALUES "
  908. insertParams := make([]string, 0)
  909. insertData := make([]interface{}, 0)
  910. for _, info := range cancelStockInfo {
  911. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  912. insertData = append(insertData, info.GoodId)
  913. insertData = append(insertData, info.CancelStockId)
  914. insertData = append(insertData, info.GoodTypeId)
  915. insertData = append(insertData, info.Count)
  916. insertData = append(insertData, info.Price)
  917. insertData = append(insertData, info.Total)
  918. insertData = append(insertData, info.Ctime)
  919. insertData = append(insertData, info.Status)
  920. insertData = append(insertData, info.OrgId)
  921. insertData = append(insertData, info.OrderNumber)
  922. insertData = append(insertData, info.Type)
  923. insertData = append(insertData, info.Dealer)
  924. insertData = append(insertData, info.Manufacturer)
  925. }
  926. thisSQL += strings.Join(insertParams, ", ")
  927. err = utx.Exec(thisSQL, insertData...).Error
  928. if err != nil {
  929. utx.Rollback()
  930. return
  931. }
  932. }
  933. utx.Commit()
  934. }
  935. return
  936. }
  937. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  938. if len(cancelStockInfo) > 0 {
  939. utx := writeDb.Begin()
  940. if len(cancelStockInfo) > 0 {
  941. thisSQL := "INSERT INTO xt_drug_cancel_stock_info (drug_id,cancel_stock_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer,retail_price,retail_total_price) VALUES "
  942. insertParams := make([]string, 0)
  943. insertData := make([]interface{}, 0)
  944. for _, info := range cancelStockInfo {
  945. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  946. insertData = append(insertData, info.DrugId)
  947. insertData = append(insertData, info.CancelStockId)
  948. insertData = append(insertData, info.Count)
  949. insertData = append(insertData, info.Price)
  950. insertData = append(insertData, info.Total)
  951. insertData = append(insertData, info.Ctime)
  952. insertData = append(insertData, info.Status)
  953. insertData = append(insertData, info.OrgId)
  954. insertData = append(insertData, info.OrderNumber)
  955. insertData = append(insertData, info.Type)
  956. insertData = append(insertData, info.Dealer)
  957. insertData = append(insertData, info.Manufacturer)
  958. insertData = append(insertData, info.RetailPrice)
  959. insertData = append(insertData, info.RetailTotalPrice)
  960. }
  961. thisSQL += strings.Join(insertParams, ", ")
  962. err = utx.Exec(thisSQL, insertData...).Error
  963. if err != nil {
  964. utx.Rollback()
  965. return
  966. }
  967. }
  968. utx.Commit()
  969. }
  970. return
  971. }
  972. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  973. db := readDb.Model(&models.CancelStock{})
  974. db = db.Where("org_id = ? AND status = 1", orgId)
  975. err = db.Order("ctime desc").Find(&list).Error
  976. return
  977. }
  978. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  979. db := readDb.Model(&models.WarehousingInfo{})
  980. db = db.Where("org_id = ? AND status = 1", orgId)
  981. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  982. if startTime > 0 {
  983. db = db.Where("ctime >=?", startTime)
  984. }
  985. if endTime > 0 {
  986. db = db.Where("ctime<= ?", endTime)
  987. }
  988. db = db.Count(&total)
  989. offset := (page - 1) * limit
  990. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  991. return
  992. }
  993. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  994. db := readDb.Model(&models.StockInfo{})
  995. if startime > 0 {
  996. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  997. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  998. })
  999. }
  1000. if endtime > 0 {
  1001. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1002. return db.Where("org_id = ? AND status = 1 and sys_record_time<=?", orgId, endtime)
  1003. })
  1004. }
  1005. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  1006. db = db.Group("xt_good_information.id")
  1007. if startime == 0 || endtime == 0 {
  1008. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1009. return db.Where("org_id = ? AND status = 1", orgId)
  1010. })
  1011. }
  1012. if startime > 0 {
  1013. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1014. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  1015. })
  1016. }
  1017. if endtime > 0 {
  1018. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1019. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  1020. })
  1021. }
  1022. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  1023. if startime == 0 || endtime == 0 {
  1024. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1025. return db.Where("org_id = ? AND status = 1", orgId)
  1026. })
  1027. }
  1028. if startime == 0 || endtime == 0 {
  1029. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  1030. }
  1031. if startime > 0 {
  1032. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  1033. }
  1034. if endtime > 0 {
  1035. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  1036. }
  1037. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  1038. if len(keyword) > 0 {
  1039. likeKey := "%" + keyword + "%"
  1040. db = db.Joins("join xt_goods_type on xt_goods_type.id = xt_good_information.good_type_id AND xt_goods_type.org_id = ? AND xt_goods_type.status = 1", orgId)
  1041. db = db.Where("xt_good_information.good_code LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ? OR xt_goods_type.type_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_good_information.id")
  1042. }
  1043. if type_name > 0 {
  1044. db = db.Joins("join xt_goods_type on xt_goods_type.id = xt_good_information.good_type_id AND xt_goods_type.org_id = ? AND xt_goods_type.status = 1 and xt_goods_type.id = ?", orgId, type_name)
  1045. }
  1046. db = db.Count(&total)
  1047. offset := (page - 1) * limit
  1048. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1049. return
  1050. }
  1051. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  1052. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  1053. return
  1054. }
  1055. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  1056. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  1057. return
  1058. }
  1059. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  1060. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"operation_time": warehouse.OperationTime, "mtime": warehouse.Mtime, "modifier": warehouse.Modifier}).Error
  1061. return
  1062. }
  1063. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  1064. err = readDb.Model(&models.WarehouseOut{}).Where("warehouse_out_order_number = ? AND status = 1", warehouseOut.WarehouseOutOrderNumber).Update(map[string]interface{}{"operation_time": warehouseOut.OperationTime, "mtime": warehouseOut.Mtime, "modifier": warehouseOut.Modifier}).Error
  1065. return
  1066. }
  1067. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  1068. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  1069. "good_id": warehouseInfo.GoodId,
  1070. "number": warehouseInfo.Number,
  1071. "product_date": warehouseInfo.ProductDate,
  1072. "expiry_date": warehouseInfo.ExpiryDate,
  1073. "warehousing_count": warehouseInfo.WarehousingCount,
  1074. "price": warehouseInfo.Price,
  1075. "total_price": warehouseInfo.TotalPrice,
  1076. "mtime": warehouseInfo.Mtime,
  1077. "remark": warehouseInfo.Remark,
  1078. }).Error
  1079. return
  1080. }
  1081. func UpDateWarehouseInfoByStock(warehouseInfo *models.WarehousingInfo) (err error) {
  1082. err = writeDb.Save(&warehouseInfo).Error
  1083. return
  1084. }
  1085. //func UpDateWarehouseInfoByStockDelete(id int64,count int64) (err error) {
  1086. // err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?",id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  1087. // return err
  1088. //}
  1089. func UpDateWarehouseStatus(id int64) (err error) {
  1090. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1091. return
  1092. }
  1093. func UpDateDrugWarehouseStatus(id int64) (err error) {
  1094. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1095. return
  1096. }
  1097. func DeleteWarehouse(ids []string) (err error) {
  1098. ut := writeDb.Begin()
  1099. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1100. if err != nil {
  1101. ut.Rollback()
  1102. return
  1103. }
  1104. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1105. if err != nil {
  1106. ut.Rollback()
  1107. return
  1108. }
  1109. ut.Commit()
  1110. return
  1111. }
  1112. func DeleteDrugWarehouse(ids []string) (err error) {
  1113. ut := writeDb.Begin()
  1114. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1115. if err != nil {
  1116. ut.Rollback()
  1117. return
  1118. }
  1119. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1120. if err != nil {
  1121. ut.Rollback()
  1122. return
  1123. }
  1124. ut.Commit()
  1125. return
  1126. }
  1127. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  1128. warehouse := &models.Warehousing{}
  1129. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  1130. return warehouse, err
  1131. }
  1132. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  1133. warehouseOut := &models.WarehouseOut{}
  1134. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  1135. return warehouseOut, err
  1136. }
  1137. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  1138. warehouseOut := &models.DrugWarehouseOut{}
  1139. err := readDb.Model(&models.DrugWarehouseOut{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  1140. return warehouseOut, err
  1141. }
  1142. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  1143. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  1144. return
  1145. }
  1146. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  1147. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  1148. return
  1149. }
  1150. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  1151. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1152. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1153. return
  1154. }
  1155. func DeleteStockInRecordByReocordId(id int64) (err error) {
  1156. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1157. return
  1158. }
  1159. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  1160. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  1161. if manufacturer_id > 0 {
  1162. db = db.Where("manufacturer = ?", manufacturer_id)
  1163. }
  1164. if dealer_id > 0 {
  1165. db = db.Where("dealer = ?", dealer_id)
  1166. }
  1167. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  1168. return goodInfo, err
  1169. }
  1170. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  1171. db := readDb.Model(&models.Warehousing{})
  1172. likekey := "%" + keywords + "%"
  1173. db = db.Where("org_id = ? AND status = 1 ", orgId)
  1174. db = db.Where("warehousing_order LIKE ? ", likekey)
  1175. db = db.Count(&total)
  1176. offset := (page - 1) * limit
  1177. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1178. return
  1179. }
  1180. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  1181. err = writeDb.Save(&info).Error
  1182. return err
  1183. }
  1184. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  1185. err = writeDb.Save(&info).Error
  1186. return err
  1187. }
  1188. func EditWarehousing(warehouse models.Warehousing) {
  1189. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer}).Error
  1190. }
  1191. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  1192. err = readDb.Model(&models.DrugWarehouse{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer}).Error
  1193. }
  1194. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  1195. err = readDb.Model(&models.Warehousing{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1196. return list, err
  1197. }
  1198. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  1199. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  1200. return db.Where("status = 1 AND org_id = ?", org_id).Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id)
  1201. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1202. return list, err
  1203. }
  1204. func DeleteSalesReturn(ids []string) (err error) {
  1205. ut := writeDb.Begin()
  1206. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1207. if err != nil {
  1208. ut.Rollback()
  1209. return
  1210. }
  1211. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1212. if err != nil {
  1213. ut.Rollback()
  1214. return
  1215. }
  1216. ut.Commit()
  1217. return
  1218. }
  1219. func DeleteDrugSalesReturn(ids []string) (err error) {
  1220. ut := writeDb.Begin()
  1221. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1222. if err != nil {
  1223. ut.Rollback()
  1224. return
  1225. }
  1226. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1227. if err != nil {
  1228. ut.Rollback()
  1229. return
  1230. }
  1231. ut.Commit()
  1232. return
  1233. }
  1234. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1235. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1236. return
  1237. }
  1238. type DrugSalesReturnInfo struct {
  1239. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1240. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1241. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  1242. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1243. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1244. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1245. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1246. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1247. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1248. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1249. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1250. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1251. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1252. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1253. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1254. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1255. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1256. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1257. Number string `gorm:"column:number" json:"number" form:"number"`
  1258. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1259. }
  1260. func (DrugSalesReturnInfo) TableName() string {
  1261. return "xt_drug_sales_return_info"
  1262. }
  1263. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  1264. err = readDb.Model(&DrugSalesReturnInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("sales_return_id = ? AND status = 1 AND org_id = ?", id, org_id).Find(&list).Error
  1265. return
  1266. }
  1267. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  1268. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1269. return
  1270. }
  1271. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  1272. err = readDb.Model(&models.DrugSalesReturn{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1273. return
  1274. }
  1275. func UpDateSaleReturnStatus(id int64) (err error) {
  1276. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1277. return
  1278. }
  1279. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  1280. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1281. return
  1282. }
  1283. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  1284. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1285. return salesReturn, err
  1286. }
  1287. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  1288. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1289. return salesReturn, err
  1290. }
  1291. func EditSaleReturn(sales models.SalesReturn) {
  1292. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  1293. }
  1294. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  1295. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  1296. }
  1297. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  1298. err = writeDb.Save(&info).Error
  1299. return err
  1300. }
  1301. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  1302. err = writeDb.Save(&info).Error
  1303. return err
  1304. }
  1305. func DeleteWarehouseOut(ids []string) (err error) {
  1306. ut := writeDb.Begin()
  1307. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1308. if err != nil {
  1309. ut.Rollback()
  1310. return
  1311. }
  1312. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1313. if err != nil {
  1314. ut.Rollback()
  1315. return
  1316. }
  1317. ut.Commit()
  1318. return
  1319. }
  1320. func DeleteDrugWarehouseOut(ids []string) (err error) {
  1321. ut := writeDb.Begin()
  1322. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1323. if err != nil {
  1324. ut.Rollback()
  1325. return
  1326. }
  1327. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1328. if err != nil {
  1329. ut.Rollback()
  1330. return
  1331. }
  1332. ut.Commit()
  1333. return
  1334. }
  1335. func UpDateWarehouseOutStatus(id int64) (err error) {
  1336. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1337. return
  1338. }
  1339. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  1340. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1341. return
  1342. }
  1343. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  1344. err = readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", warehouseOut.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehouse_out_time": warehouseOut.WarehouseOutTime, "dealer": warehouseOut.Dealer, "manufacturer": warehouseOut.Manufacturer}).Error
  1345. }
  1346. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  1347. err = readDb.Model(&models.DrugWarehouseOut{}).Where("id = ? AND status = 1", warehouseOut.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehouse_out_time": warehouseOut.WarehouseOutTime, "dealer": warehouseOut.Dealer, "manufacturer": warehouseOut.Manufacturer}).Error
  1348. }
  1349. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  1350. err = writeDb.Save(&info).Error
  1351. return err
  1352. }
  1353. func UpDateDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) (err error) {
  1354. err = writeDb.Save(&info).Error
  1355. return err
  1356. }
  1357. func FindAllCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.CancelStock, total int64, err error) {
  1358. db := readDb.Model(&models.CancelStock{})
  1359. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  1360. if len(keywords) > 0 {
  1361. likeKey := "%" + keywords + "%"
  1362. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  1363. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  1364. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_cancel_stock.id")
  1365. }
  1366. if startTime > 0 {
  1367. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  1368. }
  1369. if endTime > 0 {
  1370. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  1371. }
  1372. db = db.Count(&total)
  1373. offset := (page - 1) * limit
  1374. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  1375. return
  1376. }
  1377. func FindAllDrugCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugCancelStock, total int64, err error) {
  1378. db := readDb.Model(&models.DrugCancelStock{})
  1379. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  1380. if len(keywords) > 0 {
  1381. likeKey := "%" + keywords + "%"
  1382. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  1383. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  1384. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_cancel_stock.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_cancel_stock.id")
  1385. }
  1386. if startTime > 0 {
  1387. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  1388. }
  1389. if endTime > 0 {
  1390. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  1391. }
  1392. db = db.Count(&total)
  1393. offset := (page - 1) * limit
  1394. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  1395. return
  1396. }
  1397. func DeleteCancelStock(ids []string) (err error) {
  1398. ut := writeDb.Begin()
  1399. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1400. if err != nil {
  1401. ut.Rollback()
  1402. return
  1403. }
  1404. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1405. if err != nil {
  1406. ut.Rollback()
  1407. return
  1408. }
  1409. ut.Commit()
  1410. return
  1411. }
  1412. func DeleteDrugCancelStock(ids []string) (err error) {
  1413. ut := writeDb.Begin()
  1414. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1415. if err != nil {
  1416. ut.Rollback()
  1417. return
  1418. }
  1419. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1420. if err != nil {
  1421. ut.Rollback()
  1422. return
  1423. }
  1424. ut.Commit()
  1425. return
  1426. }
  1427. func UpDateCancleStockStatus(id int64) (err error) {
  1428. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1429. return
  1430. }
  1431. func UpDateDrugCancleStockStatus(id int64) (err error) {
  1432. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1433. return
  1434. }
  1435. func EditCancelStock(sales models.CancelStock) {
  1436. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  1437. }
  1438. func EditDrugCancelStock(sales models.DrugCancelStock) {
  1439. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  1440. }
  1441. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  1442. err = readDb.Model(&models.CancelStock{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  1443. return cancelStock, err
  1444. }
  1445. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  1446. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  1447. return cancelStock, err
  1448. }
  1449. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  1450. err = writeDb.Save(&info).Error
  1451. return err
  1452. }
  1453. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  1454. err = writeDb.Save(&info).Error
  1455. return err
  1456. }
  1457. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  1458. err = readDb.Model(&models.WarehouseOut{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1459. return list, err
  1460. }
  1461. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  1462. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  1463. return db.Where("status = 1 AND org_id = ?", org_id).Preload("GoodsType", "status = 1 AND org_id = ? ", org_id).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id)
  1464. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1465. return list, err
  1466. }
  1467. type StockDetailModel struct {
  1468. GoodId int64 `gorm:"good_id" json:"good_id"`
  1469. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  1470. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  1471. Price float64 `gorm:"price" json:"price"`
  1472. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  1473. Type int `gorm:"type" json:"type"`
  1474. Creater int64 `gorm:"creater" json:"creater"`
  1475. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  1476. Types int64 `gorm:"types" json:"types"`
  1477. }
  1478. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.WarehousingInfo, total int64, err error) {
  1479. db := readDb.Model(&models.WarehousingInfo{})
  1480. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  1481. if len(keywords) > 0 {
  1482. likeKey := "%" + keywords + "%"
  1483. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  1484. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  1485. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1486. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  1487. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse.manufacturer")
  1488. //db = db.Where(" sgj_user_admin_role.user_name LIKE ?", likeKey).Group("xt_warehouse_info.id")
  1489. db = db.Where(" sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_info.warehousing_order LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_info.id")
  1490. } else {
  1491. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  1492. }
  1493. if manufacturer > 0 {
  1494. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  1495. }
  1496. if order_type > 0 {
  1497. db = db.Where("xt_warehouse_info.type = ?", order_type)
  1498. }
  1499. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1500. db = db.Count(&total)
  1501. offset := (page - 1) * limit
  1502. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.ctime desc").Find(&list).Error
  1503. return
  1504. }
  1505. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (err error, totalPrice float64) {
  1506. db := readDb.Model(&models.WarehousingInfo{})
  1507. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  1508. if len(keywords) > 0 {
  1509. likeKey := "%" + keywords + "%"
  1510. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  1511. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  1512. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1513. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  1514. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_info.warehousing_order LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_info.id")
  1515. } else {
  1516. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  1517. }
  1518. if manufacturer > 0 {
  1519. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  1520. }
  1521. if order_type > 0 {
  1522. db = db.Where("xt_warehouse_info.type = ?", order_type)
  1523. }
  1524. type PriceAmount struct {
  1525. Total float64
  1526. }
  1527. var result []PriceAmount
  1528. err = db.Select("total_price as total").Scan(&result).Error
  1529. var total float64
  1530. for _, value := range result {
  1531. total = total + value.Total
  1532. }
  1533. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  1534. return err, total
  1535. }
  1536. func FloatRound(f float64, n int) float64 {
  1537. format := "%." + strconv.Itoa(n) + "f"
  1538. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  1539. return res
  1540. }
  1541. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1542. db := readDb.Model(&models.WarehouseOutInfo{})
  1543. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 ", orgId)
  1544. if len(keywords) > 0 {
  1545. likeKey := "%" + keywords + "%"
  1546. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  1547. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1548. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  1549. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out_info.warehouse_out_order_number LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_out_info.id")
  1550. } else {
  1551. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  1552. }
  1553. if manufacturer > 0 {
  1554. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  1555. }
  1556. if order_type > 0 {
  1557. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  1558. }
  1559. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  1560. db = db.Count(&total)
  1561. offset := (page - 1) * limit
  1562. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.ctime desc").Find(&list).Error
  1563. return
  1564. }
  1565. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (err error, totalPrice float64) {
  1566. db := readDb.Model(&models.WarehouseOutInfo{})
  1567. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0", orgId)
  1568. if len(keywords) > 0 {
  1569. likeKey := "%" + keywords + "%"
  1570. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  1571. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1572. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  1573. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out_info.warehouse_out_order_number LIKE ?", likeKey, likeKey).Group("xt_warehouse_out_info.id")
  1574. } else {
  1575. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  1576. }
  1577. if manufacturer > 0 {
  1578. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  1579. }
  1580. if order_type > 0 {
  1581. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  1582. }
  1583. type PriceAmount struct {
  1584. Total float64
  1585. }
  1586. var result []PriceAmount
  1587. err = db.Select("(count * price) as total").Scan(&result).Error
  1588. var total float64
  1589. for _, value := range result {
  1590. total = total + value.Total
  1591. }
  1592. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  1593. fmt.Println(total)
  1594. return err, total
  1595. }
  1596. func FindSalesReturnDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.SalesReturnInfo, total int64, err error) {
  1597. db := readDb.Model(&models.SalesReturnInfo{})
  1598. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  1599. if len(keywords) > 0 {
  1600. likeKey := "%" + keywords + "%"
  1601. db = db.Joins("join xt_sales_return on xt_sales_return.id = xt_sales_return_info.sales_return_id AND xt_sales_return.return_time >=? AND xt_sales_return.return_time<= ? AND xt_sales_return.status = 1 AND xt_sales_return.org_id = ?", startTime, endTime, orgId)
  1602. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1603. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  1604. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_sales_return_info.order_number LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_sales_return_info.id")
  1605. } else {
  1606. db = db.Joins("join xt_sales_return on xt_sales_return.id = xt_sales_return_info.sales_return_id AND xt_sales_return.return_time >=? AND xt_sales_return.return_time<= ? AND xt_sales_return.status = 1 AND xt_sales_return.org_id = ?", startTime, endTime, orgId)
  1607. }
  1608. if manufacturer > 0 {
  1609. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  1610. }
  1611. if order_type > 0 {
  1612. db = db.Where("xt_sales_return_info.type = ?", order_type)
  1613. }
  1614. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  1615. offset := (page - 1) * limit
  1616. db = db.Count(&total)
  1617. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  1618. return
  1619. }
  1620. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.CancelStockInfo, total int64, err error) {
  1621. db := readDb.Model(&models.CancelStockInfo{})
  1622. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  1623. if len(keywords) > 0 {
  1624. likeKey := "%" + keywords + "%"
  1625. db = db.Joins("join xt_cancel_stock on xt_cancel_stock.id = xt_cancel_stock_info.cancel_stock_id AND xt_cancel_stock.return_time >=? AND xt_cancel_stock.return_time<= ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.org_id = ?", startTime, endTime, orgId)
  1626. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  1627. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  1628. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock_info.order_number LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_cancel_stock_info.id")
  1629. } else {
  1630. db = db.Joins("join xt_cancel_stock on xt_cancel_stock.id = xt_cancel_stock_info.cancel_stock_id AND xt_cancel_stock.return_time >=? AND xt_cancel_stock.return_time<= ? ", startTime, endTime)
  1631. }
  1632. if manufacturer > 0 {
  1633. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  1634. }
  1635. if order_type > 0 {
  1636. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  1637. }
  1638. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  1639. db = db.Count(&total)
  1640. offset := (page - 1) * limit
  1641. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  1642. return
  1643. }
  1644. func FindStockInByDealerId(id int64) (total int64, err error) {
  1645. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  1646. return
  1647. }
  1648. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  1649. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  1650. return
  1651. }
  1652. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  1653. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  1654. return
  1655. }
  1656. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  1657. readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 AND specification_name = ? AND good_type_id = ? ", org_id, name, good_type_id).Count(&total)
  1658. return
  1659. }
  1660. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  1661. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  1662. return goodType, err
  1663. }
  1664. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  1665. err = readDb.Model(&models.WarehousingInfo{}).Preload("GoodInfo", " org_id = ? AND good_type_id = ? AND status = 1 AND specification_name <> '' ", org_id, good_type_id).Where(" org_id = ? AND good_type_id = ? AND status = 1 ", org_id, good_type_id).Find(&info).Error
  1666. return info, err
  1667. }
  1668. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  1669. err = readDb.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  1670. return
  1671. }
  1672. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  1673. err = readDb.Model(&models.DrugWarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  1674. return
  1675. }
  1676. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  1677. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).Last(&in).Error
  1678. return
  1679. }
  1680. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  1681. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  1682. return
  1683. }
  1684. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  1685. err := writeDb.Create(&info).Error
  1686. return err
  1687. }
  1688. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  1689. err := writeDb.Create(&info).Error
  1690. return err
  1691. }
  1692. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  1693. err := XTWriteDB().Create(&detail).Error
  1694. return err
  1695. }
  1696. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  1697. err := writeDb.Create(&detail).Error
  1698. return err
  1699. }
  1700. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  1701. err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND (warehouse_out_id = ? or warehouse_out_order_number = ?)", org_id, good_type_id, good_id, out_id, number).First(&out).Error
  1702. return
  1703. }
  1704. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  1705. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ? AND is_sys = 1 AND (warehouse_out_id = ? or warehouse_out_order_number = ?)", org_id, drug_id, out_id, number).First(&out).Error
  1706. return
  1707. }
  1708. func UpdateStockOutInfoCount(org_id int64, id int64) {
  1709. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  1710. }
  1711. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  1712. db := readDb.Model(&models.AutomaticReduceDetail{})
  1713. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  1714. db = db.Preload("GoodsType", "status = 1")
  1715. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  1716. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0", org_id, id)
  1717. db = db.Count(&total)
  1718. err = db.Find(&user).Error
  1719. return
  1720. }
  1721. type DrugAutomaticReduceDetail struct {
  1722. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1723. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1724. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1725. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1726. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1727. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1728. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1729. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  1730. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1731. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1732. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1733. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1734. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  1735. }
  1736. func (DrugAutomaticReduceDetail) TableName() string {
  1737. return "xt_drug_automatic_reduce_detail"
  1738. }
  1739. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  1740. db := readDb.Model(&DrugAutomaticReduceDetail{})
  1741. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  1742. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  1743. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  1744. db = db.Count(&total)
  1745. err = db.Find(&user).Error
  1746. return
  1747. }
  1748. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  1749. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  1750. }
  1751. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  1752. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  1753. }
  1754. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  1755. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("org_id = ? AND status = 1 AND good_id = ? AND good_type_id = ? AND patient_id = ? AND record_time = ?", org_id, good_id, good_type_id, patient_id, record_time).Count(&count).Error
  1756. return
  1757. }
  1758. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  1759. err = readDb.Model(&models.DrugAutomaticReduceDetail{}).Where("org_id = ? AND status = 1 AND drug_id = ? AND patient_id = ? AND record_time = ?", org_id, drug_id, patient_id, record_time).Count(&count).Error
  1760. return
  1761. }
  1762. type VMWarehousingInfo struct {
  1763. ID int64 `gorm:"column:id" json:"id"`
  1764. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  1765. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  1766. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  1767. Status int64 `gorm:"column:status" json:"status"`
  1768. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  1769. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  1770. }
  1771. func (VMWarehousingInfo) TableName() string {
  1772. return "xt_warehouse_info"
  1773. }
  1774. type GoodInfo struct {
  1775. ID int64 `gorm:"column:id" json:"id"`
  1776. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  1777. Status int64 `gorm:"column:status" json:"status"`
  1778. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  1779. }
  1780. func (GoodInfo) TableName() string {
  1781. return "xt_good_information"
  1782. }
  1783. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  1784. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  1785. return
  1786. }
  1787. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  1788. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND record_date = ? AND status = 1", patient_id, orgId, record_time).Find(&goodUser).Error
  1789. return
  1790. }
  1791. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  1792. likeKey := "%" + keyword + "%"
  1793. db := XTReadDB().Model(&goods)
  1794. if len(keyword) > 0 {
  1795. db = db.Where("type_name like ?", likeKey)
  1796. }
  1797. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  1798. return goods, err
  1799. }