stock_service.go 390KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157
  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. }).Error
  247. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  248. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  249. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. return err, goodInfo
  254. }
  255. 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) {
  256. offset := (page - 1) * limit
  257. db := readDb.Model(&models.GoodInfo{})
  258. db = db.Where("org_id = ? AND status = 1", orgId)
  259. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  260. if len(keyword) > 0 {
  261. likeKey := "%" + keyword + "%"
  262. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  263. if is_use > 0 {
  264. if is_use == 2 {
  265. db = db.Where("find_in_set('停用',good_status) = 0")
  266. } else {
  267. db = db.Where("find_in_set('停用',good_status) > 0")
  268. }
  269. }
  270. if is_charge > 0 {
  271. if is_charge == 1 {
  272. db = db.Where("find_in_set('收费',good_status) > 0")
  273. } else {
  274. db = db.Where("find_in_set('收费',good_status) = 0")
  275. }
  276. }
  277. if good_kind > 0 {
  278. db = db.Where(" good_kind = ?", good_kind)
  279. }
  280. if is_mark > 0 {
  281. db = db.Where("is_mark = ?", is_mark)
  282. }
  283. if manufacturer > 0 {
  284. db = db.Where("manufacturer = ?", manufacturer)
  285. }
  286. if is_type > 0 {
  287. db = db.Where("good_type_id = ?", is_type)
  288. }
  289. } else {
  290. if is_use > 0 {
  291. if is_use == 2 {
  292. db = db.Where("find_in_set('停用',good_status) = 0")
  293. } else {
  294. db = db.Where("find_in_set('停用',good_status) > 0")
  295. }
  296. }
  297. if is_charge > 0 {
  298. if is_charge == 1 {
  299. db = db.Where("find_in_set('收费',good_status) > 0")
  300. } else {
  301. db = db.Where("find_in_set('收费',good_status) = 0")
  302. }
  303. }
  304. if good_kind > 0 {
  305. db = db.Where(" good_kind = ?", good_kind)
  306. }
  307. if is_mark > 0 {
  308. db = db.Where("is_mark = ?", is_mark)
  309. }
  310. if manufacturer > 0 {
  311. db = db.Where("manufacturer = ?", manufacturer)
  312. }
  313. if is_type > 0 {
  314. db = db.Where("good_type_id = ?", is_type)
  315. }
  316. }
  317. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  318. return
  319. }
  320. func DeleteGoodInfoById(id int64, operateId int64) error {
  321. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  322. return err
  323. }
  324. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  325. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  326. return list, err
  327. }
  328. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  329. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  330. return list, err
  331. }
  332. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  333. goodInfo := &models.GoodInfo{}
  334. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  335. return goodInfo, err
  336. }
  337. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  338. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  339. return goodType, err
  340. }
  341. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  342. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  343. return goodType, err
  344. }
  345. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  346. 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
  347. return goodInfo, err
  348. }
  349. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  350. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  351. return goodInfo, err
  352. }
  353. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  354. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  355. return total, err
  356. }
  357. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  358. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  359. return total, err
  360. }
  361. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  362. err := writeDb.Create(&warehouse).Error
  363. return err
  364. }
  365. func UpdateWarehouse(id int64, storehouse_id int64) error {
  366. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  367. return err
  368. }
  369. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  370. err := writeDb.Create(&warehouse).Error
  371. return err
  372. }
  373. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  374. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  375. return err
  376. }
  377. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  378. err := writeDb.Create(&salesReturn).Error
  379. return err
  380. }
  381. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  382. err := writeDb.Create(&salesReturn).Error
  383. return err
  384. }
  385. func UpDateGoodReturnStatus(ids []string) {
  386. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  387. }
  388. func UpDateCanCelStockStatus(ids []string) {
  389. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  390. }
  391. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  392. if len(warehousingInfo) > 0 {
  393. utx := writeDb.Begin()
  394. if len(warehousingInfo) > 0 {
  395. 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) VALUES "
  396. insertParams := make([]string, 0)
  397. insertData := make([]interface{}, 0)
  398. for _, info := range warehousingInfo {
  399. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  400. insertData = append(insertData, info.WarehousingId)
  401. insertData = append(insertData, info.GoodId)
  402. insertData = append(insertData, info.GoodTypeId)
  403. insertData = append(insertData, info.Number)
  404. insertData = append(insertData, info.ProductDate)
  405. insertData = append(insertData, info.ExpiryDate)
  406. insertData = append(insertData, info.WarehousingCount)
  407. insertData = append(insertData, info.Price)
  408. insertData = append(insertData, info.TotalPrice)
  409. insertData = append(insertData, info.Dealer)
  410. insertData = append(insertData, info.Manufacturer)
  411. insertData = append(insertData, info.Remark)
  412. insertData = append(insertData, info.Ctime)
  413. insertData = append(insertData, info.Mtime)
  414. insertData = append(insertData, info.Status)
  415. insertData = append(insertData, info.OrgId)
  416. insertData = append(insertData, info.WarehousingOrder)
  417. insertData = append(insertData, info.Type)
  418. insertData = append(insertData, info.WarehousingCount)
  419. insertData = append(insertData, info.LicenseNumber)
  420. insertData = append(insertData, info.PackingPrice)
  421. insertData = append(insertData, info.StorehouseId)
  422. }
  423. thisSQL += strings.Join(insertParams, ", ")
  424. err = utx.Exec(thisSQL, insertData...).Error
  425. if err != nil {
  426. utx.Rollback()
  427. return
  428. }
  429. }
  430. utx.Commit()
  431. }
  432. return
  433. }
  434. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  435. if len(warehousingInfo) > 0 {
  436. utx := writeDb.Begin()
  437. if len(warehousingInfo) > 0 {
  438. 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 "
  439. insertParams := make([]string, 0)
  440. insertData := make([]interface{}, 0)
  441. for _, info := range warehousingInfo {
  442. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  443. insertData = append(insertData, info.WarehousingId)
  444. insertData = append(insertData, info.DrugId)
  445. insertData = append(insertData, info.Number)
  446. insertData = append(insertData, info.ProductDate)
  447. insertData = append(insertData, info.ExpiryDate)
  448. insertData = append(insertData, info.WarehousingCount)
  449. insertData = append(insertData, info.Price)
  450. insertData = append(insertData, info.TotalPrice)
  451. insertData = append(insertData, info.Dealer)
  452. insertData = append(insertData, info.Manufacturer)
  453. insertData = append(insertData, info.Remark)
  454. insertData = append(insertData, info.Ctime)
  455. insertData = append(insertData, info.Mtime)
  456. insertData = append(insertData, info.Status)
  457. insertData = append(insertData, info.OrgId)
  458. insertData = append(insertData, info.WarehousingOrder)
  459. insertData = append(insertData, info.Type)
  460. insertData = append(insertData, info.RetailPrice)
  461. insertData = append(insertData, info.RetailTotalPrice)
  462. insertData = append(insertData, info.StockMaxNumber)
  463. insertData = append(insertData, info.MaxUnit)
  464. insertData = append(insertData, info.MinUnit)
  465. insertData = append(insertData, info.BatchNumber)
  466. insertData = append(insertData, info.StockMinNumber)
  467. insertData = append(insertData, info.StorehouseId)
  468. }
  469. thisSQL += strings.Join(insertParams, ", ")
  470. err = utx.Exec(thisSQL, insertData...).Error
  471. if err != nil {
  472. utx.Rollback()
  473. return
  474. }
  475. }
  476. utx.Commit()
  477. }
  478. return
  479. }
  480. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  481. if len(warehousingInfo) > 0 {
  482. utx := writeDb.Begin()
  483. if len(warehousingInfo) > 0 {
  484. 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 "
  485. insertParams := make([]string, 0)
  486. insertData := make([]interface{}, 0)
  487. for _, info := range warehousingInfo {
  488. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  489. insertData = append(insertData, info.WarehousingId)
  490. insertData = append(insertData, info.DrugId)
  491. insertData = append(insertData, info.Number)
  492. insertData = append(insertData, info.ProductDate)
  493. insertData = append(insertData, info.ExpiryDate)
  494. insertData = append(insertData, info.WarehousingCount)
  495. insertData = append(insertData, info.Price)
  496. insertData = append(insertData, info.TotalPrice)
  497. insertData = append(insertData, info.Dealer)
  498. insertData = append(insertData, info.Manufacturer)
  499. insertData = append(insertData, info.Remark)
  500. insertData = append(insertData, info.Ctime)
  501. insertData = append(insertData, info.Mtime)
  502. insertData = append(insertData, info.Status)
  503. insertData = append(insertData, info.OrgId)
  504. insertData = append(insertData, info.WarehousingOrder)
  505. insertData = append(insertData, info.Type)
  506. insertData = append(insertData, info.RetailPrice)
  507. insertData = append(insertData, info.RetailTotalPrice)
  508. insertData = append(insertData, info.StockMaxNumber)
  509. insertData = append(insertData, info.MaxUnit)
  510. insertData = append(insertData, info.MinUnit)
  511. insertData = append(insertData, info.BatchNumber)
  512. insertData = append(insertData, info.StockMinNumber)
  513. insertData = append(insertData, info.SupplyWarehouseId)
  514. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  515. insertData = append(insertData, info.StorehouseId)
  516. insertData = append(insertData, info.IsCheck)
  517. }
  518. thisSQL += strings.Join(insertParams, ", ")
  519. err = utx.Exec(thisSQL, insertData...).Error
  520. if err != nil {
  521. utx.Rollback()
  522. return
  523. }
  524. }
  525. utx.Commit()
  526. }
  527. return
  528. }
  529. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  530. var total float64
  531. var min_number int64
  532. for _, item := range warehousingInfo {
  533. //查询刚入库耗材的库存
  534. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  535. //更新药品库的总量
  536. info, _ := FindeDrugInfo(item.DrugId)
  537. min_number = count.StockMaxNumber * info.MinNumber
  538. str := strconv.FormatInt(min_number, 10)
  539. minFloat, _ := strconv.ParseFloat(str, 64)
  540. total = info.Total + minFloat
  541. lib := models.BaseDrugLib{
  542. Total: total,
  543. }
  544. UpdateBaseDrug(&lib, item.ID)
  545. }
  546. return err
  547. }
  548. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  549. info := models.XtDrugWarehouseInfo{}
  550. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  551. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  552. return info, err
  553. }
  554. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  555. lib := models.BaseDrugLib{}
  556. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  557. return lib, err
  558. }
  559. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  560. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  561. return err
  562. }
  563. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  564. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  565. return err
  566. }
  567. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  568. info := models.DrugWarehouseInfo{}
  569. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  570. return info, err
  571. }
  572. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  573. out := models.DrugWarehouseOut{}
  574. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  575. return out, err
  576. }
  577. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  578. if len(drugflow) > 0 {
  579. utx := writeDb.Begin()
  580. if len(drugflow) > 0 {
  581. 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 "
  582. insertParams := make([]string, 0)
  583. insertData := make([]interface{}, 0)
  584. for _, info := range drugflow {
  585. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  586. insertData = append(insertData, info.WarehousingId)
  587. insertData = append(insertData, info.DrugId)
  588. insertData = append(insertData, info.Number)
  589. insertData = append(insertData, info.BatchNumber)
  590. insertData = append(insertData, info.Count)
  591. insertData = append(insertData, info.UserOrgId)
  592. insertData = append(insertData, info.PatientId)
  593. insertData = append(insertData, info.SystemTime)
  594. insertData = append(insertData, info.ConsumableType)
  595. insertData = append(insertData, info.IsSys)
  596. insertData = append(insertData, info.WarehousingOrder)
  597. insertData = append(insertData, info.WarehouseOutId)
  598. insertData = append(insertData, info.WarehouseOutOrderNumber)
  599. insertData = append(insertData, info.IsEdit)
  600. insertData = append(insertData, info.CancelStockId)
  601. insertData = append(insertData, info.CancelOrderNumber)
  602. insertData = append(insertData, info.Manufacturer)
  603. insertData = append(insertData, info.Dealer)
  604. insertData = append(insertData, info.Creator)
  605. insertData = append(insertData, info.UpdateCreator)
  606. insertData = append(insertData, info.Status)
  607. insertData = append(insertData, info.Ctime)
  608. insertData = append(insertData, info.Mtime)
  609. insertData = append(insertData, info.Price)
  610. insertData = append(insertData, info.WarehousingDetailId)
  611. insertData = append(insertData, info.WarehouseOutDetailId)
  612. insertData = append(insertData, info.CancelOutDetailId)
  613. insertData = append(insertData, info.ExpireDate)
  614. insertData = append(insertData, info.ProductDate)
  615. insertData = append(insertData, info.MaxUnit)
  616. insertData = append(insertData, info.MinUnit)
  617. insertData = append(insertData, info.StorehouseId)
  618. }
  619. thisSQL += strings.Join(insertParams, ", ")
  620. err = utx.Exec(thisSQL, insertData...).Error
  621. if err != nil {
  622. utx.Rollback()
  623. return
  624. }
  625. }
  626. utx.Commit()
  627. }
  628. return
  629. }
  630. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  631. info := models.DrugFlow{}
  632. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  633. if err == gorm.ErrRecordNotFound {
  634. return nil, err
  635. }
  636. if err != nil {
  637. return nil, err
  638. }
  639. return &info, nil
  640. }
  641. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  642. info := models.XtDrugWarehouseInfo{}
  643. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  644. return info, err
  645. }
  646. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  647. warehouseInfo := models.XtDrugWarehouseInfo{}
  648. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  649. return err
  650. }
  651. func CreateDrugFlowOne(flow models.DrugFlow) error {
  652. ut := writeDb.Begin()
  653. err := ut.Create(&flow).Error
  654. if err != nil {
  655. ut.Rollback()
  656. return err
  657. }
  658. ut.Commit()
  659. return err
  660. }
  661. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  662. err := tx.Create(&flow).Error
  663. if err != nil {
  664. tx.Rollback()
  665. return err
  666. }
  667. return err
  668. }
  669. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  670. detail := models.DrugAutomaticReduceDetail{}
  671. 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
  672. 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
  673. return detail, err
  674. }
  675. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  676. flow := models.DrugFlow{}
  677. 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
  678. return flow, err
  679. }
  680. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  681. tx := XTWriteDB().Begin()
  682. err := tx.Create(&flow).Error
  683. if err != nil {
  684. tx.Rollback()
  685. return err
  686. }
  687. tx.Commit()
  688. return err
  689. }
  690. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  691. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  692. return info, err
  693. }
  694. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  695. info := models.WarehousingInfo{}
  696. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  697. return info, err
  698. }
  699. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  700. err := XTWriteDB().Create(&info).Error
  701. return err
  702. }
  703. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  704. if storehouse_id == 0 {
  705. 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
  706. }
  707. if storehouse_id > 0 {
  708. 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
  709. }
  710. return info, err
  711. }
  712. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  713. if storehouse_id == 0 {
  714. 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
  715. if err != gorm.ErrRecordNotFound {
  716. if err != nil {
  717. tx.Rollback()
  718. return info, err
  719. }
  720. }
  721. }
  722. if storehouse_id > 0 {
  723. 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
  724. if err != gorm.ErrRecordNotFound {
  725. if err != nil {
  726. tx.Rollback()
  727. return info, err
  728. }
  729. }
  730. }
  731. return info, err
  732. }
  733. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  734. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  735. if good_id > 0 {
  736. db = db.Where("x.good_id = ?", good_id)
  737. }
  738. if good_type_id > 0 {
  739. db = db.Where("x.good_type_id = ?", good_type_id)
  740. }
  741. if storehouse_id > 0 {
  742. db = db.Where("x.storehouse_id = ?", storehouse_id)
  743. }
  744. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  745. return info, err
  746. }
  747. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  748. info := models.WarehousingInfo{}
  749. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  750. return info, err
  751. }
  752. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  753. info := models.WarehousingInfo{}
  754. var err error
  755. if storehouse_id == 0 {
  756. 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
  757. }
  758. if storehouse_id > 0 {
  759. 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
  760. }
  761. return info, err
  762. }
  763. //药品先进先出,找出最先入库的批次
  764. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  765. 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
  766. return info, err
  767. }
  768. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  769. 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
  770. if err != gorm.ErrRecordNotFound {
  771. if err != nil {
  772. tx.Rollback()
  773. return info, err
  774. }
  775. }
  776. return info, err
  777. }
  778. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  779. if is_source == 1 {
  780. 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
  781. }
  782. if is_source == 2 || is_source == 0 {
  783. 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
  784. }
  785. return info, err
  786. }
  787. // 出库时先进先出,找出最先入库的批次
  788. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  789. 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
  790. return info, err
  791. }
  792. //
  793. //// 出库时先进先出,找出最先入库的批次
  794. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  795. // 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
  796. // return info, err
  797. //}
  798. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  799. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  800. return info, err
  801. }
  802. 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) {
  803. offset := (page - 1) * limit
  804. db := readDb.Model(&models.Warehousing{})
  805. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  806. if len(keywords) > 0 {
  807. likeKey := "%" + keywords + "%"
  808. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  809. 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")
  810. }
  811. if startTime > 0 {
  812. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  813. }
  814. if endTime > 0 {
  815. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  816. }
  817. if storehouse_id > 0 {
  818. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  819. }
  820. if check_type == 0 {
  821. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  822. }
  823. if check_type > 0 {
  824. db = db.Where("xt_warehouse.is_check = ?", check_type)
  825. }
  826. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  827. return
  828. }
  829. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  830. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  831. if orgid > 0 {
  832. db = db.Where("org_id = ?", orgid)
  833. }
  834. if len(keyword) > 0 {
  835. likeKey := "%" + keyword + "%"
  836. db = db.Where("good_name like ?", likeKey)
  837. }
  838. err = db.Find(&goodInfo).Error
  839. return goodInfo, err
  840. }
  841. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  842. db := XTReadDB().Model(&info).Where("status =1")
  843. if len(good_id) > 0 {
  844. db = db.Where("good_id in (?)", good_id)
  845. }
  846. if startime > 0 {
  847. db = db.Where("ctime >=?", startime)
  848. }
  849. if endtime > 0 {
  850. db = db.Where("ctime<=?", endtime)
  851. }
  852. if orgid > 0 {
  853. db = db.Where("org_id = ?", orgid)
  854. }
  855. if storehouse_id > 0 {
  856. db = db.Where("storehouse_id = ?", storehouse_id)
  857. }
  858. err = db.Find(&info).Error
  859. return info, err
  860. }
  861. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  862. db := XTReadDB().Model(&info).Where("status =1")
  863. if len(good_id) > 0 {
  864. db = db.Where("good_id in (?)", good_id)
  865. }
  866. if startime > 0 {
  867. db = db.Where("ctime >=?", startime)
  868. }
  869. if endtime > 0 {
  870. db = db.Where("ctime<=?", endtime)
  871. }
  872. if orgid > 0 {
  873. db = db.Where("org_id = ?", orgid)
  874. }
  875. if storehouse_id > 0 {
  876. db = db.Where("storehouse_id = ?", storehouse_id)
  877. }
  878. err = db.Find(&info).Error
  879. return info, err
  880. }
  881. type VMDrugWarehouse struct {
  882. ID int64 `gorm:"column:id" json:"id" form:"id"`
  883. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  884. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  885. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  886. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  887. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  888. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  889. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  890. Status int64 `gorm:"column:status" json:"status" form:"status"`
  891. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  892. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  893. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  894. Type int64 `gorm:"column:type" json:"type" form:"type"`
  895. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  896. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  897. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  898. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  899. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  900. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  901. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  902. }
  903. func (VMDrugWarehouse) TableName() string {
  904. return "xt_drug_warehouse"
  905. }
  906. type VMDrugWarehouseInfo struct {
  907. ID int64 `gorm:"column:id" json:"id" form:"id"`
  908. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  909. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  910. Number string `gorm:"column:number" json:"number" form:"number"`
  911. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  912. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  913. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  914. Price float64 `gorm:"column:price" json:"price" form:"price"`
  915. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  916. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  917. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  918. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  919. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  920. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  921. Status int64 `gorm:"column:status" json:"status" form:"status"`
  922. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  923. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  924. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  925. Type int64 `gorm:"column:type" json:"type" form:"type"`
  926. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  927. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  928. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  929. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  930. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  931. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  932. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  933. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  934. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  935. }
  936. func (VMDrugWarehouseInfo) TableName() string {
  937. return "xt_drug_warehouse_info"
  938. }
  939. type Drug struct {
  940. ID int64 `gorm:"column:id" json:"id" form:"id"`
  941. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  942. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  943. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  944. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  945. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  946. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  947. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  948. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  949. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  950. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  951. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  952. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  953. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  954. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  955. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  956. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  957. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  958. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  959. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  960. Number string `gorm:"column:number" json:"number" form:"number"`
  961. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  962. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  963. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  964. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  965. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  966. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  967. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  968. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  969. Code string `gorm:"column:code" json:"code" form:"code"`
  970. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  971. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  972. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  973. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  974. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  975. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  976. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  977. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  978. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  979. Label int64 `gorm:"column:label" json:"label" form:"label"`
  980. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  981. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  982. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  983. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  984. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  985. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  986. Status int64 `gorm:"column:status" json:"status" form:"status"`
  987. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  988. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  989. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  990. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  991. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  992. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  993. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  994. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  995. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  996. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  997. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  998. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  999. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1000. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1001. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1002. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1003. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1004. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1005. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1006. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1007. }
  1008. func (Drug) TableName() string {
  1009. return "xt_base_drug"
  1010. }
  1011. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1012. db := readDb.Model(&VMDrugWarehouse{})
  1013. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1014. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1015. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1016. if len(keywords) > 0 {
  1017. likeKey := "%" + keywords + "%"
  1018. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1019. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1020. 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")
  1021. }
  1022. if startTime > 0 {
  1023. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1024. }
  1025. if endTime > 0 {
  1026. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1027. }
  1028. db = db.Count(&total)
  1029. offset := (page - 1) * limit
  1030. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1031. return
  1032. }
  1033. 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) {
  1034. db := readDb.Model(&VMDrugWarehouse{})
  1035. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1036. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1037. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1038. if len(keywords) > 0 {
  1039. likeKey := "%" + keywords + "%"
  1040. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1041. 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")
  1042. }
  1043. if startTime > 0 {
  1044. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1045. }
  1046. if endTime > 0 {
  1047. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1048. }
  1049. if storehouse_id > 0 {
  1050. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1051. }
  1052. if check_type > 0 {
  1053. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1054. }
  1055. db = db.Count(&total)
  1056. offset := (page - 1) * limit
  1057. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1058. return
  1059. }
  1060. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1061. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1062. return list, err
  1063. }
  1064. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1065. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1066. return list, err
  1067. }
  1068. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1069. 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
  1070. return list, err
  1071. }
  1072. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1073. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1074. return list, err
  1075. }
  1076. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1077. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1078. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1079. fmt.Println(table)
  1080. if len(id) > 0 {
  1081. db = db.Where("x.warehousing_id in(?)", id)
  1082. }
  1083. if org_id > 0 {
  1084. db = db.Where("x.org_id = ?", org_id)
  1085. }
  1086. 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
  1087. return list, err
  1088. }
  1089. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1090. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1091. return warehousing, err
  1092. }
  1093. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1094. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1095. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1096. return warehousing, err
  1097. }
  1098. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1099. 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
  1100. return warehousing, err
  1101. }
  1102. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1103. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1104. if len(id) > 0 {
  1105. db = db.Where("x.id in(?)", id)
  1106. }
  1107. if org_id > 0 {
  1108. db = db.Where("x.org_id = ?", org_id)
  1109. }
  1110. 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
  1111. return warehousing, err
  1112. }
  1113. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1114. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1115. // return warehousing, err
  1116. //
  1117. //}
  1118. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1119. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1120. return
  1121. }
  1122. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1123. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1124. return
  1125. }
  1126. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1127. if len(salesReturnInfo) > 0 {
  1128. utx := writeDb.Begin()
  1129. if len(salesReturnInfo) > 0 {
  1130. 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 "
  1131. insertParams := make([]string, 0)
  1132. insertData := make([]interface{}, 0)
  1133. for _, info := range salesReturnInfo {
  1134. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1135. insertData = append(insertData, info.GoodId)
  1136. insertData = append(insertData, info.SalesReturnId)
  1137. insertData = append(insertData, info.GoodTypeId)
  1138. insertData = append(insertData, info.Count)
  1139. insertData = append(insertData, info.Price)
  1140. insertData = append(insertData, info.Total)
  1141. insertData = append(insertData, info.Ctime)
  1142. insertData = append(insertData, info.Status)
  1143. insertData = append(insertData, info.OrgId)
  1144. insertData = append(insertData, info.OrderNumber)
  1145. insertData = append(insertData, info.Type)
  1146. insertData = append(insertData, info.Dealer)
  1147. insertData = append(insertData, info.Manufacturer)
  1148. }
  1149. thisSQL += strings.Join(insertParams, ", ")
  1150. err = utx.Exec(thisSQL, insertData...).Error
  1151. if err != nil {
  1152. utx.Rollback()
  1153. return
  1154. }
  1155. }
  1156. utx.Commit()
  1157. }
  1158. return
  1159. }
  1160. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1161. if len(salesReturnInfo) > 0 {
  1162. utx := writeDb.Begin()
  1163. if len(salesReturnInfo) > 0 {
  1164. 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 "
  1165. insertParams := make([]string, 0)
  1166. insertData := make([]interface{}, 0)
  1167. for _, info := range salesReturnInfo {
  1168. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1169. insertData = append(insertData, info.DrugId)
  1170. insertData = append(insertData, info.SalesReturnId)
  1171. insertData = append(insertData, info.Count)
  1172. insertData = append(insertData, info.Price)
  1173. insertData = append(insertData, info.Total)
  1174. insertData = append(insertData, info.Ctime)
  1175. insertData = append(insertData, info.Status)
  1176. insertData = append(insertData, info.OrgId)
  1177. insertData = append(insertData, info.OrderNumber)
  1178. insertData = append(insertData, info.Type)
  1179. insertData = append(insertData, info.Dealer)
  1180. insertData = append(insertData, info.Manufacturer)
  1181. insertData = append(insertData, info.RetailPrice)
  1182. insertData = append(insertData, info.RetailTotalPrice)
  1183. }
  1184. thisSQL += strings.Join(insertParams, ", ")
  1185. err = utx.Exec(thisSQL, insertData...).Error
  1186. if err != nil {
  1187. utx.Rollback()
  1188. return
  1189. }
  1190. }
  1191. utx.Commit()
  1192. }
  1193. return
  1194. }
  1195. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1196. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1197. return total, err
  1198. }
  1199. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1200. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1201. return total, err
  1202. }
  1203. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1204. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1205. if err != gorm.ErrRecordNotFound {
  1206. if err != nil {
  1207. tx.Rollback()
  1208. return total, err
  1209. }
  1210. }
  1211. return total, err
  1212. }
  1213. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1214. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1215. return total, err
  1216. }
  1217. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1218. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1219. if err != gorm.ErrRecordNotFound {
  1220. if err != nil {
  1221. tx.Rollback()
  1222. return total, err
  1223. }
  1224. }
  1225. return total, err
  1226. }
  1227. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1228. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1229. // return total, err
  1230. //}
  1231. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1232. db := readDb.Model(&models.SalesReturn{})
  1233. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1234. if len(keywords) > 0 {
  1235. likeKey := "%" + keywords + "%"
  1236. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1237. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1238. 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")
  1239. }
  1240. if startTime > 0 {
  1241. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1242. }
  1243. if endTime > 0 {
  1244. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1245. }
  1246. db = db.Count(&total)
  1247. offset := (page - 1) * limit
  1248. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1249. return
  1250. }
  1251. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1252. db := readDb.Model(&models.DrugSalesReturn{})
  1253. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1254. if len(keywords) > 0 {
  1255. likeKey := "%" + keywords + "%"
  1256. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1257. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1258. 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")
  1259. }
  1260. if startTime > 0 {
  1261. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1262. }
  1263. if endTime > 0 {
  1264. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1265. }
  1266. db = db.Count(&total)
  1267. offset := (page - 1) * limit
  1268. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1269. return
  1270. }
  1271. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1272. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1273. return list, err
  1274. }
  1275. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1276. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1277. return list, err
  1278. }
  1279. type DrugCancelStockInfo struct {
  1280. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1281. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1282. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1283. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1284. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1285. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1286. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1287. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1288. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1289. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1290. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1291. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1292. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1293. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1294. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1295. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1296. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1297. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1298. Number string `gorm:"column:number" json:"number" form:"number"`
  1299. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1300. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1301. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1302. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1303. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1304. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1305. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1306. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1307. }
  1308. func (DrugCancelStockInfo) TableName() string {
  1309. return "xt_drug_cancel_stock_info"
  1310. }
  1311. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1312. 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
  1313. return list, err
  1314. }
  1315. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1316. db := readDb.Model(&models.WarehousingInfo{})
  1317. db = db.Where("org_id = ? AND status = 1", orgId)
  1318. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1319. if startTime > 0 {
  1320. db = db.Where("ctime >=?", startTime)
  1321. }
  1322. if endTime > 0 {
  1323. db = db.Where("ctime<= ?", endTime)
  1324. }
  1325. db = db.Count(&total)
  1326. offset := (page - 1) * limit
  1327. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1328. return list, total, err
  1329. }
  1330. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1331. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1332. return total, err
  1333. }
  1334. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1335. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1336. return total, err
  1337. }
  1338. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1339. err := writeDb.Create(&warehouseOut).Error
  1340. return err
  1341. }
  1342. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1343. err := tx.Create(&warehouseOut).Error
  1344. if err != nil {
  1345. tx.Rollback()
  1346. return err
  1347. }
  1348. return err
  1349. }
  1350. func UpdateSingleWarehoseOut(id int64, storehouse_id int64) error {
  1351. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1352. return err
  1353. }
  1354. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1355. err := writeDb.Create(&warehouseOut).Error
  1356. return err
  1357. }
  1358. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1359. err := tx.Create(&warehouseOut).Error
  1360. if err != nil {
  1361. tx.Rollback()
  1362. return err
  1363. }
  1364. return err
  1365. }
  1366. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1367. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1368. return err
  1369. }
  1370. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1371. if len(warehouseOutInfo) > 0 {
  1372. utx := writeDb.Begin()
  1373. if len(warehouseOutInfo) > 0 {
  1374. 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 "
  1375. insertParams := make([]string, 0)
  1376. insertData := make([]interface{}, 0)
  1377. for _, info := range warehouseOutInfo {
  1378. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1379. insertData = append(insertData, info.WarehouseOutId)
  1380. insertData = append(insertData, info.GoodId)
  1381. insertData = append(insertData, info.GoodTypeId)
  1382. insertData = append(insertData, info.ProductDate)
  1383. insertData = append(insertData, info.ExpiryDate)
  1384. insertData = append(insertData, info.Count)
  1385. insertData = append(insertData, info.Price)
  1386. insertData = append(insertData, info.TotalPrice)
  1387. insertData = append(insertData, info.Remark)
  1388. insertData = append(insertData, info.Ctime)
  1389. insertData = append(insertData, info.Status)
  1390. insertData = append(insertData, info.OrgId)
  1391. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1392. insertData = append(insertData, info.Type)
  1393. insertData = append(insertData, info.Dealer)
  1394. insertData = append(insertData, info.Manufacturer)
  1395. insertData = append(insertData, info.Number)
  1396. insertData = append(insertData, info.LicenseNumber)
  1397. }
  1398. thisSQL += strings.Join(insertParams, ", ")
  1399. err = utx.Exec(thisSQL, insertData...).Error
  1400. if err != nil {
  1401. utx.Rollback()
  1402. return
  1403. }
  1404. }
  1405. utx.Commit()
  1406. }
  1407. return
  1408. }
  1409. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1410. if len(warehouseOutInfo) > 0 {
  1411. utx := writeDb.Begin()
  1412. if len(warehouseOutInfo) > 0 {
  1413. 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 "
  1414. insertParams := make([]string, 0)
  1415. insertData := make([]interface{}, 0)
  1416. for _, info := range warehouseOutInfo {
  1417. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1418. insertData = append(insertData, info.WarehouseOutId)
  1419. insertData = append(insertData, info.DrugId)
  1420. insertData = append(insertData, info.ProductDate)
  1421. insertData = append(insertData, info.ExpiryDate)
  1422. insertData = append(insertData, info.Count)
  1423. insertData = append(insertData, info.Price)
  1424. insertData = append(insertData, info.TotalPrice)
  1425. insertData = append(insertData, info.Remark)
  1426. insertData = append(insertData, info.Ctime)
  1427. insertData = append(insertData, info.Status)
  1428. insertData = append(insertData, info.OrgId)
  1429. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1430. insertData = append(insertData, info.Type)
  1431. insertData = append(insertData, info.Dealer)
  1432. insertData = append(insertData, info.Manufacturer)
  1433. insertData = append(insertData, info.RetailPrice)
  1434. insertData = append(insertData, info.RetailTotalPrice)
  1435. insertData = append(insertData, info.Number)
  1436. insertData = append(insertData, info.BatchNumber)
  1437. insertData = append(insertData, info.CountUnit)
  1438. }
  1439. thisSQL += strings.Join(insertParams, ", ")
  1440. err = utx.Exec(thisSQL, insertData...).Error
  1441. if err != nil {
  1442. utx.Rollback()
  1443. return
  1444. }
  1445. }
  1446. utx.Commit()
  1447. }
  1448. return
  1449. }
  1450. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1451. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1452. return info, err
  1453. }
  1454. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1455. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1456. return info, err
  1457. }
  1458. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1459. db := readDb.Model(&models.WarehouseOut{})
  1460. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1461. if len(keywords) > 0 {
  1462. likeKey := "%" + keywords + "%"
  1463. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1464. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1465. 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")
  1466. }
  1467. if startTime > 0 {
  1468. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1469. }
  1470. if endTime > 0 {
  1471. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1472. }
  1473. db = db.Count(&total)
  1474. offset := (page - 1) * limit
  1475. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1476. return list, total, err
  1477. }
  1478. 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) {
  1479. db := readDb.Model(&models.WarehouseOut{})
  1480. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1481. if len(keywords) > 0 {
  1482. likeKey := "%" + keywords + "%"
  1483. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1484. 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")
  1485. }
  1486. if startTime > 0 {
  1487. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1488. }
  1489. if endTime > 0 {
  1490. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1491. }
  1492. if storehouse_id > 0 {
  1493. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1494. }
  1495. if way_type == 0 {
  1496. 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")
  1497. }
  1498. if way_type == 1 {
  1499. db = db.Where("xt_warehouse_out.is_sys = 0")
  1500. }
  1501. if way_type == 2 {
  1502. db = db.Where("xt_warehouse_out.is_sys = 1")
  1503. }
  1504. if way_type == 3 {
  1505. db = db.Where("xt_warehouse_out.is_sys = 12")
  1506. }
  1507. if way_type == 4 {
  1508. db = db.Where("xt_warehouse_out.is_sys = 5")
  1509. }
  1510. if check_type > 0 {
  1511. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1512. }
  1513. if check_type == 0 {
  1514. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1515. }
  1516. db = db.Count(&total)
  1517. offset := (page - 1) * limit
  1518. if orgId == 10191 {
  1519. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1520. } else {
  1521. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1522. }
  1523. return list, total, err
  1524. }
  1525. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1526. db := readDb.Model(&models.DrugWarehouseOut{})
  1527. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1528. if len(keywords) > 0 {
  1529. likeKey := "%" + keywords + "%"
  1530. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1531. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1532. 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")
  1533. }
  1534. if startTime > 0 {
  1535. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1536. }
  1537. if endTime > 0 {
  1538. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1539. }
  1540. db = db.Count(&total)
  1541. offset := (page - 1) * limit
  1542. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1543. return list, total, err
  1544. }
  1545. 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) {
  1546. db := readDb.Model(&models.DrugWarehouseOut{})
  1547. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1548. if len(keywords) > 0 {
  1549. likeKey := "%" + keywords + "%"
  1550. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1551. 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")
  1552. }
  1553. if startTime > 0 {
  1554. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1555. }
  1556. if endTime > 0 {
  1557. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1558. }
  1559. if storehouse_id > 0 {
  1560. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1561. }
  1562. if check_type > 0 {
  1563. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1564. }
  1565. if way_type == 0 {
  1566. 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")
  1567. }
  1568. if way_type == 1 {
  1569. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1570. }
  1571. if way_type == 2 {
  1572. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1573. }
  1574. if way_type == 3 {
  1575. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1576. }
  1577. if way_type == 4 {
  1578. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1579. }
  1580. db = db.Count(&total)
  1581. offset := (page - 1) * limit
  1582. if orgId == 10191 {
  1583. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1584. } else {
  1585. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1586. }
  1587. return list, total, err
  1588. }
  1589. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1590. 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
  1591. return list, err
  1592. }
  1593. type DrugWarehouseOutInfo struct {
  1594. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1595. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1596. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1597. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1598. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1599. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1600. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1601. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1602. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1603. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1604. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1605. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1606. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1607. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1608. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1609. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1610. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1611. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1612. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1613. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1614. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1615. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1616. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1617. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1618. Number string `gorm:"column:number" json:"number" form:"number"`
  1619. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1620. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1621. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1622. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1623. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1624. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1625. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1626. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1627. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1628. }
  1629. func (DrugWarehouseOutInfo) TableName() string {
  1630. return "xt_drug_warehouse_out_info"
  1631. }
  1632. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1633. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1634. return list, err
  1635. }
  1636. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1637. info := &models.WarehousingInfo{}
  1638. type WarehouseInfoCountAmount struct {
  1639. Total int64
  1640. }
  1641. var result WarehouseInfoCountAmount
  1642. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1643. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1644. return result.Total, info
  1645. }
  1646. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1647. type SalesReturnCountAmount struct {
  1648. Total int64
  1649. }
  1650. var result SalesReturnCountAmount
  1651. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1652. return result.Total, err
  1653. }
  1654. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1655. type WarehouseOutInfoCountAmount struct {
  1656. Total int64
  1657. }
  1658. var result WarehouseOutInfoCountAmount
  1659. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1660. return result.Total, err
  1661. }
  1662. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1663. type CancleStockoCountAmount struct {
  1664. Total int64
  1665. }
  1666. var result CancleStockoCountAmount
  1667. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1668. return result.Total, err
  1669. }
  1670. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1671. info := models.WarehousingInfo{}
  1672. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1673. return info, err
  1674. }
  1675. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1676. stock := models.CancelStock{}
  1677. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1678. if err == gorm.ErrRecordNotFound {
  1679. return nil, err
  1680. }
  1681. if err != nil {
  1682. return nil, err
  1683. }
  1684. return &stock, nil
  1685. }
  1686. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1687. stock := models.CancelStock{}
  1688. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1689. if err == gorm.ErrRecordNotFound {
  1690. return nil, err
  1691. }
  1692. if err != nil {
  1693. return nil, err
  1694. }
  1695. return &stock, nil
  1696. }
  1697. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  1698. stock := models.CancelStock{}
  1699. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1700. if err != gorm.ErrRecordNotFound {
  1701. if err != nil {
  1702. tx.Rollback()
  1703. return &stock, err
  1704. }
  1705. }
  1706. return &stock, err
  1707. }
  1708. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1709. err := writeDb.Create(&cancelStock).Error
  1710. return err
  1711. }
  1712. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  1713. err := tx.Create(&cancelStock).Error
  1714. if err != nil {
  1715. tx.Rollback()
  1716. return err
  1717. }
  1718. return err
  1719. }
  1720. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  1721. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1722. return err
  1723. }
  1724. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  1725. stock := models.CancelStock{}
  1726. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1727. return stock, err
  1728. }
  1729. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  1730. stock := models.CancelStock{}
  1731. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1732. if err != gorm.ErrRecordNotFound {
  1733. if err != nil {
  1734. tx.Rollback()
  1735. return stock, err
  1736. }
  1737. }
  1738. return stock, err
  1739. }
  1740. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  1741. stock := models.DrugCancelStock{}
  1742. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1743. return stock, err
  1744. }
  1745. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  1746. stock := models.DrugCancelStock{}
  1747. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1748. if err != gorm.ErrRecordNotFound {
  1749. if err != nil {
  1750. tx.Rollback()
  1751. return stock, err
  1752. }
  1753. }
  1754. return stock, err
  1755. }
  1756. func GetManufactureById(id int64) (models.Manufacturer, error) {
  1757. manufacturer := models.Manufacturer{}
  1758. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1759. return manufacturer, err
  1760. }
  1761. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  1762. manufacturer := models.Manufacturer{}
  1763. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1764. if err != gorm.ErrRecordNotFound {
  1765. if err != nil {
  1766. tx.Rollback()
  1767. return manufacturer, err
  1768. }
  1769. }
  1770. return manufacturer, err
  1771. }
  1772. func GetDealerById(id int64) (models.Dealer, error) {
  1773. dealer := models.Dealer{}
  1774. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1775. return dealer, err
  1776. }
  1777. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  1778. dealer := models.Dealer{}
  1779. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1780. if err != gorm.ErrRecordNotFound {
  1781. if err != nil {
  1782. tx.Rollback()
  1783. return dealer, err
  1784. }
  1785. }
  1786. return dealer, err
  1787. }
  1788. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1789. err := writeDb.Create(&cancelStock).Error
  1790. return err
  1791. }
  1792. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  1793. err := tx.Create(&cancelStock).Error
  1794. if err != nil {
  1795. tx.Rollback()
  1796. return err
  1797. }
  1798. return err
  1799. }
  1800. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  1801. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1802. return err
  1803. }
  1804. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  1805. // err := tx.Create(&cancelStock).Error
  1806. // return err
  1807. //}
  1808. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1809. if len(cancelStockInfo) > 0 {
  1810. utx := writeDb.Begin()
  1811. if len(cancelStockInfo) > 0 {
  1812. 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 "
  1813. insertParams := make([]string, 0)
  1814. insertData := make([]interface{}, 0)
  1815. for _, info := range cancelStockInfo {
  1816. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1817. insertData = append(insertData, info.GoodId)
  1818. insertData = append(insertData, info.CancelStockId)
  1819. insertData = append(insertData, info.GoodTypeId)
  1820. insertData = append(insertData, info.Count)
  1821. insertData = append(insertData, info.Price)
  1822. insertData = append(insertData, info.Total)
  1823. insertData = append(insertData, info.Ctime)
  1824. insertData = append(insertData, info.Status)
  1825. insertData = append(insertData, info.OrgId)
  1826. insertData = append(insertData, info.OrderNumber)
  1827. insertData = append(insertData, info.Type)
  1828. insertData = append(insertData, info.Dealer)
  1829. insertData = append(insertData, info.Manufacturer)
  1830. insertData = append(insertData, info.Number)
  1831. insertData = append(insertData, info.RegisterAccount)
  1832. insertData = append(insertData, info.Remark)
  1833. insertData = append(insertData, info.ProductDate)
  1834. insertData = append(insertData, info.ExpiryDate)
  1835. insertData = append(insertData, info.WarehouseInfoId)
  1836. insertData = append(insertData, info.WarehouseInfoId)
  1837. }
  1838. thisSQL += strings.Join(insertParams, ", ")
  1839. err = utx.Exec(thisSQL, insertData...).Error
  1840. if err != nil {
  1841. utx.Rollback()
  1842. return
  1843. }
  1844. }
  1845. utx.Commit()
  1846. }
  1847. return
  1848. }
  1849. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  1850. err := XTWriteDB().Create(&info).Error
  1851. return err
  1852. }
  1853. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  1854. // err := tx.Create(&info).Error
  1855. // return err
  1856. //}
  1857. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  1858. info := models.DrugCancelStockInfo{}
  1859. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1860. return info, err
  1861. }
  1862. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  1863. // info := models.DrugCancelStockInfo{}
  1864. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1865. // return info, err
  1866. //}
  1867. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  1868. err := XTWriteDB().Create(&info).Error
  1869. return err
  1870. }
  1871. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  1872. err := XTWriteDB().Create(&info).Error
  1873. if err != nil {
  1874. tx.Rollback()
  1875. return err
  1876. }
  1877. return err
  1878. }
  1879. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  1880. info := models.CancelStockInfo{}
  1881. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1882. return info, err
  1883. }
  1884. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  1885. info := models.CancelStockInfo{}
  1886. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1887. if err != gorm.ErrRecordNotFound {
  1888. if err != nil {
  1889. tx.Rollback()
  1890. return info, err
  1891. }
  1892. }
  1893. return info, err
  1894. }
  1895. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  1896. info := models.DrugCancelStockInfo{}
  1897. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  1898. return info, err
  1899. }
  1900. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1901. if len(cancelStockInfo) > 0 {
  1902. utx := writeDb.Begin()
  1903. if len(cancelStockInfo) > 0 {
  1904. 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 "
  1905. insertParams := make([]string, 0)
  1906. insertData := make([]interface{}, 0)
  1907. for _, info := range cancelStockInfo {
  1908. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1909. insertData = append(insertData, info.DrugId)
  1910. insertData = append(insertData, info.CancelStockId)
  1911. insertData = append(insertData, info.Count)
  1912. insertData = append(insertData, info.Price)
  1913. insertData = append(insertData, info.Total)
  1914. insertData = append(insertData, info.Ctime)
  1915. insertData = append(insertData, info.Status)
  1916. insertData = append(insertData, info.OrgId)
  1917. insertData = append(insertData, info.OrderNumber)
  1918. insertData = append(insertData, info.Type)
  1919. insertData = append(insertData, info.Dealer)
  1920. insertData = append(insertData, info.Manufacturer)
  1921. insertData = append(insertData, info.RetailPrice)
  1922. insertData = append(insertData, info.RetailTotalPrice)
  1923. insertData = append(insertData, info.RegisterAccount)
  1924. insertData = append(insertData, info.BatchNumber)
  1925. insertData = append(insertData, info.Remark)
  1926. insertData = append(insertData, info.MaxUnit)
  1927. insertData = append(insertData, info.ProductDate)
  1928. insertData = append(insertData, info.ExpiryDate)
  1929. insertData = append(insertData, info.BatchNumberId)
  1930. }
  1931. thisSQL += strings.Join(insertParams, ", ")
  1932. err = utx.Exec(thisSQL, insertData...).Error
  1933. if err != nil {
  1934. utx.Rollback()
  1935. return
  1936. }
  1937. }
  1938. utx.Commit()
  1939. }
  1940. return
  1941. }
  1942. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  1943. db := readDb.Model(&models.CancelStock{})
  1944. db = db.Where("org_id = ? AND status = 1", orgId)
  1945. err = db.Order("ctime desc").Find(&list).Error
  1946. return
  1947. }
  1948. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1949. db := readDb.Model(&models.WarehousingInfo{})
  1950. db = db.Where("org_id = ? AND status = 1", orgId)
  1951. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  1952. if startTime > 0 {
  1953. db = db.Where("ctime >=?", startTime)
  1954. }
  1955. if endTime > 0 {
  1956. db = db.Where("ctime<= ?", endTime)
  1957. }
  1958. db = db.Count(&total)
  1959. offset := (page - 1) * limit
  1960. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1961. return
  1962. }
  1963. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  1964. db := XTReadDB().Model(&models.StockInfo{})
  1965. if startime > 0 {
  1966. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1967. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  1968. })
  1969. }
  1970. if startime == 0 || endtime == 0 {
  1971. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1972. return db.Where("org_id = ? AND status = 1", orgId)
  1973. })
  1974. }
  1975. if endtime > 0 {
  1976. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1977. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  1978. })
  1979. }
  1980. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  1981. db = db.Group("xt_good_information.id")
  1982. if startime == 0 || endtime == 0 {
  1983. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1984. return db.Where("org_id = ? AND status = 1", orgId)
  1985. })
  1986. }
  1987. if startime > 0 {
  1988. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1989. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  1990. })
  1991. }
  1992. if endtime > 0 {
  1993. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1994. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  1995. })
  1996. }
  1997. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  1998. if startime == 0 || endtime == 0 {
  1999. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2000. }
  2001. if startime > 0 {
  2002. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2003. }
  2004. if endtime > 0 {
  2005. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2006. }
  2007. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2008. if len(keyword) > 0 {
  2009. likeKey := "%" + keyword + "%"
  2010. 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)
  2011. 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")
  2012. }
  2013. if type_name > 0 {
  2014. 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)
  2015. }
  2016. db = db.Count(&total)
  2017. offset := (page - 1) * limit
  2018. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2019. return
  2020. }
  2021. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2022. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2023. return
  2024. }
  2025. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2026. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2027. return
  2028. }
  2029. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2030. 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
  2031. return
  2032. }
  2033. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2034. 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
  2035. return
  2036. }
  2037. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2038. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2039. "good_id": warehouseInfo.GoodId,
  2040. "number": warehouseInfo.Number,
  2041. "product_date": warehouseInfo.ProductDate,
  2042. "expiry_date": warehouseInfo.ExpiryDate,
  2043. "warehousing_count": warehouseInfo.WarehousingCount,
  2044. "price": warehouseInfo.Price,
  2045. "total_price": warehouseInfo.TotalPrice,
  2046. "mtime": warehouseInfo.Mtime,
  2047. "remark": warehouseInfo.Remark,
  2048. }).Error
  2049. return
  2050. }
  2051. func UpDateWarehouseStatus(id int64) (err error) {
  2052. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2053. return
  2054. }
  2055. func UpdatedStockFlowById(id int64) (err error) {
  2056. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2057. return
  2058. }
  2059. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2060. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2061. return
  2062. }
  2063. func UpdateDrugFlowById(id int64) (err error) {
  2064. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2065. return
  2066. }
  2067. func DeleteWarehouse(ids []string) (err error) {
  2068. ut := writeDb.Begin()
  2069. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2070. if err != nil {
  2071. ut.Rollback()
  2072. return
  2073. }
  2074. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2075. if err != nil {
  2076. ut.Rollback()
  2077. return
  2078. }
  2079. ut.Commit()
  2080. return
  2081. }
  2082. func DeleteStockFlow(ids []string) (err error) {
  2083. ut := writeDb.Begin()
  2084. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2085. if err != nil {
  2086. ut.Rollback()
  2087. return
  2088. }
  2089. ut.Commit()
  2090. return
  2091. }
  2092. func DeleteDrugWarehouse(ids []string) (err error) {
  2093. ut := writeDb.Begin()
  2094. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2095. if err != nil {
  2096. ut.Rollback()
  2097. return
  2098. }
  2099. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2100. if err != nil {
  2101. ut.Rollback()
  2102. return
  2103. }
  2104. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2105. if err != nil {
  2106. ut.Rollback()
  2107. return
  2108. }
  2109. ut.Commit()
  2110. return
  2111. }
  2112. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2113. warehouse := &models.Warehousing{}
  2114. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2115. return warehouse, err
  2116. }
  2117. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  2118. warehouseOut := &models.WarehouseOut{}
  2119. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2120. return warehouseOut, err
  2121. }
  2122. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2123. warehouseOut := &models.DrugWarehouseOut{}
  2124. 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
  2125. return warehouseOut, err
  2126. }
  2127. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2128. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2129. return
  2130. }
  2131. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2132. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2133. return
  2134. }
  2135. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2136. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2137. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2138. return
  2139. }
  2140. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2141. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2142. return
  2143. }
  2144. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2145. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2146. if manufacturer_id > 0 {
  2147. db = db.Where("manufacturer = ?", manufacturer_id)
  2148. }
  2149. if dealer_id > 0 {
  2150. db = db.Where("dealer = ?", dealer_id)
  2151. }
  2152. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2153. return goodInfo, err
  2154. }
  2155. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2156. db := readDb.Model(&models.Warehousing{})
  2157. likekey := "%" + keywords + "%"
  2158. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2159. db = db.Where("warehousing_order LIKE ? ", likekey)
  2160. db = db.Count(&total)
  2161. offset := (page - 1) * limit
  2162. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2163. return
  2164. }
  2165. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2166. info := models.WarehousingInfo{}
  2167. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2168. return info, err
  2169. }
  2170. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2171. info := models.WarehousingInfo{}
  2172. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2173. return info, err
  2174. }
  2175. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2176. err = writeDb.Save(&info).Error
  2177. return err
  2178. }
  2179. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2180. err = writeDb.Save(&info).Error
  2181. return err
  2182. }
  2183. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2184. 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
  2185. return err
  2186. }
  2187. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2188. 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
  2189. return err
  2190. }
  2191. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2192. info := models.DrugWarehouseInfo{}
  2193. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2194. return info, err
  2195. }
  2196. func EditWarehousing(warehouse models.Warehousing) {
  2197. 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
  2198. }
  2199. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  2200. 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
  2201. }
  2202. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  2203. 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
  2204. return list, err
  2205. }
  2206. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2207. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2208. 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)
  2209. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2210. return list, err
  2211. }
  2212. func DeleteSalesReturn(ids []string) (err error) {
  2213. ut := writeDb.Begin()
  2214. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2215. if err != nil {
  2216. ut.Rollback()
  2217. return
  2218. }
  2219. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2220. if err != nil {
  2221. ut.Rollback()
  2222. return
  2223. }
  2224. ut.Commit()
  2225. return
  2226. }
  2227. func DeleteDrugSalesReturn(ids []string) (err error) {
  2228. ut := writeDb.Begin()
  2229. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2230. if err != nil {
  2231. ut.Rollback()
  2232. return
  2233. }
  2234. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2235. if err != nil {
  2236. ut.Rollback()
  2237. return
  2238. }
  2239. ut.Commit()
  2240. return
  2241. }
  2242. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2243. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2244. return
  2245. }
  2246. type DrugSalesReturnInfo struct {
  2247. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2248. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2249. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2250. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2251. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2252. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2253. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2254. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2255. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2256. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2257. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2258. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2259. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2260. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2261. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2262. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2263. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2264. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2265. Number string `gorm:"column:number" json:"number" form:"number"`
  2266. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2267. }
  2268. func (DrugSalesReturnInfo) TableName() string {
  2269. return "xt_drug_sales_return_info"
  2270. }
  2271. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2272. 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
  2273. return
  2274. }
  2275. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2276. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2277. return
  2278. }
  2279. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2280. 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
  2281. return
  2282. }
  2283. func UpDateSaleReturnStatus(id int64) (err error) {
  2284. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2285. return
  2286. }
  2287. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2288. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2289. return
  2290. }
  2291. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2292. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2293. return salesReturn, err
  2294. }
  2295. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2296. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2297. return salesReturn, err
  2298. }
  2299. func EditSaleReturn(sales models.SalesReturn) {
  2300. 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
  2301. }
  2302. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2303. 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
  2304. }
  2305. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2306. err = writeDb.Save(&info).Error
  2307. return err
  2308. }
  2309. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2310. err = writeDb.Save(&info).Error
  2311. return err
  2312. }
  2313. func DeleteWarehouseOut(ids []string) (err error) {
  2314. ut := writeDb.Begin()
  2315. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2316. if err != nil {
  2317. ut.Rollback()
  2318. return
  2319. }
  2320. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2321. if err != nil {
  2322. ut.Rollback()
  2323. return
  2324. }
  2325. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2326. if err != nil {
  2327. ut.Rollback()
  2328. return
  2329. }
  2330. ut.Commit()
  2331. return
  2332. }
  2333. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2334. ut := writeDb.Begin()
  2335. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2336. if err != nil {
  2337. ut.Rollback()
  2338. return
  2339. }
  2340. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2341. if err != nil {
  2342. ut.Rollback()
  2343. return
  2344. }
  2345. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2346. if err != nil {
  2347. ut.Rollback()
  2348. return
  2349. }
  2350. ut.Commit()
  2351. return
  2352. }
  2353. func UpDateWarehouseOutStatus(id int64) (err error) {
  2354. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2355. return
  2356. }
  2357. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2358. ut := writeDb.Begin()
  2359. err = ut.Save(&info).Error
  2360. if err != nil {
  2361. ut.Rollback()
  2362. return err
  2363. }
  2364. ut.Commit()
  2365. return err
  2366. }
  2367. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2368. err = tx.Save(&info).Error
  2369. if err != nil {
  2370. tx.Rollback()
  2371. return err
  2372. }
  2373. return err
  2374. }
  2375. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2376. ut := writeDb.Begin()
  2377. err = ut.Save(&info).Error
  2378. if err != nil {
  2379. ut.Rollback()
  2380. return err
  2381. }
  2382. ut.Commit()
  2383. return err
  2384. }
  2385. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2386. err = tx.Save(&info).Error
  2387. if err != nil {
  2388. tx.Rollback()
  2389. return err
  2390. }
  2391. return err
  2392. }
  2393. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2394. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2395. return
  2396. }
  2397. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  2398. 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
  2399. }
  2400. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2401. 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
  2402. }
  2403. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  2404. err = writeDb.Save(&info).Error
  2405. return err
  2406. }
  2407. 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) {
  2408. db := readDb.Model(&models.CancelStock{})
  2409. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2410. if len(keywords) > 0 {
  2411. likeKey := "%" + keywords + "%"
  2412. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2413. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2414. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2415. }
  2416. if startTime > 0 {
  2417. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2418. }
  2419. if endTime > 0 {
  2420. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2421. }
  2422. if storehouse_id > 0 {
  2423. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2424. }
  2425. if check_type > 0 {
  2426. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2427. }
  2428. db = db.Count(&total)
  2429. offset := (page - 1) * limit
  2430. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2431. return
  2432. }
  2433. 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) {
  2434. db := readDb.Model(&models.DrugCancelStock{})
  2435. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2436. if len(keywords) > 0 {
  2437. likeKey := "%" + keywords + "%"
  2438. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2439. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2440. 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")
  2441. }
  2442. if startTime > 0 {
  2443. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2444. }
  2445. if endTime > 0 {
  2446. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2447. }
  2448. if storehoue_id > 0 {
  2449. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2450. }
  2451. if check_type > 0 {
  2452. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2453. }
  2454. db = db.Count(&total)
  2455. offset := (page - 1) * limit
  2456. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2457. return
  2458. }
  2459. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2460. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2461. return info, err
  2462. }
  2463. func DeleteCancelStock(ids []string) (err error) {
  2464. ut := writeDb.Begin()
  2465. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2466. if err != nil {
  2467. ut.Rollback()
  2468. return
  2469. }
  2470. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2471. if err != nil {
  2472. ut.Rollback()
  2473. return
  2474. }
  2475. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2476. if err != nil {
  2477. ut.Rollback()
  2478. return
  2479. }
  2480. ut.Commit()
  2481. return
  2482. }
  2483. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2484. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2485. return info, err
  2486. }
  2487. func DeleteDrugCancelStock(ids []string) (err error) {
  2488. ut := writeDb.Begin()
  2489. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2490. if err != nil {
  2491. ut.Rollback()
  2492. return
  2493. }
  2494. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2495. if err != nil {
  2496. ut.Rollback()
  2497. return
  2498. }
  2499. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2500. if err != nil {
  2501. ut.Rollback()
  2502. return
  2503. }
  2504. ut.Commit()
  2505. return
  2506. }
  2507. func UpDateCancleStockStatus(id int64) (err error) {
  2508. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2509. return
  2510. }
  2511. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2512. info := models.CancelStockInfo{}
  2513. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2514. return info, err
  2515. }
  2516. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2517. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2518. return err
  2519. }
  2520. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2521. 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
  2522. return
  2523. }
  2524. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2525. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2526. return
  2527. }
  2528. func UpDateDrugCancelStockById(id int64) (err error) {
  2529. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2530. return
  2531. }
  2532. func EditCancelStock(sales models.CancelStock) {
  2533. 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
  2534. }
  2535. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2536. 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
  2537. }
  2538. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2539. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2540. return cancelStock, err
  2541. }
  2542. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2543. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2544. return cancelStock, err
  2545. }
  2546. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2547. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2548. return cancelStock, err
  2549. }
  2550. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2551. err = writeDb.Save(&info).Error
  2552. return err
  2553. }
  2554. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2555. err = writeDb.Save(&info).Error
  2556. return err
  2557. }
  2558. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2559. 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
  2560. return list, err
  2561. }
  2562. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2563. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2564. 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)
  2565. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2566. return list, err
  2567. }
  2568. type StockDetailModel struct {
  2569. GoodId int64 `gorm:"good_id" json:"good_id"`
  2570. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2571. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2572. Price float64 `gorm:"price" json:"price"`
  2573. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2574. Type int `gorm:"type" json:"type"`
  2575. Creater int64 `gorm:"creater" json:"creater"`
  2576. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2577. Types int64 `gorm:"types" json:"types"`
  2578. }
  2579. 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) {
  2580. db := readDb.Model(&models.WarehousingInfo{})
  2581. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2582. if len(keywords) > 0 {
  2583. likeKey := "%" + keywords + "%"
  2584. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2585. 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)
  2586. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2587. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2588. 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")
  2589. } else {
  2590. 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)
  2591. }
  2592. if manufacturer > 0 {
  2593. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2594. }
  2595. if storehouse_id > 0 {
  2596. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2597. }
  2598. if order_type > 0 {
  2599. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2600. }
  2601. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2602. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2603. db = db.Count(&total)
  2604. offset := (page - 1) * limit
  2605. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2606. return
  2607. }
  2608. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2609. db := readDb.Model(&models.WarehousingInfo{})
  2610. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2611. if len(keywords) > 0 {
  2612. likeKey := "%" + keywords + "%"
  2613. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2614. 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)
  2615. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2616. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2617. 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")
  2618. } else {
  2619. 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)
  2620. }
  2621. if manufacturer > 0 {
  2622. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2623. }
  2624. if order_type > 0 {
  2625. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2626. }
  2627. if storehouse_id > 0 {
  2628. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2629. }
  2630. type PriceAmount struct {
  2631. Total float64
  2632. }
  2633. var result []PriceAmount
  2634. err = db.Select("total_price as total").Scan(&result).Error
  2635. var total float64
  2636. for _, value := range result {
  2637. total = total + value.Total
  2638. }
  2639. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2640. return err, total
  2641. }
  2642. func FloatRound(f float64, n int) float64 {
  2643. format := "%." + strconv.Itoa(n) + "f"
  2644. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2645. return res
  2646. }
  2647. 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) {
  2648. if orgId == 9671 {
  2649. db := readDb.Model(&models.WarehouseOutInfo{})
  2650. 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)
  2651. if len(keywords) > 0 {
  2652. likeKey := "%" + keywords + "%"
  2653. 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)
  2654. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2655. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2656. 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")
  2657. } else {
  2658. 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)
  2659. }
  2660. if manufacturer > 0 {
  2661. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2662. }
  2663. if order_type > 0 {
  2664. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2665. }
  2666. if storehouse_id > 0 {
  2667. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2668. }
  2669. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2670. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2671. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2672. db = db.Count(&total)
  2673. offset := (page - 1) * limit
  2674. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2675. return
  2676. } else {
  2677. db := readDb.Model(&models.WarehouseOutInfo{})
  2678. 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)
  2679. if len(keywords) > 0 {
  2680. likeKey := "%" + keywords + "%"
  2681. 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)
  2682. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2683. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2684. 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")
  2685. } else {
  2686. 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)
  2687. }
  2688. if manufacturer > 0 {
  2689. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2690. }
  2691. if order_type > 0 {
  2692. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2693. }
  2694. if storehouse_id > 0 {
  2695. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2696. }
  2697. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2698. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2699. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2700. db = db.Count(&total)
  2701. offset := (page - 1) * limit
  2702. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2703. return
  2704. }
  2705. }
  2706. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2707. db := readDb.Model(&models.WarehouseOutInfo{})
  2708. 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)
  2709. if len(keywords) > 0 {
  2710. likeKey := "%" + keywords + "%"
  2711. 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)
  2712. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2713. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2714. 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")
  2715. } else {
  2716. 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)
  2717. }
  2718. if manufacturer > 0 {
  2719. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2720. }
  2721. if order_type > 0 {
  2722. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2723. }
  2724. if storehouse_id > 0 {
  2725. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2726. }
  2727. type PriceAmount struct {
  2728. Total float64
  2729. }
  2730. var result []PriceAmount
  2731. err = db.Select("(count * price) as total").Scan(&result).Error
  2732. var total float64
  2733. for _, value := range result {
  2734. total = total + value.Total
  2735. }
  2736. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2737. fmt.Println(total)
  2738. return err, total
  2739. }
  2740. 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) {
  2741. db := readDb.Model(&models.SalesReturnInfo{})
  2742. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2743. if len(keywords) > 0 {
  2744. likeKey := "%" + keywords + "%"
  2745. 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)
  2746. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2747. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2748. 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")
  2749. } else {
  2750. 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)
  2751. }
  2752. if manufacturer > 0 {
  2753. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2754. }
  2755. if order_type > 0 {
  2756. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2757. }
  2758. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2759. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2760. offset := (page - 1) * limit
  2761. db = db.Count(&total)
  2762. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2763. return
  2764. }
  2765. 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) {
  2766. db := readDb.Model(&models.CancelStockInfo{})
  2767. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2768. if len(keywords) > 0 {
  2769. likeKey := "%" + keywords + "%"
  2770. 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)
  2771. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2772. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2773. 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")
  2774. } else {
  2775. 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)
  2776. }
  2777. if manufacturer > 0 {
  2778. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2779. }
  2780. if order_type > 0 {
  2781. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2782. }
  2783. if storehouse_id > 0 {
  2784. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  2785. }
  2786. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2787. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2788. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2789. return
  2790. }
  2791. 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) {
  2792. db := readDb.Model(&models.CancelStockInfo{})
  2793. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2794. if len(keywords) > 0 {
  2795. likeKey := "%" + keywords + "%"
  2796. 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)
  2797. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2798. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2799. 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")
  2800. } else {
  2801. 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)
  2802. }
  2803. if manufacturer > 0 {
  2804. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2805. }
  2806. if order_type > 0 {
  2807. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2808. }
  2809. if storehouse_id > 0 {
  2810. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  2811. }
  2812. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2813. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2814. db = db.Count(&total)
  2815. offset := (page - 1) * limit
  2816. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  2817. return
  2818. }
  2819. func FindStockInByDealerId(id int64) (total int64, err error) {
  2820. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2821. return
  2822. }
  2823. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2824. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2825. return
  2826. }
  2827. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2828. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2829. return
  2830. }
  2831. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2832. 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)
  2833. return
  2834. }
  2835. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2836. 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)
  2837. return
  2838. }
  2839. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2840. 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)
  2841. return
  2842. }
  2843. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2844. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2845. return goodType, err
  2846. }
  2847. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2848. 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
  2849. return info, err
  2850. }
  2851. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2852. 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
  2853. return
  2854. }
  2855. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  2856. 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
  2857. return
  2858. }
  2859. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2860. 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
  2861. return
  2862. }
  2863. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  2864. 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
  2865. if err != gorm.ErrRecordNotFound {
  2866. if err != nil {
  2867. tx.Rollback()
  2868. return
  2869. }
  2870. }
  2871. return
  2872. }
  2873. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2874. 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
  2875. return
  2876. }
  2877. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2878. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2879. return
  2880. }
  2881. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2882. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2883. return
  2884. }
  2885. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2886. info := models.WarehouseOutInfo{}
  2887. 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
  2888. if err == gorm.ErrRecordNotFound {
  2889. return nil, err
  2890. }
  2891. if err != nil {
  2892. return nil, err
  2893. }
  2894. return &info, nil
  2895. }
  2896. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2897. info := models.WarehouseOutInfo{}
  2898. 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
  2899. if err == gorm.ErrRecordNotFound {
  2900. return nil, err
  2901. }
  2902. if err != nil {
  2903. return nil, err
  2904. }
  2905. return &info, nil
  2906. }
  2907. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2908. 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
  2909. return err
  2910. }
  2911. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2912. 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
  2913. return err
  2914. }
  2915. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2916. err := writeDb.Create(&info).Error
  2917. return err
  2918. }
  2919. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  2920. err := tx.Create(&info).Error
  2921. if err != nil {
  2922. tx.Rollback()
  2923. return err
  2924. }
  2925. return err
  2926. }
  2927. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  2928. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  2929. return err
  2930. }
  2931. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  2932. err := writeDb.Create(&info).Error
  2933. return err
  2934. }
  2935. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  2936. err := tx.Create(&info).Error
  2937. if err != nil {
  2938. tx.Rollback()
  2939. return err
  2940. }
  2941. return err
  2942. }
  2943. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  2944. ut := writeDb.Begin()
  2945. err := ut.Create(&detail).Error
  2946. if err != nil {
  2947. ut.Rollback()
  2948. return err
  2949. }
  2950. ut.Commit()
  2951. return err
  2952. }
  2953. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  2954. err := writeDb.Create(&detail).Error
  2955. return err
  2956. }
  2957. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  2958. err := tx.Create(&detail).Error
  2959. if err != nil {
  2960. tx.Rollback()
  2961. return err
  2962. }
  2963. return err
  2964. }
  2965. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  2966. 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
  2967. return
  2968. }
  2969. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  2970. 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
  2971. return
  2972. }
  2973. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  2974. 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
  2975. return
  2976. }
  2977. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  2978. info := models.WarehouseOutInfo{}
  2979. 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
  2980. return info, err
  2981. }
  2982. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  2983. 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
  2984. return
  2985. }
  2986. func UpdateStockOutInfoCount(org_id int64, id int64) {
  2987. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  2988. }
  2989. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  2990. db := readDb.Model(&models.AutomaticReduceDetail{})
  2991. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  2992. db = db.Preload("GoodsType", "status = 1")
  2993. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2994. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  2995. db = db.Count(&total)
  2996. err = db.Find(&user).Error
  2997. return
  2998. }
  2999. type DrugAutomaticReduceDetail struct {
  3000. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3001. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3002. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3003. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3004. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3005. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3006. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3007. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3008. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3009. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3010. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3011. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3012. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3013. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3014. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3015. }
  3016. func (DrugAutomaticReduceDetail) TableName() string {
  3017. return "xt_drug_automatic_reduce_detail"
  3018. }
  3019. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3020. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3021. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3022. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3023. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3024. db = db.Count(&total)
  3025. err = db.Find(&user).Error
  3026. return
  3027. }
  3028. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3029. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3030. }
  3031. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3032. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3033. }
  3034. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3035. 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
  3036. return
  3037. }
  3038. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3039. 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
  3040. return
  3041. }
  3042. type VMWarehousingInfo struct {
  3043. ID int64 `gorm:"column:id" json:"id"`
  3044. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3045. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3046. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3047. Status int64 `gorm:"column:status" json:"status"`
  3048. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3049. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3050. }
  3051. func (VMWarehousingInfo) TableName() string {
  3052. return "xt_warehouse_info"
  3053. }
  3054. type GoodInfo struct {
  3055. ID int64 `gorm:"column:id" json:"id"`
  3056. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3057. Status int64 `gorm:"column:status" json:"status"`
  3058. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3059. }
  3060. func (GoodInfo) TableName() string {
  3061. return "xt_good_information"
  3062. }
  3063. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3064. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3065. return
  3066. }
  3067. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3068. 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
  3069. return
  3070. }
  3071. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3072. likeKey := "%" + keyword + "%"
  3073. db := XTReadDB().Model(&goods)
  3074. if len(keyword) > 0 {
  3075. db = db.Where("type_name like ?", likeKey)
  3076. }
  3077. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3078. return goods, err
  3079. }
  3080. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3081. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3082. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3083. return goodstype, err
  3084. }
  3085. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3086. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3087. if orgid > 0 {
  3088. db = db.Where("x.user_org_id = ?", orgid)
  3089. }
  3090. if recordtime > 0 {
  3091. db = db.Where("x.record_date = ?", recordtime)
  3092. }
  3093. if patientId > 0 {
  3094. db = db.Where("x.patient_id = ?", patientId)
  3095. }
  3096. 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
  3097. return prepare, err
  3098. }
  3099. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3100. detail := models.XtAutomaticReduceDetail{}
  3101. 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
  3102. if err == gorm.ErrRecordNotFound {
  3103. return nil, err
  3104. }
  3105. if err != nil {
  3106. return nil, err
  3107. }
  3108. return &detail, nil
  3109. }
  3110. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3111. 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
  3112. return autoMatic, err
  3113. }
  3114. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3115. 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
  3116. return autoMatic, err
  3117. }
  3118. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3119. var manufacturer models.Manufacturer
  3120. var err error
  3121. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3122. if err == gorm.ErrRecordNotFound {
  3123. return nil, err
  3124. }
  3125. if err != nil {
  3126. return nil, err
  3127. }
  3128. return &manufacturer, nil
  3129. }
  3130. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3131. err := XTWriteDB().Create(manufacturer).Error
  3132. return err
  3133. }
  3134. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3135. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3136. return err
  3137. }
  3138. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3139. var good models.GoodInfo
  3140. var err error
  3141. 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
  3142. if err == gorm.ErrRecordNotFound {
  3143. return nil, err
  3144. }
  3145. if err != nil {
  3146. return nil, err
  3147. }
  3148. return &good, nil
  3149. }
  3150. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3151. err := XTWriteDB().Create(&info).Error
  3152. return err
  3153. }
  3154. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3155. 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
  3156. return err
  3157. }
  3158. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3159. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3160. 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
  3161. return houseOutInfo, err
  3162. }
  3163. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3164. key := "%" + keyword + "%"
  3165. if len(keyword) == 0 {
  3166. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3167. } else {
  3168. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3169. }
  3170. return goodInfo, err
  3171. }
  3172. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3173. var drug models.BaseDrugLib
  3174. var err error
  3175. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3176. if err == gorm.ErrRecordNotFound {
  3177. return nil, err
  3178. }
  3179. if err != nil {
  3180. return nil, err
  3181. }
  3182. return &drug, nil
  3183. }
  3184. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3185. var drug models.BaseDrugLib
  3186. var err error
  3187. 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
  3188. if err == gorm.ErrRecordNotFound {
  3189. return nil, err
  3190. }
  3191. if err != nil {
  3192. return nil, err
  3193. }
  3194. return &drug, nil
  3195. }
  3196. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3197. err := XTWriteDB().Create(&info).Error
  3198. return err
  3199. }
  3200. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3201. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  3202. return err
  3203. }
  3204. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3205. var projoect models.HisProject
  3206. var err error
  3207. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3208. if err == gorm.ErrRecordNotFound {
  3209. return nil, err
  3210. }
  3211. if err != nil {
  3212. return nil, err
  3213. }
  3214. return &projoect, nil
  3215. }
  3216. func CreateProjectInfomation(info *models.HisProject) error {
  3217. err := XTWriteDB().Create(&info).Error
  3218. return err
  3219. }
  3220. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3221. 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
  3222. return err
  3223. }
  3224. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3225. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3226. return warehouseinfo, err
  3227. }
  3228. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3229. 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
  3230. return err
  3231. }
  3232. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3233. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3234. return list, err
  3235. }
  3236. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3237. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3238. return list, err
  3239. }
  3240. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3241. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3242. return list, err
  3243. }
  3244. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3245. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3246. // return list, err
  3247. // }
  3248. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3249. var dealer models.Dealer
  3250. var err error
  3251. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3252. if err == gorm.ErrRecordNotFound {
  3253. return nil, err
  3254. }
  3255. if err != nil {
  3256. return nil, err
  3257. }
  3258. return &dealer, nil
  3259. }
  3260. func CreateDealer(dealer *models.Dealer) error {
  3261. err := XTWriteDB().Create(&dealer).Error
  3262. return err
  3263. }
  3264. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3265. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3266. return err
  3267. }
  3268. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3269. dataconfig := models.DictDataconfig{}
  3270. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3271. return dataconfig, err
  3272. }
  3273. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3274. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3275. return dataconfig, err
  3276. }
  3277. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3278. var dataconfig models.DictDataconfig
  3279. var err error
  3280. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3281. if err == gorm.ErrRecordNotFound {
  3282. return nil, err
  3283. }
  3284. if err != nil {
  3285. return nil, err
  3286. }
  3287. return &dataconfig, nil
  3288. }
  3289. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3290. dataconfig := models.DictDataconfig{}
  3291. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3292. return dataconfig, err
  3293. }
  3294. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3295. err := XTWriteDB().Create(&dataconfig).Error
  3296. return err
  3297. }
  3298. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3299. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3300. return err
  3301. }
  3302. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3303. var goodType models.GoodsType
  3304. var err error
  3305. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3306. if err == gorm.ErrRecordNotFound {
  3307. return nil, err
  3308. }
  3309. if err != nil {
  3310. return nil, err
  3311. }
  3312. return &goodType, nil
  3313. }
  3314. func CreatedGoodType(goodsType *models.GoodsType) error {
  3315. err := XTWriteDB().Create(&goodsType).Error
  3316. return err
  3317. }
  3318. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3319. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3320. return goodsType, err
  3321. }
  3322. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3323. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3324. return goodsType, err
  3325. }
  3326. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3327. var department models.XtHisDepartment
  3328. var err error
  3329. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3330. if err == gorm.ErrRecordNotFound {
  3331. return nil, err
  3332. }
  3333. if err != nil {
  3334. return nil, err
  3335. }
  3336. return &department, nil
  3337. }
  3338. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3339. dataconfig := models.Dataconfig{}
  3340. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3341. return dataconfig, err
  3342. }
  3343. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3344. var dataconfig models.Dataconfig
  3345. var err error
  3346. 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
  3347. if err == gorm.ErrRecordNotFound {
  3348. return nil, err
  3349. }
  3350. if err != nil {
  3351. return nil, err
  3352. }
  3353. return &dataconfig, nil
  3354. }
  3355. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3356. dataconfig := models.Dataconfig{}
  3357. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3358. return dataconfig, err
  3359. }
  3360. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3361. err := XTWriteDB().Create(&dataconfig).Error
  3362. return err
  3363. }
  3364. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3365. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3366. return dataconfig, err
  3367. }
  3368. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3369. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3370. return list, err
  3371. }
  3372. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3373. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3374. if len(keywords) > 0 {
  3375. likekey := "%" + keywords + "%"
  3376. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3377. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3378. }).Find(&drug).Error
  3379. } else {
  3380. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3381. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3382. }).Find(&drug).Error
  3383. }
  3384. return
  3385. }
  3386. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3387. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3388. if len(keywords) > 0 {
  3389. likekey := "%" + keywords + "%"
  3390. 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 {
  3391. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3392. }).Find(&drug).Error
  3393. } else {
  3394. 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 {
  3395. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3396. }).Find(&drug).Error
  3397. }
  3398. return
  3399. }
  3400. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3401. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3402. if len(keywords) > 0 {
  3403. likekey := "%" + keywords + "%"
  3404. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3405. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3406. }).Find(&good).Error
  3407. } else {
  3408. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3409. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3410. }).Find(&good).Error
  3411. }
  3412. return
  3413. }
  3414. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3415. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3416. if len(keywords) > 0 {
  3417. likekey := "%" + keywords + "%"
  3418. 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 {
  3419. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3420. }).Find(&good).Error
  3421. } else {
  3422. 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 {
  3423. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3424. }).Find(&good).Error
  3425. }
  3426. return
  3427. }
  3428. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3429. db := writeDb.Begin()
  3430. err := db.Create(&detail).Error
  3431. if err != nil {
  3432. db.Rollback()
  3433. return err
  3434. }
  3435. db.Commit()
  3436. return err
  3437. }
  3438. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3439. var autoreduece models.BloodAutomaticReduceDetail
  3440. var err error
  3441. 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
  3442. if err == gorm.ErrRecordNotFound {
  3443. return nil, err
  3444. }
  3445. if err != nil {
  3446. return nil, err
  3447. }
  3448. return &autoreduece, nil
  3449. }
  3450. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3451. auto := models.BloodAutomaticReduceDetail{}
  3452. 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
  3453. return auto, err
  3454. }
  3455. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3456. detail := models.BloodAutomaticReduceDetail{}
  3457. 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
  3458. return err
  3459. }
  3460. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3461. 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
  3462. return stockflow, err
  3463. }
  3464. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3465. 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
  3466. 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
  3467. 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
  3468. return err
  3469. }
  3470. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3471. detail := models.BloodAutomaticReduceDetail{}
  3472. 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
  3473. info := models.WarehouseOutInfo{}
  3474. 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
  3475. prepare := models.DialysisBeforePrepare{}
  3476. 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
  3477. flow := models.VmStockFlow{}
  3478. 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
  3479. return err
  3480. }
  3481. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3482. detail := models.BloodAutomaticReduceDetail{}
  3483. 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
  3484. info := models.WarehouseOutInfo{}
  3485. 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
  3486. prepare := models.DialysisBeforePrepare{}
  3487. 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
  3488. flow := models.VmStockFlow{}
  3489. 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
  3490. return err
  3491. }
  3492. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3493. detail := models.BloodAutomaticReduceDetail{}
  3494. 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
  3495. info := models.WarehouseOutInfo{}
  3496. 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
  3497. prepare := models.DialysisBeforePrepare{}
  3498. 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
  3499. flow := models.VmStockFlow{}
  3500. 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
  3501. return err
  3502. }
  3503. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3504. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3505. //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
  3506. return err
  3507. }
  3508. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3509. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3510. return info, err
  3511. }
  3512. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3513. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3514. return list, err
  3515. }
  3516. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3517. 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
  3518. return err
  3519. }
  3520. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3521. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3522. return err
  3523. }
  3524. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3525. var autoreduece models.BloodAutomaticReduceDetail
  3526. var err error
  3527. 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
  3528. if err == gorm.ErrRecordNotFound {
  3529. return nil, err
  3530. }
  3531. if err != nil {
  3532. return nil, err
  3533. }
  3534. return &autoreduece, nil
  3535. }
  3536. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3537. detail := models.WarehouseOutInfoSeven{}
  3538. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3539. return detail, err
  3540. }
  3541. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3542. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3543. return detail, err
  3544. }
  3545. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3546. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3547. return project, err
  3548. }
  3549. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3550. project := models.HisPrescriptionProject{}
  3551. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3552. return project, err
  3553. }
  3554. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3555. 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
  3556. return auto, err
  3557. }
  3558. func ModefyWarehouseInfo(count int64, id int64) error {
  3559. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3560. return err
  3561. }
  3562. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3563. 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
  3564. 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
  3565. 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
  3566. //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
  3567. return err
  3568. }
  3569. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  3570. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3571. return
  3572. }
  3573. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  3574. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3575. return
  3576. }
  3577. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  3578. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3579. return info, err
  3580. }
  3581. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3582. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3583. return info, err
  3584. }
  3585. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3586. 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
  3587. return info, err
  3588. }
  3589. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3590. offset := (page - 1) * limit
  3591. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3592. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3593. fmt.Println(table)
  3594. if startime > 0 {
  3595. db = db.Where("x.ctime>=?", startime)
  3596. }
  3597. if endtime > 0 {
  3598. db = db.Where("x.ctime<=?", endtime)
  3599. }
  3600. likeKey := "%" + keyword + "%"
  3601. if len(likeKey) > 0 {
  3602. db = db.Where("t.good_name like ?", likeKey)
  3603. }
  3604. if orgid > 0 {
  3605. db = db.Where("x.org_id = ?", orgid)
  3606. }
  3607. if good_type > 0 {
  3608. db = db.Where("t.good_type_id = ?", good_type)
  3609. }
  3610. 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
  3611. return info, total, err
  3612. }
  3613. 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) {
  3614. offset := (page - 1) * limit
  3615. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  3616. likeKey := "%" + keyword + "%"
  3617. if len(likeKey) > 0 {
  3618. db = db.Where("x.good_name like ?", likeKey)
  3619. }
  3620. if orgid > 0 {
  3621. db = db.Where("x.org_id = ?", orgid)
  3622. }
  3623. if good_type > 0 {
  3624. db = db.Where("x.good_type_id = ?", good_type)
  3625. }
  3626. if good_id > 0 {
  3627. db = db.Where("x.id = ?", good_id)
  3628. }
  3629. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3630. if startime > 0 {
  3631. db = db.Where("ctime>=?", startime)
  3632. }
  3633. if endtime > 0 {
  3634. db = db.Where("ctime<=?", endtime)
  3635. }
  3636. if good_id > 0 {
  3637. db = db.Where("good_id = ?", good_id)
  3638. }
  3639. if storehouse_id > 0 {
  3640. db = db.Where("storehouse_id = ?", storehouse_id)
  3641. }
  3642. return db.Where("status = 1 and is_check = 1")
  3643. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3644. if startime > 0 {
  3645. db = db.Where("ctime>=?", startime)
  3646. }
  3647. if endtime > 0 {
  3648. db = db.Where("ctime<=?", endtime)
  3649. }
  3650. if good_id > 0 {
  3651. db = db.Where("good_id = ?", good_id)
  3652. }
  3653. if storehouse_id > 0 {
  3654. db = db.Where("storehouse_id = ?", storehouse_id)
  3655. }
  3656. return db.Where("status = 1").Group("good_id,storehouse_id")
  3657. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3658. if startime > 0 {
  3659. db = db.Where("ctime>=?", startime)
  3660. }
  3661. if endtime > 0 {
  3662. db = db.Where("ctime<=?", endtime)
  3663. }
  3664. return db.Where("status = 1")
  3665. }).Find(&info).Error
  3666. return info, total, err
  3667. }
  3668. 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) {
  3669. offset := (page - 1) * limit
  3670. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  3671. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3672. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  3673. fmt.Println(table, tables)
  3674. if startime > 0 {
  3675. db = db.Where("x.ctime>=?", startime)
  3676. }
  3677. if endtime > 0 {
  3678. db = db.Where("x.ctime<=?", endtime)
  3679. }
  3680. if startfirsttime > 0 {
  3681. db = db.Where("x.expiry_date >=?", startfirsttime)
  3682. }
  3683. if endfirsttime > 0 {
  3684. db = db.Where("x.expiry_date <=?", endfirsttime)
  3685. }
  3686. if orgid > 0 {
  3687. db = db.Where("x.org_id = ?", orgid)
  3688. }
  3689. if good_id > 0 {
  3690. db = db.Where("x.good_id = ?", good_id)
  3691. }
  3692. 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
  3693. return info, total, err
  3694. }
  3695. 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) {
  3696. offset := (page - 1) * limit
  3697. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  3698. if startime > 0 {
  3699. db = db.Where("x.ctime>=?", startime)
  3700. }
  3701. if endtime > 0 {
  3702. db = db.Where("x.ctime<=?", endtime)
  3703. }
  3704. if orgid > 0 {
  3705. db = db.Where("x.org_id = ?", orgid)
  3706. }
  3707. if good_id > 0 {
  3708. db = db.Where("x.good_id = ?", good_id)
  3709. }
  3710. if is_sys == 2 {
  3711. db = db.Where("x.is_sys = 1")
  3712. }
  3713. if is_sys == 3 {
  3714. db = db.Where("x.is_sys = 0")
  3715. }
  3716. 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
  3717. return info, total, err
  3718. }
  3719. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3720. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3721. if startime > 0 {
  3722. db = db.Where("x.ctime >=?", startime)
  3723. }
  3724. if endtime > 0 {
  3725. db = db.Where("x.ctime<=?", endtime)
  3726. }
  3727. if orgid > 0 {
  3728. db = db.Where("x.org_id = ?", orgid)
  3729. }
  3730. 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
  3731. return info, err
  3732. }
  3733. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3734. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3735. if startime > 0 {
  3736. db = db.Where("x.ctime >=?", startime)
  3737. }
  3738. if endtime > 0 {
  3739. db = db.Where("x.ctime<=?", endtime)
  3740. }
  3741. if orgid > 0 {
  3742. db = db.Where("x.org_id = ?", orgid)
  3743. }
  3744. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  3745. return info, err
  3746. }
  3747. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3748. if endtime > 0 {
  3749. 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
  3750. }
  3751. if endtime == 0 {
  3752. 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
  3753. }
  3754. return autoMatic, err
  3755. }
  3756. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  3757. out := models.WarehouseOut{}
  3758. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  3759. return out, err
  3760. }
  3761. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3762. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3763. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3764. fmt.Println(table)
  3765. if id > 0 {
  3766. db = db.Where("x.warehouse_out_id = ?", id)
  3767. }
  3768. if orgid > 0 {
  3769. db = db.Where("x.org_id = ?", orgid)
  3770. }
  3771. 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
  3772. return out, err
  3773. }
  3774. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  3775. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  3776. if len(id) > 0 {
  3777. db = db.Where("x.id in(?)", id)
  3778. }
  3779. if orgid > 0 {
  3780. db = db.Where("x.org_id= ?", orgid)
  3781. }
  3782. 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
  3783. return out, err
  3784. }
  3785. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3786. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3787. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3788. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  3789. fmt.Println(table, tableOne)
  3790. if len(id) > 0 {
  3791. db = db.Where("x.warehouse_out_id in (?)", id)
  3792. }
  3793. if orgid > 0 {
  3794. db = db.Where("x.org_id = ?", orgid)
  3795. }
  3796. 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").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
  3797. return out, err
  3798. }
  3799. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3800. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3801. if len(id) > 0 {
  3802. db = db.Where("warehouse_out_id in (?)", id)
  3803. }
  3804. if orgid > 0 {
  3805. db = db.Where("user_org_id = ?", orgid)
  3806. }
  3807. err = db.Group("warehousing_id").Find(&list).Error
  3808. return list, err
  3809. }
  3810. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3811. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3812. if len(id) > 0 {
  3813. db = db.Where("warehouse_out_id in (?)", id)
  3814. }
  3815. if orgid > 0 {
  3816. db = db.Where("user_org_id = ?", orgid)
  3817. }
  3818. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3819. return list, err
  3820. }
  3821. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3822. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  3823. if len(id) > 0 {
  3824. db = db.Where("warehouse_out_id in (?)", id)
  3825. }
  3826. if orgid > 0 {
  3827. db = db.Where("user_org_id = ?", orgid)
  3828. }
  3829. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3830. return list, err
  3831. }
  3832. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  3833. db := XTReadDB().Model(&info)
  3834. db = db.Preload("GoodInfo", "status = 1")
  3835. 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
  3836. return info, err
  3837. }
  3838. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3839. db := readDb.Model(&models.AutomaticReduceDetail{})
  3840. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3841. db = db.Preload("GoodsType", "status = 1")
  3842. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3843. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  3844. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  3845. db = db.Count(&total)
  3846. err = db.Find(&user).Error
  3847. return
  3848. }
  3849. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  3850. info := models.GoodInfo{}
  3851. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3852. return info, err
  3853. }
  3854. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  3855. info := models.GoodInformationThirty{}
  3856. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3857. return info, err
  3858. }
  3859. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  3860. info := models.GoodInformationThirty{}
  3861. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  3862. if err != gorm.ErrRecordNotFound {
  3863. if err != nil {
  3864. tx.Rollback()
  3865. return info, err
  3866. }
  3867. }
  3868. return info, err
  3869. }
  3870. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  3871. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  3872. return err
  3873. }
  3874. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  3875. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3876. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3877. fmt.Println(table)
  3878. if orgid > 0 {
  3879. db = db.Where("x.org_id = ?", orgid)
  3880. }
  3881. if len(orderid) > 0 {
  3882. db = db.Where("x.warehousing_id in(?)", orderid)
  3883. }
  3884. if startime > 0 {
  3885. db = db.Where("x.ctime>=?", startime)
  3886. }
  3887. if endtime > 0 {
  3888. db = db.Where("x.ctime<=?", endtime)
  3889. }
  3890. 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
  3891. return info, err
  3892. }
  3893. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  3894. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3895. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3896. fmt.Println(table)
  3897. if orgid > 0 {
  3898. db = db.Where("x.org_id = ?", orgid)
  3899. }
  3900. if len(orderid) > 0 {
  3901. db = db.Where("x.warehousing_id in(?)", orderid)
  3902. }
  3903. if startime > 0 {
  3904. db = db.Where("x.ctime>=?", startime)
  3905. }
  3906. if endtime > 0 {
  3907. db = db.Where("x.ctime<=?", endtime)
  3908. }
  3909. if storehouse_id > 0 {
  3910. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3911. }
  3912. 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
  3913. return info, err
  3914. }
  3915. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3916. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3917. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  3918. fmt.Println(table)
  3919. if startime > 0 {
  3920. db = db.Where("x.ctime>=?", startime)
  3921. }
  3922. if endtime > 0 {
  3923. db = db.Where("x.ctime<=?", endtime)
  3924. }
  3925. if orgid > 0 {
  3926. db = db.Where("x.org_id = ?", orgid)
  3927. }
  3928. 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
  3929. return info, err
  3930. }
  3931. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  3932. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  3933. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3934. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  3935. fmt.Println(table, tableOne)
  3936. if orgid > 0 {
  3937. db = db.Where("x.org_id = ?", orgid)
  3938. }
  3939. if len(ids) > 0 {
  3940. db = db.Where("x.warehouse_out_id in (?)", ids)
  3941. }
  3942. if startime > 0 {
  3943. db = db.Where("x.ctime>=?", startime)
  3944. }
  3945. if endtime > 0 {
  3946. db = db.Where("x.ctime<=?", endtime)
  3947. }
  3948. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,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
  3949. return out, err
  3950. }
  3951. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  3952. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  3953. if len(ids) > 0 {
  3954. db = db.Where("x.id in(?)", ids)
  3955. }
  3956. if orgid > 0 {
  3957. db = db.Where("x.org_id = ?", orgid)
  3958. }
  3959. 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
  3960. return info, err
  3961. }
  3962. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3963. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  3964. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  3965. fmt.Println(table)
  3966. if len(ids) > 0 {
  3967. db = db.Where("x.warehouse_out_id in(?)", ids)
  3968. }
  3969. if orgid > 0 {
  3970. db = db.Where("x.org_id = ?", orgid)
  3971. }
  3972. 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
  3973. return info, err
  3974. }
  3975. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  3976. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  3977. likeKey := "%" + keyword + "%"
  3978. if len(keyword) > 0 {
  3979. db = db.Where("x.drug_name like ?", likeKey)
  3980. }
  3981. if orgid > 0 {
  3982. db = db.Where("x.org_id = ?", orgid)
  3983. }
  3984. err = db.Find(&basedrug).Error
  3985. return basedrug, err
  3986. }
  3987. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  3988. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3989. if len(ids) > 0 {
  3990. db = db.Where("x.drug_id in(?)", ids)
  3991. }
  3992. if orgid > 0 {
  3993. db = db.Where("x.org_id = ?", orgid)
  3994. }
  3995. if startime > 0 {
  3996. db = db.Where("x.ctime>=?", startime)
  3997. }
  3998. if endtime > 0 {
  3999. db = db.Where("x.ctime<=?", endtime)
  4000. }
  4001. if storehouse_id > 0 {
  4002. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4003. }
  4004. err = db.Find(&info).Error
  4005. return info, err
  4006. }
  4007. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4008. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4009. if len(ids) > 0 {
  4010. db = db.Where("x.drug_id in(?)", ids)
  4011. }
  4012. if orgid > 0 {
  4013. db = db.Where("x.org_id = ?", orgid)
  4014. }
  4015. if startime > 0 {
  4016. db = db.Where("x.ctime>=?", startime)
  4017. }
  4018. if endtime > 0 {
  4019. db = db.Where("x.ctime<=?", endtime)
  4020. }
  4021. if storehouse_id > 0 {
  4022. db = db.Where("storehouse_id = ?", storehouse_id)
  4023. }
  4024. err = db.Find(&info).Error
  4025. return info, err
  4026. }
  4027. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4028. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4029. if drugid > 0 {
  4030. db = db.Where("x.drug_id = ?", drugid)
  4031. }
  4032. if orgid > 0 {
  4033. db = db.Where("x.org_id = ?", orgid)
  4034. }
  4035. if storehouse_id > 0 {
  4036. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4037. }
  4038. 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
  4039. return info, err
  4040. }
  4041. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4042. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4043. if drugid > 0 {
  4044. db = db.Where("x.drug_id = ?", drugid)
  4045. }
  4046. if orgid > 0 {
  4047. db = db.Where("x.org_id = ?", orgid)
  4048. }
  4049. if storehouse_id > 0 {
  4050. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4051. }
  4052. 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
  4053. if err != gorm.ErrRecordNotFound {
  4054. if err != nil {
  4055. tx.Rollback()
  4056. return info, err
  4057. }
  4058. }
  4059. return info, err
  4060. }
  4061. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4062. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4063. if drugid > 0 {
  4064. db = db.Where("x.drug_id = ?", drugid)
  4065. }
  4066. if orgid > 0 {
  4067. db = db.Where("x.org_id = ?", orgid)
  4068. }
  4069. if storehouse_id > 0 {
  4070. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4071. }
  4072. 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
  4073. return info, err
  4074. }
  4075. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4076. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4077. if id > 0 {
  4078. db = db.Where("x.id = ?", id)
  4079. }
  4080. if orgid > 0 {
  4081. db = db.Where("x.org_id = ?", orgid)
  4082. }
  4083. 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
  4084. return info, err
  4085. }
  4086. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4087. info := models.VmDrugWarehouseInfo{}
  4088. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4089. if id == 0 {
  4090. db = db.Where("id = ?", id)
  4091. }
  4092. if drugid > 0 {
  4093. db = db.Where("x.drug_id = ?", drugid)
  4094. }
  4095. if orgid > 0 {
  4096. db = db.Where("x.org_id = ?", orgid)
  4097. }
  4098. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4099. return info, err
  4100. }
  4101. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4102. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4103. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4104. fmt.Println(table)
  4105. if drugid > 0 {
  4106. db = db.Where("x.drug_id = ?", drugid)
  4107. }
  4108. if storehouse_id > 0 {
  4109. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4110. }
  4111. 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
  4112. return list, err
  4113. }
  4114. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4115. lib := models.BaseDrugLib{}
  4116. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4117. return lib, err
  4118. }
  4119. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4120. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4121. if goodid > 0 {
  4122. db = db.Where("x.good_id = ?", goodid)
  4123. }
  4124. if orgid > 0 {
  4125. db = db.Where("x.org_id = ?", orgid)
  4126. }
  4127. 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
  4128. return info, err
  4129. }
  4130. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4131. 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
  4132. return err
  4133. }
  4134. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4135. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4136. return info, err
  4137. }
  4138. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4139. //遍历循环入库数据
  4140. for i, item := range list {
  4141. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4142. if (item.WarehousingCount - item.StockCount) >= count {
  4143. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4144. info := models.WarehousingInfo{
  4145. GoodId: item.GoodId,
  4146. OrgId: item.OrgId,
  4147. WarehousingId: item.WarehousingId,
  4148. StockCount: item.StockCount + count,
  4149. }
  4150. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4151. fmt.Println("2323233232323232323232323232323", err2)
  4152. return err
  4153. }
  4154. if (item.WarehousingCount - item.StockCount) < count {
  4155. //如果该批次小于退库数量,则先出库该相差数量
  4156. var countOne = item.WarehousingCount - item.StockCount
  4157. infoOne := models.WarehousingInfo{
  4158. GoodId: item.GoodId,
  4159. OrgId: item.OrgId,
  4160. WarehousingId: item.WarehousingId,
  4161. StockCount: countOne + item.StockCount,
  4162. }
  4163. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4164. if err == nil {
  4165. //剩余的出库到第二个批次
  4166. var countTwo = count - countOne
  4167. infos := append(list, list[i+1])
  4168. var idArray []*models.WarehousingInfo
  4169. for _, it := range infos {
  4170. if it.WarehousingId != item.WarehousingId {
  4171. idArray = append(idArray, it)
  4172. }
  4173. }
  4174. //查询更新后的库存
  4175. UpdateCancelOut(idArray, countTwo)
  4176. return err
  4177. } else {
  4178. return err
  4179. }
  4180. }
  4181. }
  4182. return err
  4183. }
  4184. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4185. if len(idstr) > 0 {
  4186. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4187. } else {
  4188. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4189. }
  4190. return info, err
  4191. }
  4192. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4193. if len(idstr) > 0 {
  4194. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4195. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4196. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4197. }).Find(&info).Error
  4198. }
  4199. return info, err
  4200. }
  4201. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4202. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4203. if startime > 0 {
  4204. db = db.Where("x.ctime >=?", startime)
  4205. }
  4206. if endtime > 0 {
  4207. db = db.Where("x.ctime <= ?", endtime)
  4208. }
  4209. if orgid > 0 {
  4210. db = db.Where("x.org_id = ?", orgid)
  4211. }
  4212. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4213. return info, err
  4214. }
  4215. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4216. info := models.WarehouseOutInfo{}
  4217. var err error
  4218. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4219. if err == gorm.ErrRecordNotFound {
  4220. return nil, err
  4221. }
  4222. if err != nil {
  4223. return nil, err
  4224. }
  4225. return &info, nil
  4226. }
  4227. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4228. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4229. if goodid > 0 {
  4230. db = db.Where("x.good_id = ?", goodid)
  4231. }
  4232. if orgid > 0 {
  4233. db = db.Where("x.org_id = ?", orgid)
  4234. }
  4235. if id > 0 {
  4236. db = db.Where("x.warehouse_info_id = ?", id)
  4237. }
  4238. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4239. return info, err
  4240. }
  4241. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4242. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4243. if goodid > 0 {
  4244. db = db.Where("x.good_id = ?", goodid)
  4245. }
  4246. if orgid > 0 {
  4247. db = db.Where("x.user_org_id = ?", orgid)
  4248. }
  4249. if id > 0 {
  4250. db = db.Where("x.warehousing_id = ?", id)
  4251. }
  4252. if storehouse_id > 0 {
  4253. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4254. }
  4255. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4256. return info, err
  4257. }
  4258. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4259. stock := models.CancelStock{}
  4260. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4261. return stock, err
  4262. }
  4263. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4264. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4265. return info, err
  4266. }
  4267. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4268. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4269. return info, err
  4270. }
  4271. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4272. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4273. return stock, err
  4274. }
  4275. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4276. 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)")
  4277. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4278. fmt.Println(table)
  4279. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4280. fmt.Println(tableone)
  4281. if id > 0 {
  4282. db = db.Where("x.warehouse_out_id = ?", id)
  4283. }
  4284. if goodid > 0 {
  4285. db = db.Where("x.good_id = ?", goodid)
  4286. }
  4287. 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
  4288. return stock, err
  4289. }
  4290. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4291. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4292. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4293. fmt.Println(table)
  4294. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4295. fmt.Println(tableone)
  4296. if id > 0 {
  4297. db = db.Where("x.warehouse_out_id = ?", id)
  4298. }
  4299. if goodid > 0 {
  4300. db = db.Where("x.good_id = ?", goodid)
  4301. }
  4302. 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
  4303. return stock, err
  4304. }
  4305. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4306. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4307. return info, err
  4308. }
  4309. func ExportDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseInfo, err error) {
  4310. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1")
  4311. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4312. tableOne := XTReadDB().Table("xt_drug_warehouse as p")
  4313. fmt.Println("table", table)
  4314. fmt.Println("tableOne", tableOne)
  4315. if len(warehouse_out_id) > 0 {
  4316. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4317. }
  4318. 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
  4319. return out, err
  4320. }
  4321. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4322. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4323. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4324. fmt.Println("table", table)
  4325. if len(warehouse_out_id) > 0 {
  4326. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4327. }
  4328. 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
  4329. return out, err
  4330. }
  4331. func GetExportOutOrderDrugListOne(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4332. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1")
  4333. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4334. fmt.Println("table", table)
  4335. if len(warehouse_out_id) > 0 {
  4336. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4337. }
  4338. 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
  4339. return out, err
  4340. }
  4341. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4342. db := XTReadDB().Model(&info).Where("status = 1")
  4343. if len(warehouse_out_id) > 0 {
  4344. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4345. }
  4346. if orgid > 0 {
  4347. db = db.Where("org_id = ?", orgid)
  4348. }
  4349. err = db.Find(&info).Error
  4350. return info, err
  4351. }
  4352. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4353. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4354. return out, err
  4355. }
  4356. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4357. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4358. 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)
  4359. }).Find(&out).Error
  4360. return out, err
  4361. }
  4362. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4363. info := models.WarehouseOutInfo{}
  4364. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4365. return info, err
  4366. }
  4367. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4368. info := models.WarehousingInfo{}
  4369. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4370. if goodid > 0 {
  4371. db = db.Where("x.good_id = ?", goodid)
  4372. }
  4373. if storehouse_id > 0 {
  4374. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4375. }
  4376. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4377. return info, err
  4378. }
  4379. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4380. info := models.WarehousingInfo{}
  4381. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4382. return info, err
  4383. }
  4384. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4385. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4386. return err
  4387. }
  4388. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4389. if storehouse_id == 0 {
  4390. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4391. }
  4392. if storehouse_id > 0 {
  4393. 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
  4394. }
  4395. return info, err
  4396. }
  4397. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4398. if len(warehousingInfo) > 0 {
  4399. utx := writeDb.Begin()
  4400. if len(warehousingInfo) > 0 {
  4401. 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 "
  4402. insertParams := make([]string, 0)
  4403. insertData := make([]interface{}, 0)
  4404. for _, info := range warehousingInfo {
  4405. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4406. insertData = append(insertData, info.WarehousingId)
  4407. insertData = append(insertData, info.GoodId)
  4408. insertData = append(insertData, info.Number)
  4409. insertData = append(insertData, info.LicenseNumber)
  4410. insertData = append(insertData, info.Count)
  4411. insertData = append(insertData, info.UserOrgId)
  4412. insertData = append(insertData, info.PatientId)
  4413. insertData = append(insertData, info.SystemTime)
  4414. insertData = append(insertData, info.ConsumableType)
  4415. insertData = append(insertData, info.IsSys)
  4416. insertData = append(insertData, info.WarehousingOrder)
  4417. insertData = append(insertData, info.WarehouseOutId)
  4418. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4419. insertData = append(insertData, info.IsEdit)
  4420. insertData = append(insertData, info.CancelStockId)
  4421. insertData = append(insertData, info.CancelOrderNumber)
  4422. insertData = append(insertData, info.Manufacturer)
  4423. insertData = append(insertData, info.Dealer)
  4424. insertData = append(insertData, info.Creator)
  4425. insertData = append(insertData, info.UpdateCreator)
  4426. insertData = append(insertData, info.Status)
  4427. insertData = append(insertData, info.Ctime)
  4428. insertData = append(insertData, info.Mtime)
  4429. insertData = append(insertData, info.Price)
  4430. insertData = append(insertData, info.WarehousingDetailId)
  4431. insertData = append(insertData, info.WarehouseOutDetailId)
  4432. insertData = append(insertData, info.CancelOutDetailId)
  4433. insertData = append(insertData, info.ExpireDate)
  4434. insertData = append(insertData, info.ProductDate)
  4435. }
  4436. thisSQL += strings.Join(insertParams, ", ")
  4437. err = utx.Exec(thisSQL, insertData...).Error
  4438. if err != nil {
  4439. utx.Rollback()
  4440. return
  4441. }
  4442. }
  4443. utx.Commit()
  4444. }
  4445. return
  4446. }
  4447. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4448. utx := XTWriteDB().Begin()
  4449. err := utx.Create(&flow).Error
  4450. if err != nil {
  4451. utx.Rollback()
  4452. return err
  4453. }
  4454. utx.Commit()
  4455. return err
  4456. }
  4457. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4458. err := tx.Create(&flow).Error
  4459. if err != nil {
  4460. tx.Rollback()
  4461. return err
  4462. }
  4463. return err
  4464. }
  4465. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4466. err := XTWriteDB().Create(&flow).Error
  4467. return err
  4468. }
  4469. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4470. err := XTWriteDB().Save(&flow).Error
  4471. return err
  4472. }
  4473. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4474. 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
  4475. return err
  4476. }
  4477. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  4478. 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
  4479. return err
  4480. }
  4481. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  4482. stock := models.VmStockFlow{}
  4483. var err error
  4484. 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
  4485. if err == gorm.ErrRecordNotFound {
  4486. return nil, err
  4487. }
  4488. if err != nil {
  4489. return nil, err
  4490. }
  4491. return &stock, nil
  4492. }
  4493. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4494. 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
  4495. return flow, nil
  4496. }
  4497. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4498. 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
  4499. return flow, nil
  4500. }
  4501. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4502. 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
  4503. if err != gorm.ErrRecordNotFound {
  4504. if err != nil {
  4505. tx.Rollback()
  4506. return flow, nil
  4507. }
  4508. }
  4509. return flow, nil
  4510. }
  4511. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4512. 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
  4513. return flow, nil
  4514. }
  4515. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  4516. 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
  4517. if err != gorm.ErrRecordNotFound {
  4518. if err != nil {
  4519. tx.Rollback()
  4520. return flow, nil
  4521. }
  4522. }
  4523. return flow, nil
  4524. }
  4525. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  4526. 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
  4527. return flow, nil
  4528. }
  4529. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  4530. 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
  4531. if err != gorm.ErrRecordNotFound {
  4532. if err != nil {
  4533. tx.Rollback()
  4534. return flow, err
  4535. }
  4536. }
  4537. return flow, err
  4538. }
  4539. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4540. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  4541. return flow, nil
  4542. }
  4543. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4544. 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
  4545. return flow, nil
  4546. }
  4547. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4548. 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
  4549. return flow, nil
  4550. }
  4551. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4552. 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
  4553. if err != gorm.ErrRecordNotFound {
  4554. if err != nil {
  4555. tx.Rollback()
  4556. return flow, nil
  4557. }
  4558. }
  4559. return flow, nil
  4560. }
  4561. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4562. 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
  4563. return flow, nil
  4564. }
  4565. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  4566. info := models.WarehouseOutInfo{}
  4567. var err error
  4568. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4569. if err == gorm.ErrRecordNotFound {
  4570. return nil, err
  4571. }
  4572. if err != nil {
  4573. return nil, err
  4574. }
  4575. return &info, nil
  4576. }
  4577. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  4578. info := models.WarehouseOutInfo{}
  4579. var err error
  4580. 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
  4581. if err == gorm.ErrRecordNotFound {
  4582. return nil, err
  4583. }
  4584. if err != nil {
  4585. return nil, err
  4586. }
  4587. return &info, nil
  4588. }
  4589. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  4590. info := models.WarehouseOutInfo{}
  4591. 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
  4592. return info, err
  4593. }
  4594. 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) {
  4595. offset := (page - 1) * limit
  4596. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  4597. if consumable_type > 0 {
  4598. //入库
  4599. if consumable_type == 1 {
  4600. db = db.Where("consumable_type = ?", consumable_type)
  4601. }
  4602. //2 手动出库
  4603. if consumable_type == 2 {
  4604. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  4605. }
  4606. //3.自动出库
  4607. if consumable_type == 3 {
  4608. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  4609. }
  4610. //4.手动退库
  4611. if consumable_type == 4 {
  4612. db = db.Where(" consumable_type = ?", consumable_type)
  4613. }
  4614. //自动退库
  4615. if consumable_type == 7 {
  4616. db = db.Where(" consumable_type = ?", consumable_type)
  4617. }
  4618. if consumable_type == 10 {
  4619. db = db.Where(" consumable_type = ?", consumable_type)
  4620. }
  4621. if consumable_type == 11 {
  4622. db = db.Where(" consumable_type = ?", consumable_type)
  4623. }
  4624. if consumable_type == 12 {
  4625. db = db.Where(" consumable_type = ?", consumable_type)
  4626. }
  4627. if consumable_type == 13 {
  4628. db = db.Where(" consumable_type = ?", consumable_type)
  4629. }
  4630. if consumable_type == 15 {
  4631. db = db.Where(" consumable_type = ?", consumable_type)
  4632. }
  4633. }
  4634. if orgId > 0 {
  4635. db = db.Where("user_org_id = ?", orgId)
  4636. }
  4637. if startTime > 0 {
  4638. db = db.Where("ctime >= ?", startTime)
  4639. }
  4640. if endTime > 0 {
  4641. db = db.Where("ctime<=?", endTime)
  4642. }
  4643. if good_id > 0 {
  4644. db = db.Where("good_id = ?", good_id)
  4645. }
  4646. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  4647. return flow, total, err
  4648. }
  4649. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  4650. detail := models.AutomaticReduceDetail{}
  4651. 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
  4652. return detail, err
  4653. }
  4654. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  4655. detail := models.WarehouseOutInfo{}
  4656. 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
  4657. return detail, err
  4658. }
  4659. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  4660. outInfo := models.WarehouseOutInfo{}
  4661. 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
  4662. return err
  4663. }
  4664. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  4665. outInfo := models.WarehouseOutInfo{}
  4666. 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
  4667. if err != nil {
  4668. tx.Rollback()
  4669. return err
  4670. }
  4671. return err
  4672. }
  4673. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  4674. outInfo := models.WarehouseOutInfo{}
  4675. 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
  4676. return err
  4677. }
  4678. 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 {
  4679. outInfo := models.WarehouseOutInfo{}
  4680. 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
  4681. if err != nil {
  4682. tx.Rollback()
  4683. return err
  4684. }
  4685. return err
  4686. }
  4687. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  4688. info := models.WarehouseOutInfo{}
  4689. 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
  4690. return info, err
  4691. }
  4692. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  4693. info := models.WarehousingInfo{}
  4694. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4695. return info, err
  4696. }
  4697. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  4698. info := models.WarehousingInfo{}
  4699. db := XTReadDB().Model(&info)
  4700. if good_id > 0 {
  4701. db = db.Where("good_id = ? and status = 1", good_id)
  4702. }
  4703. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  4704. return info, err
  4705. }
  4706. 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) {
  4707. offset := (page - 1) * limit
  4708. db := XTReadDB().Model(drug_flow).Where("status = 1")
  4709. if drugid > 0 {
  4710. db = db.Where("drug_id = ?", drugid)
  4711. }
  4712. if orgid > 0 {
  4713. db = db.Where("user_org_id = ?", orgid)
  4714. }
  4715. if startTime > 0 {
  4716. db = db.Where("ctime >=? ", startTime)
  4717. }
  4718. if endTime > 0 {
  4719. db = db.Where("ctime<=?", endTime)
  4720. }
  4721. if stock_type > 0 {
  4722. //手动入库
  4723. if stock_type == 1 {
  4724. db = db.Where("consumable_type = 1")
  4725. }
  4726. //手动出库
  4727. if stock_type == 2 {
  4728. db = db.Where("consumable_type = 2 and is_sys = 0")
  4729. }
  4730. //自动出库
  4731. if stock_type == 3 {
  4732. db = db.Where("consumable_type = 3 and is_sys = 1")
  4733. }
  4734. if stock_type == 4 {
  4735. db = db.Where("consumable_type = 4")
  4736. }
  4737. if stock_type == 10 {
  4738. db = db.Where("consumable_type = 10")
  4739. }
  4740. if stock_type == 11 {
  4741. db = db.Where("consumable_type = 11")
  4742. }
  4743. if stock_type == 12 {
  4744. db = db.Where("consumable_type = 12")
  4745. }
  4746. if stock_type == 13 {
  4747. db = db.Where("consumable_type = 13")
  4748. }
  4749. if stock_type == 15 {
  4750. db = db.Where("consumable_type = 15")
  4751. }
  4752. }
  4753. 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
  4754. return drug_flow, total, err
  4755. }
  4756. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  4757. outInfo := models.DrugWarehouseOutInfo{}
  4758. var err error
  4759. 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
  4760. if err == gorm.ErrRecordNotFound {
  4761. return nil, err
  4762. }
  4763. if err != nil {
  4764. return nil, err
  4765. }
  4766. return &outInfo, nil
  4767. }
  4768. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  4769. 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
  4770. return outInfo, err
  4771. }
  4772. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  4773. outInfo := models.DrugWarehouseOutInfo{}
  4774. 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
  4775. return err
  4776. }
  4777. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  4778. db := XTReadDB().Model(&cancle).Where("status = 1")
  4779. if startime > 0 {
  4780. db = db.Where("ctime >=?", startime)
  4781. }
  4782. if endtime > 0 {
  4783. db = db.Where("ctime <=?", endtime)
  4784. }
  4785. if len(ids) > 0 {
  4786. db = db.Where("cancel_stock_id in(?)", ids)
  4787. }
  4788. if orgid > 0 {
  4789. db = db.Where("org_id = ?", orgid)
  4790. }
  4791. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  4792. err = db.Find(&cancle).Error
  4793. return cancle, err
  4794. }
  4795. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  4796. db := XTReadDB().Model(&info)
  4797. if id > 0 {
  4798. db = db.Where("cancel_stock_id = ? and status = 1", id)
  4799. }
  4800. db = db.Preload("BaseDrugLib")
  4801. err = db.Find(&info).Error
  4802. return info, err
  4803. }
  4804. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  4805. info := models.XtDrugWarehouseInfo{}
  4806. db := XTReadDB().Model(&info).Where("status = 1")
  4807. if id > 0 {
  4808. db = db.Where("id = ?", id)
  4809. }
  4810. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4811. return info, err
  4812. }
  4813. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  4814. // info := models.XtDrugWarehouseInfo{}
  4815. // db := tx.Model(&info).Where("status = 1")
  4816. // if id > 0 {
  4817. // db = db.Where("id = ?", id)
  4818. // }
  4819. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4820. // return info, err
  4821. //}
  4822. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4823. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  4824. return info, err
  4825. }
  4826. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  4827. 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
  4828. return info, err
  4829. }
  4830. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  4831. info := models.HisDoctorAdviceInfo{}
  4832. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  4833. return info, err
  4834. }
  4835. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  4836. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4837. return info, err
  4838. }
  4839. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  4840. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4841. if err != gorm.ErrRecordNotFound {
  4842. if err != nil {
  4843. tx.Rollback()
  4844. return info, err
  4845. }
  4846. }
  4847. return info, err
  4848. }
  4849. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  4850. adviceInfo := models.HisDoctorAdviceInfo{}
  4851. 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
  4852. if err != gorm.ErrRecordNotFound {
  4853. if err != nil {
  4854. tx.Rollback()
  4855. return adviceInfo, err
  4856. }
  4857. }
  4858. return adviceInfo, err
  4859. }
  4860. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4861. info := models.HisDoctorAdviceInfo{}
  4862. 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
  4863. return info, err
  4864. }
  4865. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  4866. detail := models.DrugAutomaticReduceDetail{}
  4867. 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
  4868. return detail, err
  4869. }
  4870. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  4871. 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
  4872. return out, err
  4873. }
  4874. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  4875. 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
  4876. if err != gorm.ErrRecordNotFound {
  4877. if err != nil {
  4878. tx.Rollback()
  4879. return out, err
  4880. }
  4881. }
  4882. return out, err
  4883. }
  4884. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  4885. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4886. return err
  4887. }
  4888. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  4889. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4890. return err
  4891. }
  4892. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  4893. 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
  4894. 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
  4895. 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
  4896. return err
  4897. }
  4898. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  4899. 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
  4900. 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
  4901. return err
  4902. }
  4903. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64, tx *gorm.DB) error {
  4904. err := tx.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
  4905. err = tx.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
  4906. if err != nil {
  4907. tx.Rollback()
  4908. return err
  4909. }
  4910. return err
  4911. }
  4912. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  4913. info := models.XtDrugWarehouseInfo{}
  4914. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  4915. return info, err
  4916. }
  4917. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  4918. info := models.XtDrugWarehouseInfo{}
  4919. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  4920. return info, err
  4921. }
  4922. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  4923. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  4924. return err
  4925. }
  4926. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  4927. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  4928. // return err
  4929. //}
  4930. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  4931. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  4932. //return err
  4933. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  4934. return err
  4935. }
  4936. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  4937. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  4938. // return err
  4939. //}
  4940. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  4941. info := models.DrugCancelStockInfo{}
  4942. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  4943. return info, err
  4944. }
  4945. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  4946. info := models.DrugCancelStockInfo{}
  4947. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  4948. return info, err
  4949. }
  4950. func UpdatedDrugInfo(id int64, count int64) (err error) {
  4951. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  4952. return err
  4953. }
  4954. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  4955. 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
  4956. return err
  4957. }
  4958. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  4959. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  4960. return err
  4961. }
  4962. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  4963. 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
  4964. return err
  4965. }
  4966. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  4967. 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
  4968. return err
  4969. }
  4970. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  4971. 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
  4972. return err
  4973. }
  4974. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  4975. 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
  4976. return err
  4977. }
  4978. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  4979. info := models.WarehousingInfo{}
  4980. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4981. return info, err
  4982. }
  4983. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4984. info := models.WarehousingInfo{}
  4985. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  4986. return info, err
  4987. }
  4988. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4989. info := models.WarehousingInfo{}
  4990. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  4991. return info, err
  4992. }
  4993. func CreateWarehouseOut(out models.WarehouseOut) error {
  4994. err := XTWriteDB().Create(&out).Error
  4995. return err
  4996. }
  4997. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  4998. out := models.WarehouseOut{}
  4999. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5000. return out, err
  5001. }
  5002. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5003. err := XTWriteDB().Create(&info).Error
  5004. return err
  5005. }
  5006. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5007. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5008. return err
  5009. }
  5010. func UpdateWareInfoById(id int64, count int64) error {
  5011. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5012. return err
  5013. }
  5014. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5015. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5016. return err
  5017. }
  5018. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5019. info := models.CancelStockInfo{}
  5020. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5021. return info, err
  5022. }
  5023. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5024. info := models.CancelStockInfo{}
  5025. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5026. return info, err
  5027. }
  5028. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5029. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5030. return info, err
  5031. }
  5032. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5033. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5034. return info, err
  5035. }
  5036. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5037. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5038. return err
  5039. }
  5040. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5041. drugFlow := models.DrugFlow{}
  5042. 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
  5043. return err
  5044. }
  5045. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5046. drugFlow := models.DrugFlow{}
  5047. 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
  5048. return err
  5049. }
  5050. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5051. stock := models.DrugCancelStock{}
  5052. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5053. return stock, err
  5054. }
  5055. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5056. // stock := models.DrugCancelStock{}
  5057. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5058. // return stock, err
  5059. //}
  5060. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5061. db := XTReadDB().Model(&info)
  5062. if warehouse_info_id > 0 {
  5063. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5064. }
  5065. if drug_id > 0 {
  5066. db = db.Where("drug_id = ? and status =1", drug_id)
  5067. }
  5068. err = db.Find(&info).Error
  5069. return info, err
  5070. }
  5071. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5072. // db := tx.Model(&info)
  5073. // if warehouse_info_id > 0 {
  5074. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5075. // }
  5076. // if drug_id > 0 {
  5077. // db = db.Where("drug_id = ? and status =1", drug_id)
  5078. // }
  5079. // err = db.Find(&info).Error
  5080. // return info, err
  5081. //}
  5082. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5083. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5084. return info, err
  5085. }
  5086. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5087. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5088. // return info, err
  5089. //}
  5090. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5091. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5092. return config, err
  5093. }
  5094. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5095. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5096. return config, err
  5097. }
  5098. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5099. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5100. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5101. fmt.Println(table)
  5102. likeKey := "%" + keyword + "%"
  5103. if orgid > 0 {
  5104. db = db.Where("x.org_id = ?", orgid)
  5105. }
  5106. if storehouse_id > 0 {
  5107. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5108. }
  5109. 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
  5110. return info, err
  5111. }
  5112. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5113. err := XTWriteDB().Create(&adjustPrice).Error
  5114. return err
  5115. }
  5116. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5117. likeKey := "%" + keyword + "%"
  5118. offset := (page - 1) * limit
  5119. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5120. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5121. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5122. fmt.Println(table, tab)
  5123. if startime > 0 {
  5124. db = db.Where("x.start_time>=?", startime)
  5125. }
  5126. if endtime > 0 {
  5127. db = db.Where("x.start_time<=?", endtime)
  5128. }
  5129. if len(keyword) > 0 {
  5130. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5131. }
  5132. if orgid > 0 {
  5133. db = db.Where("x.user_org_id = ?", orgid)
  5134. }
  5135. 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
  5136. return list, total, err
  5137. }
  5138. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5139. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5140. return err
  5141. }
  5142. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5143. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5144. return err
  5145. }
  5146. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5147. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5148. return info, err
  5149. }
  5150. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5151. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5152. return adjust, err
  5153. }
  5154. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5155. err := XTWriteDB().Create(&price).Error
  5156. return err
  5157. }
  5158. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5159. likeKey := "%" + keyword + "%"
  5160. offset := (page - 1) * limit
  5161. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5162. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5163. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5164. fmt.Println(table, tab)
  5165. if startime > 0 {
  5166. db = db.Where("x.ctime>=?", startime)
  5167. }
  5168. if endtime > 0 {
  5169. db = db.Where("x.ctime<=?", endtime)
  5170. }
  5171. if len(keyword) > 0 {
  5172. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5173. }
  5174. if orgid > 0 {
  5175. db = db.Where("x.user_org_id = ?", orgid)
  5176. }
  5177. 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
  5178. return adjust, total, err
  5179. }
  5180. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5181. likeKey := "%" + keyword + "%"
  5182. offset := (page - 1) * limit
  5183. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5184. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5185. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5186. fmt.Println(table, tab)
  5187. if len(keyword) > 0 {
  5188. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5189. }
  5190. if orgid > 0 {
  5191. db = db.Where("x.user_org_id = ?", orgid)
  5192. }
  5193. if startime > 0 {
  5194. db = db.Where("x.ctime >=?", startime)
  5195. }
  5196. if endtime > 0 {
  5197. db = db.Where("x.ctime <=?", endtime)
  5198. }
  5199. if storehouse_id > 0 {
  5200. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5201. }
  5202. 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
  5203. return adjust, total, err
  5204. }
  5205. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5206. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5207. if inventory_type == 0 {
  5208. db = db.Where("x.inventory_type = 4")
  5209. }
  5210. if inventory_type > 0 {
  5211. db = db.Where("x.inventory_type = ?", inventory_type)
  5212. }
  5213. if len(keyword) > 0 {
  5214. likeKey := "%" + keyword + "%"
  5215. db = db.Where("x.warehousing_order like ?", likeKey)
  5216. }
  5217. if orgid > 0 {
  5218. db = db.Where("x.user_org_id = ?", orgid)
  5219. }
  5220. 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
  5221. return adjust, err
  5222. }
  5223. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5224. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5225. if orgid > 0 {
  5226. db = db.Where("x.user_org_id = ?", orgid)
  5227. }
  5228. 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
  5229. return adjust, err
  5230. }
  5231. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5232. 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
  5233. return err
  5234. }
  5235. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5236. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5237. return report, err
  5238. }
  5239. func CreateInentory(inventory models.XtStockInventory) error {
  5240. err = XTWriteDB().Create(&inventory).Error
  5241. return err
  5242. }
  5243. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5244. likeKey := "%" + keyword + "%"
  5245. offset := (page - 1) * limit
  5246. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5247. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5248. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5249. fmt.Println(table, tab)
  5250. if startime > 0 {
  5251. db = db.Where("x.ctime>=?", startime)
  5252. }
  5253. if endtime > 0 {
  5254. db = db.Where("x.ctime<=?", endtime)
  5255. }
  5256. if len(keyword) > 0 {
  5257. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5258. }
  5259. if orgid > 0 {
  5260. db = db.Where("x.user_org_id = ?", orgid)
  5261. }
  5262. if inventory_status > 0 {
  5263. db = db.Where("checker_status = ?", inventory_status)
  5264. }
  5265. 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
  5266. return adjust, total, err
  5267. }
  5268. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5269. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5270. return err
  5271. }
  5272. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5273. info := models.XtStockAdjustPrice{}
  5274. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5275. return info, err
  5276. }
  5277. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5278. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5279. return list, err
  5280. }
  5281. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5282. info := models.WarehousingInfo{}
  5283. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5284. return info, err
  5285. }
  5286. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5287. info := models.WarehousingInfo{}
  5288. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5289. //return info, err
  5290. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5291. if id > 0 {
  5292. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5293. }
  5294. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5295. return info, err
  5296. }
  5297. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5298. info := models.WarehousingInfo{}
  5299. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5300. if id > 0 {
  5301. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5302. }
  5303. if storehouse_id > 0 {
  5304. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5305. }
  5306. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5307. return info, err
  5308. }
  5309. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5310. 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
  5311. return err
  5312. }
  5313. func DeleteStockPrice(id int64) error {
  5314. info := models.XtStockAdjustPrice{}
  5315. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5316. return err
  5317. }
  5318. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5319. report := models.XtStockReportPrice{}
  5320. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5321. return report, err
  5322. }
  5323. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5324. 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
  5325. return err
  5326. }
  5327. func DeleteDamage(id int64) error {
  5328. adjust := models.XtStockReportPrice{}
  5329. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5330. return err
  5331. }
  5332. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5333. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5334. return adjust, err
  5335. }
  5336. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5337. inventory := models.XtStockInventory{}
  5338. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5339. return inventory, err
  5340. }
  5341. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5342. 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
  5343. return err
  5344. }
  5345. func DeleteInventory(id int64) error {
  5346. inventory := models.XtStockInventory{}
  5347. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5348. return err
  5349. }
  5350. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5351. likeKey := "%" + keyword + "%"
  5352. offset := (page - 1) * limit
  5353. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5354. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5355. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5356. fmt.Println(table, tab)
  5357. if len(keyword) > 0 {
  5358. db = db.Where("x.good_name like ?", likeKey)
  5359. }
  5360. if orgid > 0 {
  5361. db = db.Where("x.user_org_id = ?", orgid)
  5362. }
  5363. if storehouse_id > 0 {
  5364. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5365. }
  5366. 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
  5367. return list, total, err
  5368. }
  5369. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5370. likeKey := "%" + keyword + "%"
  5371. offset := (page - 1) * limit
  5372. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5373. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5374. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5375. fmt.Println(table, tab)
  5376. if len(keyword) > 0 {
  5377. db = db.Where("x.good_name like ?", likeKey)
  5378. }
  5379. if orgid > 0 {
  5380. db = db.Where("x.user_org_id = ?", orgid)
  5381. }
  5382. if storehouse_id > 0 {
  5383. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5384. }
  5385. if len(ids) > 0 {
  5386. db = db.Where("x.id in(?)", ids)
  5387. }
  5388. 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
  5389. return list, total, err
  5390. }
  5391. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5392. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5393. return list, err
  5394. }
  5395. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5396. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5397. return list, err
  5398. }
  5399. func DeleteDrugPrice(id int64) error {
  5400. info := models.XtDrugAdjustPrice{}
  5401. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5402. return err
  5403. }
  5404. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5405. info := models.WarehousingInfo{}
  5406. db := XTReadDB().Model(&info).Where("status = 1")
  5407. if goodid > 0 {
  5408. db = db.Where("good_id = ?", goodid)
  5409. }
  5410. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5411. return info, err
  5412. }
  5413. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5414. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5415. return err
  5416. }
  5417. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5418. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5419. return err
  5420. }
  5421. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5422. err := XTWriteDB().Create(&record).Error
  5423. return err
  5424. }
  5425. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5426. info := models.WarehousingInfo{}
  5427. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5428. //return info, err
  5429. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5430. if id > 0 {
  5431. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5432. }
  5433. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5434. return info, err
  5435. }
  5436. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5437. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5438. return info, err
  5439. }
  5440. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5441. info := models.DrugWarehouseInfo{}
  5442. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5443. return info, err
  5444. }
  5445. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5446. 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
  5447. return err
  5448. }
  5449. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5450. 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
  5451. return err
  5452. }
  5453. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5454. 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
  5455. return info, err
  5456. }
  5457. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  5458. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5459. return err
  5460. }
  5461. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  5462. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  5463. return err
  5464. }
  5465. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  5466. db := XTReadDB().Model(&list).Where(" status = 1")
  5467. if good_id > 0 {
  5468. db = db.Where("good_id = ?", good_id)
  5469. }
  5470. if len(keyword) > 0 {
  5471. likeKey := "%" + keyword + "%"
  5472. db = db.Where("warehousing_order like ?", likeKey)
  5473. }
  5474. if inventory_type > 0 {
  5475. db = db.Where("inventory_type = ?", inventory_type)
  5476. }
  5477. if inventory_type == 0 {
  5478. db = db.Where("inventory_type = 4")
  5479. }
  5480. err = db.Find(&list).Error
  5481. return list, err
  5482. }
  5483. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  5484. flow := models.DrugFlow{}
  5485. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  5486. if err == gorm.ErrRecordNotFound {
  5487. return nil, err
  5488. }
  5489. if err != nil {
  5490. return nil, err
  5491. }
  5492. return &flow, nil
  5493. }
  5494. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  5495. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  5496. return info, err
  5497. }
  5498. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  5499. info := models.XtDrugWarehouseInfo{}
  5500. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  5501. return info, err
  5502. }
  5503. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5504. info := models.WarehousingInfo{}
  5505. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  5506. return info, err
  5507. }
  5508. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  5509. flow := models.VmStockFlow{}
  5510. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  5511. if err == gorm.ErrRecordNotFound {
  5512. return nil, err
  5513. }
  5514. if err != nil {
  5515. return nil, err
  5516. }
  5517. return &flow, nil
  5518. }
  5519. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  5520. info := models.GoodInfo{}
  5521. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  5522. return info, err
  5523. }
  5524. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  5525. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  5526. return err
  5527. }
  5528. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  5529. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  5530. return err
  5531. }
  5532. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  5533. 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}).Error
  5534. return err
  5535. }
  5536. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5537. 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
  5538. return err
  5539. }
  5540. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5541. 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
  5542. return err
  5543. }
  5544. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  5545. 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
  5546. return err
  5547. }
  5548. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  5549. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  5550. return err
  5551. }
  5552. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  5553. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5554. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5555. fmt.Println(table)
  5556. if orgid > 0 {
  5557. db = db.Where("x.org_id = ?", orgid)
  5558. }
  5559. 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
  5560. return info, err
  5561. }
  5562. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  5563. warehouseInfo := models.XtDrugWarehouseInfo{}
  5564. 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
  5565. return err
  5566. }
  5567. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  5568. err := XTWriteDB().Create(&info).Error
  5569. return err
  5570. }
  5571. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  5572. err := tx.Create(&info).Error
  5573. if err != nil {
  5574. tx.Rollback()
  5575. return err
  5576. }
  5577. return err
  5578. }
  5579. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  5580. flow := models.VmStockFlow{}
  5581. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  5582. if err == gorm.ErrRecordNotFound {
  5583. return nil, err
  5584. }
  5585. if err != nil {
  5586. return nil, err
  5587. }
  5588. return &flow, nil
  5589. }
  5590. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  5591. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5592. return err
  5593. }
  5594. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  5595. 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
  5596. return err
  5597. }
  5598. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  5599. flow := models.DrugWarehouseOutInfo{}
  5600. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  5601. return flow, err
  5602. }
  5603. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  5604. info := models.WarehouseOutInfo{}
  5605. 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
  5606. return info, err
  5607. }
  5608. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  5609. 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
  5610. return err
  5611. }
  5612. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  5613. 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
  5614. return err
  5615. }
  5616. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  5617. info := models.DrugWarehouseOutInfo{}
  5618. 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
  5619. if err == gorm.ErrRecordNotFound {
  5620. return nil, err
  5621. }
  5622. if err != nil {
  5623. return nil, err
  5624. }
  5625. return &info, nil
  5626. }
  5627. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  5628. drugFlow := models.DrugFlow{}
  5629. 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
  5630. if err == gorm.ErrRecordNotFound {
  5631. return nil, err
  5632. }
  5633. if err != nil {
  5634. return nil, err
  5635. }
  5636. return &drugFlow, nil
  5637. }
  5638. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  5639. 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
  5640. return err
  5641. }
  5642. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  5643. 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
  5644. return err
  5645. }
  5646. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  5647. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5648. return detail, err
  5649. }
  5650. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  5651. detail := models.DrugAutomaticReduceDetail{}
  5652. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5653. if err == gorm.ErrRecordNotFound {
  5654. return nil, err
  5655. }
  5656. if err != nil {
  5657. return nil, err
  5658. }
  5659. return &detail, nil
  5660. }
  5661. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  5662. 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
  5663. return err
  5664. }
  5665. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  5666. 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
  5667. return err
  5668. }
  5669. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  5670. info := models.DrugWarehouseInfo{}
  5671. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5672. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5673. return info, err
  5674. }
  5675. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  5676. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5677. return err
  5678. }
  5679. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  5680. info := models.DrugWarehouseInfo{}
  5681. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5682. return err
  5683. }
  5684. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  5685. info := models.DrugCancelStockInfo{}
  5686. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5687. return info, err
  5688. }
  5689. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  5690. info := models.DrugWarehouseInfo{}
  5691. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  5692. return err
  5693. }
  5694. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  5695. info := models.DrugWarehouseInfo{}
  5696. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  5697. return err
  5698. }
  5699. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  5700. 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
  5701. return advice, err
  5702. }
  5703. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  5704. 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
  5705. if err != gorm.ErrRecordNotFound {
  5706. if err != nil {
  5707. tx.Rollback()
  5708. return advice, err
  5709. }
  5710. }
  5711. return advice, err
  5712. }
  5713. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5714. 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
  5715. return info, err
  5716. }
  5717. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5718. 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
  5719. if err != gorm.ErrRecordNotFound {
  5720. if err != nil {
  5721. tx.Rollback()
  5722. return info, err
  5723. }
  5724. }
  5725. return info, err
  5726. }
  5727. // 耗材加库存
  5728. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5729. info := models.GoodInfo{}
  5730. 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
  5731. return info, err
  5732. }
  5733. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5734. info := models.GoodInfo{}
  5735. 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
  5736. 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
  5737. return info, err
  5738. }
  5739. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5740. info := models.GoodInfo{}
  5741. 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
  5742. return info, err
  5743. }
  5744. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5745. info := models.GoodInfo{}
  5746. 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
  5747. 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
  5748. return info, err
  5749. }
  5750. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  5751. info := models.GoodInfo{}
  5752. 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
  5753. return info, err
  5754. }
  5755. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5756. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5757. return info, err
  5758. }
  5759. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5760. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5761. return info, err
  5762. }
  5763. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  5764. info := models.WarehousingInfo{}
  5765. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  5766. return info, err
  5767. }
  5768. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  5769. info := models.WarehousingInfo{}
  5770. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  5771. return info, err
  5772. }
  5773. // 药品加库存
  5774. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5775. info := models.BaseDrugLib{}
  5776. 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
  5777. return info, err
  5778. }
  5779. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5780. info := models.BaseDrugLib{}
  5781. 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
  5782. 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
  5783. return info, err
  5784. }
  5785. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5786. info := models.BaseDrugLib{}
  5787. 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
  5788. return info, err
  5789. }
  5790. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5791. info := models.BaseDrugLib{}
  5792. 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
  5793. 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
  5794. return info, err
  5795. }
  5796. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  5797. info := models.BaseDrugLib{}
  5798. 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
  5799. 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
  5800. return info, err
  5801. }
  5802. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5803. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5804. return info, err
  5805. }
  5806. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5807. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5808. return info, err
  5809. }
  5810. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  5811. info := models.WarehouseOutInfo{}
  5812. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5813. return info, err
  5814. }
  5815. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  5816. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  5817. return out, err
  5818. }
  5819. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  5820. info := models.WarehousingInfo{}
  5821. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5822. return info, err
  5823. }
  5824. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  5825. err = XTReadDB().Where("status = 1").Find(&template).Error
  5826. return template, err
  5827. }
  5828. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  5829. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  5830. return good, err
  5831. }
  5832. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5833. info := models.GoodInfo{}
  5834. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5835. return info, err
  5836. }
  5837. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  5838. db := XTReadDB().Where("status = 1")
  5839. if good_id > 0 {
  5840. db = db.Where("good_id = ?", good_id)
  5841. }
  5842. err = db.Find(&info).Error
  5843. return info, err
  5844. }
  5845. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5846. db := XTReadDB().Model(&info).Where("status = 1")
  5847. if drug_id > 0 {
  5848. db = db.Where("drug_id = ?", drug_id)
  5849. }
  5850. err = db.Find(&info).Error
  5851. return info, err
  5852. }
  5853. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5854. lib := models.BaseDrugLib{}
  5855. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5856. return lib, err
  5857. }
  5858. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  5859. info := models.Warehousing{}
  5860. 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
  5861. if err == gorm.ErrRecordNotFound {
  5862. return nil, err
  5863. }
  5864. if err != nil {
  5865. return nil, err
  5866. }
  5867. return &info, nil
  5868. }
  5869. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  5870. info := models.Warehousing{}
  5871. 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
  5872. return info, err
  5873. }
  5874. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  5875. out := models.DrugWarehouseOut{}
  5876. 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
  5877. if err == gorm.ErrRecordNotFound {
  5878. return nil, err
  5879. }
  5880. if err != nil {
  5881. return nil, err
  5882. }
  5883. return &out, nil
  5884. }
  5885. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  5886. info := models.DrugWarehouseOut{}
  5887. 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
  5888. return info, err
  5889. }
  5890. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  5891. info := models.DrugWarehouse{}
  5892. 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
  5893. if err == gorm.ErrRecordNotFound {
  5894. return nil, err
  5895. }
  5896. if err != nil {
  5897. return nil, err
  5898. }
  5899. return &info, nil
  5900. }
  5901. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  5902. err := XTWriteDB().Create(&warehouse).Error
  5903. return err
  5904. }
  5905. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  5906. info := models.DrugWarehouse{}
  5907. 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
  5908. return info, err
  5909. }
  5910. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5911. info := models.GoodInfo{}
  5912. 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
  5913. 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
  5914. return info, err
  5915. }
  5916. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5917. info := models.GoodInfo{}
  5918. 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
  5919. 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
  5920. return info, err
  5921. }
  5922. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  5923. tx := XTWriteDB().Begin()
  5924. 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
  5925. if err != nil {
  5926. tx.Rollback()
  5927. return err
  5928. }
  5929. tx.Commit()
  5930. return err
  5931. }
  5932. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  5933. // 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
  5934. // return err
  5935. //}
  5936. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  5937. 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
  5938. return err
  5939. }
  5940. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  5941. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  5942. table := XTReadDB().Table("xt_base_drug as x d")
  5943. fmt.Println(table)
  5944. if startime > 0 {
  5945. db = db.Where("x.advice_date >= ?", startime)
  5946. }
  5947. if endtime > 0 {
  5948. db = db.Where("x.advice_date <= ?", endtime)
  5949. }
  5950. if orgid > 0 {
  5951. db = db.Where("x.user_org_id = ?", orgid)
  5952. }
  5953. if len(keyword) > 0 {
  5954. likeKey := "%" + keyword + "%"
  5955. 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)
  5956. }
  5957. offset := (page - 1) * limit
  5958. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  5959. return adviceinfo, total, err
  5960. }
  5961. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  5962. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  5963. table := XTReadDB().Table("xt_base_drug as x d")
  5964. fmt.Println(table)
  5965. if startime > 0 {
  5966. db = db.Where("x.advice_date >= ?", startime)
  5967. }
  5968. if endtime > 0 {
  5969. db = db.Where("x.advice_date <= ?", endtime)
  5970. }
  5971. if orgid > 0 {
  5972. db = db.Where("x.user_org_id = ?", orgid)
  5973. }
  5974. if len(keyword) > 0 {
  5975. likeKey := "%" + keyword + "%"
  5976. 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)
  5977. }
  5978. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  5979. return adviceinfo, total, err
  5980. }
  5981. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  5982. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  5983. return drug, err
  5984. }
  5985. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  5986. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  5987. table := XTReadDB().Table("xt_good_information as d")
  5988. fmt.Println(table)
  5989. if startime > 0 {
  5990. db = db.Where("x.record_date >= ?", startime)
  5991. }
  5992. if endtime > 0 {
  5993. db = db.Where("x.record_date <= ?", endtime)
  5994. }
  5995. if orgid > 0 {
  5996. db = db.Where("x.user_org_id = ?", orgid)
  5997. }
  5998. if len(keyword) > 0 {
  5999. likeKey := "%" + keyword + "%"
  6000. 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")
  6001. }
  6002. offset := (page - 1) * limit
  6003. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6004. return project, total, err
  6005. }
  6006. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6007. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6008. table := XTReadDB().Table("xt_good_information as d")
  6009. fmt.Println(table)
  6010. if startime > 0 {
  6011. db = db.Where("x.record_date >= ?", startime)
  6012. }
  6013. if endtime > 0 {
  6014. db = db.Where("x.record_date <= ?", endtime)
  6015. }
  6016. if orgid > 0 {
  6017. db = db.Where("x.user_org_id = ?", orgid)
  6018. }
  6019. if len(keyword) > 0 {
  6020. likeKey := "%" + keyword + "%"
  6021. 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")
  6022. }
  6023. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6024. return project, total, err
  6025. }
  6026. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6027. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6028. return good, err
  6029. }
  6030. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6031. setting := models.XtStockSetting{}
  6032. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6033. if err == gorm.ErrRecordNotFound {
  6034. return nil, err
  6035. }
  6036. if err != nil {
  6037. return nil, err
  6038. }
  6039. return &setting, nil
  6040. }
  6041. func CreateStockSetting(setting models.XtStockSetting) error {
  6042. err := XTWriteDB().Create(&setting).Error
  6043. return err
  6044. }
  6045. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6046. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6047. return err
  6048. }
  6049. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6050. setting := models.XtStockSetting{}
  6051. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6052. return setting, err
  6053. }
  6054. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6055. detail := models.BloodAutomaticReduceDetail{}
  6056. 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
  6057. return detail, err
  6058. }
  6059. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6060. detail := models.WarehouseOutInfoNight{}
  6061. 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
  6062. return detail, err
  6063. }
  6064. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6065. setting := models.XtAdviceSetting{}
  6066. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6067. if err == gorm.ErrRecordNotFound {
  6068. return nil, err
  6069. }
  6070. if err != nil {
  6071. return nil, err
  6072. }
  6073. return &setting, nil
  6074. }
  6075. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6076. err := XTWriteDB().Create(&setting).Error
  6077. return err
  6078. }
  6079. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6080. 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
  6081. return err
  6082. }
  6083. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6084. setting := models.XtAdviceSetting{}
  6085. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6086. return setting, err
  6087. }
  6088. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6089. config := models.XtPrescriptionConfig{}
  6090. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6091. if err == gorm.ErrRecordNotFound {
  6092. return nil, err
  6093. }
  6094. if err != nil {
  6095. return nil, err
  6096. }
  6097. return &config, nil
  6098. }
  6099. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6100. err := XTWriteDB().Create(&prescriptionConfig).Error
  6101. return err
  6102. }
  6103. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6104. 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
  6105. return err
  6106. }
  6107. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6108. config := models.XtPrescriptionConfig{}
  6109. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6110. return config, err
  6111. }
  6112. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6113. var deliver_number int64 = 0
  6114. var stock_number int64 = 0
  6115. deliver_number = maxNumber
  6116. // 根据先进先出原则,查询最先入库的批次,进行出库
  6117. // 如果没有对应的库存,则报错
  6118. //开启事物
  6119. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6120. if err != nil {
  6121. return err
  6122. }
  6123. // 将该批次的剩余库存数量转换为拆零数量
  6124. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6125. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6126. if stock_number >= deliver_number {
  6127. var maxNumber int64 = 0
  6128. var minNumber int64 = 0
  6129. var stock_max_number int64 = 0
  6130. stock_max_number = warehouse.StockMaxNumber
  6131. maxNumber = deliver_number / drup.MinNumber
  6132. minNumber = deliver_number % drup.MinNumber
  6133. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6134. minNumber = maxNumber
  6135. }
  6136. if drup.MaxUnit != drup.MinUnit {
  6137. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6138. return errors.New("库存数量不足")
  6139. }
  6140. }
  6141. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6142. if warehouse.StockMaxNumber < 0 {
  6143. warehouse.StockMaxNumber = 0
  6144. }
  6145. if warehouse.StockMinNumber < 0 {
  6146. warehouse.StockMinNumber = 0
  6147. }
  6148. warehouse.Mtime = time.Now().Unix()
  6149. if warehouse.StockMinNumber < minNumber {
  6150. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6151. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6152. } else {
  6153. if minNumber > 0 {
  6154. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6155. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6156. } else {
  6157. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6158. }
  6159. }
  6160. if minNumber == 0 && maxNumber != 1 {
  6161. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6162. }
  6163. }
  6164. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6165. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6166. if warehouse.StockMinNumber == 0 {
  6167. warehouse.StockMaxNumber = stock_max_number
  6168. }
  6169. }
  6170. if drup.MaxUnit != drup.MinUnit {
  6171. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6172. return errors.New("库存数量不足")
  6173. }
  6174. }
  6175. if warehouse.StockMinNumber <= 0 {
  6176. warehouse.StockMinNumber = 0
  6177. }
  6178. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6179. fmt.Println(errThree)
  6180. //查询默认仓库
  6181. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6182. //查询默认仓库剩余多少库存
  6183. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6184. var sum_count int64
  6185. var sum_in_count int64
  6186. for _, it := range list {
  6187. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6188. if it.MaxUnit == baseDrug.MaxUnit {
  6189. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6190. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6191. sum_count += it.StockMaxNumber + it.StockMinNumber
  6192. sum_in_count += it.WarehousingCount
  6193. }
  6194. }
  6195. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6196. return nil
  6197. } else {
  6198. warehouse.StockMaxNumber = 0
  6199. warehouse.StockMinNumber = 0
  6200. warehouse.Mtime = time.Now().Unix()
  6201. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6202. fmt.Println(errThree)
  6203. //查询默认仓库
  6204. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6205. //查询默认仓库剩余多少库存
  6206. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6207. var sum_count int64
  6208. var sum_in_count int64
  6209. for _, it := range list {
  6210. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6211. if it.MaxUnit == baseDrug.MaxUnit {
  6212. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6213. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6214. sum_count += it.StockMaxNumber + it.StockMinNumber
  6215. sum_in_count += it.WarehousingCount
  6216. }
  6217. }
  6218. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6219. if errThree != nil {
  6220. return errThree
  6221. }
  6222. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6223. prescribingNumber_two_temp := deliver_number - stock_number
  6224. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6225. }
  6226. return
  6227. }
  6228. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6229. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6230. return err
  6231. }
  6232. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6233. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6234. return err
  6235. }
  6236. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6237. config := models.DrugOutConfig{}
  6238. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6239. if err == gorm.ErrRecordNotFound {
  6240. return nil, err
  6241. }
  6242. if err != nil {
  6243. return nil, err
  6244. }
  6245. return &config, nil
  6246. }
  6247. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6248. config := models.DrugOutConfig{}
  6249. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6250. return config, err
  6251. }
  6252. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6253. config := models.XtHisConfig{}
  6254. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6255. return config, err
  6256. }
  6257. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6258. err := XTWriteDB().Create(&config).Error
  6259. return err
  6260. }
  6261. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6262. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6263. return err
  6264. }
  6265. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6266. config := models.DrugOutConfig{}
  6267. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6268. return config, err
  6269. }
  6270. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6271. config := models.HisSettleStockConfig{}
  6272. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6273. return config, err
  6274. }
  6275. func UpdatedWarehouseOut(id int64) error {
  6276. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6277. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6278. return err
  6279. }
  6280. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6281. tx := XTWriteDB().Begin()
  6282. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6283. if err != nil {
  6284. tx.Rollback()
  6285. return err
  6286. }
  6287. tx.Commit()
  6288. return err
  6289. }
  6290. func GetLastGoodWarehouseOutInfo(orgid int64, id int64) (info []*models.WarehouseOutInfo, err error) {
  6291. err = XTReadDB().Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6292. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6293. }).Find(&info).Error
  6294. return info, err
  6295. }
  6296. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6297. info := models.WarehouseOutInfo{}
  6298. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6299. return info, err
  6300. }
  6301. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6302. 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
  6303. return err
  6304. }
  6305. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6306. 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
  6307. return err
  6308. }
  6309. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6310. out := models.WarehouseOut{}
  6311. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6312. return out, err
  6313. }
  6314. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6315. ut := writeDb.Begin()
  6316. 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
  6317. if err != nil {
  6318. ut.Rollback()
  6319. return err
  6320. }
  6321. ut.Commit()
  6322. return err
  6323. }
  6324. func ModifyGoodWarehouseOut(id int64) error {
  6325. ut := writeDb.Begin()
  6326. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6327. if err != nil {
  6328. ut.Rollback()
  6329. return err
  6330. }
  6331. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6332. if err != nil {
  6333. ut.Rollback()
  6334. return err
  6335. }
  6336. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6337. if err != nil {
  6338. ut.Rollback()
  6339. return err
  6340. }
  6341. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6342. if err != nil {
  6343. ut.Rollback()
  6344. return err
  6345. }
  6346. ut.Commit()
  6347. return err
  6348. }
  6349. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6350. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6351. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6352. }).Find(&info).Error
  6353. return info, err
  6354. }
  6355. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  6356. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  6357. return info, err
  6358. }
  6359. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  6360. 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}).Error
  6361. return err
  6362. }
  6363. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  6364. tx := XTWriteDB().Begin()
  6365. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6366. if err != nil {
  6367. tx.Rollback()
  6368. return err
  6369. }
  6370. tx.Commit()
  6371. return err
  6372. }
  6373. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  6374. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6375. return info, err
  6376. }
  6377. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  6378. tx := XTWriteDB().Begin()
  6379. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  6380. if err != nil {
  6381. tx.Rollback()
  6382. return err
  6383. }
  6384. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6385. if err != nil {
  6386. tx.Rollback()
  6387. return err
  6388. }
  6389. tx.Commit()
  6390. return err
  6391. }
  6392. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  6393. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6394. return info, err
  6395. }
  6396. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  6397. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6398. return info, err
  6399. }
  6400. func FindLastDrugWarehousingInfoTwenty(org_id int64) (info models.DrugWarehouseInfo, err error) {
  6401. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6402. return info, err
  6403. }
  6404. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6405. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  6406. return info, err
  6407. }
  6408. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  6409. err := XTWriteDB().Create(&info).Error
  6410. return err
  6411. }
  6412. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  6413. err := XTWriteDB().Save(&info).Error
  6414. return err
  6415. }
  6416. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  6417. tx := XTWriteDB().Begin()
  6418. 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
  6419. if err != nil {
  6420. tx.Rollback()
  6421. return err
  6422. }
  6423. tx.Commit()
  6424. return err
  6425. }
  6426. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6427. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6428. return info, err
  6429. }
  6430. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  6431. tx := XTWriteDB().Begin()
  6432. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6433. if err != nil {
  6434. tx.Rollback()
  6435. return err
  6436. }
  6437. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6438. if err != nil {
  6439. tx.Rollback()
  6440. return err
  6441. }
  6442. tx.Commit()
  6443. return err
  6444. }
  6445. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  6446. tx := XTWriteDB().Begin()
  6447. 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
  6448. if err != nil {
  6449. tx.Rollback()
  6450. return err
  6451. }
  6452. tx.Commit()
  6453. return err
  6454. }
  6455. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  6456. tx := XTWriteDB().Begin()
  6457. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6458. if err != nil {
  6459. tx.Rollback()
  6460. return err
  6461. }
  6462. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6463. tx.Commit()
  6464. return err
  6465. }
  6466. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  6467. tx := XTWriteDB().Begin()
  6468. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6469. if err != nil {
  6470. tx.Rollback()
  6471. return err
  6472. }
  6473. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6474. if err != nil {
  6475. tx.Rollback()
  6476. return err
  6477. }
  6478. tx.Commit()
  6479. return err
  6480. }
  6481. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  6482. tx := XTWriteDB().Begin()
  6483. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6484. if err != nil {
  6485. tx.Rollback()
  6486. return err
  6487. }
  6488. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6489. if err != nil {
  6490. tx.Rollback()
  6491. return err
  6492. }
  6493. tx.Commit()
  6494. return err
  6495. }
  6496. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  6497. tx := XTWriteDB().Begin()
  6498. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6499. if err != nil {
  6500. tx.Rollback()
  6501. return err
  6502. }
  6503. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6504. if err != nil {
  6505. tx.Rollback()
  6506. return err
  6507. }
  6508. tx.Commit()
  6509. return err
  6510. }
  6511. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  6512. 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}).Error
  6513. return err
  6514. }
  6515. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  6516. 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}).Error
  6517. return err
  6518. }
  6519. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  6520. out := models.DrugWarehouseOut{}
  6521. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  6522. return out, err
  6523. }
  6524. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6525. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  6526. return info, err
  6527. }
  6528. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  6529. tx := XTWriteDB().Begin()
  6530. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  6531. if err != nil {
  6532. tx.Rollback()
  6533. return err
  6534. }
  6535. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6536. if err != nil {
  6537. tx.Rollback()
  6538. return err
  6539. }
  6540. tx.Commit()
  6541. return err
  6542. }
  6543. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  6544. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  6545. return err
  6546. }
  6547. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  6548. out := models.DrugWarehouseOut{}
  6549. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  6550. return out, err
  6551. }
  6552. func UpdateDrugMaxNumber(count int64, id int64) error {
  6553. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6554. return err
  6555. }
  6556. func UpdateDrugMinNumber(count int64, id int64) error {
  6557. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6558. return err
  6559. }
  6560. func DeleteDrugOutFlow(id int64, orgid int64) error {
  6561. 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
  6562. return err
  6563. }
  6564. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  6565. 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
  6566. return outInfo, err
  6567. }
  6568. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6569. 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
  6570. return outInfo, err
  6571. }
  6572. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6573. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  6574. return info, err
  6575. }
  6576. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6577. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  6578. return info, err
  6579. }
  6580. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  6581. 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
  6582. return info, err
  6583. }
  6584. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  6585. 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
  6586. 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
  6587. return err
  6588. }
  6589. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  6590. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6591. return err
  6592. }
  6593. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  6594. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6595. return err
  6596. }
  6597. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  6598. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6599. 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
  6600. return err
  6601. }
  6602. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  6603. 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
  6604. return stock, err
  6605. }
  6606. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  6607. 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
  6608. return err
  6609. }
  6610. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6611. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6612. return info, err
  6613. }
  6614. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  6615. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6616. return info, err
  6617. }
  6618. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  6619. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6620. return info, err
  6621. }
  6622. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  6623. 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
  6624. 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
  6625. return err
  6626. }
  6627. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6628. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  6629. return info, err
  6630. }
  6631. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  6632. 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
  6633. return err
  6634. }
  6635. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  6636. 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
  6637. return err
  6638. }
  6639. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  6640. 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
  6641. 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
  6642. return err
  6643. }
  6644. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  6645. 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
  6646. return err
  6647. }
  6648. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  6649. 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
  6650. 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
  6651. return err
  6652. }
  6653. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6654. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6655. 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
  6656. return err
  6657. }
  6658. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6659. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  6660. return info, err
  6661. }
  6662. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  6663. 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
  6664. 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
  6665. return err
  6666. }
  6667. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  6668. 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
  6669. return info, err
  6670. }
  6671. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  6672. 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
  6673. if err != gorm.ErrRecordNotFound {
  6674. if err != nil {
  6675. tx.Rollback()
  6676. return info, err
  6677. }
  6678. }
  6679. return info, err
  6680. }
  6681. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  6682. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6683. 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
  6684. return err
  6685. }
  6686. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  6687. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6688. return err
  6689. }
  6690. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  6691. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6692. if err != nil {
  6693. tx.Rollback()
  6694. return err
  6695. }
  6696. return err
  6697. }
  6698. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6699. 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
  6700. return info, err
  6701. }
  6702. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6703. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6704. return info, err
  6705. }
  6706. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  6707. 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
  6708. 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
  6709. return err
  6710. }
  6711. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  6712. stock := models.DrugCancelStock{}
  6713. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  6714. return stock, err
  6715. }
  6716. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6717. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  6718. return stock, err
  6719. }
  6720. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6721. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  6722. return stock, err
  6723. }
  6724. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6725. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  6726. return info, err
  6727. }
  6728. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  6729. 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
  6730. return err
  6731. }
  6732. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6733. 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
  6734. if err != nil {
  6735. tx.Rollback()
  6736. return err
  6737. }
  6738. return err
  6739. }
  6740. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  6741. 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
  6742. return err
  6743. }
  6744. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6745. 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
  6746. if err != nil {
  6747. tx.Rollback()
  6748. return err
  6749. }
  6750. return err
  6751. }
  6752. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  6753. 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
  6754. return err
  6755. }
  6756. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  6757. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6758. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6759. return err
  6760. }
  6761. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6762. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  6763. return info, err
  6764. }
  6765. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  6766. 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
  6767. return err
  6768. }
  6769. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6770. 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
  6771. return err
  6772. }
  6773. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6774. 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
  6775. return err
  6776. }
  6777. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  6778. info := models.DrugWarehouseInfo{}
  6779. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  6780. return info, err
  6781. }
  6782. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  6783. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  6784. if storehouse_id > 0 {
  6785. db = db.Where("storehouse_id = ?", storehouse_id)
  6786. }
  6787. if orgid > 0 {
  6788. db = db.Where("user_org_id = ?", orgid)
  6789. }
  6790. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  6791. return storeinventory, err
  6792. }
  6793. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  6794. detail := models.AutomaticReduceDetail{}
  6795. 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
  6796. return detail, err
  6797. }
  6798. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  6799. detail := models.WarehouseOutInfo{}
  6800. 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
  6801. return detail, err
  6802. }
  6803. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  6804. stock := models.XtGoodStockCount{}
  6805. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  6806. return stock, err
  6807. }
  6808. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  6809. err := XTWriteDB().Create(&stock).Error
  6810. return err
  6811. }
  6812. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6813. 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
  6814. return err
  6815. }
  6816. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6817. ut := XTWriteDB().Begin()
  6818. 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
  6819. if err != nil {
  6820. ut.Rollback()
  6821. return err
  6822. }
  6823. ut.Commit()
  6824. return err
  6825. }
  6826. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  6827. 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
  6828. return err
  6829. }
  6830. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  6831. 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
  6832. if err != nil {
  6833. tx.Rollback()
  6834. return err
  6835. }
  6836. return err
  6837. }
  6838. func CreateDrugStock(stock models.XtDrugStockCount) error {
  6839. err := XTWriteDB().Create(&stock).Error
  6840. return err
  6841. }
  6842. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  6843. ut := XTWriteDB().Begin()
  6844. 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
  6845. if err != nil {
  6846. ut.Rollback()
  6847. return err
  6848. }
  6849. 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
  6850. if err != nil {
  6851. ut.Rollback()
  6852. return err
  6853. }
  6854. ut.Commit()
  6855. return err
  6856. }
  6857. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  6858. ut := XTWriteDB().Begin()
  6859. 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
  6860. if err != nil {
  6861. ut.Rollback()
  6862. return err
  6863. }
  6864. ut.Commit()
  6865. return err
  6866. }
  6867. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  6868. ut := XTWriteDB().Begin()
  6869. 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
  6870. if err != nil {
  6871. ut.Rollback()
  6872. return err
  6873. }
  6874. 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
  6875. if err != nil {
  6876. ut.Rollback()
  6877. return err
  6878. }
  6879. ut.Commit()
  6880. return err
  6881. }
  6882. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  6883. ut := XTWriteDB().Begin()
  6884. 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
  6885. if err != nil {
  6886. ut.Rollback()
  6887. return err
  6888. }
  6889. ut.Commit()
  6890. return err
  6891. }
  6892. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6893. ut := XTWriteDB().Begin()
  6894. 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
  6895. if err != nil {
  6896. ut.Rollback()
  6897. return err
  6898. }
  6899. 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
  6900. if err != nil {
  6901. ut.Rollback()
  6902. return err
  6903. }
  6904. ut.Commit()
  6905. return err
  6906. }
  6907. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6908. ut := XTWriteDB().Begin()
  6909. 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
  6910. if err != nil {
  6911. ut.Rollback()
  6912. return err
  6913. }
  6914. 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
  6915. if err != nil {
  6916. ut.Rollback()
  6917. return err
  6918. }
  6919. ut.Commit()
  6920. return err
  6921. }
  6922. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  6923. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  6924. 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
  6925. return info, err
  6926. } else {
  6927. 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
  6928. return info, err
  6929. }
  6930. }
  6931. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  6932. config := models.HisSettleStockConfig{}
  6933. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  6934. if err == gorm.ErrRecordNotFound {
  6935. return nil, err
  6936. }
  6937. if err != nil {
  6938. return nil, err
  6939. }
  6940. return &config, nil
  6941. }
  6942. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  6943. err := XTWriteDB().Create(&config).Error
  6944. return err
  6945. }
  6946. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  6947. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  6948. return err
  6949. }
  6950. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  6951. config := models.DrugStockConfig{}
  6952. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  6953. return config, err
  6954. }
  6955. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  6956. stockConfig := models.PharmacyConfig{}
  6957. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  6958. return stockConfig, err
  6959. }
  6960. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  6961. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  6962. return err
  6963. }
  6964. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  6965. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  6966. return err
  6967. }
  6968. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  6969. config := models.HisSettleStockConfig{}
  6970. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  6971. return config, nil
  6972. }
  6973. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  6974. info := models.GoodInformationFourty{}
  6975. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  6976. return info, err
  6977. }
  6978. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  6979. advice := models.HisDoctorAdvice{}
  6980. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  6981. return advice, err
  6982. }
  6983. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  6984. advice := models.DoctorAdvice{}
  6985. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  6986. return advice, err
  6987. }
  6988. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  6989. out := models.WarehouseOut{}
  6990. 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
  6991. return out, err
  6992. }
  6993. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  6994. config := models.XtGoodOutConfig{}
  6995. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  6996. if err == gorm.ErrRecordNotFound {
  6997. return nil, err
  6998. }
  6999. if err != nil {
  7000. return nil, err
  7001. }
  7002. return &config, nil
  7003. }
  7004. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  7005. err := XTWriteDB().Create(&good).Error
  7006. return err
  7007. }
  7008. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  7009. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  7010. return err
  7011. }
  7012. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  7013. config := models.XtGoodOutConfig{}
  7014. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7015. return config, err
  7016. }
  7017. func UpdateGoodAutoConfig(user_org_id int64) error {
  7018. gobalConfig := models.GobalConfig{}
  7019. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7020. return err
  7021. }
  7022. func UpdateGoodOutConfig(user_org_id int64) error {
  7023. config := models.XtGoodOutConfig{}
  7024. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7025. return err
  7026. }
  7027. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  7028. 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
  7029. return info, err
  7030. }
  7031. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  7032. 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
  7033. return good, err
  7034. }
  7035. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  7036. 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
  7037. return err
  7038. }
  7039. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7040. 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
  7041. return err
  7042. }
  7043. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7044. 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
  7045. return err
  7046. }
  7047. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7048. out := models.WarehouseOut{}
  7049. 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
  7050. return out, err
  7051. }
  7052. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7053. out := models.WarehouseOut{}
  7054. 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
  7055. if err != gorm.ErrRecordNotFound {
  7056. if err != nil {
  7057. tx.Rollback()
  7058. return out, err
  7059. }
  7060. }
  7061. return out, err
  7062. }
  7063. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7064. out := models.WarehouseOut{}
  7065. 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
  7066. return out, err
  7067. }
  7068. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7069. out := models.WarehouseOut{}
  7070. 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
  7071. if err != gorm.ErrRecordNotFound {
  7072. if err != nil {
  7073. tx.Rollback()
  7074. return out, err
  7075. }
  7076. }
  7077. return out, err
  7078. }
  7079. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  7080. goodsType := models.GoodsType{}
  7081. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  7082. return goodsType, err
  7083. }
  7084. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  7085. goodsTypeList, _ := GetAllGoodType(org_id)
  7086. for _, item := range goodsTypeList {
  7087. if good_type_id == item.ID {
  7088. good_type_name = item.TypeName
  7089. }
  7090. }
  7091. return good_type_name
  7092. }
  7093. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  7094. information := models.GoodInformation{}
  7095. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  7096. return information, err
  7097. }
  7098. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  7099. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  7100. return info, err
  7101. }
  7102. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  7103. ut := writeDb.Begin()
  7104. 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
  7105. if err != nil {
  7106. ut.Rollback()
  7107. return err
  7108. }
  7109. ut.Commit()
  7110. return err
  7111. }
  7112. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  7113. 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
  7114. if err != nil {
  7115. tx.Rollback()
  7116. return err
  7117. }
  7118. return err
  7119. }
  7120. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  7121. info := models.WarehouseOutInfo{}
  7122. var err error
  7123. 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
  7124. if err == gorm.ErrRecordNotFound {
  7125. return nil, err
  7126. }
  7127. if err != nil {
  7128. return nil, err
  7129. }
  7130. return &info, nil
  7131. }
  7132. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  7133. info := models.WarehouseOutInfo{}
  7134. 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
  7135. return info, err
  7136. }
  7137. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7138. info := models.WarehouseOutInfo{}
  7139. 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
  7140. if err != gorm.ErrRecordNotFound {
  7141. if err != nil {
  7142. tx.Rollback()
  7143. return info, err
  7144. }
  7145. }
  7146. return info, err
  7147. }
  7148. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  7149. info := models.WarehouseOutInfo{}
  7150. 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
  7151. return info, err
  7152. }
  7153. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7154. info := models.WarehouseOutInfo{}
  7155. 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
  7156. if err != gorm.ErrRecordNotFound {
  7157. if err != nil {
  7158. tx.Rollback()
  7159. return info, err
  7160. }
  7161. }
  7162. return info, err
  7163. }
  7164. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  7165. info := models.WarehouseOutInfo{}
  7166. var err error
  7167. 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
  7168. if err == gorm.ErrRecordNotFound {
  7169. return nil, err
  7170. }
  7171. if err != nil {
  7172. return nil, err
  7173. }
  7174. return &info, nil
  7175. }
  7176. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  7177. info := models.WarehouseOutInfo{}
  7178. var err error
  7179. 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
  7180. if err != gorm.ErrRecordNotFound {
  7181. if err != nil {
  7182. tx.Rollback()
  7183. return &info, err
  7184. }
  7185. }
  7186. return &info, err
  7187. }
  7188. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  7189. db := XTReadDB().Model(&info).Where("status = 1")
  7190. if startime > 0 {
  7191. db = db.Where("ctime>=?", startime)
  7192. }
  7193. if endtime > 0 {
  7194. db = db.Where("ctime<=?", endtime)
  7195. }
  7196. if orgid > 0 {
  7197. db = db.Where("org_id = ?", orgid)
  7198. }
  7199. if len(good_id) > 0 {
  7200. db = db.Where("good_id in(?)", good_id)
  7201. }
  7202. if storehouse_id > 0 {
  7203. db = db.Where("storehouse_id = ?", storehouse_id)
  7204. }
  7205. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  7206. return info, err
  7207. }
  7208. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  7209. db := XTReadDB().Model(&info).Where("status= 1")
  7210. if start_time > 0 {
  7211. db = db.Where("ctime>=?", start_time)
  7212. }
  7213. if end_time > 0 {
  7214. db = db.Where("ctime <=?", end_time)
  7215. }
  7216. if orgid > 0 {
  7217. db = db.Where("org_id = ?", orgid)
  7218. }
  7219. if len(good_id) > 0 {
  7220. db = db.Where("good_id in(?)", good_id)
  7221. }
  7222. if storehouse_id > 0 {
  7223. db = db.Where("storehouse_id = ?", storehouse_id)
  7224. }
  7225. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  7226. return info, err
  7227. }
  7228. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  7229. config := models.XtGoodOutConfig{}
  7230. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7231. return config, err
  7232. }
  7233. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  7234. 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
  7235. 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
  7236. return err
  7237. }
  7238. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  7239. tx := XTWriteDB().Begin()
  7240. for _, item := range list {
  7241. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  7242. if err != nil {
  7243. tx.Rollback()
  7244. return err
  7245. }
  7246. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  7247. if err != nil {
  7248. tx.Rollback()
  7249. return err
  7250. }
  7251. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7252. if err != nil {
  7253. tx.Rollback()
  7254. return err
  7255. }
  7256. var goodList []*models.WarehousingInfo
  7257. 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
  7258. var flush_count int64
  7259. for _, it := range goodList {
  7260. flush_count += it.StockCount
  7261. }
  7262. 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
  7263. if err != nil {
  7264. tx.Rollback()
  7265. return err
  7266. }
  7267. 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
  7268. if err != nil {
  7269. tx.Rollback()
  7270. return err
  7271. }
  7272. storeHouseConfig := models.XtStorehouseConfig{}
  7273. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  7274. var stockList []*models.WarehousingInfo
  7275. //查询该耗材剩余多少库存
  7276. 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
  7277. var sum_count int64
  7278. var sum_in_count int64
  7279. for _, it := range stockList {
  7280. sum_count += it.StockCount
  7281. sum_in_count += it.WarehousingCount
  7282. }
  7283. 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
  7284. if err != nil {
  7285. tx.Rollback()
  7286. return err
  7287. }
  7288. 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
  7289. if err != nil {
  7290. tx.Rollback()
  7291. return err
  7292. }
  7293. }
  7294. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7295. if err != nil {
  7296. tx.Rollback()
  7297. return err
  7298. }
  7299. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7300. tx.Commit()
  7301. return err
  7302. }
  7303. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  7304. if storehouse_id == 0 {
  7305. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  7306. }
  7307. if storehouse_id > 0 {
  7308. 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
  7309. }
  7310. return info, err
  7311. }
  7312. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  7313. 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
  7314. return list, err
  7315. }
  7316. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  7317. 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
  7318. return infor, err
  7319. }
  7320. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  7321. info := models.WarehouseOutInfo{}
  7322. 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
  7323. return info, err
  7324. }
  7325. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  7326. info := models.WarehousingInfo{}
  7327. 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
  7328. return info, err
  7329. }
  7330. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7331. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7332. return list, err
  7333. }
  7334. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7335. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7336. return list, err
  7337. }
  7338. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  7339. templateL := models.HisPrescriptionProjectTemplateL{}
  7340. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  7341. return templateL, err
  7342. }