stock_service.go 247KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677
  1. package service
  2. import (
  3. "XT_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 GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  106. goodsType := models.GoodsType{}
  107. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  108. if err == gorm.ErrRecordNotFound {
  109. return nil, err
  110. }
  111. if err != nil {
  112. return nil, err
  113. }
  114. return &goodsType, nil
  115. }
  116. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  117. goodsType := models.GoodsType{}
  118. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  119. if err == gorm.ErrRecordNotFound {
  120. return nil, err
  121. }
  122. if err != nil {
  123. return nil, err
  124. }
  125. return &goodsType, nil
  126. }
  127. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  128. err := writeDb.Create(&goodType).Error
  129. return err, goodType
  130. }
  131. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  132. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  133. "modifier": goodType.Modifier,
  134. "mtime": time.Now().Unix(),
  135. "remark": goodType.Remark,
  136. "type_name": goodType.TypeName,
  137. "out_stock": goodType.OutStock,
  138. "stock_attribute": goodType.StockAttribute,
  139. }).Error
  140. return err, goodType
  141. }
  142. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  143. offset := (page - 1) * limit
  144. db := readDb.Model(&models.GoodsType{})
  145. db = db.Where("org_id=? AND status = 1", orgId)
  146. if len(keyword) > 0 {
  147. likeKey := "%" + keyword + "%"
  148. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  149. }
  150. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  151. return
  152. }
  153. func DeleteGoodTypeById(id int64, operateId int64) error {
  154. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  155. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  156. return err
  157. }
  158. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  159. goodType := &models.GoodsType{}
  160. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  161. return goodType, err
  162. }
  163. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  164. db := readDb.Model(&models.GoodInfo{})
  165. db = db.Where("org_id = ?", orgId).Count(&total)
  166. return
  167. }
  168. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  169. err := writeDb.Create(&goodInfo).Error
  170. return err, goodInfo
  171. }
  172. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  173. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  174. "good_type_id": goodInfo.GoodTypeId,
  175. "modifier": goodInfo.Modifier,
  176. "mtime": time.Now().Unix(),
  177. "remark": goodInfo.Remark,
  178. "specification_name": goodInfo.SpecificationName,
  179. "good_unit": goodInfo.GoodUnit,
  180. "buy_price": goodInfo.BuyPrice,
  181. "sell_price": goodInfo.SellPrice,
  182. "manufacturer": goodInfo.Manufacturer,
  183. "dealer": goodInfo.Dealer,
  184. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  185. "stock_warn_count": goodInfo.StockWarnCount,
  186. "is_reuse": goodInfo.IsReuse,
  187. "good_name": goodInfo.GoodName,
  188. "pinyin": goodInfo.Pinyin,
  189. "wubi": goodInfo.Wubi,
  190. "good_kind": goodInfo.GoodKind,
  191. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  192. "retail_price": goodInfo.PackingPrice,
  193. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  194. "is_special_diseases": goodInfo.IsSpecialDiseases,
  195. "is_record": goodInfo.IsRecord,
  196. "statistics_category": goodInfo.StatisticsCategory,
  197. "good_status": goodInfo.GoodStatus,
  198. "default_count": goodInfo.DefaultCount,
  199. "sign": goodInfo.Sign,
  200. "is_default": goodInfo.IsDefault,
  201. "is_charge_use": goodInfo.IsChargeUse,
  202. "is_charge_predict": goodInfo.IsChargePredict,
  203. "is_statistics_work": goodInfo.IsStatisticsWork,
  204. "sort": goodInfo.Sort,
  205. "is_doctor_use": goodInfo.IsDoctorUse,
  206. "agent": goodInfo.Agent,
  207. "good_number": goodInfo.GoodNumber,
  208. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  209. "special_medical": goodInfo.SpecialMedical,
  210. "production_type": goodInfo.ProductionType,
  211. "min_number": goodInfo.MinNumber,
  212. "packing_unit": goodInfo.PackingUnit,
  213. "packing_price": goodInfo.PackingPrice,
  214. "default_count_unit": goodInfo.DefaultCountUnit,
  215. "min_unit": goodInfo.MinUnit,
  216. "register_number": goodInfo.RegisterNumber,
  217. }).Error
  218. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  219. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  220. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  221. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  222. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  223. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  224. return err, goodInfo
  225. }
  226. func FindGoodInfoList(orgId int64, page int64, limit int64, keyword string, is_use int64, is_charge int64, good_kind int64, is_mark int64, manufacturer int64, is_type int64) (list []*models.GoodInfo, total int64, err error) {
  227. offset := (page - 1) * limit
  228. db := readDb.Model(&models.GoodInfo{})
  229. db = db.Where("org_id = ? AND status = 1", orgId)
  230. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  231. if len(keyword) > 0 {
  232. likeKey := "%" + keyword + "%"
  233. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  234. if is_use > 0 {
  235. if is_use == 2 {
  236. db = db.Where("find_in_set('停用',good_status) = 0")
  237. } else {
  238. db = db.Where("find_in_set('停用',good_status) > 0")
  239. }
  240. }
  241. if is_charge > 0 {
  242. if is_charge == 1 {
  243. db = db.Where("find_in_set('收费',good_status) > 0")
  244. } else {
  245. db = db.Where("find_in_set('收费',good_status) = 0")
  246. }
  247. }
  248. if good_kind > 0 {
  249. db = db.Where(" good_kind = ?", good_kind)
  250. }
  251. if is_mark > 0 {
  252. db = db.Where("is_mark = ?", is_mark)
  253. }
  254. if manufacturer > 0 {
  255. db = db.Where("manufacturer = ?", manufacturer)
  256. }
  257. if is_type > 0 {
  258. db = db.Where("good_type_id = ?", is_type)
  259. }
  260. } else {
  261. if is_use > 0 {
  262. if is_use == 2 {
  263. db = db.Where("find_in_set('停用',good_status) = 0")
  264. } else {
  265. db = db.Where("find_in_set('停用',good_status) > 0")
  266. }
  267. }
  268. if is_charge > 0 {
  269. if is_charge == 1 {
  270. db = db.Where("find_in_set('收费',good_status) > 0")
  271. } else {
  272. db = db.Where("find_in_set('收费',good_status) = 0")
  273. }
  274. }
  275. if good_kind > 0 {
  276. db = db.Where(" good_kind = ?", good_kind)
  277. }
  278. if is_mark > 0 {
  279. db = db.Where("is_mark = ?", is_mark)
  280. }
  281. if manufacturer > 0 {
  282. db = db.Where("manufacturer = ?", manufacturer)
  283. }
  284. if is_type > 0 {
  285. db = db.Where("good_type_id = ?", is_type)
  286. }
  287. }
  288. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  289. return
  290. }
  291. func DeleteGoodInfoById(id int64, operateId int64) error {
  292. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  293. return err
  294. }
  295. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  296. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  297. return list, err
  298. }
  299. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  300. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  301. return list, err
  302. }
  303. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  304. goodInfo := &models.GoodInfo{}
  305. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  306. return goodInfo, err
  307. }
  308. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  309. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  310. return goodType, err
  311. }
  312. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  313. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  314. return goodType, err
  315. }
  316. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  317. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  318. return goodInfo, err
  319. }
  320. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  321. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  322. return total, err
  323. }
  324. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  325. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  326. return total, err
  327. }
  328. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  329. err := writeDb.Create(&warehouse).Error
  330. return err
  331. }
  332. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  333. err := writeDb.Create(&warehouse).Error
  334. return err
  335. }
  336. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  337. err := writeDb.Create(&salesReturn).Error
  338. return err
  339. }
  340. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  341. err := writeDb.Create(&salesReturn).Error
  342. return err
  343. }
  344. func UpDateGoodReturnStatus(ids []string) {
  345. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  346. }
  347. func UpDateCanCelStockStatus(ids []string) {
  348. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  349. }
  350. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  351. if len(warehousingInfo) > 0 {
  352. utx := writeDb.Begin()
  353. if len(warehousingInfo) > 0 {
  354. 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,stock_count,license_number) VALUES "
  355. insertParams := make([]string, 0)
  356. insertData := make([]interface{}, 0)
  357. for _, info := range warehousingInfo {
  358. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  359. insertData = append(insertData, info.WarehousingId)
  360. insertData = append(insertData, info.GoodId)
  361. insertData = append(insertData, info.GoodTypeId)
  362. insertData = append(insertData, info.Number)
  363. insertData = append(insertData, info.ProductDate)
  364. insertData = append(insertData, info.ExpiryDate)
  365. insertData = append(insertData, info.WarehousingCount)
  366. insertData = append(insertData, info.Price)
  367. insertData = append(insertData, info.TotalPrice)
  368. insertData = append(insertData, info.Dealer)
  369. insertData = append(insertData, info.Manufacturer)
  370. insertData = append(insertData, info.Remark)
  371. insertData = append(insertData, info.Ctime)
  372. insertData = append(insertData, info.Mtime)
  373. insertData = append(insertData, info.Status)
  374. insertData = append(insertData, info.OrgId)
  375. insertData = append(insertData, info.WarehousingOrder)
  376. insertData = append(insertData, info.Type)
  377. insertData = append(insertData, info.WarehousingCount)
  378. insertData = append(insertData, info.LicenseNumber)
  379. }
  380. thisSQL += strings.Join(insertParams, ", ")
  381. err = utx.Exec(thisSQL, insertData...).Error
  382. if err != nil {
  383. utx.Rollback()
  384. return
  385. }
  386. }
  387. utx.Commit()
  388. }
  389. return
  390. }
  391. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  392. if len(warehousingInfo) > 0 {
  393. utx := writeDb.Begin()
  394. if len(warehousingInfo) > 0 {
  395. 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,stock_max_number,max_unit,min_unit,batch_number,stock_min_number) VALUES "
  396. insertParams := make([]string, 0)
  397. insertData := make([]interface{}, 0)
  398. for _, info := range warehousingInfo {
  399. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  400. insertData = append(insertData, info.WarehousingId)
  401. insertData = append(insertData, info.DrugId)
  402. insertData = append(insertData, info.Number)
  403. insertData = append(insertData, info.ProductDate)
  404. insertData = append(insertData, info.ExpiryDate)
  405. insertData = append(insertData, info.WarehousingCount)
  406. insertData = append(insertData, info.Price)
  407. insertData = append(insertData, info.TotalPrice)
  408. insertData = append(insertData, info.Dealer)
  409. insertData = append(insertData, info.Manufacturer)
  410. insertData = append(insertData, info.Remark)
  411. insertData = append(insertData, info.Ctime)
  412. insertData = append(insertData, info.Mtime)
  413. insertData = append(insertData, info.Status)
  414. insertData = append(insertData, info.OrgId)
  415. insertData = append(insertData, info.WarehousingOrder)
  416. insertData = append(insertData, info.Type)
  417. insertData = append(insertData, info.RetailPrice)
  418. insertData = append(insertData, info.RetailTotalPrice)
  419. insertData = append(insertData, info.StockMaxNumber)
  420. insertData = append(insertData, info.MaxUnit)
  421. insertData = append(insertData, info.MinUnit)
  422. insertData = append(insertData, info.BatchNumber)
  423. insertData = append(insertData, info.StockMinNumber)
  424. }
  425. thisSQL += strings.Join(insertParams, ", ")
  426. err = utx.Exec(thisSQL, insertData...).Error
  427. if err != nil {
  428. utx.Rollback()
  429. return
  430. }
  431. }
  432. utx.Commit()
  433. }
  434. return
  435. }
  436. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  437. var total float64
  438. var min_number int64
  439. for _, item := range warehousingInfo {
  440. //查询刚入库耗材的库存
  441. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  442. //更新药品库的总量
  443. info, _ := FindeDrugInfo(item.DrugId)
  444. min_number = count.StockMaxNumber * info.MinNumber
  445. str := strconv.FormatInt(min_number, 10)
  446. minFloat, _ := strconv.ParseFloat(str, 64)
  447. total = info.Total + minFloat
  448. lib := models.BaseDrugLib{
  449. Total: total,
  450. }
  451. UpdateBaseDrug(&lib, item.ID)
  452. }
  453. return err
  454. }
  455. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  456. info := models.XtDrugWarehouseInfo{}
  457. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  458. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  459. return info, err
  460. }
  461. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  462. lib := models.BaseDrugLib{}
  463. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  464. return lib, err
  465. }
  466. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  467. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  468. return err
  469. }
  470. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  471. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  472. return err
  473. }
  474. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  475. info := models.DrugWarehouseInfo{}
  476. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  477. return info, err
  478. }
  479. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  480. out := models.DrugWarehouseOut{}
  481. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  482. return out, err
  483. }
  484. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  485. if len(drugflow) > 0 {
  486. utx := writeDb.Begin()
  487. if len(drugflow) > 0 {
  488. thisSQL := "INSERT INTO xt_drug_flow (warehousing_id, drug_id, number,batch_number,count,user_org_id,patient_id,system_time,consumable_type,is_sys,warehousing_order,warehouse_out_id,warehouse_out_order_number,is_edit,cancel_stock_id,cancel_order_number,manufacturer,dealer,creator,update_creator,status,ctime,mtime,price,warehousing_detail_id,warehouse_out_detail_id,cancel_out_detail_id,expire_date,product_date,max_unit,min_unit) VALUES "
  489. insertParams := make([]string, 0)
  490. insertData := make([]interface{}, 0)
  491. for _, info := range drugflow {
  492. fmt.Println("hhhhhh", info.WarehouseOutId)
  493. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  494. insertData = append(insertData, info.WarehousingId)
  495. insertData = append(insertData, info.DrugId)
  496. insertData = append(insertData, info.Number)
  497. insertData = append(insertData, info.BatchNumber)
  498. insertData = append(insertData, info.Count)
  499. insertData = append(insertData, info.UserOrgId)
  500. insertData = append(insertData, info.PatientId)
  501. insertData = append(insertData, info.SystemTime)
  502. insertData = append(insertData, info.ConsumableType)
  503. insertData = append(insertData, info.IsSys)
  504. insertData = append(insertData, info.WarehousingOrder)
  505. insertData = append(insertData, info.WarehouseOutId)
  506. insertData = append(insertData, info.WarehouseOutOrderNumber)
  507. insertData = append(insertData, info.IsEdit)
  508. insertData = append(insertData, info.CancelStockId)
  509. insertData = append(insertData, info.CancelOrderNumber)
  510. insertData = append(insertData, info.Manufacturer)
  511. insertData = append(insertData, info.Dealer)
  512. insertData = append(insertData, info.Creator)
  513. insertData = append(insertData, info.UpdateCreator)
  514. insertData = append(insertData, info.Status)
  515. insertData = append(insertData, info.Ctime)
  516. insertData = append(insertData, info.Mtime)
  517. insertData = append(insertData, info.Price)
  518. insertData = append(insertData, info.WarehousingDetailId)
  519. insertData = append(insertData, info.WarehouseOutDetailId)
  520. insertData = append(insertData, info.CancelOutDetailId)
  521. insertData = append(insertData, info.ExpireDate)
  522. insertData = append(insertData, info.ProductDate)
  523. insertData = append(insertData, info.MaxUnit)
  524. insertData = append(insertData, info.MinUnit)
  525. }
  526. thisSQL += strings.Join(insertParams, ", ")
  527. err = utx.Exec(thisSQL, insertData...).Error
  528. if err != nil {
  529. utx.Rollback()
  530. return
  531. }
  532. }
  533. utx.Commit()
  534. }
  535. return
  536. }
  537. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  538. info := models.DrugFlow{}
  539. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  540. if err == gorm.ErrRecordNotFound {
  541. return nil, err
  542. }
  543. if err != nil {
  544. return nil, err
  545. }
  546. return &info, nil
  547. }
  548. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  549. info := models.XtDrugWarehouseInfo{}
  550. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  551. return info, err
  552. }
  553. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  554. warehouseInfo := models.XtDrugWarehouseInfo{}
  555. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  556. return err
  557. }
  558. func CreateDrugFlowOne(flow models.DrugFlow) error {
  559. err := XTWriteDB().Create(&flow).Error
  560. return err
  561. }
  562. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  563. detail := models.DrugAutomaticReduceDetail{}
  564. err := XTWriteDB().Model(&detail).Where("patient_id = ? and record_time = ? and drug_id = ? and status= 1 and advice_id =?", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  565. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("patient_id =? and sys_record_time =? and drug_id = ? and status = 1 and advice_id =?", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  566. return detail, err
  567. }
  568. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  569. flow := models.DrugFlow{}
  570. err := XTWriteDB().Model(&flow).Where("patient_id =? and warehouse_out_order_number = ? and drug_id = ? and status = 1 and advice_id =?", patientid, warehouse_out_number, drug_id, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  571. return flow, err
  572. }
  573. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  574. err := XTWriteDB().Create(&flow).Error
  575. return err
  576. }
  577. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  578. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  579. return info, err
  580. }
  581. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  582. info := models.WarehousingInfo{}
  583. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  584. return info, err
  585. }
  586. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  587. err := XTWriteDB().Create(&info).Error
  588. return err
  589. }
  590. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64) (info models.WarehousingInfo, err error) {
  591. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0", good_id, good_type_id).Order("ctime").First(&info).Error
  592. return info, err
  593. }
  594. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64) (info models.VmWarehousingInfo, err error) {
  595. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  596. if good_id > 0 {
  597. db = db.Where("x.good_id = ?", good_id)
  598. }
  599. if good_type_id > 0 {
  600. db = db.Where("x.good_type_id = ?", good_type_id)
  601. }
  602. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  603. return info, err
  604. }
  605. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  606. info := models.WarehousingInfo{}
  607. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  608. return info, err
  609. }
  610. func FindWarehousingInfoTen(goodId int64) (models.WarehousingInfo, error) {
  611. info := models.WarehousingInfo{}
  612. err := XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1", goodId).Find(&info).Error
  613. return info, err
  614. }
  615. //药品先进先出,找出最先入库的批次
  616. func FindLastDrugWarehousingInfoByID(drug_id int64) (info models.XtDrugWarehouseInfo, err error) {
  617. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) ", drug_id).Order("ctime").First(&info).Error
  618. return info, err
  619. }
  620. // 出库时先进先出,找出最先入库的批次
  621. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  622. err = readDb.Model(&models.WarehousingInfo{}).Where("drup_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) ", drug_id).Order("ctime").First(&info).Error
  623. return info, err
  624. }
  625. //
  626. //// 出库时先进先出,找出最先入库的批次
  627. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  628. // err = readDb.Model(&models.WarehousingInfo{}).Where("drup_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) ", drug_id).Order("ctime").First(&info).Error
  629. // return info, err
  630. //}
  631. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  632. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  633. return info, err
  634. }
  635. func FindAllWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*models.Warehousing, total int64, err error) {
  636. db := readDb.Model(&models.Warehousing{})
  637. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  638. if len(keywords) > 0 {
  639. likeKey := "%" + keywords + "%"
  640. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  641. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse.warehousing_order LIKE ? or xt_warehouse.id in(?)", likeKey, likeKey, ids).Group("xt_warehouse.id")
  642. }
  643. if startTime > 0 {
  644. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  645. }
  646. if endTime > 0 {
  647. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  648. }
  649. db = db.Count(&total)
  650. offset := (page - 1) * limit
  651. err = db.Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  652. return
  653. }
  654. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  655. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  656. if orgid > 0 {
  657. db = db.Where("org_id = ?", orgid)
  658. }
  659. if len(keyword) > 0 {
  660. likeKey := "%" + keyword + "%"
  661. db = db.Where("good_name like ?", likeKey)
  662. }
  663. err = db.Find(&goodInfo).Error
  664. return goodInfo, err
  665. }
  666. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  667. db := XTReadDB().Model(&info).Where("status =1")
  668. if len(good_id) > 0 {
  669. db = db.Where("good_id in (?)", good_id)
  670. }
  671. if startime > 0 {
  672. db = db.Where("ctime >=?", startime)
  673. }
  674. if endtime > 0 {
  675. db = db.Where("ctime<=?", endtime)
  676. }
  677. if orgid > 0 {
  678. db = db.Where("org_id = ?", orgid)
  679. }
  680. err = db.Find(&info).Error
  681. return info, err
  682. }
  683. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64) (info []*models.WarehouseOutInfo, err error) {
  684. db := XTReadDB().Model(&info).Where("status =1")
  685. if len(good_id) > 0 {
  686. db = db.Where("good_id in (?)", good_id)
  687. }
  688. if startime > 0 {
  689. db = db.Where("ctime >=?", startime)
  690. }
  691. if endtime > 0 {
  692. db = db.Where("ctime<=?", endtime)
  693. }
  694. if orgid > 0 {
  695. db = db.Where("org_id = ?", orgid)
  696. }
  697. err = db.Find(&info).Error
  698. return info, err
  699. }
  700. type VMDrugWarehouse struct {
  701. ID int64 `gorm:"column:id" json:"id" form:"id"`
  702. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  703. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  704. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  705. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  706. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  707. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  708. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  709. Status int64 `gorm:"column:status" json:"status" form:"status"`
  710. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  711. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  712. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  713. Type int64 `gorm:"column:type" json:"type" form:"type"`
  714. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  715. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  716. }
  717. func (VMDrugWarehouse) TableName() string {
  718. return "xt_drug_warehouse"
  719. }
  720. type VMDrugWarehouseInfo struct {
  721. ID int64 `gorm:"column:id" json:"id" form:"id"`
  722. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  723. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  724. Number string `gorm:"column:number" json:"number" form:"number"`
  725. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  726. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  727. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  728. Price float64 `gorm:"column:price" json:"price" form:"price"`
  729. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  730. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  731. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  732. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  733. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  734. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  735. Status int64 `gorm:"column:status" json:"status" form:"status"`
  736. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  737. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  738. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  739. Type int64 `gorm:"column:type" json:"type" form:"type"`
  740. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  741. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  742. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  743. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  744. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  745. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  746. }
  747. func (VMDrugWarehouseInfo) TableName() string {
  748. return "xt_drug_warehouse_info"
  749. }
  750. type Drug struct {
  751. ID int64 `gorm:"column:id" json:"id" form:"id"`
  752. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  753. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  754. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  755. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  756. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  757. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  758. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  759. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  760. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  761. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  762. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  763. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  764. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  765. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  766. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  767. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  768. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  769. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  770. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  771. Number string `gorm:"column:number" json:"number" form:"number"`
  772. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  773. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  774. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  775. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  776. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  777. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  778. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  779. Code string `gorm:"column:code" json:"code" form:"code"`
  780. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  781. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  782. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  783. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  784. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  785. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  786. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  787. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  788. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  789. Label int64 `gorm:"column:label" json:"label" form:"label"`
  790. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  791. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  792. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  793. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  794. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  795. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  796. Status int64 `gorm:"column:status" json:"status" form:"status"`
  797. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  798. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  799. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  800. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  801. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  802. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  803. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  804. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  805. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  806. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  807. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  808. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  809. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  810. Total float64 `gorm:"column:total" json:"total" form:"total"`
  811. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  812. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  813. Dose float64 `gorm:"column:dose" json:"dose" form:"dose"`
  814. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  815. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  816. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  817. }
  818. func (Drug) TableName() string {
  819. return "xt_base_drug"
  820. }
  821. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  822. db := readDb.Model(&VMDrugWarehouse{})
  823. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  824. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  825. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  826. if len(keywords) > 0 {
  827. likeKey := "%" + keywords + "%"
  828. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  829. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  830. 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")
  831. }
  832. if startTime > 0 {
  833. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  834. }
  835. if endTime > 0 {
  836. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  837. }
  838. db = db.Count(&total)
  839. offset := (page - 1) * limit
  840. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  841. return
  842. }
  843. func FindAllDrugWarehousingListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*VMDrugWarehouse, total int64, err error) {
  844. db := readDb.Model(&VMDrugWarehouse{})
  845. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  846. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  847. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  848. if len(keywords) > 0 {
  849. likeKey := "%" + keywords + "%"
  850. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  851. db = db.Where(" sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse.warehousing_order LIKE ? OR xt_drug_warehouse.id in(?)", likeKey, likeKey, ids).Group("xt_drug_warehouse.id")
  852. }
  853. if startTime > 0 {
  854. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  855. }
  856. if endTime > 0 {
  857. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  858. }
  859. db = db.Count(&total)
  860. offset := (page - 1) * limit
  861. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  862. return
  863. }
  864. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  865. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  866. return list, err
  867. }
  868. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  869. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  870. return list, err
  871. }
  872. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  873. 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
  874. return list, err
  875. }
  876. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  877. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  878. return list, err
  879. }
  880. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  881. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  882. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  883. fmt.Println(table)
  884. if len(id) > 0 {
  885. db = db.Where("x.warehousing_id in(?)", id)
  886. }
  887. if org_id > 0 {
  888. db = db.Where("x.org_id = ?", org_id)
  889. }
  890. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.price,x.remark,x.batch_number,x.batch_number,t.manufacturer,t.dealer,t.drug_name,t.last_price,t.dose_unit,t.dose,t.min_number,t.min_unit,t.max_unit,t.drug_type").Joins("left join xt_base_drug as t on t.id = x.drug_id").Scan(&list).Error
  891. return list, err
  892. }
  893. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  894. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  895. return warehousing, err
  896. }
  897. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  898. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  899. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  900. return warehousing, err
  901. }
  902. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  903. 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
  904. return warehousing, err
  905. }
  906. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  907. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  908. if len(id) > 0 {
  909. db = db.Where("x.id in(?)", id)
  910. }
  911. if org_id > 0 {
  912. db = db.Where("x.org_id = ?", org_id)
  913. }
  914. err = db.Select("x.id,x.warehousing_order,x.operation_time,x.org_id,x.creater,x.ctime,x.modifier,x.warehousing_time").Find(&warehousing).Error
  915. return warehousing, err
  916. }
  917. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  918. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  919. // return warehousing, err
  920. //
  921. //}
  922. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  923. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  924. return
  925. }
  926. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  927. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  928. return
  929. }
  930. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  931. if len(salesReturnInfo) > 0 {
  932. utx := writeDb.Begin()
  933. if len(salesReturnInfo) > 0 {
  934. 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 "
  935. insertParams := make([]string, 0)
  936. insertData := make([]interface{}, 0)
  937. for _, info := range salesReturnInfo {
  938. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  939. insertData = append(insertData, info.GoodId)
  940. insertData = append(insertData, info.SalesReturnId)
  941. insertData = append(insertData, info.GoodTypeId)
  942. insertData = append(insertData, info.Count)
  943. insertData = append(insertData, info.Price)
  944. insertData = append(insertData, info.Total)
  945. insertData = append(insertData, info.Ctime)
  946. insertData = append(insertData, info.Status)
  947. insertData = append(insertData, info.OrgId)
  948. insertData = append(insertData, info.OrderNumber)
  949. insertData = append(insertData, info.Type)
  950. insertData = append(insertData, info.Dealer)
  951. insertData = append(insertData, info.Manufacturer)
  952. }
  953. thisSQL += strings.Join(insertParams, ", ")
  954. err = utx.Exec(thisSQL, insertData...).Error
  955. if err != nil {
  956. utx.Rollback()
  957. return
  958. }
  959. }
  960. utx.Commit()
  961. }
  962. return
  963. }
  964. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  965. if len(salesReturnInfo) > 0 {
  966. utx := writeDb.Begin()
  967. if len(salesReturnInfo) > 0 {
  968. 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 "
  969. insertParams := make([]string, 0)
  970. insertData := make([]interface{}, 0)
  971. for _, info := range salesReturnInfo {
  972. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  973. insertData = append(insertData, info.DrugId)
  974. insertData = append(insertData, info.SalesReturnId)
  975. insertData = append(insertData, info.Count)
  976. insertData = append(insertData, info.Price)
  977. insertData = append(insertData, info.Total)
  978. insertData = append(insertData, info.Ctime)
  979. insertData = append(insertData, info.Status)
  980. insertData = append(insertData, info.OrgId)
  981. insertData = append(insertData, info.OrderNumber)
  982. insertData = append(insertData, info.Type)
  983. insertData = append(insertData, info.Dealer)
  984. insertData = append(insertData, info.Manufacturer)
  985. insertData = append(insertData, info.RetailPrice)
  986. insertData = append(insertData, info.RetailTotalPrice)
  987. }
  988. thisSQL += strings.Join(insertParams, ", ")
  989. err = utx.Exec(thisSQL, insertData...).Error
  990. if err != nil {
  991. utx.Rollback()
  992. return
  993. }
  994. }
  995. utx.Commit()
  996. }
  997. return
  998. }
  999. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1000. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1001. return total, err
  1002. }
  1003. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1004. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1005. return total, err
  1006. }
  1007. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1008. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1009. return total, err
  1010. }
  1011. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1012. db := readDb.Model(&models.SalesReturn{})
  1013. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1014. if len(keywords) > 0 {
  1015. likeKey := "%" + keywords + "%"
  1016. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1017. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1018. 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")
  1019. }
  1020. if startTime > 0 {
  1021. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1022. }
  1023. if endTime > 0 {
  1024. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1025. }
  1026. db = db.Count(&total)
  1027. offset := (page - 1) * limit
  1028. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1029. return
  1030. }
  1031. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1032. db := readDb.Model(&models.DrugSalesReturn{})
  1033. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1034. if len(keywords) > 0 {
  1035. likeKey := "%" + keywords + "%"
  1036. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1037. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1038. 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")
  1039. }
  1040. if startTime > 0 {
  1041. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1042. }
  1043. if endTime > 0 {
  1044. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1045. }
  1046. db = db.Count(&total)
  1047. offset := (page - 1) * limit
  1048. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1049. return
  1050. }
  1051. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1052. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1053. return list, err
  1054. }
  1055. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1056. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1057. return list, err
  1058. }
  1059. type DrugCancelStockInfo struct {
  1060. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1061. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1062. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1063. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1064. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1065. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1066. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1067. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1068. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1069. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1070. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1071. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1072. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1073. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1074. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1075. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1076. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1077. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1078. Number string `gorm:"column:number" json:"number" form:"number"`
  1079. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1080. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1081. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1082. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1083. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1084. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1085. }
  1086. func (DrugCancelStockInfo) TableName() string {
  1087. return "xt_drug_cancel_stock_info"
  1088. }
  1089. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1090. err = readDb.Model(&DrugCancelStockInfo{}).Preload("Drug", "status = 1 AND org_id = ? ", org_id).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  1091. return list, err
  1092. }
  1093. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1094. db := readDb.Model(&models.WarehousingInfo{})
  1095. db = db.Where("org_id = ? AND status = 1", orgId)
  1096. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1097. if startTime > 0 {
  1098. db = db.Where("ctime >=?", startTime)
  1099. }
  1100. if endTime > 0 {
  1101. db = db.Where("ctime<= ?", endTime)
  1102. }
  1103. db = db.Count(&total)
  1104. offset := (page - 1) * limit
  1105. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1106. return list, total, err
  1107. }
  1108. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1109. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1110. return total, err
  1111. }
  1112. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1113. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1114. return total, err
  1115. }
  1116. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1117. err := writeDb.Create(&warehouseOut).Error
  1118. return err
  1119. }
  1120. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1121. err := writeDb.Create(&warehouseOut).Error
  1122. return err
  1123. }
  1124. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1125. if len(warehouseOutInfo) > 0 {
  1126. utx := writeDb.Begin()
  1127. if len(warehouseOutInfo) > 0 {
  1128. 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,number,license_number) VALUES "
  1129. insertParams := make([]string, 0)
  1130. insertData := make([]interface{}, 0)
  1131. for _, info := range warehouseOutInfo {
  1132. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1133. insertData = append(insertData, info.WarehouseOutId)
  1134. insertData = append(insertData, info.GoodId)
  1135. insertData = append(insertData, info.GoodTypeId)
  1136. insertData = append(insertData, info.ProductDate)
  1137. insertData = append(insertData, info.ExpiryDate)
  1138. insertData = append(insertData, info.Count)
  1139. insertData = append(insertData, info.Price)
  1140. insertData = append(insertData, info.TotalPrice)
  1141. insertData = append(insertData, info.Remark)
  1142. insertData = append(insertData, info.Ctime)
  1143. insertData = append(insertData, info.Status)
  1144. insertData = append(insertData, info.OrgId)
  1145. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1146. insertData = append(insertData, info.Type)
  1147. insertData = append(insertData, info.Dealer)
  1148. insertData = append(insertData, info.Manufacturer)
  1149. insertData = append(insertData, info.Number)
  1150. insertData = append(insertData, info.LicenseNumber)
  1151. }
  1152. thisSQL += strings.Join(insertParams, ", ")
  1153. err = utx.Exec(thisSQL, insertData...).Error
  1154. if err != nil {
  1155. utx.Rollback()
  1156. return
  1157. }
  1158. }
  1159. utx.Commit()
  1160. }
  1161. return
  1162. }
  1163. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1164. if len(warehouseOutInfo) > 0 {
  1165. utx := writeDb.Begin()
  1166. if len(warehouseOutInfo) > 0 {
  1167. 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,number,batch_number,count_unit) VALUES "
  1168. insertParams := make([]string, 0)
  1169. insertData := make([]interface{}, 0)
  1170. for _, info := range warehouseOutInfo {
  1171. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1172. insertData = append(insertData, info.WarehouseOutId)
  1173. insertData = append(insertData, info.DrugId)
  1174. insertData = append(insertData, info.ProductDate)
  1175. insertData = append(insertData, info.ExpiryDate)
  1176. insertData = append(insertData, info.Count)
  1177. insertData = append(insertData, info.Price)
  1178. insertData = append(insertData, info.TotalPrice)
  1179. insertData = append(insertData, info.Remark)
  1180. insertData = append(insertData, info.Ctime)
  1181. insertData = append(insertData, info.Status)
  1182. insertData = append(insertData, info.OrgId)
  1183. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1184. insertData = append(insertData, info.Type)
  1185. insertData = append(insertData, info.Dealer)
  1186. insertData = append(insertData, info.Manufacturer)
  1187. insertData = append(insertData, info.RetailPrice)
  1188. insertData = append(insertData, info.RetailTotalPrice)
  1189. insertData = append(insertData, info.Number)
  1190. insertData = append(insertData, info.BatchNumber)
  1191. insertData = append(insertData, info.CountUnit)
  1192. }
  1193. thisSQL += strings.Join(insertParams, ", ")
  1194. err = utx.Exec(thisSQL, insertData...).Error
  1195. if err != nil {
  1196. utx.Rollback()
  1197. return
  1198. }
  1199. }
  1200. utx.Commit()
  1201. }
  1202. return
  1203. }
  1204. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1205. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1206. return info, err
  1207. }
  1208. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1209. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1210. return info, err
  1211. }
  1212. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1213. db := readDb.Model(&models.WarehouseOut{})
  1214. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1215. if len(keywords) > 0 {
  1216. likeKey := "%" + keywords + "%"
  1217. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1218. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1219. 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")
  1220. }
  1221. if startTime > 0 {
  1222. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1223. }
  1224. if endTime > 0 {
  1225. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1226. }
  1227. db = db.Count(&total)
  1228. offset := (page - 1) * limit
  1229. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1230. return list, total, err
  1231. }
  1232. func FindAllWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*models.WarehouseOut, total int64, err error) {
  1233. db := readDb.Model(&models.WarehouseOut{})
  1234. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1235. if len(keywords) > 0 {
  1236. likeKey := "%" + keywords + "%"
  1237. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1238. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out.warehouse_out_order_number LIKE ? or xt_warehouse_out.id in(?)", likeKey, likeKey, ids).Group("xt_warehouse_out.id")
  1239. }
  1240. if startTime > 0 {
  1241. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1242. }
  1243. if endTime > 0 {
  1244. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1245. }
  1246. db = db.Count(&total)
  1247. offset := (page - 1) * limit
  1248. if orgId == 10191 {
  1249. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1250. } else {
  1251. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1252. }
  1253. return list, total, err
  1254. }
  1255. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1256. db := readDb.Model(&models.DrugWarehouseOut{})
  1257. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1258. if len(keywords) > 0 {
  1259. likeKey := "%" + keywords + "%"
  1260. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1261. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1262. 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")
  1263. }
  1264. if startTime > 0 {
  1265. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1266. }
  1267. if endTime > 0 {
  1268. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1269. }
  1270. db = db.Count(&total)
  1271. offset := (page - 1) * limit
  1272. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1273. return list, total, err
  1274. }
  1275. func FindAllDrugWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*models.DrugWarehouseOut, total int64, err error) {
  1276. db := readDb.Model(&models.DrugWarehouseOut{})
  1277. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1278. if len(keywords) > 0 {
  1279. likeKey := "%" + keywords + "%"
  1280. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1281. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse_out.warehouse_out_order_number LIKE ? or xt_drug_warehouse_out.id in(?) ", likeKey, likeKey, ids).Group("xt_drug_warehouse_out.id")
  1282. }
  1283. if startTime > 0 {
  1284. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1285. }
  1286. if endTime > 0 {
  1287. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1288. }
  1289. db = db.Count(&total)
  1290. offset := (page - 1) * limit
  1291. if orgId == 10191 {
  1292. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1293. } else {
  1294. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1295. }
  1296. return list, total, err
  1297. }
  1298. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1299. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? AND status = 1 AND count <> 0 AND good_id <> 0", id).Preload("GoodInfo", "status = 1").Order("good_type_id desc").Find(&list).Error
  1300. return list, err
  1301. }
  1302. type DrugWarehouseOutInfo struct {
  1303. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1304. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1305. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1306. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1307. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1308. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1309. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1310. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1311. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1312. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1313. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1314. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1315. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1316. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1317. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1318. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1319. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1320. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1321. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1322. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1323. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1324. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1325. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1326. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1327. Number string `gorm:"column:number" json:"number" form:"number"`
  1328. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1329. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1330. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1331. }
  1332. func (DrugWarehouseOutInfo) TableName() string {
  1333. return "xt_drug_warehouse_out_info"
  1334. }
  1335. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1336. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1337. return list, err
  1338. }
  1339. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1340. info := &models.WarehousingInfo{}
  1341. type WarehouseInfoCountAmount struct {
  1342. Total int64
  1343. }
  1344. var result WarehouseInfoCountAmount
  1345. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1346. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1347. return result.Total, info
  1348. }
  1349. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1350. type SalesReturnCountAmount struct {
  1351. Total int64
  1352. }
  1353. var result SalesReturnCountAmount
  1354. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1355. return result.Total, err
  1356. }
  1357. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1358. type WarehouseOutInfoCountAmount struct {
  1359. Total int64
  1360. }
  1361. var result WarehouseOutInfoCountAmount
  1362. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1363. return result.Total, err
  1364. }
  1365. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1366. type CancleStockoCountAmount struct {
  1367. Total int64
  1368. }
  1369. var result CancleStockoCountAmount
  1370. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1371. return result.Total, err
  1372. }
  1373. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1374. info := models.WarehousingInfo{}
  1375. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1376. return info, err
  1377. }
  1378. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1379. stock := models.CancelStock{}
  1380. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1381. if err == gorm.ErrRecordNotFound {
  1382. return nil, err
  1383. }
  1384. if err != nil {
  1385. return nil, err
  1386. }
  1387. return &stock, nil
  1388. }
  1389. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1390. stock := models.CancelStock{}
  1391. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1392. if err == gorm.ErrRecordNotFound {
  1393. return nil, err
  1394. }
  1395. if err != nil {
  1396. return nil, err
  1397. }
  1398. return &stock, nil
  1399. }
  1400. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1401. err := writeDb.Create(&cancelStock).Error
  1402. return err
  1403. }
  1404. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  1405. stock := models.CancelStock{}
  1406. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1407. return stock, err
  1408. }
  1409. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  1410. stock := models.DrugCancelStock{}
  1411. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1412. return stock, err
  1413. }
  1414. func GetManufactureById(id int64) (models.Manufacturer, error) {
  1415. manufacturer := models.Manufacturer{}
  1416. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1417. return manufacturer, err
  1418. }
  1419. func GetDealerById(id int64) (models.Dealer, error) {
  1420. dealer := models.Dealer{}
  1421. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1422. return dealer, err
  1423. }
  1424. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1425. err := writeDb.Create(&cancelStock).Error
  1426. return err
  1427. }
  1428. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1429. if len(cancelStockInfo) > 0 {
  1430. utx := writeDb.Begin()
  1431. if len(cancelStockInfo) > 0 {
  1432. 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,number,register_account,remark,product_date,expiry_date,warehouse_info_id) VALUES "
  1433. insertParams := make([]string, 0)
  1434. insertData := make([]interface{}, 0)
  1435. for _, info := range cancelStockInfo {
  1436. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1437. insertData = append(insertData, info.GoodId)
  1438. insertData = append(insertData, info.CancelStockId)
  1439. insertData = append(insertData, info.GoodTypeId)
  1440. insertData = append(insertData, info.Count)
  1441. insertData = append(insertData, info.Price)
  1442. insertData = append(insertData, info.Total)
  1443. insertData = append(insertData, info.Ctime)
  1444. insertData = append(insertData, info.Status)
  1445. insertData = append(insertData, info.OrgId)
  1446. insertData = append(insertData, info.OrderNumber)
  1447. insertData = append(insertData, info.Type)
  1448. insertData = append(insertData, info.Dealer)
  1449. insertData = append(insertData, info.Manufacturer)
  1450. insertData = append(insertData, info.Number)
  1451. insertData = append(insertData, info.RegisterAccount)
  1452. insertData = append(insertData, info.Remark)
  1453. insertData = append(insertData, info.ProductDate)
  1454. insertData = append(insertData, info.ExpiryDate)
  1455. insertData = append(insertData, info.WarehouseInfoId)
  1456. }
  1457. thisSQL += strings.Join(insertParams, ", ")
  1458. err = utx.Exec(thisSQL, insertData...).Error
  1459. if err != nil {
  1460. utx.Rollback()
  1461. return
  1462. }
  1463. }
  1464. utx.Commit()
  1465. }
  1466. return
  1467. }
  1468. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  1469. err := XTWriteDB().Create(&info).Error
  1470. return err
  1471. }
  1472. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  1473. info := models.DrugCancelStockInfo{}
  1474. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1475. return info, err
  1476. }
  1477. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  1478. err := XTWriteDB().Create(&info).Error
  1479. return err
  1480. }
  1481. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  1482. info := models.CancelStockInfo{}
  1483. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1484. return info, err
  1485. }
  1486. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  1487. info := models.DrugCancelStockInfo{}
  1488. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  1489. return info, err
  1490. }
  1491. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1492. if len(cancelStockInfo) > 0 {
  1493. utx := writeDb.Begin()
  1494. if len(cancelStockInfo) > 0 {
  1495. 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,register_account,batch_number,remark,max_unit,product_date,expiry_date,batch_number_id) VALUES "
  1496. insertParams := make([]string, 0)
  1497. insertData := make([]interface{}, 0)
  1498. for _, info := range cancelStockInfo {
  1499. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1500. insertData = append(insertData, info.DrugId)
  1501. insertData = append(insertData, info.CancelStockId)
  1502. insertData = append(insertData, info.Count)
  1503. insertData = append(insertData, info.Price)
  1504. insertData = append(insertData, info.Total)
  1505. insertData = append(insertData, info.Ctime)
  1506. insertData = append(insertData, info.Status)
  1507. insertData = append(insertData, info.OrgId)
  1508. insertData = append(insertData, info.OrderNumber)
  1509. insertData = append(insertData, info.Type)
  1510. insertData = append(insertData, info.Dealer)
  1511. insertData = append(insertData, info.Manufacturer)
  1512. insertData = append(insertData, info.RetailPrice)
  1513. insertData = append(insertData, info.RetailTotalPrice)
  1514. insertData = append(insertData, info.RegisterAccount)
  1515. insertData = append(insertData, info.BatchNumber)
  1516. insertData = append(insertData, info.Remark)
  1517. insertData = append(insertData, info.MaxUnit)
  1518. insertData = append(insertData, info.ProductDate)
  1519. insertData = append(insertData, info.ExpiryDate)
  1520. insertData = append(insertData, info.BatchNumberId)
  1521. }
  1522. thisSQL += strings.Join(insertParams, ", ")
  1523. err = utx.Exec(thisSQL, insertData...).Error
  1524. if err != nil {
  1525. utx.Rollback()
  1526. return
  1527. }
  1528. }
  1529. utx.Commit()
  1530. }
  1531. return
  1532. }
  1533. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  1534. db := readDb.Model(&models.CancelStock{})
  1535. db = db.Where("org_id = ? AND status = 1", orgId)
  1536. err = db.Order("ctime desc").Find(&list).Error
  1537. return
  1538. }
  1539. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1540. db := readDb.Model(&models.WarehousingInfo{})
  1541. db = db.Where("org_id = ? AND status = 1", orgId)
  1542. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  1543. if startTime > 0 {
  1544. db = db.Where("ctime >=?", startTime)
  1545. }
  1546. if endTime > 0 {
  1547. db = db.Where("ctime<= ?", endTime)
  1548. }
  1549. db = db.Count(&total)
  1550. offset := (page - 1) * limit
  1551. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1552. return
  1553. }
  1554. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  1555. db := XTReadDB().Model(&models.StockInfo{})
  1556. if startime > 0 {
  1557. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1558. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  1559. })
  1560. }
  1561. if startime == 0 || endtime == 0 {
  1562. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1563. return db.Where("org_id = ? AND status = 1", orgId)
  1564. })
  1565. }
  1566. if endtime > 0 {
  1567. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1568. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  1569. })
  1570. }
  1571. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  1572. db = db.Group("xt_good_information.id")
  1573. if startime == 0 || endtime == 0 {
  1574. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1575. return db.Where("org_id = ? AND status = 1", orgId)
  1576. })
  1577. }
  1578. if startime > 0 {
  1579. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1580. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  1581. })
  1582. }
  1583. if endtime > 0 {
  1584. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1585. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  1586. })
  1587. }
  1588. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  1589. if startime == 0 || endtime == 0 {
  1590. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  1591. }
  1592. if startime > 0 {
  1593. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  1594. }
  1595. if endtime > 0 {
  1596. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  1597. }
  1598. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  1599. if len(keyword) > 0 {
  1600. likeKey := "%" + keyword + "%"
  1601. 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)
  1602. 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")
  1603. }
  1604. if type_name > 0 {
  1605. 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)
  1606. }
  1607. db = db.Count(&total)
  1608. offset := (page - 1) * limit
  1609. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1610. return
  1611. }
  1612. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  1613. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  1614. return
  1615. }
  1616. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  1617. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  1618. return
  1619. }
  1620. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  1621. 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
  1622. return
  1623. }
  1624. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  1625. 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
  1626. return
  1627. }
  1628. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  1629. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  1630. "good_id": warehouseInfo.GoodId,
  1631. "number": warehouseInfo.Number,
  1632. "product_date": warehouseInfo.ProductDate,
  1633. "expiry_date": warehouseInfo.ExpiryDate,
  1634. "warehousing_count": warehouseInfo.WarehousingCount,
  1635. "price": warehouseInfo.Price,
  1636. "total_price": warehouseInfo.TotalPrice,
  1637. "mtime": warehouseInfo.Mtime,
  1638. "remark": warehouseInfo.Remark,
  1639. }).Error
  1640. return
  1641. }
  1642. func UpDateWarehouseStatus(id int64) (err error) {
  1643. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1644. return
  1645. }
  1646. func UpdatedStockFlowById(id int64) (err error) {
  1647. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1648. return
  1649. }
  1650. func UpDateDrugWarehouseStatus(id int64) (err error) {
  1651. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1652. return
  1653. }
  1654. func UpdateDrugFlowById(id int64) (err error) {
  1655. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1656. return
  1657. }
  1658. func DeleteWarehouse(ids []string) (err error) {
  1659. ut := writeDb.Begin()
  1660. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1661. if err != nil {
  1662. ut.Rollback()
  1663. return
  1664. }
  1665. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1666. if err != nil {
  1667. ut.Rollback()
  1668. return
  1669. }
  1670. ut.Commit()
  1671. return
  1672. }
  1673. func DeleteStockFlow(ids []string) (err error) {
  1674. ut := writeDb.Begin()
  1675. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1676. if err != nil {
  1677. ut.Rollback()
  1678. return
  1679. }
  1680. ut.Commit()
  1681. return
  1682. }
  1683. func DeleteDrugWarehouse(ids []string) (err error) {
  1684. ut := writeDb.Begin()
  1685. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1686. if err != nil {
  1687. ut.Rollback()
  1688. return
  1689. }
  1690. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1691. if err != nil {
  1692. ut.Rollback()
  1693. return
  1694. }
  1695. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1696. if err != nil {
  1697. ut.Rollback()
  1698. return
  1699. }
  1700. ut.Commit()
  1701. return
  1702. }
  1703. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  1704. warehouse := &models.Warehousing{}
  1705. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  1706. return warehouse, err
  1707. }
  1708. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  1709. warehouseOut := &models.WarehouseOut{}
  1710. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  1711. return warehouseOut, err
  1712. }
  1713. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  1714. warehouseOut := &models.DrugWarehouseOut{}
  1715. 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
  1716. return warehouseOut, err
  1717. }
  1718. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  1719. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  1720. return
  1721. }
  1722. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  1723. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  1724. return
  1725. }
  1726. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  1727. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1728. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1729. return
  1730. }
  1731. func DeleteStockInRecordByReocordId(id int64) (err error) {
  1732. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1733. return
  1734. }
  1735. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  1736. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  1737. if manufacturer_id > 0 {
  1738. db = db.Where("manufacturer = ?", manufacturer_id)
  1739. }
  1740. if dealer_id > 0 {
  1741. db = db.Where("dealer = ?", dealer_id)
  1742. }
  1743. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  1744. return goodInfo, err
  1745. }
  1746. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  1747. db := readDb.Model(&models.Warehousing{})
  1748. likekey := "%" + keywords + "%"
  1749. db = db.Where("org_id = ? AND status = 1 ", orgId)
  1750. db = db.Where("warehousing_order LIKE ? ", likekey)
  1751. db = db.Count(&total)
  1752. offset := (page - 1) * limit
  1753. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1754. return
  1755. }
  1756. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  1757. info := models.WarehousingInfo{}
  1758. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  1759. return info, err
  1760. }
  1761. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  1762. info := models.WarehousingInfo{}
  1763. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  1764. return info, err
  1765. }
  1766. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  1767. err = writeDb.Save(&info).Error
  1768. return err
  1769. }
  1770. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  1771. err = writeDb.Save(&info).Error
  1772. return err
  1773. }
  1774. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  1775. err = writeDb.Model(&info).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "retail_price": info.RetailPrice}).Error
  1776. return err
  1777. }
  1778. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  1779. info := models.DrugWarehouseInfo{}
  1780. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  1781. return info, err
  1782. }
  1783. func EditWarehousing(warehouse models.Warehousing) {
  1784. 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
  1785. }
  1786. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  1787. 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
  1788. }
  1789. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  1790. 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
  1791. return list, err
  1792. }
  1793. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  1794. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  1795. 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)
  1796. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1797. return list, err
  1798. }
  1799. func DeleteSalesReturn(ids []string) (err error) {
  1800. ut := writeDb.Begin()
  1801. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1802. if err != nil {
  1803. ut.Rollback()
  1804. return
  1805. }
  1806. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1807. if err != nil {
  1808. ut.Rollback()
  1809. return
  1810. }
  1811. ut.Commit()
  1812. return
  1813. }
  1814. func DeleteDrugSalesReturn(ids []string) (err error) {
  1815. ut := writeDb.Begin()
  1816. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1817. if err != nil {
  1818. ut.Rollback()
  1819. return
  1820. }
  1821. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1822. if err != nil {
  1823. ut.Rollback()
  1824. return
  1825. }
  1826. ut.Commit()
  1827. return
  1828. }
  1829. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1830. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1831. return
  1832. }
  1833. type DrugSalesReturnInfo struct {
  1834. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1835. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1836. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  1837. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1838. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1839. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1840. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1841. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1842. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1843. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1844. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1845. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1846. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1847. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1848. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1849. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1850. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1851. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1852. Number string `gorm:"column:number" json:"number" form:"number"`
  1853. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1854. }
  1855. func (DrugSalesReturnInfo) TableName() string {
  1856. return "xt_drug_sales_return_info"
  1857. }
  1858. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  1859. 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
  1860. return
  1861. }
  1862. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  1863. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1864. return
  1865. }
  1866. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  1867. 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
  1868. return
  1869. }
  1870. func UpDateSaleReturnStatus(id int64) (err error) {
  1871. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1872. return
  1873. }
  1874. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  1875. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1876. return
  1877. }
  1878. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  1879. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1880. return salesReturn, err
  1881. }
  1882. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  1883. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1884. return salesReturn, err
  1885. }
  1886. func EditSaleReturn(sales models.SalesReturn) {
  1887. 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
  1888. }
  1889. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  1890. 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
  1891. }
  1892. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  1893. err = writeDb.Save(&info).Error
  1894. return err
  1895. }
  1896. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  1897. err = writeDb.Save(&info).Error
  1898. return err
  1899. }
  1900. func DeleteWarehouseOut(ids []string) (err error) {
  1901. ut := writeDb.Begin()
  1902. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1903. if err != nil {
  1904. ut.Rollback()
  1905. return
  1906. }
  1907. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1908. if err != nil {
  1909. ut.Rollback()
  1910. return
  1911. }
  1912. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1913. if err != nil {
  1914. ut.Rollback()
  1915. return
  1916. }
  1917. ut.Commit()
  1918. return
  1919. }
  1920. func GetWarehouseOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1921. err = XTReadDB().Where("warehouse_out_id in(?) and status = 0", ids).Find(&info).Error
  1922. return info, err
  1923. }
  1924. func UpdateWarehouseInfoById(id int64, count int64) error {
  1925. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  1926. return err
  1927. }
  1928. func GetDrugWarhouseOutByIds(ids []string) (info []*models.DrugWarehouseOutInfo, err error) {
  1929. err = XTReadDB().Where("warehouse_out_id in(?) and status = 0", ids).Find(&info).Error
  1930. return info, err
  1931. }
  1932. func UpdateDrugInfoByIds(id int64, count int64) error {
  1933. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  1934. return err
  1935. }
  1936. func UpdateDrugInfoByIdsOne(id int64, count int64) error {
  1937. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  1938. return err
  1939. }
  1940. func DeleteDrugWarehouseOut(ids []string) (err error) {
  1941. ut := writeDb.Begin()
  1942. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1943. if err != nil {
  1944. ut.Rollback()
  1945. return
  1946. }
  1947. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1948. if err != nil {
  1949. ut.Rollback()
  1950. return
  1951. }
  1952. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1953. if err != nil {
  1954. ut.Rollback()
  1955. return
  1956. }
  1957. ut.Commit()
  1958. return
  1959. }
  1960. func UpDateWarehouseOutStatus(id int64) (err error) {
  1961. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1962. return
  1963. }
  1964. func UpdateStockFlowWarehouseOutById(id int64) (err error) {
  1965. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehouse_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1966. return err
  1967. }
  1968. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  1969. ut := writeDb.Begin()
  1970. err = ut.Save(&info).Error
  1971. if err != nil {
  1972. ut.Rollback()
  1973. return err
  1974. }
  1975. ut.Commit()
  1976. return err
  1977. }
  1978. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  1979. ut := writeDb.Begin()
  1980. err = ut.Save(&info).Error
  1981. if err != nil {
  1982. ut.Rollback()
  1983. return err
  1984. }
  1985. ut.Commit()
  1986. return err
  1987. }
  1988. //func UpDateWarehouseInfoByStockDelete(id int64,count int64) (err error) {
  1989. // err = DB.Model(&models.WarehousingInfo{}).Where("id = ?",info.ID).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  1990. // return err
  1991. //}
  1992. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  1993. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1994. return
  1995. }
  1996. func DeleteDrugWarehouseOutInfoById(id int64) (err error) {
  1997. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehouse_out_detail_id = ? and satus = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1998. return
  1999. }
  2000. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  2001. 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
  2002. }
  2003. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2004. 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
  2005. }
  2006. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  2007. err = writeDb.Save(&info).Error
  2008. return err
  2009. }
  2010. func UpDateDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) (err error) {
  2011. err = writeDb.Save(&info).Error
  2012. return err
  2013. }
  2014. func UpdateDrugFlowSix(warehousoutid int64, drugid int64, warehouse_out_order_number string, flow models.DrugFlow) error {
  2015. err := XTWriteDB().Model(&flow).Where("warehouse_out_id = ? and drug_id = ? and warehouse_out_order_number = ?", warehousoutid, drugid, warehouse_out_order_number).Updates(map[string]interface{}{"count": flow.Count, "expire_date": flow.ExpireDate, "product_date": flow.ProductDate, "price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "number": flow.Number}).Error
  2016. return err
  2017. }
  2018. func FindAllCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.CancelStock, total int64, err error) {
  2019. db := readDb.Model(&models.CancelStock{})
  2020. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2021. if len(keywords) > 0 {
  2022. likeKey := "%" + keywords + "%"
  2023. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2024. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2025. 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")
  2026. }
  2027. if startTime > 0 {
  2028. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2029. }
  2030. if endTime > 0 {
  2031. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2032. }
  2033. db = db.Count(&total)
  2034. offset := (page - 1) * limit
  2035. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2036. return
  2037. }
  2038. func FindAllDrugCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugCancelStock, total int64, err error) {
  2039. db := readDb.Model(&models.DrugCancelStock{})
  2040. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2041. if len(keywords) > 0 {
  2042. likeKey := "%" + keywords + "%"
  2043. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2044. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2045. 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")
  2046. }
  2047. if startTime > 0 {
  2048. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2049. }
  2050. if endTime > 0 {
  2051. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2052. }
  2053. db = db.Count(&total)
  2054. offset := (page - 1) * limit
  2055. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2056. return
  2057. }
  2058. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2059. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2060. return info, err
  2061. }
  2062. func DeleteCancelStock(ids []string) (err error) {
  2063. ut := writeDb.Begin()
  2064. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2065. if err != nil {
  2066. ut.Rollback()
  2067. return
  2068. }
  2069. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2070. if err != nil {
  2071. ut.Rollback()
  2072. return
  2073. }
  2074. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2075. if err != nil {
  2076. ut.Rollback()
  2077. return
  2078. }
  2079. ut.Commit()
  2080. return
  2081. }
  2082. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2083. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2084. return info, err
  2085. }
  2086. func DeleteDrugCancelStock(ids []string) (err error) {
  2087. ut := writeDb.Begin()
  2088. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2089. if err != nil {
  2090. ut.Rollback()
  2091. return
  2092. }
  2093. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2094. if err != nil {
  2095. ut.Rollback()
  2096. return
  2097. }
  2098. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2099. if err != nil {
  2100. ut.Rollback()
  2101. return
  2102. }
  2103. ut.Commit()
  2104. return
  2105. }
  2106. func UpDateCancleStockStatus(id int64) (err error) {
  2107. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2108. return
  2109. }
  2110. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2111. info := models.CancelStockInfo{}
  2112. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2113. return info, err
  2114. }
  2115. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2116. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2117. return err
  2118. }
  2119. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2120. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and status = 1 and good_id = ?", id, good_id).Updates(map[string]interface{}{"status": 0}).Error
  2121. return
  2122. }
  2123. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2124. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2125. return
  2126. }
  2127. func UpDateDrugCancelStockById(id int64) (err error) {
  2128. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2129. return
  2130. }
  2131. func EditCancelStock(sales models.CancelStock) {
  2132. 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
  2133. }
  2134. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2135. 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
  2136. }
  2137. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2138. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2139. return cancelStock, err
  2140. }
  2141. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2142. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2143. return cancelStock, err
  2144. }
  2145. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2146. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2147. return cancelStock, err
  2148. }
  2149. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2150. err = writeDb.Save(&info).Error
  2151. return err
  2152. }
  2153. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2154. err = writeDb.Save(&info).Error
  2155. return err
  2156. }
  2157. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2158. 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
  2159. return list, err
  2160. }
  2161. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2162. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2163. 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)
  2164. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2165. return list, err
  2166. }
  2167. type StockDetailModel struct {
  2168. GoodId int64 `gorm:"good_id" json:"good_id"`
  2169. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2170. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2171. Price float64 `gorm:"price" json:"price"`
  2172. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2173. Type int `gorm:"type" json:"type"`
  2174. Creater int64 `gorm:"creater" json:"creater"`
  2175. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2176. Types int64 `gorm:"types" json:"types"`
  2177. }
  2178. 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) {
  2179. db := readDb.Model(&models.WarehousingInfo{})
  2180. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2181. if len(keywords) > 0 {
  2182. likeKey := "%" + keywords + "%"
  2183. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2184. 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)
  2185. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2186. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2187. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse.manufacturer")
  2188. //db = db.Where(" sgj_user_admin_role.user_name LIKE ?", likeKey).Group("xt_warehouse_info.id")
  2189. db = db.Where(" sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_info.warehousing_order LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_warehouse_info.id")
  2190. } else {
  2191. 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)
  2192. }
  2193. if manufacturer > 0 {
  2194. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2195. }
  2196. if order_type > 0 {
  2197. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2198. }
  2199. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2200. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2201. db = db.Count(&total)
  2202. offset := (page - 1) * limit
  2203. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.ctime desc").Find(&list).Error
  2204. return
  2205. }
  2206. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (err error, totalPrice float64) {
  2207. db := readDb.Model(&models.WarehousingInfo{})
  2208. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2209. if len(keywords) > 0 {
  2210. likeKey := "%" + keywords + "%"
  2211. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2212. 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)
  2213. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2214. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2215. 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")
  2216. } else {
  2217. 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)
  2218. }
  2219. if manufacturer > 0 {
  2220. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2221. }
  2222. if order_type > 0 {
  2223. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2224. }
  2225. type PriceAmount struct {
  2226. Total float64
  2227. }
  2228. var result []PriceAmount
  2229. err = db.Select("total_price as total").Scan(&result).Error
  2230. var total float64
  2231. for _, value := range result {
  2232. total = total + value.Total
  2233. }
  2234. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2235. return err, total
  2236. }
  2237. func FloatRound(f float64, n int) float64 {
  2238. format := "%." + strconv.Itoa(n) + "f"
  2239. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2240. return res
  2241. }
  2242. 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) {
  2243. db := readDb.Model(&models.WarehouseOutInfo{})
  2244. 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)
  2245. if len(keywords) > 0 {
  2246. likeKey := "%" + keywords + "%"
  2247. 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)
  2248. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2249. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2250. 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 ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_warehouse_out_info.id")
  2251. } else {
  2252. 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)
  2253. }
  2254. if manufacturer > 0 {
  2255. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2256. }
  2257. if order_type > 0 {
  2258. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2259. }
  2260. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2261. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2262. db = db.Count(&total)
  2263. offset := (page - 1) * limit
  2264. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.ctime desc").Find(&list).Error
  2265. return
  2266. }
  2267. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (err error, totalPrice float64) {
  2268. db := readDb.Model(&models.WarehouseOutInfo{})
  2269. 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)
  2270. if len(keywords) > 0 {
  2271. likeKey := "%" + keywords + "%"
  2272. 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)
  2273. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2274. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2275. 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")
  2276. } else {
  2277. 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)
  2278. }
  2279. if manufacturer > 0 {
  2280. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2281. }
  2282. if order_type > 0 {
  2283. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2284. }
  2285. type PriceAmount struct {
  2286. Total float64
  2287. }
  2288. var result []PriceAmount
  2289. err = db.Select("(count * price) as total").Scan(&result).Error
  2290. var total float64
  2291. for _, value := range result {
  2292. total = total + value.Total
  2293. }
  2294. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2295. fmt.Println(total)
  2296. return err, total
  2297. }
  2298. 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) {
  2299. db := readDb.Model(&models.SalesReturnInfo{})
  2300. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2301. if len(keywords) > 0 {
  2302. likeKey := "%" + keywords + "%"
  2303. 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)
  2304. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2305. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2306. 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 ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_sales_return_info.id")
  2307. } else {
  2308. 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)
  2309. }
  2310. if manufacturer > 0 {
  2311. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2312. }
  2313. if order_type > 0 {
  2314. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2315. }
  2316. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2317. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2318. offset := (page - 1) * limit
  2319. db = db.Count(&total)
  2320. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2321. return
  2322. }
  2323. func FindeCancelGroup(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.CancelStockInfo, err error) {
  2324. db := readDb.Model(&models.CancelStockInfo{})
  2325. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2326. if len(keywords) > 0 {
  2327. likeKey := "%" + keywords + "%"
  2328. 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)
  2329. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2330. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2331. 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")
  2332. } else {
  2333. 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)
  2334. }
  2335. if manufacturer > 0 {
  2336. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2337. }
  2338. if order_type > 0 {
  2339. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2340. }
  2341. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2342. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2343. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2344. return
  2345. }
  2346. 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) {
  2347. db := readDb.Model(&models.CancelStockInfo{})
  2348. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2349. if len(keywords) > 0 {
  2350. likeKey := "%" + keywords + "%"
  2351. 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)
  2352. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2353. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2354. 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 ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_cancel_stock_info.id")
  2355. } else {
  2356. 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)
  2357. }
  2358. if manufacturer > 0 {
  2359. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2360. }
  2361. if order_type > 0 {
  2362. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2363. }
  2364. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2365. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2366. db = db.Count(&total)
  2367. offset := (page - 1) * limit
  2368. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2369. return
  2370. }
  2371. func FindStockInByDealerId(id int64) (total int64, err error) {
  2372. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2373. return
  2374. }
  2375. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2376. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2377. return
  2378. }
  2379. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2380. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2381. return
  2382. }
  2383. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2384. 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)
  2385. return
  2386. }
  2387. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2388. readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 AND specification_name = ? AND good_type_id = ? and good_name = ?", org_id, name, good_type_id, good_name).Count(&total)
  2389. return
  2390. }
  2391. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2392. readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 AND specification_name = ? AND good_type_id = ? and id <> ? and good_name = ? ", org_id, name, good_type_id, id, goodname).Count(&total)
  2393. return
  2394. }
  2395. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2396. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2397. return goodType, err
  2398. }
  2399. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2400. 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
  2401. return info, err
  2402. }
  2403. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2404. 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
  2405. return
  2406. }
  2407. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2408. 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
  2409. return
  2410. }
  2411. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2412. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2413. return
  2414. }
  2415. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2416. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2417. return
  2418. }
  2419. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2420. info := models.WarehouseOutInfo{}
  2421. err := XTReadDB().Model(&info).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and patient_id = ? and org_id = ? and status = 1", goodid, goodtypeid, recordtime, patientid, orgid).Find(&info).Error
  2422. if err == gorm.ErrRecordNotFound {
  2423. return nil, err
  2424. }
  2425. if err != nil {
  2426. return nil, err
  2427. }
  2428. return &info, nil
  2429. }
  2430. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2431. info := models.WarehouseOutInfo{}
  2432. err := XTReadDB().Model(&info).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and org_id = ? and status = 1", goodid, goodtypeid, recordtime, orgid).Find(&info).Error
  2433. if err == gorm.ErrRecordNotFound {
  2434. return nil, err
  2435. }
  2436. if err != nil {
  2437. return nil, err
  2438. }
  2439. return &info, nil
  2440. }
  2441. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2442. err := XTWriteDB().Model(&prepare).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and patient_id = ? and org_id = ? and status =1", goodid, goodtypeid, recordtime, patientid, orgid).Updates(map[string]interface{}{"count": prepare.Count}).Error
  2443. return err
  2444. }
  2445. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2446. err := XTWriteDB().Model(&prepare).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and org_id = ? and status =1", goodid, goodtypeid, recordtime, orgid).Updates(map[string]interface{}{"count": prepare.Count}).Error
  2447. return err
  2448. }
  2449. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2450. err := writeDb.Create(&info).Error
  2451. return err
  2452. }
  2453. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  2454. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  2455. return err
  2456. }
  2457. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  2458. ut := writeDb.Begin()
  2459. err := ut.Create(&info).Error
  2460. if err != nil {
  2461. ut.Rollback()
  2462. return err
  2463. }
  2464. ut.Commit()
  2465. return err
  2466. }
  2467. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  2468. ut := writeDb.Begin()
  2469. err := ut.Create(&detail).Error
  2470. if err != nil {
  2471. ut.Rollback()
  2472. return err
  2473. }
  2474. ut.Commit()
  2475. return err
  2476. }
  2477. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  2478. err := writeDb.Create(&detail).Error
  2479. return err
  2480. }
  2481. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  2482. 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
  2483. return
  2484. }
  2485. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  2486. err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND sys_record_time = ? AND patient_id = ?", org_id, good_type_id, good_id, record_date, patient_id).Order("ctime desc").Find(&out).Error
  2487. return
  2488. }
  2489. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  2490. err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND sys_record_time = ?", org_id, good_type_id, good_id, record_date).Order("ctime desc").Find(&out).Error
  2491. return
  2492. }
  2493. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  2494. info := models.WarehouseOutInfo{}
  2495. err = readDb.Model(&info).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND sys_record_time = ? and patient_id = ?", org_id, good_type_id, good_id, record_date, patient_id).Order("ctime desc").Find(&info).Error
  2496. return info, err
  2497. }
  2498. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  2499. 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
  2500. return
  2501. }
  2502. func UpdateStockOutInfoCount(org_id int64, id int64) {
  2503. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  2504. }
  2505. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  2506. db := readDb.Model(&models.AutomaticReduceDetail{})
  2507. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  2508. db = db.Preload("GoodsType", "status = 1")
  2509. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2510. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  2511. db = db.Count(&total)
  2512. err = db.Find(&user).Error
  2513. return
  2514. }
  2515. type DrugAutomaticReduceDetail struct {
  2516. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2517. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  2518. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  2519. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  2520. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2521. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2522. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2523. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  2524. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2525. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2526. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2527. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  2528. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2529. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  2530. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  2531. }
  2532. func (DrugAutomaticReduceDetail) TableName() string {
  2533. return "xt_drug_automatic_reduce_detail"
  2534. }
  2535. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  2536. db := readDb.Model(&DrugAutomaticReduceDetail{})
  2537. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  2538. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2539. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  2540. db = db.Count(&total)
  2541. err = db.Find(&user).Error
  2542. return
  2543. }
  2544. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  2545. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  2546. }
  2547. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  2548. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  2549. }
  2550. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  2551. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("org_id = ?AND status = 1 AND good_id = ? AND good_type_id = ? AND patient_id = ? AND record_time = ? AND count <> 0", org_id, good_id, good_type_id, patient_id, record_time).Count(&count).Error
  2552. return
  2553. }
  2554. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  2555. 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
  2556. return
  2557. }
  2558. type VMWarehousingInfo struct {
  2559. ID int64 `gorm:"column:id" json:"id"`
  2560. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  2561. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  2562. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2563. Status int64 `gorm:"column:status" json:"status"`
  2564. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2565. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  2566. }
  2567. func (VMWarehousingInfo) TableName() string {
  2568. return "xt_warehouse_info"
  2569. }
  2570. type GoodInfo struct {
  2571. ID int64 `gorm:"column:id" json:"id"`
  2572. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  2573. Status int64 `gorm:"column:status" json:"status"`
  2574. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2575. }
  2576. func (GoodInfo) TableName() string {
  2577. return "xt_good_information"
  2578. }
  2579. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  2580. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  2581. return
  2582. }
  2583. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  2584. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND record_date = ? AND status = 1 AND count <> 0 ", patient_id, orgId, record_time).Find(&goodUser).Error
  2585. return
  2586. }
  2587. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  2588. likeKey := "%" + keyword + "%"
  2589. db := XTReadDB().Model(&goods)
  2590. if len(keyword) > 0 {
  2591. db = db.Where("type_name like ?", likeKey)
  2592. }
  2593. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  2594. return goods, err
  2595. }
  2596. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  2597. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3)", orgid).
  2598. Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&goodstype).Error
  2599. return goodstype, err
  2600. }
  2601. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  2602. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  2603. if orgid > 0 {
  2604. db = db.Where("x.user_org_id = ?", orgid)
  2605. }
  2606. if recordtime > 0 {
  2607. db = db.Where("x.record_date = ?", recordtime)
  2608. }
  2609. if patientId > 0 {
  2610. db = db.Where("x.patient_id = ?", patientId)
  2611. }
  2612. err = db.Select("x.id,x.user_org_id,x.patient_id,x.record_date,x.good_id,x.good_type_id,x.count,x.commdity_code,t.specification_name").Joins("left join xt_good_information as t on t.id = x.good_id and t.org_id = ? and t.status = 1", orgid).Scan(&prepare).Error
  2613. return prepare, err
  2614. }
  2615. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  2616. detail := models.XtAutomaticReduceDetail{}
  2617. err := XTReadDB().Model(&detail).Where("org_id = ? and record_time = ? and good_id = ? and good_type_id = ? and status =1", orgid, recordtime, goodid, goodtypeid).Find(&detail).Error
  2618. if err == gorm.ErrRecordNotFound {
  2619. return nil, err
  2620. }
  2621. if err != nil {
  2622. return nil, err
  2623. }
  2624. return &detail, nil
  2625. }
  2626. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  2627. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time = ? and `status` = 1) as b GROUP BY good_id", orgid, wareouttime).Scan(&autoMatic).Error
  2628. return autoMatic, err
  2629. }
  2630. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  2631. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time >= ? and x.record_time<=? and `status` = 1) as b GROUP BY good_id", orgid, startime, endtime).Scan(&autoMatic).Error
  2632. return autoMatic, err
  2633. }
  2634. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  2635. var manufacturer models.Manufacturer
  2636. var err error
  2637. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  2638. if err == gorm.ErrRecordNotFound {
  2639. return nil, err
  2640. }
  2641. if err != nil {
  2642. return nil, err
  2643. }
  2644. return &manufacturer, nil
  2645. }
  2646. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  2647. err := XTWriteDB().Create(manufacturer).Error
  2648. return err
  2649. }
  2650. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  2651. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  2652. return err
  2653. }
  2654. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  2655. var good models.GoodInfo
  2656. var err error
  2657. err = XTReadDB().Model(&good).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Find(&good).Error
  2658. if err == gorm.ErrRecordNotFound {
  2659. return nil, err
  2660. }
  2661. if err != nil {
  2662. return nil, err
  2663. }
  2664. return &good, nil
  2665. }
  2666. func CreateGoodsInfomation(info *models.GoodInfo) error {
  2667. err := XTWriteDB().Create(&info).Error
  2668. return err
  2669. }
  2670. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  2671. err := XTWriteDB().Model(&info).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Updates(map[string]interface{}{"good_name": info.GoodName, "good_type_id": info.GoodTypeId, "medical_insurance_level": info.MedicalInsuranceLevel, "good_kind": info.GoodKind, "specification_name": info.SpecificationName, "manufacturer": info.Manufacturer, "good_unit": info.GoodUnit, "retail_price": info.RetailPrice, "stock_warn_count": info.StockWarnCount, "dealer": info.Dealer, "pinyin": info.Pinyin, "wubi": info.Wubi, "buy_price": info.BuyPrice, "medical_insurance_number": info.MedicalInsuranceNumber, "is_special_diseases": info.IsSpecialDiseases, "is_record": info.IsRecord, "statistics_category": info.StatisticsCategory, "good_status": info.GoodStatus, "social_security_directory_code": info.SocialSecurityDirectoryCode, "production_type": info.ProductionType, "special_medical": info.SpecialMedical, "remark": info.Remark}).Error
  2672. return err
  2673. }
  2674. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  2675. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  2676. err = db.Select("x.good_id,Sum(x.count) as count,x.remark,x.price,x.org_id").Where("x.ctime >=? and x.ctime<=? and x.org_id = ?", startime, endtime, orgid).Group("x.good_id").Scan(&houseOutInfo).Error
  2677. return houseOutInfo, err
  2678. }
  2679. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  2680. key := "%" + keyword + "%"
  2681. if len(keyword) == 0 {
  2682. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  2683. } else {
  2684. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  2685. }
  2686. return goodInfo, err
  2687. }
  2688. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  2689. var drug models.BaseDrugLib
  2690. var err error
  2691. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  2692. if err == gorm.ErrRecordNotFound {
  2693. return nil, err
  2694. }
  2695. if err != nil {
  2696. return nil, err
  2697. }
  2698. return &drug, nil
  2699. }
  2700. func IsExistDrugByNameOne(drugname string, dosage float64, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  2701. var drug models.BaseDrugLib
  2702. var err error
  2703. err = XTReadDB().Model(&drug).Where("drug_name = ? and dose = ? and dose_unit = ? and min_number = ? and min_unit=? and max_unit = ? and status =1 and org_id = ?", drugname, dosage, doseunit, minnumber, minunit, packtunit, orgid).Find(&drug).Error
  2704. if err == gorm.ErrRecordNotFound {
  2705. return nil, err
  2706. }
  2707. if err != nil {
  2708. return nil, err
  2709. }
  2710. return &drug, nil
  2711. }
  2712. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  2713. err := XTWriteDB().Create(&info).Error
  2714. return err
  2715. }
  2716. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  2717. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  2718. return err
  2719. }
  2720. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  2721. var projoect models.HisProject
  2722. var err error
  2723. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  2724. if err == gorm.ErrRecordNotFound {
  2725. return nil, err
  2726. }
  2727. if err != nil {
  2728. return nil, err
  2729. }
  2730. return &projoect, nil
  2731. }
  2732. func CreateProjectInfomation(info *models.HisProject) error {
  2733. err := XTWriteDB().Create(&info).Error
  2734. return err
  2735. }
  2736. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  2737. err := XTWriteDB().Model(&project).Where("project_name = ? and user_org_id = ? and status = 1", projectName, orgid).Updates(map[string]interface{}{"project_name": project.ProjectName, "price": project.Price, "unit": project.Unit, "cost_classify": project.CostClassify, "executive_section": project.ExecutiveSection, "medical_coverage": project.MedicalCoverage, "pinyin": project.Pinyin, "wubi": project.Wubi, "statistical_classification": project.StatisticalClassification, "disease_directory": project.DiseaseDirectory, "is_record": project.IsRecord, "medical_code": project.MedicalCode, "tube_color": project.TubeColor, "single_dose": project.SingleDose, "delivery_way": project.DeliveryWay, "execution_frequency": project.ExecutionFrequency, "number_days": project.NumberDays, "total": project.Total, "medical_status": project.MedicalStatus, "category": project.Category, "specail_project": project.SpecailProject, "social_security_directory_code": project.SocialSecurityDirectoryCode, "record_date": project.RecordDate, "remark": project.Remark}).Error
  2738. return err
  2739. }
  2740. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  2741. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  2742. return warehouseinfo, err
  2743. }
  2744. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) {
  2745. err = readDb.Model(&models.DrugWarehouse{}).Where("warehousing_order = ? AND status = 1 and id = ?", warehouse.WarehousingOrder, id).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer}).Error
  2746. }
  2747. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  2748. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  2749. return list, err
  2750. }
  2751. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  2752. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  2753. return list, err
  2754. }
  2755. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  2756. var dealer models.Dealer
  2757. var err error
  2758. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  2759. if err == gorm.ErrRecordNotFound {
  2760. return nil, err
  2761. }
  2762. if err != nil {
  2763. return nil, err
  2764. }
  2765. return &dealer, nil
  2766. }
  2767. func CreateDealer(dealer *models.Dealer) error {
  2768. err := XTWriteDB().Create(&dealer).Error
  2769. return err
  2770. }
  2771. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  2772. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  2773. return err
  2774. }
  2775. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  2776. dataconfig := models.DictDataconfig{}
  2777. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  2778. return dataconfig, err
  2779. }
  2780. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  2781. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  2782. return dataconfig, err
  2783. }
  2784. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  2785. var dataconfig models.DictDataconfig
  2786. var err error
  2787. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  2788. if err == gorm.ErrRecordNotFound {
  2789. return nil, err
  2790. }
  2791. if err != nil {
  2792. return nil, err
  2793. }
  2794. return &dataconfig, nil
  2795. }
  2796. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  2797. dataconfig := models.DictDataconfig{}
  2798. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  2799. return dataconfig, err
  2800. }
  2801. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  2802. err := XTWriteDB().Create(&dataconfig).Error
  2803. return err
  2804. }
  2805. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  2806. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  2807. return err
  2808. }
  2809. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  2810. var goodType models.GoodsType
  2811. var err error
  2812. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  2813. if err == gorm.ErrRecordNotFound {
  2814. return nil, err
  2815. }
  2816. if err != nil {
  2817. return nil, err
  2818. }
  2819. return &goodType, nil
  2820. }
  2821. func CreatedGoodType(goodsType *models.GoodsType) error {
  2822. err := XTWriteDB().Create(&goodsType).Error
  2823. return err
  2824. }
  2825. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  2826. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  2827. return goodsType, err
  2828. }
  2829. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  2830. var department models.XtHisDepartment
  2831. var err error
  2832. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  2833. if err == gorm.ErrRecordNotFound {
  2834. return nil, err
  2835. }
  2836. if err != nil {
  2837. return nil, err
  2838. }
  2839. return &department, nil
  2840. }
  2841. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  2842. dataconfig := models.Dataconfig{}
  2843. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  2844. return dataconfig, err
  2845. }
  2846. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  2847. var dataconfig models.Dataconfig
  2848. var err error
  2849. err = XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and name = ? and status = 1", parentid, orgid, name).Find(&dataconfig).Error
  2850. if err == gorm.ErrRecordNotFound {
  2851. return nil, err
  2852. }
  2853. if err != nil {
  2854. return nil, err
  2855. }
  2856. return &dataconfig, nil
  2857. }
  2858. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  2859. dataconfig := models.Dataconfig{}
  2860. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  2861. return dataconfig, err
  2862. }
  2863. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  2864. err := XTWriteDB().Create(&dataconfig).Error
  2865. return err
  2866. }
  2867. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  2868. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  2869. return dataconfig, err
  2870. }
  2871. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  2872. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  2873. return list, err
  2874. }
  2875. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  2876. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and drug_status<>'停用'", orgid)
  2877. if len(keywords) > 0 {
  2878. likekey := "%" + keywords + "%"
  2879. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  2880. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  2881. }).Find(&drug).Error
  2882. } else {
  2883. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  2884. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  2885. }).Find(&drug).Error
  2886. }
  2887. return
  2888. }
  2889. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  2890. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  2891. if len(keywords) > 0 {
  2892. likekey := "%" + keywords + "%"
  2893. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2894. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  2895. }).Find(&good).Error
  2896. } else {
  2897. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2898. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  2899. }).Find(&good).Error
  2900. }
  2901. return
  2902. }
  2903. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  2904. db := writeDb.Begin()
  2905. err := db.Create(&detail).Error
  2906. if err != nil {
  2907. db.Rollback()
  2908. return err
  2909. }
  2910. db.Commit()
  2911. return err
  2912. }
  2913. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  2914. var autoreduece models.BloodAutomaticReduceDetail
  2915. var err error
  2916. err = XTReadDB().Model(&autoreduece).Where("org_id = ? and patient_id = ? and status = 1 and record_time = ? and good_id = ? and good_type_id = ? and count<> 0", orgid, patient_id, recordtime, goodid, goodtypeid).Find(&autoreduece).Error
  2917. if err == gorm.ErrRecordNotFound {
  2918. return nil, err
  2919. }
  2920. if err != nil {
  2921. return nil, err
  2922. }
  2923. return &autoreduece, nil
  2924. }
  2925. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  2926. detail := models.BloodAutomaticReduceDetail{}
  2927. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? and good_type_id = ?", orgid, patient_id, recordtime, goodid, goodtypeid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  2928. return err
  2929. }
  2930. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  2931. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and consumable_type = 3 and status = 1", patient_id, recrod_date, goodid).Find(&stockflow).Error
  2932. return stockflow, err
  2933. }
  2934. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64) error {
  2935. detail := models.BloodAutomaticReduceDetail{}
  2936. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? ", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  2937. info := models.WarehouseOutInfo{}
  2938. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  2939. prepare := models.DialysisBeforePrepare{}
  2940. err = XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  2941. flow := models.VmStockFlow{}
  2942. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  2943. return err
  2944. }
  2945. func UpDateWarehouseInfoByStockDelete(id int64, count int64) (err error) {
  2946. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  2947. return err
  2948. }
  2949. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  2950. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  2951. return info, err
  2952. }
  2953. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  2954. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  2955. return list, err
  2956. }
  2957. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  2958. err = writeDb.Model(&models.VmStockFlow{}).Where("warehousing_id = ? and system_time = ? and good_id = ? and patient_id = ?", id, recordtime, good_id, patient_id).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  2959. return err
  2960. }
  2961. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  2962. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  2963. return err
  2964. }
  2965. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  2966. var autoreduece models.BloodAutomaticReduceDetail
  2967. var err error
  2968. err = XTReadDB().Model(&autoreduece).Where("good_id = ? and good_type_id = ? and patient_id = ? and org_id = ? and status = 1 and record_time = ?", goodid, goodtypeid, patientid, orgid, recorddate).Find(&autoreduece).Error
  2969. if err == gorm.ErrRecordNotFound {
  2970. return nil, err
  2971. }
  2972. if err != nil {
  2973. return nil, err
  2974. }
  2975. return &autoreduece, nil
  2976. }
  2977. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  2978. detail := models.WarehouseOutInfoSeven{}
  2979. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  2980. return detail, err
  2981. }
  2982. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  2983. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  2984. return detail, err
  2985. }
  2986. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  2987. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  2988. return project, err
  2989. }
  2990. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  2991. project := models.HisPrescriptionProject{}
  2992. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  2993. return project, err
  2994. }
  2995. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  2996. err = XTReadDB().Select("count,warehouse_info_id").Where(" patient_id = ? and sys_record_time = ? and status = 1 and is_sys = 1 and count>0", patientid, recorddate).Find(&auto).Error
  2997. return auto, err
  2998. }
  2999. //func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.VmStockFlow, err error) {
  3000. //
  3001. // err = XTReadDB().Select("count,warehousing_id").Where(" patient_id = ? and system_time = ? and status = 1 and is_sys = 1 and count>0",patientid,recorddate).Find(&auto).Error
  3002. // return auto, err
  3003. //
  3004. //}
  3005. func ModefyWarehouseInfo(count int64, id int64) error {
  3006. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3007. return err
  3008. }
  3009. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3010. err := XTWriteDB().Model(models.AutomaticReduceDetail{}).Where("patient_id = ? and record_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3011. err = XTWriteDB().Model(models.DialysisBeforePrepare{}).Where("patient_id = ? and record_date = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3012. err = XTWriteDB().Model(models.VmStockFlow{}).Where("patient_id = ? and system_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3013. err = XTWriteDB().Model(models.WarehouseOutInfoNight{}).Where("patient_id = ? and sys_record_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3014. fmt.Println("err2332332322323322323232323232332", err)
  3015. return err
  3016. }
  3017. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  3018. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3019. return
  3020. }
  3021. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  3022. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3023. return
  3024. }
  3025. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  3026. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3027. return info, err
  3028. }
  3029. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3030. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3031. return info, err
  3032. }
  3033. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3034. offset := (page - 1) * limit
  3035. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3036. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3037. fmt.Println(table)
  3038. if startime > 0 {
  3039. db = db.Where("x.ctime>=?", startime)
  3040. }
  3041. if endtime > 0 {
  3042. db = db.Where("x.ctime<=?", endtime)
  3043. }
  3044. likeKey := "%" + keyword + "%"
  3045. if len(likeKey) > 0 {
  3046. db = db.Where("t.good_name like ?", likeKey)
  3047. }
  3048. if orgid > 0 {
  3049. db = db.Where("x.org_id = ?", orgid)
  3050. }
  3051. if good_type > 0 {
  3052. db = db.Where("t.good_type_id = ?", good_type)
  3053. }
  3054. err = db.Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehousing_unit,x.stock_count,x.price,x.total_price,x.dealer,x.remark,x.ctime,x.is_return,x.warehousing_order,x.type,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.manufacturer").Joins("left join xt_good_information as t on t.id = x.good_id").Group("x.good_id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&info).Error
  3055. return info, total, err
  3056. }
  3057. func GetAllGoodInfoStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.GoodInfo, total int64, err error) {
  3058. offset := (page - 1) * limit
  3059. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  3060. likeKey := "%" + keyword + "%"
  3061. if len(likeKey) > 0 {
  3062. db = db.Where("x.good_name like ?", likeKey)
  3063. }
  3064. if orgid > 0 {
  3065. db = db.Where("x.org_id = ?", orgid)
  3066. }
  3067. if good_type > 0 {
  3068. db = db.Where("x.good_type_id = ?", good_type)
  3069. }
  3070. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3071. if startime > 0 {
  3072. db = db.Where("ctime>=?", startime)
  3073. }
  3074. if endtime > 0 {
  3075. db = db.Where("ctime<=?", endtime)
  3076. }
  3077. return db.Where("status = 1")
  3078. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3079. if startime > 0 {
  3080. db = db.Where("ctime>=?", startime)
  3081. }
  3082. if endtime > 0 {
  3083. db = db.Where("ctime<=?", endtime)
  3084. }
  3085. return db.Where("status = 1")
  3086. }).Find(&info).Error
  3087. return info, total, err
  3088. }
  3089. func GetStockListById(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3090. offset := (page - 1) * limit
  3091. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3092. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3093. fmt.Println(table)
  3094. if startime > 0 {
  3095. db = db.Where("x.ctime>=?", startime)
  3096. }
  3097. if endtime > 0 {
  3098. db = db.Where("x.ctime<=?", endtime)
  3099. }
  3100. if orgid > 0 {
  3101. db = db.Where("x.org_id = ?", orgid)
  3102. }
  3103. if good_id > 0 {
  3104. db = db.Where("x.good_id = ?", good_id)
  3105. }
  3106. err = db.Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehousing_unit,x.stock_count,x.price,x.total_price,x.dealer,t.manufacturer,x.remark,x.ctime,x.is_return,x.warehousing_order,x.type,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.min_unit,t.buy_price").Joins("left join xt_good_information as t on t.id = x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&info).Error
  3107. return info, total, err
  3108. }
  3109. func GetStockOutList(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64, is_sys int64) (info []*models.WarehouseOutInfo, total int64, err error) {
  3110. offset := (page - 1) * limit
  3111. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  3112. if startime > 0 {
  3113. db = db.Where("x.ctime>=?", startime)
  3114. }
  3115. if endtime > 0 {
  3116. db = db.Where("x.ctime<=?", endtime)
  3117. }
  3118. if orgid > 0 {
  3119. db = db.Where("x.org_id = ?", orgid)
  3120. }
  3121. if good_id > 0 {
  3122. db = db.Where("x.good_id = ?", good_id)
  3123. }
  3124. if is_sys == 2 {
  3125. db = db.Where("x.is_sys = 1")
  3126. }
  3127. if is_sys == 3 {
  3128. db = db.Where("x.is_sys = 0")
  3129. }
  3130. err = db.Select("x.id,x.warehouse_out_id,x.warehouse_info_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.is_sys,x.number,x.dealer,x.manufacturer,x.consumable_type").Offset(offset).Count(&total).Scan(&info).Error
  3131. return info, total, err
  3132. }
  3133. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3134. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3135. if startime > 0 {
  3136. db = db.Where("x.ctime >=?", startime)
  3137. }
  3138. if endtime > 0 {
  3139. db = db.Where("x.ctime<=?", endtime)
  3140. }
  3141. if orgid > 0 {
  3142. db = db.Where("x.org_id = ?", orgid)
  3143. }
  3144. err = db.Select("sum(x.warehousing_count) as count,sum(x.stock_count) as stock_count,x.good_id").Group("x.good_id").Scan(&info).Error
  3145. return info, err
  3146. }
  3147. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3148. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3149. if startime > 0 {
  3150. db = db.Where("x.ctime >=?", startime)
  3151. }
  3152. if endtime > 0 {
  3153. db = db.Where("x.ctime<=?", endtime)
  3154. }
  3155. if orgid > 0 {
  3156. db = db.Where("x.org_id = ?", orgid)
  3157. }
  3158. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  3159. return info, err
  3160. }
  3161. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3162. if endtime > 0 {
  3163. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count,x.record_time FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time >= ? and x.record_time <=? and `status` = 1) as b GROUP BY good_id", orgid, startime, endtime).Scan(&autoMatic).Error
  3164. }
  3165. if endtime == 0 {
  3166. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count,x.record_time FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and `status` = 1) as b GROUP BY good_id", orgid).Scan(&autoMatic).Error
  3167. }
  3168. return autoMatic, err
  3169. }
  3170. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3171. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3172. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3173. fmt.Println(table)
  3174. if id > 0 {
  3175. db = db.Where("x.warehouse_out_id = ?", id)
  3176. }
  3177. if orgid > 0 {
  3178. db = db.Where("x.org_id = ?", orgid)
  3179. }
  3180. err = db.Select("x.id,x.warehouse_out_id,x.warehouse_info_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.good_id,x.warehouse_info_id").Order("x.ctime desc").Scan(&out).Error
  3181. return out, err
  3182. }
  3183. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  3184. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  3185. if len(id) > 0 {
  3186. db = db.Where("x.id in(?)", id)
  3187. }
  3188. if orgid > 0 {
  3189. db = db.Where("x.org_id= ?", orgid)
  3190. }
  3191. err = db.Select("x.id,x.warehouse_out_order_number,x.operation_time,x.creater,x.org_id,x.modifier,x.remark,x.warehouse_out_time,x.dealer,x.manufacturer,x.type").Find(&out).Error
  3192. return out, err
  3193. }
  3194. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3195. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3196. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3197. fmt.Println(table)
  3198. if len(id) > 0 {
  3199. db = db.Where("x.warehouse_out_id in (?)", id)
  3200. }
  3201. if orgid > 0 {
  3202. db = db.Where("x.org_id = ?", orgid)
  3203. }
  3204. err = db.Select("x.id,x.warehouse_out_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.warehouse_out_id,x.good_id").Order("x.ctime desc").Scan(&out).Error
  3205. return out, err
  3206. }
  3207. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  3208. db := XTReadDB().Model(&info)
  3209. db = db.Preload("GoodInfo", "status = 1")
  3210. err = db.Raw("select * from(SELECT * FROM xt_warehouse_out_info WHERE warehouse_out_id IN(?) AND org_id = ? and status = 1 ORDER BY id DESC LIMIT 1000) a group by good_id", ids, orgId).Find(&info).Error
  3211. return info, err
  3212. }
  3213. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3214. db := readDb.Model(&models.AutomaticReduceDetail{})
  3215. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3216. db = db.Preload("GoodsType", "status = 1")
  3217. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3218. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  3219. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  3220. db = db.Count(&total)
  3221. err = db.Find(&user).Error
  3222. return
  3223. }
  3224. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  3225. info := models.GoodInfo{}
  3226. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3227. return info, err
  3228. }
  3229. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  3230. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  3231. return err
  3232. }
  3233. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  3234. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3235. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3236. fmt.Println(table)
  3237. if orgid > 0 {
  3238. db = db.Where("x.org_id = ?", orgid)
  3239. }
  3240. if len(orderid) > 0 {
  3241. db = db.Where("x.warehousing_id in(?)", orderid)
  3242. }
  3243. if startime > 0 {
  3244. db = db.Where("x.ctime>=?", startime)
  3245. }
  3246. if endtime > 0 {
  3247. db = db.Where("x.ctime<=?", endtime)
  3248. }
  3249. err = db.Select("x.id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.dealer,x.manufacturer,x.remark,x.license_number,t.good_name,t.packing_unit,t.specification_name").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&info).Error
  3250. return info, err
  3251. }
  3252. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3253. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3254. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  3255. fmt.Println(table)
  3256. if startime > 0 {
  3257. db = db.Where("x.ctime>=?", startime)
  3258. }
  3259. if endtime > 0 {
  3260. db = db.Where("x.ctime<=?", endtime)
  3261. }
  3262. if orgid > 0 {
  3263. db = db.Where("x.org_id = ?", orgid)
  3264. }
  3265. err = db.Select("x.drug_id,x.count,x.count_unit,t.max_unit,t.min_unit,t.min_number").Joins("left join xt_base_drug as t on t.id = x.drug_id").Scan(&info).Error
  3266. return info, err
  3267. }
  3268. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  3269. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  3270. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3271. fmt.Println(table)
  3272. if orgid > 0 {
  3273. db = db.Where("x.org_id = ?", orgid)
  3274. }
  3275. if len(ids) > 0 {
  3276. db = db.Where("x.warehouse_out_id in (?)", ids)
  3277. }
  3278. if startime > 0 {
  3279. db = db.Where("x.ctime>=?", startime)
  3280. }
  3281. if endtime > 0 {
  3282. db = db.Where("x.ctime<=?", endtime)
  3283. }
  3284. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,t.manufacturer,t.dealer").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  3285. return out, err
  3286. }
  3287. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  3288. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  3289. if len(ids) > 0 {
  3290. db = db.Where("x.id in(?)", ids)
  3291. }
  3292. if orgid > 0 {
  3293. db = db.Where("x.org_id = ?", orgid)
  3294. }
  3295. err = db.Select("x.id,x.warehouse_out_order_number,x.operation_time,x.creater,x.org_id,x.modifier,x.remark,x.warehouse_out_time,x.is_sys").Scan(&info).Error
  3296. return info, err
  3297. }
  3298. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3299. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  3300. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  3301. fmt.Println(table)
  3302. if len(ids) > 0 {
  3303. db = db.Where("x.warehouse_out_id in(?)", ids)
  3304. }
  3305. if orgid > 0 {
  3306. db = db.Where("x.org_id = ?", orgid)
  3307. }
  3308. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.warehousing_out_target,x.count,x.count_unit,x.price,x.product_date,x.expiry_date,x.number,x.batch_number,t.drug_type,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.drug_name").Joins("left join xt_base_drug as t on t.id =x.drug_id").Scan(&info).Error
  3309. return info, err
  3310. }
  3311. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  3312. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  3313. likeKey := "%" + keyword + "%"
  3314. if len(keyword) > 0 {
  3315. db = db.Where("x.drug_name like ?", likeKey)
  3316. }
  3317. if orgid > 0 {
  3318. db = db.Where("x.org_id = ?", orgid)
  3319. }
  3320. err = db.Find(&basedrug).Error
  3321. return basedrug, err
  3322. }
  3323. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  3324. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3325. if len(ids) > 0 {
  3326. db = db.Where("x.drug_id in(?)", ids)
  3327. }
  3328. if orgid > 0 {
  3329. db = db.Where("x.org_id = ?", orgid)
  3330. }
  3331. if startime > 0 {
  3332. db = db.Where("x.ctime>=?", startime)
  3333. }
  3334. if endtime > 0 {
  3335. db = db.Where("x.ctime<=?", endtime)
  3336. }
  3337. err = db.Find(&info).Error
  3338. return info, err
  3339. }
  3340. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseInfo, err error) {
  3341. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3342. if len(ids) > 0 {
  3343. db = db.Where("x.drug_id in(?)", ids)
  3344. }
  3345. if orgid > 0 {
  3346. db = db.Where("x.org_id = ?", orgid)
  3347. }
  3348. if startime > 0 {
  3349. db = db.Where("x.ctime>=?", startime)
  3350. }
  3351. if endtime > 0 {
  3352. db = db.Where("x.ctime<=?", endtime)
  3353. }
  3354. err = db.Find(&info).Error
  3355. return info, err
  3356. }
  3357. func GetDrugTotalCount(drugid int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  3358. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3359. if drugid > 0 {
  3360. db = db.Where("x.drug_id = ?", drugid)
  3361. }
  3362. if orgid > 0 {
  3363. db = db.Where("x.org_id = ?", orgid)
  3364. }
  3365. err = db.Select("x.drug_id,sum(x.stock_max_number) as count,sum(x.stock_min_number) as stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  3366. return info, err
  3367. }
  3368. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  3369. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3370. if id > 0 {
  3371. db = db.Where("x.id = ?", id)
  3372. }
  3373. if orgid > 0 {
  3374. db = db.Where("x.org_id = ?", orgid)
  3375. }
  3376. err = db.Select("x.drug_id,sum(x.stock_max_number) as count,sum(x.stock_min_number) as stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  3377. return info, err
  3378. }
  3379. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  3380. info := models.VmDrugWarehouseInfo{}
  3381. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3382. if id == 0 {
  3383. db = db.Where("id = ?", id)
  3384. }
  3385. if drugid > 0 {
  3386. db = db.Where("x.drug_id = ?", drugid)
  3387. }
  3388. if orgid > 0 {
  3389. db = db.Where("x.org_id = ?", orgid)
  3390. }
  3391. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  3392. return info, err
  3393. }
  3394. func GetDrugTotalCountTwo(drugid int64) (list []*models.VmDrugWarehouseInfo, err error) {
  3395. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3396. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  3397. fmt.Println(table)
  3398. if drugid > 0 {
  3399. db = db.Where("x.drug_id = ?", drugid)
  3400. }
  3401. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,d.min_number").Joins("left join xt_base_drug as d on d.id = x.drug_id").Scan(&list).Error
  3402. return list, err
  3403. }
  3404. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  3405. lib := models.BaseDrugLib{}
  3406. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  3407. return lib, err
  3408. }
  3409. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  3410. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3411. if goodid > 0 {
  3412. db = db.Where("x.good_id = ?", goodid)
  3413. }
  3414. if orgid > 0 {
  3415. db = db.Where("x.org_id = ?", orgid)
  3416. }
  3417. err = db.Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,x.dealer,x.manufacturer,x.remark,x.is_return,x.warehousing_order,x.warehousing_unit,x.stock_count,x.license_number,x.org_id").Find(&info).Error
  3418. return info, err
  3419. }
  3420. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  3421. err := XTWriteDB().Model(models.WarehousingInfo{}).Where("good_id = ? and org_id = ? and status = 1 and warehousing_id = ?", goodid, orgid, warehousingId).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3422. return err
  3423. }
  3424. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  3425. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  3426. return info, err
  3427. }
  3428. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  3429. //遍历循环入库数据
  3430. for i, item := range list {
  3431. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  3432. if (item.WarehousingCount - item.StockCount) >= count {
  3433. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  3434. info := models.WarehousingInfo{
  3435. GoodId: item.GoodId,
  3436. OrgId: item.OrgId,
  3437. WarehousingId: item.WarehousingId,
  3438. StockCount: item.StockCount + count,
  3439. }
  3440. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  3441. fmt.Println("2323233232323232323232323232323", err2)
  3442. return err
  3443. }
  3444. if (item.WarehousingCount - item.StockCount) < count {
  3445. //如果该批次小于退库数量,则先出库该相差数量
  3446. var countOne = item.WarehousingCount - item.StockCount
  3447. infoOne := models.WarehousingInfo{
  3448. GoodId: item.GoodId,
  3449. OrgId: item.OrgId,
  3450. WarehousingId: item.WarehousingId,
  3451. StockCount: countOne + item.StockCount,
  3452. }
  3453. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  3454. if err == nil {
  3455. //剩余的出库到第二个批次
  3456. var countTwo = count - countOne
  3457. infos := append(list, list[i+1])
  3458. var idArray []*models.WarehousingInfo
  3459. for _, it := range infos {
  3460. if it.WarehousingId != item.WarehousingId {
  3461. idArray = append(idArray, it)
  3462. }
  3463. }
  3464. //查询更新后的库存
  3465. UpdateCancelOut(idArray, countTwo)
  3466. return err
  3467. } else {
  3468. return err
  3469. }
  3470. }
  3471. }
  3472. return err
  3473. }
  3474. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  3475. if len(idstr) > 0 {
  3476. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  3477. } else {
  3478. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  3479. }
  3480. return info, err
  3481. }
  3482. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  3483. if len(idstr) > 0 {
  3484. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  3485. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3486. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  3487. }).Find(&info).Error
  3488. }
  3489. return info, err
  3490. }
  3491. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  3492. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  3493. if startime > 0 {
  3494. db = db.Where("x.ctime >=?", startime)
  3495. }
  3496. if endtime > 0 {
  3497. db = db.Where("x.ctime <= ?", endtime)
  3498. }
  3499. if orgid > 0 {
  3500. db = db.Where("x.org_id = ?", orgid)
  3501. }
  3502. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  3503. return info, err
  3504. }
  3505. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3506. info := models.WarehouseOutInfo{}
  3507. var err error
  3508. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  3509. if err == gorm.ErrRecordNotFound {
  3510. return nil, err
  3511. }
  3512. if err != nil {
  3513. return nil, err
  3514. }
  3515. return &info, nil
  3516. }
  3517. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  3518. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3519. if goodid > 0 {
  3520. db = db.Where("x.good_id = ?", goodid)
  3521. }
  3522. if orgid > 0 {
  3523. db = db.Where("x.org_id = ?", orgid)
  3524. }
  3525. if id > 0 {
  3526. db = db.Where("x.warehouse_info_id = ?", id)
  3527. }
  3528. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  3529. return info, err
  3530. }
  3531. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64) (info models.VmStockFlow, err error) {
  3532. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  3533. if goodid > 0 {
  3534. db = db.Where("x.good_id = ?", goodid)
  3535. }
  3536. if orgid > 0 {
  3537. db = db.Where("x.user_org_id = ?", orgid)
  3538. }
  3539. if id > 0 {
  3540. db = db.Where("x.warehousing_id = ?", id)
  3541. }
  3542. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  3543. return info, err
  3544. }
  3545. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  3546. stock := models.CancelStock{}
  3547. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  3548. return stock, err
  3549. }
  3550. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  3551. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  3552. return info, err
  3553. }
  3554. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  3555. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  3556. return info, err
  3557. }
  3558. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  3559. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  3560. return stock, err
  3561. }
  3562. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  3563. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and count > 0")
  3564. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  3565. fmt.Println(table)
  3566. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  3567. fmt.Println(tableone)
  3568. if id > 0 {
  3569. db = db.Where("x.warehouse_out_id = ?", id)
  3570. }
  3571. if goodid > 0 {
  3572. db = db.Where("x.good_id = ?", goodid)
  3573. }
  3574. err = db.Select("x.id,x.warehousing_id,x.good_id,x.number,x.count,x.patient_id,x.system_time,x.is_sys,x.warehousing_order,x.warehouse_out_id,x.warehouse_out_order_number,x.price,x.product_date,x.expire_date,x.ctime,o.packing_unit,t.name").Joins("left join xt_patients as t on t.id = x.patient_id").Joins("left join xt_good_information as o on o.id = x.good_id").Scan(&stock).Error
  3575. //err =XTReadDB()()()()().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&stock).Error
  3576. return stock, err
  3577. }
  3578. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  3579. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  3580. return info, err
  3581. }
  3582. func ExportDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseInfo, err error) {
  3583. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1")
  3584. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3585. fmt.Println("table", table)
  3586. if len(warehouse_out_id) > 0 {
  3587. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  3588. }
  3589. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.price,x.dealer,x.manufacturer,x.warehousing_order,x.batch_number,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  3590. return out, err
  3591. }
  3592. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  3593. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3594. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3595. fmt.Println("table", table)
  3596. if len(warehouse_out_id) > 0 {
  3597. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  3598. }
  3599. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.count_unit,x.price,x.product_date,x.expiry_date,x.warehouse_out_order_number,x.dealer,x.manufacturer,x.is_sys,x.sys_record_time,x.warehouse_info_id,x.number,x.batch_number,x.patient_id,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type,b.min_price").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  3600. return out, err
  3601. }
  3602. func GetExportOutOrderDrugListOne(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  3603. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1")
  3604. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3605. fmt.Println("table", table)
  3606. if len(warehouse_out_id) > 0 {
  3607. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  3608. }
  3609. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.max_unit as count_unit,x.price,x.product_date,x.expire_date,x.warehouse_out_order_number,x.dealer,x.manufacturer,x.is_sys,x.number,x.batch_number,x.patient_id,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type,b.min_price").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  3610. return out, err
  3611. }
  3612. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  3613. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  3614. return out, err
  3615. }
  3616. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  3617. info := models.WarehouseOutInfo{}
  3618. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  3619. return info, err
  3620. }
  3621. func GetTotalCountByGoodId(goodid int64) (models.WarehousingInfo, error) {
  3622. info := models.WarehousingInfo{}
  3623. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3624. if goodid > 0 {
  3625. db = db.Where("x.good_id = ?", goodid)
  3626. }
  3627. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  3628. return info, err
  3629. }
  3630. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  3631. info := models.WarehousingInfo{}
  3632. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  3633. return info, err
  3634. }
  3635. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  3636. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3637. return err
  3638. }
  3639. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64) (info models.WarehousingInfo, err error) {
  3640. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  3641. return info, err
  3642. }
  3643. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  3644. if len(warehousingInfo) > 0 {
  3645. utx := writeDb.Begin()
  3646. if len(warehousingInfo) > 0 {
  3647. thisSQL := "INSERT INTO xt_stock_flow (warehousing_id, good_id, number, license_number, count,user_org_id,patient_id,system_time,consumable_type,is_sys,warehousing_order,warehouse_out_id,warehouse_out_order_number,is_edit,cancel_stock_id,cancel_order_number,manufacturer,dealer,creator,update_creator,status,ctime,mtime,price,warehousing_detail_id,warehouse_out_detail_id,cancel_out_detail_id,expire_date,product_date) VALUES "
  3648. insertParams := make([]string, 0)
  3649. insertData := make([]interface{}, 0)
  3650. for _, info := range warehousingInfo {
  3651. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  3652. insertData = append(insertData, info.WarehousingId)
  3653. insertData = append(insertData, info.GoodId)
  3654. insertData = append(insertData, info.Number)
  3655. insertData = append(insertData, info.LicenseNumber)
  3656. insertData = append(insertData, info.Count)
  3657. insertData = append(insertData, info.UserOrgId)
  3658. insertData = append(insertData, info.PatientId)
  3659. insertData = append(insertData, info.SystemTime)
  3660. insertData = append(insertData, info.ConsumableType)
  3661. insertData = append(insertData, info.IsSys)
  3662. insertData = append(insertData, info.WarehousingOrder)
  3663. insertData = append(insertData, info.WarehouseOutId)
  3664. insertData = append(insertData, info.WarehouseOutOrderNumber)
  3665. insertData = append(insertData, info.IsEdit)
  3666. insertData = append(insertData, info.CancelStockId)
  3667. insertData = append(insertData, info.CancelOrderNumber)
  3668. insertData = append(insertData, info.Manufacturer)
  3669. insertData = append(insertData, info.Dealer)
  3670. insertData = append(insertData, info.Creator)
  3671. insertData = append(insertData, info.UpdateCreator)
  3672. insertData = append(insertData, info.Status)
  3673. insertData = append(insertData, info.Ctime)
  3674. insertData = append(insertData, info.Mtime)
  3675. insertData = append(insertData, info.Price)
  3676. insertData = append(insertData, info.WarehousingDetailId)
  3677. insertData = append(insertData, info.WarehouseOutDetailId)
  3678. insertData = append(insertData, info.CancelOutDetailId)
  3679. insertData = append(insertData, info.ExpireDate)
  3680. insertData = append(insertData, info.ProductDate)
  3681. }
  3682. thisSQL += strings.Join(insertParams, ", ")
  3683. err = utx.Exec(thisSQL, insertData...).Error
  3684. if err != nil {
  3685. utx.Rollback()
  3686. return
  3687. }
  3688. }
  3689. utx.Commit()
  3690. }
  3691. return
  3692. }
  3693. func CreateStockFlowOne(flow models.VmStockFlow) error {
  3694. err := XTWriteDB().Create(&flow).Error
  3695. return err
  3696. }
  3697. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  3698. err := XTWriteDB().Create(&flow).Error
  3699. return err
  3700. }
  3701. func UpdatedStockFlow(flow models.VmStockFlow) error {
  3702. err := XTWriteDB().Save(&flow).Error
  3703. return err
  3704. }
  3705. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  3706. err = XTWriteDB().Model(&flow).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehousing_id, patient_id, record_time, good_id).Updates(map[string]interface{}{"number": flow.Number, "license_number": flow.LicenseNumber, "count": flow.Count, "expire_date": flow.ExpireDate, "product_date": flow.ProductDate, "price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer}).Error
  3707. return err
  3708. }
  3709. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  3710. stock := models.VmStockFlow{}
  3711. var err error
  3712. err = XTReadDB().Model(&stock).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehousing_id, patient_id, record_time, good_id).Find(&stock).Error
  3713. if err == gorm.ErrRecordNotFound {
  3714. return nil, err
  3715. }
  3716. if err != nil {
  3717. return nil, err
  3718. }
  3719. return &stock, nil
  3720. }
  3721. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  3722. info := models.WarehouseOutInfo{}
  3723. var err error
  3724. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  3725. if err == gorm.ErrRecordNotFound {
  3726. return nil, err
  3727. }
  3728. if err != nil {
  3729. return nil, err
  3730. }
  3731. return &info, nil
  3732. }
  3733. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64) (*models.WarehouseOutInfo, error) {
  3734. info := models.WarehouseOutInfo{}
  3735. var err error
  3736. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ?", good_id, record_time, patient_id).Find(&info).Error
  3737. if err == gorm.ErrRecordNotFound {
  3738. return nil, err
  3739. }
  3740. if err != nil {
  3741. return nil, err
  3742. }
  3743. return &info, nil
  3744. }
  3745. func GetStockFlowList(limit int64, page int64, consumable_type int64, orgId int64, startTime int64, endTime int64, good_id int64) (flow []*models.VmStockFlow, total int64, err error) {
  3746. offset := (page - 1) * limit
  3747. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  3748. if consumable_type > 0 {
  3749. //入库
  3750. if consumable_type == 1 {
  3751. db = db.Where("consumable_type = ?", consumable_type)
  3752. }
  3753. //2 手动出库
  3754. if consumable_type == 2 {
  3755. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  3756. }
  3757. //3.自动出库
  3758. if consumable_type == 3 {
  3759. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  3760. }
  3761. //4.手动退库
  3762. if consumable_type == 4 {
  3763. db = db.Where(" consumable_type = ?", consumable_type)
  3764. }
  3765. if consumable_type == 10 {
  3766. db = db.Where(" consumable_type = ?", consumable_type)
  3767. }
  3768. if consumable_type == 11 {
  3769. db = db.Where(" consumable_type = ?", consumable_type)
  3770. }
  3771. }
  3772. if orgId > 0 {
  3773. db = db.Where("user_org_id = ?", orgId)
  3774. }
  3775. if startTime > 0 {
  3776. db = db.Where("ctime >= ?", startTime)
  3777. }
  3778. if endTime > 0 {
  3779. db = db.Where("ctime<=?", endTime)
  3780. }
  3781. if good_id > 0 {
  3782. db = db.Where("good_id = ?", good_id)
  3783. }
  3784. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&flow).Error
  3785. return flow, total, err
  3786. }
  3787. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  3788. detail := models.AutomaticReduceDetail{}
  3789. err := XTReadDB().Model(&detail).Where("record_time = ? and patient_id = ? and good_id = ? and good_type_id = ? and status = 1", recordtime, patientid, goodid, goodtypeid).Last(&detail).Error
  3790. return detail, err
  3791. }
  3792. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  3793. detail := models.WarehouseOutInfo{}
  3794. err := XTReadDB().Model(&detail).Where("sys_record_time = ? and patient_id = ? and good_id = ? and good_type_id = ? and status = 1", recordtime, patientid, goodid, goodtypeid).Last(&detail).Error
  3795. return detail, err
  3796. }
  3797. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64) error {
  3798. outInfo := models.WarehouseOutInfo{}
  3799. err := XTWriteDB().Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", good_id, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber}).Error
  3800. return err
  3801. }
  3802. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  3803. info := models.WarehouseOutInfo{}
  3804. err = XTReadDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and good_type_id = ? and status = 1", orgid, patient_id, record_time, goodid, good_type_id).Last(&info).Error
  3805. return info, err
  3806. }
  3807. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  3808. info := models.WarehousingInfo{}
  3809. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  3810. return info, err
  3811. }
  3812. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  3813. info := models.WarehousingInfo{}
  3814. db := XTReadDB().Model(&info)
  3815. if good_id > 0 {
  3816. db = db.Where("good_id = ? and status = 1", good_id)
  3817. }
  3818. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  3819. return info, err
  3820. }
  3821. func GetDrugFlow(drugid int64, orgid int64, limit int64, page int64, startTime int64, endTime int64, stock_type int64) (drug_flow []*models.DrugFlow, total int64, err error) {
  3822. offset := (page - 1) * limit
  3823. db := XTReadDB().Model(drug_flow).Where("status = 1")
  3824. if drugid > 0 {
  3825. db = db.Where("drug_id = ?", drugid)
  3826. }
  3827. if orgid > 0 {
  3828. db = db.Where("user_org_id = ?", orgid)
  3829. }
  3830. if startTime > 0 {
  3831. db = db.Where("ctime >=? ", startTime)
  3832. }
  3833. if endTime > 0 {
  3834. db = db.Where("ctime<=?", endTime)
  3835. }
  3836. if stock_type > 0 {
  3837. //手动入库
  3838. if stock_type == 1 {
  3839. db = db.Where("consumable_type = 1")
  3840. }
  3841. //手动出库
  3842. if stock_type == 2 {
  3843. db = db.Where("consumable_type = 2 and is_sys = 0")
  3844. }
  3845. //自动出库
  3846. if stock_type == 3 {
  3847. db = db.Where("consumable_type = 3 and is_sys = 1")
  3848. }
  3849. if stock_type == 4 {
  3850. db = db.Where("consumable_type = 4")
  3851. }
  3852. if stock_type == 10 {
  3853. db = db.Where("consumable_type = 10")
  3854. }
  3855. if stock_type == 11 {
  3856. db = db.Where("consumable_type = 11")
  3857. }
  3858. }
  3859. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Preload("BaseDrugLib", "status = 1").Preload("DrugWarehouseInfo", "status = 1").Find(&drug_flow).Error
  3860. return drug_flow, total, err
  3861. }
  3862. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  3863. outInfo := models.DrugWarehouseOutInfo{}
  3864. var err error
  3865. err = XTReadDB().Model(&outInfo).Where("drug_id = ? and patient_id = ? and status = 1 and sys_record_time = ?", drug_id, patient_id, recordtime).Find(&outInfo).Error
  3866. if err == gorm.ErrRecordNotFound {
  3867. return nil, err
  3868. }
  3869. if err != nil {
  3870. return nil, err
  3871. }
  3872. return &outInfo, nil
  3873. }
  3874. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  3875. err = XTReadDB().Model(&outInfo).Where("drug_id = ? and patient_id = ? and status = 1 and sys_record_time = ?", drug_id, patient_id, recordtime).Find(&outInfo).Error
  3876. return outInfo, err
  3877. }
  3878. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  3879. outInfo := models.DrugWarehouseOutInfo{}
  3880. err := XTWriteDB().Model(&outInfo).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ?", drug_id, patient_id, record_date, orgid).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "mtime": info.Mtime, "ctime": info.Ctime, "org_id": info.OrgId, "warehouse_out_order_number": info.WarehouseOutOrderNumber, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "is_sys": info.IsSys, "sys_record_time": info.SysRecordTime, "warehouse_info_id": info.WarehouseInfoId, "number": info.Number, "batch_number": info.BatchNumber, "patient_id": info.PatientId}).Error
  3881. return err
  3882. }
  3883. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  3884. db := XTReadDB().Model(&cancle).Where("status = 1")
  3885. if startime > 0 {
  3886. db = db.Where("ctime >=?", startime)
  3887. }
  3888. if endtime > 0 {
  3889. db = db.Where("ctime <=?", endtime)
  3890. }
  3891. if len(ids) > 0 {
  3892. db = db.Where("cancel_stock_id in(?)", ids)
  3893. }
  3894. if orgid > 0 {
  3895. db = db.Where("org_id = ?", orgid)
  3896. }
  3897. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  3898. err = db.Find(&cancle).Error
  3899. return cancle, err
  3900. }
  3901. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  3902. db := XTReadDB().Model(&info)
  3903. if id > 0 {
  3904. db = db.Where("cancel_stock_id = ?", id)
  3905. }
  3906. db = db.Preload("BaseDrugLib")
  3907. err = db.Find(&info).Error
  3908. return info, err
  3909. }
  3910. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  3911. info := models.XtDrugWarehouseInfo{}
  3912. db := XTReadDB().Model(&info).Where("status = 1")
  3913. if id > 0 {
  3914. db = db.Where("id = ?", id)
  3915. }
  3916. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  3917. return info, err
  3918. }
  3919. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  3920. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  3921. return info, err
  3922. }
  3923. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  3924. err = XTReadDB().Select("id,stock_max_number,stock_min_number").Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  3925. return info, err
  3926. }
  3927. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  3928. info := models.HisDoctorAdviceInfo{}
  3929. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  3930. return info, err
  3931. }
  3932. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  3933. info := models.HisDoctorAdviceInfo{}
  3934. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).Find(&info).Error
  3935. return info, err
  3936. }
  3937. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  3938. detail := models.DrugAutomaticReduceDetail{}
  3939. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1 and advice_id = ?", drugid, patient_id, advice_date, advice_id).Last(&detail).Error
  3940. return detail, err
  3941. }
  3942. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  3943. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  3944. return err
  3945. }
  3946. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  3947. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  3948. return err
  3949. }
  3950. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  3951. err := XTWriteDB().Model(models.DrugAutomaticReduceDetail{}).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3952. err = XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1", drugid, patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3953. err = XTWriteDB().Model(models.DrugFlow{}).Where("drug_id = ? and patient_id = ? and system_time = ? and status = 1", drugid, patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3954. return err
  3955. }
  3956. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  3957. err := XTWriteDB().Model(models.DrugAutomaticReduceDetail{}).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  3958. err = XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  3959. err = XTWriteDB().Model(models.DrugFlow{}).Where("drug_id = ? and patient_id = ? and system_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  3960. return err
  3961. }
  3962. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  3963. info := models.XtDrugWarehouseInfo{}
  3964. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  3965. return info, err
  3966. }
  3967. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  3968. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  3969. return err
  3970. }
  3971. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  3972. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  3973. //return err
  3974. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  3975. return err
  3976. }
  3977. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  3978. info := models.DrugCancelStockInfo{}
  3979. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  3980. return info, err
  3981. }
  3982. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  3983. info := models.DrugCancelStockInfo{}
  3984. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  3985. return info, err
  3986. }
  3987. func UpdatedDrugInfo(id int64, count int64) (err error) {
  3988. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  3989. return err
  3990. }
  3991. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  3992. err := XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_stock_id = ? and drug_id = ? and status = 1", cancelstockid, drug_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  3993. return err
  3994. }
  3995. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  3996. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  3997. return err
  3998. }
  3999. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64) error {
  4000. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  4001. return err
  4002. }
  4003. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  4004. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  4005. return err
  4006. }
  4007. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64) error {
  4008. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  4009. return err
  4010. }
  4011. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  4012. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  4013. return err
  4014. }
  4015. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  4016. info := models.WarehousingInfo{}
  4017. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4018. return info, err
  4019. }
  4020. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4021. info := models.WarehousingInfo{}
  4022. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  4023. return info, err
  4024. }
  4025. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4026. info := models.WarehousingInfo{}
  4027. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  4028. return info, err
  4029. }
  4030. func CreateWarehouseOut(out models.WarehouseOut) error {
  4031. err := XTWriteDB().Create(&out).Error
  4032. return err
  4033. }
  4034. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  4035. out := models.WarehouseOut{}
  4036. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  4037. return out, err
  4038. }
  4039. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  4040. err := XTReadDB().Create(&info).Error
  4041. return err
  4042. }
  4043. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  4044. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4045. return err
  4046. }
  4047. func UpdateWareInfoById(id int64, count int64) error {
  4048. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4049. return err
  4050. }
  4051. func UpdateWareInfoByIdSix(id int64, count int64) error {
  4052. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4053. return err
  4054. }
  4055. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  4056. info := models.CancelStockInfo{}
  4057. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4058. return info, err
  4059. }
  4060. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  4061. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4062. return info, err
  4063. }
  4064. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  4065. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  4066. return err
  4067. }
  4068. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  4069. drugFlow := models.DrugFlow{}
  4070. err := XTWriteDB().Model(&drugFlow).Where("drug_id = ? and warehousing_id= ? and status = 1", drugid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit, "min_unit": flow.MinUnit, "price": flow.Price, "expire_date": flow.ExpireDate}).Error
  4071. return err
  4072. }
  4073. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  4074. drugFlow := models.DrugFlow{}
  4075. err := XTWriteDB().Model(&drugFlow).Where("drug_id = ? and warehousing_detail_id= ? and status = 1", drugid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit, "min_unit": flow.MinUnit, "price": flow.Price, "expire_date": flow.ExpireDate}).Error
  4076. return err
  4077. }
  4078. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  4079. stock := models.DrugCancelStock{}
  4080. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4081. return stock, err
  4082. }
  4083. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  4084. db := XTReadDB().Model(&info)
  4085. if warehouse_info_id > 0 {
  4086. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  4087. }
  4088. if drug_id > 0 {
  4089. db = db.Where("drug_id = ? and status =1", drug_id)
  4090. }
  4091. err = db.Find(&info).Error
  4092. return info, err
  4093. }
  4094. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  4095. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  4096. return info, err
  4097. }
  4098. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  4099. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  4100. return config, err
  4101. }
  4102. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  4103. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  4104. return config, err
  4105. }
  4106. func GetSearchGoodWarehouseList(keyword string, orgid int64) (info []*models.BloodWarehouseInfo, err error) {
  4107. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4108. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  4109. fmt.Println(table)
  4110. likeKey := "%" + keyword + "%"
  4111. if orgid > 0 {
  4112. db = db.Where("x.org_id = ?", orgid)
  4113. }
  4114. err = db.Group("x.good_id").Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,t.dealer,x.remark,x.warehousing_order,x.warehousing_unit,x.stock_count,x.license_number,x.warehousing_unit,t.good_name,t.specification_name,t.manufacturer,t.packing_price,t.id as good_id,t.packing_unit,t.buy_price").Joins("left join xt_good_information as t on t.id = x.good_id").Where("t.good_name like ? and t.org_id = ?", likeKey, orgid).Order("x.ctime desc").Scan(&info).Error
  4115. return info, err
  4116. }
  4117. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  4118. err := XTWriteDB().Create(&adjustPrice).Error
  4119. return err
  4120. }
  4121. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  4122. likeKey := "%" + keyword + "%"
  4123. offset := (page - 1) * limit
  4124. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  4125. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4126. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4127. fmt.Println(table, tab)
  4128. if startime > 0 {
  4129. db = db.Where("x.start_time>=?", startime)
  4130. }
  4131. if endtime > 0 {
  4132. db = db.Where("x.start_time<=?", endtime)
  4133. }
  4134. if len(keyword) > 0 {
  4135. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  4136. }
  4137. if orgid > 0 {
  4138. db = db.Where("x.user_org_id = ?", orgid)
  4139. }
  4140. err = db.Group("x.id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,t.packing_unit").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&list).Error
  4141. return list, total, err
  4142. }
  4143. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  4144. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  4145. return err
  4146. }
  4147. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  4148. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  4149. return err
  4150. }
  4151. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  4152. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  4153. return info, err
  4154. }
  4155. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  4156. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  4157. return adjust, err
  4158. }
  4159. func CreateReportPrice(price *models.XtStockReportPrice) error {
  4160. err := XTWriteDB().Create(&price).Error
  4161. return err
  4162. }
  4163. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  4164. likeKey := "%" + keyword + "%"
  4165. offset := (page - 1) * limit
  4166. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  4167. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4168. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4169. fmt.Println(table, tab)
  4170. if startime > 0 {
  4171. db = db.Where("x.ctime>=?", startime)
  4172. }
  4173. if endtime > 0 {
  4174. db = db.Where("x.ctime<=?", endtime)
  4175. }
  4176. if len(keyword) > 0 {
  4177. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  4178. }
  4179. if orgid > 0 {
  4180. db = db.Where("x.user_org_id = ?", orgid)
  4181. }
  4182. err = db.Group("x.id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  4183. return adjust, total, err
  4184. }
  4185. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64) (adjust []*models.VmStockInventory, total int64, err error) {
  4186. likeKey := "%" + keyword + "%"
  4187. offset := (page - 1) * limit
  4188. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  4189. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4190. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4191. fmt.Println(table, tab)
  4192. if len(keyword) > 0 {
  4193. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  4194. }
  4195. if orgid > 0 {
  4196. db = db.Where("x.user_org_id = ?", orgid)
  4197. }
  4198. if startime > 0 {
  4199. db = db.Where("x.ctime >=?", startime)
  4200. }
  4201. if endtime > 0 {
  4202. db = db.Where("x.ctime <=?", endtime)
  4203. }
  4204. err = db.Group("x.id").Select("x.ctime,x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  4205. return adjust, total, err
  4206. }
  4207. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  4208. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  4209. if inventory_type == 0 {
  4210. db = db.Where("x.inventory_type = 4")
  4211. }
  4212. if inventory_type > 0 {
  4213. db = db.Where("x.inventory_type = ?", inventory_type)
  4214. }
  4215. if len(keyword) > 0 {
  4216. likeKey := "%" + keyword + "%"
  4217. db = db.Where("x.warehousing_order like ?", likeKey)
  4218. }
  4219. if orgid > 0 {
  4220. db = db.Where("x.user_org_id = ?", orgid)
  4221. }
  4222. err = db.Group("x.good_id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,sum(x.count) as count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number").Scan(&adjust).Error
  4223. return adjust, err
  4224. }
  4225. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  4226. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  4227. if orgid > 0 {
  4228. db = db.Where("x.user_org_id = ?", orgid)
  4229. }
  4230. err = db.Group("x.good_id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,sum(x.count) as count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number").Scan(&adjust).Error
  4231. return adjust, err
  4232. }
  4233. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  4234. err := XTWriteDB().Model(&reportPrice).Where("id in(?) and status = 1", ids).Updates(map[string]interface{}{"checker": reportPrice.Checker, "checker_time": reportPrice.CheckerTime, "checker_status": reportPrice.CheckerStatus}).Error
  4235. return err
  4236. }
  4237. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  4238. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  4239. return report, err
  4240. }
  4241. func CreateInentory(inventory models.XtStockInventory) error {
  4242. err = XTWriteDB().Create(&inventory).Error
  4243. return err
  4244. }
  4245. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  4246. likeKey := "%" + keyword + "%"
  4247. offset := (page - 1) * limit
  4248. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  4249. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4250. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4251. fmt.Println(table, tab)
  4252. if startime > 0 {
  4253. db = db.Where("x.ctime>=?", startime)
  4254. }
  4255. if endtime > 0 {
  4256. db = db.Where("x.ctime<=?", endtime)
  4257. }
  4258. if len(keyword) > 0 {
  4259. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  4260. }
  4261. if orgid > 0 {
  4262. db = db.Where("x.user_org_id = ?", orgid)
  4263. }
  4264. if inventory_status > 0 {
  4265. db = db.Where("checker_status = ?", inventory_status)
  4266. }
  4267. err = db.Group("x.id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  4268. return adjust, total, err
  4269. }
  4270. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  4271. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  4272. return err
  4273. }
  4274. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  4275. info := models.XtStockAdjustPrice{}
  4276. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4277. return info, err
  4278. }
  4279. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  4280. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  4281. return list, err
  4282. }
  4283. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  4284. info := models.WarehousingInfo{}
  4285. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  4286. //return info, err
  4287. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4288. if id > 0 {
  4289. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  4290. }
  4291. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  4292. return info, err
  4293. }
  4294. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  4295. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"new_price": info.NewPrice, "good_name": info.GoodName, "packing_price": info.PackingPrice, "good_id": info.GoodId, "remark": info.Remark, "manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  4296. return err
  4297. }
  4298. func DeleteStockPrice(id int64) error {
  4299. info := models.XtStockAdjustPrice{}
  4300. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4301. return err
  4302. }
  4303. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  4304. report := models.XtStockReportPrice{}
  4305. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  4306. return report, err
  4307. }
  4308. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  4309. err := XTWriteDB().Model(&info).Where("id = ?", id).Updates(map[string]interface{}{"count": info.Count, "specification_name": info.SpecificationName, "good_name": info.GoodName, "number": info.Number, "warehousing_unit": info.WarehousingUnit, "buy_price": info.BuyPrice, "packing_price": info.PackingPrice, "good_origin_place": info.GoodOriginPlace, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "total": info.Total, "remark": info.Remark, "warehousing_info_id": info.WarehousingInfoId, "good_id": info.GoodId}).Error
  4310. return err
  4311. }
  4312. func DeleteDamage(id int64) error {
  4313. adjust := models.XtStockReportPrice{}
  4314. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4315. return err
  4316. }
  4317. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  4318. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  4319. return adjust, err
  4320. }
  4321. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  4322. inventory := models.XtStockInventory{}
  4323. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  4324. return inventory, err
  4325. }
  4326. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  4327. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"good_id": inventory.GoodId, "good_name": inventory.GoodName, "specification_name": inventory.SpecificationName, "number": inventory.Number, "warehousing_unit": inventory.WarehousingUnit, "buy_price": inventory.BuyPrice, "packing_price": inventory.PackingPrice, "total": inventory.Total, "good_origin_place": inventory.GoodOriginPlace, "license_number": inventory.LicenseNumber, "dealer": inventory.Dealer, "manufacturer": inventory.Dealer, "count": inventory.Count, "remark": inventory.Remark}).Error
  4328. return err
  4329. }
  4330. func DeleteInventory(id int64) error {
  4331. inventory := models.XtStockInventory{}
  4332. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4333. return err
  4334. }
  4335. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64) (list []*models.VmStockInventory, total int64, err error) {
  4336. likeKey := "%" + keyword + "%"
  4337. offset := (page - 1) * limit
  4338. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  4339. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4340. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4341. fmt.Println(table, tab)
  4342. if len(keyword) > 0 {
  4343. db = db.Where("x.good_name like ?", likeKey)
  4344. }
  4345. if orgid > 0 {
  4346. db = db.Where("x.user_org_id = ?", orgid)
  4347. }
  4348. err = db.Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.expire_date,x.product_date,t.packing_unit,x.last_stock_count,x.type,x.inventory_type,x.stock_count").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&list).Error
  4349. return list, total, err
  4350. }
  4351. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  4352. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  4353. return list, err
  4354. }
  4355. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  4356. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  4357. return list, err
  4358. }
  4359. func DeleteDrugPrice(id int64) error {
  4360. info := models.XtDrugAdjustPrice{}
  4361. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4362. return err
  4363. }
  4364. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  4365. info := models.WarehousingInfo{}
  4366. db := XTReadDB().Model(&info).Where("status = 1")
  4367. if goodid > 0 {
  4368. db = db.Where("good_id = ?", goodid)
  4369. }
  4370. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  4371. return info, err
  4372. }
  4373. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  4374. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4375. return err
  4376. }
  4377. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  4378. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  4379. return err
  4380. }
  4381. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  4382. err := XTWriteDB().Create(&record).Error
  4383. return err
  4384. }
  4385. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  4386. info := models.WarehousingInfo{}
  4387. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  4388. //return info, err
  4389. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4390. if id > 0 {
  4391. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  4392. }
  4393. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  4394. return info, err
  4395. }
  4396. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  4397. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  4398. return info, err
  4399. }
  4400. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  4401. info := models.DrugWarehouseInfo{}
  4402. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  4403. return info, err
  4404. }
  4405. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  4406. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber}).Error
  4407. return err
  4408. }
  4409. func GetGoodInventoryWarehouseList(id int64) (info []*models.WarehousingInfo, err error) {
  4410. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1", id).Preload("GoodInfo", "status = 1").Find(&info).Error
  4411. return info, err
  4412. }
  4413. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4414. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4415. return err
  4416. }
  4417. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  4418. db := XTReadDB().Model(&list).Where(" status = 1")
  4419. if good_id > 0 {
  4420. db = db.Where("good_id = ?", good_id)
  4421. }
  4422. if len(keyword) > 0 {
  4423. likeKey := "%" + keyword + "%"
  4424. db = db.Where("warehousing_order like ?", likeKey)
  4425. }
  4426. if inventory_type > 0 {
  4427. db = db.Where("inventory_type = ?", inventory_type)
  4428. }
  4429. if inventory_type == 0 {
  4430. db = db.Where("inventory_type = 4")
  4431. }
  4432. err = db.Find(&list).Error
  4433. return list, err
  4434. }
  4435. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  4436. flow := models.DrugFlow{}
  4437. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  4438. if err == gorm.ErrRecordNotFound {
  4439. return nil, err
  4440. }
  4441. if err != nil {
  4442. return nil, err
  4443. }
  4444. return &flow, nil
  4445. }
  4446. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  4447. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  4448. return info, err
  4449. }
  4450. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  4451. info := models.XtDrugWarehouseInfo{}
  4452. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  4453. return info, err
  4454. }
  4455. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4456. info := models.WarehousingInfo{}
  4457. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  4458. return info, err
  4459. }
  4460. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  4461. flow := models.VmStockFlow{}
  4462. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  4463. if err == gorm.ErrRecordNotFound {
  4464. return nil, err
  4465. }
  4466. if err != nil {
  4467. return nil, err
  4468. }
  4469. return &flow, nil
  4470. }
  4471. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  4472. info := models.GoodInfo{}
  4473. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  4474. return info, err
  4475. }
  4476. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  4477. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  4478. return err
  4479. }
  4480. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  4481. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  4482. return err
  4483. }
  4484. func UpdateWarehouseInfoByGoodIdOne(info models.WarehousingInfo, id int64) error {
  4485. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark}).Error
  4486. return err
  4487. }
  4488. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  4489. err := XTWriteDB().Model(&flow).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, good_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  4490. return err
  4491. }
  4492. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  4493. err := XTWriteDB().Model(&flow).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, good_id).Updates(map[string]interface{}{"count": flow.Count, "number": flow.Number, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate, "Price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "license_number": flow.LicenseNumber}).Error
  4494. return err
  4495. }
  4496. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  4497. err := XTWriteDB().Model(&flow).Where("warehouse_out_detail_id = ? and status = 1", wareoutdetailid).Updates(map[string]interface{}{"count": flow.Count, "number": flow.Number, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate, "Price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "license_number": flow.LicenseNumber, "price": flow.Price}).Error
  4498. return err
  4499. }
  4500. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  4501. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  4502. return err
  4503. }
  4504. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  4505. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4506. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4507. fmt.Println(table)
  4508. if orgid > 0 {
  4509. db = db.Where("x.org_id = ?", orgid)
  4510. }
  4511. err = db.Select("x.id,x.good_id,x.number,x.number,x.product_date,x.expiry_date,x.warehousing_unit,x.dealer,x.manufacturer,t.good_name,t.specification_name,t.packing_unit,t.remark").Joins("left join xt_good_information as t on t.id = x.good_id").Group("x.good_id,x.number").Scan(&info).Error
  4512. return info, err
  4513. }
  4514. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  4515. warehouseInfo := models.XtDrugWarehouseInfo{}
  4516. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit}).Error
  4517. return err
  4518. }
  4519. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  4520. err := XTWriteDB().Create(&info).Error
  4521. return err
  4522. }
  4523. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  4524. flow := models.VmStockFlow{}
  4525. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  4526. if err == gorm.ErrRecordNotFound {
  4527. return nil, err
  4528. }
  4529. if err != nil {
  4530. return nil, err
  4531. }
  4532. return &flow, nil
  4533. }
  4534. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  4535. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  4536. return err
  4537. }
  4538. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  4539. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count, "manufacturer": flow.Count, "dealer": flow.Dealer, "number": flow.Number, "license_number": flow.LicenseNumber, "price": flow.Price, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate}).Error
  4540. return err
  4541. }
  4542. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  4543. flow := models.DrugWarehouseOutInfo{}
  4544. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  4545. return flow, err
  4546. }
  4547. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  4548. info := models.WarehouseOutInfo{}
  4549. err := XTReadDB().Model(&info).Where("good_id = ? and patient_id = ? and sys_record_time = ? and status = 1", goodid, patient_id, record_date).First(&info).Error
  4550. return info, err
  4551. }
  4552. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  4553. err := XTWriteDB().Model(&info).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1", drugid, patientid, sys_record_time).Updates(map[string]interface{}{"count": info.Count, "count_unit": info.CountUnit}).Error
  4554. return err
  4555. }
  4556. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  4557. err := XTWriteDB().Model(&info).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and warehouse_info_id = ?", drugid, patientid, sys_record_time, warehouse_info_id).Updates(map[string]interface{}{"count": info.Count, "count_unit": info.CountUnit}).Error
  4558. return err
  4559. }
  4560. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  4561. info := models.DrugWarehouseOutInfo{}
  4562. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and warehouse_info_id = ? and status = 1", drugid, patientid, recorddate, warehosueinfoid).Find(&info).Error
  4563. if err == gorm.ErrRecordNotFound {
  4564. return nil, err
  4565. }
  4566. if err != nil {
  4567. return nil, err
  4568. }
  4569. return &info, nil
  4570. }
  4571. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  4572. drugFlow := models.DrugFlow{}
  4573. err := XTReadDB().Where("warehouse_out_order_number = ? and drug_id =? and patient_id = ? and status = 1", warehouse_out_order_number, drugid, patientid).Find(&drugFlow).Error
  4574. if err == gorm.ErrRecordNotFound {
  4575. return nil, err
  4576. }
  4577. if err != nil {
  4578. return nil, err
  4579. }
  4580. return &drugFlow, nil
  4581. }
  4582. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  4583. err := XTWriteDB().Model(&flow).Where("warehouse_out_order_number = ? and drug_id = ? and patient_id = ? and status = 1", warehouse_out_order_number, drugid, patient_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit}).Error
  4584. return err
  4585. }
  4586. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  4587. err := XTWriteDB().Model(&flow).Where("warehouse_out_order_number = ? and drug_id = ? and patient_id = ? and status = 1 and warehousing_detail_id = ?", warehouse_out_order_number, drugid, patient_id, warehousing_detail_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit}).Error
  4588. return err
  4589. }
  4590. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  4591. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  4592. return detail, err
  4593. }
  4594. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  4595. detail := models.DrugAutomaticReduceDetail{}
  4596. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  4597. if err == gorm.ErrRecordNotFound {
  4598. return nil, err
  4599. }
  4600. if err != nil {
  4601. return nil, err
  4602. }
  4603. return &detail, nil
  4604. }
  4605. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  4606. err := XTWriteDB().Model(&auto).Where("patient_id = ? and record_time = ? and drug_id = ? and status = 1", patientid, recordate, drugid).Updates(map[string]interface{}{"count": auto.Count, "count_unit": auto.CountUnit}).Error
  4607. return err
  4608. }
  4609. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  4610. err := XTWriteDB().Model(&auto).Where("patient_id = ? and record_time = ? and drug_id = ? and status = 1 and warehouse_info_id = ?", patientid, recordate, drugid, warehouse_info_id).Updates(map[string]interface{}{"count": auto.Count, "count_unit": auto.CountUnit}).Error
  4611. return err
  4612. }
  4613. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  4614. info := models.DrugWarehouseInfo{}
  4615. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4616. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4617. return info, err
  4618. }