stock_service.go 462KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  12. err := writeDb.Create(&dealer).Error
  13. return err, dealer
  14. }
  15. func ModifyDealer(dealer *models.Dealer) error {
  16. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  17. "dealer_name": dealer.DealerName,
  18. "contact": dealer.Contact,
  19. "contact_phone": dealer.ContactPhone,
  20. "platform_number": dealer.PlatformNumber,
  21. "email": dealer.Email,
  22. "contact_address": dealer.ContactAddress,
  23. "modifier": dealer.Modifier,
  24. "mtime": time.Now().Unix(),
  25. "remark": dealer.Remark,
  26. "wubi": dealer.WuBi,
  27. "pinyin": dealer.PinYin,
  28. }).Error
  29. return err
  30. }
  31. func FindAllDealerTotal(orgId int64) (total int64) {
  32. db := readDb.Model(&models.Dealer{})
  33. db = db.Where("org_id = ? ", orgId).Count(&total)
  34. return
  35. }
  36. // 获取经销商的名称
  37. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  38. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  39. return
  40. }
  41. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  42. offset := (page - 1) * limit
  43. db := readDb.Model(&models.Dealer{})
  44. db = db.Where("org_id = ? AND status = 1", orgId)
  45. if key != 0 {
  46. db = db.Where("id = ? ", key)
  47. offset = 0
  48. limit = 10
  49. }
  50. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  51. return
  52. }
  53. func DeleteDealerById(id int64) error {
  54. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  55. return err
  56. }
  57. func FindDealerById(id int64) (*models.Dealer, error) {
  58. dealer := &models.Dealer{}
  59. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  60. return dealer, err
  61. }
  62. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  63. err := writeDb.Create(&manufacturer).Error
  64. return err, manufacturer
  65. }
  66. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  67. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  68. "manufacturer_name": manufacturer.ManufacturerName,
  69. "contact": manufacturer.Contact,
  70. "contact_phone": manufacturer.ContactPhone,
  71. "platform_number": manufacturer.PlatformNumber,
  72. "email": manufacturer.Email,
  73. "contact_address": manufacturer.ContactAddress,
  74. "modifier": manufacturer.Modifier,
  75. "mtime": time.Now().Unix(),
  76. "remark": manufacturer.Remark,
  77. "wubi": manufacturer.WuBi,
  78. "pinyin": manufacturer.PinYin,
  79. }).Error
  80. return err
  81. }
  82. // 获取生产厂商名称
  83. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  84. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  85. return
  86. }
  87. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  88. offset := (page - 1) * limit
  89. db := readDb.Model(&models.Manufacturer{})
  90. db = db.Where("org_id = ? AND status = 1", orgId)
  91. if key != 0 {
  92. db = db.Where("id = ?", key)
  93. offset = 0
  94. limit = 10
  95. }
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  97. return
  98. }
  99. func DeleteManufacturerById(id int64) error {
  100. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  101. return err
  102. }
  103. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  104. dealer := &models.Manufacturer{}
  105. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  106. return dealer, err
  107. }
  108. func FindManufacturerTotal(orgId int64) (total int64) {
  109. db := readDb.Model(&models.Manufacturer{})
  110. db = db.Where("org_id = ?", orgId).Count(&total)
  111. return
  112. }
  113. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  114. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  115. return dealers, err
  116. }
  117. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  118. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  119. return manufacturer, err
  120. }
  121. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id = ?", orgId).Count(&total)
  124. return
  125. }
  126. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  127. goodsType := models.GoodsType{}
  128. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  129. if err == gorm.ErrRecordNotFound {
  130. return nil, err
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &goodsType, nil
  136. }
  137. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  138. goodsType := models.GoodsType{}
  139. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  140. if err == gorm.ErrRecordNotFound {
  141. return nil, err
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return &goodsType, nil
  147. }
  148. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  149. err := writeDb.Create(&goodType).Error
  150. return err, goodType
  151. }
  152. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  153. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  154. "modifier": goodType.Modifier,
  155. "mtime": time.Now().Unix(),
  156. "remark": goodType.Remark,
  157. "type_name": goodType.TypeName,
  158. "out_stock": goodType.OutStock,
  159. "stock_attribute": goodType.StockAttribute,
  160. }).Error
  161. return err, goodType
  162. }
  163. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  164. offset := (page - 1) * limit
  165. db := readDb.Model(&models.GoodsType{})
  166. db = db.Where("org_id=? AND status = 1", orgId)
  167. if len(keyword) > 0 {
  168. likeKey := "%" + keyword + "%"
  169. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  170. }
  171. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  172. return
  173. }
  174. func DeleteGoodTypeById(id int64, operateId int64) error {
  175. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  176. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  177. return err
  178. }
  179. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  180. goodType := &models.GoodsType{}
  181. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  182. return goodType, err
  183. }
  184. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  185. db := readDb.Model(&models.GoodInfo{})
  186. db = db.Where("org_id = ?", orgId).Count(&total)
  187. return
  188. }
  189. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  190. err := writeDb.Create(&goodInfo).Error
  191. return err, goodInfo
  192. }
  193. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  194. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  195. "good_type_id": goodInfo.GoodTypeId,
  196. "modifier": goodInfo.Modifier,
  197. "mtime": time.Now().Unix(),
  198. "remark": goodInfo.Remark,
  199. "specification_name": goodInfo.SpecificationName,
  200. "good_unit": goodInfo.GoodUnit,
  201. "buy_price": goodInfo.BuyPrice,
  202. "sell_price": goodInfo.SellPrice,
  203. "manufacturer": goodInfo.Manufacturer,
  204. "dealer": goodInfo.Dealer,
  205. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  206. "stock_warn_count": goodInfo.StockWarnCount,
  207. "is_reuse": goodInfo.IsReuse,
  208. "good_name": goodInfo.GoodName,
  209. "pinyin": goodInfo.Pinyin,
  210. "wubi": goodInfo.Wubi,
  211. "good_kind": goodInfo.GoodKind,
  212. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  213. "retail_price": goodInfo.PackingPrice,
  214. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  215. "is_special_diseases": goodInfo.IsSpecialDiseases,
  216. "is_record": goodInfo.IsRecord,
  217. "statistics_category": goodInfo.StatisticsCategory,
  218. "good_status": goodInfo.GoodStatus,
  219. "default_count": goodInfo.DefaultCount,
  220. "sign": goodInfo.Sign,
  221. "is_default": goodInfo.IsDefault,
  222. "is_charge_use": goodInfo.IsChargeUse,
  223. "is_charge_predict": goodInfo.IsChargePredict,
  224. "is_statistics_work": goodInfo.IsStatisticsWork,
  225. "sort": goodInfo.Sort,
  226. "is_doctor_use": goodInfo.IsDoctorUse,
  227. "agent": goodInfo.Agent,
  228. "good_number": goodInfo.GoodNumber,
  229. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  230. "special_medical": goodInfo.SpecialMedical,
  231. "production_type": goodInfo.ProductionType,
  232. "min_number": goodInfo.MinNumber,
  233. "packing_unit": goodInfo.PackingUnit,
  234. "packing_price": goodInfo.PackingPrice,
  235. "default_count_unit": goodInfo.DefaultCountUnit,
  236. "min_unit": goodInfo.MinUnit,
  237. "register_number": goodInfo.RegisterNumber,
  238. "provinces_code": goodInfo.ProvincesCode,
  239. "is_user": goodInfo.IsUser,
  240. "number": goodInfo.Number,
  241. "is_warehouse": goodInfo.IsWarehouse,
  242. "bbx01": goodInfo.Bbx01,
  243. "bby01": goodInfo.Bby01,
  244. "zuobiao_id": goodInfo.Bbx01,
  245. "first_letter": goodInfo.FirstLetter,
  246. "is_show": goodInfo.IsShow,
  247. "is_zero_flag": goodInfo.IsZeroFlag,
  248. "split_of_number": goodInfo.SplitOfNumber,
  249. "register_number_name": goodInfo.RegisterNumberName,
  250. }).Error
  251. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  255. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  256. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  257. return err, goodInfo
  258. }
  259. 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) {
  260. offset := (page - 1) * limit
  261. db := readDb.Model(&models.GoodInfo{})
  262. db = db.Where("org_id = ? AND status = 1", orgId)
  263. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  264. if len(keyword) > 0 {
  265. likeKey := "%" + keyword + "%"
  266. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  267. if is_use > 0 {
  268. if is_use == 2 {
  269. db = db.Where("find_in_set('停用',good_status) = 0")
  270. } else {
  271. db = db.Where("find_in_set('停用',good_status) > 0")
  272. }
  273. }
  274. if is_charge > 0 {
  275. if is_charge == 1 {
  276. db = db.Where("find_in_set('收费',good_status) > 0")
  277. } else {
  278. db = db.Where("find_in_set('收费',good_status) = 0")
  279. }
  280. }
  281. if good_kind > 0 {
  282. db = db.Where(" good_kind = ?", good_kind)
  283. }
  284. if is_mark > 0 {
  285. db = db.Where("is_mark = ?", is_mark)
  286. }
  287. if manufacturer > 0 {
  288. db = db.Where("manufacturer = ?", manufacturer)
  289. }
  290. if is_type > 0 {
  291. db = db.Where("good_type_id = ?", is_type)
  292. }
  293. } else {
  294. if is_use > 0 {
  295. if is_use == 2 {
  296. db = db.Where("find_in_set('停用',good_status) = 0")
  297. } else {
  298. db = db.Where("find_in_set('停用',good_status) > 0")
  299. }
  300. }
  301. if is_charge > 0 {
  302. if is_charge == 1 {
  303. db = db.Where("find_in_set('收费',good_status) > 0")
  304. } else {
  305. db = db.Where("find_in_set('收费',good_status) = 0")
  306. }
  307. }
  308. if good_kind > 0 {
  309. db = db.Where(" good_kind = ?", good_kind)
  310. }
  311. if is_mark > 0 {
  312. db = db.Where("is_mark = ?", is_mark)
  313. }
  314. if manufacturer > 0 {
  315. db = db.Where("manufacturer = ?", manufacturer)
  316. }
  317. if is_type > 0 {
  318. db = db.Where("good_type_id = ?", is_type)
  319. }
  320. }
  321. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  322. return
  323. }
  324. func DeleteGoodInfoById(id int64, operateId int64) error {
  325. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  326. return err
  327. }
  328. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  329. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  330. return list, err
  331. }
  332. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  333. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  334. return list, err
  335. }
  336. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  337. goodInfo := &models.GoodInfo{}
  338. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  339. return goodInfo, err
  340. }
  341. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  342. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  343. return goodType, err
  344. }
  345. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  346. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  347. return goodType, err
  348. }
  349. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  350. 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
  351. return goodInfo, err
  352. }
  353. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  354. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  355. return goodInfo, err
  356. }
  357. func FindAllGoodInfoThree(org_id int64) (goodInfo []*models.XtGoodInformationFourty, err error) {
  358. err = XTReadDB().Model(&models.XtGoodInformationFourty{}).Where("org_id = ? AND status = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  359. return goodInfo, err
  360. }
  361. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  362. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  363. return total, err
  364. }
  365. func FindNewAllWarehouseTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  366. err = tx.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  367. if err != gorm.ErrRecordNotFound {
  368. if err != nil {
  369. tx.Rollback()
  370. return total, err
  371. }
  372. }
  373. return total, err
  374. }
  375. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  376. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  377. return total, err
  378. }
  379. func FindNewAllWarehouseTotalOne(org_id int64, tx *gorm.DB) (total int64, err error) {
  380. err = tx.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  381. if err != gorm.ErrRecordNotFound {
  382. if err != nil {
  383. tx.Rollback()
  384. return total, err
  385. }
  386. }
  387. return total, err
  388. }
  389. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  390. err := writeDb.Create(&warehouse).Error
  391. return err
  392. }
  393. func AddNewSigleWarehouse(warehouse *models.Warehousing, tx *gorm.DB) error {
  394. err := writeDb.Create(&warehouse).Error
  395. if err != nil {
  396. tx.Rollback()
  397. return err
  398. }
  399. return err
  400. }
  401. func UpdateWarehouse(id int64, storehouse_id int64) error {
  402. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  403. return err
  404. }
  405. func UpdateNewWarehouse(id int64, storehouse_id int64, tx *gorm.DB) error {
  406. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  407. if err != nil {
  408. tx.Rollback()
  409. return err
  410. }
  411. return err
  412. }
  413. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  414. err := writeDb.Create(&warehouse).Error
  415. return err
  416. }
  417. func AddNewSigleDrugWarehouse(warehouse *models.DrugWarehouse, tx *gorm.DB) error {
  418. err := tx.Create(&warehouse).Error
  419. if err != nil {
  420. tx.Rollback()
  421. return err
  422. }
  423. return err
  424. }
  425. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  426. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  427. return err
  428. }
  429. func UpdateNewSigleDrugWarehouse(id int64, storehosue_id int64, tx *gorm.DB) error {
  430. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  431. if err != nil {
  432. tx.Rollback()
  433. return err
  434. }
  435. return err
  436. }
  437. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  438. err := writeDb.Create(&salesReturn).Error
  439. return err
  440. }
  441. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  442. err := writeDb.Create(&salesReturn).Error
  443. return err
  444. }
  445. func UpDateGoodReturnStatus(ids []string) {
  446. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  447. }
  448. func UpDateCanCelStockStatus(ids []string) {
  449. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  450. }
  451. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  452. if len(warehousingInfo) > 0 {
  453. utx := writeDb.Begin()
  454. if len(warehousingInfo) > 0 {
  455. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id,good_code) VALUES "
  456. insertParams := make([]string, 0)
  457. insertData := make([]interface{}, 0)
  458. for _, info := range warehousingInfo {
  459. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  460. insertData = append(insertData, info.WarehousingId)
  461. insertData = append(insertData, info.GoodId)
  462. insertData = append(insertData, info.GoodTypeId)
  463. insertData = append(insertData, info.Number)
  464. insertData = append(insertData, info.ProductDate)
  465. insertData = append(insertData, info.ExpiryDate)
  466. insertData = append(insertData, info.WarehousingCount)
  467. insertData = append(insertData, info.Price)
  468. insertData = append(insertData, info.TotalPrice)
  469. insertData = append(insertData, info.Dealer)
  470. insertData = append(insertData, info.Manufacturer)
  471. insertData = append(insertData, info.Remark)
  472. insertData = append(insertData, info.Ctime)
  473. insertData = append(insertData, info.Mtime)
  474. insertData = append(insertData, info.Status)
  475. insertData = append(insertData, info.OrgId)
  476. insertData = append(insertData, info.WarehousingOrder)
  477. insertData = append(insertData, info.Type)
  478. insertData = append(insertData, info.WarehousingCount)
  479. insertData = append(insertData, info.LicenseNumber)
  480. insertData = append(insertData, info.PackingPrice)
  481. insertData = append(insertData, info.StorehouseId)
  482. insertData = append(insertData, info.GoodCode)
  483. }
  484. thisSQL += strings.Join(insertParams, ", ")
  485. err = utx.Exec(thisSQL, insertData...).Error
  486. if err != nil {
  487. utx.Rollback()
  488. return
  489. }
  490. }
  491. utx.Commit()
  492. }
  493. return
  494. }
  495. func CreateNewWarehousingInfo(warehousingInfo []*models.WarehousingInfo, tx *gorm.DB) (err error) {
  496. if len(warehousingInfo) > 0 {
  497. if len(warehousingInfo) > 0 {
  498. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id,good_code) VALUES "
  499. insertParams := make([]string, 0)
  500. insertData := make([]interface{}, 0)
  501. for _, info := range warehousingInfo {
  502. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  503. insertData = append(insertData, info.WarehousingId)
  504. insertData = append(insertData, info.GoodId)
  505. insertData = append(insertData, info.GoodTypeId)
  506. insertData = append(insertData, info.Number)
  507. insertData = append(insertData, info.ProductDate)
  508. insertData = append(insertData, info.ExpiryDate)
  509. insertData = append(insertData, info.WarehousingCount)
  510. insertData = append(insertData, info.Price)
  511. insertData = append(insertData, info.TotalPrice)
  512. insertData = append(insertData, info.Dealer)
  513. insertData = append(insertData, info.Manufacturer)
  514. insertData = append(insertData, info.Remark)
  515. insertData = append(insertData, info.Ctime)
  516. insertData = append(insertData, info.Mtime)
  517. insertData = append(insertData, info.Status)
  518. insertData = append(insertData, info.OrgId)
  519. insertData = append(insertData, info.WarehousingOrder)
  520. insertData = append(insertData, info.Type)
  521. insertData = append(insertData, info.WarehousingCount)
  522. insertData = append(insertData, info.LicenseNumber)
  523. insertData = append(insertData, info.PackingPrice)
  524. insertData = append(insertData, info.StorehouseId)
  525. insertData = append(insertData, info.GoodCode)
  526. }
  527. thisSQL += strings.Join(insertParams, ", ")
  528. err = tx.Exec(thisSQL, insertData...).Error
  529. if err != nil {
  530. tx.Rollback()
  531. return
  532. }
  533. }
  534. }
  535. return
  536. }
  537. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  538. if len(warehousingInfo) > 0 {
  539. utx := writeDb.Begin()
  540. if len(warehousingInfo) > 0 {
  541. 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 "
  542. insertParams := make([]string, 0)
  543. insertData := make([]interface{}, 0)
  544. for _, info := range warehousingInfo {
  545. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  546. insertData = append(insertData, info.WarehousingId)
  547. insertData = append(insertData, info.DrugId)
  548. insertData = append(insertData, info.Number)
  549. insertData = append(insertData, info.ProductDate)
  550. insertData = append(insertData, info.ExpiryDate)
  551. insertData = append(insertData, info.WarehousingCount)
  552. insertData = append(insertData, info.Price)
  553. insertData = append(insertData, info.TotalPrice)
  554. insertData = append(insertData, info.Dealer)
  555. insertData = append(insertData, info.Manufacturer)
  556. insertData = append(insertData, info.Remark)
  557. insertData = append(insertData, info.Ctime)
  558. insertData = append(insertData, info.Mtime)
  559. insertData = append(insertData, info.Status)
  560. insertData = append(insertData, info.OrgId)
  561. insertData = append(insertData, info.WarehousingOrder)
  562. insertData = append(insertData, info.Type)
  563. insertData = append(insertData, info.RetailPrice)
  564. insertData = append(insertData, info.RetailTotalPrice)
  565. insertData = append(insertData, info.StockMaxNumber)
  566. insertData = append(insertData, info.MaxUnit)
  567. insertData = append(insertData, info.MinUnit)
  568. insertData = append(insertData, info.BatchNumber)
  569. insertData = append(insertData, info.StockMinNumber)
  570. insertData = append(insertData, info.StorehouseId)
  571. }
  572. thisSQL += strings.Join(insertParams, ", ")
  573. err = utx.Exec(thisSQL, insertData...).Error
  574. if err != nil {
  575. utx.Rollback()
  576. return
  577. }
  578. }
  579. utx.Commit()
  580. }
  581. return
  582. }
  583. func CreateNewDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo, tx *gorm.DB) (err error) {
  584. if len(warehousingInfo) > 0 {
  585. if len(warehousingInfo) > 0 {
  586. 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,drug_code) VALUES "
  587. insertParams := make([]string, 0)
  588. insertData := make([]interface{}, 0)
  589. for _, info := range warehousingInfo {
  590. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  591. insertData = append(insertData, info.WarehousingId)
  592. insertData = append(insertData, info.DrugId)
  593. insertData = append(insertData, info.Number)
  594. insertData = append(insertData, info.ProductDate)
  595. insertData = append(insertData, info.ExpiryDate)
  596. insertData = append(insertData, info.WarehousingCount)
  597. insertData = append(insertData, info.Price)
  598. insertData = append(insertData, info.TotalPrice)
  599. insertData = append(insertData, info.Dealer)
  600. insertData = append(insertData, info.Manufacturer)
  601. insertData = append(insertData, info.Remark)
  602. insertData = append(insertData, info.Ctime)
  603. insertData = append(insertData, info.Mtime)
  604. insertData = append(insertData, info.Status)
  605. insertData = append(insertData, info.OrgId)
  606. insertData = append(insertData, info.WarehousingOrder)
  607. insertData = append(insertData, info.Type)
  608. insertData = append(insertData, info.RetailPrice)
  609. insertData = append(insertData, info.RetailTotalPrice)
  610. insertData = append(insertData, info.StockMaxNumber)
  611. insertData = append(insertData, info.MaxUnit)
  612. insertData = append(insertData, info.MinUnit)
  613. insertData = append(insertData, info.BatchNumber)
  614. insertData = append(insertData, info.StockMinNumber)
  615. insertData = append(insertData, info.StorehouseId)
  616. insertData = append(insertData, info.DrugCode)
  617. }
  618. thisSQL += strings.Join(insertParams, ", ")
  619. err = tx.Exec(thisSQL, insertData...).Error
  620. if err != nil {
  621. tx.Rollback()
  622. return
  623. }
  624. }
  625. }
  626. return
  627. }
  628. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  629. if len(warehousingInfo) > 0 {
  630. utx := writeDb.Begin()
  631. if len(warehousingInfo) > 0 {
  632. 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 "
  633. insertParams := make([]string, 0)
  634. insertData := make([]interface{}, 0)
  635. for _, info := range warehousingInfo {
  636. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  637. insertData = append(insertData, info.WarehousingId)
  638. insertData = append(insertData, info.DrugId)
  639. insertData = append(insertData, info.Number)
  640. insertData = append(insertData, info.ProductDate)
  641. insertData = append(insertData, info.ExpiryDate)
  642. insertData = append(insertData, info.WarehousingCount)
  643. insertData = append(insertData, info.Price)
  644. insertData = append(insertData, info.TotalPrice)
  645. insertData = append(insertData, info.Dealer)
  646. insertData = append(insertData, info.Manufacturer)
  647. insertData = append(insertData, info.Remark)
  648. insertData = append(insertData, info.Ctime)
  649. insertData = append(insertData, info.Mtime)
  650. insertData = append(insertData, info.Status)
  651. insertData = append(insertData, info.OrgId)
  652. insertData = append(insertData, info.WarehousingOrder)
  653. insertData = append(insertData, info.Type)
  654. insertData = append(insertData, info.RetailPrice)
  655. insertData = append(insertData, info.RetailTotalPrice)
  656. insertData = append(insertData, info.StockMaxNumber)
  657. insertData = append(insertData, info.MaxUnit)
  658. insertData = append(insertData, info.MinUnit)
  659. insertData = append(insertData, info.BatchNumber)
  660. insertData = append(insertData, info.StockMinNumber)
  661. insertData = append(insertData, info.SupplyWarehouseId)
  662. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  663. insertData = append(insertData, info.StorehouseId)
  664. insertData = append(insertData, info.IsCheck)
  665. }
  666. thisSQL += strings.Join(insertParams, ", ")
  667. err = utx.Exec(thisSQL, insertData...).Error
  668. if err != nil {
  669. utx.Rollback()
  670. return
  671. }
  672. }
  673. utx.Commit()
  674. }
  675. return
  676. }
  677. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  678. var total float64
  679. var min_number int64
  680. for _, item := range warehousingInfo {
  681. //查询刚入库耗材的库存
  682. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  683. //更新药品库的总量
  684. info, _ := FindeDrugInfo(item.DrugId)
  685. min_number = count.StockMaxNumber * info.MinNumber
  686. str := strconv.FormatInt(min_number, 10)
  687. minFloat, _ := strconv.ParseFloat(str, 64)
  688. total = info.Total + minFloat
  689. lib := models.BaseDrugLib{
  690. Total: total,
  691. }
  692. UpdateBaseDrug(&lib, item.ID)
  693. }
  694. return err
  695. }
  696. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  697. info := models.XtDrugWarehouseInfo{}
  698. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  699. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  700. return info, err
  701. }
  702. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  703. lib := models.BaseDrugLib{}
  704. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  705. return lib, err
  706. }
  707. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  708. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  709. return err
  710. }
  711. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  712. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  713. return err
  714. }
  715. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  716. info := models.DrugWarehouseInfo{}
  717. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  718. return info, err
  719. }
  720. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  721. out := models.DrugWarehouseOut{}
  722. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  723. return out, err
  724. }
  725. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  726. if len(drugflow) > 0 {
  727. utx := writeDb.Begin()
  728. if len(drugflow) > 0 {
  729. 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 "
  730. insertParams := make([]string, 0)
  731. insertData := make([]interface{}, 0)
  732. for _, info := range drugflow {
  733. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  734. insertData = append(insertData, info.WarehousingId)
  735. insertData = append(insertData, info.DrugId)
  736. insertData = append(insertData, info.Number)
  737. insertData = append(insertData, info.BatchNumber)
  738. insertData = append(insertData, info.Count)
  739. insertData = append(insertData, info.UserOrgId)
  740. insertData = append(insertData, info.PatientId)
  741. insertData = append(insertData, info.SystemTime)
  742. insertData = append(insertData, info.ConsumableType)
  743. insertData = append(insertData, info.IsSys)
  744. insertData = append(insertData, info.WarehousingOrder)
  745. insertData = append(insertData, info.WarehouseOutId)
  746. insertData = append(insertData, info.WarehouseOutOrderNumber)
  747. insertData = append(insertData, info.IsEdit)
  748. insertData = append(insertData, info.CancelStockId)
  749. insertData = append(insertData, info.CancelOrderNumber)
  750. insertData = append(insertData, info.Manufacturer)
  751. insertData = append(insertData, info.Dealer)
  752. insertData = append(insertData, info.Creator)
  753. insertData = append(insertData, info.UpdateCreator)
  754. insertData = append(insertData, info.Status)
  755. insertData = append(insertData, info.Ctime)
  756. insertData = append(insertData, info.Mtime)
  757. insertData = append(insertData, info.Price)
  758. insertData = append(insertData, info.WarehousingDetailId)
  759. insertData = append(insertData, info.WarehouseOutDetailId)
  760. insertData = append(insertData, info.CancelOutDetailId)
  761. insertData = append(insertData, info.ExpireDate)
  762. insertData = append(insertData, info.ProductDate)
  763. insertData = append(insertData, info.MaxUnit)
  764. insertData = append(insertData, info.MinUnit)
  765. insertData = append(insertData, info.StorehouseId)
  766. }
  767. thisSQL += strings.Join(insertParams, ", ")
  768. err = utx.Exec(thisSQL, insertData...).Error
  769. if err != nil {
  770. utx.Rollback()
  771. return
  772. }
  773. }
  774. utx.Commit()
  775. }
  776. return
  777. }
  778. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  779. info := models.DrugFlow{}
  780. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  781. if err == gorm.ErrRecordNotFound {
  782. return nil, err
  783. }
  784. if err != nil {
  785. return nil, err
  786. }
  787. return &info, nil
  788. }
  789. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  790. info := models.XtDrugWarehouseInfo{}
  791. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  792. return info, err
  793. }
  794. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  795. warehouseInfo := models.XtDrugWarehouseInfo{}
  796. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  797. return err
  798. }
  799. func CreateDrugFlowOne(flow models.DrugFlow) error {
  800. ut := writeDb.Begin()
  801. err := ut.Create(&flow).Error
  802. if err != nil {
  803. ut.Rollback()
  804. return err
  805. }
  806. ut.Commit()
  807. return err
  808. }
  809. func CreateSelfDrugFlowTwo(flow models.XtSelfDrugFlow) error {
  810. ut := writeDb.Begin()
  811. err := ut.Create(&flow).Error
  812. if err != nil {
  813. ut.Rollback()
  814. return err
  815. }
  816. ut.Commit()
  817. return err
  818. }
  819. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  820. err := tx.Create(&flow).Error
  821. if err != nil {
  822. tx.Rollback()
  823. return err
  824. }
  825. return err
  826. }
  827. func CreateDrugOutInfoLog(log models.XtDrugWarehouseOutInfoLog, tx *gorm.DB) error {
  828. err := tx.Create(&log).Error
  829. if err != nil {
  830. tx.Rollback()
  831. return err
  832. }
  833. return err
  834. }
  835. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  836. detail := models.DrugAutomaticReduceDetail{}
  837. 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
  838. 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
  839. return detail, err
  840. }
  841. func UpdateSelfDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  842. detail := models.XtSelfDrugWarehouseOutInfo{}
  843. err = XTWriteDB().Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("patient_id =? and sys_record_time =? and drug_id = ? and status = 1 and advice_id =?", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  844. return detail, err
  845. }
  846. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  847. flow := models.DrugFlow{}
  848. 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
  849. return flow, err
  850. }
  851. func UpdateDrugFlowTensOne(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.XtSelfDrugFlow, error) {
  852. flow := models.XtSelfDrugFlow{}
  853. 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
  854. return flow, err
  855. }
  856. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  857. tx := XTWriteDB().Begin()
  858. err := tx.Create(&flow).Error
  859. if err != nil {
  860. tx.Rollback()
  861. return err
  862. }
  863. tx.Commit()
  864. return err
  865. }
  866. func CreateNewDrugFlowTwo(flow *models.DrugFlow, tx *gorm.DB) error {
  867. err := tx.Create(&flow).Error
  868. if err != nil {
  869. tx.Rollback()
  870. return err
  871. }
  872. return err
  873. }
  874. func CreateInfoLog(log models.XtDrugWarehouseInfoLog, tx *gorm.DB) error {
  875. err := tx.Create(&log).Error
  876. if err != nil {
  877. tx.Rollback()
  878. return err
  879. }
  880. return err
  881. }
  882. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  883. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  884. return info, err
  885. }
  886. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  887. info := models.WarehousingInfo{}
  888. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  889. return info, err
  890. }
  891. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  892. err := XTWriteDB().Create(&info).Error
  893. return err
  894. }
  895. func CreatedNewWarehouseingDetail(info *models.WarehousingInfo, tx *gorm.DB) error {
  896. err := tx.Create(&info).Error
  897. if err != nil {
  898. tx.Rollback()
  899. return err
  900. }
  901. return err
  902. }
  903. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  904. if storehouse_id == 0 {
  905. 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
  906. }
  907. if storehouse_id > 0 {
  908. 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
  909. }
  910. return info, err
  911. }
  912. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  913. if storehouse_id == 0 {
  914. 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
  915. if err != gorm.ErrRecordNotFound {
  916. if err != nil {
  917. tx.Rollback()
  918. return info, err
  919. }
  920. }
  921. }
  922. if storehouse_id > 0 {
  923. 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
  924. if err != gorm.ErrRecordNotFound {
  925. if err != nil {
  926. tx.Rollback()
  927. return info, err
  928. }
  929. }
  930. }
  931. return info, err
  932. }
  933. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  934. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  935. if good_id > 0 {
  936. db = db.Where("x.good_id = ?", good_id)
  937. }
  938. if good_type_id > 0 {
  939. db = db.Where("x.good_type_id = ?", good_type_id)
  940. }
  941. if storehouse_id > 0 {
  942. db = db.Where("x.storehouse_id = ?", storehouse_id)
  943. }
  944. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  945. return info, err
  946. }
  947. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  948. info := models.WarehousingInfo{}
  949. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  950. return info, err
  951. }
  952. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  953. info := models.WarehousingInfo{}
  954. var err error
  955. if storehouse_id == 0 {
  956. 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
  957. }
  958. if storehouse_id > 0 {
  959. 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
  960. }
  961. return info, err
  962. }
  963. func FindNewWarehousingInfoTen(goodId int64, storehouse_id int64, tx *gorm.DB) (models.WarehousingInfo, error) {
  964. info := models.WarehousingInfo{}
  965. var err error
  966. if storehouse_id == 0 {
  967. err = tx.Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and is_check = 1", goodId).Find(&info).Error
  968. if err != gorm.ErrRecordNotFound {
  969. if err != nil {
  970. tx.Rollback()
  971. return info, err
  972. }
  973. }
  974. }
  975. if storehouse_id > 0 {
  976. err := tx.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
  977. if err != gorm.ErrRecordNotFound {
  978. if err != nil {
  979. tx.Rollback()
  980. return info, err
  981. }
  982. }
  983. }
  984. return info, err
  985. }
  986. //药品先进先出,找出最先入库的批次
  987. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  988. 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
  989. return info, err
  990. }
  991. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  992. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").Last(&info).Error
  993. return info, err
  994. }
  995. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  996. 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
  997. if err != gorm.ErrRecordNotFound {
  998. if err != nil {
  999. tx.Rollback()
  1000. return info, err
  1001. }
  1002. }
  1003. return info, err
  1004. }
  1005. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  1006. if is_source == 1 {
  1007. 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
  1008. }
  1009. if is_source == 2 || is_source == 0 {
  1010. 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
  1011. }
  1012. return info, err
  1013. }
  1014. // 出库时先进先出,找出最先入库的批次
  1015. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  1016. 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
  1017. return info, err
  1018. }
  1019. //
  1020. //// 出库时先进先出,找出最先入库的批次
  1021. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  1022. // 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
  1023. // return info, err
  1024. //}
  1025. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  1026. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  1027. return info, err
  1028. }
  1029. 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) {
  1030. offset := (page - 1) * limit
  1031. db := readDb.Model(&models.Warehousing{})
  1032. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  1033. if len(keywords) > 0 {
  1034. likeKey := "%" + keywords + "%"
  1035. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1036. 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")
  1037. }
  1038. if startTime > 0 {
  1039. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  1040. }
  1041. if endTime > 0 {
  1042. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  1043. }
  1044. if storehouse_id > 0 {
  1045. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  1046. }
  1047. if check_type == 0 {
  1048. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  1049. }
  1050. if check_type > 0 {
  1051. db = db.Where("xt_warehouse.is_check = ?", check_type)
  1052. }
  1053. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.warehousing_time desc").Find(&list).Error
  1054. return
  1055. }
  1056. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  1057. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  1058. if orgid > 0 {
  1059. db = db.Where("org_id = ?", orgid)
  1060. }
  1061. if len(keyword) > 0 {
  1062. likeKey := "%" + keyword + "%"
  1063. db = db.Where("good_name like ?", likeKey)
  1064. }
  1065. err = db.Find(&goodInfo).Error
  1066. return goodInfo, err
  1067. }
  1068. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  1069. db := XTReadDB().Model(&info).Where("status =1")
  1070. if len(good_id) > 0 {
  1071. db = db.Where("good_id in (?)", good_id)
  1072. }
  1073. if startime > 0 {
  1074. db = db.Where("ctime >=?", startime)
  1075. }
  1076. if endtime > 0 {
  1077. db = db.Where("ctime<=?", endtime)
  1078. }
  1079. if orgid > 0 {
  1080. db = db.Where("org_id = ?", orgid)
  1081. }
  1082. if storehouse_id > 0 {
  1083. db = db.Where("storehouse_id = ?", storehouse_id)
  1084. }
  1085. err = db.Find(&info).Error
  1086. return info, err
  1087. }
  1088. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  1089. db := XTReadDB().Model(&info).Where("status =1")
  1090. if len(good_id) > 0 {
  1091. db = db.Where("good_id in (?)", good_id)
  1092. }
  1093. if startime > 0 {
  1094. db = db.Where("ctime >=?", startime)
  1095. }
  1096. if endtime > 0 {
  1097. db = db.Where("ctime<=?", endtime)
  1098. }
  1099. if orgid > 0 {
  1100. db = db.Where("org_id = ?", orgid)
  1101. }
  1102. if storehouse_id > 0 {
  1103. db = db.Where("storehouse_id = ?", storehouse_id)
  1104. }
  1105. err = db.Find(&info).Error
  1106. return info, err
  1107. }
  1108. type VMDrugWarehouse struct {
  1109. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1110. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1111. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  1112. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1113. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  1114. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1115. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1116. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1117. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1118. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  1119. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1120. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1121. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1122. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  1123. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  1124. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1125. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1126. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1127. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  1128. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  1129. }
  1130. func (VMDrugWarehouse) TableName() string {
  1131. return "xt_drug_warehouse"
  1132. }
  1133. type VMDrugWarehouseInfo struct {
  1134. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1135. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  1136. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1137. Number string `gorm:"column:number" json:"number" form:"number"`
  1138. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1139. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1140. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  1141. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1142. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1143. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1144. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1145. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1146. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1147. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1148. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1149. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1150. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  1151. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1152. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1153. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1154. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1155. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1156. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1157. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1158. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1159. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1160. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  1161. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1162. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1163. }
  1164. func (VMDrugWarehouseInfo) TableName() string {
  1165. return "xt_drug_warehouse_info"
  1166. }
  1167. type Drug struct {
  1168. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1169. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  1170. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  1171. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  1172. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  1173. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  1174. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  1175. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  1176. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  1177. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  1178. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  1179. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  1180. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  1181. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  1182. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1183. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1184. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  1185. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1186. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1187. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  1188. Number string `gorm:"column:number" json:"number" form:"number"`
  1189. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  1190. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  1191. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  1192. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  1193. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  1194. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1195. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  1196. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  1197. Code string `gorm:"column:code" json:"code" form:"code"`
  1198. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  1199. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  1200. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  1201. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  1202. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  1203. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  1204. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  1205. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  1206. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  1207. Label int64 `gorm:"column:label" json:"label" form:"label"`
  1208. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  1209. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  1210. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  1211. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  1212. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  1213. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  1214. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1215. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1216. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1217. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1218. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1219. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1220. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  1221. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1222. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1223. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1224. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1225. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1226. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1227. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1228. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1229. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1230. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1231. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1232. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1233. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1234. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1235. IsZeroFlag int64 `gorm:"column:is_zero_flag" json:"is_zero_flag" form:"is_zero_flag"`
  1236. }
  1237. func (Drug) TableName() string {
  1238. return "xt_base_drug"
  1239. }
  1240. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1241. db := readDb.Model(&VMDrugWarehouse{})
  1242. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1243. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1244. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1245. if len(keywords) > 0 {
  1246. likeKey := "%" + keywords + "%"
  1247. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1248. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1249. 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")
  1250. }
  1251. if startTime > 0 {
  1252. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1253. }
  1254. if endTime > 0 {
  1255. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1256. }
  1257. db = db.Count(&total)
  1258. offset := (page - 1) * limit
  1259. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1260. return
  1261. }
  1262. 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) {
  1263. db := readDb.Model(&VMDrugWarehouse{})
  1264. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1265. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1266. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1267. if len(keywords) > 0 {
  1268. likeKey := "%" + keywords + "%"
  1269. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1270. 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")
  1271. }
  1272. if startTime > 0 {
  1273. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1274. }
  1275. if endTime > 0 {
  1276. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1277. }
  1278. if storehouse_id > 0 {
  1279. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1280. }
  1281. if check_type > 0 {
  1282. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1283. }
  1284. db = db.Count(&total)
  1285. offset := (page - 1) * limit
  1286. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.warehousing_time desc").Find(&list).Error
  1287. return
  1288. }
  1289. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1290. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1291. return list, err
  1292. }
  1293. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1294. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1295. return list, err
  1296. }
  1297. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1298. 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
  1299. return list, err
  1300. }
  1301. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1302. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1303. return list, err
  1304. }
  1305. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1306. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1307. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1308. fmt.Println(table)
  1309. if len(id) > 0 {
  1310. db = db.Where("x.warehousing_id in(?)", id)
  1311. }
  1312. if org_id > 0 {
  1313. db = db.Where("x.org_id = ?", org_id)
  1314. }
  1315. 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
  1316. return list, err
  1317. }
  1318. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1319. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1320. return warehousing, err
  1321. }
  1322. func FindNewWarehousingById(id int64, tx *gorm.DB) (warehousing models.Warehousing, err error) {
  1323. err = tx.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1324. if err != gorm.ErrRecordNotFound {
  1325. if err != nil {
  1326. tx.Rollback()
  1327. return warehousing, err
  1328. }
  1329. }
  1330. return warehousing, err
  1331. }
  1332. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1333. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1334. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1335. return warehousing, err
  1336. }
  1337. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1338. 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
  1339. return warehousing, err
  1340. }
  1341. func FindNewDrugWarehousingById(id int64, org_id int64, tx *gorm.DB) (warehousing VMDrugWarehouse, err error) {
  1342. err = tx.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
  1343. if err != gorm.ErrRecordNotFound {
  1344. if err != nil {
  1345. tx.Rollback()
  1346. return warehousing, err
  1347. }
  1348. }
  1349. return warehousing, err
  1350. }
  1351. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1352. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1353. if len(id) > 0 {
  1354. db = db.Where("x.id in(?)", id)
  1355. }
  1356. if org_id > 0 {
  1357. db = db.Where("x.org_id = ?", org_id)
  1358. }
  1359. 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
  1360. return warehousing, err
  1361. }
  1362. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1363. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1364. // return warehousing, err
  1365. //
  1366. //}
  1367. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1368. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1369. return
  1370. }
  1371. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1372. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1373. return
  1374. }
  1375. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1376. if len(salesReturnInfo) > 0 {
  1377. utx := writeDb.Begin()
  1378. if len(salesReturnInfo) > 0 {
  1379. 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 "
  1380. insertParams := make([]string, 0)
  1381. insertData := make([]interface{}, 0)
  1382. for _, info := range salesReturnInfo {
  1383. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1384. insertData = append(insertData, info.GoodId)
  1385. insertData = append(insertData, info.SalesReturnId)
  1386. insertData = append(insertData, info.GoodTypeId)
  1387. insertData = append(insertData, info.Count)
  1388. insertData = append(insertData, info.Price)
  1389. insertData = append(insertData, info.Total)
  1390. insertData = append(insertData, info.Ctime)
  1391. insertData = append(insertData, info.Status)
  1392. insertData = append(insertData, info.OrgId)
  1393. insertData = append(insertData, info.OrderNumber)
  1394. insertData = append(insertData, info.Type)
  1395. insertData = append(insertData, info.Dealer)
  1396. insertData = append(insertData, info.Manufacturer)
  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 CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1410. if len(salesReturnInfo) > 0 {
  1411. utx := writeDb.Begin()
  1412. if len(salesReturnInfo) > 0 {
  1413. 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 "
  1414. insertParams := make([]string, 0)
  1415. insertData := make([]interface{}, 0)
  1416. for _, info := range salesReturnInfo {
  1417. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1418. insertData = append(insertData, info.DrugId)
  1419. insertData = append(insertData, info.SalesReturnId)
  1420. insertData = append(insertData, info.Count)
  1421. insertData = append(insertData, info.Price)
  1422. insertData = append(insertData, info.Total)
  1423. insertData = append(insertData, info.Ctime)
  1424. insertData = append(insertData, info.Status)
  1425. insertData = append(insertData, info.OrgId)
  1426. insertData = append(insertData, info.OrderNumber)
  1427. insertData = append(insertData, info.Type)
  1428. insertData = append(insertData, info.Dealer)
  1429. insertData = append(insertData, info.Manufacturer)
  1430. insertData = append(insertData, info.RetailPrice)
  1431. insertData = append(insertData, info.RetailTotalPrice)
  1432. }
  1433. thisSQL += strings.Join(insertParams, ", ")
  1434. err = utx.Exec(thisSQL, insertData...).Error
  1435. if err != nil {
  1436. utx.Rollback()
  1437. return
  1438. }
  1439. }
  1440. utx.Commit()
  1441. }
  1442. return
  1443. }
  1444. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1445. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1446. return total, err
  1447. }
  1448. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1449. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1450. return total, err
  1451. }
  1452. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1453. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1454. if err != gorm.ErrRecordNotFound {
  1455. if err != nil {
  1456. tx.Rollback()
  1457. return total, err
  1458. }
  1459. }
  1460. return total, err
  1461. }
  1462. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1463. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1464. return total, err
  1465. }
  1466. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1467. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1468. if err != gorm.ErrRecordNotFound {
  1469. if err != nil {
  1470. tx.Rollback()
  1471. return total, err
  1472. }
  1473. }
  1474. return total, err
  1475. }
  1476. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1477. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1478. // return total, err
  1479. //}
  1480. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1481. db := readDb.Model(&models.SalesReturn{})
  1482. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1483. if len(keywords) > 0 {
  1484. likeKey := "%" + keywords + "%"
  1485. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1486. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1487. 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")
  1488. }
  1489. if startTime > 0 {
  1490. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1491. }
  1492. if endTime > 0 {
  1493. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1494. }
  1495. db = db.Count(&total)
  1496. offset := (page - 1) * limit
  1497. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1498. return
  1499. }
  1500. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1501. db := readDb.Model(&models.DrugSalesReturn{})
  1502. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1503. if len(keywords) > 0 {
  1504. likeKey := "%" + keywords + "%"
  1505. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1506. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1507. 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")
  1508. }
  1509. if startTime > 0 {
  1510. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1511. }
  1512. if endTime > 0 {
  1513. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1514. }
  1515. db = db.Count(&total)
  1516. offset := (page - 1) * limit
  1517. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1518. return
  1519. }
  1520. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1521. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1522. return list, err
  1523. }
  1524. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1525. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1526. return list, err
  1527. }
  1528. type DrugCancelStockInfo struct {
  1529. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1530. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1531. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1532. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1533. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1534. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1535. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1536. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1537. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1538. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1539. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1540. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1541. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1542. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1543. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1544. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1545. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1546. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1547. Number string `gorm:"column:number" json:"number" form:"number"`
  1548. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1549. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1550. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1551. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1552. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1553. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1554. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1555. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1556. }
  1557. func (DrugCancelStockInfo) TableName() string {
  1558. return "xt_drug_cancel_stock_info"
  1559. }
  1560. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1561. 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
  1562. return list, err
  1563. }
  1564. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1565. db := readDb.Model(&models.WarehousingInfo{})
  1566. db = db.Where("org_id = ? AND status = 1", orgId)
  1567. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1568. if startTime > 0 {
  1569. db = db.Where("ctime >=?", startTime)
  1570. }
  1571. if endTime > 0 {
  1572. db = db.Where("ctime<= ?", endTime)
  1573. }
  1574. db = db.Count(&total)
  1575. offset := (page - 1) * limit
  1576. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1577. return list, total, err
  1578. }
  1579. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1580. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1581. return total, err
  1582. }
  1583. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1584. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1585. return total, err
  1586. }
  1587. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1588. err = tx.Model(&models.XtDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1589. if err != gorm.ErrRecordNotFound {
  1590. if err != nil {
  1591. tx.Rollback()
  1592. return
  1593. }
  1594. }
  1595. return total, err
  1596. }
  1597. func AddSigleWarehouseOutOne(warehouseOut *models.WarehouseOut) error {
  1598. err := XTWriteDB().Create(&warehouseOut).Error
  1599. return err
  1600. }
  1601. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1602. err := tx.Create(&warehouseOut).Error
  1603. if err != nil {
  1604. tx.Rollback()
  1605. return err
  1606. }
  1607. return err
  1608. }
  1609. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1610. err := tx.Create(&warehouseOut).Error
  1611. if err != nil {
  1612. tx.Rollback()
  1613. return err
  1614. }
  1615. return err
  1616. }
  1617. func UpdateSingleWarehoseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1618. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1619. if err != nil {
  1620. tx.Rollback()
  1621. return err
  1622. }
  1623. return err
  1624. }
  1625. func GetDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string) (models.DrugWarehouseOut, error) {
  1626. drugWarehouseOut := models.DrugWarehouseOut{}
  1627. err := XTReadDB().Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).Find(&drugWarehouseOut).Error
  1628. return drugWarehouseOut, err
  1629. }
  1630. func GetNewDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  1631. drugWarehouseOut := models.DrugWarehouseOut{}
  1632. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).Find(&drugWarehouseOut).Error
  1633. if err != gorm.ErrRecordNotFound {
  1634. if err != nil {
  1635. tx.Rollback()
  1636. return drugWarehouseOut, err
  1637. }
  1638. }
  1639. return drugWarehouseOut, err
  1640. }
  1641. func GetNewDrugWarehouseOutIsExitOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB, id int64) (models.DrugWarehouseOut, error) {
  1642. drugWarehouseOut := models.DrugWarehouseOut{}
  1643. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND id = ?", is_sys, org_id, id).Find(&drugWarehouseOut).Error
  1644. if err != gorm.ErrRecordNotFound {
  1645. if err != nil {
  1646. tx.Rollback()
  1647. return drugWarehouseOut, err
  1648. }
  1649. }
  1650. return drugWarehouseOut, err
  1651. }
  1652. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1653. err := writeDb.Create(&warehouseOut).Error
  1654. return err
  1655. }
  1656. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1657. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1658. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1659. if err != gorm.ErrRecordNotFound {
  1660. if err != nil {
  1661. tx.Rollback()
  1662. return selfDrugWarehouse, err
  1663. }
  1664. }
  1665. return selfDrugWarehouse, err
  1666. }
  1667. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1668. err := tx.Create(&warehouseOut).Error
  1669. if err != nil {
  1670. tx.Rollback()
  1671. return err
  1672. }
  1673. return err
  1674. }
  1675. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1676. err := tx.Create(&warehouseOut).Error
  1677. if err != nil {
  1678. tx.Rollback()
  1679. return err
  1680. }
  1681. return err
  1682. }
  1683. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1684. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1685. return err
  1686. }
  1687. func UpdateNewSingleDrugWarehouseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1688. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1689. if err != nil {
  1690. tx.Rollback()
  1691. return err
  1692. }
  1693. return err
  1694. }
  1695. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1696. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1697. return err
  1698. }
  1699. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1700. if len(warehouseOutInfo) > 0 {
  1701. utx := writeDb.Begin()
  1702. if len(warehouseOutInfo) > 0 {
  1703. 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 "
  1704. insertParams := make([]string, 0)
  1705. insertData := make([]interface{}, 0)
  1706. for _, info := range warehouseOutInfo {
  1707. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1708. insertData = append(insertData, info.WarehouseOutId)
  1709. insertData = append(insertData, info.GoodId)
  1710. insertData = append(insertData, info.GoodTypeId)
  1711. insertData = append(insertData, info.ProductDate)
  1712. insertData = append(insertData, info.ExpiryDate)
  1713. insertData = append(insertData, info.Count)
  1714. insertData = append(insertData, info.Price)
  1715. insertData = append(insertData, info.TotalPrice)
  1716. insertData = append(insertData, info.Remark)
  1717. insertData = append(insertData, info.Ctime)
  1718. insertData = append(insertData, info.Status)
  1719. insertData = append(insertData, info.OrgId)
  1720. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1721. insertData = append(insertData, info.Type)
  1722. insertData = append(insertData, info.Dealer)
  1723. insertData = append(insertData, info.Manufacturer)
  1724. insertData = append(insertData, info.Number)
  1725. insertData = append(insertData, info.LicenseNumber)
  1726. }
  1727. thisSQL += strings.Join(insertParams, ", ")
  1728. err = utx.Exec(thisSQL, insertData...).Error
  1729. if err != nil {
  1730. utx.Rollback()
  1731. return
  1732. }
  1733. }
  1734. utx.Commit()
  1735. }
  1736. return
  1737. }
  1738. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1739. if len(warehouseOutInfo) > 0 {
  1740. utx := writeDb.Begin()
  1741. if len(warehouseOutInfo) > 0 {
  1742. 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 "
  1743. insertParams := make([]string, 0)
  1744. insertData := make([]interface{}, 0)
  1745. for _, info := range warehouseOutInfo {
  1746. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1747. insertData = append(insertData, info.WarehouseOutId)
  1748. insertData = append(insertData, info.DrugId)
  1749. insertData = append(insertData, info.ProductDate)
  1750. insertData = append(insertData, info.ExpiryDate)
  1751. insertData = append(insertData, info.Count)
  1752. insertData = append(insertData, info.Price)
  1753. insertData = append(insertData, info.TotalPrice)
  1754. insertData = append(insertData, info.Remark)
  1755. insertData = append(insertData, info.Ctime)
  1756. insertData = append(insertData, info.Status)
  1757. insertData = append(insertData, info.OrgId)
  1758. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1759. insertData = append(insertData, info.Type)
  1760. insertData = append(insertData, info.Dealer)
  1761. insertData = append(insertData, info.Manufacturer)
  1762. insertData = append(insertData, info.RetailPrice)
  1763. insertData = append(insertData, info.RetailTotalPrice)
  1764. insertData = append(insertData, info.Number)
  1765. insertData = append(insertData, info.BatchNumber)
  1766. insertData = append(insertData, info.CountUnit)
  1767. }
  1768. thisSQL += strings.Join(insertParams, ", ")
  1769. err = utx.Exec(thisSQL, insertData...).Error
  1770. if err != nil {
  1771. utx.Rollback()
  1772. return
  1773. }
  1774. }
  1775. utx.Commit()
  1776. }
  1777. return
  1778. }
  1779. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1780. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1781. return info, err
  1782. }
  1783. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1784. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1785. return info, err
  1786. }
  1787. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1788. db := readDb.Model(&models.WarehouseOut{})
  1789. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1790. if len(keywords) > 0 {
  1791. likeKey := "%" + keywords + "%"
  1792. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1793. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1794. 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")
  1795. }
  1796. if startTime > 0 {
  1797. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1798. }
  1799. if endTime > 0 {
  1800. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1801. }
  1802. db = db.Count(&total)
  1803. offset := (page - 1) * limit
  1804. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1805. return list, total, err
  1806. }
  1807. 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) {
  1808. db := readDb.Model(&models.WarehouseOut{})
  1809. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1", orgId)
  1810. if len(keywords) > 0 {
  1811. likeKey := "%" + keywords + "%"
  1812. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1813. 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")
  1814. }
  1815. if startTime > 0 {
  1816. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1817. }
  1818. if endTime > 0 {
  1819. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1820. }
  1821. if storehouse_id > 0 {
  1822. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1823. }
  1824. if way_type == 0 {
  1825. 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")
  1826. }
  1827. if way_type == 1 {
  1828. db = db.Where("xt_warehouse_out.is_sys = 0")
  1829. }
  1830. if way_type == 2 {
  1831. db = db.Where("xt_warehouse_out.is_sys = 1")
  1832. }
  1833. if way_type == 3 {
  1834. db = db.Where("xt_warehouse_out.is_sys = 12")
  1835. }
  1836. if way_type == 4 {
  1837. db = db.Where("xt_warehouse_out.is_sys = 5")
  1838. }
  1839. if check_type > 0 {
  1840. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1841. }
  1842. if check_type == 0 {
  1843. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1844. }
  1845. db = db.Count(&total)
  1846. offset := (page - 1) * limit
  1847. if orgId == 10191 {
  1848. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1849. } else {
  1850. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1851. }
  1852. return list, total, err
  1853. }
  1854. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1855. db := readDb.Model(&models.DrugWarehouseOut{})
  1856. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1857. if len(keywords) > 0 {
  1858. likeKey := "%" + keywords + "%"
  1859. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1860. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1861. 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")
  1862. }
  1863. if startTime > 0 {
  1864. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1865. }
  1866. if endTime > 0 {
  1867. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1868. }
  1869. db = db.Count(&total)
  1870. offset := (page - 1) * limit
  1871. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1872. return list, total, err
  1873. }
  1874. 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) {
  1875. db := readDb.Model(&models.DrugWarehouseOut{})
  1876. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1877. if len(keywords) > 0 {
  1878. likeKey := "%" + keywords + "%"
  1879. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1880. 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")
  1881. }
  1882. if startTime > 0 {
  1883. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1884. }
  1885. if endTime > 0 {
  1886. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1887. }
  1888. if storehouse_id > 0 {
  1889. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1890. }
  1891. if check_type > 0 {
  1892. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1893. }
  1894. if way_type == 0 {
  1895. 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")
  1896. }
  1897. if way_type == 1 {
  1898. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1899. }
  1900. if way_type == 2 {
  1901. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1902. }
  1903. if way_type == 3 {
  1904. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1905. }
  1906. if way_type == 4 {
  1907. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1908. }
  1909. db = db.Count(&total)
  1910. offset := (page - 1) * limit
  1911. if orgId == 10191 {
  1912. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1913. } else {
  1914. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1915. }
  1916. return list, total, err
  1917. }
  1918. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1919. 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
  1920. return list, err
  1921. }
  1922. type DrugWarehouseOutInfo struct {
  1923. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1924. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1925. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1926. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1927. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1928. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1929. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1930. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1931. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1932. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1933. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1934. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1935. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1936. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1937. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1938. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1939. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1940. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1941. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1942. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1943. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1944. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1945. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1946. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1947. Number string `gorm:"column:number" json:"number" form:"number"`
  1948. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1949. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1950. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1951. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1952. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1953. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1954. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1955. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1956. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1957. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1958. }
  1959. func (DrugWarehouseOutInfo) TableName() string {
  1960. return "xt_drug_warehouse_out_info"
  1961. }
  1962. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1963. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1964. return list, err
  1965. }
  1966. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1967. info := &models.WarehousingInfo{}
  1968. type WarehouseInfoCountAmount struct {
  1969. Total int64
  1970. }
  1971. var result WarehouseInfoCountAmount
  1972. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1973. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1974. return result.Total, info
  1975. }
  1976. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1977. type SalesReturnCountAmount struct {
  1978. Total int64
  1979. }
  1980. var result SalesReturnCountAmount
  1981. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1982. return result.Total, err
  1983. }
  1984. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1985. type WarehouseOutInfoCountAmount struct {
  1986. Total int64
  1987. }
  1988. var result WarehouseOutInfoCountAmount
  1989. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1990. return result.Total, err
  1991. }
  1992. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1993. type CancleStockoCountAmount struct {
  1994. Total int64
  1995. }
  1996. var result CancleStockoCountAmount
  1997. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1998. return result.Total, err
  1999. }
  2000. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  2001. info := models.WarehousingInfo{}
  2002. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  2003. return info, err
  2004. }
  2005. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  2006. stock := models.CancelStock{}
  2007. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  2008. if err == gorm.ErrRecordNotFound {
  2009. return nil, err
  2010. }
  2011. if err != nil {
  2012. return nil, err
  2013. }
  2014. return &stock, nil
  2015. }
  2016. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  2017. stock := models.CancelStock{}
  2018. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2019. if err == gorm.ErrRecordNotFound {
  2020. return nil, err
  2021. }
  2022. if err != nil {
  2023. return nil, err
  2024. }
  2025. return &stock, nil
  2026. }
  2027. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  2028. stock := models.CancelStock{}
  2029. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2030. if err != gorm.ErrRecordNotFound {
  2031. if err != nil {
  2032. tx.Rollback()
  2033. return &stock, err
  2034. }
  2035. }
  2036. return &stock, err
  2037. }
  2038. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  2039. err := writeDb.Create(&cancelStock).Error
  2040. return err
  2041. }
  2042. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  2043. err := tx.Create(&cancelStock).Error
  2044. if err != nil {
  2045. tx.Rollback()
  2046. return err
  2047. }
  2048. return err
  2049. }
  2050. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  2051. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2052. return err
  2053. }
  2054. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  2055. stock := models.CancelStock{}
  2056. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2057. return stock, err
  2058. }
  2059. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  2060. stock := models.CancelStock{}
  2061. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2062. if err != gorm.ErrRecordNotFound {
  2063. if err != nil {
  2064. tx.Rollback()
  2065. return stock, err
  2066. }
  2067. }
  2068. return stock, err
  2069. }
  2070. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  2071. stock := models.DrugCancelStock{}
  2072. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2073. return stock, err
  2074. }
  2075. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  2076. stock := models.DrugCancelStock{}
  2077. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2078. if err != gorm.ErrRecordNotFound {
  2079. if err != nil {
  2080. tx.Rollback()
  2081. return stock, err
  2082. }
  2083. }
  2084. return stock, err
  2085. }
  2086. func GetManufactureById(id int64) (models.Manufacturer, error) {
  2087. manufacturer := models.Manufacturer{}
  2088. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2089. return manufacturer, err
  2090. }
  2091. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  2092. manufacturer := models.Manufacturer{}
  2093. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2094. if err != gorm.ErrRecordNotFound {
  2095. if err != nil {
  2096. tx.Rollback()
  2097. return manufacturer, err
  2098. }
  2099. }
  2100. return manufacturer, err
  2101. }
  2102. func GetDealerById(id int64) (models.Dealer, error) {
  2103. dealer := models.Dealer{}
  2104. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2105. return dealer, err
  2106. }
  2107. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  2108. dealer := models.Dealer{}
  2109. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2110. if err != gorm.ErrRecordNotFound {
  2111. if err != nil {
  2112. tx.Rollback()
  2113. return dealer, err
  2114. }
  2115. }
  2116. return dealer, err
  2117. }
  2118. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  2119. err := writeDb.Create(&cancelStock).Error
  2120. return err
  2121. }
  2122. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  2123. err := tx.Create(&cancelStock).Error
  2124. if err != nil {
  2125. tx.Rollback()
  2126. return err
  2127. }
  2128. return err
  2129. }
  2130. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  2131. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2132. return err
  2133. }
  2134. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  2135. // err := tx.Create(&cancelStock).Error
  2136. // return err
  2137. //}
  2138. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  2139. if len(cancelStockInfo) > 0 {
  2140. utx := writeDb.Begin()
  2141. if len(cancelStockInfo) > 0 {
  2142. 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 "
  2143. insertParams := make([]string, 0)
  2144. insertData := make([]interface{}, 0)
  2145. for _, info := range cancelStockInfo {
  2146. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2147. insertData = append(insertData, info.GoodId)
  2148. insertData = append(insertData, info.CancelStockId)
  2149. insertData = append(insertData, info.GoodTypeId)
  2150. insertData = append(insertData, info.Count)
  2151. insertData = append(insertData, info.Price)
  2152. insertData = append(insertData, info.Total)
  2153. insertData = append(insertData, info.Ctime)
  2154. insertData = append(insertData, info.Status)
  2155. insertData = append(insertData, info.OrgId)
  2156. insertData = append(insertData, info.OrderNumber)
  2157. insertData = append(insertData, info.Type)
  2158. insertData = append(insertData, info.Dealer)
  2159. insertData = append(insertData, info.Manufacturer)
  2160. insertData = append(insertData, info.Number)
  2161. insertData = append(insertData, info.RegisterAccount)
  2162. insertData = append(insertData, info.Remark)
  2163. insertData = append(insertData, info.ProductDate)
  2164. insertData = append(insertData, info.ExpiryDate)
  2165. insertData = append(insertData, info.WarehouseInfoId)
  2166. insertData = append(insertData, info.WarehouseInfoId)
  2167. }
  2168. thisSQL += strings.Join(insertParams, ", ")
  2169. err = utx.Exec(thisSQL, insertData...).Error
  2170. if err != nil {
  2171. utx.Rollback()
  2172. return
  2173. }
  2174. }
  2175. utx.Commit()
  2176. }
  2177. return
  2178. }
  2179. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  2180. err := XTWriteDB().Create(&info).Error
  2181. return err
  2182. }
  2183. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  2184. // err := tx.Create(&info).Error
  2185. // return err
  2186. //}
  2187. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  2188. info := models.DrugCancelStockInfo{}
  2189. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2190. return info, err
  2191. }
  2192. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  2193. // info := models.DrugCancelStockInfo{}
  2194. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2195. // return info, err
  2196. //}
  2197. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  2198. err := XTWriteDB().Create(&info).Error
  2199. return err
  2200. }
  2201. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  2202. err := XTWriteDB().Create(&info).Error
  2203. if err != nil {
  2204. tx.Rollback()
  2205. return err
  2206. }
  2207. return err
  2208. }
  2209. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  2210. info := models.CancelStockInfo{}
  2211. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2212. return info, err
  2213. }
  2214. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  2215. info := models.CancelStockInfo{}
  2216. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2217. if err != gorm.ErrRecordNotFound {
  2218. if err != nil {
  2219. tx.Rollback()
  2220. return info, err
  2221. }
  2222. }
  2223. return info, err
  2224. }
  2225. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  2226. info := models.DrugCancelStockInfo{}
  2227. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  2228. return info, err
  2229. }
  2230. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  2231. if len(cancelStockInfo) > 0 {
  2232. utx := writeDb.Begin()
  2233. if len(cancelStockInfo) > 0 {
  2234. 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 "
  2235. insertParams := make([]string, 0)
  2236. insertData := make([]interface{}, 0)
  2237. for _, info := range cancelStockInfo {
  2238. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2239. insertData = append(insertData, info.DrugId)
  2240. insertData = append(insertData, info.CancelStockId)
  2241. insertData = append(insertData, info.Count)
  2242. insertData = append(insertData, info.Price)
  2243. insertData = append(insertData, info.Total)
  2244. insertData = append(insertData, info.Ctime)
  2245. insertData = append(insertData, info.Status)
  2246. insertData = append(insertData, info.OrgId)
  2247. insertData = append(insertData, info.OrderNumber)
  2248. insertData = append(insertData, info.Type)
  2249. insertData = append(insertData, info.Dealer)
  2250. insertData = append(insertData, info.Manufacturer)
  2251. insertData = append(insertData, info.RetailPrice)
  2252. insertData = append(insertData, info.RetailTotalPrice)
  2253. insertData = append(insertData, info.RegisterAccount)
  2254. insertData = append(insertData, info.BatchNumber)
  2255. insertData = append(insertData, info.Remark)
  2256. insertData = append(insertData, info.MaxUnit)
  2257. insertData = append(insertData, info.ProductDate)
  2258. insertData = append(insertData, info.ExpiryDate)
  2259. insertData = append(insertData, info.BatchNumberId)
  2260. }
  2261. thisSQL += strings.Join(insertParams, ", ")
  2262. err = utx.Exec(thisSQL, insertData...).Error
  2263. if err != nil {
  2264. utx.Rollback()
  2265. return
  2266. }
  2267. }
  2268. utx.Commit()
  2269. }
  2270. return
  2271. }
  2272. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  2273. db := readDb.Model(&models.CancelStock{})
  2274. db = db.Where("org_id = ? AND status = 1", orgId)
  2275. err = db.Order("ctime desc").Find(&list).Error
  2276. return
  2277. }
  2278. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2279. db := readDb.Model(&models.WarehousingInfo{})
  2280. db = db.Where("org_id = ? AND status = 1", orgId)
  2281. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2282. if startTime > 0 {
  2283. db = db.Where("ctime >=?", startTime)
  2284. }
  2285. if endTime > 0 {
  2286. db = db.Where("ctime<= ?", endTime)
  2287. }
  2288. db = db.Count(&total)
  2289. offset := (page - 1) * limit
  2290. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2291. return
  2292. }
  2293. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2294. db := XTReadDB().Model(&models.StockInfo{})
  2295. if startime > 0 {
  2296. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2297. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2298. })
  2299. }
  2300. if startime == 0 || endtime == 0 {
  2301. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2302. return db.Where("org_id = ? AND status = 1", orgId)
  2303. })
  2304. }
  2305. if endtime > 0 {
  2306. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2307. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2308. })
  2309. }
  2310. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2311. db = db.Group("xt_good_information.id")
  2312. if startime == 0 || endtime == 0 {
  2313. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2314. return db.Where("org_id = ? AND status = 1", orgId)
  2315. })
  2316. }
  2317. if startime > 0 {
  2318. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2319. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2320. })
  2321. }
  2322. if endtime > 0 {
  2323. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2324. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2325. })
  2326. }
  2327. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2328. if startime == 0 || endtime == 0 {
  2329. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2330. }
  2331. if startime > 0 {
  2332. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2333. }
  2334. if endtime > 0 {
  2335. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2336. }
  2337. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2338. if len(keyword) > 0 {
  2339. likeKey := "%" + keyword + "%"
  2340. 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)
  2341. 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")
  2342. }
  2343. if type_name > 0 {
  2344. 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)
  2345. }
  2346. db = db.Count(&total)
  2347. offset := (page - 1) * limit
  2348. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2349. return
  2350. }
  2351. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2352. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2353. return
  2354. }
  2355. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2356. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2357. return
  2358. }
  2359. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2360. 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
  2361. return
  2362. }
  2363. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2364. 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
  2365. return
  2366. }
  2367. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2368. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2369. "good_id": warehouseInfo.GoodId,
  2370. "number": warehouseInfo.Number,
  2371. "product_date": warehouseInfo.ProductDate,
  2372. "expiry_date": warehouseInfo.ExpiryDate,
  2373. "warehousing_count": warehouseInfo.WarehousingCount,
  2374. "price": warehouseInfo.Price,
  2375. "total_price": warehouseInfo.TotalPrice,
  2376. "mtime": warehouseInfo.Mtime,
  2377. "remark": warehouseInfo.Remark,
  2378. }).Error
  2379. return
  2380. }
  2381. func UpDateWarehouseStatus(id int64) (err error) {
  2382. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2383. return
  2384. }
  2385. func UpdatedStockFlowById(id int64) (err error) {
  2386. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2387. return
  2388. }
  2389. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2390. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2391. return
  2392. }
  2393. func UpdateDrugFlowById(id int64) (err error) {
  2394. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2395. return
  2396. }
  2397. func DeleteWarehouse(ids []string) (err error) {
  2398. ut := writeDb.Begin()
  2399. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2400. if err != nil {
  2401. ut.Rollback()
  2402. return
  2403. }
  2404. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2405. if err != nil {
  2406. ut.Rollback()
  2407. return
  2408. }
  2409. ut.Commit()
  2410. return
  2411. }
  2412. func DeleteStockFlow(ids []string) (err error) {
  2413. ut := writeDb.Begin()
  2414. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2415. if err != nil {
  2416. ut.Rollback()
  2417. return
  2418. }
  2419. ut.Commit()
  2420. return
  2421. }
  2422. func DeleteDrugWarehouse(ids []string) (err error) {
  2423. ut := writeDb.Begin()
  2424. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2425. if err != nil {
  2426. ut.Rollback()
  2427. return
  2428. }
  2429. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2430. if err != nil {
  2431. ut.Rollback()
  2432. return
  2433. }
  2434. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2435. if err != nil {
  2436. ut.Rollback()
  2437. return
  2438. }
  2439. ut.Commit()
  2440. return
  2441. }
  2442. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2443. warehouse := &models.Warehousing{}
  2444. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2445. return warehouse, err
  2446. }
  2447. func FindWareHouseOutByIdOne(id int64) (*models.WarehouseOut, error) {
  2448. warehouseOut := &models.WarehouseOut{}
  2449. err := XTReadDB().Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2450. return warehouseOut, err
  2451. }
  2452. func FindWareHouseOutById(id int64, tx *gorm.DB) (*models.WarehouseOut, error) {
  2453. warehouseOut := &models.WarehouseOut{}
  2454. err := tx.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2455. if err != gorm.ErrRecordNotFound {
  2456. if err != nil {
  2457. tx.Rollback()
  2458. return warehouseOut, err
  2459. }
  2460. }
  2461. return warehouseOut, err
  2462. }
  2463. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2464. warehouseOut := &models.DrugWarehouseOut{}
  2465. 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
  2466. return warehouseOut, err
  2467. }
  2468. func FindNewDrugWareHouseOutById(id int64, org_id int64, tx *gorm.DB) (*models.DrugWarehouseOut, error) {
  2469. warehouseOut := &models.DrugWarehouseOut{}
  2470. err := tx.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
  2471. if err != nil {
  2472. tx.Rollback()
  2473. return warehouseOut, err
  2474. }
  2475. return warehouseOut, err
  2476. }
  2477. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2478. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2479. return
  2480. }
  2481. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2482. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2483. return
  2484. }
  2485. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2486. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2487. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2488. return
  2489. }
  2490. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2491. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2492. return
  2493. }
  2494. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2495. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2496. if manufacturer_id > 0 {
  2497. db = db.Where("manufacturer = ?", manufacturer_id)
  2498. }
  2499. if dealer_id > 0 {
  2500. db = db.Where("dealer = ?", dealer_id)
  2501. }
  2502. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2503. return goodInfo, err
  2504. }
  2505. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2506. db := readDb.Model(&models.Warehousing{})
  2507. likekey := "%" + keywords + "%"
  2508. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2509. db = db.Where("warehousing_order LIKE ? ", likekey)
  2510. db = db.Count(&total)
  2511. offset := (page - 1) * limit
  2512. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2513. return
  2514. }
  2515. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2516. info := models.WarehousingInfo{}
  2517. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2518. return info, err
  2519. }
  2520. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2521. info := models.WarehousingInfo{}
  2522. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2523. return info, err
  2524. }
  2525. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2526. err = writeDb.Save(&info).Error
  2527. return err
  2528. }
  2529. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2530. err = writeDb.Save(&info).Error
  2531. return err
  2532. }
  2533. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2534. 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
  2535. return err
  2536. }
  2537. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2538. 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
  2539. return err
  2540. }
  2541. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2542. info := models.DrugWarehouseInfo{}
  2543. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2544. return info, err
  2545. }
  2546. func EditWarehousing(warehouse models.Warehousing) {
  2547. 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
  2548. }
  2549. func EditNewWarehousing(warehouse models.Warehousing, tx *gorm.DB) {
  2550. err = tx.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
  2551. if err != nil {
  2552. tx.Rollback()
  2553. return
  2554. }
  2555. return
  2556. }
  2557. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2558. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2559. 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)
  2560. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2561. return list, err
  2562. }
  2563. func DeleteSalesReturn(ids []string) (err error) {
  2564. ut := writeDb.Begin()
  2565. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2566. if err != nil {
  2567. ut.Rollback()
  2568. return
  2569. }
  2570. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2571. if err != nil {
  2572. ut.Rollback()
  2573. return
  2574. }
  2575. ut.Commit()
  2576. return
  2577. }
  2578. func DeleteDrugSalesReturn(ids []string) (err error) {
  2579. ut := writeDb.Begin()
  2580. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2581. if err != nil {
  2582. ut.Rollback()
  2583. return
  2584. }
  2585. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2586. if err != nil {
  2587. ut.Rollback()
  2588. return
  2589. }
  2590. ut.Commit()
  2591. return
  2592. }
  2593. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2594. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2595. return
  2596. }
  2597. type DrugSalesReturnInfo struct {
  2598. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2599. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2600. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2601. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2602. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2603. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2604. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2605. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2606. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2607. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2608. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2609. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2610. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2611. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2612. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2613. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2614. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2615. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2616. Number string `gorm:"column:number" json:"number" form:"number"`
  2617. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2618. }
  2619. func (DrugSalesReturnInfo) TableName() string {
  2620. return "xt_drug_sales_return_info"
  2621. }
  2622. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2623. 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
  2624. return
  2625. }
  2626. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2627. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2628. return
  2629. }
  2630. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2631. 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
  2632. return
  2633. }
  2634. func UpDateSaleReturnStatus(id int64) (err error) {
  2635. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2636. return
  2637. }
  2638. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2639. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2640. return
  2641. }
  2642. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2643. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2644. return salesReturn, err
  2645. }
  2646. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2647. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2648. return salesReturn, err
  2649. }
  2650. func EditSaleReturn(sales models.SalesReturn) {
  2651. 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
  2652. }
  2653. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2654. 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
  2655. }
  2656. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2657. err = writeDb.Save(&info).Error
  2658. return err
  2659. }
  2660. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2661. err = writeDb.Save(&info).Error
  2662. return err
  2663. }
  2664. func DeleteWarehouseOut(ids []string) (err error) {
  2665. ut := writeDb.Begin()
  2666. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2667. if err != nil {
  2668. ut.Rollback()
  2669. return
  2670. }
  2671. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2672. if err != nil {
  2673. ut.Rollback()
  2674. return
  2675. }
  2676. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2677. if err != nil {
  2678. ut.Rollback()
  2679. return
  2680. }
  2681. ut.Commit()
  2682. return
  2683. }
  2684. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2685. ut := writeDb.Begin()
  2686. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2687. if err != nil {
  2688. ut.Rollback()
  2689. return
  2690. }
  2691. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2692. if err != nil {
  2693. ut.Rollback()
  2694. return
  2695. }
  2696. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2697. if err != nil {
  2698. ut.Rollback()
  2699. return
  2700. }
  2701. ut.Commit()
  2702. return
  2703. }
  2704. func UpDateWarehouseOutStatus(id int64) (err error) {
  2705. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2706. return
  2707. }
  2708. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2709. ut := writeDb.Begin()
  2710. err = ut.Save(&info).Error
  2711. if err != nil {
  2712. ut.Rollback()
  2713. return err
  2714. }
  2715. ut.Commit()
  2716. return err
  2717. }
  2718. func UpdateDrugWarehouseInfoSumCount(id int64, deliver_number int64, tx *gorm.DB) error {
  2719. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status=1 and is_check=1", id).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", deliver_number)).Error
  2720. if err != nil {
  2721. tx.Rollback()
  2722. return err
  2723. }
  2724. return err
  2725. }
  2726. func UpdateDrugWarehouseInfoSumCountOne(id int64, deliver_number int64) error {
  2727. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status=1 and is_check=1", id).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", deliver_number)).Error
  2728. return err
  2729. }
  2730. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2731. err = tx.Save(&info).Error
  2732. if err != nil {
  2733. tx.Rollback()
  2734. return err
  2735. }
  2736. return err
  2737. }
  2738. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2739. ut := writeDb.Begin()
  2740. err = ut.Save(&info).Error
  2741. if err != nil {
  2742. ut.Rollback()
  2743. return err
  2744. }
  2745. ut.Commit()
  2746. return err
  2747. }
  2748. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2749. err = tx.Save(&info).Error
  2750. if err != nil {
  2751. tx.Rollback()
  2752. return err
  2753. }
  2754. return err
  2755. }
  2756. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2757. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2758. return
  2759. }
  2760. func EditWarehouseOut(warehouseOut models.WarehouseOut, tx *gorm.DB) {
  2761. err = tx.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
  2762. if err != nil {
  2763. tx.Rollback()
  2764. return
  2765. }
  2766. return
  2767. }
  2768. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2769. 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
  2770. }
  2771. func EditNewDrugWarehouseOut(warehouseOut models.DrugWarehouseOut, tx *gorm.DB) {
  2772. err = tx.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
  2773. if err != nil {
  2774. tx.Rollback()
  2775. return
  2776. }
  2777. return
  2778. }
  2779. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) (err error) {
  2780. err = tx.Save(&info).Error
  2781. if err != nil {
  2782. tx.Rollback()
  2783. return err
  2784. }
  2785. return err
  2786. }
  2787. 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) {
  2788. db := readDb.Model(&models.CancelStock{})
  2789. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2790. if len(keywords) > 0 {
  2791. likeKey := "%" + keywords + "%"
  2792. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2793. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2794. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2795. }
  2796. if startTime > 0 {
  2797. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2798. }
  2799. if endTime > 0 {
  2800. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2801. }
  2802. if storehouse_id > 0 {
  2803. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2804. }
  2805. if check_type > 0 {
  2806. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2807. }
  2808. db = db.Count(&total)
  2809. offset := (page - 1) * limit
  2810. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2811. return
  2812. }
  2813. 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) {
  2814. db := readDb.Model(&models.DrugCancelStock{})
  2815. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2816. if len(keywords) > 0 {
  2817. likeKey := "%" + keywords + "%"
  2818. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2819. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2820. 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")
  2821. }
  2822. if startTime > 0 {
  2823. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2824. }
  2825. if endTime > 0 {
  2826. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2827. }
  2828. if storehoue_id > 0 {
  2829. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2830. }
  2831. if check_type > 0 {
  2832. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2833. }
  2834. db = db.Count(&total)
  2835. offset := (page - 1) * limit
  2836. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2837. return
  2838. }
  2839. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2840. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2841. return info, err
  2842. }
  2843. func DeleteCancelStock(ids []string) (err error) {
  2844. ut := writeDb.Begin()
  2845. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2846. if err != nil {
  2847. ut.Rollback()
  2848. return
  2849. }
  2850. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2851. if err != nil {
  2852. ut.Rollback()
  2853. return
  2854. }
  2855. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2856. if err != nil {
  2857. ut.Rollback()
  2858. return
  2859. }
  2860. ut.Commit()
  2861. return
  2862. }
  2863. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2864. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2865. return info, err
  2866. }
  2867. func DeleteDrugCancelStock(ids []string) (err error) {
  2868. ut := writeDb.Begin()
  2869. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2870. if err != nil {
  2871. ut.Rollback()
  2872. return
  2873. }
  2874. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2875. if err != nil {
  2876. ut.Rollback()
  2877. return
  2878. }
  2879. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2880. if err != nil {
  2881. ut.Rollback()
  2882. return
  2883. }
  2884. ut.Commit()
  2885. return
  2886. }
  2887. func UpDateCancleStockStatus(id int64) (err error) {
  2888. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2889. return
  2890. }
  2891. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2892. info := models.CancelStockInfo{}
  2893. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2894. return info, err
  2895. }
  2896. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2897. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2898. return err
  2899. }
  2900. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2901. 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
  2902. return
  2903. }
  2904. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2905. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2906. return
  2907. }
  2908. func UpDateDrugCancelStockById(id int64) (err error) {
  2909. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2910. return
  2911. }
  2912. func EditCancelStock(sales models.CancelStock) {
  2913. 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
  2914. }
  2915. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2916. 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
  2917. }
  2918. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2919. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2920. return cancelStock, err
  2921. }
  2922. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2923. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2924. return cancelStock, err
  2925. }
  2926. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2927. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2928. return cancelStock, err
  2929. }
  2930. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2931. err = writeDb.Save(&info).Error
  2932. return err
  2933. }
  2934. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2935. err = writeDb.Save(&info).Error
  2936. return err
  2937. }
  2938. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2939. 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
  2940. return list, err
  2941. }
  2942. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2943. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2944. 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)
  2945. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2946. return list, err
  2947. }
  2948. type StockDetailModel struct {
  2949. GoodId int64 `gorm:"good_id" json:"good_id"`
  2950. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2951. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2952. Price float64 `gorm:"price" json:"price"`
  2953. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2954. Type int `gorm:"type" json:"type"`
  2955. Creater int64 `gorm:"creater" json:"creater"`
  2956. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2957. Types int64 `gorm:"types" json:"types"`
  2958. }
  2959. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.WarehousingInfo, total int64, err error) {
  2960. db := readDb.Model(&models.WarehousingInfo{})
  2961. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2962. if good_id > 0 {
  2963. db = db.Where("xt_warehouse_info.good_id = ?", good_id)
  2964. }
  2965. if len(keywords) > 0 {
  2966. likeKey := "%" + keywords + "%"
  2967. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2968. 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)
  2969. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2970. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2971. 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")
  2972. } else {
  2973. 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)
  2974. }
  2975. if manufacturer > 0 {
  2976. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2977. }
  2978. if storehouse_id > 0 {
  2979. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2980. }
  2981. if order_type > 0 {
  2982. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2983. }
  2984. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2985. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2986. db = db.Count(&total)
  2987. offset := (page - 1) * limit
  2988. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2989. return
  2990. }
  2991. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (err error, totalPrice float64) {
  2992. db := readDb.Model(&models.WarehousingInfo{})
  2993. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2994. if good_id > 0 {
  2995. db = db.Where("good_id = ?", good_id)
  2996. }
  2997. if len(keywords) > 0 {
  2998. likeKey := "%" + keywords + "%"
  2999. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  3000. 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)
  3001. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  3002. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  3003. 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")
  3004. } else {
  3005. 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)
  3006. }
  3007. if manufacturer > 0 {
  3008. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  3009. }
  3010. if order_type > 0 {
  3011. db = db.Where("xt_warehouse_info.type = ?", order_type)
  3012. }
  3013. if storehouse_id > 0 {
  3014. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  3015. }
  3016. type PriceAmount struct {
  3017. Total float64
  3018. }
  3019. var result []PriceAmount
  3020. err = db.Select("total_price as total").Scan(&result).Error
  3021. var total float64
  3022. for _, value := range result {
  3023. total = total + value.Total
  3024. }
  3025. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3026. return err, total
  3027. }
  3028. func FloatRound(f float64, n int) float64 {
  3029. format := "%." + strconv.Itoa(n) + "f"
  3030. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  3031. return res
  3032. }
  3033. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64, order_way int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  3034. if orgId == 9671 {
  3035. db := readDb.Model(&models.WarehouseOutInfo{})
  3036. 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)
  3037. if len(keywords) > 0 {
  3038. likeKey := "%" + keywords + "%"
  3039. 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)
  3040. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3041. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3042. 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")
  3043. } else {
  3044. 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)
  3045. }
  3046. if manufacturer > 0 {
  3047. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3048. }
  3049. if order_type > 0 {
  3050. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3051. }
  3052. if storehouse_id > 0 {
  3053. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3054. }
  3055. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  3056. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  3057. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3058. db = db.Count(&total)
  3059. offset := (page - 1) * limit
  3060. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3061. return
  3062. } else {
  3063. db := readDb.Model(&models.WarehouseOutInfo{})
  3064. 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.count <> 0 ", orgId)
  3065. if good_id > 0 {
  3066. db = db.Where("good_id = ?", good_id)
  3067. }
  3068. if order_way == 1 {
  3069. db = db.Where("xt_warehouse_out_info.is_sys =0")
  3070. }
  3071. if order_way == 2 {
  3072. db = db.Where("xt_warehouse_out_info.is_sys !=0")
  3073. }
  3074. if len(keywords) > 0 {
  3075. likeKey := "%" + keywords + "%"
  3076. 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)
  3077. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3078. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3079. 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")
  3080. } else {
  3081. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id and xt_warehouse_out.org_id = xt_warehouse_out_info.org_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)
  3082. }
  3083. if manufacturer > 0 {
  3084. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3085. }
  3086. if order_type > 0 {
  3087. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3088. }
  3089. if storehouse_id > 0 {
  3090. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3091. }
  3092. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ? and is_check =1", orgId)
  3093. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ? and is_check =1", orgId)
  3094. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3095. db = db.Count(&total)
  3096. offset := (page - 1) * limit
  3097. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3098. return
  3099. }
  3100. }
  3101. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64, order_way int64) (err error, totalPrice float64) {
  3102. db := readDb.Model(&models.WarehouseOutInfo{})
  3103. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.count > 0", orgId)
  3104. if len(keywords) > 0 {
  3105. likeKey := "%" + keywords + "%"
  3106. 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)
  3107. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3108. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3109. 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")
  3110. } else {
  3111. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id and xt_warehouse_out.org_id = xt_warehouse_out_info.org_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)
  3112. }
  3113. if good_id > 0 {
  3114. db = db.Where("xt_warehouse_out_info.good_id = ?", good_id)
  3115. }
  3116. if order_way == 1 {
  3117. db = db.Where("xt_warehouse_out_info.is_sys = ?", 0)
  3118. }
  3119. if order_way == 2 {
  3120. db = db.Where("xt_warehouse_out_info.is_sys != ?", 0)
  3121. }
  3122. if manufacturer > 0 {
  3123. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3124. }
  3125. if order_type > 0 {
  3126. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3127. }
  3128. if storehouse_id > 0 {
  3129. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3130. }
  3131. type PriceAmount struct {
  3132. Total float64
  3133. }
  3134. var result []PriceAmount
  3135. err = db.Select("(count * price) as total").Scan(&result).Error
  3136. var total float64
  3137. for _, value := range result {
  3138. total = total + value.Total
  3139. }
  3140. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3141. fmt.Println(total)
  3142. return err, total
  3143. }
  3144. 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) {
  3145. db := readDb.Model(&models.SalesReturnInfo{})
  3146. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  3147. if len(keywords) > 0 {
  3148. likeKey := "%" + keywords + "%"
  3149. 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)
  3150. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  3151. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  3152. 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")
  3153. } else {
  3154. 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)
  3155. }
  3156. if manufacturer > 0 {
  3157. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  3158. }
  3159. if order_type > 0 {
  3160. db = db.Where("xt_sales_return_info.type = ?", order_type)
  3161. }
  3162. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  3163. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3164. offset := (page - 1) * limit
  3165. db = db.Count(&total)
  3166. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  3167. return
  3168. }
  3169. func FindeCancelGroup(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.CancelStockInfo, err error) {
  3170. db := readDb.Model(&models.CancelStockInfo{})
  3171. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3172. if len(keywords) > 0 {
  3173. likeKey := "%" + keywords + "%"
  3174. 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)
  3175. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3176. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3177. 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")
  3178. } else {
  3179. 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)
  3180. }
  3181. if manufacturer > 0 {
  3182. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3183. }
  3184. if order_type > 0 {
  3185. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3186. }
  3187. if storehouse_id > 0 {
  3188. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  3189. }
  3190. if good_id > 0 {
  3191. db = db.Where("xt_cancel_stock_info.good_id = ?", good_id)
  3192. }
  3193. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3194. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3195. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  3196. return
  3197. }
  3198. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.CancelStockInfo, total int64, err error) {
  3199. db := readDb.Model(&models.CancelStockInfo{})
  3200. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3201. if len(keywords) > 0 {
  3202. likeKey := "%" + keywords + "%"
  3203. 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)
  3204. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3205. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3206. 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")
  3207. } else {
  3208. 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)
  3209. }
  3210. if manufacturer > 0 {
  3211. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3212. }
  3213. if order_type > 0 {
  3214. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3215. }
  3216. if storehouse_id > 0 {
  3217. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  3218. }
  3219. if good_id > 0 {
  3220. db = db.Where("xt_cancel_stock_info.good_id =?", good_id)
  3221. }
  3222. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3223. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3224. db = db.Count(&total)
  3225. offset := (page - 1) * limit
  3226. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  3227. return
  3228. }
  3229. func FindStockInByDealerId(id int64) (total int64, err error) {
  3230. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  3231. return
  3232. }
  3233. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  3234. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  3235. return
  3236. }
  3237. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  3238. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  3239. return
  3240. }
  3241. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  3242. 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)
  3243. return
  3244. }
  3245. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  3246. 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)
  3247. return
  3248. }
  3249. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  3250. 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)
  3251. return
  3252. }
  3253. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  3254. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  3255. return goodType, err
  3256. }
  3257. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  3258. 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
  3259. return info, err
  3260. }
  3261. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  3262. 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
  3263. return
  3264. }
  3265. func FindStockOutByIsSysOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (out models.WarehouseOut, err error) {
  3266. err = tx.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).First(&out).Error
  3267. if err != gorm.ErrRecordNotFound {
  3268. if err != nil {
  3269. tx.Rollback()
  3270. return
  3271. }
  3272. }
  3273. return
  3274. }
  3275. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  3276. 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
  3277. return
  3278. }
  3279. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3280. 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
  3281. return
  3282. }
  3283. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  3284. 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
  3285. if err != gorm.ErrRecordNotFound {
  3286. if err != nil {
  3287. tx.Rollback()
  3288. return
  3289. }
  3290. }
  3291. return
  3292. }
  3293. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3294. 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
  3295. return
  3296. }
  3297. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  3298. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  3299. return
  3300. }
  3301. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  3302. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  3303. return
  3304. }
  3305. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3306. info := models.WarehouseOutInfo{}
  3307. 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
  3308. if err == gorm.ErrRecordNotFound {
  3309. return nil, err
  3310. }
  3311. if err != nil {
  3312. return nil, err
  3313. }
  3314. return &info, nil
  3315. }
  3316. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3317. info := models.WarehouseOutInfo{}
  3318. 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
  3319. if err == gorm.ErrRecordNotFound {
  3320. return nil, err
  3321. }
  3322. if err != nil {
  3323. return nil, err
  3324. }
  3325. return &info, nil
  3326. }
  3327. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3328. 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
  3329. return err
  3330. }
  3331. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3332. 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
  3333. return err
  3334. }
  3335. func GetGoodsIsExsit(good_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  3336. outInfo := models.WarehouseOutInfo{}
  3337. err := tx.Where("good_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", good_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3338. if err != gorm.ErrRecordNotFound {
  3339. if err != nil {
  3340. tx.Rollback()
  3341. return outInfo, err
  3342. }
  3343. }
  3344. return outInfo, err
  3345. }
  3346. func GetDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64) (models.XtDrugWarehouseOutInfo, error) {
  3347. outInfo := models.XtDrugWarehouseOutInfo{}
  3348. err := XTReadDB().Where("drug_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", drug_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3349. return outInfo, err
  3350. }
  3351. func GetNewDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.XtDrugWarehouseOutInfo, error) {
  3352. outInfo := models.XtDrugWarehouseOutInfo{}
  3353. err := tx.Where("drug_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", drug_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3354. if err != gorm.ErrRecordNotFound {
  3355. if err != nil {
  3356. tx.Rollback()
  3357. return outInfo, err
  3358. }
  3359. }
  3360. return outInfo, err
  3361. }
  3362. func AddSigleWarehouseOutInfoOne(info *models.WarehouseOutInfo) error {
  3363. err := XTWriteDB().Create(&info).Error
  3364. return err
  3365. }
  3366. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3367. err := tx.Create(&info).Error
  3368. if err != nil {
  3369. tx.Rollback()
  3370. return err
  3371. }
  3372. return err
  3373. }
  3374. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3375. err := tx.Create(&info).Error
  3376. if err != nil {
  3377. tx.Rollback()
  3378. return err
  3379. }
  3380. return err
  3381. }
  3382. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  3383. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3384. return err
  3385. }
  3386. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  3387. err := writeDb.Create(&info).Error
  3388. return err
  3389. }
  3390. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, tx *gorm.DB) error {
  3391. err := tx.Create(&info).Error
  3392. if err != nil {
  3393. tx.Rollback()
  3394. return err
  3395. }
  3396. return err
  3397. }
  3398. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  3399. err := tx.Create(&info).Error
  3400. if err != nil {
  3401. tx.Rollback()
  3402. return err
  3403. }
  3404. return err
  3405. }
  3406. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  3407. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3408. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  3409. if err != nil {
  3410. tx.Rollback()
  3411. return err
  3412. }
  3413. return err
  3414. }
  3415. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3416. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3417. return err
  3418. }
  3419. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3420. ut := writeDb.Begin()
  3421. err := ut.Create(&detail).Error
  3422. if err != nil {
  3423. ut.Rollback()
  3424. return err
  3425. }
  3426. ut.Commit()
  3427. return err
  3428. }
  3429. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3430. err := writeDb.Create(&detail).Error
  3431. return err
  3432. }
  3433. func GetDrugAutoMaticReduceDetail(drug_id int64, patient_id int64, record_time int64, org_id int64, adivce_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
  3434. detail := models.DrugAutomaticReduceDetail{}
  3435. err := tx.Where("drug_id = ? and patient_id =? and record_time =? and org_id = ? and advice_id = ? and warehouse_info_id =?", drug_id, patient_id, record_time, org_id, adivce_id, warehouse_info_id).Find(&detail).Error
  3436. if err != gorm.ErrRecordNotFound {
  3437. if err != nil {
  3438. tx.Rollback()
  3439. return detail, err
  3440. }
  3441. }
  3442. return detail, err
  3443. }
  3444. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3445. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3446. if err != nil {
  3447. tx.Rollback()
  3448. return err
  3449. }
  3450. return err
  3451. }
  3452. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3453. err := tx.Create(&detail).Error
  3454. if err != nil {
  3455. tx.Rollback()
  3456. return err
  3457. }
  3458. return err
  3459. }
  3460. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3461. 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
  3462. return
  3463. }
  3464. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3465. 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
  3466. return
  3467. }
  3468. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3469. 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
  3470. return
  3471. }
  3472. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3473. info := models.WarehouseOutInfo{}
  3474. 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
  3475. return info, err
  3476. }
  3477. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3478. 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
  3479. return
  3480. }
  3481. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3482. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3483. }
  3484. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3485. db := readDb.Model(&models.AutomaticReduceDetail{})
  3486. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3487. db = db.Preload("GoodsType", "status = 1")
  3488. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3489. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3490. db = db.Count(&total)
  3491. err = db.Find(&user).Error
  3492. return
  3493. }
  3494. type DrugAutomaticReduceDetail struct {
  3495. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3496. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3497. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3498. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3499. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3500. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3501. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3502. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3503. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3504. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3505. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3506. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3507. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3508. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3509. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3510. }
  3511. func (DrugAutomaticReduceDetail) TableName() string {
  3512. return "xt_drug_automatic_reduce_detail"
  3513. }
  3514. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3515. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3516. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3517. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3518. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3519. db = db.Count(&total)
  3520. err = db.Find(&user).Error
  3521. return
  3522. }
  3523. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3524. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3525. }
  3526. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3527. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3528. }
  3529. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3530. 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
  3531. return
  3532. }
  3533. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3534. 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
  3535. return
  3536. }
  3537. type VMWarehousingInfo struct {
  3538. ID int64 `gorm:"column:id" json:"id"`
  3539. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3540. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3541. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3542. Status int64 `gorm:"column:status" json:"status"`
  3543. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3544. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3545. }
  3546. func (VMWarehousingInfo) TableName() string {
  3547. return "xt_warehouse_info"
  3548. }
  3549. type GoodInfo struct {
  3550. ID int64 `gorm:"column:id" json:"id"`
  3551. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3552. Status int64 `gorm:"column:status" json:"status"`
  3553. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3554. }
  3555. func (GoodInfo) TableName() string {
  3556. return "xt_good_information"
  3557. }
  3558. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3559. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3560. return
  3561. }
  3562. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3563. 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
  3564. return
  3565. }
  3566. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3567. likeKey := "%" + keyword + "%"
  3568. db := XTReadDB().Model(&goods)
  3569. if len(keyword) > 0 {
  3570. db = db.Where("type_name like ?", likeKey)
  3571. }
  3572. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3573. return goods, err
  3574. }
  3575. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3576. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3577. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3578. return goodstype, err
  3579. }
  3580. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3581. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3582. if orgid > 0 {
  3583. db = db.Where("x.user_org_id = ?", orgid)
  3584. }
  3585. if recordtime > 0 {
  3586. db = db.Where("x.record_date = ?", recordtime)
  3587. }
  3588. if patientId > 0 {
  3589. db = db.Where("x.patient_id = ?", patientId)
  3590. }
  3591. 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
  3592. return prepare, err
  3593. }
  3594. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3595. detail := models.XtAutomaticReduceDetail{}
  3596. 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
  3597. if err == gorm.ErrRecordNotFound {
  3598. return nil, err
  3599. }
  3600. if err != nil {
  3601. return nil, err
  3602. }
  3603. return &detail, nil
  3604. }
  3605. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3606. 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
  3607. return autoMatic, err
  3608. }
  3609. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3610. 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
  3611. return autoMatic, err
  3612. }
  3613. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3614. var manufacturer models.Manufacturer
  3615. var err error
  3616. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3617. if err == gorm.ErrRecordNotFound {
  3618. return nil, err
  3619. }
  3620. if err != nil {
  3621. return nil, err
  3622. }
  3623. return &manufacturer, nil
  3624. }
  3625. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3626. err := XTWriteDB().Create(manufacturer).Error
  3627. return err
  3628. }
  3629. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3630. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3631. return err
  3632. }
  3633. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3634. var good models.GoodInfo
  3635. var err error
  3636. 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
  3637. if err == gorm.ErrRecordNotFound {
  3638. return nil, err
  3639. }
  3640. if err != nil {
  3641. return nil, err
  3642. }
  3643. return &good, nil
  3644. }
  3645. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3646. err := XTWriteDB().Create(&info).Error
  3647. return err
  3648. }
  3649. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3650. 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
  3651. return err
  3652. }
  3653. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3654. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3655. 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
  3656. return houseOutInfo, err
  3657. }
  3658. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3659. key := "%" + keyword + "%"
  3660. if len(keyword) == 0 {
  3661. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3662. } else {
  3663. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3664. }
  3665. return goodInfo, err
  3666. }
  3667. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3668. var drug models.BaseDrugLib
  3669. var err error
  3670. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3671. if err == gorm.ErrRecordNotFound {
  3672. return nil, err
  3673. }
  3674. if err != nil {
  3675. return nil, err
  3676. }
  3677. return &drug, nil
  3678. }
  3679. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3680. var drug models.BaseDrugLib
  3681. var err error
  3682. 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
  3683. if err == gorm.ErrRecordNotFound {
  3684. return nil, err
  3685. }
  3686. if err != nil {
  3687. return nil, err
  3688. }
  3689. return &drug, nil
  3690. }
  3691. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3692. err := XTWriteDB().Create(&info).Error
  3693. return err
  3694. }
  3695. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3696. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "single_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  3697. return err
  3698. }
  3699. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3700. var projoect models.HisProject
  3701. var err error
  3702. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3703. if err == gorm.ErrRecordNotFound {
  3704. return nil, err
  3705. }
  3706. if err != nil {
  3707. return nil, err
  3708. }
  3709. return &projoect, nil
  3710. }
  3711. func CreateProjectInfomation(info *models.HisProject) error {
  3712. err := XTWriteDB().Create(&info).Error
  3713. return err
  3714. }
  3715. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3716. 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
  3717. return err
  3718. }
  3719. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3720. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3721. return warehouseinfo, err
  3722. }
  3723. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3724. 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
  3725. return err
  3726. }
  3727. func EditNewDrugWarehousingOne(warehouse models.DrugWarehouse, id int64, tx *gorm.DB) error {
  3728. err := tx.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
  3729. if err != nil {
  3730. tx.Rollback()
  3731. return err
  3732. }
  3733. return err
  3734. }
  3735. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3736. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3737. return list, err
  3738. }
  3739. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3740. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3741. return list, err
  3742. }
  3743. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3744. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3745. return list, err
  3746. }
  3747. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3748. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3749. // return list, err
  3750. // }
  3751. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3752. var dealer models.Dealer
  3753. var err error
  3754. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3755. if err == gorm.ErrRecordNotFound {
  3756. return nil, err
  3757. }
  3758. if err != nil {
  3759. return nil, err
  3760. }
  3761. return &dealer, nil
  3762. }
  3763. func CreateDealer(dealer *models.Dealer) error {
  3764. err := XTWriteDB().Create(&dealer).Error
  3765. return err
  3766. }
  3767. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3768. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3769. return err
  3770. }
  3771. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3772. dataconfig := models.DictDataconfig{}
  3773. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3774. return dataconfig, err
  3775. }
  3776. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3777. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3778. return dataconfig, err
  3779. }
  3780. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3781. var dataconfig models.DictDataconfig
  3782. var err error
  3783. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3784. if err == gorm.ErrRecordNotFound {
  3785. return nil, err
  3786. }
  3787. if err != nil {
  3788. return nil, err
  3789. }
  3790. return &dataconfig, nil
  3791. }
  3792. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3793. dataconfig := models.DictDataconfig{}
  3794. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3795. return dataconfig, err
  3796. }
  3797. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3798. err := XTWriteDB().Create(&dataconfig).Error
  3799. return err
  3800. }
  3801. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3802. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3803. return err
  3804. }
  3805. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3806. var goodType models.GoodsType
  3807. var err error
  3808. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3809. if err == gorm.ErrRecordNotFound {
  3810. return nil, err
  3811. }
  3812. if err != nil {
  3813. return nil, err
  3814. }
  3815. return &goodType, nil
  3816. }
  3817. func CreatedGoodType(goodsType *models.GoodsType) error {
  3818. err := XTWriteDB().Create(&goodsType).Error
  3819. return err
  3820. }
  3821. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3822. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3823. return goodsType, err
  3824. }
  3825. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3826. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3827. return goodsType, err
  3828. }
  3829. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3830. var department models.XtHisDepartment
  3831. var err error
  3832. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3833. if err == gorm.ErrRecordNotFound {
  3834. return nil, err
  3835. }
  3836. if err != nil {
  3837. return nil, err
  3838. }
  3839. return &department, nil
  3840. }
  3841. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3842. dataconfig := models.Dataconfig{}
  3843. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3844. return dataconfig, err
  3845. }
  3846. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3847. var dataconfig models.Dataconfig
  3848. var err error
  3849. 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
  3850. if err == gorm.ErrRecordNotFound {
  3851. return nil, err
  3852. }
  3853. if err != nil {
  3854. return nil, err
  3855. }
  3856. return &dataconfig, nil
  3857. }
  3858. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3859. dataconfig := models.Dataconfig{}
  3860. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3861. return dataconfig, err
  3862. }
  3863. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3864. err := XTWriteDB().Create(&dataconfig).Error
  3865. return err
  3866. }
  3867. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3868. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3869. return dataconfig, err
  3870. }
  3871. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3872. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3873. return list, err
  3874. }
  3875. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3876. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3877. if len(keywords) > 0 {
  3878. likekey := "%" + keywords + "%"
  3879. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3880. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3881. }).Find(&drug).Error
  3882. } else {
  3883. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3884. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3885. }).Find(&drug).Error
  3886. }
  3887. return
  3888. }
  3889. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3890. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3891. if len(keywords) > 0 {
  3892. likekey := "%" + keywords + "%"
  3893. 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 {
  3894. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3895. }).Find(&drug).Error
  3896. } else {
  3897. 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 {
  3898. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3899. }).Find(&drug).Error
  3900. }
  3901. return
  3902. }
  3903. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3904. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3905. if len(keywords) > 0 {
  3906. likekey := "%" + keywords + "%"
  3907. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3908. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3909. }).Find(&good).Error
  3910. } else {
  3911. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3912. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3913. }).Find(&good).Error
  3914. }
  3915. return
  3916. }
  3917. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3918. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3919. if len(keywords) > 0 {
  3920. likekey := "%" + keywords + "%"
  3921. 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 {
  3922. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3923. }).Find(&good).Error
  3924. } else {
  3925. 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 {
  3926. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3927. }).Find(&good).Error
  3928. }
  3929. return
  3930. }
  3931. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3932. db := writeDb.Begin()
  3933. err := db.Create(&detail).Error
  3934. if err != nil {
  3935. db.Rollback()
  3936. return err
  3937. }
  3938. db.Commit()
  3939. return err
  3940. }
  3941. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3942. var autoreduece models.BloodAutomaticReduceDetail
  3943. var err error
  3944. 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
  3945. if err == gorm.ErrRecordNotFound {
  3946. return nil, err
  3947. }
  3948. if err != nil {
  3949. return nil, err
  3950. }
  3951. return &autoreduece, nil
  3952. }
  3953. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3954. auto := models.BloodAutomaticReduceDetail{}
  3955. 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
  3956. return auto, err
  3957. }
  3958. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3959. detail := models.BloodAutomaticReduceDetail{}
  3960. 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
  3961. return err
  3962. }
  3963. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3964. 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
  3965. return stockflow, err
  3966. }
  3967. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3968. 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
  3969. 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
  3970. 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
  3971. return err
  3972. }
  3973. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3974. detail := models.BloodAutomaticReduceDetail{}
  3975. 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
  3976. info := models.WarehouseOutInfo{}
  3977. 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
  3978. prepare := models.DialysisBeforePrepare{}
  3979. 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
  3980. flow := models.VmStockFlow{}
  3981. 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
  3982. return err
  3983. }
  3984. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3985. detail := models.BloodAutomaticReduceDetail{}
  3986. 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
  3987. info := models.WarehouseOutInfo{}
  3988. 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
  3989. prepare := models.DialysisBeforePrepare{}
  3990. 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
  3991. flow := models.VmStockFlow{}
  3992. 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
  3993. return err
  3994. }
  3995. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3996. detail := models.BloodAutomaticReduceDetail{}
  3997. 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
  3998. info := models.WarehouseOutInfo{}
  3999. 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
  4000. prepare := models.DialysisBeforePrepare{}
  4001. 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
  4002. flow := models.VmStockFlow{}
  4003. 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
  4004. return err
  4005. }
  4006. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  4007. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4008. //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
  4009. return err
  4010. }
  4011. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  4012. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  4013. return info, err
  4014. }
  4015. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  4016. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  4017. return list, err
  4018. }
  4019. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  4020. 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
  4021. return err
  4022. }
  4023. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  4024. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4025. return err
  4026. }
  4027. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  4028. var autoreduece models.BloodAutomaticReduceDetail
  4029. var err error
  4030. 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
  4031. if err == gorm.ErrRecordNotFound {
  4032. return nil, err
  4033. }
  4034. if err != nil {
  4035. return nil, err
  4036. }
  4037. return &autoreduece, nil
  4038. }
  4039. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  4040. detail := models.WarehouseOutInfoSeven{}
  4041. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4042. return detail, err
  4043. }
  4044. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  4045. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4046. return detail, err
  4047. }
  4048. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  4049. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  4050. return project, err
  4051. }
  4052. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  4053. project := models.HisPrescriptionProject{}
  4054. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  4055. return project, err
  4056. }
  4057. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  4058. 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
  4059. return auto, err
  4060. }
  4061. func ModefyWarehouseInfo(count int64, id int64) error {
  4062. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4063. return err
  4064. }
  4065. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  4066. 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
  4067. 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
  4068. 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
  4069. //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
  4070. return err
  4071. }
  4072. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  4073. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4074. return
  4075. }
  4076. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  4077. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4078. return
  4079. }
  4080. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  4081. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4082. return info, err
  4083. }
  4084. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4085. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4086. return info, err
  4087. }
  4088. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4089. 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
  4090. return info, err
  4091. }
  4092. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  4093. offset := (page - 1) * limit
  4094. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  4095. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4096. fmt.Println(table)
  4097. if startime > 0 {
  4098. db = db.Where("x.ctime>=?", startime)
  4099. }
  4100. if endtime > 0 {
  4101. db = db.Where("x.ctime<=?", endtime)
  4102. }
  4103. likeKey := "%" + keyword + "%"
  4104. if len(likeKey) > 0 {
  4105. db = db.Where("t.good_name like ?", likeKey)
  4106. }
  4107. if orgid > 0 {
  4108. db = db.Where("x.org_id = ?", orgid)
  4109. }
  4110. if good_type > 0 {
  4111. db = db.Where("t.good_type_id = ?", good_type)
  4112. }
  4113. 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
  4114. return info, total, err
  4115. }
  4116. 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) {
  4117. offset := (page - 1) * limit
  4118. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  4119. likeKey := "%" + keyword + "%"
  4120. if len(likeKey) > 0 {
  4121. db = db.Where("x.good_name like ?", likeKey)
  4122. }
  4123. if orgid > 0 {
  4124. db = db.Where("x.org_id = ?", orgid)
  4125. }
  4126. if good_type > 0 {
  4127. db = db.Where("x.good_type_id = ?", good_type)
  4128. }
  4129. if good_id > 0 {
  4130. db = db.Where("x.id = ?", good_id)
  4131. }
  4132. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4133. if startime > 0 {
  4134. db = db.Where("ctime>=?", startime)
  4135. }
  4136. if endtime > 0 {
  4137. db = db.Where("ctime<=?", endtime)
  4138. }
  4139. if good_id > 0 {
  4140. db = db.Where("good_id = ?", good_id)
  4141. }
  4142. if storehouse_id > 0 {
  4143. db = db.Where("storehouse_id = ?", storehouse_id)
  4144. }
  4145. return db.Where("status = 1 and is_check = 1")
  4146. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4147. if startime > 0 {
  4148. db = db.Where("ctime>=?", startime)
  4149. }
  4150. if endtime > 0 {
  4151. db = db.Where("ctime<=?", endtime)
  4152. }
  4153. if good_id > 0 {
  4154. db = db.Where("good_id = ?", good_id)
  4155. }
  4156. if storehouse_id > 0 {
  4157. db = db.Where("storehouse_id = ?", storehouse_id)
  4158. }
  4159. return db.Where("status = 1").Group("good_id,storehouse_id")
  4160. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4161. if startime > 0 {
  4162. db = db.Where("ctime>=?", startime)
  4163. }
  4164. if endtime > 0 {
  4165. db = db.Where("ctime<=?", endtime)
  4166. }
  4167. return db.Where("status = 1")
  4168. }).Find(&info).Error
  4169. return info, total, err
  4170. }
  4171. 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) {
  4172. offset := (page - 1) * limit
  4173. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  4174. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4175. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  4176. fmt.Println(table, tables)
  4177. if startime > 0 {
  4178. db = db.Where("x.ctime>=?", startime)
  4179. }
  4180. if endtime > 0 {
  4181. db = db.Where("x.ctime<=?", endtime)
  4182. }
  4183. if startfirsttime > 0 {
  4184. db = db.Where("x.expiry_date >=?", startfirsttime)
  4185. }
  4186. if endfirsttime > 0 {
  4187. db = db.Where("x.expiry_date <=?", endfirsttime)
  4188. }
  4189. if orgid > 0 {
  4190. db = db.Where("x.org_id = ?", orgid)
  4191. }
  4192. if good_id > 0 {
  4193. db = db.Where("x.good_id = ?", good_id)
  4194. }
  4195. 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
  4196. return info, total, err
  4197. }
  4198. 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) {
  4199. offset := (page - 1) * limit
  4200. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  4201. if startime > 0 {
  4202. db = db.Where("x.ctime>=?", startime)
  4203. }
  4204. if endtime > 0 {
  4205. db = db.Where("x.ctime<=?", endtime)
  4206. }
  4207. if orgid > 0 {
  4208. db = db.Where("x.org_id = ?", orgid)
  4209. }
  4210. if good_id > 0 {
  4211. db = db.Where("x.good_id = ?", good_id)
  4212. }
  4213. if is_sys == 2 {
  4214. db = db.Where("x.is_sys = 1")
  4215. }
  4216. if is_sys == 3 {
  4217. db = db.Where("x.is_sys = 0")
  4218. }
  4219. 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
  4220. return info, total, err
  4221. }
  4222. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4223. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4224. if startime > 0 {
  4225. db = db.Where("x.ctime >=?", startime)
  4226. }
  4227. if endtime > 0 {
  4228. db = db.Where("x.ctime<=?", endtime)
  4229. }
  4230. if orgid > 0 {
  4231. db = db.Where("x.org_id = ?", orgid)
  4232. }
  4233. 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
  4234. return info, err
  4235. }
  4236. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4237. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4238. if startime > 0 {
  4239. db = db.Where("x.ctime >=?", startime)
  4240. }
  4241. if endtime > 0 {
  4242. db = db.Where("x.ctime<=?", endtime)
  4243. }
  4244. if orgid > 0 {
  4245. db = db.Where("x.org_id = ?", orgid)
  4246. }
  4247. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  4248. return info, err
  4249. }
  4250. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  4251. if endtime > 0 {
  4252. 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
  4253. }
  4254. if endtime == 0 {
  4255. 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
  4256. }
  4257. return autoMatic, err
  4258. }
  4259. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  4260. out := models.WarehouseOut{}
  4261. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  4262. return out, err
  4263. }
  4264. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4265. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4266. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4267. fmt.Println(table)
  4268. if id > 0 {
  4269. db = db.Where("x.warehouse_out_id = ?", id)
  4270. }
  4271. if orgid > 0 {
  4272. db = db.Where("x.org_id = ?", orgid)
  4273. }
  4274. 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
  4275. return out, err
  4276. }
  4277. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  4278. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  4279. if len(id) > 0 {
  4280. db = db.Where("x.id in(?)", id)
  4281. }
  4282. if orgid > 0 {
  4283. db = db.Where("x.org_id= ?", orgid)
  4284. }
  4285. 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
  4286. return out, err
  4287. }
  4288. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4289. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4290. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4291. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  4292. fmt.Println(table, tableOne)
  4293. if len(id) > 0 {
  4294. db = db.Where("x.warehouse_out_id in (?)", id)
  4295. }
  4296. if orgid > 0 {
  4297. db = db.Where("x.org_id = ?", orgid)
  4298. }
  4299. err = db.Select("x.id,x.warehouse_out_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,n.number,x.remark,x.license_number,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.warehouse_info_id,x.remark,x.patient_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price,x.register_number,x.class_type").Joins("left join xt_warehouse_info as n on n.id=x.warehouse_info_id").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.warehouse_out_id,x.good_id").Order("x.ctime desc").Scan(&out).Error
  4300. return out, err
  4301. }
  4302. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4303. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4304. if len(id) > 0 {
  4305. db = db.Where("warehouse_out_id in (?)", id)
  4306. }
  4307. if orgid > 0 {
  4308. db = db.Where("user_org_id = ?", orgid)
  4309. }
  4310. err = db.Group("warehousing_id").Find(&list).Error
  4311. return list, err
  4312. }
  4313. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4314. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4315. if len(id) > 0 {
  4316. db = db.Where("warehouse_out_id in (?)", id)
  4317. }
  4318. if orgid > 0 {
  4319. db = db.Where("user_org_id = ?", orgid)
  4320. }
  4321. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4322. return list, err
  4323. }
  4324. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4325. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  4326. if len(id) > 0 {
  4327. db = db.Where("warehouse_out_id in (?)", id)
  4328. }
  4329. if orgid > 0 {
  4330. db = db.Where("user_org_id = ?", orgid)
  4331. }
  4332. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4333. return list, err
  4334. }
  4335. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  4336. db := XTReadDB().Model(&info)
  4337. db = db.Preload("GoodInfo", "status = 1")
  4338. 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
  4339. return info, err
  4340. }
  4341. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  4342. db := readDb.Model(&models.AutomaticReduceDetail{})
  4343. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  4344. db = db.Preload("GoodsType", "status = 1")
  4345. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  4346. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  4347. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  4348. db = db.Count(&total)
  4349. err = db.Find(&user).Error
  4350. return
  4351. }
  4352. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  4353. info := models.GoodInfo{}
  4354. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4355. return info, err
  4356. }
  4357. func GetNewGoodInformationByGoodId(good_id int64, tx *gorm.DB) (models.GoodInfo, error) {
  4358. info := models.GoodInfo{}
  4359. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4360. if err != gorm.ErrRecordNotFound {
  4361. if err != nil {
  4362. tx.Rollback()
  4363. return info, err
  4364. }
  4365. }
  4366. return info, err
  4367. }
  4368. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  4369. info := models.GoodInformationThirty{}
  4370. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4371. return info, err
  4372. }
  4373. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  4374. info := models.GoodInformationThirty{}
  4375. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4376. if err != gorm.ErrRecordNotFound {
  4377. if err != nil {
  4378. tx.Rollback()
  4379. return info, err
  4380. }
  4381. }
  4382. return info, err
  4383. }
  4384. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  4385. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  4386. return err
  4387. }
  4388. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  4389. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4390. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4391. fmt.Println(table)
  4392. if orgid > 0 {
  4393. db = db.Where("x.org_id = ?", orgid)
  4394. }
  4395. if len(orderid) > 0 {
  4396. db = db.Where("x.warehousing_id in(?)", orderid)
  4397. }
  4398. if startime > 0 {
  4399. db = db.Where("x.ctime>=?", startime)
  4400. }
  4401. if endtime > 0 {
  4402. db = db.Where("x.ctime<=?", endtime)
  4403. }
  4404. 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
  4405. return info, err
  4406. }
  4407. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  4408. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4409. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4410. fmt.Println(table)
  4411. if orgid > 0 {
  4412. db = db.Where("x.org_id = ?", orgid)
  4413. }
  4414. if len(orderid) > 0 {
  4415. db = db.Where("x.warehousing_id in(?)", orderid)
  4416. }
  4417. if startime > 0 {
  4418. db = db.Where("x.ctime>=?", startime)
  4419. }
  4420. if endtime > 0 {
  4421. db = db.Where("x.ctime<=?", endtime)
  4422. }
  4423. if storehouse_id > 0 {
  4424. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4425. }
  4426. 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
  4427. return info, err
  4428. }
  4429. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4430. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4431. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  4432. fmt.Println(table)
  4433. if startime > 0 {
  4434. db = db.Where("x.ctime>=?", startime)
  4435. }
  4436. if endtime > 0 {
  4437. db = db.Where("x.ctime<=?", endtime)
  4438. }
  4439. if orgid > 0 {
  4440. db = db.Where("x.org_id = ?", orgid)
  4441. }
  4442. 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
  4443. return info, err
  4444. }
  4445. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4446. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4447. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4448. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4449. fmt.Println(table, tableOne)
  4450. if orgid > 0 {
  4451. db = db.Where("x.org_id = ?", orgid)
  4452. }
  4453. if orgid > 0 {
  4454. db = db.Where("t.org_id = ?", orgid)
  4455. }
  4456. if orgid > 0 {
  4457. db = db.Where("p.org_id = ?", orgid)
  4458. }
  4459. if len(ids) > 0 {
  4460. db = db.Where("x.warehouse_out_id in (?)", ids)
  4461. }
  4462. if startime > 0 {
  4463. db = db.Where("x.ctime>=?", startime)
  4464. }
  4465. if endtime > 0 {
  4466. db = db.Where("x.ctime<=?", endtime)
  4467. }
  4468. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,x.warehouse_out_order_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,x.manufacturer,x.dealer,p.warehouse_out_time").Joins("left join xt_warehouse_out as p on p.id =x.warehouse_out_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  4469. return out, err
  4470. }
  4471. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4472. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4473. if len(ids) > 0 {
  4474. db = db.Where("x.id in(?)", ids)
  4475. }
  4476. if orgid > 0 {
  4477. db = db.Where("x.org_id = ?", orgid)
  4478. }
  4479. 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
  4480. return info, err
  4481. }
  4482. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4483. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4484. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4485. fmt.Println(table)
  4486. if len(ids) > 0 {
  4487. db = db.Where("x.warehouse_out_id in(?)", ids)
  4488. }
  4489. if orgid > 0 {
  4490. db = db.Where("x.org_id = ?", orgid)
  4491. }
  4492. 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
  4493. return info, err
  4494. }
  4495. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4496. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4497. likeKey := "%" + keyword + "%"
  4498. if len(keyword) > 0 {
  4499. db = db.Where("x.drug_name like ?", likeKey)
  4500. }
  4501. if orgid > 0 {
  4502. db = db.Where("x.org_id = ?", orgid)
  4503. }
  4504. err = db.Find(&basedrug).Error
  4505. return basedrug, err
  4506. }
  4507. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4508. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4509. if len(ids) > 0 {
  4510. db = db.Where("x.drug_id in(?)", ids)
  4511. }
  4512. if orgid > 0 {
  4513. db = db.Where("x.org_id = ?", orgid)
  4514. }
  4515. if startime > 0 {
  4516. db = db.Where("x.ctime>=?", startime)
  4517. }
  4518. if endtime > 0 {
  4519. db = db.Where("x.ctime<=?", endtime)
  4520. }
  4521. if storehouse_id > 0 {
  4522. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4523. }
  4524. err = db.Find(&info).Error
  4525. return info, err
  4526. }
  4527. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4528. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4529. if len(ids) > 0 {
  4530. db = db.Where("x.drug_id in(?)", ids)
  4531. }
  4532. if orgid > 0 {
  4533. db = db.Where("x.org_id = ?", orgid)
  4534. }
  4535. if startime > 0 {
  4536. db = db.Where("x.ctime>=?", startime)
  4537. }
  4538. if endtime > 0 {
  4539. db = db.Where("x.ctime<=?", endtime)
  4540. }
  4541. if storehouse_id > 0 {
  4542. db = db.Where("storehouse_id = ?", storehouse_id)
  4543. }
  4544. err = db.Find(&info).Error
  4545. return info, err
  4546. }
  4547. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4548. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4549. if drugid > 0 {
  4550. db = db.Where("x.drug_id = ?", drugid)
  4551. }
  4552. if orgid > 0 {
  4553. db = db.Where("x.org_id = ?", orgid)
  4554. }
  4555. if storehouse_id > 0 {
  4556. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4557. }
  4558. 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
  4559. return info, err
  4560. }
  4561. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4562. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4563. if drugid > 0 {
  4564. db = db.Where("x.drug_id = ?", drugid)
  4565. }
  4566. if orgid > 0 {
  4567. db = db.Where("x.org_id = ?", orgid)
  4568. }
  4569. if storehouse_id > 0 {
  4570. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4571. }
  4572. 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
  4573. if err != gorm.ErrRecordNotFound {
  4574. if err != nil {
  4575. tx.Rollback()
  4576. return info, err
  4577. }
  4578. }
  4579. return info, err
  4580. }
  4581. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4582. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4583. if drugid > 0 {
  4584. db = db.Where("x.drug_id = ?", drugid)
  4585. }
  4586. if orgid > 0 {
  4587. db = db.Where("x.org_id = ?", orgid)
  4588. }
  4589. if storehouse_id > 0 {
  4590. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4591. }
  4592. 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
  4593. return info, err
  4594. }
  4595. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4596. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4597. if id > 0 {
  4598. db = db.Where("x.id = ?", id)
  4599. }
  4600. if orgid > 0 {
  4601. db = db.Where("x.org_id = ?", orgid)
  4602. }
  4603. 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
  4604. return info, err
  4605. }
  4606. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4607. info := models.VmDrugWarehouseInfo{}
  4608. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4609. if id == 0 {
  4610. db = db.Where("id = ?", id)
  4611. }
  4612. if drugid > 0 {
  4613. db = db.Where("x.drug_id = ?", drugid)
  4614. }
  4615. if orgid > 0 {
  4616. db = db.Where("x.org_id = ?", orgid)
  4617. }
  4618. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4619. return info, err
  4620. }
  4621. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4622. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4623. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4624. fmt.Println(table)
  4625. if drugid > 0 {
  4626. db = db.Where("x.drug_id = ?", drugid)
  4627. }
  4628. if storehouse_id > 0 {
  4629. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4630. }
  4631. 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
  4632. return list, err
  4633. }
  4634. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4635. lib := models.BaseDrugLib{}
  4636. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4637. return lib, err
  4638. }
  4639. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4640. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4641. if goodid > 0 {
  4642. db = db.Where("x.good_id = ?", goodid)
  4643. }
  4644. if orgid > 0 {
  4645. db = db.Where("x.org_id = ?", orgid)
  4646. }
  4647. 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
  4648. return info, err
  4649. }
  4650. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4651. 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
  4652. return err
  4653. }
  4654. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4655. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4656. return info, err
  4657. }
  4658. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4659. //遍历循环入库数据
  4660. for i, item := range list {
  4661. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4662. if (item.WarehousingCount - item.StockCount) >= count {
  4663. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4664. info := models.WarehousingInfo{
  4665. GoodId: item.GoodId,
  4666. OrgId: item.OrgId,
  4667. WarehousingId: item.WarehousingId,
  4668. StockCount: item.StockCount + count,
  4669. }
  4670. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4671. fmt.Println("2323233232323232323232323232323", err2)
  4672. return err
  4673. }
  4674. if (item.WarehousingCount - item.StockCount) < count {
  4675. //如果该批次小于退库数量,则先出库该相差数量
  4676. var countOne = item.WarehousingCount - item.StockCount
  4677. infoOne := models.WarehousingInfo{
  4678. GoodId: item.GoodId,
  4679. OrgId: item.OrgId,
  4680. WarehousingId: item.WarehousingId,
  4681. StockCount: countOne + item.StockCount,
  4682. }
  4683. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4684. if err == nil {
  4685. //剩余的出库到第二个批次
  4686. var countTwo = count - countOne
  4687. infos := append(list, list[i+1])
  4688. var idArray []*models.WarehousingInfo
  4689. for _, it := range infos {
  4690. if it.WarehousingId != item.WarehousingId {
  4691. idArray = append(idArray, it)
  4692. }
  4693. }
  4694. //查询更新后的库存
  4695. UpdateCancelOut(idArray, countTwo)
  4696. return err
  4697. } else {
  4698. return err
  4699. }
  4700. }
  4701. }
  4702. return err
  4703. }
  4704. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4705. if len(idstr) > 0 {
  4706. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4707. } else {
  4708. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4709. }
  4710. return info, err
  4711. }
  4712. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4713. if len(idstr) > 0 {
  4714. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4715. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4716. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4717. }).Find(&info).Error
  4718. }
  4719. return info, err
  4720. }
  4721. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4722. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4723. if startime > 0 {
  4724. db = db.Where("x.ctime >=?", startime)
  4725. }
  4726. if endtime > 0 {
  4727. db = db.Where("x.ctime <= ?", endtime)
  4728. }
  4729. if orgid > 0 {
  4730. db = db.Where("x.org_id = ?", orgid)
  4731. }
  4732. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4733. return info, err
  4734. }
  4735. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4736. info := models.WarehouseOutInfo{}
  4737. var err error
  4738. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4739. if err == gorm.ErrRecordNotFound {
  4740. return nil, err
  4741. }
  4742. if err != nil {
  4743. return nil, err
  4744. }
  4745. return &info, nil
  4746. }
  4747. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4748. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4749. if goodid > 0 {
  4750. db = db.Where("x.good_id = ?", goodid)
  4751. }
  4752. if orgid > 0 {
  4753. db = db.Where("x.org_id = ?", orgid)
  4754. }
  4755. if id > 0 {
  4756. db = db.Where("x.warehouse_info_id = ?", id)
  4757. }
  4758. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4759. return info, err
  4760. }
  4761. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4762. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4763. if goodid > 0 {
  4764. db = db.Where("x.good_id = ?", goodid)
  4765. }
  4766. if orgid > 0 {
  4767. db = db.Where("x.user_org_id = ?", orgid)
  4768. }
  4769. if id > 0 {
  4770. db = db.Where("x.warehousing_id = ?", id)
  4771. }
  4772. if storehouse_id > 0 {
  4773. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4774. }
  4775. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4776. return info, err
  4777. }
  4778. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4779. stock := models.CancelStock{}
  4780. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4781. return stock, err
  4782. }
  4783. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4784. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4785. return info, err
  4786. }
  4787. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4788. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4789. return info, err
  4790. }
  4791. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4792. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4793. return stock, err
  4794. }
  4795. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4796. 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)")
  4797. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4798. fmt.Println(table)
  4799. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4800. fmt.Println(tableone)
  4801. if id > 0 {
  4802. db = db.Where("x.warehouse_out_id = ?", id)
  4803. }
  4804. if goodid > 0 {
  4805. db = db.Where("x.good_id = ?", goodid)
  4806. }
  4807. 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
  4808. return stock, err
  4809. }
  4810. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4811. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4812. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4813. fmt.Println(table)
  4814. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4815. fmt.Println(tableone)
  4816. if id > 0 {
  4817. db = db.Where("x.warehouse_out_id = ?", id)
  4818. }
  4819. if goodid > 0 {
  4820. db = db.Where("x.good_id = ?", goodid)
  4821. }
  4822. 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
  4823. return stock, err
  4824. }
  4825. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4826. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4827. return info, err
  4828. }
  4829. func ExportDrugList(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseInfo, err error) {
  4830. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1 AND x.org_id=?", org_id)
  4831. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4832. tableOne := XTReadDB().Table("xt_drug_warehouse as p").Where("p.status=1 and p.org_id=?", org_id)
  4833. fmt.Println("table", table)
  4834. fmt.Println("tableOne", tableOne)
  4835. if len(warehouse_out_id) > 0 {
  4836. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4837. }
  4838. 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
  4839. return out, err
  4840. }
  4841. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4842. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4843. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4844. fmt.Println("table", table)
  4845. if len(warehouse_out_id) > 0 {
  4846. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4847. }
  4848. 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
  4849. return out, err
  4850. }
  4851. func GetExportOutOrderDrugListOne(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseOutInfo, err error) {
  4852. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1 and x.user_org_id =?", org_id)
  4853. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4854. fmt.Println("table", table)
  4855. if len(warehouse_out_id) > 0 {
  4856. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4857. }
  4858. 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
  4859. return out, err
  4860. }
  4861. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4862. db := XTReadDB().Model(&info).Where("status = 1")
  4863. if len(warehouse_out_id) > 0 {
  4864. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4865. }
  4866. if orgid > 0 {
  4867. db = db.Where("org_id = ?", orgid)
  4868. }
  4869. err = db.Find(&info).Error
  4870. return info, err
  4871. }
  4872. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4873. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4874. return out, err
  4875. }
  4876. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4877. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4878. 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)
  4879. }).Find(&out).Error
  4880. return out, err
  4881. }
  4882. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4883. info := models.WarehouseOutInfo{}
  4884. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4885. return info, err
  4886. }
  4887. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4888. info := models.WarehousingInfo{}
  4889. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4890. if goodid > 0 {
  4891. db = db.Where("x.good_id = ?", goodid)
  4892. }
  4893. if storehouse_id > 0 {
  4894. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4895. }
  4896. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4897. return info, err
  4898. }
  4899. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4900. info := models.WarehousingInfo{}
  4901. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4902. return info, err
  4903. }
  4904. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4905. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4906. return err
  4907. }
  4908. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4909. if storehouse_id == 0 {
  4910. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4911. }
  4912. if storehouse_id > 0 {
  4913. 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
  4914. }
  4915. return info, err
  4916. }
  4917. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4918. if len(warehousingInfo) > 0 {
  4919. utx := writeDb.Begin()
  4920. if len(warehousingInfo) > 0 {
  4921. 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 "
  4922. insertParams := make([]string, 0)
  4923. insertData := make([]interface{}, 0)
  4924. for _, info := range warehousingInfo {
  4925. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4926. insertData = append(insertData, info.WarehousingId)
  4927. insertData = append(insertData, info.GoodId)
  4928. insertData = append(insertData, info.Number)
  4929. insertData = append(insertData, info.LicenseNumber)
  4930. insertData = append(insertData, info.Count)
  4931. insertData = append(insertData, info.UserOrgId)
  4932. insertData = append(insertData, info.PatientId)
  4933. insertData = append(insertData, info.SystemTime)
  4934. insertData = append(insertData, info.ConsumableType)
  4935. insertData = append(insertData, info.IsSys)
  4936. insertData = append(insertData, info.WarehousingOrder)
  4937. insertData = append(insertData, info.WarehouseOutId)
  4938. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4939. insertData = append(insertData, info.IsEdit)
  4940. insertData = append(insertData, info.CancelStockId)
  4941. insertData = append(insertData, info.CancelOrderNumber)
  4942. insertData = append(insertData, info.Manufacturer)
  4943. insertData = append(insertData, info.Dealer)
  4944. insertData = append(insertData, info.Creator)
  4945. insertData = append(insertData, info.UpdateCreator)
  4946. insertData = append(insertData, info.Status)
  4947. insertData = append(insertData, info.Ctime)
  4948. insertData = append(insertData, info.Mtime)
  4949. insertData = append(insertData, info.Price)
  4950. insertData = append(insertData, info.WarehousingDetailId)
  4951. insertData = append(insertData, info.WarehouseOutDetailId)
  4952. insertData = append(insertData, info.CancelOutDetailId)
  4953. insertData = append(insertData, info.ExpireDate)
  4954. insertData = append(insertData, info.ProductDate)
  4955. }
  4956. thisSQL += strings.Join(insertParams, ", ")
  4957. err = utx.Exec(thisSQL, insertData...).Error
  4958. if err != nil {
  4959. utx.Rollback()
  4960. return
  4961. }
  4962. }
  4963. utx.Commit()
  4964. }
  4965. return
  4966. }
  4967. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4968. utx := XTWriteDB().Begin()
  4969. err := utx.Create(&flow).Error
  4970. if err != nil {
  4971. utx.Rollback()
  4972. return err
  4973. }
  4974. utx.Commit()
  4975. return err
  4976. }
  4977. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4978. err := tx.Create(&flow).Error
  4979. if err != nil {
  4980. tx.Rollback()
  4981. return err
  4982. }
  4983. return err
  4984. }
  4985. func CreateOutInfoLog(log models.XtGoodWarehouseOutInfoLog, tx *gorm.DB) error {
  4986. err := tx.Create(&log).Error
  4987. if err != nil {
  4988. tx.Rollback()
  4989. return err
  4990. }
  4991. return err
  4992. }
  4993. func CreateGoodWarehouseInfoLog(log models.XtGoodWarehouseInfoLog, tx *gorm.DB) error {
  4994. err := tx.Create(&log).Error
  4995. if err != nil {
  4996. tx.Rollback()
  4997. return err
  4998. }
  4999. return err
  5000. }
  5001. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  5002. err := XTWriteDB().Create(&flow).Error
  5003. return err
  5004. }
  5005. func UpdatedStockFlow(flow models.VmStockFlow) error {
  5006. err := XTWriteDB().Save(&flow).Error
  5007. return err
  5008. }
  5009. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  5010. 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
  5011. return err
  5012. }
  5013. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  5014. 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
  5015. return err
  5016. }
  5017. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  5018. stock := models.VmStockFlow{}
  5019. var err error
  5020. 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
  5021. if err == gorm.ErrRecordNotFound {
  5022. return nil, err
  5023. }
  5024. if err != nil {
  5025. return nil, err
  5026. }
  5027. return &stock, nil
  5028. }
  5029. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5030. 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
  5031. return flow, nil
  5032. }
  5033. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5034. 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
  5035. return flow, nil
  5036. }
  5037. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5038. 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
  5039. if err != gorm.ErrRecordNotFound {
  5040. if err != nil {
  5041. tx.Rollback()
  5042. return flow, nil
  5043. }
  5044. }
  5045. return flow, nil
  5046. }
  5047. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5048. 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
  5049. return flow, nil
  5050. }
  5051. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  5052. 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
  5053. if err != gorm.ErrRecordNotFound {
  5054. if err != nil {
  5055. tx.Rollback()
  5056. return flow, nil
  5057. }
  5058. }
  5059. return flow, nil
  5060. }
  5061. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  5062. 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
  5063. return flow, nil
  5064. }
  5065. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  5066. 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
  5067. if err != gorm.ErrRecordNotFound {
  5068. if err != nil {
  5069. tx.Rollback()
  5070. return flow, err
  5071. }
  5072. }
  5073. return flow, err
  5074. }
  5075. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5076. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  5077. return flow, nil
  5078. }
  5079. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5080. 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
  5081. return flow, nil
  5082. }
  5083. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5084. 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
  5085. return flow, nil
  5086. }
  5087. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5088. 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
  5089. if err != gorm.ErrRecordNotFound {
  5090. if err != nil {
  5091. tx.Rollback()
  5092. return flow, nil
  5093. }
  5094. }
  5095. return flow, nil
  5096. }
  5097. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5098. 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
  5099. return flow, nil
  5100. }
  5101. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  5102. info := models.WarehouseOutInfo{}
  5103. var err error
  5104. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5105. if err == gorm.ErrRecordNotFound {
  5106. return nil, err
  5107. }
  5108. if err != nil {
  5109. return nil, err
  5110. }
  5111. return &info, nil
  5112. }
  5113. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  5114. info := models.WarehouseOutInfo{}
  5115. var err error
  5116. 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
  5117. if err == gorm.ErrRecordNotFound {
  5118. return nil, err
  5119. }
  5120. if err != nil {
  5121. return nil, err
  5122. }
  5123. return &info, nil
  5124. }
  5125. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  5126. info := models.WarehouseOutInfo{}
  5127. 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
  5128. return info, err
  5129. }
  5130. 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) {
  5131. offset := (page - 1) * limit
  5132. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  5133. if consumable_type > 0 {
  5134. //入库
  5135. if consumable_type == 1 {
  5136. db = db.Where("consumable_type = ?", consumable_type)
  5137. }
  5138. //2 手动出库
  5139. if consumable_type == 2 {
  5140. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  5141. }
  5142. //3.自动出库
  5143. if consumable_type == 3 {
  5144. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  5145. }
  5146. //4.手动退库
  5147. if consumable_type == 4 {
  5148. db = db.Where(" consumable_type = ?", consumable_type)
  5149. }
  5150. //自动退库
  5151. if consumable_type == 7 {
  5152. db = db.Where(" consumable_type = ?", consumable_type)
  5153. }
  5154. if consumable_type == 10 {
  5155. db = db.Where(" consumable_type = ?", consumable_type)
  5156. }
  5157. if consumable_type == 11 {
  5158. db = db.Where(" consumable_type = ?", consumable_type)
  5159. }
  5160. if consumable_type == 12 {
  5161. db = db.Where(" consumable_type = ?", consumable_type)
  5162. }
  5163. if consumable_type == 13 {
  5164. db = db.Where(" consumable_type = ?", consumable_type)
  5165. }
  5166. if consumable_type == 15 {
  5167. db = db.Where(" consumable_type = ?", consumable_type)
  5168. }
  5169. }
  5170. if orgId > 0 {
  5171. db = db.Where("user_org_id = ?", orgId)
  5172. }
  5173. if startTime > 0 {
  5174. db = db.Where("ctime >= ?", startTime)
  5175. }
  5176. if endTime > 0 {
  5177. db = db.Where("ctime<=?", endTime)
  5178. }
  5179. if good_id > 0 {
  5180. db = db.Where("good_id = ?", good_id)
  5181. }
  5182. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  5183. return flow, total, err
  5184. }
  5185. func GetHidsDoctorAdvice(good_id int64, user_org_id int64, patient_id int64, record_date int64) (models.HisPrescriptionProject, error) {
  5186. project := models.HisPrescriptionProject{}
  5187. err = XTReadDB().Where("project_id = ? and type =3 and user_org_id = ? and record_date =? and patient_id = ?", good_id, user_org_id, record_date, patient_id).Find(&project).Error
  5188. return project, err
  5189. }
  5190. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  5191. detail := models.AutomaticReduceDetail{}
  5192. 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
  5193. return detail, err
  5194. }
  5195. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  5196. detail := models.WarehouseOutInfo{}
  5197. 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
  5198. return detail, err
  5199. }
  5200. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  5201. outInfo := models.WarehouseOutInfo{}
  5202. 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
  5203. return err
  5204. }
  5205. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  5206. outInfo := models.WarehouseOutInfo{}
  5207. 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
  5208. if err != nil {
  5209. tx.Rollback()
  5210. return err
  5211. }
  5212. return err
  5213. }
  5214. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  5215. outInfo := models.WarehouseOutInfo{}
  5216. 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
  5217. return err
  5218. }
  5219. 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 {
  5220. outInfo := models.WarehouseOutInfo{}
  5221. 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
  5222. if err != nil {
  5223. tx.Rollback()
  5224. return err
  5225. }
  5226. return err
  5227. }
  5228. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  5229. info := models.WarehouseOutInfo{}
  5230. 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
  5231. return info, err
  5232. }
  5233. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  5234. info := models.WarehousingInfo{}
  5235. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5236. return info, err
  5237. }
  5238. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  5239. info := models.WarehousingInfo{}
  5240. db := XTReadDB().Model(&info)
  5241. if good_id > 0 {
  5242. db = db.Where("good_id = ? and status = 1", good_id)
  5243. }
  5244. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  5245. return info, err
  5246. }
  5247. 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) {
  5248. offset := (page - 1) * limit
  5249. db := XTReadDB().Model(drug_flow).Where("status = 1")
  5250. if drugid > 0 {
  5251. db = db.Where("drug_id = ?", drugid)
  5252. }
  5253. if orgid > 0 {
  5254. db = db.Where("user_org_id = ?", orgid)
  5255. }
  5256. if startTime > 0 {
  5257. db = db.Where("ctime >=? ", startTime)
  5258. }
  5259. if endTime > 0 {
  5260. db = db.Where("ctime<=?", endTime)
  5261. }
  5262. if stock_type > 0 {
  5263. //手动入库
  5264. if stock_type == 1 {
  5265. db = db.Where("consumable_type = 1")
  5266. }
  5267. //手动出库
  5268. if stock_type == 2 {
  5269. db = db.Where("consumable_type = 2 and is_sys = 0")
  5270. }
  5271. //自动出库
  5272. if stock_type == 3 {
  5273. db = db.Where("consumable_type = 3 and is_sys = 1")
  5274. }
  5275. if stock_type == 4 {
  5276. db = db.Where("consumable_type = 4")
  5277. }
  5278. if stock_type == 10 {
  5279. db = db.Where("consumable_type = 10")
  5280. }
  5281. if stock_type == 11 {
  5282. db = db.Where("consumable_type = 11")
  5283. }
  5284. if stock_type == 12 {
  5285. db = db.Where("consumable_type = 12")
  5286. }
  5287. if stock_type == 13 {
  5288. db = db.Where("consumable_type = 13")
  5289. }
  5290. if stock_type == 15 {
  5291. db = db.Where("consumable_type = 15")
  5292. }
  5293. }
  5294. 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
  5295. return drug_flow, total, err
  5296. }
  5297. func HisDoctorAdviceMap(patient_id int64, advice_date int64, drug_id int64, user_org_id int64) (models.HisDoctorAdviceInfo, error) {
  5298. advicelist := models.HisDoctorAdviceInfo{}
  5299. err = XTReadDB().Where("patient_id = ? and drug_id = ? and user_org_id = ? and advice_date = ? and status=1", patient_id, drug_id, user_org_id, advice_date).Find(&advicelist).Error
  5300. return advicelist, err
  5301. }
  5302. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  5303. outInfo := models.DrugWarehouseOutInfo{}
  5304. var err error
  5305. 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
  5306. if err == gorm.ErrRecordNotFound {
  5307. return nil, err
  5308. }
  5309. if err != nil {
  5310. return nil, err
  5311. }
  5312. return &outInfo, nil
  5313. }
  5314. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  5315. 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
  5316. return outInfo, err
  5317. }
  5318. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  5319. outInfo := models.DrugWarehouseOutInfo{}
  5320. 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
  5321. return err
  5322. }
  5323. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  5324. db := XTReadDB().Model(&cancle).Where("status = 1")
  5325. if startime > 0 {
  5326. db = db.Where("ctime >=?", startime)
  5327. }
  5328. if endtime > 0 {
  5329. db = db.Where("ctime <=?", endtime)
  5330. }
  5331. if len(ids) > 0 {
  5332. db = db.Where("cancel_stock_id in(?)", ids)
  5333. }
  5334. if orgid > 0 {
  5335. db = db.Where("org_id = ?", orgid)
  5336. }
  5337. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  5338. err = db.Find(&cancle).Error
  5339. return cancle, err
  5340. }
  5341. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  5342. db := XTReadDB().Model(&info)
  5343. if id > 0 {
  5344. db = db.Where("cancel_stock_id = ? and status = 1", id)
  5345. }
  5346. db = db.Preload("BaseDrugLib")
  5347. err = db.Find(&info).Error
  5348. return info, err
  5349. }
  5350. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  5351. info := models.XtDrugWarehouseInfo{}
  5352. db := XTReadDB().Model(&info).Where("status = 1")
  5353. if id > 0 {
  5354. db = db.Where("id = ?", id)
  5355. }
  5356. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5357. return info, err
  5358. }
  5359. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  5360. info := models.XtDrugWarehouseInfo{}
  5361. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  5362. return info, err
  5363. }
  5364. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  5365. // info := models.XtDrugWarehouseInfo{}
  5366. // db := tx.Model(&info).Where("status = 1")
  5367. // if id > 0 {
  5368. // db = db.Where("id = ?", id)
  5369. // }
  5370. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5371. // return info, err
  5372. //}
  5373. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5374. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  5375. return info, err
  5376. }
  5377. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  5378. 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
  5379. return info, err
  5380. }
  5381. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  5382. info := models.HisDoctorAdviceInfo{}
  5383. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  5384. return info, err
  5385. }
  5386. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  5387. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5388. return info, err
  5389. }
  5390. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  5391. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5392. if err != gorm.ErrRecordNotFound {
  5393. if err != nil {
  5394. tx.Rollback()
  5395. return info, err
  5396. }
  5397. }
  5398. return info, err
  5399. }
  5400. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  5401. adviceInfo := models.HisDoctorAdviceInfo{}
  5402. 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
  5403. if err != gorm.ErrRecordNotFound {
  5404. if err != nil {
  5405. tx.Rollback()
  5406. return adviceInfo, err
  5407. }
  5408. }
  5409. return adviceInfo, err
  5410. }
  5411. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5412. info := models.HisDoctorAdviceInfo{}
  5413. 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
  5414. return info, err
  5415. }
  5416. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5417. info := models.HisDoctorAdviceInfo{}
  5418. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).Find(&info).Error
  5419. return info, err
  5420. }
  5421. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  5422. detail := models.DrugAutomaticReduceDetail{}
  5423. 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
  5424. return detail, err
  5425. }
  5426. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  5427. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and advice_id =? and status =1", drug_id, patient_id, record_date, advice_id).Find(&out).Error
  5428. return out, err
  5429. }
  5430. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  5431. 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
  5432. return out, err
  5433. }
  5434. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  5435. 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
  5436. if err != gorm.ErrRecordNotFound {
  5437. if err != nil {
  5438. tx.Rollback()
  5439. return out, err
  5440. }
  5441. }
  5442. return out, err
  5443. }
  5444. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  5445. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5446. return err
  5447. }
  5448. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  5449. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5450. return err
  5451. }
  5452. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  5453. 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
  5454. 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
  5455. 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
  5456. return err
  5457. }
  5458. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5459. 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
  5460. 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
  5461. return err
  5462. }
  5463. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5464. 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
  5465. 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
  5466. return err
  5467. }
  5468. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5469. info := models.XtDrugWarehouseInfo{}
  5470. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5471. return info, err
  5472. }
  5473. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5474. info := models.XtDrugWarehouseInfo{}
  5475. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5476. return info, err
  5477. }
  5478. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5479. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5480. return err
  5481. }
  5482. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5483. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5484. // return err
  5485. //}
  5486. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5487. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5488. //return err
  5489. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5490. return err
  5491. }
  5492. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5493. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5494. // return err
  5495. //}
  5496. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5497. info := models.DrugCancelStockInfo{}
  5498. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5499. return info, err
  5500. }
  5501. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5502. info := models.DrugCancelStockInfo{}
  5503. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5504. return info, err
  5505. }
  5506. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5507. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5508. return err
  5509. }
  5510. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5511. 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
  5512. return err
  5513. }
  5514. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5515. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5516. return err
  5517. }
  5518. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5519. 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
  5520. return err
  5521. }
  5522. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5523. 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
  5524. return err
  5525. }
  5526. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5527. 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
  5528. return err
  5529. }
  5530. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5531. 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
  5532. return err
  5533. }
  5534. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5535. info := models.WarehousingInfo{}
  5536. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5537. return info, err
  5538. }
  5539. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5540. info := models.WarehousingInfo{}
  5541. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5542. return info, err
  5543. }
  5544. func GetLastWarehouseInfoTwo(goodid int64, storehouse_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  5545. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and org_id = ? and stock_count > 0", goodid, storehouse_id, user_org_id).Order("ctime").First(&info).Error
  5546. return info, err
  5547. }
  5548. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5549. info := models.WarehousingInfo{}
  5550. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5551. return info, err
  5552. }
  5553. func CreateWarehouseOut(out models.WarehouseOut) error {
  5554. err := XTWriteDB().Create(&out).Error
  5555. return err
  5556. }
  5557. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5558. out := models.WarehouseOut{}
  5559. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5560. return out, err
  5561. }
  5562. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5563. err := XTWriteDB().Create(&info).Error
  5564. return err
  5565. }
  5566. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5567. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5568. return err
  5569. }
  5570. func UpdateWareInfoById(id int64, count int64) error {
  5571. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5572. return err
  5573. }
  5574. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5575. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5576. return err
  5577. }
  5578. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5579. info := models.CancelStockInfo{}
  5580. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5581. return info, err
  5582. }
  5583. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5584. info := models.CancelStockInfo{}
  5585. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5586. return info, err
  5587. }
  5588. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5589. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5590. return info, err
  5591. }
  5592. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5593. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5594. return info, err
  5595. }
  5596. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5597. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5598. return err
  5599. }
  5600. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5601. drugFlow := models.DrugFlow{}
  5602. 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
  5603. return err
  5604. }
  5605. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5606. drugFlow := models.DrugFlow{}
  5607. 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
  5608. return err
  5609. }
  5610. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5611. stock := models.DrugCancelStock{}
  5612. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5613. return stock, err
  5614. }
  5615. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5616. // stock := models.DrugCancelStock{}
  5617. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5618. // return stock, err
  5619. //}
  5620. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5621. db := XTReadDB().Model(&info)
  5622. if warehouse_info_id > 0 {
  5623. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5624. }
  5625. if drug_id > 0 {
  5626. db = db.Where("drug_id = ? and status =1", drug_id)
  5627. }
  5628. err = db.Find(&info).Error
  5629. return info, err
  5630. }
  5631. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5632. // db := tx.Model(&info)
  5633. // if warehouse_info_id > 0 {
  5634. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5635. // }
  5636. // if drug_id > 0 {
  5637. // db = db.Where("drug_id = ? and status =1", drug_id)
  5638. // }
  5639. // err = db.Find(&info).Error
  5640. // return info, err
  5641. //}
  5642. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5643. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5644. return info, err
  5645. }
  5646. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5647. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5648. // return info, err
  5649. //}
  5650. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5651. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5652. return config, err
  5653. }
  5654. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5655. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5656. return config, err
  5657. }
  5658. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5659. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5660. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5661. fmt.Println(table)
  5662. likeKey := "%" + keyword + "%"
  5663. if orgid > 0 {
  5664. db = db.Where("x.org_id = ?", orgid)
  5665. }
  5666. if storehouse_id > 0 {
  5667. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5668. }
  5669. 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
  5670. return info, err
  5671. }
  5672. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5673. err := XTWriteDB().Create(&adjustPrice).Error
  5674. return err
  5675. }
  5676. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5677. likeKey := "%" + keyword + "%"
  5678. offset := (page - 1) * limit
  5679. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5680. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5681. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5682. fmt.Println(table, tab)
  5683. if startime > 0 {
  5684. db = db.Where("x.start_time>=?", startime)
  5685. }
  5686. if endtime > 0 {
  5687. db = db.Where("x.start_time<=?", endtime)
  5688. }
  5689. if len(keyword) > 0 {
  5690. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5691. }
  5692. if orgid > 0 {
  5693. db = db.Where("x.user_org_id = ?", orgid)
  5694. }
  5695. 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
  5696. return list, total, err
  5697. }
  5698. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5699. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5700. return err
  5701. }
  5702. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5703. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5704. return err
  5705. }
  5706. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5707. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5708. return info, err
  5709. }
  5710. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5711. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5712. return adjust, err
  5713. }
  5714. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5715. err := XTWriteDB().Create(&price).Error
  5716. return err
  5717. }
  5718. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5719. likeKey := "%" + keyword + "%"
  5720. offset := (page - 1) * limit
  5721. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5722. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5723. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5724. fmt.Println(table, tab)
  5725. if startime > 0 {
  5726. db = db.Where("x.ctime>=?", startime)
  5727. }
  5728. if endtime > 0 {
  5729. db = db.Where("x.ctime<=?", endtime)
  5730. }
  5731. if len(keyword) > 0 {
  5732. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5733. }
  5734. if orgid > 0 {
  5735. db = db.Where("x.user_org_id = ?", orgid)
  5736. }
  5737. 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
  5738. return adjust, total, err
  5739. }
  5740. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5741. likeKey := "%" + keyword + "%"
  5742. offset := (page - 1) * limit
  5743. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5744. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5745. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5746. fmt.Println(table, tab)
  5747. if len(keyword) > 0 {
  5748. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5749. }
  5750. if orgid > 0 {
  5751. db = db.Where("x.user_org_id = ?", orgid)
  5752. }
  5753. if startime > 0 {
  5754. db = db.Where("x.ctime >=?", startime)
  5755. }
  5756. if endtime > 0 {
  5757. db = db.Where("x.ctime <=?", endtime)
  5758. }
  5759. if storehouse_id > 0 {
  5760. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5761. }
  5762. 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
  5763. return adjust, total, err
  5764. }
  5765. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5766. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5767. if inventory_type == 0 {
  5768. db = db.Where("x.inventory_type = 4")
  5769. }
  5770. if inventory_type > 0 {
  5771. db = db.Where("x.inventory_type = ?", inventory_type)
  5772. }
  5773. if len(keyword) > 0 {
  5774. likeKey := "%" + keyword + "%"
  5775. db = db.Where("x.warehousing_order like ?", likeKey)
  5776. }
  5777. if orgid > 0 {
  5778. db = db.Where("x.user_org_id = ?", orgid)
  5779. }
  5780. 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
  5781. return adjust, err
  5782. }
  5783. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5784. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5785. if orgid > 0 {
  5786. db = db.Where("x.user_org_id = ?", orgid)
  5787. }
  5788. 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
  5789. return adjust, err
  5790. }
  5791. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5792. 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
  5793. return err
  5794. }
  5795. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5796. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5797. return report, err
  5798. }
  5799. func CreateInentory(inventory models.XtStockInventory) error {
  5800. err = XTWriteDB().Create(&inventory).Error
  5801. return err
  5802. }
  5803. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5804. likeKey := "%" + keyword + "%"
  5805. offset := (page - 1) * limit
  5806. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5807. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5808. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5809. fmt.Println(table, tab)
  5810. if startime > 0 {
  5811. db = db.Where("x.ctime>=?", startime)
  5812. }
  5813. if endtime > 0 {
  5814. db = db.Where("x.ctime<=?", endtime)
  5815. }
  5816. if len(keyword) > 0 {
  5817. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5818. }
  5819. if orgid > 0 {
  5820. db = db.Where("x.user_org_id = ?", orgid)
  5821. }
  5822. if inventory_status > 0 {
  5823. db = db.Where("checker_status = ?", inventory_status)
  5824. }
  5825. 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
  5826. return adjust, total, err
  5827. }
  5828. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5829. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5830. return err
  5831. }
  5832. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5833. info := models.XtStockAdjustPrice{}
  5834. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5835. return info, err
  5836. }
  5837. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5838. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5839. return list, err
  5840. }
  5841. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5842. info := models.WarehousingInfo{}
  5843. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5844. return info, err
  5845. }
  5846. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5847. info := models.WarehousingInfo{}
  5848. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5849. //return info, err
  5850. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5851. if id > 0 {
  5852. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5853. }
  5854. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5855. return info, err
  5856. }
  5857. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5858. info := models.WarehousingInfo{}
  5859. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5860. if id > 0 {
  5861. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5862. }
  5863. if storehouse_id > 0 {
  5864. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5865. }
  5866. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5867. return info, err
  5868. }
  5869. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5870. 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
  5871. return err
  5872. }
  5873. func DeleteStockPrice(id int64) error {
  5874. info := models.XtStockAdjustPrice{}
  5875. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5876. return err
  5877. }
  5878. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5879. report := models.XtStockReportPrice{}
  5880. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5881. return report, err
  5882. }
  5883. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5884. 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
  5885. return err
  5886. }
  5887. func DeleteDamage(id int64) error {
  5888. adjust := models.XtStockReportPrice{}
  5889. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5890. return err
  5891. }
  5892. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5893. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5894. return adjust, err
  5895. }
  5896. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5897. inventory := models.XtStockInventory{}
  5898. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5899. return inventory, err
  5900. }
  5901. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5902. 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
  5903. return err
  5904. }
  5905. func DeleteInventory(id int64) error {
  5906. inventory := models.XtStockInventory{}
  5907. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5908. return err
  5909. }
  5910. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5911. likeKey := "%" + keyword + "%"
  5912. offset := (page - 1) * limit
  5913. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5914. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5915. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5916. fmt.Println(table, tab)
  5917. if len(keyword) > 0 {
  5918. db = db.Where("x.good_name like ?", likeKey)
  5919. }
  5920. if orgid > 0 {
  5921. db = db.Where("x.user_org_id = ?", orgid)
  5922. }
  5923. if storehouse_id > 0 {
  5924. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5925. }
  5926. 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
  5927. return list, total, err
  5928. }
  5929. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5930. likeKey := "%" + keyword + "%"
  5931. offset := (page - 1) * limit
  5932. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5933. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5934. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5935. fmt.Println(table, tab)
  5936. if len(keyword) > 0 {
  5937. db = db.Where("x.good_name like ?", likeKey)
  5938. }
  5939. if orgid > 0 {
  5940. db = db.Where("x.user_org_id = ?", orgid)
  5941. }
  5942. if storehouse_id > 0 {
  5943. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5944. }
  5945. if len(ids) > 0 {
  5946. db = db.Where("x.id in(?)", ids)
  5947. }
  5948. 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
  5949. return list, total, err
  5950. }
  5951. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5952. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5953. return list, err
  5954. }
  5955. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5956. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5957. return list, err
  5958. }
  5959. func DeleteDrugPrice(id int64) error {
  5960. info := models.XtDrugAdjustPrice{}
  5961. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5962. return err
  5963. }
  5964. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5965. info := models.WarehousingInfo{}
  5966. db := XTReadDB().Model(&info).Where("status = 1")
  5967. if goodid > 0 {
  5968. db = db.Where("good_id = ?", goodid)
  5969. }
  5970. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5971. return info, err
  5972. }
  5973. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5974. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5975. return err
  5976. }
  5977. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5978. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5979. return err
  5980. }
  5981. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5982. err := XTWriteDB().Create(&record).Error
  5983. return err
  5984. }
  5985. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5986. info := models.WarehousingInfo{}
  5987. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5988. //return info, err
  5989. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5990. if id > 0 {
  5991. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5992. }
  5993. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5994. return info, err
  5995. }
  5996. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5997. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5998. return info, err
  5999. }
  6000. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  6001. info := models.DrugWarehouseInfo{}
  6002. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  6003. return info, err
  6004. }
  6005. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  6006. 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
  6007. return err
  6008. }
  6009. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  6010. 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
  6011. return err
  6012. }
  6013. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  6014. 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
  6015. return info, err
  6016. }
  6017. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  6018. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  6019. return err
  6020. }
  6021. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  6022. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  6023. return err
  6024. }
  6025. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  6026. db := XTReadDB().Model(&list).Where(" status = 1")
  6027. if good_id > 0 {
  6028. db = db.Where("good_id = ?", good_id)
  6029. }
  6030. if len(keyword) > 0 {
  6031. likeKey := "%" + keyword + "%"
  6032. db = db.Where("warehousing_order like ?", likeKey)
  6033. }
  6034. if inventory_type > 0 {
  6035. db = db.Where("inventory_type = ?", inventory_type)
  6036. }
  6037. if inventory_type == 0 {
  6038. db = db.Where("inventory_type = 4")
  6039. }
  6040. err = db.Find(&list).Error
  6041. return list, err
  6042. }
  6043. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  6044. flow := models.DrugFlow{}
  6045. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  6046. if err == gorm.ErrRecordNotFound {
  6047. return nil, err
  6048. }
  6049. if err != nil {
  6050. return nil, err
  6051. }
  6052. return &flow, nil
  6053. }
  6054. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  6055. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  6056. return info, err
  6057. }
  6058. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  6059. info := models.XtDrugWarehouseInfo{}
  6060. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  6061. return info, err
  6062. }
  6063. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  6064. info := models.WarehousingInfo{}
  6065. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  6066. return info, err
  6067. }
  6068. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  6069. flow := models.VmStockFlow{}
  6070. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  6071. if err == gorm.ErrRecordNotFound {
  6072. return nil, err
  6073. }
  6074. if err != nil {
  6075. return nil, err
  6076. }
  6077. return &flow, nil
  6078. }
  6079. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  6080. info := models.GoodInfo{}
  6081. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  6082. return info, err
  6083. }
  6084. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  6085. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  6086. return err
  6087. }
  6088. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  6089. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  6090. return err
  6091. }
  6092. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  6093. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  6094. return err
  6095. }
  6096. func UpdateNewWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  6097. err := tx.Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  6098. if err != nil {
  6099. tx.Rollback()
  6100. return err
  6101. }
  6102. return err
  6103. }
  6104. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6105. 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
  6106. return err
  6107. }
  6108. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6109. 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
  6110. return err
  6111. }
  6112. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  6113. 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
  6114. return err
  6115. }
  6116. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  6117. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  6118. return err
  6119. }
  6120. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  6121. if orgid == 10653 {
  6122. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0 and x.stock_count>0")
  6123. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6124. fmt.Println(table)
  6125. if orgid > 0 {
  6126. db = db.Where("x.org_id = ?", orgid)
  6127. }
  6128. 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,x.stock_count,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
  6129. return info, err
  6130. } else {
  6131. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0")
  6132. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6133. fmt.Println(table)
  6134. if orgid > 0 {
  6135. db = db.Where("x.org_id = ?", orgid)
  6136. }
  6137. 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,x.stock_count,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
  6138. return info, err
  6139. }
  6140. }
  6141. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  6142. warehouseInfo := models.XtDrugWarehouseInfo{}
  6143. 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
  6144. return err
  6145. }
  6146. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  6147. err := XTWriteDB().Create(&info).Error
  6148. return err
  6149. }
  6150. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  6151. err := tx.Create(&info).Error
  6152. if err != nil {
  6153. tx.Rollback()
  6154. return err
  6155. }
  6156. return err
  6157. }
  6158. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  6159. flow := models.VmStockFlow{}
  6160. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  6161. if err == gorm.ErrRecordNotFound {
  6162. return nil, err
  6163. }
  6164. if err != nil {
  6165. return nil, err
  6166. }
  6167. return &flow, nil
  6168. }
  6169. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  6170. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  6171. return err
  6172. }
  6173. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  6174. 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
  6175. return err
  6176. }
  6177. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  6178. flow := models.DrugWarehouseOutInfo{}
  6179. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6180. return flow, err
  6181. }
  6182. func GetLastNewWarehouseOutInfoById(drugid int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  6183. flow := models.DrugWarehouseOutInfo{}
  6184. err := tx.Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6185. if err != gorm.ErrRecordNotFound {
  6186. if err != nil {
  6187. tx.Rollback()
  6188. return flow, err
  6189. }
  6190. }
  6191. return flow, err
  6192. }
  6193. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  6194. info := models.WarehouseOutInfo{}
  6195. 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
  6196. return info, err
  6197. }
  6198. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  6199. 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
  6200. return err
  6201. }
  6202. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  6203. 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
  6204. return err
  6205. }
  6206. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  6207. info := models.DrugWarehouseOutInfo{}
  6208. 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
  6209. if err == gorm.ErrRecordNotFound {
  6210. return nil, err
  6211. }
  6212. if err != nil {
  6213. return nil, err
  6214. }
  6215. return &info, nil
  6216. }
  6217. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  6218. drugFlow := models.DrugFlow{}
  6219. 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
  6220. if err == gorm.ErrRecordNotFound {
  6221. return nil, err
  6222. }
  6223. if err != nil {
  6224. return nil, err
  6225. }
  6226. return &drugFlow, nil
  6227. }
  6228. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  6229. 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
  6230. return err
  6231. }
  6232. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  6233. 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
  6234. return err
  6235. }
  6236. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  6237. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6238. return detail, err
  6239. }
  6240. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  6241. detail := models.DrugAutomaticReduceDetail{}
  6242. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6243. if err == gorm.ErrRecordNotFound {
  6244. return nil, err
  6245. }
  6246. if err != nil {
  6247. return nil, err
  6248. }
  6249. return &detail, nil
  6250. }
  6251. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  6252. 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
  6253. return err
  6254. }
  6255. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  6256. 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
  6257. return err
  6258. }
  6259. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  6260. info := models.DrugWarehouseInfo{}
  6261. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6262. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6263. return info, err
  6264. }
  6265. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  6266. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6267. return err
  6268. }
  6269. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  6270. info := models.DrugWarehouseInfo{}
  6271. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6272. return err
  6273. }
  6274. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  6275. info := models.DrugCancelStockInfo{}
  6276. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6277. return info, err
  6278. }
  6279. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  6280. info := models.DrugWarehouseInfo{}
  6281. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  6282. return err
  6283. }
  6284. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  6285. info := models.DrugWarehouseInfo{}
  6286. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  6287. return err
  6288. }
  6289. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  6290. 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
  6291. return advice, err
  6292. }
  6293. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  6294. 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
  6295. if err != gorm.ErrRecordNotFound {
  6296. if err != nil {
  6297. tx.Rollback()
  6298. return advice, err
  6299. }
  6300. }
  6301. return advice, err
  6302. }
  6303. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6304. 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
  6305. return info, err
  6306. }
  6307. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  6308. 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
  6309. if err != gorm.ErrRecordNotFound {
  6310. if err != nil {
  6311. tx.Rollback()
  6312. return info, err
  6313. }
  6314. }
  6315. return info, err
  6316. }
  6317. // 耗材加库存
  6318. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6319. info := models.GoodInfo{}
  6320. 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
  6321. return info, err
  6322. }
  6323. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6324. info := models.GoodInfo{}
  6325. 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
  6326. 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
  6327. return info, err
  6328. }
  6329. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6330. info := models.GoodInfo{}
  6331. 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
  6332. return info, err
  6333. }
  6334. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6335. info := models.GoodInfo{}
  6336. 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
  6337. 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
  6338. return info, err
  6339. }
  6340. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  6341. info := models.GoodInfo{}
  6342. 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
  6343. return info, err
  6344. }
  6345. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6346. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6347. return info, err
  6348. }
  6349. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6350. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6351. return info, err
  6352. }
  6353. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  6354. info := models.WarehousingInfo{}
  6355. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  6356. return info, err
  6357. }
  6358. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  6359. info := models.WarehousingInfo{}
  6360. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  6361. return info, err
  6362. }
  6363. // 药品加库存
  6364. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6365. info := models.BaseDrugLib{}
  6366. 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
  6367. return info, err
  6368. }
  6369. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6370. info := models.BaseDrugLib{}
  6371. 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
  6372. 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
  6373. return info, err
  6374. }
  6375. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6376. info := models.BaseDrugLib{}
  6377. 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
  6378. return info, err
  6379. }
  6380. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6381. info := models.BaseDrugLib{}
  6382. 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
  6383. 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
  6384. return info, err
  6385. }
  6386. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  6387. info := models.BaseDrugLib{}
  6388. 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
  6389. 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
  6390. return info, err
  6391. }
  6392. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6393. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6394. return info, err
  6395. }
  6396. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6397. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6398. return info, err
  6399. }
  6400. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  6401. info := models.WarehouseOutInfo{}
  6402. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6403. return info, err
  6404. }
  6405. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  6406. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  6407. return out, err
  6408. }
  6409. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  6410. info := models.WarehousingInfo{}
  6411. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6412. return info, err
  6413. }
  6414. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  6415. err = XTReadDB().Where("status = 1").Find(&template).Error
  6416. return template, err
  6417. }
  6418. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  6419. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  6420. return good, err
  6421. }
  6422. func GetMobileGoodList(org_id int64) (list []*models.GoodInformationThirty, err error) {
  6423. err = XTReadDB().Where("org_id = ? and status =1", org_id).Find(&list).Error
  6424. return list, err
  6425. }
  6426. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  6427. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  6428. return project, err
  6429. }
  6430. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6431. info := models.GoodInfo{}
  6432. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6433. return info, err
  6434. }
  6435. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  6436. db := XTReadDB().Where("status = 1")
  6437. if good_id > 0 {
  6438. db = db.Where("good_id = ?", good_id)
  6439. }
  6440. err = db.Find(&info).Error
  6441. return info, err
  6442. }
  6443. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6444. db := XTReadDB().Model(&info).Where("status = 1")
  6445. if drug_id > 0 {
  6446. db = db.Where("drug_id = ?", drug_id)
  6447. }
  6448. err = db.Find(&info).Error
  6449. return info, err
  6450. }
  6451. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6452. lib := models.BaseDrugLib{}
  6453. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6454. return lib, err
  6455. }
  6456. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  6457. info := models.Warehousing{}
  6458. 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
  6459. if err == gorm.ErrRecordNotFound {
  6460. return nil, err
  6461. }
  6462. if err != nil {
  6463. return nil, err
  6464. }
  6465. return &info, nil
  6466. }
  6467. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  6468. info := models.Warehousing{}
  6469. 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
  6470. return info, err
  6471. }
  6472. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  6473. out := models.DrugWarehouseOut{}
  6474. 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
  6475. if err == gorm.ErrRecordNotFound {
  6476. return nil, err
  6477. }
  6478. if err != nil {
  6479. return nil, err
  6480. }
  6481. return &out, nil
  6482. }
  6483. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  6484. info := models.DrugWarehouseOut{}
  6485. 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
  6486. return info, err
  6487. }
  6488. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  6489. info := models.DrugWarehouse{}
  6490. 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
  6491. if err == gorm.ErrRecordNotFound {
  6492. return nil, err
  6493. }
  6494. if err != nil {
  6495. return nil, err
  6496. }
  6497. return &info, nil
  6498. }
  6499. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  6500. err := XTWriteDB().Create(&warehouse).Error
  6501. return err
  6502. }
  6503. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  6504. info := models.DrugWarehouse{}
  6505. 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
  6506. return info, err
  6507. }
  6508. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6509. info := models.GoodInfo{}
  6510. 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
  6511. 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
  6512. return info, err
  6513. }
  6514. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6515. info := models.GoodInfo{}
  6516. 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
  6517. 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
  6518. return info, err
  6519. }
  6520. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6521. tx := XTWriteDB().Begin()
  6522. 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
  6523. if err != nil {
  6524. tx.Rollback()
  6525. return err
  6526. }
  6527. tx.Commit()
  6528. return err
  6529. }
  6530. func UpdateNewMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64, tx *gorm.DB) error {
  6531. 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
  6532. if err != nil {
  6533. tx.Rollback()
  6534. return err
  6535. }
  6536. return err
  6537. }
  6538. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6539. tx := XTWriteDB().Begin()
  6540. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6541. if err != nil {
  6542. tx.Rollback()
  6543. return err
  6544. }
  6545. tx.Commit()
  6546. return err
  6547. }
  6548. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6549. // 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
  6550. // return err
  6551. //}
  6552. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6553. 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
  6554. return err
  6555. }
  6556. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6557. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6558. table := XTReadDB().Table("xt_base_drug as x d")
  6559. fmt.Println(table)
  6560. if startime > 0 {
  6561. db = db.Where("x.advice_date >= ?", startime)
  6562. }
  6563. if endtime > 0 {
  6564. db = db.Where("x.advice_date <= ?", endtime)
  6565. }
  6566. if orgid > 0 {
  6567. db = db.Where("x.user_org_id = ?", orgid)
  6568. }
  6569. if len(keyword) > 0 {
  6570. likeKey := "%" + keyword + "%"
  6571. 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)
  6572. }
  6573. offset := (page - 1) * limit
  6574. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6575. return adviceinfo, total, err
  6576. }
  6577. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6578. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6579. table := XTReadDB().Table("xt_base_drug as x d")
  6580. fmt.Println(table)
  6581. if startime > 0 {
  6582. db = db.Where("x.advice_date >= ?", startime)
  6583. }
  6584. if endtime > 0 {
  6585. db = db.Where("x.advice_date <= ?", endtime)
  6586. }
  6587. if orgid > 0 {
  6588. db = db.Where("x.user_org_id = ?", orgid)
  6589. }
  6590. if len(keyword) > 0 {
  6591. likeKey := "%" + keyword + "%"
  6592. 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)
  6593. }
  6594. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6595. return adviceinfo, total, err
  6596. }
  6597. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6598. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6599. return drug, err
  6600. }
  6601. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6602. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6603. table := XTReadDB().Table("xt_good_information as d")
  6604. fmt.Println(table)
  6605. if startime > 0 {
  6606. db = db.Where("x.record_date >= ?", startime)
  6607. }
  6608. if endtime > 0 {
  6609. db = db.Where("x.record_date <= ?", endtime)
  6610. }
  6611. if orgid > 0 {
  6612. db = db.Where("x.user_org_id = ?", orgid)
  6613. }
  6614. if len(keyword) > 0 {
  6615. likeKey := "%" + keyword + "%"
  6616. 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")
  6617. }
  6618. offset := (page - 1) * limit
  6619. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6620. return project, total, err
  6621. }
  6622. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6623. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6624. table := XTReadDB().Table("xt_good_information as d")
  6625. fmt.Println(table)
  6626. if startime > 0 {
  6627. db = db.Where("x.record_date >= ?", startime)
  6628. }
  6629. if endtime > 0 {
  6630. db = db.Where("x.record_date <= ?", endtime)
  6631. }
  6632. if orgid > 0 {
  6633. db = db.Where("x.user_org_id = ?", orgid)
  6634. }
  6635. if len(keyword) > 0 {
  6636. likeKey := "%" + keyword + "%"
  6637. 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")
  6638. }
  6639. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6640. return project, total, err
  6641. }
  6642. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6643. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6644. return good, err
  6645. }
  6646. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6647. setting := models.XtStockSetting{}
  6648. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6649. if err == gorm.ErrRecordNotFound {
  6650. return nil, err
  6651. }
  6652. if err != nil {
  6653. return nil, err
  6654. }
  6655. return &setting, nil
  6656. }
  6657. func CreateStockSetting(setting models.XtStockSetting) error {
  6658. err := XTWriteDB().Create(&setting).Error
  6659. return err
  6660. }
  6661. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6662. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6663. return err
  6664. }
  6665. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6666. setting := models.XtStockSetting{}
  6667. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6668. return setting, err
  6669. }
  6670. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6671. detail := models.BloodAutomaticReduceDetail{}
  6672. 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
  6673. return detail, err
  6674. }
  6675. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6676. detail := models.WarehouseOutInfoNight{}
  6677. 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
  6678. return detail, err
  6679. }
  6680. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6681. setting := models.XtAdviceSetting{}
  6682. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6683. if err == gorm.ErrRecordNotFound {
  6684. return nil, err
  6685. }
  6686. if err != nil {
  6687. return nil, err
  6688. }
  6689. return &setting, nil
  6690. }
  6691. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6692. err := XTWriteDB().Create(&setting).Error
  6693. return err
  6694. }
  6695. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6696. 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
  6697. return err
  6698. }
  6699. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6700. setting := models.XtAdviceSetting{}
  6701. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6702. return setting, err
  6703. }
  6704. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6705. config := models.XtPrescriptionConfig{}
  6706. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6707. if err == gorm.ErrRecordNotFound {
  6708. return nil, err
  6709. }
  6710. if err != nil {
  6711. return nil, err
  6712. }
  6713. return &config, nil
  6714. }
  6715. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6716. err := XTWriteDB().Create(&prescriptionConfig).Error
  6717. return err
  6718. }
  6719. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6720. 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
  6721. return err
  6722. }
  6723. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6724. config := models.XtPrescriptionConfig{}
  6725. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6726. return config, err
  6727. }
  6728. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6729. var deliver_number int64 = 0
  6730. var stock_number int64 = 0
  6731. deliver_number = maxNumber
  6732. // 根据先进先出原则,查询最先入库的批次,进行出库
  6733. // 如果没有对应的库存,则报错
  6734. //开启事物
  6735. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6736. if err != nil {
  6737. return err
  6738. }
  6739. // 将该批次的剩余库存数量转换为拆零数量
  6740. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6741. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6742. if stock_number >= deliver_number {
  6743. var maxNumber int64 = 0
  6744. var minNumber int64 = 0
  6745. var stock_max_number int64 = 0
  6746. stock_max_number = warehouse.StockMaxNumber
  6747. maxNumber = deliver_number / drup.MinNumber
  6748. minNumber = deliver_number % drup.MinNumber
  6749. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6750. minNumber = maxNumber
  6751. }
  6752. if drup.MaxUnit != drup.MinUnit {
  6753. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6754. return errors.New("库存数量不足")
  6755. }
  6756. }
  6757. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6758. if warehouse.StockMaxNumber < 0 {
  6759. warehouse.StockMaxNumber = 0
  6760. }
  6761. if warehouse.StockMinNumber < 0 {
  6762. warehouse.StockMinNumber = 0
  6763. }
  6764. warehouse.Mtime = time.Now().Unix()
  6765. if warehouse.StockMinNumber < minNumber {
  6766. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6767. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6768. } else {
  6769. if minNumber > 0 {
  6770. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6771. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6772. } else {
  6773. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6774. }
  6775. }
  6776. if minNumber == 0 && maxNumber != 1 {
  6777. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6778. }
  6779. }
  6780. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6781. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6782. if warehouse.StockMinNumber == 0 {
  6783. warehouse.StockMaxNumber = stock_max_number
  6784. }
  6785. }
  6786. if drup.MaxUnit != drup.MinUnit {
  6787. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6788. return errors.New("库存数量不足")
  6789. }
  6790. }
  6791. if warehouse.StockMinNumber <= 0 {
  6792. warehouse.StockMinNumber = 0
  6793. }
  6794. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6795. fmt.Println(errThree)
  6796. //查询默认仓库
  6797. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6798. //查询默认仓库剩余多少库存
  6799. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6800. var sum_count int64
  6801. var sum_in_count int64
  6802. for _, it := range list {
  6803. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6804. if it.MaxUnit == baseDrug.MaxUnit {
  6805. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6806. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6807. sum_count += it.StockMaxNumber + it.StockMinNumber
  6808. sum_in_count += it.WarehousingCount
  6809. }
  6810. }
  6811. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6812. return nil
  6813. } else {
  6814. warehouse.StockMaxNumber = 0
  6815. warehouse.StockMinNumber = 0
  6816. warehouse.Mtime = time.Now().Unix()
  6817. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6818. fmt.Println(errThree)
  6819. //查询默认仓库
  6820. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6821. //查询默认仓库剩余多少库存
  6822. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6823. var sum_count int64
  6824. var sum_in_count int64
  6825. for _, it := range list {
  6826. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6827. if it.MaxUnit == baseDrug.MaxUnit {
  6828. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6829. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6830. sum_count += it.StockMaxNumber + it.StockMinNumber
  6831. sum_in_count += it.WarehousingCount
  6832. }
  6833. }
  6834. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6835. if errThree != nil {
  6836. return errThree
  6837. }
  6838. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6839. prescribingNumber_two_temp := deliver_number - stock_number
  6840. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6841. }
  6842. return
  6843. }
  6844. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6845. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6846. return err
  6847. }
  6848. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6849. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6850. return err
  6851. }
  6852. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6853. config := models.DrugOutConfig{}
  6854. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6855. if err == gorm.ErrRecordNotFound {
  6856. return nil, err
  6857. }
  6858. if err != nil {
  6859. return nil, err
  6860. }
  6861. return &config, nil
  6862. }
  6863. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6864. config := models.DrugOutConfig{}
  6865. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6866. return config, err
  6867. }
  6868. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6869. config := models.XtHisConfig{}
  6870. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6871. return config, err
  6872. }
  6873. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6874. err := XTWriteDB().Create(&config).Error
  6875. return err
  6876. }
  6877. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6878. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6879. return err
  6880. }
  6881. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6882. config := models.DrugOutConfig{}
  6883. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6884. return config, err
  6885. }
  6886. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6887. config := models.HisSettleStockConfig{}
  6888. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6889. return config, err
  6890. }
  6891. func UpdatedWarehouseOut(id int64) error {
  6892. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6893. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6894. return err
  6895. }
  6896. func UpdatedNewWarehouseOut(id int64, tx *gorm.DB) error {
  6897. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6898. if err != nil {
  6899. tx.Rollback()
  6900. return err
  6901. }
  6902. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6903. if err != nil {
  6904. tx.Rollback()
  6905. return err
  6906. }
  6907. return err
  6908. }
  6909. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6910. tx := XTWriteDB().Begin()
  6911. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6912. if err != nil {
  6913. tx.Rollback()
  6914. return err
  6915. }
  6916. tx.Commit()
  6917. return err
  6918. }
  6919. func UpdatedNewSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6920. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6921. if err != nil {
  6922. tx.Rollback()
  6923. return err
  6924. }
  6925. return err
  6926. }
  6927. func GetLastGoodWarehouseOutInfo(orgid int64, id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  6928. err = tx.Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6929. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6930. }).Find(&info).Error
  6931. if err != nil {
  6932. tx.Rollback()
  6933. return info, err
  6934. }
  6935. return info, err
  6936. }
  6937. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6938. info := models.WarehouseOutInfo{}
  6939. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6940. return info, err
  6941. }
  6942. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6943. err := tx.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
  6944. if err != nil {
  6945. tx.Rollback()
  6946. return err
  6947. }
  6948. return err
  6949. }
  6950. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6951. 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
  6952. return err
  6953. }
  6954. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6955. out := models.WarehouseOut{}
  6956. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6957. return out, err
  6958. }
  6959. func GetLastGoodWarehouseOutTwentyOne(orgid int64, tx *gorm.DB) (models.WarehouseOut, error) {
  6960. out := models.WarehouseOut{}
  6961. err := tx.Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6962. if err != gorm.ErrRecordNotFound {
  6963. if err != nil {
  6964. tx.Rollback()
  6965. return out, err
  6966. }
  6967. }
  6968. return out, err
  6969. }
  6970. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6971. ut := writeDb.Begin()
  6972. 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
  6973. if err != nil {
  6974. ut.Rollback()
  6975. return err
  6976. }
  6977. ut.Commit()
  6978. return err
  6979. }
  6980. func ModifyGoodOutInfoLog(good_id int64, id int64, record_date int64, user_org_id int64, tx *gorm.DB) error {
  6981. outInfoLog := models.XtGoodWarehouseOutInfoLog{}
  6982. err := tx.Model(&outInfoLog).Where("good_id = ? and warehouse_out_info_id = ? and status=1 and record_date = ? and user_org_id = ?", good_id, id, record_date, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  6983. if err != nil {
  6984. tx.Rollback()
  6985. return err
  6986. }
  6987. return err
  6988. }
  6989. func UpdateaNewGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64, tx *gorm.DB) error {
  6990. err := tx.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
  6991. if err != nil {
  6992. tx.Rollback()
  6993. return err
  6994. }
  6995. return err
  6996. }
  6997. func ModifyGoodWarehouseOut(id int64) error {
  6998. ut := writeDb.Begin()
  6999. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7000. if err != nil {
  7001. ut.Rollback()
  7002. return err
  7003. }
  7004. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7005. if err != nil {
  7006. ut.Rollback()
  7007. return err
  7008. }
  7009. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7010. if err != nil {
  7011. ut.Rollback()
  7012. return err
  7013. }
  7014. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7015. if err != nil {
  7016. ut.Rollback()
  7017. return err
  7018. }
  7019. ut.Commit()
  7020. return err
  7021. }
  7022. func ModifyNewGoodWarehouseOut(id int64, tx *gorm.DB) error {
  7023. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7024. if err != nil {
  7025. tx.Rollback()
  7026. return err
  7027. }
  7028. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7029. if err != nil {
  7030. tx.Rollback()
  7031. return err
  7032. }
  7033. err = tx.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7034. if err != nil {
  7035. tx.Rollback()
  7036. return err
  7037. }
  7038. err = tx.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7039. if err != nil {
  7040. tx.Rollback()
  7041. return err
  7042. }
  7043. return err
  7044. }
  7045. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7046. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7047. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7048. }).Find(&info).Error
  7049. return info, err
  7050. }
  7051. func GetNewGoodWarehouseInfoById(waresing_id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7052. err = tx.Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7053. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7054. }).Find(&info).Error
  7055. if err != gorm.ErrRecordNotFound {
  7056. if err != nil {
  7057. tx.Rollback()
  7058. return info, err
  7059. }
  7060. }
  7061. return info, err
  7062. }
  7063. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  7064. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  7065. return info, err
  7066. }
  7067. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  7068. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  7069. return err
  7070. }
  7071. func UpdateNewWarehouseDetail(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  7072. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  7073. if err != nil {
  7074. tx.Rollback()
  7075. return err
  7076. }
  7077. return err
  7078. }
  7079. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  7080. tx := XTWriteDB().Begin()
  7081. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7082. if err != nil {
  7083. tx.Rollback()
  7084. return err
  7085. }
  7086. tx.Commit()
  7087. return err
  7088. }
  7089. func UpdateNewCheckWarehouseInfo(id int64, checker int64, tx *gorm.DB) error {
  7090. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7091. if err != nil {
  7092. tx.Rollback()
  7093. return err
  7094. }
  7095. return err
  7096. }
  7097. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  7098. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7099. return info, err
  7100. }
  7101. func GetNewWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7102. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7103. if err != gorm.ErrRecordNotFound {
  7104. if err != nil {
  7105. tx.Rollback()
  7106. return info, err
  7107. }
  7108. }
  7109. return info, err
  7110. }
  7111. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  7112. tx := XTWriteDB().Begin()
  7113. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7114. if err != nil {
  7115. tx.Rollback()
  7116. return err
  7117. }
  7118. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7119. if err != nil {
  7120. tx.Rollback()
  7121. return err
  7122. }
  7123. tx.Commit()
  7124. return err
  7125. }
  7126. func UpdateNewWarehouseInfoByIdListThree(count int64, id int64, tx *gorm.DB) error {
  7127. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7128. if err != nil {
  7129. tx.Rollback()
  7130. return err
  7131. }
  7132. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7133. if err != nil {
  7134. tx.Rollback()
  7135. return err
  7136. }
  7137. return err
  7138. }
  7139. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  7140. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7141. return info, err
  7142. }
  7143. func FindNewLastWarehousingSix(org_id int64, tx *gorm.DB) (info models.Warehousing, err error) {
  7144. err = tx.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7145. if err != gorm.ErrRecordNotFound {
  7146. if err != nil {
  7147. tx.Rollback()
  7148. return info, err
  7149. }
  7150. }
  7151. return info, err
  7152. }
  7153. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  7154. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7155. return info, err
  7156. }
  7157. func FindNewLastWarehousingSeven(org_id int64, tx *gorm.DB) (info models.DrugWarehouse, err error) {
  7158. err = tx.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7159. if err != gorm.ErrRecordNotFound {
  7160. if err != nil {
  7161. tx.Rollback()
  7162. return info, err
  7163. }
  7164. }
  7165. return info, err
  7166. }
  7167. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7168. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7169. return info, err
  7170. }
  7171. func FindNewDrugWarehouseInfoListById(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7172. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7173. if err != gorm.ErrRecordNotFound {
  7174. if err != nil {
  7175. tx.Rollback()
  7176. return info, err
  7177. }
  7178. }
  7179. return info, err
  7180. }
  7181. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  7182. err := XTWriteDB().Create(&info).Error
  7183. return err
  7184. }
  7185. func CreatedNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7186. err := tx.Create(&info).Error
  7187. if err != nil {
  7188. tx.Rollback()
  7189. return err
  7190. }
  7191. return err
  7192. }
  7193. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  7194. err := XTWriteDB().Save(&info).Error
  7195. return err
  7196. }
  7197. func UpdateNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7198. err := tx.Save(&info).Error
  7199. if err != nil {
  7200. tx.Rollback()
  7201. return err
  7202. }
  7203. return err
  7204. }
  7205. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  7206. tx := XTWriteDB().Begin()
  7207. 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
  7208. if err != nil {
  7209. tx.Rollback()
  7210. return err
  7211. }
  7212. tx.Commit()
  7213. return err
  7214. }
  7215. func CheckNewWarehousingInfo(id int64, orgid int64, checker int64, tx *gorm.DB) error {
  7216. 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
  7217. if err != nil {
  7218. tx.Rollback()
  7219. return err
  7220. }
  7221. return err
  7222. }
  7223. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  7224. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7225. return info, err
  7226. }
  7227. func GetNewWarehousingInfoByList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7228. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7229. if err != gorm.ErrRecordNotFound {
  7230. if err != nil {
  7231. tx.Rollback()
  7232. return info, err
  7233. }
  7234. }
  7235. return info, err
  7236. }
  7237. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  7238. tx := XTWriteDB().Begin()
  7239. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7240. if err != nil {
  7241. tx.Rollback()
  7242. return err
  7243. }
  7244. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7245. if err != nil {
  7246. tx.Rollback()
  7247. return err
  7248. }
  7249. tx.Commit()
  7250. return err
  7251. }
  7252. func AddNewDrugWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7253. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7254. if err != nil {
  7255. tx.Rollback()
  7256. return err
  7257. }
  7258. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7259. if err != nil {
  7260. tx.Rollback()
  7261. return err
  7262. }
  7263. return err
  7264. }
  7265. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  7266. tx := XTWriteDB().Begin()
  7267. 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
  7268. if err != nil {
  7269. tx.Rollback()
  7270. return err
  7271. }
  7272. tx.Commit()
  7273. return err
  7274. }
  7275. func DeleteModiftyDrugWarehouseInfo(id int64, tx *gorm.DB) error {
  7276. info := models.DrugWarehouseInfo{}
  7277. err := tx.Model(&info).Where("id= ? and status=1", id).Updates(map[string]interface{}{"sum_count": 0}).Error
  7278. if err != nil {
  7279. tx.Rollback()
  7280. return err
  7281. }
  7282. return err
  7283. }
  7284. func UpdateNewWarehousingInfoFlow(id int64, orgid int64, tx *gorm.DB) error {
  7285. 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
  7286. if err != nil {
  7287. tx.Rollback()
  7288. return err
  7289. }
  7290. return err
  7291. }
  7292. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  7293. tx := XTWriteDB().Begin()
  7294. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7295. if err != nil {
  7296. tx.Rollback()
  7297. return err
  7298. }
  7299. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7300. tx.Commit()
  7301. return err
  7302. }
  7303. func AddNewDrugWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7304. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7305. if err != nil {
  7306. tx.Rollback()
  7307. return err
  7308. }
  7309. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7310. if err != nil {
  7311. tx.Rollback()
  7312. return err
  7313. }
  7314. return err
  7315. }
  7316. func ModifyDrugWarehouseInfoById(id int64, sum_count int64, tx *gorm.DB) error {
  7317. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  7318. if err != nil {
  7319. tx.Rollback()
  7320. return err
  7321. }
  7322. return err
  7323. }
  7324. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  7325. tx := XTWriteDB().Begin()
  7326. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7327. if err != nil {
  7328. tx.Rollback()
  7329. return err
  7330. }
  7331. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7332. if err != nil {
  7333. tx.Rollback()
  7334. return err
  7335. }
  7336. tx.Commit()
  7337. return err
  7338. }
  7339. func ReturnNewCheckWarehouseingInfo(id int64, orgid int64, tx *gorm.DB) error {
  7340. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7341. if err != nil {
  7342. tx.Rollback()
  7343. return err
  7344. }
  7345. err = tx.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7346. if err != nil {
  7347. tx.Rollback()
  7348. return err
  7349. }
  7350. err = tx.Model(models.XtDrugWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", id).Update(map[string]interface{}{"status": 0}).Error
  7351. if err != nil {
  7352. tx.Rollback()
  7353. return err
  7354. }
  7355. return err
  7356. }
  7357. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  7358. tx := XTWriteDB().Begin()
  7359. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7360. if err != nil {
  7361. tx.Rollback()
  7362. return err
  7363. }
  7364. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7365. if err != nil {
  7366. tx.Rollback()
  7367. return err
  7368. }
  7369. tx.Commit()
  7370. return err
  7371. }
  7372. func AddNewDrugReturnWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7373. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7374. if err != nil {
  7375. tx.Rollback()
  7376. return err
  7377. }
  7378. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7379. if err != nil {
  7380. tx.Rollback()
  7381. return err
  7382. }
  7383. return err
  7384. }
  7385. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  7386. tx := XTWriteDB().Begin()
  7387. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7388. if err != nil {
  7389. tx.Rollback()
  7390. return err
  7391. }
  7392. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7393. if err != nil {
  7394. tx.Rollback()
  7395. return err
  7396. }
  7397. tx.Commit()
  7398. return err
  7399. }
  7400. func AddNewDrugReturnWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7401. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7402. if err != nil {
  7403. tx.Rollback()
  7404. return err
  7405. }
  7406. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7407. if err != nil {
  7408. tx.Rollback()
  7409. return err
  7410. }
  7411. return err
  7412. }
  7413. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  7414. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  7415. return err
  7416. }
  7417. func UpdateNewDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7418. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  7419. if err != nil {
  7420. tx.Rollback()
  7421. return err
  7422. }
  7423. return err
  7424. }
  7425. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  7426. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7427. return err
  7428. }
  7429. func UpdatedNewDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7430. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7431. if err != nil {
  7432. tx.Rollback()
  7433. return err
  7434. }
  7435. return err
  7436. }
  7437. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7438. err := tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7439. if err != gorm.ErrRecordNotFound {
  7440. return err
  7441. }
  7442. return err
  7443. }
  7444. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  7445. out := models.DrugWarehouseOut{}
  7446. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7447. return out, err
  7448. }
  7449. func GetNewDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7450. out := models.DrugWarehouseOut{}
  7451. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7452. if err != gorm.ErrRecordNotFound {
  7453. if err != nil {
  7454. tx.Rollback()
  7455. return out, err
  7456. }
  7457. }
  7458. return out, err
  7459. }
  7460. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7461. out := models.XtSelfDrugWarehouseOut{}
  7462. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7463. if err != gorm.ErrRecordNotFound {
  7464. if err != nil {
  7465. tx.Rollback()
  7466. return out, err
  7467. }
  7468. }
  7469. return out, err
  7470. }
  7471. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7472. out := models.XtSelfDrugWarehouseOut{}
  7473. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7474. if err != gorm.ErrRecordNotFound {
  7475. if err != nil {
  7476. tx.Rollback()
  7477. return out, err
  7478. }
  7479. }
  7480. return out, err
  7481. }
  7482. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7483. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7484. return info, err
  7485. }
  7486. func GetNewDrugWarehouseOutListById(warehouse_out_id int64, orgId int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  7487. err = tx.Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7488. if err != gorm.ErrRecordNotFound {
  7489. if err != nil {
  7490. tx.Rollback()
  7491. return info, err
  7492. }
  7493. }
  7494. return info, err
  7495. }
  7496. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  7497. tx := XTWriteDB().Begin()
  7498. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7499. if err != nil {
  7500. tx.Rollback()
  7501. return err
  7502. }
  7503. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7504. if err != nil {
  7505. tx.Rollback()
  7506. return err
  7507. }
  7508. tx.Commit()
  7509. return err
  7510. }
  7511. func UpdateNewCheckDrugOut(out models.DrugWarehouseOut, id int64, tx *gorm.DB) error {
  7512. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7513. if err != nil {
  7514. tx.Rollback()
  7515. return err
  7516. }
  7517. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7518. if err != nil {
  7519. tx.Rollback()
  7520. return err
  7521. }
  7522. return err
  7523. }
  7524. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  7525. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7526. return err
  7527. }
  7528. func UpdateNewCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64, tx *gorm.DB) error {
  7529. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7530. if err != nil {
  7531. tx.Rollback()
  7532. return err
  7533. }
  7534. return err
  7535. }
  7536. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  7537. out := models.DrugWarehouseOut{}
  7538. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  7539. return out, err
  7540. }
  7541. func GetNewDrugWarehouseOutByLastId(id int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7542. out := models.DrugWarehouseOut{}
  7543. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7544. if err != gorm.ErrRecordNotFound {
  7545. if err != nil {
  7546. tx.Rollback()
  7547. return out, err
  7548. }
  7549. }
  7550. return out, err
  7551. }
  7552. func UpdateDrugMaxNumber(count int64, id int64) error {
  7553. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7554. return err
  7555. }
  7556. func UpdateNewDrugMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7557. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7558. if err != gorm.ErrRecordNotFound {
  7559. if err != nil {
  7560. tx.Rollback()
  7561. return err
  7562. }
  7563. }
  7564. return err
  7565. }
  7566. func UpdateNewDrugMaxNumberOne(count int64, id int64, tx *gorm.DB) error {
  7567. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7568. if err != gorm.ErrRecordNotFound {
  7569. if err != nil {
  7570. tx.Rollback()
  7571. return err
  7572. }
  7573. }
  7574. return err
  7575. }
  7576. func UpdateDrugMinNumber(count int64, id int64) error {
  7577. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7578. return err
  7579. }
  7580. func UpdateNewDrugMinNumber(count int64, id int64, tx *gorm.DB) error {
  7581. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7582. if err != nil {
  7583. tx.Rollback()
  7584. return err
  7585. }
  7586. return err
  7587. }
  7588. func DeleteDrugOutFlow(id int64, orgid int64) error {
  7589. 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
  7590. return err
  7591. }
  7592. func DeleteNewDrugOutFlow(id int64, orgid int64, tx *gorm.DB) error {
  7593. err := tx.Model(models.DrugFlow{}).Where("warehouse_out_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  7594. if err != nil {
  7595. tx.Rollback()
  7596. return err
  7597. }
  7598. return err
  7599. }
  7600. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  7601. 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
  7602. return outInfo, err
  7603. }
  7604. func GetNewWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.WarehouseOutInfo, err error) {
  7605. err = tx.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
  7606. if err != gorm.ErrRecordNotFound {
  7607. if err != nil {
  7608. tx.Rollback()
  7609. return outInfo, err
  7610. }
  7611. }
  7612. return outInfo, err
  7613. }
  7614. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7615. 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
  7616. return outInfo, err
  7617. }
  7618. func GetNewDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7619. err = tx.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
  7620. if err != gorm.ErrRecordNotFound {
  7621. if err != nil {
  7622. tx.Rollback()
  7623. return outInfo, err
  7624. }
  7625. }
  7626. return outInfo, err
  7627. }
  7628. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7629. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7630. return info, err
  7631. }
  7632. func GetNewAllStockInList(id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7633. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7634. if err != gorm.ErrRecordNotFound {
  7635. if err != nil {
  7636. tx.Rollback()
  7637. return info, err
  7638. }
  7639. }
  7640. return info, err
  7641. }
  7642. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7643. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7644. return info, err
  7645. }
  7646. func GetNewDrugWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7647. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7648. if err != gorm.ErrRecordNotFound {
  7649. if err != nil {
  7650. tx.Rollback()
  7651. return info, err
  7652. }
  7653. }
  7654. return info, err
  7655. }
  7656. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  7657. 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
  7658. return info, err
  7659. }
  7660. func GetNewStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7661. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", goodid, storehouse_id, orgId).Find(&info).Error
  7662. if err != gorm.ErrRecordNotFound {
  7663. if err != nil {
  7664. tx.Rollback()
  7665. return info, err
  7666. }
  7667. }
  7668. return info, err
  7669. }
  7670. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  7671. 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
  7672. 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
  7673. return err
  7674. }
  7675. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  7676. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7677. return err
  7678. }
  7679. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  7680. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7681. return err
  7682. }
  7683. func UpdateNewGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7684. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7685. if err != gorm.ErrRecordNotFound {
  7686. if err != nil {
  7687. tx.Rollback()
  7688. return err
  7689. }
  7690. }
  7691. return err
  7692. }
  7693. func GetGoodFlowByWarehouseOutId(good_id int64, user_org_id int64, id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  7694. err = tx.Where("good_id = ? and user_org_id = ? and warehouse_out_detail_id = ? and status=1", good_id, user_org_id, id).Find(&flow).Error
  7695. if err != gorm.ErrRecordNotFound {
  7696. if err != nil {
  7697. tx.Rollback()
  7698. return flow, err
  7699. }
  7700. }
  7701. return
  7702. }
  7703. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  7704. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7705. 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
  7706. return err
  7707. }
  7708. func UpdateNewGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7709. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7710. if err != nil {
  7711. tx.Rollback()
  7712. return err
  7713. }
  7714. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7715. if err != nil {
  7716. tx.Rollback()
  7717. return err
  7718. }
  7719. return err
  7720. }
  7721. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  7722. 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
  7723. return stock, err
  7724. }
  7725. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  7726. 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
  7727. return err
  7728. }
  7729. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7730. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7731. return info, err
  7732. }
  7733. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  7734. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7735. return info, err
  7736. }
  7737. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  7738. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7739. return info, err
  7740. }
  7741. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  7742. 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
  7743. 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
  7744. return err
  7745. }
  7746. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7747. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  7748. return info, err
  7749. }
  7750. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  7751. 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
  7752. return err
  7753. }
  7754. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  7755. 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
  7756. return err
  7757. }
  7758. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  7759. 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
  7760. 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
  7761. return err
  7762. }
  7763. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  7764. 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
  7765. return err
  7766. }
  7767. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  7768. 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
  7769. 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
  7770. return err
  7771. }
  7772. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  7773. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7774. 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
  7775. return err
  7776. }
  7777. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  7778. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  7779. return info, err
  7780. }
  7781. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  7782. 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
  7783. 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
  7784. return err
  7785. }
  7786. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  7787. 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
  7788. return info, err
  7789. }
  7790. func GetSelfDrugAllStockInfoOne(patient_id int64, orgid int64, drugid int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  7791. err = XTReadDB().Where("patient_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", patient_id, orgid, drugid).Find(&info).Error
  7792. return info, err
  7793. }
  7794. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7795. 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
  7796. if err != gorm.ErrRecordNotFound {
  7797. if err != nil {
  7798. tx.Rollback()
  7799. return info, err
  7800. }
  7801. }
  7802. return info, err
  7803. }
  7804. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  7805. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7806. 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
  7807. return err
  7808. }
  7809. func UpdateNewBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7810. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7811. if err != gorm.ErrRecordNotFound {
  7812. if err != nil {
  7813. tx.Rollback()
  7814. return err
  7815. }
  7816. }
  7817. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7818. if err != gorm.ErrRecordNotFound {
  7819. if err != nil {
  7820. tx.Rollback()
  7821. return err
  7822. }
  7823. }
  7824. return err
  7825. }
  7826. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  7827. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7828. return err
  7829. }
  7830. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7831. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7832. if err != nil {
  7833. tx.Rollback()
  7834. return err
  7835. }
  7836. return err
  7837. }
  7838. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7839. 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
  7840. return info, err
  7841. }
  7842. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7843. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7844. return info, err
  7845. }
  7846. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  7847. 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
  7848. 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
  7849. return err
  7850. }
  7851. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  7852. stock := models.DrugCancelStock{}
  7853. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  7854. return stock, err
  7855. }
  7856. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7857. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  7858. return stock, err
  7859. }
  7860. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7861. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  7862. return stock, err
  7863. }
  7864. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7865. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  7866. return info, err
  7867. }
  7868. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  7869. 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
  7870. return err
  7871. }
  7872. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7873. 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
  7874. if err != nil {
  7875. tx.Rollback()
  7876. return err
  7877. }
  7878. return err
  7879. }
  7880. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  7881. 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
  7882. return err
  7883. }
  7884. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7885. 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
  7886. if err != nil {
  7887. tx.Rollback()
  7888. return err
  7889. }
  7890. return err
  7891. }
  7892. func ModifyDrugWareshoueInfoId(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7893. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", count)).Error
  7894. if err != nil {
  7895. tx.Rollback()
  7896. return err
  7897. }
  7898. return err
  7899. }
  7900. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  7901. 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
  7902. return err
  7903. }
  7904. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  7905. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7906. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7907. return err
  7908. }
  7909. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7910. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  7911. return info, err
  7912. }
  7913. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  7914. 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
  7915. return err
  7916. }
  7917. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7918. 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
  7919. return err
  7920. }
  7921. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7922. 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
  7923. return err
  7924. }
  7925. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  7926. info := models.DrugWarehouseInfo{}
  7927. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7928. return info, err
  7929. }
  7930. func GetSelfDrugInfoSix(id int64, orgid int64) (models.XtSelfDrugWarehouseInfo, error) {
  7931. info := models.XtSelfDrugWarehouseInfo{}
  7932. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7933. return info, err
  7934. }
  7935. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  7936. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  7937. if storehouse_id > 0 {
  7938. db = db.Where("storehouse_id = ?", storehouse_id)
  7939. }
  7940. if orgid > 0 {
  7941. db = db.Where("user_org_id = ?", orgid)
  7942. }
  7943. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  7944. return storeinventory, err
  7945. }
  7946. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  7947. detail := models.AutomaticReduceDetail{}
  7948. 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
  7949. return detail, err
  7950. }
  7951. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  7952. detail := models.WarehouseOutInfo{}
  7953. 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
  7954. return detail, err
  7955. }
  7956. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  7957. stock := models.XtGoodStockCount{}
  7958. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  7959. return stock, err
  7960. }
  7961. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  7962. err := XTWriteDB().Create(&stock).Error
  7963. return err
  7964. }
  7965. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7966. 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
  7967. return err
  7968. }
  7969. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7970. ut := XTWriteDB().Begin()
  7971. 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
  7972. if err != nil {
  7973. ut.Rollback()
  7974. return err
  7975. }
  7976. ut.Commit()
  7977. return err
  7978. }
  7979. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  7980. 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
  7981. return err
  7982. }
  7983. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  7984. 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
  7985. if err != nil {
  7986. tx.Rollback()
  7987. return err
  7988. }
  7989. return err
  7990. }
  7991. func CreateDrugStock(stock models.XtDrugStockCount) error {
  7992. err := XTWriteDB().Create(&stock).Error
  7993. return err
  7994. }
  7995. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  7996. ut := XTWriteDB().Begin()
  7997. 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
  7998. if err != nil {
  7999. ut.Rollback()
  8000. return err
  8001. }
  8002. 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
  8003. if err != nil {
  8004. ut.Rollback()
  8005. return err
  8006. }
  8007. ut.Commit()
  8008. return err
  8009. }
  8010. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  8011. ut := XTWriteDB().Begin()
  8012. 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
  8013. if err != nil {
  8014. ut.Rollback()
  8015. return err
  8016. }
  8017. ut.Commit()
  8018. return err
  8019. }
  8020. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  8021. ut := XTWriteDB().Begin()
  8022. 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
  8023. if err != nil {
  8024. ut.Rollback()
  8025. return err
  8026. }
  8027. 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
  8028. if err != nil {
  8029. ut.Rollback()
  8030. return err
  8031. }
  8032. ut.Commit()
  8033. return err
  8034. }
  8035. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  8036. ut := XTWriteDB().Begin()
  8037. 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
  8038. if err != nil {
  8039. ut.Rollback()
  8040. return err
  8041. }
  8042. ut.Commit()
  8043. return err
  8044. }
  8045. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8046. ut := XTWriteDB().Begin()
  8047. 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
  8048. if err != nil {
  8049. ut.Rollback()
  8050. return err
  8051. }
  8052. 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
  8053. if err != nil {
  8054. ut.Rollback()
  8055. return err
  8056. }
  8057. ut.Commit()
  8058. return err
  8059. }
  8060. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8061. ut := XTWriteDB().Begin()
  8062. 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
  8063. if err != nil {
  8064. ut.Rollback()
  8065. return err
  8066. }
  8067. 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
  8068. if err != nil {
  8069. ut.Rollback()
  8070. return err
  8071. }
  8072. ut.Commit()
  8073. return err
  8074. }
  8075. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  8076. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  8077. 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
  8078. return info, err
  8079. } else {
  8080. 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
  8081. return info, err
  8082. }
  8083. }
  8084. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  8085. config := models.HisSettleStockConfig{}
  8086. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8087. if err == gorm.ErrRecordNotFound {
  8088. return nil, err
  8089. }
  8090. if err != nil {
  8091. return nil, err
  8092. }
  8093. return &config, nil
  8094. }
  8095. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  8096. err := XTWriteDB().Create(&config).Error
  8097. return err
  8098. }
  8099. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  8100. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  8101. return err
  8102. }
  8103. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  8104. config := models.DrugStockConfig{}
  8105. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8106. return config, err
  8107. }
  8108. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  8109. stockConfig := models.PharmacyConfig{}
  8110. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8111. return stockConfig, err
  8112. }
  8113. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  8114. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8115. return err
  8116. }
  8117. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  8118. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8119. return err
  8120. }
  8121. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  8122. config := models.HisSettleStockConfig{}
  8123. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8124. return config, nil
  8125. }
  8126. func GetGoodWareHouseOutInfoByGood(good_id int64, sys_record_time int64, user_org_id int64) (info []*models.WarehouseOutInfo, err error) {
  8127. err = XTReadDB().Where("good_id= ? and sys_record_time = ? and org_id = ? and status=1", good_id, sys_record_time, user_org_id).Find(&info).Error
  8128. return info, err
  8129. }
  8130. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  8131. info := models.GoodInformationFourty{}
  8132. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  8133. return info, err
  8134. }
  8135. func GetGoodWarehouseInfoByGoodList(good_id int64) (info []*models.WarehousingInfo, err error) {
  8136. err = XTReadDB().Where("good_id = ? and status =1 and stock_count>0 and is_check = 1", good_id).Find(&info).Error
  8137. return info, err
  8138. }
  8139. func GetAllWarehouseInfo(good_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  8140. err = XTReadDB().Where("good_id = ? and user_org_id =? and status=1 and is_check =1", good_id, user_org_id).Find(&info).Error
  8141. return info, err
  8142. }
  8143. func GetHisPrescriptionByGoodId(good_id int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  8144. err = XTReadDB().Where("project_id = ? and user_org_id =? and status=1 and type =3", good_id, user_org_id).Find(&project).Error
  8145. return project, err
  8146. }
  8147. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  8148. advice := models.HisDoctorAdvice{}
  8149. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8150. return advice, err
  8151. }
  8152. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  8153. advice := models.DoctorAdvice{}
  8154. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8155. return advice, err
  8156. }
  8157. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  8158. out := models.WarehouseOut{}
  8159. 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
  8160. return out, err
  8161. }
  8162. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  8163. config := models.XtGoodOutConfig{}
  8164. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8165. if err == gorm.ErrRecordNotFound {
  8166. return nil, err
  8167. }
  8168. if err != nil {
  8169. return nil, err
  8170. }
  8171. return &config, nil
  8172. }
  8173. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  8174. err := XTWriteDB().Create(&good).Error
  8175. return err
  8176. }
  8177. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  8178. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  8179. return err
  8180. }
  8181. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  8182. config := models.XtGoodOutConfig{}
  8183. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8184. return config, err
  8185. }
  8186. func UpdateGoodAutoConfig(user_org_id int64) error {
  8187. gobalConfig := models.GobalConfig{}
  8188. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8189. return err
  8190. }
  8191. func UpdateGoodOutConfig(user_org_id int64) error {
  8192. config := models.XtGoodOutConfig{}
  8193. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8194. return err
  8195. }
  8196. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  8197. 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
  8198. return info, err
  8199. }
  8200. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  8201. 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
  8202. return good, err
  8203. }
  8204. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  8205. 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
  8206. return err
  8207. }
  8208. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8209. 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
  8210. return err
  8211. }
  8212. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8213. 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
  8214. return err
  8215. }
  8216. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8217. out := models.WarehouseOut{}
  8218. 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
  8219. return out, err
  8220. }
  8221. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8222. out := models.WarehouseOut{}
  8223. 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
  8224. if err != gorm.ErrRecordNotFound {
  8225. if err != nil {
  8226. tx.Rollback()
  8227. return out, err
  8228. }
  8229. }
  8230. return out, err
  8231. }
  8232. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8233. out := models.WarehouseOut{}
  8234. 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
  8235. return out, err
  8236. }
  8237. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8238. out := models.WarehouseOut{}
  8239. 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
  8240. if err != gorm.ErrRecordNotFound {
  8241. if err != nil {
  8242. tx.Rollback()
  8243. return out, err
  8244. }
  8245. }
  8246. return out, err
  8247. }
  8248. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  8249. goodsType := models.GoodsType{}
  8250. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  8251. return goodsType, err
  8252. }
  8253. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  8254. goodsTypeList, _ := GetAllGoodType(org_id)
  8255. for _, item := range goodsTypeList {
  8256. if good_type_id == item.ID {
  8257. good_type_name = item.TypeName
  8258. }
  8259. }
  8260. return good_type_name
  8261. }
  8262. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  8263. information := models.GoodInformation{}
  8264. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  8265. return information, err
  8266. }
  8267. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  8268. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  8269. return info, err
  8270. }
  8271. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  8272. ut := writeDb.Begin()
  8273. 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
  8274. if err != nil {
  8275. ut.Rollback()
  8276. return err
  8277. }
  8278. ut.Commit()
  8279. return err
  8280. }
  8281. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  8282. 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
  8283. if err != nil {
  8284. tx.Rollback()
  8285. return err
  8286. }
  8287. return err
  8288. }
  8289. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  8290. info := models.WarehouseOutInfo{}
  8291. var err error
  8292. 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
  8293. if err == gorm.ErrRecordNotFound {
  8294. return nil, err
  8295. }
  8296. if err != nil {
  8297. return nil, err
  8298. }
  8299. return &info, nil
  8300. }
  8301. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  8302. info := models.WarehouseOutInfo{}
  8303. 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
  8304. return info, err
  8305. }
  8306. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8307. info := models.WarehouseOutInfo{}
  8308. 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
  8309. if err != gorm.ErrRecordNotFound {
  8310. if err != nil {
  8311. tx.Rollback()
  8312. return info, err
  8313. }
  8314. }
  8315. return info, err
  8316. }
  8317. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  8318. info := models.WarehouseOutInfo{}
  8319. 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
  8320. return info, err
  8321. }
  8322. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8323. info := models.WarehouseOutInfo{}
  8324. 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
  8325. if err != gorm.ErrRecordNotFound {
  8326. if err != nil {
  8327. tx.Rollback()
  8328. return info, err
  8329. }
  8330. }
  8331. return info, err
  8332. }
  8333. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  8334. info := models.WarehouseOutInfo{}
  8335. var err error
  8336. 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
  8337. if err == gorm.ErrRecordNotFound {
  8338. return nil, err
  8339. }
  8340. if err != nil {
  8341. return nil, err
  8342. }
  8343. return &info, nil
  8344. }
  8345. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  8346. info := models.WarehouseOutInfo{}
  8347. var err error
  8348. 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
  8349. if err != gorm.ErrRecordNotFound {
  8350. if err != nil {
  8351. tx.Rollback()
  8352. return &info, err
  8353. }
  8354. }
  8355. return &info, err
  8356. }
  8357. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  8358. db := XTReadDB().Model(&info).Where("status = 1")
  8359. if startime > 0 {
  8360. db = db.Where("ctime>=?", startime)
  8361. }
  8362. if endtime > 0 {
  8363. db = db.Where("ctime<=?", endtime)
  8364. }
  8365. if orgid > 0 {
  8366. db = db.Where("org_id = ?", orgid)
  8367. }
  8368. if len(good_id) > 0 {
  8369. db = db.Where("good_id in(?)", good_id)
  8370. }
  8371. if storehouse_id > 0 {
  8372. db = db.Where("storehouse_id = ?", storehouse_id)
  8373. }
  8374. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  8375. return info, err
  8376. }
  8377. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  8378. db := XTReadDB().Model(&info).Where("status= 1")
  8379. if start_time > 0 {
  8380. db = db.Where("ctime>=?", start_time)
  8381. }
  8382. if end_time > 0 {
  8383. db = db.Where("ctime <=?", end_time)
  8384. }
  8385. if orgid > 0 {
  8386. db = db.Where("org_id = ?", orgid)
  8387. }
  8388. if len(good_id) > 0 {
  8389. db = db.Where("good_id in(?)", good_id)
  8390. }
  8391. if storehouse_id > 0 {
  8392. db = db.Where("storehouse_id = ?", storehouse_id)
  8393. }
  8394. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  8395. return info, err
  8396. }
  8397. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  8398. config := models.XtGoodOutConfig{}
  8399. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8400. return config, err
  8401. }
  8402. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  8403. 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
  8404. 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
  8405. return err
  8406. }
  8407. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  8408. tx := XTWriteDB().Begin()
  8409. for _, item := range list {
  8410. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8411. if err != nil {
  8412. tx.Rollback()
  8413. return err
  8414. }
  8415. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8416. if err != nil {
  8417. tx.Rollback()
  8418. return err
  8419. }
  8420. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8421. if err != nil {
  8422. tx.Rollback()
  8423. return err
  8424. }
  8425. var goodList []*models.WarehousingInfo
  8426. 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
  8427. var flush_count int64
  8428. for _, it := range goodList {
  8429. flush_count += it.StockCount
  8430. }
  8431. 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
  8432. if err != nil {
  8433. tx.Rollback()
  8434. return err
  8435. }
  8436. 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
  8437. if err != nil {
  8438. tx.Rollback()
  8439. return err
  8440. }
  8441. storeHouseConfig := models.XtStorehouseConfig{}
  8442. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8443. var stockList []*models.WarehousingInfo
  8444. //查询该耗材剩余多少库存
  8445. 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
  8446. var sum_count int64
  8447. var sum_in_count int64
  8448. for _, it := range stockList {
  8449. sum_count += it.StockCount
  8450. sum_in_count += it.WarehousingCount
  8451. }
  8452. 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
  8453. if err != nil {
  8454. tx.Rollback()
  8455. return err
  8456. }
  8457. 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
  8458. if err != nil {
  8459. tx.Rollback()
  8460. return err
  8461. }
  8462. }
  8463. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8464. if err != nil {
  8465. tx.Rollback()
  8466. return err
  8467. }
  8468. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8469. tx.Commit()
  8470. return err
  8471. }
  8472. func UpdateNewWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64, tx *gorm.DB) error {
  8473. for _, item := range list {
  8474. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8475. if err != nil {
  8476. tx.Rollback()
  8477. return err
  8478. }
  8479. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8480. if err != nil {
  8481. tx.Rollback()
  8482. return err
  8483. }
  8484. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8485. if err != nil {
  8486. tx.Rollback()
  8487. return err
  8488. }
  8489. var goodList []*models.WarehousingInfo
  8490. 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
  8491. var flush_count int64
  8492. for _, it := range goodList {
  8493. flush_count += it.StockCount
  8494. }
  8495. 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
  8496. if err != nil {
  8497. tx.Rollback()
  8498. return err
  8499. }
  8500. 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
  8501. if err != nil {
  8502. tx.Rollback()
  8503. return err
  8504. }
  8505. storeHouseConfig := models.XtStorehouseConfig{}
  8506. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8507. var stockList []*models.WarehousingInfo
  8508. //查询该耗材剩余多少库存
  8509. 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
  8510. var sum_count int64
  8511. var sum_in_count int64
  8512. for _, it := range stockList {
  8513. sum_count += it.StockCount
  8514. sum_in_count += it.WarehousingCount
  8515. }
  8516. 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
  8517. if err != nil {
  8518. tx.Rollback()
  8519. return err
  8520. }
  8521. 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
  8522. if err != nil {
  8523. tx.Rollback()
  8524. return err
  8525. }
  8526. err = tx.Model(models.XtGoodWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", item.ID).Update(map[string]interface{}{"status": 0}).Error
  8527. if err != nil {
  8528. tx.Rollback()
  8529. return err
  8530. }
  8531. }
  8532. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8533. if err != nil {
  8534. tx.Rollback()
  8535. return err
  8536. }
  8537. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8538. return err
  8539. }
  8540. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8541. if storehouse_id == 0 {
  8542. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  8543. }
  8544. if storehouse_id > 0 {
  8545. 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
  8546. }
  8547. return info, err
  8548. }
  8549. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8550. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  8551. return info, err
  8552. }
  8553. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  8554. 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
  8555. return list, err
  8556. }
  8557. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  8558. 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
  8559. return infor, err
  8560. }
  8561. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  8562. info := models.WarehouseOutInfo{}
  8563. 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
  8564. return info, err
  8565. }
  8566. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  8567. info := models.WarehousingInfo{}
  8568. 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
  8569. return info, err
  8570. }
  8571. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8572. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8573. return list, err
  8574. }
  8575. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8576. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8577. return list, err
  8578. }
  8579. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  8580. templateL := models.HisPrescriptionProjectTemplateL{}
  8581. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  8582. return templateL, err
  8583. }
  8584. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  8585. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", id, orgid, goodid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  8586. if err != nil {
  8587. tx.Rollback()
  8588. return err
  8589. }
  8590. return err
  8591. }
  8592. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  8593. err = XTReadDB().Where("org_id = ? and drug_id = ? and status=1", user_org_id, drug_id).Preload("XtBaseDrug", "org_id = ? and status=1", user_org_id).Order("id desc").Find(&info).Error
  8594. return info, err
  8595. }
  8596. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  8597. err := XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"stock_max_number": last_stock_max_number, "stock_min_number": last_stock_min_number}).Error
  8598. return err
  8599. }
  8600. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  8601. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  8602. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  8603. return drugInfoConfig, err
  8604. }
  8605. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8606. err := XTWriteDB().Create(&infoConfig).Error
  8607. return err
  8608. }
  8609. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8610. err := XTWriteDB().Save(&infoConfig).Error
  8611. return err
  8612. }
  8613. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  8614. drugOutConfig := models.XtSelfDrugOutConfig{}
  8615. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  8616. return drugOutConfig, err
  8617. }
  8618. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8619. err := XTWriteDB().Create(&config).Error
  8620. return err
  8621. }
  8622. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8623. err := XTWriteDB().Save(&config).Error
  8624. return err
  8625. }
  8626. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  8627. err := tx.Create(&flow).Error
  8628. if err != nil {
  8629. tx.Rollback()
  8630. return err
  8631. }
  8632. return err
  8633. }
  8634. func UpdateWarehouseOutInfoByPrice(good_id int64, packing_price float64) error {
  8635. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("good_id =? and status =1 and is_check=1", good_id).Updates(map[string]interface{}{"packing_price": packing_price}).Error
  8636. return err
  8637. }
  8638. func GetWarehosueOutInfoByPrice(good_id int64, org_id int64) (out []*models.WarehouseOutInfo, err error) {
  8639. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and price =0", good_id, org_id).Find(&out).Error
  8640. return out, err
  8641. }
  8642. func UpdateWarehouseOut(good_id int64, packing_price float64) error {
  8643. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8644. return err
  8645. }
  8646. func UpdateStockFlowByList(good_id int64, packing_price float64) error {
  8647. err := XTWriteDB().Model(models.VmStockFlow{}).Where("good_id = ? and user_org_id = 10633 and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8648. return err
  8649. }
  8650. func GetGoodStockInventoryByRecordate(good_id int64, record_date int64, user_org_id int64) (models.XtGoodStockInventoryList, error) {
  8651. goodStockInventoryList := models.XtGoodStockInventoryList{}
  8652. err := XTReadDB().Where("good_id = ? and record_date = ? and user_org_id = ? and status=1", good_id, record_date, user_org_id).Find(&goodStockInventoryList).Error
  8653. return goodStockInventoryList, err
  8654. }
  8655. func CreateGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8656. err := XTWriteDB().Create(&list).Error
  8657. return err
  8658. }
  8659. func SaveGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8660. err := XTWriteDB().Save(&list).Error
  8661. return err
  8662. }
  8663. func GetGoodStockInventoryList(keyword string, limit int64, page int64, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8664. likeKey := "%" + keyword + "%"
  8665. offset := (page - 1) * limit
  8666. db := XTReadDB().Model(&models.XtGoodStockInventoryList{}).Where("status=1")
  8667. if storehouse_id > 0 {
  8668. db = db.Where("storehouse_id = ?", storehouse_id)
  8669. }
  8670. if startTime > 0 {
  8671. db = db.Where("record_date >=?", startTime)
  8672. }
  8673. if endTime > 0 {
  8674. db = db.Where("record_date<=?", endTime)
  8675. }
  8676. if len(keyword) > 0 {
  8677. db = db.Where("good_name like ? or specification_name like ?", likeKey, likeKey)
  8678. }
  8679. if orgId > 0 {
  8680. db = db.Where("user_org_id = ?", orgId)
  8681. }
  8682. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8683. return list, total, err
  8684. }
  8685. func GetDrugStockInventoryList(drug_id int64, record_date int64, user_org_id int64) (models.XtDrugStockInventoryList, error) {
  8686. drugStockInventoryList := models.XtDrugStockInventoryList{}
  8687. err := XTReadDB().Where("drug_id = ? and record_date = ? and user_org_id = ? and status=1", drug_id, record_date, user_org_id).Find(&drugStockInventoryList).Error
  8688. return drugStockInventoryList, err
  8689. }
  8690. func CreatedDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8691. err := XTWriteDB().Create(&list).Error
  8692. return err
  8693. }
  8694. func SaveDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8695. err := XTWriteDB().Save(&list).Error
  8696. return err
  8697. }
  8698. func GetStockInventoryPrintList(storehouse_id int64, keyword string, limit int64, page int64, ids []string, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8699. db := XTReadDB().Model(&list).Where("status=1")
  8700. likeKey := "%" + keyword + "%"
  8701. offset := (page - 1) * limit
  8702. if storehouse_id > 0 {
  8703. db = db.Where("storehouse_id = ?", storehouse_id)
  8704. }
  8705. if len(keyword) > 0 {
  8706. db = db.Where("good_name like ?", likeKey)
  8707. }
  8708. if orgId > 0 {
  8709. db = db.Where("user_org_id = ?", orgId)
  8710. }
  8711. if len(ids) > 0 {
  8712. db = db.Where("id in(?)", ids)
  8713. }
  8714. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8715. return list, total, err
  8716. }
  8717. func GetGoodWarehouseInfoList(startTime int64, endTime int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  8718. db := XTReadDB().Model(&info).Where("status=1 and is_check=1")
  8719. if startTime > 0 {
  8720. db = db.Where("ctime>=?", startTime)
  8721. }
  8722. if endTime > 0 {
  8723. db = db.Where("ctime<=?", endTime)
  8724. }
  8725. if orgId > 0 {
  8726. db = db.Where("org_id = ?", orgId)
  8727. }
  8728. err = db.Find(&info).Error
  8729. return info, err
  8730. }
  8731. func GetGoodWarhouseOutList(startTime int64, endTime int64, orgId int64) (outinfo []*models.WarehouseOutInfo, err error) {
  8732. db := XTReadDB().Model(&outinfo).Where("status=1 and is_check =1")
  8733. if startTime > 0 {
  8734. db = db.Where("ctime>=?", startTime)
  8735. }
  8736. if endTime > 0 {
  8737. db = db.Where("ctime<=?", endTime)
  8738. }
  8739. if orgId > 0 {
  8740. db = db.Where("org_id = ?", orgId)
  8741. }
  8742. err = db.Find(&outinfo).Error
  8743. return outinfo, err
  8744. }
  8745. func GetGoodStockFlowProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8746. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 10")
  8747. if startTime > 0 {
  8748. db = db.Where("ctime>=?", startTime)
  8749. }
  8750. if endTime > 0 {
  8751. db = db.Where("ctime<=?", endTime)
  8752. }
  8753. if orgId > 0 {
  8754. db = db.Where("user_org_id = ?", orgId)
  8755. }
  8756. err = db.Find(&flow).Error
  8757. return flow, err
  8758. }
  8759. func GetGoodStockFlowNoProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8760. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 11")
  8761. if startTime > 0 {
  8762. db = db.Where("ctime>=?", startTime)
  8763. }
  8764. if endTime > 0 {
  8765. db = db.Where("ctime<=?", endTime)
  8766. }
  8767. if orgId > 0 {
  8768. db = db.Where("user_org_id = ?", orgId)
  8769. }
  8770. err = db.Find(&flow).Error
  8771. return flow, err
  8772. }
  8773. func GetCancelStockFlowList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8774. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 7")
  8775. if startTime > 0 {
  8776. db = db.Where("ctime>=?", startTime)
  8777. }
  8778. if endTime > 0 {
  8779. db = db.Where("ctime<=?", endTime)
  8780. }
  8781. if orgId > 0 {
  8782. db = db.Where("user_org_id = ?", orgId)
  8783. }
  8784. err = db.Find(&flow).Error
  8785. return flow, err
  8786. }
  8787. func GetLastMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8788. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 1 or consumable_type = 10)")
  8789. if startTime > 0 {
  8790. db = db.Where("ctime <?", startTime)
  8791. }
  8792. if orgId > 0 {
  8793. db = db.Where("user_org_id = ?", orgId)
  8794. }
  8795. err = db.Find(&flow).Error
  8796. return flow, err
  8797. }
  8798. func GetLastMonthOverCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8799. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 2 or consumable_type=3 or consumable_type=11)")
  8800. if startTime > 0 {
  8801. db = db.Where("ctime <?", startTime)
  8802. }
  8803. if orgId > 0 {
  8804. db = db.Where("user_org_id = ?", orgId)
  8805. }
  8806. err = db.Find(&flow).Error
  8807. return flow, err
  8808. }
  8809. func GetLastCancelMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8810. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 7)")
  8811. if startTime > 0 {
  8812. db = db.Where("ctime <?", startTime)
  8813. }
  8814. if orgId > 0 {
  8815. db = db.Where("user_org_id = ?", orgId)
  8816. }
  8817. err = db.Find(&flow).Error
  8818. return flow, err
  8819. }
  8820. func GetDrugCodeOpen(user_org_id int64) (models.XtDrugCodeOpen, error) {
  8821. drugCodeOpen := models.XtDrugCodeOpen{}
  8822. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  8823. return drugCodeOpen, err
  8824. }
  8825. func GetGoodCodeOpen(user_org_id int64) (models.XtGoodCodeOpen, error) {
  8826. drugCodeOpen := models.XtGoodCodeOpen{}
  8827. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  8828. return drugCodeOpen, err
  8829. }
  8830. func CreateDrugOpen(open models.XtDrugCodeOpen) error {
  8831. err := XTWriteDB().Create(&open).Error
  8832. return err
  8833. }
  8834. func SaveDrugOpen(open models.XtDrugCodeOpen) error {
  8835. err := XTWriteDB().Save(&open).Error
  8836. return err
  8837. }
  8838. func GetHisDrugCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64, is_settle int64, keywords string, is_code int64) (list []*models.NewHisDoctorAdviceInfo, total int64, err error) {
  8839. db := XTReadDB().Model(&models.NewHisDoctorAdviceInfo{}).Preload("NewHisPrescription", "status = 1").Where("his_doctor_advice_info.status=1")
  8840. if is_settle == 2 {
  8841. db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8842. } else if is_settle == 1 {
  8843. db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status <> 2 and his_prescription.status = 1")
  8844. }
  8845. if is_code == 2 {
  8846. //db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8847. db = db.Where("his_doctor_advice_info.drug_code <> ''")
  8848. } else if is_code == 1 {
  8849. db = db.Where("length(his_doctor_advice_info.drug_code) = 0")
  8850. }
  8851. if len(keywords) > 0 {
  8852. keywords = "%" + keywords + "%"
  8853. db = db.Joins("join xt_patients on xt_patients.id = his_doctor_advice_info.patient_id and xt_patients.user_org_id = ? and xt_patients.name like ?", orgId, keywords)
  8854. }
  8855. if orgId > 0 {
  8856. db = db.Where("his_doctor_advice_info.user_org_id=?", orgId)
  8857. }
  8858. if start_time > 0 {
  8859. db = db.Where("his_doctor_advice_info.advice_date >= ?", start_time)
  8860. }
  8861. if end_time > 0 {
  8862. db = db.Where("his_doctor_advice_info.advice_date<=?", end_time)
  8863. }
  8864. offset := (page - 1) * limit
  8865. if is_sale == 1 {
  8866. db = db.Where("his_doctor_advice_info.is_upload = 1")
  8867. }
  8868. if is_sale == 2 {
  8869. db = db.Where("his_doctor_advice_info.is_upload = 2 or his_doctor_advice_info.is_upload =0")
  8870. }
  8871. err = db.Count(&total).Offset(offset).Limit(limit).Order("his_doctor_advice_info.record_date desc").Find(&list).Error
  8872. return list, total, err
  8873. }
  8874. func GetHisGoodCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64, is_settle int64, keywords string, is_code int64) (list []*models.NewHisPrintPrescriptionProject, total int64, err error) {
  8875. db := XTReadDB().Model(&models.NewHisPrintPrescriptionProject{}).Preload("NewHisPrescription", "status = 1").Preload("GoodInfo", "status = 1").Where("his_prescription_project.status=1")
  8876. if is_settle == 2 {
  8877. db = db.Joins("join his_prescription on his_prescription.id = his_prescription_project.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8878. } else if is_settle == 1 {
  8879. db = db.Joins("join his_prescription on his_prescription.id = his_prescription_project.prescription_id and his_prescription.order_status <> 2 and his_prescription.status = 1")
  8880. }
  8881. if is_code == 2 {
  8882. //db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8883. db = db.Where("his_prescription_project.drug_code <> ''")
  8884. } else if is_code == 1 {
  8885. db = db.Where("length(his_prescription_project.drug_code) = 0")
  8886. }
  8887. if len(keywords) > 0 {
  8888. keywords = "%" + keywords + "%"
  8889. db = db.Joins("join xt_patients on xt_patients.id = his_doctor_advice_info.patient_id and xt_patients.user_org_id = ? and xt_patients.name like ?", orgId, keywords)
  8890. }
  8891. if orgId > 0 {
  8892. db = db.Where("his_prescription_project.user_org_id=?", orgId)
  8893. }
  8894. if start_time > 0 {
  8895. db = db.Where("his_prescription_project.record_date >= ?", start_time)
  8896. }
  8897. if end_time > 0 {
  8898. db = db.Where("his_prescription_project.record_date<=?", end_time)
  8899. }
  8900. offset := (page - 1) * limit
  8901. if is_sale == 1 {
  8902. db = db.Where("his_prescription_project.is_upload = 1")
  8903. }
  8904. if is_sale == 2 {
  8905. db = db.Where("his_prescription_project.is_upload = 2 or his_prescription_project.is_upload =0")
  8906. }
  8907. db = db.Where("his_prescription_project.type = 3")
  8908. err = db.Count(&total).Offset(offset).Limit(limit).Order("his_prescription_project.record_date desc").Find(&list).Error
  8909. return list, total, err
  8910. }
  8911. func GetBloodDrugCode(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64) (advice []*models.DoctorAdvice, total int64, err error) {
  8912. db := XTReadDB().Model(&advice).Where("status=1 and advice_type !=1")
  8913. if orgId > 0 {
  8914. db = db.Where("user_org_id = ?", orgId)
  8915. }
  8916. if start_time > 0 {
  8917. db = db.Where("advice_date>=?", start_time)
  8918. }
  8919. if end_time > 0 {
  8920. db = db.Where("advice_date<=?", end_time)
  8921. }
  8922. if is_sale > 0 {
  8923. db = db.Where("is_upload = ?", is_sale)
  8924. }
  8925. offset := (page - 1) * limit
  8926. err = db.Count(&total).Offset(offset).Limit(limit).Order("advice_date desc").Find(&advice).Error
  8927. return advice, total, err
  8928. }
  8929. func GetDoctorAdviceInfoList(page int64, limit int64, orgId int64, start_time int64, end_time int64, is_sale int64) ([]models.HisDoctorAdviceInfo, int64, error) {
  8930. var list []models.HisDoctorAdviceInfo
  8931. var total int64
  8932. // 计算偏移量
  8933. offset := (page - 1) * limit
  8934. // 初始化数据库查询
  8935. db := XTReadDB().Model(&models.HisDoctorAdviceInfo{})
  8936. // 构建 WHERE 子句
  8937. query := db.Where("status=1")
  8938. if orgId > 0 {
  8939. query = query.Where("user_org_id=?", orgId)
  8940. }
  8941. if start_time > 0 {
  8942. query = query.Where("advice_date >= ?", start_time)
  8943. }
  8944. if end_time > 0 {
  8945. query = query.Where("advice_date <= ?", end_time)
  8946. }
  8947. if is_sale == 1 {
  8948. query = query.Where("is_upload = 1")
  8949. }
  8950. if is_sale == 2 {
  8951. query = query.Where("is_upload IN (?, ?)", 2, 0) // 使用 IN 替代 OR,通常性能更好
  8952. }
  8953. // 获取总记录数
  8954. err := query.Count(&total).Error
  8955. if err != nil {
  8956. return nil, 0, err
  8957. }
  8958. // 获取分页后的数据
  8959. err = query.Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8960. if err != nil {
  8961. return nil, 0, err
  8962. }
  8963. return list, total, nil
  8964. }
  8965. func GetDrugWarehouseOutInfoByDrugCode(drug_id int64, patient_id int64, advice_date int64, user_org_id int64) (outinfo []*models.XtDrugWarehouseOutInfo, err error) {
  8966. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time =? and org_id = ? and status =1", drug_id, patient_id, advice_date, user_org_id).Find(&outinfo).Error
  8967. return outinfo, err
  8968. }
  8969. func GetNewGoodWarehouseOutInfo(good_id int64, user_org_id int64, warehouse_out_id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  8970. err = tx.Where("good_id = ? and org_id = ? and warehouse_out_id = ? and status= 1", good_id, user_org_id, warehouse_out_id).Find(&info).Error
  8971. if err != gorm.ErrRecordNotFound {
  8972. if err != nil {
  8973. tx.Rollback()
  8974. return info, err
  8975. }
  8976. }
  8977. return info, err
  8978. }
  8979. func GetInitGoodFlow(user_org_id int64, good_id int64, warehousing_detail_id int64, tx *gorm.DB) (list []*models.VmStockFlow, err error) {
  8980. err = tx.Where("user_org_id = ? and good_id = ? and warehousing_detail_id = ? and status=1", user_org_id, good_id, warehousing_detail_id).Find(&list).Error
  8981. if err != gorm.ErrRecordNotFound {
  8982. if err != nil {
  8983. tx.Rollback()
  8984. return list, err
  8985. }
  8986. }
  8987. return list, err
  8988. }
  8989. func GetNewDrugFlowList(drug_id int64, user_org_id int64, warehousing_detail_id int64, tx *gorm.DB) (flow []*models.DrugFlow, err error) {
  8990. err = tx.Where("drug_id = ? and user_org_id =? and warehousing_detail_id = ? and status=1", drug_id, user_org_id, warehousing_detail_id).Find(&flow).Error
  8991. if err != gorm.ErrRecordNotFound {
  8992. if err != nil {
  8993. tx.Rollback()
  8994. return flow, err
  8995. }
  8996. }
  8997. return flow, err
  8998. }
  8999. func GetNewPatientAdviceList(patient_id int64, start_time int64, end_time int64, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  9000. db := XTReadDB().Model(&advice).Where("status=1 and advice_type =2")
  9001. if patient_id > 0 {
  9002. db = db.Where("patient_id = ?", patient_id)
  9003. }
  9004. if start_time > 0 {
  9005. db = db.Where("advice_date>=?", start_time)
  9006. }
  9007. if end_time > 0 {
  9008. db = db.Where("advice_date<=?", end_time)
  9009. }
  9010. if user_org_id > 0 {
  9011. db = db.Where("user_org_id = ?", user_org_id)
  9012. }
  9013. err = db.Order("drug_id desc").Find(&advice).Error
  9014. return advice, err
  9015. }
  9016. func GetSelfDrugWarehouseInfoByPatientId(patient_id int64, user_org_id int64) (drugInfo []*models.XtSelfDrugWarehouseInfo, err error) {
  9017. err = XTReadDB().Where("patient_id = ? and org_id = ? and status =1 and is_check =1", patient_id, user_org_id).Find(&drugInfo).Error
  9018. return drugInfo, err
  9019. }
  9020. func GetPatientAdviceListDetail(patient_id int64, start_time int64, end_time int64, user_org_id int64, advice_name string) (advice []*models.DoctorAdvice, err error) {
  9021. db := XTReadDB().Model(&advice).Where("status = 1")
  9022. if patient_id > 0 {
  9023. db = db.Where("patient_id = ?", patient_id)
  9024. }
  9025. if start_time > 0 {
  9026. db = db.Where("advice_date>=?", start_time)
  9027. }
  9028. if end_time > 0 {
  9029. db = db.Where("advice_date<=?", end_time)
  9030. }
  9031. if user_org_id > 0 {
  9032. db = db.Where("user_org_id = ?", user_org_id)
  9033. }
  9034. if len(advice_name) > 0 {
  9035. db = db.Where("advice_name =?", advice_name)
  9036. }
  9037. err = db.Order("advice_date asc").Find(&advice).Error
  9038. return advice, err
  9039. }
  9040. func GetGoodWarehosueInfoPrintList(good_id int64, start_time int64, end_time int64, user_org_id int64) (info []*models.PrintWarehousingInfo, err error) {
  9041. db := XTReadDB().Model(&info).Where("status=1")
  9042. if good_id > 0 {
  9043. db = db.Where("good_id = ?", good_id)
  9044. }
  9045. if start_time > 0 {
  9046. db = db.Where("ctime>=?", start_time)
  9047. }
  9048. if end_time > 0 {
  9049. db = db.Where("ctime<=?", end_time)
  9050. }
  9051. if user_org_id > 0 {
  9052. db = db.Where("org_id = ?", user_org_id)
  9053. }
  9054. err = db.Find(&info).Error
  9055. return info, err
  9056. }
  9057. func GetDrugWarehouseInfoPrintList(drug_id int64, user_org_id int64, start_time int64, end_time int64) (drugInfo []*models.PrintDrugWarehouseInfo, err error) {
  9058. db := XTReadDB().Model(&drug_id).Where("status=1")
  9059. if drug_id > 0 {
  9060. db = db.Where("drug_id = ?", drug_id)
  9061. }
  9062. if start_time > 0 {
  9063. db = db.Where("ctime>=?", start_time)
  9064. }
  9065. if end_time > 0 {
  9066. db = db.Where("ctime<=?", end_time)
  9067. }
  9068. if user_org_id > 0 {
  9069. db = db.Where("org_id = ?", user_org_id)
  9070. }
  9071. err = db.Find(&drugInfo).Error
  9072. return drugInfo, err
  9073. }