stock_service.go 412KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  12. err := writeDb.Create(&dealer).Error
  13. return err, dealer
  14. }
  15. func ModifyDealer(dealer *models.Dealer) error {
  16. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  17. "dealer_name": dealer.DealerName,
  18. "contact": dealer.Contact,
  19. "contact_phone": dealer.ContactPhone,
  20. "platform_number": dealer.PlatformNumber,
  21. "email": dealer.Email,
  22. "contact_address": dealer.ContactAddress,
  23. "modifier": dealer.Modifier,
  24. "mtime": time.Now().Unix(),
  25. "remark": dealer.Remark,
  26. "wubi": dealer.WuBi,
  27. "pinyin": dealer.PinYin,
  28. }).Error
  29. return err
  30. }
  31. func FindAllDealerTotal(orgId int64) (total int64) {
  32. db := readDb.Model(&models.Dealer{})
  33. db = db.Where("org_id = ? ", orgId).Count(&total)
  34. return
  35. }
  36. // 获取经销商的名称
  37. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  38. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  39. return
  40. }
  41. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  42. offset := (page - 1) * limit
  43. db := readDb.Model(&models.Dealer{})
  44. db = db.Where("org_id = ? AND status = 1", orgId)
  45. if key != 0 {
  46. db = db.Where("id = ? ", key)
  47. offset = 0
  48. limit = 10
  49. }
  50. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  51. return
  52. }
  53. func DeleteDealerById(id int64) error {
  54. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  55. return err
  56. }
  57. func FindDealerById(id int64) (*models.Dealer, error) {
  58. dealer := &models.Dealer{}
  59. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  60. return dealer, err
  61. }
  62. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  63. err := writeDb.Create(&manufacturer).Error
  64. return err, manufacturer
  65. }
  66. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  67. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  68. "manufacturer_name": manufacturer.ManufacturerName,
  69. "contact": manufacturer.Contact,
  70. "contact_phone": manufacturer.ContactPhone,
  71. "platform_number": manufacturer.PlatformNumber,
  72. "email": manufacturer.Email,
  73. "contact_address": manufacturer.ContactAddress,
  74. "modifier": manufacturer.Modifier,
  75. "mtime": time.Now().Unix(),
  76. "remark": manufacturer.Remark,
  77. "wubi": manufacturer.WuBi,
  78. "pinyin": manufacturer.PinYin,
  79. }).Error
  80. return err
  81. }
  82. // 获取生产厂商名称
  83. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  84. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  85. return
  86. }
  87. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  88. offset := (page - 1) * limit
  89. db := readDb.Model(&models.Manufacturer{})
  90. db = db.Where("org_id = ? AND status = 1", orgId)
  91. if key != 0 {
  92. db = db.Where("id = ?", key)
  93. offset = 0
  94. limit = 10
  95. }
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  97. return
  98. }
  99. func DeleteManufacturerById(id int64) error {
  100. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  101. return err
  102. }
  103. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  104. dealer := &models.Manufacturer{}
  105. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  106. return dealer, err
  107. }
  108. func FindManufacturerTotal(orgId int64) (total int64) {
  109. db := readDb.Model(&models.Manufacturer{})
  110. db = db.Where("org_id = ?", orgId).Count(&total)
  111. return
  112. }
  113. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  114. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  115. return dealers, err
  116. }
  117. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  118. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  119. return manufacturer, err
  120. }
  121. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id = ?", orgId).Count(&total)
  124. return
  125. }
  126. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  127. goodsType := models.GoodsType{}
  128. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  129. if err == gorm.ErrRecordNotFound {
  130. return nil, err
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &goodsType, nil
  136. }
  137. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  138. goodsType := models.GoodsType{}
  139. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  140. if err == gorm.ErrRecordNotFound {
  141. return nil, err
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return &goodsType, nil
  147. }
  148. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  149. err := writeDb.Create(&goodType).Error
  150. return err, goodType
  151. }
  152. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  153. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  154. "modifier": goodType.Modifier,
  155. "mtime": time.Now().Unix(),
  156. "remark": goodType.Remark,
  157. "type_name": goodType.TypeName,
  158. "out_stock": goodType.OutStock,
  159. "stock_attribute": goodType.StockAttribute,
  160. }).Error
  161. return err, goodType
  162. }
  163. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  164. offset := (page - 1) * limit
  165. db := readDb.Model(&models.GoodsType{})
  166. db = db.Where("org_id=? AND status = 1", orgId)
  167. if len(keyword) > 0 {
  168. likeKey := "%" + keyword + "%"
  169. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  170. }
  171. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  172. return
  173. }
  174. func DeleteGoodTypeById(id int64, operateId int64) error {
  175. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  176. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  177. return err
  178. }
  179. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  180. goodType := &models.GoodsType{}
  181. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  182. return goodType, err
  183. }
  184. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  185. db := readDb.Model(&models.GoodInfo{})
  186. db = db.Where("org_id = ?", orgId).Count(&total)
  187. return
  188. }
  189. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  190. err := writeDb.Create(&goodInfo).Error
  191. return err, goodInfo
  192. }
  193. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  194. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  195. "good_type_id": goodInfo.GoodTypeId,
  196. "modifier": goodInfo.Modifier,
  197. "mtime": time.Now().Unix(),
  198. "remark": goodInfo.Remark,
  199. "specification_name": goodInfo.SpecificationName,
  200. "good_unit": goodInfo.GoodUnit,
  201. "buy_price": goodInfo.BuyPrice,
  202. "sell_price": goodInfo.SellPrice,
  203. "manufacturer": goodInfo.Manufacturer,
  204. "dealer": goodInfo.Dealer,
  205. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  206. "stock_warn_count": goodInfo.StockWarnCount,
  207. "is_reuse": goodInfo.IsReuse,
  208. "good_name": goodInfo.GoodName,
  209. "pinyin": goodInfo.Pinyin,
  210. "wubi": goodInfo.Wubi,
  211. "good_kind": goodInfo.GoodKind,
  212. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  213. "retail_price": goodInfo.PackingPrice,
  214. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  215. "is_special_diseases": goodInfo.IsSpecialDiseases,
  216. "is_record": goodInfo.IsRecord,
  217. "statistics_category": goodInfo.StatisticsCategory,
  218. "good_status": goodInfo.GoodStatus,
  219. "default_count": goodInfo.DefaultCount,
  220. "sign": goodInfo.Sign,
  221. "is_default": goodInfo.IsDefault,
  222. "is_charge_use": goodInfo.IsChargeUse,
  223. "is_charge_predict": goodInfo.IsChargePredict,
  224. "is_statistics_work": goodInfo.IsStatisticsWork,
  225. "sort": goodInfo.Sort,
  226. "is_doctor_use": goodInfo.IsDoctorUse,
  227. "agent": goodInfo.Agent,
  228. "good_number": goodInfo.GoodNumber,
  229. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  230. "special_medical": goodInfo.SpecialMedical,
  231. "production_type": goodInfo.ProductionType,
  232. "min_number": goodInfo.MinNumber,
  233. "packing_unit": goodInfo.PackingUnit,
  234. "packing_price": goodInfo.PackingPrice,
  235. "default_count_unit": goodInfo.DefaultCountUnit,
  236. "min_unit": goodInfo.MinUnit,
  237. "register_number": goodInfo.RegisterNumber,
  238. "provinces_code": goodInfo.ProvincesCode,
  239. "is_user": goodInfo.IsUser,
  240. "number": goodInfo.Number,
  241. "is_warehouse": goodInfo.IsWarehouse,
  242. "bbx01": goodInfo.Bbx01,
  243. "bby01": goodInfo.Bby01,
  244. "zuobiao_id": goodInfo.Bbx01,
  245. "first_letter": goodInfo.FirstLetter,
  246. "is_show": goodInfo.IsShow,
  247. }).Error
  248. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  249. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. return err, goodInfo
  255. }
  256. 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) {
  257. offset := (page - 1) * limit
  258. db := readDb.Model(&models.GoodInfo{})
  259. db = db.Where("org_id = ? AND status = 1", orgId)
  260. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  261. if len(keyword) > 0 {
  262. likeKey := "%" + keyword + "%"
  263. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  264. if is_use > 0 {
  265. if is_use == 2 {
  266. db = db.Where("find_in_set('停用',good_status) = 0")
  267. } else {
  268. db = db.Where("find_in_set('停用',good_status) > 0")
  269. }
  270. }
  271. if is_charge > 0 {
  272. if is_charge == 1 {
  273. db = db.Where("find_in_set('收费',good_status) > 0")
  274. } else {
  275. db = db.Where("find_in_set('收费',good_status) = 0")
  276. }
  277. }
  278. if good_kind > 0 {
  279. db = db.Where(" good_kind = ?", good_kind)
  280. }
  281. if is_mark > 0 {
  282. db = db.Where("is_mark = ?", is_mark)
  283. }
  284. if manufacturer > 0 {
  285. db = db.Where("manufacturer = ?", manufacturer)
  286. }
  287. if is_type > 0 {
  288. db = db.Where("good_type_id = ?", is_type)
  289. }
  290. } else {
  291. if is_use > 0 {
  292. if is_use == 2 {
  293. db = db.Where("find_in_set('停用',good_status) = 0")
  294. } else {
  295. db = db.Where("find_in_set('停用',good_status) > 0")
  296. }
  297. }
  298. if is_charge > 0 {
  299. if is_charge == 1 {
  300. db = db.Where("find_in_set('收费',good_status) > 0")
  301. } else {
  302. db = db.Where("find_in_set('收费',good_status) = 0")
  303. }
  304. }
  305. if good_kind > 0 {
  306. db = db.Where(" good_kind = ?", good_kind)
  307. }
  308. if is_mark > 0 {
  309. db = db.Where("is_mark = ?", is_mark)
  310. }
  311. if manufacturer > 0 {
  312. db = db.Where("manufacturer = ?", manufacturer)
  313. }
  314. if is_type > 0 {
  315. db = db.Where("good_type_id = ?", is_type)
  316. }
  317. }
  318. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  319. return
  320. }
  321. func DeleteGoodInfoById(id int64, operateId int64) error {
  322. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  323. return err
  324. }
  325. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  326. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  327. return list, err
  328. }
  329. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  330. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  331. return list, err
  332. }
  333. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  334. goodInfo := &models.GoodInfo{}
  335. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  336. return goodInfo, err
  337. }
  338. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  339. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  340. return goodType, err
  341. }
  342. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  343. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  344. return goodType, err
  345. }
  346. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  347. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 and is_warehouse = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  348. return goodInfo, err
  349. }
  350. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  351. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  352. return goodInfo, err
  353. }
  354. func FindAllGoodInfoThree(org_id int64) (goodInfo []*models.XtGoodInformationFourty, err error) {
  355. err = XTReadDB().Model(&models.XtGoodInformationFourty{}).Where("org_id = ? AND status = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  356. return goodInfo, err
  357. }
  358. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  359. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  360. return total, err
  361. }
  362. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  363. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  364. return total, err
  365. }
  366. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  367. err := writeDb.Create(&warehouse).Error
  368. return err
  369. }
  370. func UpdateWarehouse(id int64, storehouse_id int64) error {
  371. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  372. return err
  373. }
  374. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  375. err := writeDb.Create(&warehouse).Error
  376. return err
  377. }
  378. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  379. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  380. return err
  381. }
  382. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  383. err := writeDb.Create(&salesReturn).Error
  384. return err
  385. }
  386. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  387. err := writeDb.Create(&salesReturn).Error
  388. return err
  389. }
  390. func UpDateGoodReturnStatus(ids []string) {
  391. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  392. }
  393. func UpDateCanCelStockStatus(ids []string) {
  394. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  395. }
  396. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  397. if len(warehousingInfo) > 0 {
  398. utx := writeDb.Begin()
  399. if len(warehousingInfo) > 0 {
  400. 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,packing_price,storehouse_id,good_code) VALUES "
  401. insertParams := make([]string, 0)
  402. insertData := make([]interface{}, 0)
  403. for _, info := range warehousingInfo {
  404. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  405. insertData = append(insertData, info.WarehousingId)
  406. insertData = append(insertData, info.GoodId)
  407. insertData = append(insertData, info.GoodTypeId)
  408. insertData = append(insertData, info.Number)
  409. insertData = append(insertData, info.ProductDate)
  410. insertData = append(insertData, info.ExpiryDate)
  411. insertData = append(insertData, info.WarehousingCount)
  412. insertData = append(insertData, info.Price)
  413. insertData = append(insertData, info.TotalPrice)
  414. insertData = append(insertData, info.Dealer)
  415. insertData = append(insertData, info.Manufacturer)
  416. insertData = append(insertData, info.Remark)
  417. insertData = append(insertData, info.Ctime)
  418. insertData = append(insertData, info.Mtime)
  419. insertData = append(insertData, info.Status)
  420. insertData = append(insertData, info.OrgId)
  421. insertData = append(insertData, info.WarehousingOrder)
  422. insertData = append(insertData, info.Type)
  423. insertData = append(insertData, info.WarehousingCount)
  424. insertData = append(insertData, info.LicenseNumber)
  425. insertData = append(insertData, info.PackingPrice)
  426. insertData = append(insertData, info.StorehouseId)
  427. insertData = append(insertData, info.GoodCode)
  428. }
  429. thisSQL += strings.Join(insertParams, ", ")
  430. err = utx.Exec(thisSQL, insertData...).Error
  431. if err != nil {
  432. utx.Rollback()
  433. return
  434. }
  435. }
  436. utx.Commit()
  437. }
  438. return
  439. }
  440. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  441. if len(warehousingInfo) > 0 {
  442. utx := writeDb.Begin()
  443. if len(warehousingInfo) > 0 {
  444. 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,storehouse_id) VALUES "
  445. insertParams := make([]string, 0)
  446. insertData := make([]interface{}, 0)
  447. for _, info := range warehousingInfo {
  448. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  449. insertData = append(insertData, info.WarehousingId)
  450. insertData = append(insertData, info.DrugId)
  451. insertData = append(insertData, info.Number)
  452. insertData = append(insertData, info.ProductDate)
  453. insertData = append(insertData, info.ExpiryDate)
  454. insertData = append(insertData, info.WarehousingCount)
  455. insertData = append(insertData, info.Price)
  456. insertData = append(insertData, info.TotalPrice)
  457. insertData = append(insertData, info.Dealer)
  458. insertData = append(insertData, info.Manufacturer)
  459. insertData = append(insertData, info.Remark)
  460. insertData = append(insertData, info.Ctime)
  461. insertData = append(insertData, info.Mtime)
  462. insertData = append(insertData, info.Status)
  463. insertData = append(insertData, info.OrgId)
  464. insertData = append(insertData, info.WarehousingOrder)
  465. insertData = append(insertData, info.Type)
  466. insertData = append(insertData, info.RetailPrice)
  467. insertData = append(insertData, info.RetailTotalPrice)
  468. insertData = append(insertData, info.StockMaxNumber)
  469. insertData = append(insertData, info.MaxUnit)
  470. insertData = append(insertData, info.MinUnit)
  471. insertData = append(insertData, info.BatchNumber)
  472. insertData = append(insertData, info.StockMinNumber)
  473. insertData = append(insertData, info.StorehouseId)
  474. }
  475. thisSQL += strings.Join(insertParams, ", ")
  476. err = utx.Exec(thisSQL, insertData...).Error
  477. if err != nil {
  478. utx.Rollback()
  479. return
  480. }
  481. }
  482. utx.Commit()
  483. }
  484. return
  485. }
  486. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  487. if len(warehousingInfo) > 0 {
  488. utx := writeDb.Begin()
  489. if len(warehousingInfo) > 0 {
  490. 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,supply_warehouse_id,supply_warehouse_detail_info,storehouse_id,is_check) VALUES "
  491. insertParams := make([]string, 0)
  492. insertData := make([]interface{}, 0)
  493. for _, info := range warehousingInfo {
  494. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  495. insertData = append(insertData, info.WarehousingId)
  496. insertData = append(insertData, info.DrugId)
  497. insertData = append(insertData, info.Number)
  498. insertData = append(insertData, info.ProductDate)
  499. insertData = append(insertData, info.ExpiryDate)
  500. insertData = append(insertData, info.WarehousingCount)
  501. insertData = append(insertData, info.Price)
  502. insertData = append(insertData, info.TotalPrice)
  503. insertData = append(insertData, info.Dealer)
  504. insertData = append(insertData, info.Manufacturer)
  505. insertData = append(insertData, info.Remark)
  506. insertData = append(insertData, info.Ctime)
  507. insertData = append(insertData, info.Mtime)
  508. insertData = append(insertData, info.Status)
  509. insertData = append(insertData, info.OrgId)
  510. insertData = append(insertData, info.WarehousingOrder)
  511. insertData = append(insertData, info.Type)
  512. insertData = append(insertData, info.RetailPrice)
  513. insertData = append(insertData, info.RetailTotalPrice)
  514. insertData = append(insertData, info.StockMaxNumber)
  515. insertData = append(insertData, info.MaxUnit)
  516. insertData = append(insertData, info.MinUnit)
  517. insertData = append(insertData, info.BatchNumber)
  518. insertData = append(insertData, info.StockMinNumber)
  519. insertData = append(insertData, info.SupplyWarehouseId)
  520. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  521. insertData = append(insertData, info.StorehouseId)
  522. insertData = append(insertData, info.IsCheck)
  523. }
  524. thisSQL += strings.Join(insertParams, ", ")
  525. err = utx.Exec(thisSQL, insertData...).Error
  526. if err != nil {
  527. utx.Rollback()
  528. return
  529. }
  530. }
  531. utx.Commit()
  532. }
  533. return
  534. }
  535. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  536. var total float64
  537. var min_number int64
  538. for _, item := range warehousingInfo {
  539. //查询刚入库耗材的库存
  540. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  541. //更新药品库的总量
  542. info, _ := FindeDrugInfo(item.DrugId)
  543. min_number = count.StockMaxNumber * info.MinNumber
  544. str := strconv.FormatInt(min_number, 10)
  545. minFloat, _ := strconv.ParseFloat(str, 64)
  546. total = info.Total + minFloat
  547. lib := models.BaseDrugLib{
  548. Total: total,
  549. }
  550. UpdateBaseDrug(&lib, item.ID)
  551. }
  552. return err
  553. }
  554. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  555. info := models.XtDrugWarehouseInfo{}
  556. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  557. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  558. return info, err
  559. }
  560. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  561. lib := models.BaseDrugLib{}
  562. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  563. return lib, err
  564. }
  565. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  566. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  567. return err
  568. }
  569. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  570. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  571. return err
  572. }
  573. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  574. info := models.DrugWarehouseInfo{}
  575. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  576. return info, err
  577. }
  578. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  579. out := models.DrugWarehouseOut{}
  580. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  581. return out, err
  582. }
  583. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  584. if len(drugflow) > 0 {
  585. utx := writeDb.Begin()
  586. if len(drugflow) > 0 {
  587. 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,storehouse_id) VALUES "
  588. insertParams := make([]string, 0)
  589. insertData := make([]interface{}, 0)
  590. for _, info := range drugflow {
  591. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  592. insertData = append(insertData, info.WarehousingId)
  593. insertData = append(insertData, info.DrugId)
  594. insertData = append(insertData, info.Number)
  595. insertData = append(insertData, info.BatchNumber)
  596. insertData = append(insertData, info.Count)
  597. insertData = append(insertData, info.UserOrgId)
  598. insertData = append(insertData, info.PatientId)
  599. insertData = append(insertData, info.SystemTime)
  600. insertData = append(insertData, info.ConsumableType)
  601. insertData = append(insertData, info.IsSys)
  602. insertData = append(insertData, info.WarehousingOrder)
  603. insertData = append(insertData, info.WarehouseOutId)
  604. insertData = append(insertData, info.WarehouseOutOrderNumber)
  605. insertData = append(insertData, info.IsEdit)
  606. insertData = append(insertData, info.CancelStockId)
  607. insertData = append(insertData, info.CancelOrderNumber)
  608. insertData = append(insertData, info.Manufacturer)
  609. insertData = append(insertData, info.Dealer)
  610. insertData = append(insertData, info.Creator)
  611. insertData = append(insertData, info.UpdateCreator)
  612. insertData = append(insertData, info.Status)
  613. insertData = append(insertData, info.Ctime)
  614. insertData = append(insertData, info.Mtime)
  615. insertData = append(insertData, info.Price)
  616. insertData = append(insertData, info.WarehousingDetailId)
  617. insertData = append(insertData, info.WarehouseOutDetailId)
  618. insertData = append(insertData, info.CancelOutDetailId)
  619. insertData = append(insertData, info.ExpireDate)
  620. insertData = append(insertData, info.ProductDate)
  621. insertData = append(insertData, info.MaxUnit)
  622. insertData = append(insertData, info.MinUnit)
  623. insertData = append(insertData, info.StorehouseId)
  624. }
  625. thisSQL += strings.Join(insertParams, ", ")
  626. err = utx.Exec(thisSQL, insertData...).Error
  627. if err != nil {
  628. utx.Rollback()
  629. return
  630. }
  631. }
  632. utx.Commit()
  633. }
  634. return
  635. }
  636. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  637. info := models.DrugFlow{}
  638. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  639. if err == gorm.ErrRecordNotFound {
  640. return nil, err
  641. }
  642. if err != nil {
  643. return nil, err
  644. }
  645. return &info, nil
  646. }
  647. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  648. info := models.XtDrugWarehouseInfo{}
  649. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  650. return info, err
  651. }
  652. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  653. warehouseInfo := models.XtDrugWarehouseInfo{}
  654. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  655. return err
  656. }
  657. func CreateDrugFlowOne(flow models.DrugFlow) error {
  658. ut := writeDb.Begin()
  659. err := ut.Create(&flow).Error
  660. if err != nil {
  661. ut.Rollback()
  662. return err
  663. }
  664. ut.Commit()
  665. return err
  666. }
  667. func CreateSelfDrugFlowTwo(flow models.XtSelfDrugFlow) error {
  668. ut := writeDb.Begin()
  669. err := ut.Create(&flow).Error
  670. if err != nil {
  671. ut.Rollback()
  672. return err
  673. }
  674. ut.Commit()
  675. return err
  676. }
  677. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  678. err := tx.Create(&flow).Error
  679. if err != nil {
  680. tx.Rollback()
  681. return err
  682. }
  683. return err
  684. }
  685. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  686. detail := models.DrugAutomaticReduceDetail{}
  687. 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
  688. 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
  689. return detail, err
  690. }
  691. func UpdateSelfDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  692. detail := models.XtSelfDrugWarehouseOutInfo{}
  693. err = XTWriteDB().Model(&models.XtSelfDrugWarehouseOutInfo{}).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
  694. return detail, err
  695. }
  696. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  697. flow := models.DrugFlow{}
  698. 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{}{"is_read": 1}).Error
  699. return flow, err
  700. }
  701. func UpdateDrugFlowTensOne(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.XtSelfDrugFlow, error) {
  702. flow := models.XtSelfDrugFlow{}
  703. 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{}{"is_read": 1}).Error
  704. return flow, err
  705. }
  706. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  707. tx := XTWriteDB().Begin()
  708. err := tx.Create(&flow).Error
  709. if err != nil {
  710. tx.Rollback()
  711. return err
  712. }
  713. tx.Commit()
  714. return err
  715. }
  716. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  717. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  718. return info, err
  719. }
  720. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  721. info := models.WarehousingInfo{}
  722. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  723. return info, err
  724. }
  725. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  726. err := XTWriteDB().Create(&info).Error
  727. return err
  728. }
  729. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  730. if storehouse_id == 0 {
  731. 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
  732. }
  733. if storehouse_id > 0 {
  734. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, good_type_id, storehouse_id).Order("ctime").First(&info).Error
  735. }
  736. return info, err
  737. }
  738. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  739. if storehouse_id == 0 {
  740. err = tx.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
  741. if err != gorm.ErrRecordNotFound {
  742. if err != nil {
  743. tx.Rollback()
  744. return info, err
  745. }
  746. }
  747. }
  748. if storehouse_id > 0 {
  749. err = tx.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, good_type_id, storehouse_id).Order("ctime").First(&info).Error
  750. if err != gorm.ErrRecordNotFound {
  751. if err != nil {
  752. tx.Rollback()
  753. return info, err
  754. }
  755. }
  756. }
  757. return info, err
  758. }
  759. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  760. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  761. if good_id > 0 {
  762. db = db.Where("x.good_id = ?", good_id)
  763. }
  764. if good_type_id > 0 {
  765. db = db.Where("x.good_type_id = ?", good_type_id)
  766. }
  767. if storehouse_id > 0 {
  768. db = db.Where("x.storehouse_id = ?", storehouse_id)
  769. }
  770. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  771. return info, err
  772. }
  773. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  774. info := models.WarehousingInfo{}
  775. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  776. return info, err
  777. }
  778. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  779. info := models.WarehousingInfo{}
  780. var err error
  781. if storehouse_id == 0 {
  782. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and is_check = 1", goodId).Find(&info).Error
  783. }
  784. if storehouse_id > 0 {
  785. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and storehouse_id = ? and is_check = 1", goodId, storehouse_id).Find(&info).Error
  786. }
  787. return info, err
  788. }
  789. //药品先进先出,找出最先入库的批次
  790. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  791. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").First(&info).Error
  792. return info, err
  793. }
  794. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  795. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").Last(&info).Error
  796. return info, err
  797. }
  798. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  799. err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").First(&info).Error
  800. if err != gorm.ErrRecordNotFound {
  801. if err != nil {
  802. tx.Rollback()
  803. return info, err
  804. }
  805. }
  806. return info, err
  807. }
  808. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  809. if is_source == 1 {
  810. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and supply_warehouse_id = ? and storehouse_id = ?", drug_id, warehouse_out_id, storehouse_id).Order("ctime").First(&info).Error
  811. }
  812. if is_source == 2 || is_source == 0 {
  813. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ?", drug_id, storehouse_id).Order("ctime").First(&info).Error
  814. }
  815. return info, err
  816. }
  817. // 出库时先进先出,找出最先入库的批次
  818. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  819. 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
  820. return info, err
  821. }
  822. //
  823. //// 出库时先进先出,找出最先入库的批次
  824. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  825. // 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
  826. // return info, err
  827. //}
  828. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  829. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  830. return info, err
  831. }
  832. func FindAllWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, check_type int64) (list []*models.Warehousing, total int64, err error) {
  833. offset := (page - 1) * limit
  834. db := readDb.Model(&models.Warehousing{})
  835. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  836. if len(keywords) > 0 {
  837. likeKey := "%" + keywords + "%"
  838. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  839. 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")
  840. }
  841. if startTime > 0 {
  842. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  843. }
  844. if endTime > 0 {
  845. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  846. }
  847. if storehouse_id > 0 {
  848. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  849. }
  850. if check_type == 0 {
  851. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  852. }
  853. if check_type > 0 {
  854. db = db.Where("xt_warehouse.is_check = ?", check_type)
  855. }
  856. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  857. return
  858. }
  859. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  860. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  861. if orgid > 0 {
  862. db = db.Where("org_id = ?", orgid)
  863. }
  864. if len(keyword) > 0 {
  865. likeKey := "%" + keyword + "%"
  866. db = db.Where("good_name like ?", likeKey)
  867. }
  868. err = db.Find(&goodInfo).Error
  869. return goodInfo, err
  870. }
  871. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  872. db := XTReadDB().Model(&info).Where("status =1")
  873. if len(good_id) > 0 {
  874. db = db.Where("good_id in (?)", good_id)
  875. }
  876. if startime > 0 {
  877. db = db.Where("ctime >=?", startime)
  878. }
  879. if endtime > 0 {
  880. db = db.Where("ctime<=?", endtime)
  881. }
  882. if orgid > 0 {
  883. db = db.Where("org_id = ?", orgid)
  884. }
  885. if storehouse_id > 0 {
  886. db = db.Where("storehouse_id = ?", storehouse_id)
  887. }
  888. err = db.Find(&info).Error
  889. return info, err
  890. }
  891. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  892. db := XTReadDB().Model(&info).Where("status =1")
  893. if len(good_id) > 0 {
  894. db = db.Where("good_id in (?)", good_id)
  895. }
  896. if startime > 0 {
  897. db = db.Where("ctime >=?", startime)
  898. }
  899. if endtime > 0 {
  900. db = db.Where("ctime<=?", endtime)
  901. }
  902. if orgid > 0 {
  903. db = db.Where("org_id = ?", orgid)
  904. }
  905. if storehouse_id > 0 {
  906. db = db.Where("storehouse_id = ?", storehouse_id)
  907. }
  908. err = db.Find(&info).Error
  909. return info, err
  910. }
  911. type VMDrugWarehouse struct {
  912. ID int64 `gorm:"column:id" json:"id" form:"id"`
  913. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  914. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  915. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  916. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  917. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  918. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  919. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  920. Status int64 `gorm:"column:status" json:"status" form:"status"`
  921. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  922. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  923. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  924. Type int64 `gorm:"column:type" json:"type" form:"type"`
  925. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  926. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  927. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  928. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  929. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  930. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  931. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  932. }
  933. func (VMDrugWarehouse) TableName() string {
  934. return "xt_drug_warehouse"
  935. }
  936. type VMDrugWarehouseInfo struct {
  937. ID int64 `gorm:"column:id" json:"id" form:"id"`
  938. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  939. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  940. Number string `gorm:"column:number" json:"number" form:"number"`
  941. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  942. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  943. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  944. Price float64 `gorm:"column:price" json:"price" form:"price"`
  945. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  946. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  947. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  948. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  949. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  950. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  951. Status int64 `gorm:"column:status" json:"status" form:"status"`
  952. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  953. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  954. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  955. Type int64 `gorm:"column:type" json:"type" form:"type"`
  956. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  957. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  958. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  959. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  960. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  961. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  962. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  963. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  964. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  965. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  966. }
  967. func (VMDrugWarehouseInfo) TableName() string {
  968. return "xt_drug_warehouse_info"
  969. }
  970. type Drug struct {
  971. ID int64 `gorm:"column:id" json:"id" form:"id"`
  972. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  973. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  974. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  975. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  976. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  977. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  978. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  979. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  980. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  981. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  982. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  983. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  984. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  985. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  986. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  987. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  988. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  989. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  990. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  991. Number string `gorm:"column:number" json:"number" form:"number"`
  992. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  993. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  994. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  995. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  996. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  997. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  998. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  999. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  1000. Code string `gorm:"column:code" json:"code" form:"code"`
  1001. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  1002. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  1003. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  1004. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  1005. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  1006. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  1007. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  1008. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  1009. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  1010. Label int64 `gorm:"column:label" json:"label" form:"label"`
  1011. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  1012. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  1013. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  1014. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  1015. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  1016. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  1017. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1018. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1019. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1020. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1021. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1022. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1023. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  1024. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1025. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1026. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1027. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1028. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1029. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1030. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1031. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1032. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1033. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1034. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1035. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1036. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1037. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1038. }
  1039. func (Drug) TableName() string {
  1040. return "xt_base_drug"
  1041. }
  1042. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1043. db := readDb.Model(&VMDrugWarehouse{})
  1044. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1045. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1046. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1047. if len(keywords) > 0 {
  1048. likeKey := "%" + keywords + "%"
  1049. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1050. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1051. 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")
  1052. }
  1053. if startTime > 0 {
  1054. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1055. }
  1056. if endTime > 0 {
  1057. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1058. }
  1059. db = db.Count(&total)
  1060. offset := (page - 1) * limit
  1061. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1062. return
  1063. }
  1064. func FindAllDrugWarehousingListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, check_type int64) (list []*VMDrugWarehouse, total int64, err error) {
  1065. db := readDb.Model(&VMDrugWarehouse{})
  1066. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1067. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1068. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1069. if len(keywords) > 0 {
  1070. likeKey := "%" + keywords + "%"
  1071. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1072. 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")
  1073. }
  1074. if startTime > 0 {
  1075. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1076. }
  1077. if endTime > 0 {
  1078. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1079. }
  1080. if storehouse_id > 0 {
  1081. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1082. }
  1083. if check_type > 0 {
  1084. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1085. }
  1086. db = db.Count(&total)
  1087. offset := (page - 1) * limit
  1088. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1089. return
  1090. }
  1091. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1092. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1093. return list, err
  1094. }
  1095. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1096. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1097. return list, err
  1098. }
  1099. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1100. 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
  1101. return list, err
  1102. }
  1103. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1104. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1105. return list, err
  1106. }
  1107. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1108. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1109. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1110. fmt.Println(table)
  1111. if len(id) > 0 {
  1112. db = db.Where("x.warehousing_id in(?)", id)
  1113. }
  1114. if org_id > 0 {
  1115. db = db.Where("x.org_id = ?", org_id)
  1116. }
  1117. err = db.Select("x.id,x.max_unit as limax_unit,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,x.manufacturer,x.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
  1118. return list, err
  1119. }
  1120. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1121. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1122. return warehousing, err
  1123. }
  1124. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1125. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1126. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1127. return warehousing, err
  1128. }
  1129. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1130. 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
  1131. return warehousing, err
  1132. }
  1133. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1134. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1135. if len(id) > 0 {
  1136. db = db.Where("x.id in(?)", id)
  1137. }
  1138. if org_id > 0 {
  1139. db = db.Where("x.org_id = ?", org_id)
  1140. }
  1141. 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
  1142. return warehousing, err
  1143. }
  1144. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1145. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1146. // return warehousing, err
  1147. //
  1148. //}
  1149. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1150. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1151. return
  1152. }
  1153. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1154. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1155. return
  1156. }
  1157. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1158. if len(salesReturnInfo) > 0 {
  1159. utx := writeDb.Begin()
  1160. if len(salesReturnInfo) > 0 {
  1161. 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 "
  1162. insertParams := make([]string, 0)
  1163. insertData := make([]interface{}, 0)
  1164. for _, info := range salesReturnInfo {
  1165. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1166. insertData = append(insertData, info.GoodId)
  1167. insertData = append(insertData, info.SalesReturnId)
  1168. insertData = append(insertData, info.GoodTypeId)
  1169. insertData = append(insertData, info.Count)
  1170. insertData = append(insertData, info.Price)
  1171. insertData = append(insertData, info.Total)
  1172. insertData = append(insertData, info.Ctime)
  1173. insertData = append(insertData, info.Status)
  1174. insertData = append(insertData, info.OrgId)
  1175. insertData = append(insertData, info.OrderNumber)
  1176. insertData = append(insertData, info.Type)
  1177. insertData = append(insertData, info.Dealer)
  1178. insertData = append(insertData, info.Manufacturer)
  1179. }
  1180. thisSQL += strings.Join(insertParams, ", ")
  1181. err = utx.Exec(thisSQL, insertData...).Error
  1182. if err != nil {
  1183. utx.Rollback()
  1184. return
  1185. }
  1186. }
  1187. utx.Commit()
  1188. }
  1189. return
  1190. }
  1191. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1192. if len(salesReturnInfo) > 0 {
  1193. utx := writeDb.Begin()
  1194. if len(salesReturnInfo) > 0 {
  1195. 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 "
  1196. insertParams := make([]string, 0)
  1197. insertData := make([]interface{}, 0)
  1198. for _, info := range salesReturnInfo {
  1199. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1200. insertData = append(insertData, info.DrugId)
  1201. insertData = append(insertData, info.SalesReturnId)
  1202. insertData = append(insertData, info.Count)
  1203. insertData = append(insertData, info.Price)
  1204. insertData = append(insertData, info.Total)
  1205. insertData = append(insertData, info.Ctime)
  1206. insertData = append(insertData, info.Status)
  1207. insertData = append(insertData, info.OrgId)
  1208. insertData = append(insertData, info.OrderNumber)
  1209. insertData = append(insertData, info.Type)
  1210. insertData = append(insertData, info.Dealer)
  1211. insertData = append(insertData, info.Manufacturer)
  1212. insertData = append(insertData, info.RetailPrice)
  1213. insertData = append(insertData, info.RetailTotalPrice)
  1214. }
  1215. thisSQL += strings.Join(insertParams, ", ")
  1216. err = utx.Exec(thisSQL, insertData...).Error
  1217. if err != nil {
  1218. utx.Rollback()
  1219. return
  1220. }
  1221. }
  1222. utx.Commit()
  1223. }
  1224. return
  1225. }
  1226. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1227. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1228. return total, err
  1229. }
  1230. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1231. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1232. return total, err
  1233. }
  1234. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1235. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1236. if err != gorm.ErrRecordNotFound {
  1237. if err != nil {
  1238. tx.Rollback()
  1239. return total, err
  1240. }
  1241. }
  1242. return total, err
  1243. }
  1244. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1245. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1246. return total, err
  1247. }
  1248. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1249. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1250. if err != gorm.ErrRecordNotFound {
  1251. if err != nil {
  1252. tx.Rollback()
  1253. return total, err
  1254. }
  1255. }
  1256. return total, err
  1257. }
  1258. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1259. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1260. // return total, err
  1261. //}
  1262. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1263. db := readDb.Model(&models.SalesReturn{})
  1264. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1265. if len(keywords) > 0 {
  1266. likeKey := "%" + keywords + "%"
  1267. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1268. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1269. 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")
  1270. }
  1271. if startTime > 0 {
  1272. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1273. }
  1274. if endTime > 0 {
  1275. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1276. }
  1277. db = db.Count(&total)
  1278. offset := (page - 1) * limit
  1279. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1280. return
  1281. }
  1282. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1283. db := readDb.Model(&models.DrugSalesReturn{})
  1284. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1285. if len(keywords) > 0 {
  1286. likeKey := "%" + keywords + "%"
  1287. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1288. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1289. 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")
  1290. }
  1291. if startTime > 0 {
  1292. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1293. }
  1294. if endTime > 0 {
  1295. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1296. }
  1297. db = db.Count(&total)
  1298. offset := (page - 1) * limit
  1299. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1300. return
  1301. }
  1302. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1303. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1304. return list, err
  1305. }
  1306. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1307. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1308. return list, err
  1309. }
  1310. type DrugCancelStockInfo struct {
  1311. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1312. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1313. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1314. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1315. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1316. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1317. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1318. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1319. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1320. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1321. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1322. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1323. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1324. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1325. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1326. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1327. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1328. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1329. Number string `gorm:"column:number" json:"number" form:"number"`
  1330. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1331. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1332. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1333. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1334. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1335. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1336. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1337. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1338. }
  1339. func (DrugCancelStockInfo) TableName() string {
  1340. return "xt_drug_cancel_stock_info"
  1341. }
  1342. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1343. err = readDb.Model(&DrugCancelStockInfo{}).Preload("Drug", "status = 1 AND org_id = ? ", org_id).Preload("DrugWarehouseInfo", "status = 1 AND org_id = ? ", org_id).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  1344. return list, err
  1345. }
  1346. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1347. db := readDb.Model(&models.WarehousingInfo{})
  1348. db = db.Where("org_id = ? AND status = 1", orgId)
  1349. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1350. if startTime > 0 {
  1351. db = db.Where("ctime >=?", startTime)
  1352. }
  1353. if endTime > 0 {
  1354. db = db.Where("ctime<= ?", endTime)
  1355. }
  1356. db = db.Count(&total)
  1357. offset := (page - 1) * limit
  1358. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1359. return list, total, err
  1360. }
  1361. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1362. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1363. return total, err
  1364. }
  1365. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1366. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1367. return total, err
  1368. }
  1369. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1370. err = tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1371. if err != gorm.ErrRecordNotFound {
  1372. if err != nil {
  1373. tx.Rollback()
  1374. return
  1375. }
  1376. }
  1377. return total, err
  1378. }
  1379. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1380. err := writeDb.Create(&warehouseOut).Error
  1381. return err
  1382. }
  1383. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1384. err := tx.Create(&warehouseOut).Error
  1385. if err != nil {
  1386. tx.Rollback()
  1387. return err
  1388. }
  1389. return err
  1390. }
  1391. func UpdateSingleWarehoseOut(id int64, storehouse_id int64) error {
  1392. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1393. return err
  1394. }
  1395. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1396. err := writeDb.Create(&warehouseOut).Error
  1397. return err
  1398. }
  1399. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1400. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1401. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1402. if err != gorm.ErrRecordNotFound {
  1403. if err != nil {
  1404. tx.Rollback()
  1405. return selfDrugWarehouse, err
  1406. }
  1407. }
  1408. return selfDrugWarehouse, err
  1409. }
  1410. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1411. err := tx.Create(&warehouseOut).Error
  1412. if err != nil {
  1413. tx.Rollback()
  1414. return err
  1415. }
  1416. return err
  1417. }
  1418. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1419. err := tx.Create(&warehouseOut).Error
  1420. if err != nil {
  1421. tx.Rollback()
  1422. return err
  1423. }
  1424. return err
  1425. }
  1426. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1427. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1428. return err
  1429. }
  1430. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1431. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1432. return err
  1433. }
  1434. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1435. if len(warehouseOutInfo) > 0 {
  1436. utx := writeDb.Begin()
  1437. if len(warehouseOutInfo) > 0 {
  1438. 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 "
  1439. insertParams := make([]string, 0)
  1440. insertData := make([]interface{}, 0)
  1441. for _, info := range warehouseOutInfo {
  1442. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1443. insertData = append(insertData, info.WarehouseOutId)
  1444. insertData = append(insertData, info.GoodId)
  1445. insertData = append(insertData, info.GoodTypeId)
  1446. insertData = append(insertData, info.ProductDate)
  1447. insertData = append(insertData, info.ExpiryDate)
  1448. insertData = append(insertData, info.Count)
  1449. insertData = append(insertData, info.Price)
  1450. insertData = append(insertData, info.TotalPrice)
  1451. insertData = append(insertData, info.Remark)
  1452. insertData = append(insertData, info.Ctime)
  1453. insertData = append(insertData, info.Status)
  1454. insertData = append(insertData, info.OrgId)
  1455. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1456. insertData = append(insertData, info.Type)
  1457. insertData = append(insertData, info.Dealer)
  1458. insertData = append(insertData, info.Manufacturer)
  1459. insertData = append(insertData, info.Number)
  1460. insertData = append(insertData, info.LicenseNumber)
  1461. }
  1462. thisSQL += strings.Join(insertParams, ", ")
  1463. err = utx.Exec(thisSQL, insertData...).Error
  1464. if err != nil {
  1465. utx.Rollback()
  1466. return
  1467. }
  1468. }
  1469. utx.Commit()
  1470. }
  1471. return
  1472. }
  1473. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1474. if len(warehouseOutInfo) > 0 {
  1475. utx := writeDb.Begin()
  1476. if len(warehouseOutInfo) > 0 {
  1477. 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 "
  1478. insertParams := make([]string, 0)
  1479. insertData := make([]interface{}, 0)
  1480. for _, info := range warehouseOutInfo {
  1481. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1482. insertData = append(insertData, info.WarehouseOutId)
  1483. insertData = append(insertData, info.DrugId)
  1484. insertData = append(insertData, info.ProductDate)
  1485. insertData = append(insertData, info.ExpiryDate)
  1486. insertData = append(insertData, info.Count)
  1487. insertData = append(insertData, info.Price)
  1488. insertData = append(insertData, info.TotalPrice)
  1489. insertData = append(insertData, info.Remark)
  1490. insertData = append(insertData, info.Ctime)
  1491. insertData = append(insertData, info.Status)
  1492. insertData = append(insertData, info.OrgId)
  1493. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1494. insertData = append(insertData, info.Type)
  1495. insertData = append(insertData, info.Dealer)
  1496. insertData = append(insertData, info.Manufacturer)
  1497. insertData = append(insertData, info.RetailPrice)
  1498. insertData = append(insertData, info.RetailTotalPrice)
  1499. insertData = append(insertData, info.Number)
  1500. insertData = append(insertData, info.BatchNumber)
  1501. insertData = append(insertData, info.CountUnit)
  1502. }
  1503. thisSQL += strings.Join(insertParams, ", ")
  1504. err = utx.Exec(thisSQL, insertData...).Error
  1505. if err != nil {
  1506. utx.Rollback()
  1507. return
  1508. }
  1509. }
  1510. utx.Commit()
  1511. }
  1512. return
  1513. }
  1514. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1515. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1516. return info, err
  1517. }
  1518. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1519. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1520. return info, err
  1521. }
  1522. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1523. db := readDb.Model(&models.WarehouseOut{})
  1524. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1525. if len(keywords) > 0 {
  1526. likeKey := "%" + keywords + "%"
  1527. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1528. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1529. 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")
  1530. }
  1531. if startTime > 0 {
  1532. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1533. }
  1534. if endTime > 0 {
  1535. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1536. }
  1537. db = db.Count(&total)
  1538. offset := (page - 1) * limit
  1539. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1540. return list, total, err
  1541. }
  1542. func FindAllWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, way_type int64, check_type int64) (list []*models.WarehouseOut, total int64, err error) {
  1543. db := readDb.Model(&models.WarehouseOut{})
  1544. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1545. if len(keywords) > 0 {
  1546. likeKey := "%" + keywords + "%"
  1547. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1548. db = db.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")
  1549. }
  1550. if startTime > 0 {
  1551. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1552. }
  1553. if endTime > 0 {
  1554. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1555. }
  1556. if storehouse_id > 0 {
  1557. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1558. }
  1559. if way_type == 0 {
  1560. db = db.Where("xt_warehouse_out.is_sys = 0 or xt_warehouse_out.is_sys = 1 or xt_warehouse_out.is_sys = 12 or xt_warehouse_out.is_sys = 5")
  1561. }
  1562. if way_type == 1 {
  1563. db = db.Where("xt_warehouse_out.is_sys = 0")
  1564. }
  1565. if way_type == 2 {
  1566. db = db.Where("xt_warehouse_out.is_sys = 1")
  1567. }
  1568. if way_type == 3 {
  1569. db = db.Where("xt_warehouse_out.is_sys = 12")
  1570. }
  1571. if way_type == 4 {
  1572. db = db.Where("xt_warehouse_out.is_sys = 5")
  1573. }
  1574. if check_type > 0 {
  1575. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1576. }
  1577. if check_type == 0 {
  1578. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1579. }
  1580. db = db.Count(&total)
  1581. offset := (page - 1) * limit
  1582. if orgId == 10191 {
  1583. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1584. } else {
  1585. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1586. }
  1587. return list, total, err
  1588. }
  1589. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1590. db := readDb.Model(&models.DrugWarehouseOut{})
  1591. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1592. if len(keywords) > 0 {
  1593. likeKey := "%" + keywords + "%"
  1594. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1595. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1596. 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")
  1597. }
  1598. if startTime > 0 {
  1599. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1600. }
  1601. if endTime > 0 {
  1602. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1603. }
  1604. db = db.Count(&total)
  1605. offset := (page - 1) * limit
  1606. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1607. return list, total, err
  1608. }
  1609. func FindAllDrugWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, way_type int64, check_type int64) (list []*models.DrugWarehouseOut, total int64, err error) {
  1610. db := readDb.Model(&models.DrugWarehouseOut{})
  1611. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1612. if len(keywords) > 0 {
  1613. likeKey := "%" + keywords + "%"
  1614. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1615. 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")
  1616. }
  1617. if startTime > 0 {
  1618. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1619. }
  1620. if endTime > 0 {
  1621. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1622. }
  1623. if storehouse_id > 0 {
  1624. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1625. }
  1626. if check_type > 0 {
  1627. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1628. }
  1629. if way_type == 0 {
  1630. db = db.Where("xt_drug_warehouse_out.is_sys = 0 || xt_drug_warehouse_out.is_sys = 1 || xt_drug_warehouse_out.is_sys = 12 || xt_drug_warehouse_out.is_sys = 5")
  1631. }
  1632. if way_type == 1 {
  1633. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1634. }
  1635. if way_type == 2 {
  1636. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1637. }
  1638. if way_type == 3 {
  1639. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1640. }
  1641. if way_type == 4 {
  1642. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1643. }
  1644. db = db.Count(&total)
  1645. offset := (page - 1) * limit
  1646. if orgId == 10191 {
  1647. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1648. } else {
  1649. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1650. }
  1651. return list, total, err
  1652. }
  1653. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1654. 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
  1655. return list, err
  1656. }
  1657. type DrugWarehouseOutInfo struct {
  1658. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1659. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1660. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1661. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1662. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1663. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1664. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1665. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1666. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1667. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1668. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1669. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1670. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1671. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1672. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1673. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1674. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1675. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1676. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1677. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1678. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1679. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1680. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1681. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1682. Number string `gorm:"column:number" json:"number" form:"number"`
  1683. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1684. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1685. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1686. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1687. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1688. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1689. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1690. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1691. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1692. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1693. }
  1694. func (DrugWarehouseOutInfo) TableName() string {
  1695. return "xt_drug_warehouse_out_info"
  1696. }
  1697. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1698. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1699. return list, err
  1700. }
  1701. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1702. info := &models.WarehousingInfo{}
  1703. type WarehouseInfoCountAmount struct {
  1704. Total int64
  1705. }
  1706. var result WarehouseInfoCountAmount
  1707. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1708. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1709. return result.Total, info
  1710. }
  1711. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1712. type SalesReturnCountAmount struct {
  1713. Total int64
  1714. }
  1715. var result SalesReturnCountAmount
  1716. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1717. return result.Total, err
  1718. }
  1719. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1720. type WarehouseOutInfoCountAmount struct {
  1721. Total int64
  1722. }
  1723. var result WarehouseOutInfoCountAmount
  1724. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1725. return result.Total, err
  1726. }
  1727. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1728. type CancleStockoCountAmount struct {
  1729. Total int64
  1730. }
  1731. var result CancleStockoCountAmount
  1732. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1733. return result.Total, err
  1734. }
  1735. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1736. info := models.WarehousingInfo{}
  1737. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1738. return info, err
  1739. }
  1740. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1741. stock := models.CancelStock{}
  1742. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1743. if err == gorm.ErrRecordNotFound {
  1744. return nil, err
  1745. }
  1746. if err != nil {
  1747. return nil, err
  1748. }
  1749. return &stock, nil
  1750. }
  1751. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1752. stock := models.CancelStock{}
  1753. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1754. if err == gorm.ErrRecordNotFound {
  1755. return nil, err
  1756. }
  1757. if err != nil {
  1758. return nil, err
  1759. }
  1760. return &stock, nil
  1761. }
  1762. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  1763. stock := models.CancelStock{}
  1764. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1765. if err != gorm.ErrRecordNotFound {
  1766. if err != nil {
  1767. tx.Rollback()
  1768. return &stock, err
  1769. }
  1770. }
  1771. return &stock, err
  1772. }
  1773. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1774. err := writeDb.Create(&cancelStock).Error
  1775. return err
  1776. }
  1777. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  1778. err := tx.Create(&cancelStock).Error
  1779. if err != nil {
  1780. tx.Rollback()
  1781. return err
  1782. }
  1783. return err
  1784. }
  1785. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  1786. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1787. return err
  1788. }
  1789. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  1790. stock := models.CancelStock{}
  1791. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1792. return stock, err
  1793. }
  1794. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  1795. stock := models.CancelStock{}
  1796. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1797. if err != gorm.ErrRecordNotFound {
  1798. if err != nil {
  1799. tx.Rollback()
  1800. return stock, err
  1801. }
  1802. }
  1803. return stock, err
  1804. }
  1805. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  1806. stock := models.DrugCancelStock{}
  1807. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1808. return stock, err
  1809. }
  1810. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  1811. stock := models.DrugCancelStock{}
  1812. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1813. if err != gorm.ErrRecordNotFound {
  1814. if err != nil {
  1815. tx.Rollback()
  1816. return stock, err
  1817. }
  1818. }
  1819. return stock, err
  1820. }
  1821. func GetManufactureById(id int64) (models.Manufacturer, error) {
  1822. manufacturer := models.Manufacturer{}
  1823. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1824. return manufacturer, err
  1825. }
  1826. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  1827. manufacturer := models.Manufacturer{}
  1828. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1829. if err != gorm.ErrRecordNotFound {
  1830. if err != nil {
  1831. tx.Rollback()
  1832. return manufacturer, err
  1833. }
  1834. }
  1835. return manufacturer, err
  1836. }
  1837. func GetDealerById(id int64) (models.Dealer, error) {
  1838. dealer := models.Dealer{}
  1839. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1840. return dealer, err
  1841. }
  1842. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  1843. dealer := models.Dealer{}
  1844. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1845. if err != gorm.ErrRecordNotFound {
  1846. if err != nil {
  1847. tx.Rollback()
  1848. return dealer, err
  1849. }
  1850. }
  1851. return dealer, err
  1852. }
  1853. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1854. err := writeDb.Create(&cancelStock).Error
  1855. return err
  1856. }
  1857. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  1858. err := tx.Create(&cancelStock).Error
  1859. if err != nil {
  1860. tx.Rollback()
  1861. return err
  1862. }
  1863. return err
  1864. }
  1865. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  1866. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1867. return err
  1868. }
  1869. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  1870. // err := tx.Create(&cancelStock).Error
  1871. // return err
  1872. //}
  1873. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1874. if len(cancelStockInfo) > 0 {
  1875. utx := writeDb.Begin()
  1876. if len(cancelStockInfo) > 0 {
  1877. 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,storehouse_id) VALUES "
  1878. insertParams := make([]string, 0)
  1879. insertData := make([]interface{}, 0)
  1880. for _, info := range cancelStockInfo {
  1881. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1882. insertData = append(insertData, info.GoodId)
  1883. insertData = append(insertData, info.CancelStockId)
  1884. insertData = append(insertData, info.GoodTypeId)
  1885. insertData = append(insertData, info.Count)
  1886. insertData = append(insertData, info.Price)
  1887. insertData = append(insertData, info.Total)
  1888. insertData = append(insertData, info.Ctime)
  1889. insertData = append(insertData, info.Status)
  1890. insertData = append(insertData, info.OrgId)
  1891. insertData = append(insertData, info.OrderNumber)
  1892. insertData = append(insertData, info.Type)
  1893. insertData = append(insertData, info.Dealer)
  1894. insertData = append(insertData, info.Manufacturer)
  1895. insertData = append(insertData, info.Number)
  1896. insertData = append(insertData, info.RegisterAccount)
  1897. insertData = append(insertData, info.Remark)
  1898. insertData = append(insertData, info.ProductDate)
  1899. insertData = append(insertData, info.ExpiryDate)
  1900. insertData = append(insertData, info.WarehouseInfoId)
  1901. insertData = append(insertData, info.WarehouseInfoId)
  1902. }
  1903. thisSQL += strings.Join(insertParams, ", ")
  1904. err = utx.Exec(thisSQL, insertData...).Error
  1905. if err != nil {
  1906. utx.Rollback()
  1907. return
  1908. }
  1909. }
  1910. utx.Commit()
  1911. }
  1912. return
  1913. }
  1914. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  1915. err := XTWriteDB().Create(&info).Error
  1916. return err
  1917. }
  1918. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  1919. // err := tx.Create(&info).Error
  1920. // return err
  1921. //}
  1922. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  1923. info := models.DrugCancelStockInfo{}
  1924. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1925. return info, err
  1926. }
  1927. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  1928. // info := models.DrugCancelStockInfo{}
  1929. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1930. // return info, err
  1931. //}
  1932. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  1933. err := XTWriteDB().Create(&info).Error
  1934. return err
  1935. }
  1936. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  1937. err := XTWriteDB().Create(&info).Error
  1938. if err != nil {
  1939. tx.Rollback()
  1940. return err
  1941. }
  1942. return err
  1943. }
  1944. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  1945. info := models.CancelStockInfo{}
  1946. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1947. return info, err
  1948. }
  1949. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  1950. info := models.CancelStockInfo{}
  1951. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1952. if err != gorm.ErrRecordNotFound {
  1953. if err != nil {
  1954. tx.Rollback()
  1955. return info, err
  1956. }
  1957. }
  1958. return info, err
  1959. }
  1960. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  1961. info := models.DrugCancelStockInfo{}
  1962. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  1963. return info, err
  1964. }
  1965. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1966. if len(cancelStockInfo) > 0 {
  1967. utx := writeDb.Begin()
  1968. if len(cancelStockInfo) > 0 {
  1969. 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 "
  1970. insertParams := make([]string, 0)
  1971. insertData := make([]interface{}, 0)
  1972. for _, info := range cancelStockInfo {
  1973. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1974. insertData = append(insertData, info.DrugId)
  1975. insertData = append(insertData, info.CancelStockId)
  1976. insertData = append(insertData, info.Count)
  1977. insertData = append(insertData, info.Price)
  1978. insertData = append(insertData, info.Total)
  1979. insertData = append(insertData, info.Ctime)
  1980. insertData = append(insertData, info.Status)
  1981. insertData = append(insertData, info.OrgId)
  1982. insertData = append(insertData, info.OrderNumber)
  1983. insertData = append(insertData, info.Type)
  1984. insertData = append(insertData, info.Dealer)
  1985. insertData = append(insertData, info.Manufacturer)
  1986. insertData = append(insertData, info.RetailPrice)
  1987. insertData = append(insertData, info.RetailTotalPrice)
  1988. insertData = append(insertData, info.RegisterAccount)
  1989. insertData = append(insertData, info.BatchNumber)
  1990. insertData = append(insertData, info.Remark)
  1991. insertData = append(insertData, info.MaxUnit)
  1992. insertData = append(insertData, info.ProductDate)
  1993. insertData = append(insertData, info.ExpiryDate)
  1994. insertData = append(insertData, info.BatchNumberId)
  1995. }
  1996. thisSQL += strings.Join(insertParams, ", ")
  1997. err = utx.Exec(thisSQL, insertData...).Error
  1998. if err != nil {
  1999. utx.Rollback()
  2000. return
  2001. }
  2002. }
  2003. utx.Commit()
  2004. }
  2005. return
  2006. }
  2007. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  2008. db := readDb.Model(&models.CancelStock{})
  2009. db = db.Where("org_id = ? AND status = 1", orgId)
  2010. err = db.Order("ctime desc").Find(&list).Error
  2011. return
  2012. }
  2013. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2014. db := readDb.Model(&models.WarehousingInfo{})
  2015. db = db.Where("org_id = ? AND status = 1", orgId)
  2016. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2017. if startTime > 0 {
  2018. db = db.Where("ctime >=?", startTime)
  2019. }
  2020. if endTime > 0 {
  2021. db = db.Where("ctime<= ?", endTime)
  2022. }
  2023. db = db.Count(&total)
  2024. offset := (page - 1) * limit
  2025. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2026. return
  2027. }
  2028. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2029. db := XTReadDB().Model(&models.StockInfo{})
  2030. if startime > 0 {
  2031. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2032. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2033. })
  2034. }
  2035. if startime == 0 || endtime == 0 {
  2036. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2037. return db.Where("org_id = ? AND status = 1", orgId)
  2038. })
  2039. }
  2040. if endtime > 0 {
  2041. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2042. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2043. })
  2044. }
  2045. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2046. db = db.Group("xt_good_information.id")
  2047. if startime == 0 || endtime == 0 {
  2048. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2049. return db.Where("org_id = ? AND status = 1", orgId)
  2050. })
  2051. }
  2052. if startime > 0 {
  2053. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2054. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2055. })
  2056. }
  2057. if endtime > 0 {
  2058. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2059. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2060. })
  2061. }
  2062. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2063. if startime == 0 || endtime == 0 {
  2064. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2065. }
  2066. if startime > 0 {
  2067. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2068. }
  2069. if endtime > 0 {
  2070. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2071. }
  2072. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2073. if len(keyword) > 0 {
  2074. likeKey := "%" + keyword + "%"
  2075. 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)
  2076. 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")
  2077. }
  2078. if type_name > 0 {
  2079. 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)
  2080. }
  2081. db = db.Count(&total)
  2082. offset := (page - 1) * limit
  2083. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2084. return
  2085. }
  2086. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2087. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2088. return
  2089. }
  2090. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2091. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2092. return
  2093. }
  2094. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2095. 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
  2096. return
  2097. }
  2098. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2099. 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
  2100. return
  2101. }
  2102. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2103. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2104. "good_id": warehouseInfo.GoodId,
  2105. "number": warehouseInfo.Number,
  2106. "product_date": warehouseInfo.ProductDate,
  2107. "expiry_date": warehouseInfo.ExpiryDate,
  2108. "warehousing_count": warehouseInfo.WarehousingCount,
  2109. "price": warehouseInfo.Price,
  2110. "total_price": warehouseInfo.TotalPrice,
  2111. "mtime": warehouseInfo.Mtime,
  2112. "remark": warehouseInfo.Remark,
  2113. }).Error
  2114. return
  2115. }
  2116. func UpDateWarehouseStatus(id int64) (err error) {
  2117. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2118. return
  2119. }
  2120. func UpdatedStockFlowById(id int64) (err error) {
  2121. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2122. return
  2123. }
  2124. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2125. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2126. return
  2127. }
  2128. func UpdateDrugFlowById(id int64) (err error) {
  2129. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2130. return
  2131. }
  2132. func DeleteWarehouse(ids []string) (err error) {
  2133. ut := writeDb.Begin()
  2134. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2135. if err != nil {
  2136. ut.Rollback()
  2137. return
  2138. }
  2139. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2140. if err != nil {
  2141. ut.Rollback()
  2142. return
  2143. }
  2144. ut.Commit()
  2145. return
  2146. }
  2147. func DeleteStockFlow(ids []string) (err error) {
  2148. ut := writeDb.Begin()
  2149. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2150. if err != nil {
  2151. ut.Rollback()
  2152. return
  2153. }
  2154. ut.Commit()
  2155. return
  2156. }
  2157. func DeleteDrugWarehouse(ids []string) (err error) {
  2158. ut := writeDb.Begin()
  2159. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2160. if err != nil {
  2161. ut.Rollback()
  2162. return
  2163. }
  2164. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2165. if err != nil {
  2166. ut.Rollback()
  2167. return
  2168. }
  2169. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2170. if err != nil {
  2171. ut.Rollback()
  2172. return
  2173. }
  2174. ut.Commit()
  2175. return
  2176. }
  2177. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2178. warehouse := &models.Warehousing{}
  2179. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2180. return warehouse, err
  2181. }
  2182. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  2183. warehouseOut := &models.WarehouseOut{}
  2184. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2185. return warehouseOut, err
  2186. }
  2187. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2188. warehouseOut := &models.DrugWarehouseOut{}
  2189. 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
  2190. return warehouseOut, err
  2191. }
  2192. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2193. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2194. return
  2195. }
  2196. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2197. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2198. return
  2199. }
  2200. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2201. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2202. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2203. return
  2204. }
  2205. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2206. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2207. return
  2208. }
  2209. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2210. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2211. if manufacturer_id > 0 {
  2212. db = db.Where("manufacturer = ?", manufacturer_id)
  2213. }
  2214. if dealer_id > 0 {
  2215. db = db.Where("dealer = ?", dealer_id)
  2216. }
  2217. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2218. return goodInfo, err
  2219. }
  2220. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2221. db := readDb.Model(&models.Warehousing{})
  2222. likekey := "%" + keywords + "%"
  2223. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2224. db = db.Where("warehousing_order LIKE ? ", likekey)
  2225. db = db.Count(&total)
  2226. offset := (page - 1) * limit
  2227. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2228. return
  2229. }
  2230. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2231. info := models.WarehousingInfo{}
  2232. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2233. return info, err
  2234. }
  2235. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2236. info := models.WarehousingInfo{}
  2237. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2238. return info, err
  2239. }
  2240. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2241. err = writeDb.Save(&info).Error
  2242. return err
  2243. }
  2244. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2245. err = writeDb.Save(&info).Error
  2246. return err
  2247. }
  2248. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2249. 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, "stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "storehouse_id": info.StorehouseId}).Error
  2250. return err
  2251. }
  2252. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2253. 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, "storehouse_id": info.StorehouseId}).Error
  2254. return err
  2255. }
  2256. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2257. info := models.DrugWarehouseInfo{}
  2258. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2259. return info, err
  2260. }
  2261. func EditWarehousing(warehouse models.Warehousing) {
  2262. 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, "storehouse_id": warehouse.StorehouseId}).Error
  2263. }
  2264. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  2265. 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
  2266. }
  2267. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  2268. 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
  2269. return list, err
  2270. }
  2271. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2272. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2273. 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)
  2274. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2275. return list, err
  2276. }
  2277. func DeleteSalesReturn(ids []string) (err error) {
  2278. ut := writeDb.Begin()
  2279. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2280. if err != nil {
  2281. ut.Rollback()
  2282. return
  2283. }
  2284. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2285. if err != nil {
  2286. ut.Rollback()
  2287. return
  2288. }
  2289. ut.Commit()
  2290. return
  2291. }
  2292. func DeleteDrugSalesReturn(ids []string) (err error) {
  2293. ut := writeDb.Begin()
  2294. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2295. if err != nil {
  2296. ut.Rollback()
  2297. return
  2298. }
  2299. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2300. if err != nil {
  2301. ut.Rollback()
  2302. return
  2303. }
  2304. ut.Commit()
  2305. return
  2306. }
  2307. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2308. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2309. return
  2310. }
  2311. type DrugSalesReturnInfo struct {
  2312. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2313. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2314. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2315. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2316. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2317. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2318. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2319. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2320. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2321. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2322. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2323. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2324. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2325. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2326. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2327. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2328. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2329. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2330. Number string `gorm:"column:number" json:"number" form:"number"`
  2331. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2332. }
  2333. func (DrugSalesReturnInfo) TableName() string {
  2334. return "xt_drug_sales_return_info"
  2335. }
  2336. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2337. 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
  2338. return
  2339. }
  2340. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2341. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2342. return
  2343. }
  2344. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2345. 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
  2346. return
  2347. }
  2348. func UpDateSaleReturnStatus(id int64) (err error) {
  2349. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2350. return
  2351. }
  2352. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2353. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2354. return
  2355. }
  2356. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2357. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2358. return salesReturn, err
  2359. }
  2360. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2361. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2362. return salesReturn, err
  2363. }
  2364. func EditSaleReturn(sales models.SalesReturn) {
  2365. 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
  2366. }
  2367. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2368. 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
  2369. }
  2370. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2371. err = writeDb.Save(&info).Error
  2372. return err
  2373. }
  2374. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2375. err = writeDb.Save(&info).Error
  2376. return err
  2377. }
  2378. func DeleteWarehouseOut(ids []string) (err error) {
  2379. ut := writeDb.Begin()
  2380. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2381. if err != nil {
  2382. ut.Rollback()
  2383. return
  2384. }
  2385. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2386. if err != nil {
  2387. ut.Rollback()
  2388. return
  2389. }
  2390. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2391. if err != nil {
  2392. ut.Rollback()
  2393. return
  2394. }
  2395. ut.Commit()
  2396. return
  2397. }
  2398. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2399. ut := writeDb.Begin()
  2400. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2401. if err != nil {
  2402. ut.Rollback()
  2403. return
  2404. }
  2405. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2406. if err != nil {
  2407. ut.Rollback()
  2408. return
  2409. }
  2410. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2411. if err != nil {
  2412. ut.Rollback()
  2413. return
  2414. }
  2415. ut.Commit()
  2416. return
  2417. }
  2418. func UpDateWarehouseOutStatus(id int64) (err error) {
  2419. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2420. return
  2421. }
  2422. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2423. ut := writeDb.Begin()
  2424. err = ut.Save(&info).Error
  2425. if err != nil {
  2426. ut.Rollback()
  2427. return err
  2428. }
  2429. ut.Commit()
  2430. return err
  2431. }
  2432. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2433. err = tx.Save(&info).Error
  2434. if err != nil {
  2435. tx.Rollback()
  2436. return err
  2437. }
  2438. return err
  2439. }
  2440. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2441. ut := writeDb.Begin()
  2442. err = ut.Save(&info).Error
  2443. if err != nil {
  2444. ut.Rollback()
  2445. return err
  2446. }
  2447. ut.Commit()
  2448. return err
  2449. }
  2450. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2451. err = tx.Save(&info).Error
  2452. if err != nil {
  2453. tx.Rollback()
  2454. return err
  2455. }
  2456. return err
  2457. }
  2458. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2459. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2460. return
  2461. }
  2462. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  2463. 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, "storehouse_id": warehouseOut.StorehouseId}).Error
  2464. }
  2465. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2466. 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, "storehouse_id": warehouseOut.StorehouseId}).Error
  2467. }
  2468. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  2469. err = writeDb.Save(&info).Error
  2470. return err
  2471. }
  2472. func FindAllCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, storehouse_id int64, check_type int64) (list []*models.CancelStock, total int64, err error) {
  2473. db := readDb.Model(&models.CancelStock{})
  2474. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2475. if len(keywords) > 0 {
  2476. likeKey := "%" + keywords + "%"
  2477. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2478. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2479. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2480. }
  2481. if startTime > 0 {
  2482. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2483. }
  2484. if endTime > 0 {
  2485. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2486. }
  2487. if storehouse_id > 0 {
  2488. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2489. }
  2490. if check_type > 0 {
  2491. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2492. }
  2493. db = db.Count(&total)
  2494. offset := (page - 1) * limit
  2495. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2496. return
  2497. }
  2498. func FindAllDrugCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, storehoue_id int64, check_type int64) (list []*models.DrugCancelStock, total int64, err error) {
  2499. db := readDb.Model(&models.DrugCancelStock{})
  2500. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2501. if len(keywords) > 0 {
  2502. likeKey := "%" + keywords + "%"
  2503. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2504. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2505. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_drug_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_drug_cancel_stock.id")
  2506. }
  2507. if startTime > 0 {
  2508. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2509. }
  2510. if endTime > 0 {
  2511. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2512. }
  2513. if storehoue_id > 0 {
  2514. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2515. }
  2516. if check_type > 0 {
  2517. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2518. }
  2519. db = db.Count(&total)
  2520. offset := (page - 1) * limit
  2521. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2522. return
  2523. }
  2524. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2525. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2526. return info, err
  2527. }
  2528. func DeleteCancelStock(ids []string) (err error) {
  2529. ut := writeDb.Begin()
  2530. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2531. if err != nil {
  2532. ut.Rollback()
  2533. return
  2534. }
  2535. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2536. if err != nil {
  2537. ut.Rollback()
  2538. return
  2539. }
  2540. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2541. if err != nil {
  2542. ut.Rollback()
  2543. return
  2544. }
  2545. ut.Commit()
  2546. return
  2547. }
  2548. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2549. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2550. return info, err
  2551. }
  2552. func DeleteDrugCancelStock(ids []string) (err error) {
  2553. ut := writeDb.Begin()
  2554. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2555. if err != nil {
  2556. ut.Rollback()
  2557. return
  2558. }
  2559. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2560. if err != nil {
  2561. ut.Rollback()
  2562. return
  2563. }
  2564. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2565. if err != nil {
  2566. ut.Rollback()
  2567. return
  2568. }
  2569. ut.Commit()
  2570. return
  2571. }
  2572. func UpDateCancleStockStatus(id int64) (err error) {
  2573. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2574. return
  2575. }
  2576. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2577. info := models.CancelStockInfo{}
  2578. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2579. return info, err
  2580. }
  2581. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2582. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2583. return err
  2584. }
  2585. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2586. 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
  2587. return
  2588. }
  2589. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2590. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2591. return
  2592. }
  2593. func UpDateDrugCancelStockById(id int64) (err error) {
  2594. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2595. return
  2596. }
  2597. func EditCancelStock(sales models.CancelStock) {
  2598. 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, "storehouse_id": sales.StorehouseId}).Error
  2599. }
  2600. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2601. 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, "storehouse_id": sales.StorehouseId}).Error
  2602. }
  2603. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2604. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2605. return cancelStock, err
  2606. }
  2607. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2608. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2609. return cancelStock, err
  2610. }
  2611. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2612. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2613. return cancelStock, err
  2614. }
  2615. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2616. err = writeDb.Save(&info).Error
  2617. return err
  2618. }
  2619. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2620. err = writeDb.Save(&info).Error
  2621. return err
  2622. }
  2623. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2624. 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
  2625. return list, err
  2626. }
  2627. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2628. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2629. 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)
  2630. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2631. return list, err
  2632. }
  2633. type StockDetailModel struct {
  2634. GoodId int64 `gorm:"good_id" json:"good_id"`
  2635. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2636. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2637. Price float64 `gorm:"price" json:"price"`
  2638. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2639. Type int `gorm:"type" json:"type"`
  2640. Creater int64 `gorm:"creater" json:"creater"`
  2641. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2642. Types int64 `gorm:"types" json:"types"`
  2643. }
  2644. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.WarehousingInfo, total int64, err error) {
  2645. db := readDb.Model(&models.WarehousingInfo{})
  2646. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2647. if len(keywords) > 0 {
  2648. likeKey := "%" + keywords + "%"
  2649. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2650. 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)
  2651. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2652. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2653. 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")
  2654. } else {
  2655. 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)
  2656. }
  2657. if manufacturer > 0 {
  2658. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2659. }
  2660. if storehouse_id > 0 {
  2661. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2662. }
  2663. if order_type > 0 {
  2664. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2665. }
  2666. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2667. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2668. db = db.Count(&total)
  2669. offset := (page - 1) * limit
  2670. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2671. return
  2672. }
  2673. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2674. db := readDb.Model(&models.WarehousingInfo{})
  2675. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2676. if len(keywords) > 0 {
  2677. likeKey := "%" + keywords + "%"
  2678. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2679. 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)
  2680. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2681. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2682. 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")
  2683. } else {
  2684. 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)
  2685. }
  2686. if manufacturer > 0 {
  2687. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2688. }
  2689. if order_type > 0 {
  2690. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2691. }
  2692. if storehouse_id > 0 {
  2693. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2694. }
  2695. type PriceAmount struct {
  2696. Total float64
  2697. }
  2698. var result []PriceAmount
  2699. err = db.Select("total_price as total").Scan(&result).Error
  2700. var total float64
  2701. for _, value := range result {
  2702. total = total + value.Total
  2703. }
  2704. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2705. return err, total
  2706. }
  2707. func FloatRound(f float64, n int) float64 {
  2708. format := "%." + strconv.Itoa(n) + "f"
  2709. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2710. return res
  2711. }
  2712. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2713. if orgId == 9671 {
  2714. db := readDb.Model(&models.WarehouseOutInfo{})
  2715. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 AND xt_warehouse_out_info.sys_record_time>=? AND xt_warehouse_out_info.sys_record_time<=?", orgId, startTime, endTime)
  2716. if len(keywords) > 0 {
  2717. likeKey := "%" + keywords + "%"
  2718. 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)
  2719. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2720. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2721. 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")
  2722. } else {
  2723. 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)
  2724. }
  2725. if manufacturer > 0 {
  2726. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2727. }
  2728. if order_type > 0 {
  2729. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2730. }
  2731. if storehouse_id > 0 {
  2732. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2733. }
  2734. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2735. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2736. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2737. db = db.Count(&total)
  2738. offset := (page - 1) * limit
  2739. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2740. return
  2741. } else {
  2742. db := readDb.Model(&models.WarehouseOutInfo{})
  2743. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 ", orgId)
  2744. if len(keywords) > 0 {
  2745. likeKey := "%" + keywords + "%"
  2746. 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)
  2747. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2748. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2749. 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")
  2750. } else {
  2751. 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)
  2752. }
  2753. if manufacturer > 0 {
  2754. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2755. }
  2756. if order_type > 0 {
  2757. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2758. }
  2759. if storehouse_id > 0 {
  2760. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2761. }
  2762. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2763. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2764. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2765. db = db.Count(&total)
  2766. offset := (page - 1) * limit
  2767. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2768. return
  2769. }
  2770. }
  2771. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2772. db := readDb.Model(&models.WarehouseOutInfo{})
  2773. 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)
  2774. if len(keywords) > 0 {
  2775. likeKey := "%" + keywords + "%"
  2776. 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)
  2777. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2778. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2779. 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")
  2780. } else {
  2781. 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)
  2782. }
  2783. if manufacturer > 0 {
  2784. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2785. }
  2786. if order_type > 0 {
  2787. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2788. }
  2789. if storehouse_id > 0 {
  2790. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2791. }
  2792. type PriceAmount struct {
  2793. Total float64
  2794. }
  2795. var result []PriceAmount
  2796. err = db.Select("(count * price) as total").Scan(&result).Error
  2797. var total float64
  2798. for _, value := range result {
  2799. total = total + value.Total
  2800. }
  2801. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2802. fmt.Println(total)
  2803. return err, total
  2804. }
  2805. 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) {
  2806. db := readDb.Model(&models.SalesReturnInfo{})
  2807. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2808. if len(keywords) > 0 {
  2809. likeKey := "%" + keywords + "%"
  2810. 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)
  2811. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2812. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2813. 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")
  2814. } else {
  2815. 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)
  2816. }
  2817. if manufacturer > 0 {
  2818. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2819. }
  2820. if order_type > 0 {
  2821. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2822. }
  2823. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2824. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2825. offset := (page - 1) * limit
  2826. db = db.Count(&total)
  2827. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2828. return
  2829. }
  2830. func FindeCancelGroup(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.CancelStockInfo, err error) {
  2831. db := readDb.Model(&models.CancelStockInfo{})
  2832. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2833. if len(keywords) > 0 {
  2834. likeKey := "%" + keywords + "%"
  2835. 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)
  2836. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2837. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2838. 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")
  2839. } else {
  2840. 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)
  2841. }
  2842. if manufacturer > 0 {
  2843. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2844. }
  2845. if order_type > 0 {
  2846. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2847. }
  2848. if storehouse_id > 0 {
  2849. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  2850. }
  2851. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2852. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2853. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2854. return
  2855. }
  2856. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.CancelStockInfo, total int64, err error) {
  2857. db := readDb.Model(&models.CancelStockInfo{})
  2858. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2859. if len(keywords) > 0 {
  2860. likeKey := "%" + keywords + "%"
  2861. 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)
  2862. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2863. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2864. 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")
  2865. } else {
  2866. 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)
  2867. }
  2868. if manufacturer > 0 {
  2869. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2870. }
  2871. if order_type > 0 {
  2872. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2873. }
  2874. if storehouse_id > 0 {
  2875. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  2876. }
  2877. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2878. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2879. db = db.Count(&total)
  2880. offset := (page - 1) * limit
  2881. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  2882. return
  2883. }
  2884. func FindStockInByDealerId(id int64) (total int64, err error) {
  2885. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2886. return
  2887. }
  2888. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2889. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2890. return
  2891. }
  2892. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2893. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2894. return
  2895. }
  2896. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2897. 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)
  2898. return
  2899. }
  2900. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2901. 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)
  2902. return
  2903. }
  2904. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2905. 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)
  2906. return
  2907. }
  2908. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2909. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2910. return goodType, err
  2911. }
  2912. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2913. 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
  2914. return info, err
  2915. }
  2916. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2917. 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
  2918. return
  2919. }
  2920. func FindStockOutByIsSysOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string) (out models.WarehouseOut, err error) {
  2921. err = readDb.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).First(&out).Error
  2922. return
  2923. }
  2924. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  2925. err = readDb.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? AND second_warehouse_id=?", is_sys, org_id, record_time, second_warehouse_id).First(&out).Error
  2926. return
  2927. }
  2928. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2929. 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
  2930. return
  2931. }
  2932. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  2933. err = tx.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
  2934. if err != gorm.ErrRecordNotFound {
  2935. if err != nil {
  2936. tx.Rollback()
  2937. return
  2938. }
  2939. }
  2940. return
  2941. }
  2942. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2943. 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
  2944. return
  2945. }
  2946. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2947. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2948. return
  2949. }
  2950. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2951. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2952. return
  2953. }
  2954. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2955. info := models.WarehouseOutInfo{}
  2956. 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
  2957. if err == gorm.ErrRecordNotFound {
  2958. return nil, err
  2959. }
  2960. if err != nil {
  2961. return nil, err
  2962. }
  2963. return &info, nil
  2964. }
  2965. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2966. info := models.WarehouseOutInfo{}
  2967. 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
  2968. if err == gorm.ErrRecordNotFound {
  2969. return nil, err
  2970. }
  2971. if err != nil {
  2972. return nil, err
  2973. }
  2974. return &info, nil
  2975. }
  2976. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2977. 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, "over_count": prepare.OverCount}).Error
  2978. return err
  2979. }
  2980. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2981. 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
  2982. return err
  2983. }
  2984. func GetGoodsIsExsit(good_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64) (models.WarehouseOutInfo, error) {
  2985. outInfo := models.WarehouseOutInfo{}
  2986. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", good_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  2987. return outInfo, err
  2988. }
  2989. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2990. err := writeDb.Create(&info).Error
  2991. return err
  2992. }
  2993. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  2994. err := tx.Create(&info).Error
  2995. if err != nil {
  2996. tx.Rollback()
  2997. return err
  2998. }
  2999. return err
  3000. }
  3001. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  3002. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3003. return err
  3004. }
  3005. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  3006. err := writeDb.Create(&info).Error
  3007. return err
  3008. }
  3009. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, tx *gorm.DB) error {
  3010. err := tx.Create(&info).Error
  3011. if err != nil {
  3012. tx.Rollback()
  3013. return err
  3014. }
  3015. return err
  3016. }
  3017. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  3018. err := tx.Create(&info).Error
  3019. if err != nil {
  3020. tx.Rollback()
  3021. return err
  3022. }
  3023. return err
  3024. }
  3025. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  3026. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3027. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  3028. if err != nil {
  3029. tx.Rollback()
  3030. return err
  3031. }
  3032. return err
  3033. }
  3034. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3035. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3036. return err
  3037. }
  3038. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3039. ut := writeDb.Begin()
  3040. err := ut.Create(&detail).Error
  3041. if err != nil {
  3042. ut.Rollback()
  3043. return err
  3044. }
  3045. ut.Commit()
  3046. return err
  3047. }
  3048. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3049. err := writeDb.Create(&detail).Error
  3050. return err
  3051. }
  3052. func GetDrugAutoMaticReduceDetail(drug_id int64, patient_id int64, record_time int64, org_id int64, adivce_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
  3053. detail := models.DrugAutomaticReduceDetail{}
  3054. err := tx.Where("drug_id = ? and patient_id =? and record_time =? and org_id = ? and advice_id = ? and warehouse_info_id =?", drug_id, patient_id, record_time, org_id, adivce_id, warehouse_info_id).Find(&detail).Error
  3055. if err != gorm.ErrRecordNotFound {
  3056. if err != nil {
  3057. tx.Rollback()
  3058. return detail, err
  3059. }
  3060. }
  3061. return detail, err
  3062. }
  3063. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3064. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3065. if err != nil {
  3066. tx.Rollback()
  3067. return err
  3068. }
  3069. return err
  3070. }
  3071. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3072. err := tx.Create(&detail).Error
  3073. if err != nil {
  3074. tx.Rollback()
  3075. return err
  3076. }
  3077. return err
  3078. }
  3079. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3080. 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
  3081. return
  3082. }
  3083. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3084. 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
  3085. return
  3086. }
  3087. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3088. 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
  3089. return
  3090. }
  3091. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3092. info := models.WarehouseOutInfo{}
  3093. 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
  3094. return info, err
  3095. }
  3096. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3097. 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
  3098. return
  3099. }
  3100. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3101. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3102. }
  3103. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3104. db := readDb.Model(&models.AutomaticReduceDetail{})
  3105. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3106. db = db.Preload("GoodsType", "status = 1")
  3107. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3108. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3109. db = db.Count(&total)
  3110. err = db.Find(&user).Error
  3111. return
  3112. }
  3113. type DrugAutomaticReduceDetail struct {
  3114. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3115. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3116. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3117. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3118. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3119. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3120. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3121. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3122. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3123. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3124. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3125. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3126. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3127. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3128. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3129. }
  3130. func (DrugAutomaticReduceDetail) TableName() string {
  3131. return "xt_drug_automatic_reduce_detail"
  3132. }
  3133. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3134. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3135. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3136. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3137. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3138. db = db.Count(&total)
  3139. err = db.Find(&user).Error
  3140. return
  3141. }
  3142. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3143. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3144. }
  3145. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3146. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3147. }
  3148. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3149. 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
  3150. return
  3151. }
  3152. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3153. 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
  3154. return
  3155. }
  3156. type VMWarehousingInfo struct {
  3157. ID int64 `gorm:"column:id" json:"id"`
  3158. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3159. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3160. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3161. Status int64 `gorm:"column:status" json:"status"`
  3162. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3163. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3164. }
  3165. func (VMWarehousingInfo) TableName() string {
  3166. return "xt_warehouse_info"
  3167. }
  3168. type GoodInfo struct {
  3169. ID int64 `gorm:"column:id" json:"id"`
  3170. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3171. Status int64 `gorm:"column:status" json:"status"`
  3172. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3173. }
  3174. func (GoodInfo) TableName() string {
  3175. return "xt_good_information"
  3176. }
  3177. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3178. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3179. return
  3180. }
  3181. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3182. 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
  3183. return
  3184. }
  3185. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3186. likeKey := "%" + keyword + "%"
  3187. db := XTReadDB().Model(&goods)
  3188. if len(keyword) > 0 {
  3189. db = db.Where("type_name like ?", likeKey)
  3190. }
  3191. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3192. return goods, err
  3193. }
  3194. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3195. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3196. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3197. return goodstype, err
  3198. }
  3199. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3200. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3201. if orgid > 0 {
  3202. db = db.Where("x.user_org_id = ?", orgid)
  3203. }
  3204. if recordtime > 0 {
  3205. db = db.Where("x.record_date = ?", recordtime)
  3206. }
  3207. if patientId > 0 {
  3208. db = db.Where("x.patient_id = ?", patientId)
  3209. }
  3210. 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
  3211. return prepare, err
  3212. }
  3213. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3214. detail := models.XtAutomaticReduceDetail{}
  3215. 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
  3216. if err == gorm.ErrRecordNotFound {
  3217. return nil, err
  3218. }
  3219. if err != nil {
  3220. return nil, err
  3221. }
  3222. return &detail, nil
  3223. }
  3224. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3225. 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
  3226. return autoMatic, err
  3227. }
  3228. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3229. 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
  3230. return autoMatic, err
  3231. }
  3232. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3233. var manufacturer models.Manufacturer
  3234. var err error
  3235. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3236. if err == gorm.ErrRecordNotFound {
  3237. return nil, err
  3238. }
  3239. if err != nil {
  3240. return nil, err
  3241. }
  3242. return &manufacturer, nil
  3243. }
  3244. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3245. err := XTWriteDB().Create(manufacturer).Error
  3246. return err
  3247. }
  3248. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3249. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3250. return err
  3251. }
  3252. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3253. var good models.GoodInfo
  3254. var err error
  3255. 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
  3256. if err == gorm.ErrRecordNotFound {
  3257. return nil, err
  3258. }
  3259. if err != nil {
  3260. return nil, err
  3261. }
  3262. return &good, nil
  3263. }
  3264. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3265. err := XTWriteDB().Create(&info).Error
  3266. return err
  3267. }
  3268. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3269. 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, "number": info.Number, "register_number": info.RegisterNumber}).Error
  3270. return err
  3271. }
  3272. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3273. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3274. 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
  3275. return houseOutInfo, err
  3276. }
  3277. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3278. key := "%" + keyword + "%"
  3279. if len(keyword) == 0 {
  3280. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3281. } else {
  3282. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3283. }
  3284. return goodInfo, err
  3285. }
  3286. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3287. var drug models.BaseDrugLib
  3288. var err error
  3289. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3290. if err == gorm.ErrRecordNotFound {
  3291. return nil, err
  3292. }
  3293. if err != nil {
  3294. return nil, err
  3295. }
  3296. return &drug, nil
  3297. }
  3298. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3299. var drug models.BaseDrugLib
  3300. var err error
  3301. 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
  3302. if err == gorm.ErrRecordNotFound {
  3303. return nil, err
  3304. }
  3305. if err != nil {
  3306. return nil, err
  3307. }
  3308. return &drug, nil
  3309. }
  3310. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3311. err := XTWriteDB().Create(&info).Error
  3312. return err
  3313. }
  3314. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3315. 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, "single_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
  3316. return err
  3317. }
  3318. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3319. var projoect models.HisProject
  3320. var err error
  3321. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3322. if err == gorm.ErrRecordNotFound {
  3323. return nil, err
  3324. }
  3325. if err != nil {
  3326. return nil, err
  3327. }
  3328. return &projoect, nil
  3329. }
  3330. func CreateProjectInfomation(info *models.HisProject) error {
  3331. err := XTWriteDB().Create(&info).Error
  3332. return err
  3333. }
  3334. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3335. 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
  3336. return err
  3337. }
  3338. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3339. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3340. return warehouseinfo, err
  3341. }
  3342. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3343. err := XTWriteDB().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, "storehouse_id": warehouse.StorehouseId}).Error
  3344. return err
  3345. }
  3346. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3347. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3348. return list, err
  3349. }
  3350. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3351. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3352. return list, err
  3353. }
  3354. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3355. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3356. return list, err
  3357. }
  3358. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3359. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3360. // return list, err
  3361. // }
  3362. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3363. var dealer models.Dealer
  3364. var err error
  3365. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3366. if err == gorm.ErrRecordNotFound {
  3367. return nil, err
  3368. }
  3369. if err != nil {
  3370. return nil, err
  3371. }
  3372. return &dealer, nil
  3373. }
  3374. func CreateDealer(dealer *models.Dealer) error {
  3375. err := XTWriteDB().Create(&dealer).Error
  3376. return err
  3377. }
  3378. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3379. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3380. return err
  3381. }
  3382. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3383. dataconfig := models.DictDataconfig{}
  3384. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3385. return dataconfig, err
  3386. }
  3387. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3388. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3389. return dataconfig, err
  3390. }
  3391. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3392. var dataconfig models.DictDataconfig
  3393. var err error
  3394. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3395. if err == gorm.ErrRecordNotFound {
  3396. return nil, err
  3397. }
  3398. if err != nil {
  3399. return nil, err
  3400. }
  3401. return &dataconfig, nil
  3402. }
  3403. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3404. dataconfig := models.DictDataconfig{}
  3405. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3406. return dataconfig, err
  3407. }
  3408. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3409. err := XTWriteDB().Create(&dataconfig).Error
  3410. return err
  3411. }
  3412. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3413. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3414. return err
  3415. }
  3416. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3417. var goodType models.GoodsType
  3418. var err error
  3419. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3420. if err == gorm.ErrRecordNotFound {
  3421. return nil, err
  3422. }
  3423. if err != nil {
  3424. return nil, err
  3425. }
  3426. return &goodType, nil
  3427. }
  3428. func CreatedGoodType(goodsType *models.GoodsType) error {
  3429. err := XTWriteDB().Create(&goodsType).Error
  3430. return err
  3431. }
  3432. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3433. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3434. return goodsType, err
  3435. }
  3436. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3437. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3438. return goodsType, err
  3439. }
  3440. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3441. var department models.XtHisDepartment
  3442. var err error
  3443. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3444. if err == gorm.ErrRecordNotFound {
  3445. return nil, err
  3446. }
  3447. if err != nil {
  3448. return nil, err
  3449. }
  3450. return &department, nil
  3451. }
  3452. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3453. dataconfig := models.Dataconfig{}
  3454. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3455. return dataconfig, err
  3456. }
  3457. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3458. var dataconfig models.Dataconfig
  3459. var err error
  3460. 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
  3461. if err == gorm.ErrRecordNotFound {
  3462. return nil, err
  3463. }
  3464. if err != nil {
  3465. return nil, err
  3466. }
  3467. return &dataconfig, nil
  3468. }
  3469. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3470. dataconfig := models.Dataconfig{}
  3471. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3472. return dataconfig, err
  3473. }
  3474. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3475. err := XTWriteDB().Create(&dataconfig).Error
  3476. return err
  3477. }
  3478. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3479. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3480. return dataconfig, err
  3481. }
  3482. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3483. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3484. return list, err
  3485. }
  3486. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3487. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3488. if len(keywords) > 0 {
  3489. likekey := "%" + keywords + "%"
  3490. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3491. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3492. }).Find(&drug).Error
  3493. } else {
  3494. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3495. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3496. }).Find(&drug).Error
  3497. }
  3498. return
  3499. }
  3500. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3501. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3502. if len(keywords) > 0 {
  3503. likekey := "%" + keywords + "%"
  3504. err = db.Where("(drug_name LIKE ?)", likekey).Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3505. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3506. }).Find(&drug).Error
  3507. } else {
  3508. err = db.Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3509. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3510. }).Find(&drug).Error
  3511. }
  3512. return
  3513. }
  3514. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3515. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3516. if len(keywords) > 0 {
  3517. likekey := "%" + keywords + "%"
  3518. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3519. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3520. }).Find(&good).Error
  3521. } else {
  3522. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3523. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3524. }).Find(&good).Error
  3525. }
  3526. return
  3527. }
  3528. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3529. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3530. if len(keywords) > 0 {
  3531. likekey := "%" + keywords + "%"
  3532. err = db.Where("(good_name Like ?)", likekey).Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3533. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3534. }).Find(&good).Error
  3535. } else {
  3536. err = db.Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3537. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3538. }).Find(&good).Error
  3539. }
  3540. return
  3541. }
  3542. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3543. db := writeDb.Begin()
  3544. err := db.Create(&detail).Error
  3545. if err != nil {
  3546. db.Rollback()
  3547. return err
  3548. }
  3549. db.Commit()
  3550. return err
  3551. }
  3552. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3553. var autoreduece models.BloodAutomaticReduceDetail
  3554. var err error
  3555. 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
  3556. if err == gorm.ErrRecordNotFound {
  3557. return nil, err
  3558. }
  3559. if err != nil {
  3560. return nil, err
  3561. }
  3562. return &autoreduece, nil
  3563. }
  3564. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3565. auto := models.BloodAutomaticReduceDetail{}
  3566. err = XTReadDB().Model(&auto).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(&auto).Error
  3567. return auto, err
  3568. }
  3569. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3570. detail := models.BloodAutomaticReduceDetail{}
  3571. 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
  3572. return err
  3573. }
  3574. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3575. 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
  3576. return stockflow, err
  3577. }
  3578. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3579. err = XTWriteDB().Model(models.BloodAutomaticReduceDetail{}).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ?", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3580. err = XTWriteDB().Model(models.WarehouseOutInfo{}).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3581. err = XTWriteDB().Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3582. return err
  3583. }
  3584. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3585. detail := models.BloodAutomaticReduceDetail{}
  3586. 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
  3587. info := models.WarehouseOutInfo{}
  3588. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3589. prepare := models.DialysisBeforePrepare{}
  3590. 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
  3591. flow := models.VmStockFlow{}
  3592. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3593. return err
  3594. }
  3595. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3596. detail := models.BloodAutomaticReduceDetail{}
  3597. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3598. info := models.WarehouseOutInfo{}
  3599. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3600. prepare := models.DialysisBeforePrepare{}
  3601. 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
  3602. flow := models.VmStockFlow{}
  3603. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3604. return err
  3605. }
  3606. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3607. detail := models.BloodAutomaticReduceDetail{}
  3608. 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
  3609. info := models.WarehouseOutInfo{}
  3610. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and order_id = ?", orgid, patient_id, recordtime, goodid, order_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3611. prepare := models.DialysisBeforePrepare{}
  3612. 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
  3613. flow := models.VmStockFlow{}
  3614. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and order_id = ?", orgid, patient_id, recordtime, goodid, order_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3615. return err
  3616. }
  3617. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3618. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3619. //err = writeDb.Model(&models.VmStockFlow{}).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", id, patient_id, record_time, goodid).UpdateColumn("over_count", gorm.Expr("over_count + ?", count)).Error
  3620. return err
  3621. }
  3622. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3623. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3624. return info, err
  3625. }
  3626. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3627. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3628. return list, err
  3629. }
  3630. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3631. 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
  3632. return err
  3633. }
  3634. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3635. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3636. return err
  3637. }
  3638. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3639. var autoreduece models.BloodAutomaticReduceDetail
  3640. var err error
  3641. 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
  3642. if err == gorm.ErrRecordNotFound {
  3643. return nil, err
  3644. }
  3645. if err != nil {
  3646. return nil, err
  3647. }
  3648. return &autoreduece, nil
  3649. }
  3650. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3651. detail := models.WarehouseOutInfoSeven{}
  3652. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3653. return detail, err
  3654. }
  3655. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3656. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3657. return detail, err
  3658. }
  3659. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3660. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3661. return project, err
  3662. }
  3663. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3664. project := models.HisPrescriptionProject{}
  3665. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3666. return project, err
  3667. }
  3668. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3669. err = XTReadDB().Select("count,warehouse_info_id,good_id,org_id,sys_record_time,patient_id,project_id,warehouse_info_id,warehouse_out_order_number,storehouse_id,price,dealer,manufacturer,good_type_id,product_date,expiry_date,number,license_number").Where(" patient_id = ? and sys_record_time = ? and status = 1 and is_sys = 1 and count>0", patientid, recorddate).Find(&auto).Error
  3670. return auto, err
  3671. }
  3672. func ModefyWarehouseInfo(count int64, id int64) error {
  3673. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3674. return err
  3675. }
  3676. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3677. 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
  3678. 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
  3679. err = XTWriteDB().Model(models.VmStockFlow{}).Where("patient_id = ? and system_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"is_read": 1}).Error
  3680. //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
  3681. return err
  3682. }
  3683. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  3684. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3685. return
  3686. }
  3687. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  3688. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3689. return
  3690. }
  3691. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  3692. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3693. return info, err
  3694. }
  3695. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3696. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3697. return info, err
  3698. }
  3699. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3700. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0 and storehouse_id = ?", good_id, storehouse_id).Find(&info).Error
  3701. return info, err
  3702. }
  3703. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3704. offset := (page - 1) * limit
  3705. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3706. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3707. fmt.Println(table)
  3708. if startime > 0 {
  3709. db = db.Where("x.ctime>=?", startime)
  3710. }
  3711. if endtime > 0 {
  3712. db = db.Where("x.ctime<=?", endtime)
  3713. }
  3714. likeKey := "%" + keyword + "%"
  3715. if len(likeKey) > 0 {
  3716. db = db.Where("t.good_name like ?", likeKey)
  3717. }
  3718. if orgid > 0 {
  3719. db = db.Where("x.org_id = ?", orgid)
  3720. }
  3721. if good_type > 0 {
  3722. db = db.Where("t.good_type_id = ?", good_type)
  3723. }
  3724. 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
  3725. return info, total, err
  3726. }
  3727. func GetAllGoodInfoStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64, good_id int64, storehouse_id int64) (info []*models.GoodInfo, total int64, err error) {
  3728. offset := (page - 1) * limit
  3729. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  3730. likeKey := "%" + keyword + "%"
  3731. if len(likeKey) > 0 {
  3732. db = db.Where("x.good_name like ?", likeKey)
  3733. }
  3734. if orgid > 0 {
  3735. db = db.Where("x.org_id = ?", orgid)
  3736. }
  3737. if good_type > 0 {
  3738. db = db.Where("x.good_type_id = ?", good_type)
  3739. }
  3740. if good_id > 0 {
  3741. db = db.Where("x.id = ?", good_id)
  3742. }
  3743. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3744. if startime > 0 {
  3745. db = db.Where("ctime>=?", startime)
  3746. }
  3747. if endtime > 0 {
  3748. db = db.Where("ctime<=?", endtime)
  3749. }
  3750. if good_id > 0 {
  3751. db = db.Where("good_id = ?", good_id)
  3752. }
  3753. if storehouse_id > 0 {
  3754. db = db.Where("storehouse_id = ?", storehouse_id)
  3755. }
  3756. return db.Where("status = 1 and is_check = 1")
  3757. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3758. if startime > 0 {
  3759. db = db.Where("ctime>=?", startime)
  3760. }
  3761. if endtime > 0 {
  3762. db = db.Where("ctime<=?", endtime)
  3763. }
  3764. if good_id > 0 {
  3765. db = db.Where("good_id = ?", good_id)
  3766. }
  3767. if storehouse_id > 0 {
  3768. db = db.Where("storehouse_id = ?", storehouse_id)
  3769. }
  3770. return db.Where("status = 1").Group("good_id,storehouse_id")
  3771. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3772. if startime > 0 {
  3773. db = db.Where("ctime>=?", startime)
  3774. }
  3775. if endtime > 0 {
  3776. db = db.Where("ctime<=?", endtime)
  3777. }
  3778. return db.Where("status = 1")
  3779. }).Find(&info).Error
  3780. return info, total, err
  3781. }
  3782. func GetStockListById(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64, startfirsttime int64, endfirsttime int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3783. offset := (page - 1) * limit
  3784. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  3785. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3786. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  3787. fmt.Println(table, tables)
  3788. if startime > 0 {
  3789. db = db.Where("x.ctime>=?", startime)
  3790. }
  3791. if endtime > 0 {
  3792. db = db.Where("x.ctime<=?", endtime)
  3793. }
  3794. if startfirsttime > 0 {
  3795. db = db.Where("x.expiry_date >=?", startfirsttime)
  3796. }
  3797. if endfirsttime > 0 {
  3798. db = db.Where("x.expiry_date <=?", endfirsttime)
  3799. }
  3800. if orgid > 0 {
  3801. db = db.Where("x.org_id = ?", orgid)
  3802. }
  3803. if good_id > 0 {
  3804. db = db.Where("x.good_id = ?", good_id)
  3805. }
  3806. 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.packing_price,t.manufacturer,x.remark,x.ctime,x.is_return,x.warehousing_order,x.type,x.storehouse_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.min_unit,t.buy_price,f.batch_number_count").Joins("left join xt_good_information as t on t.id = x.good_id").Joins("left join xt_stock_flow as f on f.warehousing_detail_id = x.id").Count(&total).Offset(offset).Limit(limit).Group("x.id").Order("x.id desc").Scan(&info).Error
  3807. return info, total, err
  3808. }
  3809. 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) {
  3810. offset := (page - 1) * limit
  3811. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  3812. if startime > 0 {
  3813. db = db.Where("x.ctime>=?", startime)
  3814. }
  3815. if endtime > 0 {
  3816. db = db.Where("x.ctime<=?", endtime)
  3817. }
  3818. if orgid > 0 {
  3819. db = db.Where("x.org_id = ?", orgid)
  3820. }
  3821. if good_id > 0 {
  3822. db = db.Where("x.good_id = ?", good_id)
  3823. }
  3824. if is_sys == 2 {
  3825. db = db.Where("x.is_sys = 1")
  3826. }
  3827. if is_sys == 3 {
  3828. db = db.Where("x.is_sys = 0")
  3829. }
  3830. 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
  3831. return info, total, err
  3832. }
  3833. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3834. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3835. if startime > 0 {
  3836. db = db.Where("x.ctime >=?", startime)
  3837. }
  3838. if endtime > 0 {
  3839. db = db.Where("x.ctime<=?", endtime)
  3840. }
  3841. if orgid > 0 {
  3842. db = db.Where("x.org_id = ?", orgid)
  3843. }
  3844. 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
  3845. return info, err
  3846. }
  3847. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3848. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3849. if startime > 0 {
  3850. db = db.Where("x.ctime >=?", startime)
  3851. }
  3852. if endtime > 0 {
  3853. db = db.Where("x.ctime<=?", endtime)
  3854. }
  3855. if orgid > 0 {
  3856. db = db.Where("x.org_id = ?", orgid)
  3857. }
  3858. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  3859. return info, err
  3860. }
  3861. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3862. if endtime > 0 {
  3863. 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
  3864. }
  3865. if endtime == 0 {
  3866. 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
  3867. }
  3868. return autoMatic, err
  3869. }
  3870. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  3871. out := models.WarehouseOut{}
  3872. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  3873. return out, err
  3874. }
  3875. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3876. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3877. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3878. fmt.Println(table)
  3879. if id > 0 {
  3880. db = db.Where("x.warehouse_out_id = ?", id)
  3881. }
  3882. if orgid > 0 {
  3883. db = db.Where("x.org_id = ?", orgid)
  3884. }
  3885. 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,x.patient_id,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,x.supply_cancel_out_id,x.supply_warehouse_id,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.register_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
  3886. return out, err
  3887. }
  3888. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  3889. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  3890. if len(id) > 0 {
  3891. db = db.Where("x.id in(?)", id)
  3892. }
  3893. if orgid > 0 {
  3894. db = db.Where("x.org_id= ?", orgid)
  3895. }
  3896. 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
  3897. return out, err
  3898. }
  3899. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3900. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3901. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3902. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  3903. fmt.Println(table, tableOne)
  3904. if len(id) > 0 {
  3905. db = db.Where("x.warehouse_out_id in (?)", id)
  3906. }
  3907. if orgid > 0 {
  3908. db = db.Where("x.org_id = ?", orgid)
  3909. }
  3910. 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,x.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,n.number,x.remark,x.license_number,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.warehouse_info_id,x.remark,x.patient_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price,x.register_number,x.class_type").Joins("left join xt_warehouse_info as n on n.id=x.warehouse_info_id").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
  3911. return out, err
  3912. }
  3913. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3914. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3915. if len(id) > 0 {
  3916. db = db.Where("warehouse_out_id in (?)", id)
  3917. }
  3918. if orgid > 0 {
  3919. db = db.Where("user_org_id = ?", orgid)
  3920. }
  3921. err = db.Group("warehousing_id").Find(&list).Error
  3922. return list, err
  3923. }
  3924. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3925. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3926. if len(id) > 0 {
  3927. db = db.Where("warehouse_out_id in (?)", id)
  3928. }
  3929. if orgid > 0 {
  3930. db = db.Where("user_org_id = ?", orgid)
  3931. }
  3932. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3933. return list, err
  3934. }
  3935. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3936. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  3937. if len(id) > 0 {
  3938. db = db.Where("warehouse_out_id in (?)", id)
  3939. }
  3940. if orgid > 0 {
  3941. db = db.Where("user_org_id = ?", orgid)
  3942. }
  3943. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3944. return list, err
  3945. }
  3946. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  3947. db := XTReadDB().Model(&info)
  3948. db = db.Preload("GoodInfo", "status = 1")
  3949. 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
  3950. return info, err
  3951. }
  3952. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3953. db := readDb.Model(&models.AutomaticReduceDetail{})
  3954. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3955. db = db.Preload("GoodsType", "status = 1")
  3956. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3957. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  3958. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  3959. db = db.Count(&total)
  3960. err = db.Find(&user).Error
  3961. return
  3962. }
  3963. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  3964. info := models.GoodInfo{}
  3965. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3966. return info, err
  3967. }
  3968. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  3969. info := models.GoodInformationThirty{}
  3970. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3971. return info, err
  3972. }
  3973. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  3974. info := models.GoodInformationThirty{}
  3975. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  3976. if err != gorm.ErrRecordNotFound {
  3977. if err != nil {
  3978. tx.Rollback()
  3979. return info, err
  3980. }
  3981. }
  3982. return info, err
  3983. }
  3984. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  3985. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  3986. return err
  3987. }
  3988. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  3989. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3990. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3991. fmt.Println(table)
  3992. if orgid > 0 {
  3993. db = db.Where("x.org_id = ?", orgid)
  3994. }
  3995. if len(orderid) > 0 {
  3996. db = db.Where("x.warehousing_id in(?)", orderid)
  3997. }
  3998. if startime > 0 {
  3999. db = db.Where("x.ctime>=?", startime)
  4000. }
  4001. if endtime > 0 {
  4002. db = db.Where("x.ctime<=?", endtime)
  4003. }
  4004. 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
  4005. return info, err
  4006. }
  4007. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  4008. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4009. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4010. fmt.Println(table)
  4011. if orgid > 0 {
  4012. db = db.Where("x.org_id = ?", orgid)
  4013. }
  4014. if len(orderid) > 0 {
  4015. db = db.Where("x.warehousing_id in(?)", orderid)
  4016. }
  4017. if startime > 0 {
  4018. db = db.Where("x.ctime>=?", startime)
  4019. }
  4020. if endtime > 0 {
  4021. db = db.Where("x.ctime<=?", endtime)
  4022. }
  4023. if storehouse_id > 0 {
  4024. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4025. }
  4026. 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,p.warehousing_time").Joins("left join xt_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&info).Error
  4027. return info, err
  4028. }
  4029. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4030. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4031. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  4032. fmt.Println(table)
  4033. if startime > 0 {
  4034. db = db.Where("x.ctime>=?", startime)
  4035. }
  4036. if endtime > 0 {
  4037. db = db.Where("x.ctime<=?", endtime)
  4038. }
  4039. if orgid > 0 {
  4040. db = db.Where("x.org_id = ?", orgid)
  4041. }
  4042. 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
  4043. return info, err
  4044. }
  4045. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4046. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4047. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4048. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4049. fmt.Println(table, tableOne)
  4050. if orgid > 0 {
  4051. db = db.Where("x.org_id = ?", orgid)
  4052. }
  4053. if orgid > 0 {
  4054. db = db.Where("t.org_id = ?", orgid)
  4055. }
  4056. if orgid > 0 {
  4057. db = db.Where("p.org_id = ?", orgid)
  4058. }
  4059. if len(ids) > 0 {
  4060. db = db.Where("x.warehouse_out_id in (?)", ids)
  4061. }
  4062. if startime > 0 {
  4063. db = db.Where("x.ctime>=?", startime)
  4064. }
  4065. if endtime > 0 {
  4066. db = db.Where("x.ctime<=?", endtime)
  4067. }
  4068. 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,x.warehouse_out_order_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,x.manufacturer,x.dealer,p.warehouse_out_time").Joins("left join xt_warehouse_out as p on p.id =x.warehouse_out_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  4069. return out, err
  4070. }
  4071. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4072. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4073. if len(ids) > 0 {
  4074. db = db.Where("x.id in(?)", ids)
  4075. }
  4076. if orgid > 0 {
  4077. db = db.Where("x.org_id = ?", orgid)
  4078. }
  4079. 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
  4080. return info, err
  4081. }
  4082. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4083. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4084. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4085. fmt.Println(table)
  4086. if len(ids) > 0 {
  4087. db = db.Where("x.warehouse_out_id in(?)", ids)
  4088. }
  4089. if orgid > 0 {
  4090. db = db.Where("x.org_id = ?", orgid)
  4091. }
  4092. 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
  4093. return info, err
  4094. }
  4095. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4096. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4097. likeKey := "%" + keyword + "%"
  4098. if len(keyword) > 0 {
  4099. db = db.Where("x.drug_name like ?", likeKey)
  4100. }
  4101. if orgid > 0 {
  4102. db = db.Where("x.org_id = ?", orgid)
  4103. }
  4104. err = db.Find(&basedrug).Error
  4105. return basedrug, err
  4106. }
  4107. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4108. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4109. if len(ids) > 0 {
  4110. db = db.Where("x.drug_id in(?)", ids)
  4111. }
  4112. if orgid > 0 {
  4113. db = db.Where("x.org_id = ?", orgid)
  4114. }
  4115. if startime > 0 {
  4116. db = db.Where("x.ctime>=?", startime)
  4117. }
  4118. if endtime > 0 {
  4119. db = db.Where("x.ctime<=?", endtime)
  4120. }
  4121. if storehouse_id > 0 {
  4122. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4123. }
  4124. err = db.Find(&info).Error
  4125. return info, err
  4126. }
  4127. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4128. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4129. if len(ids) > 0 {
  4130. db = db.Where("x.drug_id in(?)", ids)
  4131. }
  4132. if orgid > 0 {
  4133. db = db.Where("x.org_id = ?", orgid)
  4134. }
  4135. if startime > 0 {
  4136. db = db.Where("x.ctime>=?", startime)
  4137. }
  4138. if endtime > 0 {
  4139. db = db.Where("x.ctime<=?", endtime)
  4140. }
  4141. if storehouse_id > 0 {
  4142. db = db.Where("storehouse_id = ?", storehouse_id)
  4143. }
  4144. err = db.Find(&info).Error
  4145. return info, err
  4146. }
  4147. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4148. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4149. if drugid > 0 {
  4150. db = db.Where("x.drug_id = ?", drugid)
  4151. }
  4152. if orgid > 0 {
  4153. db = db.Where("x.org_id = ?", orgid)
  4154. }
  4155. if storehouse_id > 0 {
  4156. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4157. }
  4158. 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
  4159. return info, err
  4160. }
  4161. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4162. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4163. if drugid > 0 {
  4164. db = db.Where("x.drug_id = ?", drugid)
  4165. }
  4166. if orgid > 0 {
  4167. db = db.Where("x.org_id = ?", orgid)
  4168. }
  4169. if storehouse_id > 0 {
  4170. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4171. }
  4172. 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
  4173. if err != gorm.ErrRecordNotFound {
  4174. if err != nil {
  4175. tx.Rollback()
  4176. return info, err
  4177. }
  4178. }
  4179. return info, err
  4180. }
  4181. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4182. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4183. if drugid > 0 {
  4184. db = db.Where("x.drug_id = ?", drugid)
  4185. }
  4186. if orgid > 0 {
  4187. db = db.Where("x.org_id = ?", orgid)
  4188. }
  4189. if storehouse_id > 0 {
  4190. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4191. }
  4192. 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
  4193. return info, err
  4194. }
  4195. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4196. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4197. if id > 0 {
  4198. db = db.Where("x.id = ?", id)
  4199. }
  4200. if orgid > 0 {
  4201. db = db.Where("x.org_id = ?", orgid)
  4202. }
  4203. 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
  4204. return info, err
  4205. }
  4206. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4207. info := models.VmDrugWarehouseInfo{}
  4208. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4209. if id == 0 {
  4210. db = db.Where("id = ?", id)
  4211. }
  4212. if drugid > 0 {
  4213. db = db.Where("x.drug_id = ?", drugid)
  4214. }
  4215. if orgid > 0 {
  4216. db = db.Where("x.org_id = ?", orgid)
  4217. }
  4218. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4219. return info, err
  4220. }
  4221. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4222. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4223. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4224. fmt.Println(table)
  4225. if drugid > 0 {
  4226. db = db.Where("x.drug_id = ?", drugid)
  4227. }
  4228. if storehouse_id > 0 {
  4229. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4230. }
  4231. 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
  4232. return list, err
  4233. }
  4234. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4235. lib := models.BaseDrugLib{}
  4236. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4237. return lib, err
  4238. }
  4239. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4240. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4241. if goodid > 0 {
  4242. db = db.Where("x.good_id = ?", goodid)
  4243. }
  4244. if orgid > 0 {
  4245. db = db.Where("x.org_id = ?", orgid)
  4246. }
  4247. 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
  4248. return info, err
  4249. }
  4250. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4251. 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
  4252. return err
  4253. }
  4254. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4255. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4256. return info, err
  4257. }
  4258. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4259. //遍历循环入库数据
  4260. for i, item := range list {
  4261. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4262. if (item.WarehousingCount - item.StockCount) >= count {
  4263. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4264. info := models.WarehousingInfo{
  4265. GoodId: item.GoodId,
  4266. OrgId: item.OrgId,
  4267. WarehousingId: item.WarehousingId,
  4268. StockCount: item.StockCount + count,
  4269. }
  4270. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4271. fmt.Println("2323233232323232323232323232323", err2)
  4272. return err
  4273. }
  4274. if (item.WarehousingCount - item.StockCount) < count {
  4275. //如果该批次小于退库数量,则先出库该相差数量
  4276. var countOne = item.WarehousingCount - item.StockCount
  4277. infoOne := models.WarehousingInfo{
  4278. GoodId: item.GoodId,
  4279. OrgId: item.OrgId,
  4280. WarehousingId: item.WarehousingId,
  4281. StockCount: countOne + item.StockCount,
  4282. }
  4283. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4284. if err == nil {
  4285. //剩余的出库到第二个批次
  4286. var countTwo = count - countOne
  4287. infos := append(list, list[i+1])
  4288. var idArray []*models.WarehousingInfo
  4289. for _, it := range infos {
  4290. if it.WarehousingId != item.WarehousingId {
  4291. idArray = append(idArray, it)
  4292. }
  4293. }
  4294. //查询更新后的库存
  4295. UpdateCancelOut(idArray, countTwo)
  4296. return err
  4297. } else {
  4298. return err
  4299. }
  4300. }
  4301. }
  4302. return err
  4303. }
  4304. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4305. if len(idstr) > 0 {
  4306. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4307. } else {
  4308. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4309. }
  4310. return info, err
  4311. }
  4312. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4313. if len(idstr) > 0 {
  4314. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4315. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4316. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4317. }).Find(&info).Error
  4318. }
  4319. return info, err
  4320. }
  4321. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4322. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4323. if startime > 0 {
  4324. db = db.Where("x.ctime >=?", startime)
  4325. }
  4326. if endtime > 0 {
  4327. db = db.Where("x.ctime <= ?", endtime)
  4328. }
  4329. if orgid > 0 {
  4330. db = db.Where("x.org_id = ?", orgid)
  4331. }
  4332. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4333. return info, err
  4334. }
  4335. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4336. info := models.WarehouseOutInfo{}
  4337. var err error
  4338. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4339. if err == gorm.ErrRecordNotFound {
  4340. return nil, err
  4341. }
  4342. if err != nil {
  4343. return nil, err
  4344. }
  4345. return &info, nil
  4346. }
  4347. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4348. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4349. if goodid > 0 {
  4350. db = db.Where("x.good_id = ?", goodid)
  4351. }
  4352. if orgid > 0 {
  4353. db = db.Where("x.org_id = ?", orgid)
  4354. }
  4355. if id > 0 {
  4356. db = db.Where("x.warehouse_info_id = ?", id)
  4357. }
  4358. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4359. return info, err
  4360. }
  4361. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4362. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4363. if goodid > 0 {
  4364. db = db.Where("x.good_id = ?", goodid)
  4365. }
  4366. if orgid > 0 {
  4367. db = db.Where("x.user_org_id = ?", orgid)
  4368. }
  4369. if id > 0 {
  4370. db = db.Where("x.warehousing_id = ?", id)
  4371. }
  4372. if storehouse_id > 0 {
  4373. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4374. }
  4375. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4376. return info, err
  4377. }
  4378. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4379. stock := models.CancelStock{}
  4380. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4381. return stock, err
  4382. }
  4383. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4384. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4385. return info, err
  4386. }
  4387. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4388. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4389. return info, err
  4390. }
  4391. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4392. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4393. return stock, err
  4394. }
  4395. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4396. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and (x.consumable_type = 3 or x.consumable_type = 2 or x.consumable_type = 15)")
  4397. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4398. fmt.Println(table)
  4399. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4400. fmt.Println(tableone)
  4401. if id > 0 {
  4402. db = db.Where("x.warehouse_out_id = ?", id)
  4403. }
  4404. if goodid > 0 {
  4405. db = db.Where("x.good_id = ?", goodid)
  4406. }
  4407. 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
  4408. return stock, err
  4409. }
  4410. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4411. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4412. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4413. fmt.Println(table)
  4414. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4415. fmt.Println(tableone)
  4416. if id > 0 {
  4417. db = db.Where("x.warehouse_out_id = ?", id)
  4418. }
  4419. if goodid > 0 {
  4420. db = db.Where("x.good_id = ?", goodid)
  4421. }
  4422. 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
  4423. return stock, err
  4424. }
  4425. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4426. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4427. return info, err
  4428. }
  4429. func ExportDrugList(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseInfo, err error) {
  4430. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1 AND x.org_id=?", org_id)
  4431. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4432. tableOne := XTReadDB().Table("xt_drug_warehouse as p").Where("p.status=1 and p.org_id=?", org_id)
  4433. fmt.Println("table", table)
  4434. fmt.Println("tableOne", tableOne)
  4435. if len(warehouse_out_id) > 0 {
  4436. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4437. }
  4438. 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,p.warehousing_time").Joins("left join xt_drug_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  4439. return out, err
  4440. }
  4441. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4442. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4443. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4444. fmt.Println("table", table)
  4445. if len(warehouse_out_id) > 0 {
  4446. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4447. }
  4448. 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
  4449. return out, err
  4450. }
  4451. func GetExportOutOrderDrugListOne(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseOutInfo, err error) {
  4452. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1 and x.user_org_id =?", org_id)
  4453. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4454. fmt.Println("table", table)
  4455. if len(warehouse_out_id) > 0 {
  4456. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4457. }
  4458. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.max_unit as count_unit,x.price,x.retail_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,x.ctime,x.consumable_type,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
  4459. return out, err
  4460. }
  4461. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4462. db := XTReadDB().Model(&info).Where("status = 1")
  4463. if len(warehouse_out_id) > 0 {
  4464. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4465. }
  4466. if orgid > 0 {
  4467. db = db.Where("org_id = ?", orgid)
  4468. }
  4469. err = db.Find(&info).Error
  4470. return info, err
  4471. }
  4472. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4473. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4474. return out, err
  4475. }
  4476. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4477. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4478. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Preload("DrugWarehouseInfo", "status = 1 and org_id = ?", orgid)
  4479. }).Find(&out).Error
  4480. return out, err
  4481. }
  4482. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4483. info := models.WarehouseOutInfo{}
  4484. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4485. return info, err
  4486. }
  4487. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4488. info := models.WarehousingInfo{}
  4489. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4490. if goodid > 0 {
  4491. db = db.Where("x.good_id = ?", goodid)
  4492. }
  4493. if storehouse_id > 0 {
  4494. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4495. }
  4496. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4497. return info, err
  4498. }
  4499. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4500. info := models.WarehousingInfo{}
  4501. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4502. return info, err
  4503. }
  4504. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4505. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4506. return err
  4507. }
  4508. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4509. if storehouse_id == 0 {
  4510. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4511. }
  4512. if storehouse_id > 0 {
  4513. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0 and storehouse_id=?", good_id, storehouse_id).Order("ctime asc").First(&info).Error
  4514. }
  4515. return info, err
  4516. }
  4517. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4518. if len(warehousingInfo) > 0 {
  4519. utx := writeDb.Begin()
  4520. if len(warehousingInfo) > 0 {
  4521. 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 "
  4522. insertParams := make([]string, 0)
  4523. insertData := make([]interface{}, 0)
  4524. for _, info := range warehousingInfo {
  4525. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4526. insertData = append(insertData, info.WarehousingId)
  4527. insertData = append(insertData, info.GoodId)
  4528. insertData = append(insertData, info.Number)
  4529. insertData = append(insertData, info.LicenseNumber)
  4530. insertData = append(insertData, info.Count)
  4531. insertData = append(insertData, info.UserOrgId)
  4532. insertData = append(insertData, info.PatientId)
  4533. insertData = append(insertData, info.SystemTime)
  4534. insertData = append(insertData, info.ConsumableType)
  4535. insertData = append(insertData, info.IsSys)
  4536. insertData = append(insertData, info.WarehousingOrder)
  4537. insertData = append(insertData, info.WarehouseOutId)
  4538. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4539. insertData = append(insertData, info.IsEdit)
  4540. insertData = append(insertData, info.CancelStockId)
  4541. insertData = append(insertData, info.CancelOrderNumber)
  4542. insertData = append(insertData, info.Manufacturer)
  4543. insertData = append(insertData, info.Dealer)
  4544. insertData = append(insertData, info.Creator)
  4545. insertData = append(insertData, info.UpdateCreator)
  4546. insertData = append(insertData, info.Status)
  4547. insertData = append(insertData, info.Ctime)
  4548. insertData = append(insertData, info.Mtime)
  4549. insertData = append(insertData, info.Price)
  4550. insertData = append(insertData, info.WarehousingDetailId)
  4551. insertData = append(insertData, info.WarehouseOutDetailId)
  4552. insertData = append(insertData, info.CancelOutDetailId)
  4553. insertData = append(insertData, info.ExpireDate)
  4554. insertData = append(insertData, info.ProductDate)
  4555. }
  4556. thisSQL += strings.Join(insertParams, ", ")
  4557. err = utx.Exec(thisSQL, insertData...).Error
  4558. if err != nil {
  4559. utx.Rollback()
  4560. return
  4561. }
  4562. }
  4563. utx.Commit()
  4564. }
  4565. return
  4566. }
  4567. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4568. utx := XTWriteDB().Begin()
  4569. err := utx.Create(&flow).Error
  4570. if err != nil {
  4571. utx.Rollback()
  4572. return err
  4573. }
  4574. utx.Commit()
  4575. return err
  4576. }
  4577. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4578. err := tx.Create(&flow).Error
  4579. if err != nil {
  4580. tx.Rollback()
  4581. return err
  4582. }
  4583. return err
  4584. }
  4585. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4586. err := XTWriteDB().Create(&flow).Error
  4587. return err
  4588. }
  4589. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4590. err := XTWriteDB().Save(&flow).Error
  4591. return err
  4592. }
  4593. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4594. 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, "over_count": flow.OverCount}).Error
  4595. return err
  4596. }
  4597. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  4598. err = XTWriteDB().Model(&flow).Where("warehouse_out_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehouse_out_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, "over_count": flow.OverCount}).Error
  4599. return err
  4600. }
  4601. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  4602. stock := models.VmStockFlow{}
  4603. var err error
  4604. err = XTReadDB().Model(&stock).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and is_read = 0", warehousing_id, patient_id, record_time, good_id).Find(&stock).Error
  4605. if err == gorm.ErrRecordNotFound {
  4606. return nil, err
  4607. }
  4608. if err != nil {
  4609. return nil, err
  4610. }
  4611. return &stock, nil
  4612. }
  4613. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4614. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3", patient_id, record_time, good_id).Find(&flow).Error
  4615. return flow, nil
  4616. }
  4617. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4618. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4619. return flow, nil
  4620. }
  4621. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4622. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4623. if err != gorm.ErrRecordNotFound {
  4624. if err != nil {
  4625. tx.Rollback()
  4626. return flow, nil
  4627. }
  4628. }
  4629. return flow, nil
  4630. }
  4631. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4632. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4633. return flow, nil
  4634. }
  4635. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  4636. err = tx.Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4637. if err != gorm.ErrRecordNotFound {
  4638. if err != nil {
  4639. tx.Rollback()
  4640. return flow, nil
  4641. }
  4642. }
  4643. return flow, nil
  4644. }
  4645. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  4646. err = XTReadDB().Where("patient_id = ? and record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4647. return flow, nil
  4648. }
  4649. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  4650. err = tx.Where("patient_id = ? and record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4651. if err != gorm.ErrRecordNotFound {
  4652. if err != nil {
  4653. tx.Rollback()
  4654. return flow, err
  4655. }
  4656. }
  4657. return flow, err
  4658. }
  4659. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4660. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  4661. return flow, nil
  4662. }
  4663. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4664. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7", patient_id, record_time, good_id).Find(&flow).Error
  4665. return flow, nil
  4666. }
  4667. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4668. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4669. return flow, nil
  4670. }
  4671. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4672. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4673. if err != gorm.ErrRecordNotFound {
  4674. if err != nil {
  4675. tx.Rollback()
  4676. return flow, nil
  4677. }
  4678. }
  4679. return flow, nil
  4680. }
  4681. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4682. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4683. return flow, nil
  4684. }
  4685. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  4686. info := models.WarehouseOutInfo{}
  4687. var err error
  4688. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4689. if err == gorm.ErrRecordNotFound {
  4690. return nil, err
  4691. }
  4692. if err != nil {
  4693. return nil, err
  4694. }
  4695. return &info, nil
  4696. }
  4697. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  4698. info := models.WarehouseOutInfo{}
  4699. var err error
  4700. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  4701. if err == gorm.ErrRecordNotFound {
  4702. return nil, err
  4703. }
  4704. if err != nil {
  4705. return nil, err
  4706. }
  4707. return &info, nil
  4708. }
  4709. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  4710. info := models.WarehouseOutInfo{}
  4711. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  4712. return info, err
  4713. }
  4714. 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) {
  4715. offset := (page - 1) * limit
  4716. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  4717. if consumable_type > 0 {
  4718. //入库
  4719. if consumable_type == 1 {
  4720. db = db.Where("consumable_type = ?", consumable_type)
  4721. }
  4722. //2 手动出库
  4723. if consumable_type == 2 {
  4724. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  4725. }
  4726. //3.自动出库
  4727. if consumable_type == 3 {
  4728. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  4729. }
  4730. //4.手动退库
  4731. if consumable_type == 4 {
  4732. db = db.Where(" consumable_type = ?", consumable_type)
  4733. }
  4734. //自动退库
  4735. if consumable_type == 7 {
  4736. db = db.Where(" consumable_type = ?", consumable_type)
  4737. }
  4738. if consumable_type == 10 {
  4739. db = db.Where(" consumable_type = ?", consumable_type)
  4740. }
  4741. if consumable_type == 11 {
  4742. db = db.Where(" consumable_type = ?", consumable_type)
  4743. }
  4744. if consumable_type == 12 {
  4745. db = db.Where(" consumable_type = ?", consumable_type)
  4746. }
  4747. if consumable_type == 13 {
  4748. db = db.Where(" consumable_type = ?", consumable_type)
  4749. }
  4750. if consumable_type == 15 {
  4751. db = db.Where(" consumable_type = ?", consumable_type)
  4752. }
  4753. }
  4754. if orgId > 0 {
  4755. db = db.Where("user_org_id = ?", orgId)
  4756. }
  4757. if startTime > 0 {
  4758. db = db.Where("ctime >= ?", startTime)
  4759. }
  4760. if endTime > 0 {
  4761. db = db.Where("ctime<=?", endTime)
  4762. }
  4763. if good_id > 0 {
  4764. db = db.Where("good_id = ?", good_id)
  4765. }
  4766. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  4767. return flow, total, err
  4768. }
  4769. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  4770. detail := models.AutomaticReduceDetail{}
  4771. 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
  4772. return detail, err
  4773. }
  4774. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  4775. detail := models.WarehouseOutInfo{}
  4776. 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
  4777. return detail, err
  4778. }
  4779. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  4780. outInfo := models.WarehouseOutInfo{}
  4781. 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, "over_count": info.OverCount}).Error
  4782. return err
  4783. }
  4784. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  4785. outInfo := models.WarehouseOutInfo{}
  4786. err := tx.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, "over_count": info.OverCount}).Error
  4787. if err != nil {
  4788. tx.Rollback()
  4789. return err
  4790. }
  4791. return err
  4792. }
  4793. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  4794. outInfo := models.WarehouseOutInfo{}
  4795. err := XTWriteDB().Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1 and project_id = ? and warehouse_info_id =? ", good_id, patient_id, record_time, project_id, warehouse_info_id).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, "over_count": info.OverCount}).Error
  4796. return err
  4797. }
  4798. func UpdatedNewWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) error {
  4799. outInfo := models.WarehouseOutInfo{}
  4800. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1 and project_id = ? and warehouse_info_id =? ", good_id, patient_id, record_time, project_id, warehouse_info_id).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, "over_count": info.OverCount}).Error
  4801. if err != nil {
  4802. tx.Rollback()
  4803. return err
  4804. }
  4805. return err
  4806. }
  4807. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  4808. info := models.WarehouseOutInfo{}
  4809. 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
  4810. return info, err
  4811. }
  4812. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  4813. info := models.WarehousingInfo{}
  4814. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4815. return info, err
  4816. }
  4817. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  4818. info := models.WarehousingInfo{}
  4819. db := XTReadDB().Model(&info)
  4820. if good_id > 0 {
  4821. db = db.Where("good_id = ? and status = 1", good_id)
  4822. }
  4823. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  4824. return info, err
  4825. }
  4826. 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) {
  4827. offset := (page - 1) * limit
  4828. db := XTReadDB().Model(drug_flow).Where("status = 1")
  4829. if drugid > 0 {
  4830. db = db.Where("drug_id = ?", drugid)
  4831. }
  4832. if orgid > 0 {
  4833. db = db.Where("user_org_id = ?", orgid)
  4834. }
  4835. if startTime > 0 {
  4836. db = db.Where("ctime >=? ", startTime)
  4837. }
  4838. if endTime > 0 {
  4839. db = db.Where("ctime<=?", endTime)
  4840. }
  4841. if stock_type > 0 {
  4842. //手动入库
  4843. if stock_type == 1 {
  4844. db = db.Where("consumable_type = 1")
  4845. }
  4846. //手动出库
  4847. if stock_type == 2 {
  4848. db = db.Where("consumable_type = 2 and is_sys = 0")
  4849. }
  4850. //自动出库
  4851. if stock_type == 3 {
  4852. db = db.Where("consumable_type = 3 and is_sys = 1")
  4853. }
  4854. if stock_type == 4 {
  4855. db = db.Where("consumable_type = 4")
  4856. }
  4857. if stock_type == 10 {
  4858. db = db.Where("consumable_type = 10")
  4859. }
  4860. if stock_type == 11 {
  4861. db = db.Where("consumable_type = 11")
  4862. }
  4863. if stock_type == 12 {
  4864. db = db.Where("consumable_type = 12")
  4865. }
  4866. if stock_type == 13 {
  4867. db = db.Where("consumable_type = 13")
  4868. }
  4869. if stock_type == 15 {
  4870. db = db.Where("consumable_type = 15")
  4871. }
  4872. }
  4873. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Preload("BaseDrugLib", "status = 1").Preload("DrugWarehouseInfo", "status = 1").Preload("XtDrugWarehouseOutInfo", "status = 1").Find(&drug_flow).Error
  4874. return drug_flow, total, err
  4875. }
  4876. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  4877. outInfo := models.DrugWarehouseOutInfo{}
  4878. var err error
  4879. 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
  4880. if err == gorm.ErrRecordNotFound {
  4881. return nil, err
  4882. }
  4883. if err != nil {
  4884. return nil, err
  4885. }
  4886. return &outInfo, nil
  4887. }
  4888. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  4889. 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
  4890. return outInfo, err
  4891. }
  4892. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  4893. outInfo := models.DrugWarehouseOutInfo{}
  4894. 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
  4895. return err
  4896. }
  4897. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  4898. db := XTReadDB().Model(&cancle).Where("status = 1")
  4899. if startime > 0 {
  4900. db = db.Where("ctime >=?", startime)
  4901. }
  4902. if endtime > 0 {
  4903. db = db.Where("ctime <=?", endtime)
  4904. }
  4905. if len(ids) > 0 {
  4906. db = db.Where("cancel_stock_id in(?)", ids)
  4907. }
  4908. if orgid > 0 {
  4909. db = db.Where("org_id = ?", orgid)
  4910. }
  4911. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  4912. err = db.Find(&cancle).Error
  4913. return cancle, err
  4914. }
  4915. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  4916. db := XTReadDB().Model(&info)
  4917. if id > 0 {
  4918. db = db.Where("cancel_stock_id = ? and status = 1", id)
  4919. }
  4920. db = db.Preload("BaseDrugLib")
  4921. err = db.Find(&info).Error
  4922. return info, err
  4923. }
  4924. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  4925. info := models.XtDrugWarehouseInfo{}
  4926. db := XTReadDB().Model(&info).Where("status = 1")
  4927. if id > 0 {
  4928. db = db.Where("id = ?", id)
  4929. }
  4930. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4931. return info, err
  4932. }
  4933. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  4934. info := models.XtDrugWarehouseInfo{}
  4935. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  4936. return info, err
  4937. }
  4938. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  4939. // info := models.XtDrugWarehouseInfo{}
  4940. // db := tx.Model(&info).Where("status = 1")
  4941. // if id > 0 {
  4942. // db = db.Where("id = ?", id)
  4943. // }
  4944. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4945. // return info, err
  4946. //}
  4947. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4948. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  4949. return info, err
  4950. }
  4951. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  4952. err = XTReadDB().Select("id,stock_max_number,stock_min_number").Where("drug_id = ? and org_id =? and status = 1 and storehouse_id = ? and is_check = 1", drugid, orgid, storehouse_id).Find(&info).Error
  4953. return info, err
  4954. }
  4955. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  4956. info := models.HisDoctorAdviceInfo{}
  4957. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  4958. return info, err
  4959. }
  4960. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  4961. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4962. return info, err
  4963. }
  4964. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  4965. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4966. if err != gorm.ErrRecordNotFound {
  4967. if err != nil {
  4968. tx.Rollback()
  4969. return info, err
  4970. }
  4971. }
  4972. return info, err
  4973. }
  4974. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  4975. adviceInfo := models.HisDoctorAdviceInfo{}
  4976. err := tx.Where("patient_id = ? and advice_date= ? and user_org_id = ? and status=1", patient_id, advice_date, user_org_id).Last(&adviceInfo).Error
  4977. if err != gorm.ErrRecordNotFound {
  4978. if err != nil {
  4979. tx.Rollback()
  4980. return adviceInfo, err
  4981. }
  4982. }
  4983. return adviceInfo, err
  4984. }
  4985. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4986. info := models.HisDoctorAdviceInfo{}
  4987. 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).First(&info).Error
  4988. return info, err
  4989. }
  4990. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4991. info := models.HisDoctorAdviceInfo{}
  4992. 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
  4993. return info, err
  4994. }
  4995. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  4996. detail := models.DrugAutomaticReduceDetail{}
  4997. 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
  4998. return detail, err
  4999. }
  5000. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  5001. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and advice_id =? and status =1", drug_id, patient_id, record_date, advice_id).Find(&out).Error
  5002. return out, err
  5003. }
  5004. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  5005. err = XTReadDB().Where("advice_id = ? and drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and is_check = 1", advice_id, drugid, patient_id, advice_date).Find(&out).Error
  5006. return out, err
  5007. }
  5008. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  5009. err = tx.Where("advice_id = ? and drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and is_check = 1", advice_id, drugid, patient_id, advice_date).Find(&out).Error
  5010. if err != gorm.ErrRecordNotFound {
  5011. if err != nil {
  5012. tx.Rollback()
  5013. return out, err
  5014. }
  5015. }
  5016. return out, err
  5017. }
  5018. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  5019. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5020. return err
  5021. }
  5022. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  5023. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5024. return err
  5025. }
  5026. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  5027. 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
  5028. 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
  5029. 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
  5030. return err
  5031. }
  5032. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5033. 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
  5034. 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
  5035. return err
  5036. }
  5037. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5038. 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
  5039. 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
  5040. return err
  5041. }
  5042. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5043. info := models.XtDrugWarehouseInfo{}
  5044. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5045. return info, err
  5046. }
  5047. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5048. info := models.XtDrugWarehouseInfo{}
  5049. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5050. return info, err
  5051. }
  5052. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5053. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5054. return err
  5055. }
  5056. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5057. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5058. // return err
  5059. //}
  5060. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5061. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5062. //return err
  5063. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5064. return err
  5065. }
  5066. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5067. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5068. // return err
  5069. //}
  5070. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5071. info := models.DrugCancelStockInfo{}
  5072. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5073. return info, err
  5074. }
  5075. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5076. info := models.DrugCancelStockInfo{}
  5077. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5078. return info, err
  5079. }
  5080. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5081. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5082. return err
  5083. }
  5084. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5085. 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
  5086. return err
  5087. }
  5088. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5089. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5090. return err
  5091. }
  5092. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5093. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1 and id = ?", drugid, orgid, warehouse_info_id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5094. return err
  5095. }
  5096. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5097. 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
  5098. return err
  5099. }
  5100. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5101. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1 and id = ?", drugid, orgid, warehouse_info_id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5102. return err
  5103. }
  5104. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5105. 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
  5106. return err
  5107. }
  5108. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5109. info := models.WarehousingInfo{}
  5110. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5111. return info, err
  5112. }
  5113. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5114. info := models.WarehousingInfo{}
  5115. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5116. return info, err
  5117. }
  5118. func GetLastWarehouseInfoTwo(goodid int64, storehouse_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  5119. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and org_id = ? and stock_count > 0", goodid, storehouse_id, user_org_id).Order("ctime").First(&info).Error
  5120. return info, err
  5121. }
  5122. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5123. info := models.WarehousingInfo{}
  5124. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5125. return info, err
  5126. }
  5127. func CreateWarehouseOut(out models.WarehouseOut) error {
  5128. err := XTWriteDB().Create(&out).Error
  5129. return err
  5130. }
  5131. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5132. out := models.WarehouseOut{}
  5133. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5134. return out, err
  5135. }
  5136. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5137. err := XTWriteDB().Create(&info).Error
  5138. return err
  5139. }
  5140. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5141. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5142. return err
  5143. }
  5144. func UpdateWareInfoById(id int64, count int64) error {
  5145. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5146. return err
  5147. }
  5148. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5149. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5150. return err
  5151. }
  5152. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5153. info := models.CancelStockInfo{}
  5154. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5155. return info, err
  5156. }
  5157. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5158. info := models.CancelStockInfo{}
  5159. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5160. return info, err
  5161. }
  5162. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5163. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5164. return info, err
  5165. }
  5166. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5167. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5168. return info, err
  5169. }
  5170. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5171. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5172. return err
  5173. }
  5174. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5175. drugFlow := models.DrugFlow{}
  5176. 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
  5177. return err
  5178. }
  5179. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5180. drugFlow := models.DrugFlow{}
  5181. 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
  5182. return err
  5183. }
  5184. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5185. stock := models.DrugCancelStock{}
  5186. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5187. return stock, err
  5188. }
  5189. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5190. // stock := models.DrugCancelStock{}
  5191. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5192. // return stock, err
  5193. //}
  5194. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5195. db := XTReadDB().Model(&info)
  5196. if warehouse_info_id > 0 {
  5197. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5198. }
  5199. if drug_id > 0 {
  5200. db = db.Where("drug_id = ? and status =1", drug_id)
  5201. }
  5202. err = db.Find(&info).Error
  5203. return info, err
  5204. }
  5205. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5206. // db := tx.Model(&info)
  5207. // if warehouse_info_id > 0 {
  5208. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5209. // }
  5210. // if drug_id > 0 {
  5211. // db = db.Where("drug_id = ? and status =1", drug_id)
  5212. // }
  5213. // err = db.Find(&info).Error
  5214. // return info, err
  5215. //}
  5216. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5217. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5218. return info, err
  5219. }
  5220. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5221. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5222. // return info, err
  5223. //}
  5224. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5225. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5226. return config, err
  5227. }
  5228. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5229. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5230. return config, err
  5231. }
  5232. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5233. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5234. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5235. fmt.Println(table)
  5236. likeKey := "%" + keyword + "%"
  5237. if orgid > 0 {
  5238. db = db.Where("x.org_id = ?", orgid)
  5239. }
  5240. if storehouse_id > 0 {
  5241. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5242. }
  5243. 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,x.storehouse_id,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
  5244. return info, err
  5245. }
  5246. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5247. err := XTWriteDB().Create(&adjustPrice).Error
  5248. return err
  5249. }
  5250. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5251. likeKey := "%" + keyword + "%"
  5252. offset := (page - 1) * limit
  5253. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5254. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5255. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5256. fmt.Println(table, tab)
  5257. if startime > 0 {
  5258. db = db.Where("x.start_time>=?", startime)
  5259. }
  5260. if endtime > 0 {
  5261. db = db.Where("x.start_time<=?", endtime)
  5262. }
  5263. if len(keyword) > 0 {
  5264. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5265. }
  5266. if orgid > 0 {
  5267. db = db.Where("x.user_org_id = ?", orgid)
  5268. }
  5269. 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
  5270. return list, total, err
  5271. }
  5272. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5273. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5274. return err
  5275. }
  5276. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5277. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5278. return err
  5279. }
  5280. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5281. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5282. return info, err
  5283. }
  5284. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5285. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5286. return adjust, err
  5287. }
  5288. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5289. err := XTWriteDB().Create(&price).Error
  5290. return err
  5291. }
  5292. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5293. likeKey := "%" + keyword + "%"
  5294. offset := (page - 1) * limit
  5295. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5296. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5297. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5298. fmt.Println(table, tab)
  5299. if startime > 0 {
  5300. db = db.Where("x.ctime>=?", startime)
  5301. }
  5302. if endtime > 0 {
  5303. db = db.Where("x.ctime<=?", endtime)
  5304. }
  5305. if len(keyword) > 0 {
  5306. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5307. }
  5308. if orgid > 0 {
  5309. db = db.Where("x.user_org_id = ?", orgid)
  5310. }
  5311. 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
  5312. return adjust, total, err
  5313. }
  5314. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5315. likeKey := "%" + keyword + "%"
  5316. offset := (page - 1) * limit
  5317. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5318. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5319. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5320. fmt.Println(table, tab)
  5321. if len(keyword) > 0 {
  5322. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5323. }
  5324. if orgid > 0 {
  5325. db = db.Where("x.user_org_id = ?", orgid)
  5326. }
  5327. if startime > 0 {
  5328. db = db.Where("x.ctime >=?", startime)
  5329. }
  5330. if endtime > 0 {
  5331. db = db.Where("x.ctime <=?", endtime)
  5332. }
  5333. if storehouse_id > 0 {
  5334. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5335. }
  5336. err = db.Group("x.good_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,x.storehouse_id,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
  5337. return adjust, total, err
  5338. }
  5339. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5340. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5341. if inventory_type == 0 {
  5342. db = db.Where("x.inventory_type = 4")
  5343. }
  5344. if inventory_type > 0 {
  5345. db = db.Where("x.inventory_type = ?", inventory_type)
  5346. }
  5347. if len(keyword) > 0 {
  5348. likeKey := "%" + keyword + "%"
  5349. db = db.Where("x.warehousing_order like ?", likeKey)
  5350. }
  5351. if orgid > 0 {
  5352. db = db.Where("x.user_org_id = ?", orgid)
  5353. }
  5354. 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
  5355. return adjust, err
  5356. }
  5357. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5358. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5359. if orgid > 0 {
  5360. db = db.Where("x.user_org_id = ?", orgid)
  5361. }
  5362. 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
  5363. return adjust, err
  5364. }
  5365. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5366. 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
  5367. return err
  5368. }
  5369. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5370. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5371. return report, err
  5372. }
  5373. func CreateInentory(inventory models.XtStockInventory) error {
  5374. err = XTWriteDB().Create(&inventory).Error
  5375. return err
  5376. }
  5377. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5378. likeKey := "%" + keyword + "%"
  5379. offset := (page - 1) * limit
  5380. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5381. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5382. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5383. fmt.Println(table, tab)
  5384. if startime > 0 {
  5385. db = db.Where("x.ctime>=?", startime)
  5386. }
  5387. if endtime > 0 {
  5388. db = db.Where("x.ctime<=?", endtime)
  5389. }
  5390. if len(keyword) > 0 {
  5391. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5392. }
  5393. if orgid > 0 {
  5394. db = db.Where("x.user_org_id = ?", orgid)
  5395. }
  5396. if inventory_status > 0 {
  5397. db = db.Where("checker_status = ?", inventory_status)
  5398. }
  5399. 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
  5400. return adjust, total, err
  5401. }
  5402. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5403. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5404. return err
  5405. }
  5406. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5407. info := models.XtStockAdjustPrice{}
  5408. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5409. return info, err
  5410. }
  5411. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5412. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5413. return list, err
  5414. }
  5415. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5416. info := models.WarehousingInfo{}
  5417. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5418. return info, err
  5419. }
  5420. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5421. info := models.WarehousingInfo{}
  5422. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5423. //return info, err
  5424. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5425. if id > 0 {
  5426. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5427. }
  5428. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5429. return info, err
  5430. }
  5431. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5432. info := models.WarehousingInfo{}
  5433. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5434. if id > 0 {
  5435. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5436. }
  5437. if storehouse_id > 0 {
  5438. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5439. }
  5440. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5441. return info, err
  5442. }
  5443. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5444. 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
  5445. return err
  5446. }
  5447. func DeleteStockPrice(id int64) error {
  5448. info := models.XtStockAdjustPrice{}
  5449. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5450. return err
  5451. }
  5452. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5453. report := models.XtStockReportPrice{}
  5454. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5455. return report, err
  5456. }
  5457. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5458. 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
  5459. return err
  5460. }
  5461. func DeleteDamage(id int64) error {
  5462. adjust := models.XtStockReportPrice{}
  5463. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5464. return err
  5465. }
  5466. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5467. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5468. return adjust, err
  5469. }
  5470. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5471. inventory := models.XtStockInventory{}
  5472. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5473. return inventory, err
  5474. }
  5475. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5476. 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
  5477. return err
  5478. }
  5479. func DeleteInventory(id int64) error {
  5480. inventory := models.XtStockInventory{}
  5481. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5482. return err
  5483. }
  5484. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5485. likeKey := "%" + keyword + "%"
  5486. offset := (page - 1) * limit
  5487. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5488. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5489. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5490. fmt.Println(table, tab)
  5491. if len(keyword) > 0 {
  5492. db = db.Where("x.good_name like ?", likeKey)
  5493. }
  5494. if orgid > 0 {
  5495. db = db.Where("x.user_org_id = ?", orgid)
  5496. }
  5497. if storehouse_id > 0 {
  5498. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5499. }
  5500. 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,x.storehouse_id,x.ctime").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).Order("x.id desc").Scan(&list).Error
  5501. return list, total, err
  5502. }
  5503. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5504. likeKey := "%" + keyword + "%"
  5505. offset := (page - 1) * limit
  5506. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5507. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5508. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5509. fmt.Println(table, tab)
  5510. if len(keyword) > 0 {
  5511. db = db.Where("x.good_name like ?", likeKey)
  5512. }
  5513. if orgid > 0 {
  5514. db = db.Where("x.user_org_id = ?", orgid)
  5515. }
  5516. if storehouse_id > 0 {
  5517. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5518. }
  5519. if len(ids) > 0 {
  5520. db = db.Where("x.id in(?)", ids)
  5521. }
  5522. 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,x.storehouse_id").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
  5523. return list, total, err
  5524. }
  5525. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5526. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5527. return list, err
  5528. }
  5529. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5530. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5531. return list, err
  5532. }
  5533. func DeleteDrugPrice(id int64) error {
  5534. info := models.XtDrugAdjustPrice{}
  5535. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5536. return err
  5537. }
  5538. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5539. info := models.WarehousingInfo{}
  5540. db := XTReadDB().Model(&info).Where("status = 1")
  5541. if goodid > 0 {
  5542. db = db.Where("good_id = ?", goodid)
  5543. }
  5544. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5545. return info, err
  5546. }
  5547. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5548. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5549. return err
  5550. }
  5551. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5552. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5553. return err
  5554. }
  5555. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5556. err := XTWriteDB().Create(&record).Error
  5557. return err
  5558. }
  5559. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5560. info := models.WarehousingInfo{}
  5561. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5562. //return info, err
  5563. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5564. if id > 0 {
  5565. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5566. }
  5567. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5568. return info, err
  5569. }
  5570. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5571. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5572. return info, err
  5573. }
  5574. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5575. info := models.DrugWarehouseInfo{}
  5576. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5577. return info, err
  5578. }
  5579. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5580. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit}).Error
  5581. return err
  5582. }
  5583. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5584. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit}).Error
  5585. return err
  5586. }
  5587. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5588. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and is_check= 1 and stock_count>0", id, storehouse_id).Preload("GoodInfo", "status = 1").Find(&info).Error
  5589. return info, err
  5590. }
  5591. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  5592. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5593. return err
  5594. }
  5595. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  5596. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  5597. return err
  5598. }
  5599. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  5600. db := XTReadDB().Model(&list).Where(" status = 1")
  5601. if good_id > 0 {
  5602. db = db.Where("good_id = ?", good_id)
  5603. }
  5604. if len(keyword) > 0 {
  5605. likeKey := "%" + keyword + "%"
  5606. db = db.Where("warehousing_order like ?", likeKey)
  5607. }
  5608. if inventory_type > 0 {
  5609. db = db.Where("inventory_type = ?", inventory_type)
  5610. }
  5611. if inventory_type == 0 {
  5612. db = db.Where("inventory_type = 4")
  5613. }
  5614. err = db.Find(&list).Error
  5615. return list, err
  5616. }
  5617. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  5618. flow := models.DrugFlow{}
  5619. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  5620. if err == gorm.ErrRecordNotFound {
  5621. return nil, err
  5622. }
  5623. if err != nil {
  5624. return nil, err
  5625. }
  5626. return &flow, nil
  5627. }
  5628. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  5629. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  5630. return info, err
  5631. }
  5632. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  5633. info := models.XtDrugWarehouseInfo{}
  5634. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  5635. return info, err
  5636. }
  5637. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5638. info := models.WarehousingInfo{}
  5639. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  5640. return info, err
  5641. }
  5642. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  5643. flow := models.VmStockFlow{}
  5644. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  5645. if err == gorm.ErrRecordNotFound {
  5646. return nil, err
  5647. }
  5648. if err != nil {
  5649. return nil, err
  5650. }
  5651. return &flow, nil
  5652. }
  5653. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  5654. info := models.GoodInfo{}
  5655. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  5656. return info, err
  5657. }
  5658. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  5659. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  5660. return err
  5661. }
  5662. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  5663. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  5664. return err
  5665. }
  5666. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  5667. 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, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  5668. return err
  5669. }
  5670. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5671. 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
  5672. return err
  5673. }
  5674. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5675. 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, "storehouse_id": flow.StorehouseId, "register_number": flow.RegisterNumber}).Error
  5676. return err
  5677. }
  5678. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  5679. 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, "storehouse_id": flow.StorehouseId, "admin_user_id": flow.AdminUserId, "stock_count": flow.StockCount, "register_number": flow.RegisterNumber}).Error
  5680. return err
  5681. }
  5682. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  5683. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  5684. return err
  5685. }
  5686. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  5687. if orgid == 10653 {
  5688. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0 and x.stock_count>0")
  5689. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5690. fmt.Println(table)
  5691. if orgid > 0 {
  5692. db = db.Where("x.org_id = ?", orgid)
  5693. }
  5694. 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
  5695. return info, err
  5696. } else {
  5697. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0")
  5698. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5699. fmt.Println(table)
  5700. if orgid > 0 {
  5701. db = db.Where("x.org_id = ?", orgid)
  5702. }
  5703. 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
  5704. return info, err
  5705. }
  5706. }
  5707. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  5708. warehouseInfo := models.XtDrugWarehouseInfo{}
  5709. 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, "storehouse_id": info.StorehouseId}).Error
  5710. return err
  5711. }
  5712. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  5713. err := XTWriteDB().Create(&info).Error
  5714. return err
  5715. }
  5716. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  5717. err := tx.Create(&info).Error
  5718. if err != nil {
  5719. tx.Rollback()
  5720. return err
  5721. }
  5722. return err
  5723. }
  5724. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  5725. flow := models.VmStockFlow{}
  5726. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  5727. if err == gorm.ErrRecordNotFound {
  5728. return nil, err
  5729. }
  5730. if err != nil {
  5731. return nil, err
  5732. }
  5733. return &flow, nil
  5734. }
  5735. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  5736. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5737. return err
  5738. }
  5739. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  5740. 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, "storehouse_id": flow.StorehouseId}).Error
  5741. return err
  5742. }
  5743. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  5744. flow := models.DrugWarehouseOutInfo{}
  5745. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  5746. return flow, err
  5747. }
  5748. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  5749. info := models.WarehouseOutInfo{}
  5750. 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
  5751. return info, err
  5752. }
  5753. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  5754. 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
  5755. return err
  5756. }
  5757. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  5758. 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
  5759. return err
  5760. }
  5761. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  5762. info := models.DrugWarehouseOutInfo{}
  5763. 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
  5764. if err == gorm.ErrRecordNotFound {
  5765. return nil, err
  5766. }
  5767. if err != nil {
  5768. return nil, err
  5769. }
  5770. return &info, nil
  5771. }
  5772. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  5773. drugFlow := models.DrugFlow{}
  5774. 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
  5775. if err == gorm.ErrRecordNotFound {
  5776. return nil, err
  5777. }
  5778. if err != nil {
  5779. return nil, err
  5780. }
  5781. return &drugFlow, nil
  5782. }
  5783. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  5784. 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
  5785. return err
  5786. }
  5787. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  5788. 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
  5789. return err
  5790. }
  5791. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  5792. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5793. return detail, err
  5794. }
  5795. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  5796. detail := models.DrugAutomaticReduceDetail{}
  5797. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5798. if err == gorm.ErrRecordNotFound {
  5799. return nil, err
  5800. }
  5801. if err != nil {
  5802. return nil, err
  5803. }
  5804. return &detail, nil
  5805. }
  5806. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  5807. 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
  5808. return err
  5809. }
  5810. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  5811. 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
  5812. return err
  5813. }
  5814. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  5815. info := models.DrugWarehouseInfo{}
  5816. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5817. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5818. return info, err
  5819. }
  5820. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  5821. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5822. return err
  5823. }
  5824. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  5825. info := models.DrugWarehouseInfo{}
  5826. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5827. return err
  5828. }
  5829. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  5830. info := models.DrugCancelStockInfo{}
  5831. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5832. return info, err
  5833. }
  5834. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  5835. info := models.DrugWarehouseInfo{}
  5836. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  5837. return err
  5838. }
  5839. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  5840. info := models.DrugWarehouseInfo{}
  5841. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  5842. return err
  5843. }
  5844. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  5845. err = XTReadDB().Where("drug_id = ? and patient_id = ? and advice_date = ? and execution_state = 1 and status = 1 and user_org_id = ?", drugid, patient_id, advicedate, orgid).Find(&advice).Error
  5846. return advice, err
  5847. }
  5848. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  5849. err = tx.Where("drug_id = ? and patient_id = ? and advice_date = ? and execution_state = 1 and status = 1 and user_org_id = ?", drugid, patient_id, advicedate, orgid).Find(&advice).Error
  5850. if err != gorm.ErrRecordNotFound {
  5851. if err != nil {
  5852. tx.Rollback()
  5853. return advice, err
  5854. }
  5855. }
  5856. return advice, err
  5857. }
  5858. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5859. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  5860. return info, err
  5861. }
  5862. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5863. err = tx.Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  5864. if err != gorm.ErrRecordNotFound {
  5865. if err != nil {
  5866. tx.Rollback()
  5867. return info, err
  5868. }
  5869. }
  5870. return info, err
  5871. }
  5872. // 耗材加库存
  5873. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5874. info := models.GoodInfo{}
  5875. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5876. return info, err
  5877. }
  5878. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5879. info := models.GoodInfo{}
  5880. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5881. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_count)).Error
  5882. return info, err
  5883. }
  5884. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5885. info := models.GoodInfo{}
  5886. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5887. return info, err
  5888. }
  5889. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5890. info := models.GoodInfo{}
  5891. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5892. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", sum_count)).Error
  5893. return info, err
  5894. }
  5895. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  5896. info := models.GoodInfo{}
  5897. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"batch_retai_price": retail_price}).Error
  5898. return info, err
  5899. }
  5900. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5901. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5902. return info, err
  5903. }
  5904. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5905. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5906. return info, err
  5907. }
  5908. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  5909. info := models.WarehousingInfo{}
  5910. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  5911. return info, err
  5912. }
  5913. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  5914. info := models.WarehousingInfo{}
  5915. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  5916. return info, err
  5917. }
  5918. // 药品加库存
  5919. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5920. info := models.BaseDrugLib{}
  5921. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5922. return info, err
  5923. }
  5924. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5925. info := models.BaseDrugLib{}
  5926. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5927. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_count)).Error
  5928. return info, err
  5929. }
  5930. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5931. info := models.BaseDrugLib{}
  5932. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5933. return info, err
  5934. }
  5935. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5936. info := models.BaseDrugLib{}
  5937. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5938. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", sum_count)).Error
  5939. return info, err
  5940. }
  5941. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  5942. info := models.BaseDrugLib{}
  5943. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5944. err = XTWriteDB().Model(&info).Where("id=? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"batch_retai_price": batch_retai_price}).Error
  5945. return info, err
  5946. }
  5947. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5948. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5949. return info, err
  5950. }
  5951. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5952. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5953. return info, err
  5954. }
  5955. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  5956. info := models.WarehouseOutInfo{}
  5957. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5958. return info, err
  5959. }
  5960. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  5961. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  5962. return out, err
  5963. }
  5964. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  5965. info := models.WarehousingInfo{}
  5966. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5967. return info, err
  5968. }
  5969. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  5970. err = XTReadDB().Where("status = 1").Find(&template).Error
  5971. return template, err
  5972. }
  5973. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  5974. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  5975. return good, err
  5976. }
  5977. func GetMobileGoodList(org_id int64) (list []*models.GoodInformationThirty, err error) {
  5978. err = XTReadDB().Where("org_id = ? and status =1", org_id).Find(&list).Error
  5979. return list, err
  5980. }
  5981. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  5982. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  5983. return project, err
  5984. }
  5985. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5986. info := models.GoodInfo{}
  5987. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5988. return info, err
  5989. }
  5990. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  5991. db := XTReadDB().Where("status = 1")
  5992. if good_id > 0 {
  5993. db = db.Where("good_id = ?", good_id)
  5994. }
  5995. err = db.Find(&info).Error
  5996. return info, err
  5997. }
  5998. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5999. db := XTReadDB().Model(&info).Where("status = 1")
  6000. if drug_id > 0 {
  6001. db = db.Where("drug_id = ?", drug_id)
  6002. }
  6003. err = db.Find(&info).Error
  6004. return info, err
  6005. }
  6006. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6007. lib := models.BaseDrugLib{}
  6008. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6009. return lib, err
  6010. }
  6011. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  6012. info := models.Warehousing{}
  6013. err := XTReadDB().Where("is_sys= ? and second_warehouse_id=? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, sencond_wahouse_id, orgid, record_date).Find(&info).Error
  6014. if err == gorm.ErrRecordNotFound {
  6015. return nil, err
  6016. }
  6017. if err != nil {
  6018. return nil, err
  6019. }
  6020. return &info, nil
  6021. }
  6022. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  6023. info := models.Warehousing{}
  6024. err := XTReadDB().Where("is_sys = ? and second_warehouse_id=? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, sencond_wahouse_id, orgid, record_date).Last(&info).Error
  6025. return info, err
  6026. }
  6027. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  6028. out := models.DrugWarehouseOut{}
  6029. err := XTReadDB().Where("org_id = ? and is_sys = ? and second_warehouse_id = ? and warehouse_out_time = ? and status = 1", orgid, is_sys, sencond_wahouse_id, record_date).Find(&out).Error
  6030. if err == gorm.ErrRecordNotFound {
  6031. return nil, err
  6032. }
  6033. if err != nil {
  6034. return nil, err
  6035. }
  6036. return &out, nil
  6037. }
  6038. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  6039. info := models.DrugWarehouseOut{}
  6040. err := XTReadDB().Where("is_sys = ? and second_warehouse_id = ? and status = 1 and org_id =? and warehouse_out_time = ?", is_sys, sencond_wahouse_id, orgid, record_date).Last(&info).Error
  6041. return info, err
  6042. }
  6043. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  6044. info := models.DrugWarehouse{}
  6045. err := XTReadDB().Where("is_sys = ? and second_warehouse_id = ? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, second_warehouse_id, orgid, record_date).Find(&info).Error
  6046. if err == gorm.ErrRecordNotFound {
  6047. return nil, err
  6048. }
  6049. if err != nil {
  6050. return nil, err
  6051. }
  6052. return &info, nil
  6053. }
  6054. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  6055. err := XTWriteDB().Create(&warehouse).Error
  6056. return err
  6057. }
  6058. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  6059. info := models.DrugWarehouse{}
  6060. err := XTReadDB().Where("is_sys = ? and second_warehouse_id = ? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, second_warehouse_id, orgid, record_date).Last(&info).Error
  6061. return info, err
  6062. }
  6063. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6064. info := models.GoodInfo{}
  6065. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  6066. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", waresing_count)).Error
  6067. return info, err
  6068. }
  6069. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6070. info := models.GoodInfo{}
  6071. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6072. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", waresing_count)).Error
  6073. return info, err
  6074. }
  6075. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6076. tx := XTWriteDB().Begin()
  6077. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  6078. if err != nil {
  6079. tx.Rollback()
  6080. return err
  6081. }
  6082. tx.Commit()
  6083. return err
  6084. }
  6085. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6086. tx := XTWriteDB().Begin()
  6087. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6088. if err != nil {
  6089. tx.Rollback()
  6090. return err
  6091. }
  6092. tx.Commit()
  6093. return err
  6094. }
  6095. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6096. // err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  6097. // return err
  6098. //}
  6099. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6100. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  6101. return err
  6102. }
  6103. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6104. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6105. table := XTReadDB().Table("xt_base_drug as x d")
  6106. fmt.Println(table)
  6107. if startime > 0 {
  6108. db = db.Where("x.advice_date >= ?", startime)
  6109. }
  6110. if endtime > 0 {
  6111. db = db.Where("x.advice_date <= ?", endtime)
  6112. }
  6113. if orgid > 0 {
  6114. db = db.Where("x.user_org_id = ?", orgid)
  6115. }
  6116. if len(keyword) > 0 {
  6117. likeKey := "%" + keyword + "%"
  6118. db = db.Select("x.patient_id,x.user_org_id,x.id,x.advice_date,x.advice_date,x.prescribing_number_unit,x.drug_id,x.prescribing_number").Joins("left join sgj_xt.xt_base_drug as d on d.id = x.drug_id").Where("d.drug_name like ?", likeKey)
  6119. }
  6120. offset := (page - 1) * limit
  6121. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6122. return adviceinfo, total, err
  6123. }
  6124. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6125. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6126. table := XTReadDB().Table("xt_base_drug as x d")
  6127. fmt.Println(table)
  6128. if startime > 0 {
  6129. db = db.Where("x.advice_date >= ?", startime)
  6130. }
  6131. if endtime > 0 {
  6132. db = db.Where("x.advice_date <= ?", endtime)
  6133. }
  6134. if orgid > 0 {
  6135. db = db.Where("x.user_org_id = ?", orgid)
  6136. }
  6137. if len(keyword) > 0 {
  6138. likeKey := "%" + keyword + "%"
  6139. db = db.Select("x.patient_id,x.user_org_id,x.id,x.advice_date,x.advice_date,x.prescribing_number_unit,x.drug_id,x.prescribing_number").Joins("left join sgj_xt.xt_base_drug as d on d.id = x.drug_id").Where("d.drug_name like ?", likeKey)
  6140. }
  6141. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6142. return adviceinfo, total, err
  6143. }
  6144. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6145. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6146. return drug, err
  6147. }
  6148. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6149. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6150. table := XTReadDB().Table("xt_good_information as d")
  6151. fmt.Println(table)
  6152. if startime > 0 {
  6153. db = db.Where("x.record_date >= ?", startime)
  6154. }
  6155. if endtime > 0 {
  6156. db = db.Where("x.record_date <= ?", endtime)
  6157. }
  6158. if orgid > 0 {
  6159. db = db.Where("x.user_org_id = ?", orgid)
  6160. }
  6161. if len(keyword) > 0 {
  6162. likeKey := "%" + keyword + "%"
  6163. db = db.Select("x.id,x.project_id,x.user_org_id,x.patient_id,x.record_date,x.count").Joins("left join xt_good_information as d on d.id = x.project_id").Where("d.good_name like ?", likeKey).Group("d.id")
  6164. }
  6165. offset := (page - 1) * limit
  6166. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6167. return project, total, err
  6168. }
  6169. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6170. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6171. table := XTReadDB().Table("xt_good_information as d")
  6172. fmt.Println(table)
  6173. if startime > 0 {
  6174. db = db.Where("x.record_date >= ?", startime)
  6175. }
  6176. if endtime > 0 {
  6177. db = db.Where("x.record_date <= ?", endtime)
  6178. }
  6179. if orgid > 0 {
  6180. db = db.Where("x.user_org_id = ?", orgid)
  6181. }
  6182. if len(keyword) > 0 {
  6183. likeKey := "%" + keyword + "%"
  6184. db = db.Select("x.id,x.project_id,x.user_org_id,x.patient_id,x.record_date,x.count").Joins("left join xt_good_information as d on d.id = x.project_id").Where("d.good_name like ? ", likeKey).Group("d.id")
  6185. }
  6186. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6187. return project, total, err
  6188. }
  6189. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6190. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6191. return good, err
  6192. }
  6193. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6194. setting := models.XtStockSetting{}
  6195. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6196. if err == gorm.ErrRecordNotFound {
  6197. return nil, err
  6198. }
  6199. if err != nil {
  6200. return nil, err
  6201. }
  6202. return &setting, nil
  6203. }
  6204. func CreateStockSetting(setting models.XtStockSetting) error {
  6205. err := XTWriteDB().Create(&setting).Error
  6206. return err
  6207. }
  6208. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6209. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6210. return err
  6211. }
  6212. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6213. setting := models.XtStockSetting{}
  6214. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6215. return setting, err
  6216. }
  6217. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6218. detail := models.BloodAutomaticReduceDetail{}
  6219. err := XTReadDB().Where("good_id = ? and record_time = ? and org_id = ? and status = 1 and patient_id = ?", good_id, record_date, orgid, patient_id).Find(&detail).Error
  6220. return detail, err
  6221. }
  6222. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6223. detail := models.WarehouseOutInfoNight{}
  6224. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and org_id = ? and status = 1 and patient_id = ?", good_id, record_date, orgid, patient_id).Find(&detail).Error
  6225. return detail, err
  6226. }
  6227. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6228. setting := models.XtAdviceSetting{}
  6229. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6230. if err == gorm.ErrRecordNotFound {
  6231. return nil, err
  6232. }
  6233. if err != nil {
  6234. return nil, err
  6235. }
  6236. return &setting, nil
  6237. }
  6238. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6239. err := XTWriteDB().Create(&setting).Error
  6240. return err
  6241. }
  6242. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6243. err := XTWriteDB().Model(&setting).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_advice_open": setting.IsAdviceOpen, "mtime": time.Now().Unix()}).Error
  6244. return err
  6245. }
  6246. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6247. setting := models.XtAdviceSetting{}
  6248. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6249. return setting, err
  6250. }
  6251. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6252. config := models.XtPrescriptionConfig{}
  6253. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6254. if err == gorm.ErrRecordNotFound {
  6255. return nil, err
  6256. }
  6257. if err != nil {
  6258. return nil, err
  6259. }
  6260. return &config, nil
  6261. }
  6262. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6263. err := XTWriteDB().Create(&prescriptionConfig).Error
  6264. return err
  6265. }
  6266. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6267. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": config.IsOpen, "mtime": time.Now().Unix()}).Error
  6268. return err
  6269. }
  6270. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6271. config := models.XtPrescriptionConfig{}
  6272. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6273. return config, err
  6274. }
  6275. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6276. var deliver_number int64 = 0
  6277. var stock_number int64 = 0
  6278. deliver_number = maxNumber
  6279. // 根据先进先出原则,查询最先入库的批次,进行出库
  6280. // 如果没有对应的库存,则报错
  6281. //开启事物
  6282. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6283. if err != nil {
  6284. return err
  6285. }
  6286. // 将该批次的剩余库存数量转换为拆零数量
  6287. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6288. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6289. if stock_number >= deliver_number {
  6290. var maxNumber int64 = 0
  6291. var minNumber int64 = 0
  6292. var stock_max_number int64 = 0
  6293. stock_max_number = warehouse.StockMaxNumber
  6294. maxNumber = deliver_number / drup.MinNumber
  6295. minNumber = deliver_number % drup.MinNumber
  6296. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6297. minNumber = maxNumber
  6298. }
  6299. if drup.MaxUnit != drup.MinUnit {
  6300. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6301. return errors.New("库存数量不足")
  6302. }
  6303. }
  6304. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6305. if warehouse.StockMaxNumber < 0 {
  6306. warehouse.StockMaxNumber = 0
  6307. }
  6308. if warehouse.StockMinNumber < 0 {
  6309. warehouse.StockMinNumber = 0
  6310. }
  6311. warehouse.Mtime = time.Now().Unix()
  6312. if warehouse.StockMinNumber < minNumber {
  6313. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6314. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6315. } else {
  6316. if minNumber > 0 {
  6317. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6318. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6319. } else {
  6320. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6321. }
  6322. }
  6323. if minNumber == 0 && maxNumber != 1 {
  6324. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6325. }
  6326. }
  6327. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6328. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6329. if warehouse.StockMinNumber == 0 {
  6330. warehouse.StockMaxNumber = stock_max_number
  6331. }
  6332. }
  6333. if drup.MaxUnit != drup.MinUnit {
  6334. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6335. return errors.New("库存数量不足")
  6336. }
  6337. }
  6338. if warehouse.StockMinNumber <= 0 {
  6339. warehouse.StockMinNumber = 0
  6340. }
  6341. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6342. fmt.Println(errThree)
  6343. //查询默认仓库
  6344. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6345. //查询默认仓库剩余多少库存
  6346. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6347. var sum_count int64
  6348. var sum_in_count int64
  6349. for _, it := range list {
  6350. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6351. if it.MaxUnit == baseDrug.MaxUnit {
  6352. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6353. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6354. sum_count += it.StockMaxNumber + it.StockMinNumber
  6355. sum_in_count += it.WarehousingCount
  6356. }
  6357. }
  6358. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6359. return nil
  6360. } else {
  6361. warehouse.StockMaxNumber = 0
  6362. warehouse.StockMinNumber = 0
  6363. warehouse.Mtime = time.Now().Unix()
  6364. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6365. fmt.Println(errThree)
  6366. //查询默认仓库
  6367. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6368. //查询默认仓库剩余多少库存
  6369. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6370. var sum_count int64
  6371. var sum_in_count int64
  6372. for _, it := range list {
  6373. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6374. if it.MaxUnit == baseDrug.MaxUnit {
  6375. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6376. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6377. sum_count += it.StockMaxNumber + it.StockMinNumber
  6378. sum_in_count += it.WarehousingCount
  6379. }
  6380. }
  6381. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6382. if errThree != nil {
  6383. return errThree
  6384. }
  6385. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6386. prescribingNumber_two_temp := deliver_number - stock_number
  6387. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6388. }
  6389. return
  6390. }
  6391. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6392. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6393. return err
  6394. }
  6395. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6396. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6397. return err
  6398. }
  6399. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6400. config := models.DrugOutConfig{}
  6401. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6402. if err == gorm.ErrRecordNotFound {
  6403. return nil, err
  6404. }
  6405. if err != nil {
  6406. return nil, err
  6407. }
  6408. return &config, nil
  6409. }
  6410. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6411. config := models.DrugOutConfig{}
  6412. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6413. return config, err
  6414. }
  6415. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6416. config := models.XtHisConfig{}
  6417. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6418. return config, err
  6419. }
  6420. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6421. err := XTWriteDB().Create(&config).Error
  6422. return err
  6423. }
  6424. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6425. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6426. return err
  6427. }
  6428. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6429. config := models.DrugOutConfig{}
  6430. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6431. return config, err
  6432. }
  6433. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6434. config := models.HisSettleStockConfig{}
  6435. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6436. return config, err
  6437. }
  6438. func UpdatedWarehouseOut(id int64) error {
  6439. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6440. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6441. return err
  6442. }
  6443. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6444. tx := XTWriteDB().Begin()
  6445. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6446. if err != nil {
  6447. tx.Rollback()
  6448. return err
  6449. }
  6450. tx.Commit()
  6451. return err
  6452. }
  6453. func GetLastGoodWarehouseOutInfo(orgid int64, id int64) (info []*models.WarehouseOutInfo, err error) {
  6454. err = XTReadDB().Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6455. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6456. }).Find(&info).Error
  6457. return info, err
  6458. }
  6459. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6460. info := models.WarehouseOutInfo{}
  6461. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6462. return info, err
  6463. }
  6464. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6465. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "storehouse_id": info.StorehouseId, "admin_user_id": info.AdminUserId, "buy_price": info.BuyPrice, "stock_count": info.StockCount, "register_number": info.RegisterNumber}).Error
  6466. return err
  6467. }
  6468. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6469. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("warehouse_out_detail_id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "number": info.Number, "license_number": info.LicenseNumber, "count": info.Count, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "price": info.Price, "expire_date": info.ExpireDate, "product_date": info.ProductDate, "storehouse_id": info.StorehouseId, "admin_user_id": info.AdminUserId, "buy_price": info.BuyPrice, "stock_count": info.StockCount}).Error
  6470. return err
  6471. }
  6472. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6473. out := models.WarehouseOut{}
  6474. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6475. return out, err
  6476. }
  6477. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6478. ut := writeDb.Begin()
  6479. err := ut.Model(&models.WarehousingInfo{}).Where("good_id = ? and org_id = ? and status = 1 and id = ?", good_id, orgid, warehouse_info_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6480. if err != nil {
  6481. ut.Rollback()
  6482. return err
  6483. }
  6484. ut.Commit()
  6485. return err
  6486. }
  6487. func ModifyGoodWarehouseOut(id int64) error {
  6488. ut := writeDb.Begin()
  6489. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6490. if err != nil {
  6491. ut.Rollback()
  6492. return err
  6493. }
  6494. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6495. if err != nil {
  6496. ut.Rollback()
  6497. return err
  6498. }
  6499. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6500. if err != nil {
  6501. ut.Rollback()
  6502. return err
  6503. }
  6504. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6505. if err != nil {
  6506. ut.Rollback()
  6507. return err
  6508. }
  6509. ut.Commit()
  6510. return err
  6511. }
  6512. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6513. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6514. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6515. }).Find(&info).Error
  6516. return info, err
  6517. }
  6518. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  6519. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  6520. return info, err
  6521. }
  6522. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  6523. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  6524. return err
  6525. }
  6526. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  6527. tx := XTWriteDB().Begin()
  6528. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6529. if err != nil {
  6530. tx.Rollback()
  6531. return err
  6532. }
  6533. tx.Commit()
  6534. return err
  6535. }
  6536. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  6537. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6538. return info, err
  6539. }
  6540. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  6541. tx := XTWriteDB().Begin()
  6542. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  6543. if err != nil {
  6544. tx.Rollback()
  6545. return err
  6546. }
  6547. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6548. if err != nil {
  6549. tx.Rollback()
  6550. return err
  6551. }
  6552. tx.Commit()
  6553. return err
  6554. }
  6555. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  6556. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6557. return info, err
  6558. }
  6559. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  6560. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6561. return info, err
  6562. }
  6563. func FindLastDrugWarehousingInfoTwenty(org_id int64) (info models.DrugWarehouseInfo, err error) {
  6564. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6565. return info, err
  6566. }
  6567. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6568. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  6569. return info, err
  6570. }
  6571. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  6572. err := XTWriteDB().Create(&info).Error
  6573. return err
  6574. }
  6575. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  6576. err := XTWriteDB().Save(&info).Error
  6577. return err
  6578. }
  6579. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  6580. tx := XTWriteDB().Begin()
  6581. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6582. if err != nil {
  6583. tx.Rollback()
  6584. return err
  6585. }
  6586. tx.Commit()
  6587. return err
  6588. }
  6589. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6590. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6591. return info, err
  6592. }
  6593. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  6594. tx := XTWriteDB().Begin()
  6595. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6596. if err != nil {
  6597. tx.Rollback()
  6598. return err
  6599. }
  6600. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6601. if err != nil {
  6602. tx.Rollback()
  6603. return err
  6604. }
  6605. tx.Commit()
  6606. return err
  6607. }
  6608. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  6609. tx := XTWriteDB().Begin()
  6610. err := tx.Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  6611. if err != nil {
  6612. tx.Rollback()
  6613. return err
  6614. }
  6615. tx.Commit()
  6616. return err
  6617. }
  6618. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  6619. tx := XTWriteDB().Begin()
  6620. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6621. if err != nil {
  6622. tx.Rollback()
  6623. return err
  6624. }
  6625. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6626. tx.Commit()
  6627. return err
  6628. }
  6629. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  6630. tx := XTWriteDB().Begin()
  6631. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6632. if err != nil {
  6633. tx.Rollback()
  6634. return err
  6635. }
  6636. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6637. if err != nil {
  6638. tx.Rollback()
  6639. return err
  6640. }
  6641. tx.Commit()
  6642. return err
  6643. }
  6644. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  6645. tx := XTWriteDB().Begin()
  6646. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6647. if err != nil {
  6648. tx.Rollback()
  6649. return err
  6650. }
  6651. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6652. if err != nil {
  6653. tx.Rollback()
  6654. return err
  6655. }
  6656. tx.Commit()
  6657. return err
  6658. }
  6659. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  6660. tx := XTWriteDB().Begin()
  6661. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6662. if err != nil {
  6663. tx.Rollback()
  6664. return err
  6665. }
  6666. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6667. if err != nil {
  6668. tx.Rollback()
  6669. return err
  6670. }
  6671. tx.Commit()
  6672. return err
  6673. }
  6674. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  6675. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  6676. return err
  6677. }
  6678. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  6679. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  6680. return err
  6681. }
  6682. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  6683. err := tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  6684. if err != gorm.ErrRecordNotFound {
  6685. return err
  6686. }
  6687. return err
  6688. }
  6689. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  6690. out := models.DrugWarehouseOut{}
  6691. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  6692. return out, err
  6693. }
  6694. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  6695. out := models.XtSelfDrugWarehouseOut{}
  6696. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  6697. if err != gorm.ErrRecordNotFound {
  6698. if err != nil {
  6699. tx.Rollback()
  6700. return out, err
  6701. }
  6702. }
  6703. return out, err
  6704. }
  6705. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  6706. out := models.XtSelfDrugWarehouseOut{}
  6707. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  6708. if err != gorm.ErrRecordNotFound {
  6709. if err != nil {
  6710. tx.Rollback()
  6711. return out, err
  6712. }
  6713. }
  6714. return out, err
  6715. }
  6716. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6717. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  6718. return info, err
  6719. }
  6720. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  6721. tx := XTWriteDB().Begin()
  6722. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  6723. if err != nil {
  6724. tx.Rollback()
  6725. return err
  6726. }
  6727. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6728. if err != nil {
  6729. tx.Rollback()
  6730. return err
  6731. }
  6732. tx.Commit()
  6733. return err
  6734. }
  6735. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  6736. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  6737. return err
  6738. }
  6739. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  6740. out := models.DrugWarehouseOut{}
  6741. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  6742. return out, err
  6743. }
  6744. func UpdateDrugMaxNumber(count int64, id int64) error {
  6745. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6746. return err
  6747. }
  6748. func UpdateDrugMinNumber(count int64, id int64) error {
  6749. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6750. return err
  6751. }
  6752. func DeleteDrugOutFlow(id int64, orgid int64) error {
  6753. err := XTWriteDB().Model(models.DrugFlow{}).Where("warehouse_out_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  6754. return err
  6755. }
  6756. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  6757. err = XTReadDB().Where("warehouse_info_id = ? and good_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, good_id, orgid).Find(&outInfo).Error
  6758. return outInfo, err
  6759. }
  6760. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6761. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, drug_id, orgid).Find(&outInfo).Error
  6762. return outInfo, err
  6763. }
  6764. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6765. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  6766. return info, err
  6767. }
  6768. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6769. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  6770. return info, err
  6771. }
  6772. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  6773. err = XTReadDB().Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", goodid, storehouse_id, orgId).Find(&info).Error
  6774. return info, err
  6775. }
  6776. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  6777. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6778. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_count)).Error
  6779. return err
  6780. }
  6781. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  6782. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6783. return err
  6784. }
  6785. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  6786. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6787. return err
  6788. }
  6789. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  6790. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6791. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6792. return err
  6793. }
  6794. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  6795. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id = ?", orgid).Find(&stock).Error
  6796. return stock, err
  6797. }
  6798. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  6799. err := XTWriteDB().Model(&models.CancelStockInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "count": info.Count, "price": info.Price, "total": info.Total, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "register_account": info.RegisterAccount, "remark": info.Remark, "storehouse_id": info.StorehouseId}).Error
  6800. return err
  6801. }
  6802. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6803. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6804. return info, err
  6805. }
  6806. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  6807. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6808. return info, err
  6809. }
  6810. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  6811. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6812. return info, err
  6813. }
  6814. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  6815. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6816. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6817. return err
  6818. }
  6819. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6820. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  6821. return info, err
  6822. }
  6823. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  6824. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and status = 1", warehouse_info_id, orgid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", stock_count)).Error
  6825. return err
  6826. }
  6827. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  6828. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6829. return err
  6830. }
  6831. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  6832. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6833. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6834. return err
  6835. }
  6836. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  6837. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and user_org_id = ? and status = 1 and good_id =?", id, orgid, goodid).Update(map[string]interface{}{"status": 0}).Error
  6838. return err
  6839. }
  6840. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  6841. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and good_id = ? and org_id = ? and status = 1", warehouse_info_id, good_id, org_id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", stock_count)).Error
  6842. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and status = 1 and org_id = ?", good_id, org_id).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", stock_count)).Error
  6843. return err
  6844. }
  6845. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6846. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6847. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_in_count)).Error
  6848. return err
  6849. }
  6850. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6851. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  6852. return info, err
  6853. }
  6854. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  6855. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6856. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_in_count)).Error
  6857. return err
  6858. }
  6859. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  6860. err = XTReadDB().Where("storehouse_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", storehouse_id, orgid, drugid).Find(&info).Error
  6861. return info, err
  6862. }
  6863. func GetSelfDrugAllStockInfoOne(patient_id int64, orgid int64, drugid int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  6864. err = XTReadDB().Where("patient_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", patient_id, orgid, drugid).Find(&info).Error
  6865. return info, err
  6866. }
  6867. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  6868. err = tx.Where("storehouse_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", storehouse_id, orgid, drugid).Find(&info).Error
  6869. if err != gorm.ErrRecordNotFound {
  6870. if err != nil {
  6871. tx.Rollback()
  6872. return info, err
  6873. }
  6874. }
  6875. return info, err
  6876. }
  6877. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  6878. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6879. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6880. return err
  6881. }
  6882. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  6883. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6884. return err
  6885. }
  6886. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  6887. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6888. if err != nil {
  6889. tx.Rollback()
  6890. return err
  6891. }
  6892. return err
  6893. }
  6894. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6895. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", id, orgid).Preload("DrugWarehouseInfo", "status = 1 and org_id = ?", orgid).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Find(&info).Error
  6896. return info, err
  6897. }
  6898. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6899. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6900. return info, err
  6901. }
  6902. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  6903. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6904. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6905. return err
  6906. }
  6907. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  6908. stock := models.DrugCancelStock{}
  6909. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  6910. return stock, err
  6911. }
  6912. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6913. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  6914. return stock, err
  6915. }
  6916. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6917. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  6918. return stock, err
  6919. }
  6920. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6921. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  6922. return info, err
  6923. }
  6924. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  6925. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6926. return err
  6927. }
  6928. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6929. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6930. if err != nil {
  6931. tx.Rollback()
  6932. return err
  6933. }
  6934. return err
  6935. }
  6936. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  6937. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6938. return err
  6939. }
  6940. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6941. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6942. if err != nil {
  6943. tx.Rollback()
  6944. return err
  6945. }
  6946. return err
  6947. }
  6948. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  6949. err := XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "price": info.Price, "total": info.Total, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "register_account": info.RegisterAccount, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "batch_number_id": info.BatchNumberId, "storehouse_id": info.StorehouseId}).Error
  6950. return err
  6951. }
  6952. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  6953. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6954. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6955. return err
  6956. }
  6957. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6958. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  6959. return info, err
  6960. }
  6961. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  6962. err := XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and drug_id = ? and user_org_id = ? and status = 1", cancelstock_id, drugId, orgid).Update(map[string]interface{}{"status": 0}).Error
  6963. return err
  6964. }
  6965. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6966. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6967. return err
  6968. }
  6969. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6970. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6971. return err
  6972. }
  6973. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  6974. info := models.DrugWarehouseInfo{}
  6975. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  6976. return info, err
  6977. }
  6978. func GetSelfDrugInfoSix(id int64, orgid int64) (models.XtSelfDrugWarehouseInfo, error) {
  6979. info := models.XtSelfDrugWarehouseInfo{}
  6980. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  6981. return info, err
  6982. }
  6983. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  6984. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  6985. if storehouse_id > 0 {
  6986. db = db.Where("storehouse_id = ?", storehouse_id)
  6987. }
  6988. if orgid > 0 {
  6989. db = db.Where("user_org_id = ?", orgid)
  6990. }
  6991. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  6992. return storeinventory, err
  6993. }
  6994. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  6995. detail := models.AutomaticReduceDetail{}
  6996. err := XTReadDB().Where("org_id =? and patient_id = ? and record_time = ? and good_id = ? and status= 1", orgid, patient_id, sys_record_time, good_id).Find(&detail).Error
  6997. return detail, err
  6998. }
  6999. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  7000. detail := models.WarehouseOutInfo{}
  7001. err := XTReadDB().Where("org_id =? and patient_id = ? and sys_record_time = ? and good_id = ? and status= 1", orgid, patient_id, sys_record_time, good_id).Find(&detail).Error
  7002. return detail, err
  7003. }
  7004. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  7005. stock := models.XtGoodStockCount{}
  7006. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  7007. return stock, err
  7008. }
  7009. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  7010. err := XTWriteDB().Create(&stock).Error
  7011. return err
  7012. }
  7013. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7014. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  7015. return err
  7016. }
  7017. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7018. ut := XTWriteDB().Begin()
  7019. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error
  7020. if err != nil {
  7021. ut.Rollback()
  7022. return err
  7023. }
  7024. ut.Commit()
  7025. return err
  7026. }
  7027. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  7028. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7029. return err
  7030. }
  7031. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  7032. err := tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7033. if err != nil {
  7034. tx.Rollback()
  7035. return err
  7036. }
  7037. return err
  7038. }
  7039. func CreateDrugStock(stock models.XtDrugStockCount) error {
  7040. err := XTWriteDB().Create(&stock).Error
  7041. return err
  7042. }
  7043. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  7044. ut := XTWriteDB().Begin()
  7045. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7046. if err != nil {
  7047. ut.Rollback()
  7048. return err
  7049. }
  7050. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7051. if err != nil {
  7052. ut.Rollback()
  7053. return err
  7054. }
  7055. ut.Commit()
  7056. return err
  7057. }
  7058. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  7059. ut := XTWriteDB().Begin()
  7060. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7061. if err != nil {
  7062. ut.Rollback()
  7063. return err
  7064. }
  7065. ut.Commit()
  7066. return err
  7067. }
  7068. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  7069. ut := XTWriteDB().Begin()
  7070. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", sum_out_count)).Error
  7071. if err != nil {
  7072. ut.Rollback()
  7073. return err
  7074. }
  7075. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", sum_out_count)).Error
  7076. if err != nil {
  7077. ut.Rollback()
  7078. return err
  7079. }
  7080. ut.Commit()
  7081. return err
  7082. }
  7083. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  7084. ut := XTWriteDB().Begin()
  7085. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", sum_out_count)).Error
  7086. if err != nil {
  7087. ut.Rollback()
  7088. return err
  7089. }
  7090. ut.Commit()
  7091. return err
  7092. }
  7093. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7094. ut := XTWriteDB().Begin()
  7095. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error
  7096. if err != nil {
  7097. ut.Rollback()
  7098. return err
  7099. }
  7100. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", count)).Error
  7101. if err != nil {
  7102. ut.Rollback()
  7103. return err
  7104. }
  7105. ut.Commit()
  7106. return err
  7107. }
  7108. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7109. ut := XTWriteDB().Begin()
  7110. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error
  7111. if err != nil {
  7112. ut.Rollback()
  7113. return err
  7114. }
  7115. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count - ?", count)).Error
  7116. if err != nil {
  7117. ut.Rollback()
  7118. return err
  7119. }
  7120. ut.Commit()
  7121. return err
  7122. }
  7123. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  7124. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  7125. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ?", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  7126. return info, err
  7127. } else {
  7128. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ? and stock_count >0", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  7129. return info, err
  7130. }
  7131. }
  7132. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  7133. config := models.HisSettleStockConfig{}
  7134. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7135. if err == gorm.ErrRecordNotFound {
  7136. return nil, err
  7137. }
  7138. if err != nil {
  7139. return nil, err
  7140. }
  7141. return &config, nil
  7142. }
  7143. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  7144. err := XTWriteDB().Create(&config).Error
  7145. return err
  7146. }
  7147. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  7148. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  7149. return err
  7150. }
  7151. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  7152. config := models.DrugStockConfig{}
  7153. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7154. return config, err
  7155. }
  7156. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  7157. stockConfig := models.PharmacyConfig{}
  7158. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7159. return stockConfig, err
  7160. }
  7161. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  7162. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7163. return err
  7164. }
  7165. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  7166. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7167. return err
  7168. }
  7169. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  7170. config := models.HisSettleStockConfig{}
  7171. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7172. return config, nil
  7173. }
  7174. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  7175. info := models.GoodInformationFourty{}
  7176. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  7177. return info, err
  7178. }
  7179. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  7180. advice := models.HisDoctorAdvice{}
  7181. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7182. return advice, err
  7183. }
  7184. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  7185. advice := models.DoctorAdvice{}
  7186. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7187. return advice, err
  7188. }
  7189. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  7190. out := models.WarehouseOut{}
  7191. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys = ?", org_id, warehouse_out_time, is_sys).Find(&out).Error
  7192. return out, err
  7193. }
  7194. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  7195. config := models.XtGoodOutConfig{}
  7196. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7197. if err == gorm.ErrRecordNotFound {
  7198. return nil, err
  7199. }
  7200. if err != nil {
  7201. return nil, err
  7202. }
  7203. return &config, nil
  7204. }
  7205. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  7206. err := XTWriteDB().Create(&good).Error
  7207. return err
  7208. }
  7209. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  7210. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  7211. return err
  7212. }
  7213. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  7214. config := models.XtGoodOutConfig{}
  7215. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7216. return config, err
  7217. }
  7218. func UpdateGoodAutoConfig(user_org_id int64) error {
  7219. gobalConfig := models.GobalConfig{}
  7220. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7221. return err
  7222. }
  7223. func UpdateGoodOutConfig(user_org_id int64) error {
  7224. config := models.XtGoodOutConfig{}
  7225. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7226. return err
  7227. }
  7228. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  7229. err = XTReadDB().Where("patient_id = ? and record_date = ? and status = 1 and user_org_id = ? AND type = 3", patient_id, record_date, orgid).Find(&info).Error
  7230. return info, err
  7231. }
  7232. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  7233. err = XTReadDB().Where("good_id = ? and patient_id = ? and sys_record_time = ? and project_id = ? and status = 1", good_id, patient_id, sys_record_time, project_id).Find(&good).Error
  7234. return good, err
  7235. }
  7236. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  7237. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", warehouse_info_id, orgid, good_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7238. return err
  7239. }
  7240. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7241. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("good_id = ? and sys_record_time = ? and org_id = ? and project_id = ? and status=1", good_id, sys_record_time, org_id, project_id).Updates(map[string]interface{}{"status": 0}).Error
  7242. return err
  7243. }
  7244. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7245. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("good_id = ? and system_time = ? and user_org_id = ? and project_id = ? and status=1", good_id, sys_record_time, org_id, project_id).Updates(map[string]interface{}{"status": 0, "is_read": 1}).Error
  7246. return err
  7247. }
  7248. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7249. out := models.WarehouseOut{}
  7250. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Find(&out).Error
  7251. return out, err
  7252. }
  7253. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7254. out := models.WarehouseOut{}
  7255. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Find(&out).Error
  7256. if err != gorm.ErrRecordNotFound {
  7257. if err != nil {
  7258. tx.Rollback()
  7259. return out, err
  7260. }
  7261. }
  7262. return out, err
  7263. }
  7264. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7265. out := models.WarehouseOut{}
  7266. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Last(&out).Error
  7267. return out, err
  7268. }
  7269. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7270. out := models.WarehouseOut{}
  7271. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Last(&out).Error
  7272. if err != gorm.ErrRecordNotFound {
  7273. if err != nil {
  7274. tx.Rollback()
  7275. return out, err
  7276. }
  7277. }
  7278. return out, err
  7279. }
  7280. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  7281. goodsType := models.GoodsType{}
  7282. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  7283. return goodsType, err
  7284. }
  7285. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  7286. goodsTypeList, _ := GetAllGoodType(org_id)
  7287. for _, item := range goodsTypeList {
  7288. if good_type_id == item.ID {
  7289. good_type_name = item.TypeName
  7290. }
  7291. }
  7292. return good_type_name
  7293. }
  7294. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  7295. information := models.GoodInformation{}
  7296. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  7297. return information, err
  7298. }
  7299. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  7300. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  7301. return info, err
  7302. }
  7303. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  7304. ut := writeDb.Begin()
  7305. err := ut.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  7306. if err != nil {
  7307. ut.Rollback()
  7308. return err
  7309. }
  7310. ut.Commit()
  7311. return err
  7312. }
  7313. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  7314. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  7315. if err != nil {
  7316. tx.Rollback()
  7317. return err
  7318. }
  7319. return err
  7320. }
  7321. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  7322. info := models.WarehouseOutInfo{}
  7323. var err error
  7324. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7325. if err == gorm.ErrRecordNotFound {
  7326. return nil, err
  7327. }
  7328. if err != nil {
  7329. return nil, err
  7330. }
  7331. return &info, nil
  7332. }
  7333. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  7334. info := models.WarehouseOutInfo{}
  7335. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7336. return info, err
  7337. }
  7338. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7339. info := models.WarehouseOutInfo{}
  7340. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7341. if err != gorm.ErrRecordNotFound {
  7342. if err != nil {
  7343. tx.Rollback()
  7344. return info, err
  7345. }
  7346. }
  7347. return info, err
  7348. }
  7349. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  7350. info := models.WarehouseOutInfo{}
  7351. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7352. return info, err
  7353. }
  7354. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7355. info := models.WarehouseOutInfo{}
  7356. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7357. if err != gorm.ErrRecordNotFound {
  7358. if err != nil {
  7359. tx.Rollback()
  7360. return info, err
  7361. }
  7362. }
  7363. return info, err
  7364. }
  7365. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  7366. info := models.WarehouseOutInfo{}
  7367. var err error
  7368. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7369. if err == gorm.ErrRecordNotFound {
  7370. return nil, err
  7371. }
  7372. if err != nil {
  7373. return nil, err
  7374. }
  7375. return &info, nil
  7376. }
  7377. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  7378. info := models.WarehouseOutInfo{}
  7379. var err error
  7380. err = tx.Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7381. if err != gorm.ErrRecordNotFound {
  7382. if err != nil {
  7383. tx.Rollback()
  7384. return &info, err
  7385. }
  7386. }
  7387. return &info, err
  7388. }
  7389. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  7390. db := XTReadDB().Model(&info).Where("status = 1")
  7391. if startime > 0 {
  7392. db = db.Where("ctime>=?", startime)
  7393. }
  7394. if endtime > 0 {
  7395. db = db.Where("ctime<=?", endtime)
  7396. }
  7397. if orgid > 0 {
  7398. db = db.Where("org_id = ?", orgid)
  7399. }
  7400. if len(good_id) > 0 {
  7401. db = db.Where("good_id in(?)", good_id)
  7402. }
  7403. if storehouse_id > 0 {
  7404. db = db.Where("storehouse_id = ?", storehouse_id)
  7405. }
  7406. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  7407. return info, err
  7408. }
  7409. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  7410. db := XTReadDB().Model(&info).Where("status= 1")
  7411. if start_time > 0 {
  7412. db = db.Where("ctime>=?", start_time)
  7413. }
  7414. if end_time > 0 {
  7415. db = db.Where("ctime <=?", end_time)
  7416. }
  7417. if orgid > 0 {
  7418. db = db.Where("org_id = ?", orgid)
  7419. }
  7420. if len(good_id) > 0 {
  7421. db = db.Where("good_id in(?)", good_id)
  7422. }
  7423. if storehouse_id > 0 {
  7424. db = db.Where("storehouse_id = ?", storehouse_id)
  7425. }
  7426. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  7427. return info, err
  7428. }
  7429. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  7430. config := models.XtGoodOutConfig{}
  7431. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7432. return config, err
  7433. }
  7434. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  7435. err := XTWriteDB().Model(&models.AutomaticReduceDetail{}).Where("good_id = ? and record_time = ? and patient_id = ?", good_id, record_date, patient_id).Updates(map[string]interface{}{"status": 1}).Error
  7436. err = XTWriteDB().Model(&models.DialysisBeforePrepare{}).Where("good_id = ? and record_date = ? and patient_id = ?", good_id, record_date, patient_id).Updates(map[string]interface{}{"status": 1}).Error
  7437. return err
  7438. }
  7439. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  7440. tx := XTWriteDB().Begin()
  7441. for _, item := range list {
  7442. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  7443. if err != nil {
  7444. tx.Rollback()
  7445. return err
  7446. }
  7447. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  7448. if err != nil {
  7449. tx.Rollback()
  7450. return err
  7451. }
  7452. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7453. if err != nil {
  7454. tx.Rollback()
  7455. return err
  7456. }
  7457. var goodList []*models.WarehousingInfo
  7458. err = tx.Where("org_id = ? and storehouse_id = ? and good_id = ? and status =1 and is_check = 1", orgId, item.StorehouseId, item.GoodId).Find(&goodList).Error
  7459. var flush_count int64
  7460. for _, it := range goodList {
  7461. flush_count += it.StockCount
  7462. }
  7463. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", item.WarehousingCount)).Error
  7464. if err != nil {
  7465. tx.Rollback()
  7466. return err
  7467. }
  7468. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7469. if err != nil {
  7470. tx.Rollback()
  7471. return err
  7472. }
  7473. storeHouseConfig := models.XtStorehouseConfig{}
  7474. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  7475. var stockList []*models.WarehousingInfo
  7476. //查询该耗材剩余多少库存
  7477. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId).Find(&stockList).Error
  7478. var sum_count int64
  7479. var sum_in_count int64
  7480. for _, it := range stockList {
  7481. sum_count += it.StockCount
  7482. sum_in_count += it.WarehousingCount
  7483. }
  7484. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7485. if err != nil {
  7486. tx.Rollback()
  7487. return err
  7488. }
  7489. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7490. if err != nil {
  7491. tx.Rollback()
  7492. return err
  7493. }
  7494. }
  7495. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7496. if err != nil {
  7497. tx.Rollback()
  7498. return err
  7499. }
  7500. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7501. tx.Commit()
  7502. return err
  7503. }
  7504. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  7505. if storehouse_id == 0 {
  7506. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  7507. }
  7508. if storehouse_id > 0 {
  7509. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").First(&info).Error
  7510. }
  7511. return info, err
  7512. }
  7513. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  7514. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  7515. return info, err
  7516. }
  7517. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  7518. err = readDb.Where("project_id = ? and record_date =? and user_org_id =? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Find(&list).Error
  7519. return list, err
  7520. }
  7521. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  7522. err = readDb.Where("good_id =? and sys_record_time = ? and org_id = ? and patient_id = ? and status=1", project_id, record_date, orgid, patient_id).Find(&infor).Error
  7523. return infor, err
  7524. }
  7525. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  7526. info := models.WarehouseOutInfo{}
  7527. err := XTWriteDB().Model(&info).Where("good_id = ? and sys_record_time = ? and org_id = ? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Updates(map[string]interface{}{"status": 0}).Error
  7528. return info, err
  7529. }
  7530. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  7531. info := models.WarehousingInfo{}
  7532. err := XTWriteDB().Where("id = ? and good_id = ? and org_id = ? and status= 1", id, good_id, user_org_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7533. return info, err
  7534. }
  7535. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7536. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7537. return list, err
  7538. }
  7539. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7540. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7541. return list, err
  7542. }
  7543. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  7544. templateL := models.HisPrescriptionProjectTemplateL{}
  7545. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  7546. return templateL, err
  7547. }
  7548. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  7549. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", id, orgid, goodid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7550. if err != nil {
  7551. tx.Rollback()
  7552. return err
  7553. }
  7554. return err
  7555. }
  7556. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  7557. err = XTReadDB().Where("org_id = ? and drug_id = ? and status=1", user_org_id, drug_id).Preload("XtBaseDrug", "org_id = ? and status=1", user_org_id).Order("id desc").Find(&info).Error
  7558. return info, err
  7559. }
  7560. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  7561. err := XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"stock_max_number": last_stock_max_number, "stock_min_number": last_stock_min_number}).Error
  7562. return err
  7563. }
  7564. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  7565. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  7566. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  7567. return drugInfoConfig, err
  7568. }
  7569. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  7570. err := XTWriteDB().Create(&infoConfig).Error
  7571. return err
  7572. }
  7573. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  7574. err := XTWriteDB().Save(&infoConfig).Error
  7575. return err
  7576. }
  7577. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  7578. drugOutConfig := models.XtSelfDrugOutConfig{}
  7579. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  7580. return drugOutConfig, err
  7581. }
  7582. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  7583. err := XTWriteDB().Create(&config).Error
  7584. return err
  7585. }
  7586. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  7587. err := XTWriteDB().Save(&config).Error
  7588. return err
  7589. }
  7590. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  7591. err := tx.Create(&flow).Error
  7592. if err != nil {
  7593. tx.Rollback()
  7594. return err
  7595. }
  7596. return err
  7597. }
  7598. func UpdateWarehouseOutInfoByPrice(good_id int64, packing_price float64) error {
  7599. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("good_id =? and status =1 and is_check=1", good_id).Updates(map[string]interface{}{"packing_price": packing_price}).Error
  7600. return err
  7601. }
  7602. func GetWarehosueOutInfoByPrice(good_id int64, org_id int64) (out []*models.WarehouseOutInfo, err error) {
  7603. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and price =0", good_id, org_id).Find(&out).Error
  7604. return out, err
  7605. }
  7606. func UpdateWarehouseOut(good_id int64, packing_price float64) error {
  7607. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  7608. return err
  7609. }
  7610. func UpdateStockFlowByList(good_id int64, packing_price float64) error {
  7611. err := XTWriteDB().Model(models.VmStockFlow{}).Where("good_id = ? and user_org_id = 10633 and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  7612. return err
  7613. }
  7614. func GetGoodStockInventoryByRecordate(good_id int64, record_date int64, user_org_id int64) (models.XtGoodStockInventoryList, error) {
  7615. goodStockInventoryList := models.XtGoodStockInventoryList{}
  7616. err := XTReadDB().Where("good_id = ? and record_date = ? and user_org_id = ? and status=1", good_id, record_date, user_org_id).Find(&goodStockInventoryList).Error
  7617. return goodStockInventoryList, err
  7618. }
  7619. func CreateGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  7620. err := XTWriteDB().Create(&list).Error
  7621. return err
  7622. }
  7623. func SaveGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  7624. err := XTWriteDB().Save(&list).Error
  7625. return err
  7626. }
  7627. func GetGoodStockInventoryList(keyword string, limit int64, page int64, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  7628. likeKey := "%" + keyword + "%"
  7629. offset := (page - 1) * limit
  7630. db := XTReadDB().Model(&models.XtGoodStockInventoryList{}).Where("status=1")
  7631. if storehouse_id > 0 {
  7632. db = db.Where("storehouse_id = ?", storehouse_id)
  7633. }
  7634. if startTime > 0 {
  7635. db = db.Where("record_date >=?", startTime)
  7636. }
  7637. if endTime > 0 {
  7638. db = db.Where("record_date<=?", endTime)
  7639. }
  7640. if len(keyword) > 0 {
  7641. db = db.Where("good_name like ? or specification_name like ?", likeKey, likeKey)
  7642. }
  7643. if orgId > 0 {
  7644. db = db.Where("user_org_id = ?", orgId)
  7645. }
  7646. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  7647. return list, total, err
  7648. }
  7649. func GetDrugStockInventoryList(drug_id int64, record_date int64, user_org_id int64) (models.XtDrugStockInventoryList, error) {
  7650. drugStockInventoryList := models.XtDrugStockInventoryList{}
  7651. err := XTReadDB().Where("drug_id = ? and record_date = ? and user_org_id = ? and status=1", drug_id, record_date, user_org_id).Find(&drugStockInventoryList).Error
  7652. return drugStockInventoryList, err
  7653. }
  7654. func CreatedDrugStockInventory(list models.XtDrugStockInventoryList) error {
  7655. err := XTWriteDB().Create(&list).Error
  7656. return err
  7657. }
  7658. func SaveDrugStockInventory(list models.XtDrugStockInventoryList) error {
  7659. err := XTWriteDB().Save(&list).Error
  7660. return err
  7661. }
  7662. func GetStockInventoryPrintList(storehouse_id int64, keyword string, limit int64, page int64, ids []string, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  7663. db := XTReadDB().Model(&list).Where("status=1")
  7664. likeKey := "%" + keyword + "%"
  7665. offset := (page - 1) * limit
  7666. if storehouse_id > 0 {
  7667. db = db.Where("storehouse_id = ?", storehouse_id)
  7668. }
  7669. if len(keyword) > 0 {
  7670. db = db.Where("good_name like ?", likeKey)
  7671. }
  7672. if orgId > 0 {
  7673. db = db.Where("user_org_id = ?", orgId)
  7674. }
  7675. if len(ids) > 0 {
  7676. db = db.Where("id in(?)", ids)
  7677. }
  7678. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  7679. return list, total, err
  7680. }
  7681. func GetGoodWarehouseInfoList(startTime int64, endTime int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  7682. db := XTReadDB().Model(&info).Where("status=1 and is_check=1")
  7683. if startTime > 0 {
  7684. db = db.Where("ctime>=?", startTime)
  7685. }
  7686. if endTime > 0 {
  7687. db = db.Where("ctime<=?", endTime)
  7688. }
  7689. if orgId > 0 {
  7690. db = db.Where("org_id = ?", orgId)
  7691. }
  7692. err = db.Find(&info).Error
  7693. return info, err
  7694. }
  7695. func GetGoodWarhouseOutList(startTime int64, endTime int64, orgId int64) (outinfo []*models.WarehouseOutInfo, err error) {
  7696. db := XTReadDB().Model(&outinfo).Where("status=1 and is_check =1")
  7697. if startTime > 0 {
  7698. db = db.Where("ctime>=?", startTime)
  7699. }
  7700. if endTime > 0 {
  7701. db = db.Where("ctime<=?", endTime)
  7702. }
  7703. if orgId > 0 {
  7704. db = db.Where("org_id = ?", orgId)
  7705. }
  7706. err = db.Find(&outinfo).Error
  7707. return outinfo, err
  7708. }
  7709. func GetGoodStockFlowProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7710. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 10")
  7711. if startTime > 0 {
  7712. db = db.Where("ctime>=?", startTime)
  7713. }
  7714. if endTime > 0 {
  7715. db = db.Where("ctime<=?", endTime)
  7716. }
  7717. if orgId > 0 {
  7718. db = db.Where("user_org_id = ?", orgId)
  7719. }
  7720. err = db.Find(&flow).Error
  7721. return flow, err
  7722. }
  7723. func GetGoodStockFlowNoProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7724. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 11")
  7725. if startTime > 0 {
  7726. db = db.Where("ctime>=?", startTime)
  7727. }
  7728. if endTime > 0 {
  7729. db = db.Where("ctime<=?", endTime)
  7730. }
  7731. if orgId > 0 {
  7732. db = db.Where("user_org_id = ?", orgId)
  7733. }
  7734. err = db.Find(&flow).Error
  7735. return flow, err
  7736. }
  7737. func GetCancelStockFlowList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7738. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 7")
  7739. if startTime > 0 {
  7740. db = db.Where("ctime>=?", startTime)
  7741. }
  7742. if endTime > 0 {
  7743. db = db.Where("ctime<=?", endTime)
  7744. }
  7745. if orgId > 0 {
  7746. db = db.Where("user_org_id = ?", orgId)
  7747. }
  7748. err = db.Find(&flow).Error
  7749. return flow, err
  7750. }
  7751. func GetLastMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7752. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 1 or consumable_type = 10)")
  7753. if startTime > 0 {
  7754. db = db.Where("ctime <?", startTime)
  7755. }
  7756. if orgId > 0 {
  7757. db = db.Where("user_org_id = ?", orgId)
  7758. }
  7759. err = db.Find(&flow).Error
  7760. return flow, err
  7761. }
  7762. func GetLastMonthOverCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7763. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 2 or consumable_type=3 or consumable_type=11)")
  7764. if startTime > 0 {
  7765. db = db.Where("ctime <?", startTime)
  7766. }
  7767. if orgId > 0 {
  7768. db = db.Where("user_org_id = ?", orgId)
  7769. }
  7770. err = db.Find(&flow).Error
  7771. return flow, err
  7772. }
  7773. func GetLastCancelMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7774. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 7)")
  7775. if startTime > 0 {
  7776. db = db.Where("ctime <?", startTime)
  7777. }
  7778. if orgId > 0 {
  7779. db = db.Where("user_org_id = ?", orgId)
  7780. }
  7781. err = db.Find(&flow).Error
  7782. return flow, err
  7783. }
  7784. func GetDrugCodeOpen(user_org_id int64) (models.XtDrugCodeOpen, error) {
  7785. drugCodeOpen := models.XtDrugCodeOpen{}
  7786. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  7787. return drugCodeOpen, err
  7788. }
  7789. func CreateDrugOpen(open models.XtDrugCodeOpen) error {
  7790. err := XTWriteDB().Create(&open).Error
  7791. return err
  7792. }
  7793. func SaveDrugOpen(open models.XtDrugCodeOpen) error {
  7794. err := XTWriteDB().Save(&open).Error
  7795. return err
  7796. }
  7797. func GetHisDrugCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64) (list []*models.HisDoctorAdviceInfo, total int64, err error) {
  7798. db := XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("status=1")
  7799. if orgId > 0 {
  7800. db = db.Where("user_org_id=?", orgId)
  7801. }
  7802. if start_time > 0 {
  7803. db = db.Where("advice_date >= ?", start_time)
  7804. }
  7805. if end_time > 0 {
  7806. db = db.Where("advice_date<=?", end_time)
  7807. }
  7808. offset := (page - 1) * limit
  7809. if is_sale == 1 {
  7810. db = db.Where("is_upload = 1")
  7811. }
  7812. if is_sale == 2 {
  7813. db = db.Where("is_upload = 2 or is_upload =0")
  7814. }
  7815. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  7816. fmt.Println("totla====================", total)
  7817. return list, total, err
  7818. }
  7819. func GetDrugWarehouseOutInfoByDrugCode(drug_id int64, patient_id int64, advice_date int64, user_org_id int64) (outinfo []*models.XtDrugWarehouseOutInfo, err error) {
  7820. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time =? and org_id = ? and status =1", drug_id, patient_id, advice_date, user_org_id).Find(&outinfo).Error
  7821. return outinfo, err
  7822. }