stock_service.go 453KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126
  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. }).Error
  249. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  255. return err, goodInfo
  256. }
  257. 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) {
  258. offset := (page - 1) * limit
  259. db := readDb.Model(&models.GoodInfo{})
  260. db = db.Where("org_id = ? AND status = 1", orgId)
  261. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  262. if len(keyword) > 0 {
  263. likeKey := "%" + keyword + "%"
  264. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  265. if is_use > 0 {
  266. if is_use == 2 {
  267. db = db.Where("find_in_set('停用',good_status) = 0")
  268. } else {
  269. db = db.Where("find_in_set('停用',good_status) > 0")
  270. }
  271. }
  272. if is_charge > 0 {
  273. if is_charge == 1 {
  274. db = db.Where("find_in_set('收费',good_status) > 0")
  275. } else {
  276. db = db.Where("find_in_set('收费',good_status) = 0")
  277. }
  278. }
  279. if good_kind > 0 {
  280. db = db.Where(" good_kind = ?", good_kind)
  281. }
  282. if is_mark > 0 {
  283. db = db.Where("is_mark = ?", is_mark)
  284. }
  285. if manufacturer > 0 {
  286. db = db.Where("manufacturer = ?", manufacturer)
  287. }
  288. if is_type > 0 {
  289. db = db.Where("good_type_id = ?", is_type)
  290. }
  291. } else {
  292. if is_use > 0 {
  293. if is_use == 2 {
  294. db = db.Where("find_in_set('停用',good_status) = 0")
  295. } else {
  296. db = db.Where("find_in_set('停用',good_status) > 0")
  297. }
  298. }
  299. if is_charge > 0 {
  300. if is_charge == 1 {
  301. db = db.Where("find_in_set('收费',good_status) > 0")
  302. } else {
  303. db = db.Where("find_in_set('收费',good_status) = 0")
  304. }
  305. }
  306. if good_kind > 0 {
  307. db = db.Where(" good_kind = ?", good_kind)
  308. }
  309. if is_mark > 0 {
  310. db = db.Where("is_mark = ?", is_mark)
  311. }
  312. if manufacturer > 0 {
  313. db = db.Where("manufacturer = ?", manufacturer)
  314. }
  315. if is_type > 0 {
  316. db = db.Where("good_type_id = ?", is_type)
  317. }
  318. }
  319. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  320. return
  321. }
  322. func DeleteGoodInfoById(id int64, operateId int64) error {
  323. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  324. return err
  325. }
  326. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  327. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  328. return list, err
  329. }
  330. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  331. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  332. return list, err
  333. }
  334. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  335. goodInfo := &models.GoodInfo{}
  336. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  337. return goodInfo, err
  338. }
  339. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  340. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  341. return goodType, err
  342. }
  343. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  344. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  345. return goodType, err
  346. }
  347. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  348. 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
  349. return goodInfo, err
  350. }
  351. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  352. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  353. return goodInfo, err
  354. }
  355. func FindAllGoodInfoThree(org_id int64) (goodInfo []*models.XtGoodInformationFourty, err error) {
  356. err = XTReadDB().Model(&models.XtGoodInformationFourty{}).Where("org_id = ? AND status = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  357. return goodInfo, err
  358. }
  359. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  360. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  361. return total, err
  362. }
  363. func FindNewAllWarehouseTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  364. err = tx.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  365. if err != gorm.ErrRecordNotFound {
  366. if err != nil {
  367. tx.Rollback()
  368. return total, err
  369. }
  370. }
  371. return total, err
  372. }
  373. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  374. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  375. return total, err
  376. }
  377. func FindNewAllWarehouseTotalOne(org_id int64, tx *gorm.DB) (total int64, err error) {
  378. err = tx.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  379. if err != gorm.ErrRecordNotFound {
  380. if err != nil {
  381. tx.Rollback()
  382. return total, err
  383. }
  384. }
  385. return total, err
  386. }
  387. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  388. err := writeDb.Create(&warehouse).Error
  389. return err
  390. }
  391. func AddNewSigleWarehouse(warehouse *models.Warehousing, tx *gorm.DB) error {
  392. err := writeDb.Create(&warehouse).Error
  393. if err != nil {
  394. tx.Rollback()
  395. return err
  396. }
  397. return err
  398. }
  399. func UpdateWarehouse(id int64, storehouse_id int64) error {
  400. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  401. return err
  402. }
  403. func UpdateNewWarehouse(id int64, storehouse_id int64, tx *gorm.DB) error {
  404. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  405. if err != nil {
  406. tx.Rollback()
  407. return err
  408. }
  409. return err
  410. }
  411. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  412. err := writeDb.Create(&warehouse).Error
  413. return err
  414. }
  415. func AddNewSigleDrugWarehouse(warehouse *models.DrugWarehouse, tx *gorm.DB) error {
  416. err := tx.Create(&warehouse).Error
  417. if err != nil {
  418. tx.Rollback()
  419. return err
  420. }
  421. return err
  422. }
  423. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  424. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  425. return err
  426. }
  427. func UpdateNewSigleDrugWarehouse(id int64, storehosue_id int64, tx *gorm.DB) error {
  428. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  429. if err != nil {
  430. tx.Rollback()
  431. return err
  432. }
  433. return err
  434. }
  435. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  436. err := writeDb.Create(&salesReturn).Error
  437. return err
  438. }
  439. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  440. err := writeDb.Create(&salesReturn).Error
  441. return err
  442. }
  443. func UpDateGoodReturnStatus(ids []string) {
  444. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  445. }
  446. func UpDateCanCelStockStatus(ids []string) {
  447. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  448. }
  449. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  450. if len(warehousingInfo) > 0 {
  451. utx := writeDb.Begin()
  452. if len(warehousingInfo) > 0 {
  453. 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 "
  454. insertParams := make([]string, 0)
  455. insertData := make([]interface{}, 0)
  456. for _, info := range warehousingInfo {
  457. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  458. insertData = append(insertData, info.WarehousingId)
  459. insertData = append(insertData, info.GoodId)
  460. insertData = append(insertData, info.GoodTypeId)
  461. insertData = append(insertData, info.Number)
  462. insertData = append(insertData, info.ProductDate)
  463. insertData = append(insertData, info.ExpiryDate)
  464. insertData = append(insertData, info.WarehousingCount)
  465. insertData = append(insertData, info.Price)
  466. insertData = append(insertData, info.TotalPrice)
  467. insertData = append(insertData, info.Dealer)
  468. insertData = append(insertData, info.Manufacturer)
  469. insertData = append(insertData, info.Remark)
  470. insertData = append(insertData, info.Ctime)
  471. insertData = append(insertData, info.Mtime)
  472. insertData = append(insertData, info.Status)
  473. insertData = append(insertData, info.OrgId)
  474. insertData = append(insertData, info.WarehousingOrder)
  475. insertData = append(insertData, info.Type)
  476. insertData = append(insertData, info.WarehousingCount)
  477. insertData = append(insertData, info.LicenseNumber)
  478. insertData = append(insertData, info.PackingPrice)
  479. insertData = append(insertData, info.StorehouseId)
  480. insertData = append(insertData, info.GoodCode)
  481. }
  482. thisSQL += strings.Join(insertParams, ", ")
  483. err = utx.Exec(thisSQL, insertData...).Error
  484. if err != nil {
  485. utx.Rollback()
  486. return
  487. }
  488. }
  489. utx.Commit()
  490. }
  491. return
  492. }
  493. func CreateNewWarehousingInfo(warehousingInfo []*models.WarehousingInfo, tx *gorm.DB) (err error) {
  494. if len(warehousingInfo) > 0 {
  495. if len(warehousingInfo) > 0 {
  496. 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 "
  497. insertParams := make([]string, 0)
  498. insertData := make([]interface{}, 0)
  499. for _, info := range warehousingInfo {
  500. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  501. insertData = append(insertData, info.WarehousingId)
  502. insertData = append(insertData, info.GoodId)
  503. insertData = append(insertData, info.GoodTypeId)
  504. insertData = append(insertData, info.Number)
  505. insertData = append(insertData, info.ProductDate)
  506. insertData = append(insertData, info.ExpiryDate)
  507. insertData = append(insertData, info.WarehousingCount)
  508. insertData = append(insertData, info.Price)
  509. insertData = append(insertData, info.TotalPrice)
  510. insertData = append(insertData, info.Dealer)
  511. insertData = append(insertData, info.Manufacturer)
  512. insertData = append(insertData, info.Remark)
  513. insertData = append(insertData, info.Ctime)
  514. insertData = append(insertData, info.Mtime)
  515. insertData = append(insertData, info.Status)
  516. insertData = append(insertData, info.OrgId)
  517. insertData = append(insertData, info.WarehousingOrder)
  518. insertData = append(insertData, info.Type)
  519. insertData = append(insertData, info.WarehousingCount)
  520. insertData = append(insertData, info.LicenseNumber)
  521. insertData = append(insertData, info.PackingPrice)
  522. insertData = append(insertData, info.StorehouseId)
  523. insertData = append(insertData, info.GoodCode)
  524. }
  525. thisSQL += strings.Join(insertParams, ", ")
  526. err = tx.Exec(thisSQL, insertData...).Error
  527. if err != nil {
  528. tx.Rollback()
  529. return
  530. }
  531. }
  532. }
  533. return
  534. }
  535. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  536. if len(warehousingInfo) > 0 {
  537. utx := writeDb.Begin()
  538. if len(warehousingInfo) > 0 {
  539. 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 "
  540. insertParams := make([]string, 0)
  541. insertData := make([]interface{}, 0)
  542. for _, info := range warehousingInfo {
  543. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  544. insertData = append(insertData, info.WarehousingId)
  545. insertData = append(insertData, info.DrugId)
  546. insertData = append(insertData, info.Number)
  547. insertData = append(insertData, info.ProductDate)
  548. insertData = append(insertData, info.ExpiryDate)
  549. insertData = append(insertData, info.WarehousingCount)
  550. insertData = append(insertData, info.Price)
  551. insertData = append(insertData, info.TotalPrice)
  552. insertData = append(insertData, info.Dealer)
  553. insertData = append(insertData, info.Manufacturer)
  554. insertData = append(insertData, info.Remark)
  555. insertData = append(insertData, info.Ctime)
  556. insertData = append(insertData, info.Mtime)
  557. insertData = append(insertData, info.Status)
  558. insertData = append(insertData, info.OrgId)
  559. insertData = append(insertData, info.WarehousingOrder)
  560. insertData = append(insertData, info.Type)
  561. insertData = append(insertData, info.RetailPrice)
  562. insertData = append(insertData, info.RetailTotalPrice)
  563. insertData = append(insertData, info.StockMaxNumber)
  564. insertData = append(insertData, info.MaxUnit)
  565. insertData = append(insertData, info.MinUnit)
  566. insertData = append(insertData, info.BatchNumber)
  567. insertData = append(insertData, info.StockMinNumber)
  568. insertData = append(insertData, info.StorehouseId)
  569. }
  570. thisSQL += strings.Join(insertParams, ", ")
  571. err = utx.Exec(thisSQL, insertData...).Error
  572. if err != nil {
  573. utx.Rollback()
  574. return
  575. }
  576. }
  577. utx.Commit()
  578. }
  579. return
  580. }
  581. func CreateNewDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo, tx *gorm.DB) (err error) {
  582. if len(warehousingInfo) > 0 {
  583. if len(warehousingInfo) > 0 {
  584. 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 "
  585. insertParams := make([]string, 0)
  586. insertData := make([]interface{}, 0)
  587. for _, info := range warehousingInfo {
  588. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  589. insertData = append(insertData, info.WarehousingId)
  590. insertData = append(insertData, info.DrugId)
  591. insertData = append(insertData, info.Number)
  592. insertData = append(insertData, info.ProductDate)
  593. insertData = append(insertData, info.ExpiryDate)
  594. insertData = append(insertData, info.WarehousingCount)
  595. insertData = append(insertData, info.Price)
  596. insertData = append(insertData, info.TotalPrice)
  597. insertData = append(insertData, info.Dealer)
  598. insertData = append(insertData, info.Manufacturer)
  599. insertData = append(insertData, info.Remark)
  600. insertData = append(insertData, info.Ctime)
  601. insertData = append(insertData, info.Mtime)
  602. insertData = append(insertData, info.Status)
  603. insertData = append(insertData, info.OrgId)
  604. insertData = append(insertData, info.WarehousingOrder)
  605. insertData = append(insertData, info.Type)
  606. insertData = append(insertData, info.RetailPrice)
  607. insertData = append(insertData, info.RetailTotalPrice)
  608. insertData = append(insertData, info.StockMaxNumber)
  609. insertData = append(insertData, info.MaxUnit)
  610. insertData = append(insertData, info.MinUnit)
  611. insertData = append(insertData, info.BatchNumber)
  612. insertData = append(insertData, info.StockMinNumber)
  613. insertData = append(insertData, info.StorehouseId)
  614. insertData = append(insertData, info.DrugCode)
  615. }
  616. thisSQL += strings.Join(insertParams, ", ")
  617. err = tx.Exec(thisSQL, insertData...).Error
  618. if err != nil {
  619. tx.Rollback()
  620. return
  621. }
  622. }
  623. }
  624. return
  625. }
  626. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  627. if len(warehousingInfo) > 0 {
  628. utx := writeDb.Begin()
  629. if len(warehousingInfo) > 0 {
  630. 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 "
  631. insertParams := make([]string, 0)
  632. insertData := make([]interface{}, 0)
  633. for _, info := range warehousingInfo {
  634. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  635. insertData = append(insertData, info.WarehousingId)
  636. insertData = append(insertData, info.DrugId)
  637. insertData = append(insertData, info.Number)
  638. insertData = append(insertData, info.ProductDate)
  639. insertData = append(insertData, info.ExpiryDate)
  640. insertData = append(insertData, info.WarehousingCount)
  641. insertData = append(insertData, info.Price)
  642. insertData = append(insertData, info.TotalPrice)
  643. insertData = append(insertData, info.Dealer)
  644. insertData = append(insertData, info.Manufacturer)
  645. insertData = append(insertData, info.Remark)
  646. insertData = append(insertData, info.Ctime)
  647. insertData = append(insertData, info.Mtime)
  648. insertData = append(insertData, info.Status)
  649. insertData = append(insertData, info.OrgId)
  650. insertData = append(insertData, info.WarehousingOrder)
  651. insertData = append(insertData, info.Type)
  652. insertData = append(insertData, info.RetailPrice)
  653. insertData = append(insertData, info.RetailTotalPrice)
  654. insertData = append(insertData, info.StockMaxNumber)
  655. insertData = append(insertData, info.MaxUnit)
  656. insertData = append(insertData, info.MinUnit)
  657. insertData = append(insertData, info.BatchNumber)
  658. insertData = append(insertData, info.StockMinNumber)
  659. insertData = append(insertData, info.SupplyWarehouseId)
  660. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  661. insertData = append(insertData, info.StorehouseId)
  662. insertData = append(insertData, info.IsCheck)
  663. }
  664. thisSQL += strings.Join(insertParams, ", ")
  665. err = utx.Exec(thisSQL, insertData...).Error
  666. if err != nil {
  667. utx.Rollback()
  668. return
  669. }
  670. }
  671. utx.Commit()
  672. }
  673. return
  674. }
  675. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  676. var total float64
  677. var min_number int64
  678. for _, item := range warehousingInfo {
  679. //查询刚入库耗材的库存
  680. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  681. //更新药品库的总量
  682. info, _ := FindeDrugInfo(item.DrugId)
  683. min_number = count.StockMaxNumber * info.MinNumber
  684. str := strconv.FormatInt(min_number, 10)
  685. minFloat, _ := strconv.ParseFloat(str, 64)
  686. total = info.Total + minFloat
  687. lib := models.BaseDrugLib{
  688. Total: total,
  689. }
  690. UpdateBaseDrug(&lib, item.ID)
  691. }
  692. return err
  693. }
  694. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  695. info := models.XtDrugWarehouseInfo{}
  696. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  697. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  698. return info, err
  699. }
  700. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  701. lib := models.BaseDrugLib{}
  702. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  703. return lib, err
  704. }
  705. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  706. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  707. return err
  708. }
  709. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  710. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  711. return err
  712. }
  713. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  714. info := models.DrugWarehouseInfo{}
  715. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  716. return info, err
  717. }
  718. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  719. out := models.DrugWarehouseOut{}
  720. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  721. return out, err
  722. }
  723. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  724. if len(drugflow) > 0 {
  725. utx := writeDb.Begin()
  726. if len(drugflow) > 0 {
  727. 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 "
  728. insertParams := make([]string, 0)
  729. insertData := make([]interface{}, 0)
  730. for _, info := range drugflow {
  731. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  732. insertData = append(insertData, info.WarehousingId)
  733. insertData = append(insertData, info.DrugId)
  734. insertData = append(insertData, info.Number)
  735. insertData = append(insertData, info.BatchNumber)
  736. insertData = append(insertData, info.Count)
  737. insertData = append(insertData, info.UserOrgId)
  738. insertData = append(insertData, info.PatientId)
  739. insertData = append(insertData, info.SystemTime)
  740. insertData = append(insertData, info.ConsumableType)
  741. insertData = append(insertData, info.IsSys)
  742. insertData = append(insertData, info.WarehousingOrder)
  743. insertData = append(insertData, info.WarehouseOutId)
  744. insertData = append(insertData, info.WarehouseOutOrderNumber)
  745. insertData = append(insertData, info.IsEdit)
  746. insertData = append(insertData, info.CancelStockId)
  747. insertData = append(insertData, info.CancelOrderNumber)
  748. insertData = append(insertData, info.Manufacturer)
  749. insertData = append(insertData, info.Dealer)
  750. insertData = append(insertData, info.Creator)
  751. insertData = append(insertData, info.UpdateCreator)
  752. insertData = append(insertData, info.Status)
  753. insertData = append(insertData, info.Ctime)
  754. insertData = append(insertData, info.Mtime)
  755. insertData = append(insertData, info.Price)
  756. insertData = append(insertData, info.WarehousingDetailId)
  757. insertData = append(insertData, info.WarehouseOutDetailId)
  758. insertData = append(insertData, info.CancelOutDetailId)
  759. insertData = append(insertData, info.ExpireDate)
  760. insertData = append(insertData, info.ProductDate)
  761. insertData = append(insertData, info.MaxUnit)
  762. insertData = append(insertData, info.MinUnit)
  763. insertData = append(insertData, info.StorehouseId)
  764. }
  765. thisSQL += strings.Join(insertParams, ", ")
  766. err = utx.Exec(thisSQL, insertData...).Error
  767. if err != nil {
  768. utx.Rollback()
  769. return
  770. }
  771. }
  772. utx.Commit()
  773. }
  774. return
  775. }
  776. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  777. info := models.DrugFlow{}
  778. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  779. if err == gorm.ErrRecordNotFound {
  780. return nil, err
  781. }
  782. if err != nil {
  783. return nil, err
  784. }
  785. return &info, nil
  786. }
  787. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  788. info := models.XtDrugWarehouseInfo{}
  789. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  790. return info, err
  791. }
  792. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  793. warehouseInfo := models.XtDrugWarehouseInfo{}
  794. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  795. return err
  796. }
  797. func CreateDrugFlowOne(flow models.DrugFlow) error {
  798. ut := writeDb.Begin()
  799. err := ut.Create(&flow).Error
  800. if err != nil {
  801. ut.Rollback()
  802. return err
  803. }
  804. ut.Commit()
  805. return err
  806. }
  807. func CreateSelfDrugFlowTwo(flow models.XtSelfDrugFlow) error {
  808. ut := writeDb.Begin()
  809. err := ut.Create(&flow).Error
  810. if err != nil {
  811. ut.Rollback()
  812. return err
  813. }
  814. ut.Commit()
  815. return err
  816. }
  817. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  818. err := tx.Create(&flow).Error
  819. if err != nil {
  820. tx.Rollback()
  821. return err
  822. }
  823. return err
  824. }
  825. func CreateDrugOutInfoLog(log models.XtDrugWarehouseOutInfoLog, tx *gorm.DB) error {
  826. err := tx.Create(&log).Error
  827. if err != nil {
  828. tx.Rollback()
  829. return err
  830. }
  831. return err
  832. }
  833. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  834. detail := models.DrugAutomaticReduceDetail{}
  835. 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
  836. 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
  837. return detail, err
  838. }
  839. func UpdateSelfDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  840. detail := models.XtSelfDrugWarehouseOutInfo{}
  841. 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
  842. return detail, err
  843. }
  844. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  845. flow := models.DrugFlow{}
  846. 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
  847. return flow, err
  848. }
  849. func UpdateDrugFlowTensOne(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.XtSelfDrugFlow, error) {
  850. flow := models.XtSelfDrugFlow{}
  851. 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
  852. return flow, err
  853. }
  854. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  855. tx := XTWriteDB().Begin()
  856. err := tx.Create(&flow).Error
  857. if err != nil {
  858. tx.Rollback()
  859. return err
  860. }
  861. tx.Commit()
  862. return err
  863. }
  864. func CreateNewDrugFlowTwo(flow *models.DrugFlow, tx *gorm.DB) error {
  865. err := tx.Create(&flow).Error
  866. if err != nil {
  867. tx.Rollback()
  868. return err
  869. }
  870. return err
  871. }
  872. func CreateInfoLog(log models.XtDrugWarehouseInfoLog, tx *gorm.DB) error {
  873. err := tx.Create(&log).Error
  874. if err != nil {
  875. tx.Rollback()
  876. return err
  877. }
  878. return err
  879. }
  880. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  881. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  882. return info, err
  883. }
  884. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  885. info := models.WarehousingInfo{}
  886. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  887. return info, err
  888. }
  889. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  890. err := XTWriteDB().Create(&info).Error
  891. return err
  892. }
  893. func CreatedNewWarehouseingDetail(info *models.WarehousingInfo, tx *gorm.DB) error {
  894. err := tx.Create(&info).Error
  895. if err != nil {
  896. tx.Rollback()
  897. return err
  898. }
  899. return err
  900. }
  901. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  902. if storehouse_id == 0 {
  903. 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
  904. }
  905. if storehouse_id > 0 {
  906. 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
  907. }
  908. return info, err
  909. }
  910. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  911. if storehouse_id == 0 {
  912. 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
  913. if err != gorm.ErrRecordNotFound {
  914. if err != nil {
  915. tx.Rollback()
  916. return info, err
  917. }
  918. }
  919. }
  920. if storehouse_id > 0 {
  921. 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
  922. if err != gorm.ErrRecordNotFound {
  923. if err != nil {
  924. tx.Rollback()
  925. return info, err
  926. }
  927. }
  928. }
  929. return info, err
  930. }
  931. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  932. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  933. if good_id > 0 {
  934. db = db.Where("x.good_id = ?", good_id)
  935. }
  936. if good_type_id > 0 {
  937. db = db.Where("x.good_type_id = ?", good_type_id)
  938. }
  939. if storehouse_id > 0 {
  940. db = db.Where("x.storehouse_id = ?", storehouse_id)
  941. }
  942. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  943. return info, err
  944. }
  945. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  946. info := models.WarehousingInfo{}
  947. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  948. return info, err
  949. }
  950. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  951. info := models.WarehousingInfo{}
  952. var err error
  953. if storehouse_id == 0 {
  954. 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
  955. }
  956. if storehouse_id > 0 {
  957. 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
  958. }
  959. return info, err
  960. }
  961. func FindNewWarehousingInfoTen(goodId int64, storehouse_id int64, tx *gorm.DB) (models.WarehousingInfo, error) {
  962. info := models.WarehousingInfo{}
  963. var err error
  964. if storehouse_id == 0 {
  965. 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
  966. if err != gorm.ErrRecordNotFound {
  967. if err != nil {
  968. tx.Rollback()
  969. return info, err
  970. }
  971. }
  972. }
  973. if storehouse_id > 0 {
  974. 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
  975. if err != gorm.ErrRecordNotFound {
  976. if err != nil {
  977. tx.Rollback()
  978. return info, err
  979. }
  980. }
  981. }
  982. return info, err
  983. }
  984. //药品先进先出,找出最先入库的批次
  985. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  986. 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
  987. return info, err
  988. }
  989. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  990. 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
  991. return info, err
  992. }
  993. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  994. 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
  995. if err != gorm.ErrRecordNotFound {
  996. if err != nil {
  997. tx.Rollback()
  998. return info, err
  999. }
  1000. }
  1001. return info, err
  1002. }
  1003. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  1004. if is_source == 1 {
  1005. 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
  1006. }
  1007. if is_source == 2 || is_source == 0 {
  1008. 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
  1009. }
  1010. return info, err
  1011. }
  1012. // 出库时先进先出,找出最先入库的批次
  1013. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  1014. 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
  1015. return info, err
  1016. }
  1017. //
  1018. //// 出库时先进先出,找出最先入库的批次
  1019. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  1020. // 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
  1021. // return info, err
  1022. //}
  1023. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  1024. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  1025. return info, err
  1026. }
  1027. 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) {
  1028. offset := (page - 1) * limit
  1029. db := readDb.Model(&models.Warehousing{})
  1030. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  1031. if len(keywords) > 0 {
  1032. likeKey := "%" + keywords + "%"
  1033. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1034. 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")
  1035. }
  1036. if startTime > 0 {
  1037. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  1038. }
  1039. if endTime > 0 {
  1040. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  1041. }
  1042. if storehouse_id > 0 {
  1043. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  1044. }
  1045. if check_type == 0 {
  1046. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  1047. }
  1048. if check_type > 0 {
  1049. db = db.Where("xt_warehouse.is_check = ?", check_type)
  1050. }
  1051. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  1052. return
  1053. }
  1054. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  1055. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  1056. if orgid > 0 {
  1057. db = db.Where("org_id = ?", orgid)
  1058. }
  1059. if len(keyword) > 0 {
  1060. likeKey := "%" + keyword + "%"
  1061. db = db.Where("good_name like ?", likeKey)
  1062. }
  1063. err = db.Find(&goodInfo).Error
  1064. return goodInfo, err
  1065. }
  1066. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  1067. db := XTReadDB().Model(&info).Where("status =1")
  1068. if len(good_id) > 0 {
  1069. db = db.Where("good_id in (?)", good_id)
  1070. }
  1071. if startime > 0 {
  1072. db = db.Where("ctime >=?", startime)
  1073. }
  1074. if endtime > 0 {
  1075. db = db.Where("ctime<=?", endtime)
  1076. }
  1077. if orgid > 0 {
  1078. db = db.Where("org_id = ?", orgid)
  1079. }
  1080. if storehouse_id > 0 {
  1081. db = db.Where("storehouse_id = ?", storehouse_id)
  1082. }
  1083. err = db.Find(&info).Error
  1084. return info, err
  1085. }
  1086. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  1087. db := XTReadDB().Model(&info).Where("status =1")
  1088. if len(good_id) > 0 {
  1089. db = db.Where("good_id in (?)", good_id)
  1090. }
  1091. if startime > 0 {
  1092. db = db.Where("ctime >=?", startime)
  1093. }
  1094. if endtime > 0 {
  1095. db = db.Where("ctime<=?", endtime)
  1096. }
  1097. if orgid > 0 {
  1098. db = db.Where("org_id = ?", orgid)
  1099. }
  1100. if storehouse_id > 0 {
  1101. db = db.Where("storehouse_id = ?", storehouse_id)
  1102. }
  1103. err = db.Find(&info).Error
  1104. return info, err
  1105. }
  1106. type VMDrugWarehouse struct {
  1107. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1108. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1109. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  1110. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1111. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  1112. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1113. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1114. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1115. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1116. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  1117. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1118. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1119. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1120. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  1121. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  1122. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1123. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1124. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1125. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  1126. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  1127. }
  1128. func (VMDrugWarehouse) TableName() string {
  1129. return "xt_drug_warehouse"
  1130. }
  1131. type VMDrugWarehouseInfo struct {
  1132. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1133. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  1134. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1135. Number string `gorm:"column:number" json:"number" form:"number"`
  1136. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1137. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1138. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  1139. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1140. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1141. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1142. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1143. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1144. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1145. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1146. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1147. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1148. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  1149. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1150. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1151. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1152. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1153. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1154. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1155. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1156. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1157. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1158. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  1159. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1160. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1161. }
  1162. func (VMDrugWarehouseInfo) TableName() string {
  1163. return "xt_drug_warehouse_info"
  1164. }
  1165. type Drug struct {
  1166. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1167. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  1168. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  1169. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  1170. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  1171. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  1172. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  1173. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  1174. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  1175. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  1176. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  1177. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  1178. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  1179. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  1180. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1181. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1182. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  1183. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1184. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1185. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  1186. Number string `gorm:"column:number" json:"number" form:"number"`
  1187. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  1188. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  1189. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  1190. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  1191. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  1192. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1193. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  1194. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  1195. Code string `gorm:"column:code" json:"code" form:"code"`
  1196. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  1197. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  1198. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  1199. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  1200. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  1201. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  1202. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  1203. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  1204. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  1205. Label int64 `gorm:"column:label" json:"label" form:"label"`
  1206. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  1207. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  1208. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  1209. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  1210. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  1211. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  1212. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1213. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1214. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1215. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1216. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1217. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1218. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  1219. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1220. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1221. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1222. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1223. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1224. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1225. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1226. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1227. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1228. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1229. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1230. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1231. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1232. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1233. IsZeroFlag int64 `gorm:"column:is_zero_flag" json:"is_zero_flag" form:"is_zero_flag"`
  1234. }
  1235. func (Drug) TableName() string {
  1236. return "xt_base_drug"
  1237. }
  1238. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1239. db := readDb.Model(&VMDrugWarehouse{})
  1240. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1241. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1242. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1243. if len(keywords) > 0 {
  1244. likeKey := "%" + keywords + "%"
  1245. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1246. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1247. 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")
  1248. }
  1249. if startTime > 0 {
  1250. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1251. }
  1252. if endTime > 0 {
  1253. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1254. }
  1255. db = db.Count(&total)
  1256. offset := (page - 1) * limit
  1257. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1258. return
  1259. }
  1260. 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) {
  1261. db := readDb.Model(&VMDrugWarehouse{})
  1262. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1263. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1264. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1265. if len(keywords) > 0 {
  1266. likeKey := "%" + keywords + "%"
  1267. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1268. 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")
  1269. }
  1270. if startTime > 0 {
  1271. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1272. }
  1273. if endTime > 0 {
  1274. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1275. }
  1276. if storehouse_id > 0 {
  1277. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1278. }
  1279. if check_type > 0 {
  1280. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1281. }
  1282. db = db.Count(&total)
  1283. offset := (page - 1) * limit
  1284. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1285. return
  1286. }
  1287. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1288. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1289. return list, err
  1290. }
  1291. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1292. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1293. return list, err
  1294. }
  1295. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1296. 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
  1297. return list, err
  1298. }
  1299. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1300. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1301. return list, err
  1302. }
  1303. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1304. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1305. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1306. fmt.Println(table)
  1307. if len(id) > 0 {
  1308. db = db.Where("x.warehousing_id in(?)", id)
  1309. }
  1310. if org_id > 0 {
  1311. db = db.Where("x.org_id = ?", org_id)
  1312. }
  1313. 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
  1314. return list, err
  1315. }
  1316. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1317. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1318. return warehousing, err
  1319. }
  1320. func FindNewWarehousingById(id int64, tx *gorm.DB) (warehousing models.Warehousing, err error) {
  1321. err = tx.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1322. if err != gorm.ErrRecordNotFound {
  1323. if err != nil {
  1324. tx.Rollback()
  1325. return warehousing, err
  1326. }
  1327. }
  1328. return warehousing, err
  1329. }
  1330. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1331. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1332. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1333. return warehousing, err
  1334. }
  1335. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1336. 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
  1337. return warehousing, err
  1338. }
  1339. func FindNewDrugWarehousingById(id int64, org_id int64, tx *gorm.DB) (warehousing VMDrugWarehouse, err error) {
  1340. 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
  1341. if err != gorm.ErrRecordNotFound {
  1342. if err != nil {
  1343. tx.Rollback()
  1344. return warehousing, err
  1345. }
  1346. }
  1347. return warehousing, err
  1348. }
  1349. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1350. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1351. if len(id) > 0 {
  1352. db = db.Where("x.id in(?)", id)
  1353. }
  1354. if org_id > 0 {
  1355. db = db.Where("x.org_id = ?", org_id)
  1356. }
  1357. 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
  1358. return warehousing, err
  1359. }
  1360. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1361. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1362. // return warehousing, err
  1363. //
  1364. //}
  1365. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1366. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1367. return
  1368. }
  1369. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1370. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1371. return
  1372. }
  1373. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1374. if len(salesReturnInfo) > 0 {
  1375. utx := writeDb.Begin()
  1376. if len(salesReturnInfo) > 0 {
  1377. 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 "
  1378. insertParams := make([]string, 0)
  1379. insertData := make([]interface{}, 0)
  1380. for _, info := range salesReturnInfo {
  1381. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1382. insertData = append(insertData, info.GoodId)
  1383. insertData = append(insertData, info.SalesReturnId)
  1384. insertData = append(insertData, info.GoodTypeId)
  1385. insertData = append(insertData, info.Count)
  1386. insertData = append(insertData, info.Price)
  1387. insertData = append(insertData, info.Total)
  1388. insertData = append(insertData, info.Ctime)
  1389. insertData = append(insertData, info.Status)
  1390. insertData = append(insertData, info.OrgId)
  1391. insertData = append(insertData, info.OrderNumber)
  1392. insertData = append(insertData, info.Type)
  1393. insertData = append(insertData, info.Dealer)
  1394. insertData = append(insertData, info.Manufacturer)
  1395. }
  1396. thisSQL += strings.Join(insertParams, ", ")
  1397. err = utx.Exec(thisSQL, insertData...).Error
  1398. if err != nil {
  1399. utx.Rollback()
  1400. return
  1401. }
  1402. }
  1403. utx.Commit()
  1404. }
  1405. return
  1406. }
  1407. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1408. if len(salesReturnInfo) > 0 {
  1409. utx := writeDb.Begin()
  1410. if len(salesReturnInfo) > 0 {
  1411. 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 "
  1412. insertParams := make([]string, 0)
  1413. insertData := make([]interface{}, 0)
  1414. for _, info := range salesReturnInfo {
  1415. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1416. insertData = append(insertData, info.DrugId)
  1417. insertData = append(insertData, info.SalesReturnId)
  1418. insertData = append(insertData, info.Count)
  1419. insertData = append(insertData, info.Price)
  1420. insertData = append(insertData, info.Total)
  1421. insertData = append(insertData, info.Ctime)
  1422. insertData = append(insertData, info.Status)
  1423. insertData = append(insertData, info.OrgId)
  1424. insertData = append(insertData, info.OrderNumber)
  1425. insertData = append(insertData, info.Type)
  1426. insertData = append(insertData, info.Dealer)
  1427. insertData = append(insertData, info.Manufacturer)
  1428. insertData = append(insertData, info.RetailPrice)
  1429. insertData = append(insertData, info.RetailTotalPrice)
  1430. }
  1431. thisSQL += strings.Join(insertParams, ", ")
  1432. err = utx.Exec(thisSQL, insertData...).Error
  1433. if err != nil {
  1434. utx.Rollback()
  1435. return
  1436. }
  1437. }
  1438. utx.Commit()
  1439. }
  1440. return
  1441. }
  1442. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1443. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1444. return total, err
  1445. }
  1446. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1447. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1448. return total, err
  1449. }
  1450. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1451. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1452. if err != gorm.ErrRecordNotFound {
  1453. if err != nil {
  1454. tx.Rollback()
  1455. return total, err
  1456. }
  1457. }
  1458. return total, err
  1459. }
  1460. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1461. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1462. return total, err
  1463. }
  1464. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1465. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1466. if err != gorm.ErrRecordNotFound {
  1467. if err != nil {
  1468. tx.Rollback()
  1469. return total, err
  1470. }
  1471. }
  1472. return total, err
  1473. }
  1474. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1475. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1476. // return total, err
  1477. //}
  1478. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1479. db := readDb.Model(&models.SalesReturn{})
  1480. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1481. if len(keywords) > 0 {
  1482. likeKey := "%" + keywords + "%"
  1483. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1484. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1485. 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")
  1486. }
  1487. if startTime > 0 {
  1488. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1489. }
  1490. if endTime > 0 {
  1491. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1492. }
  1493. db = db.Count(&total)
  1494. offset := (page - 1) * limit
  1495. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1496. return
  1497. }
  1498. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1499. db := readDb.Model(&models.DrugSalesReturn{})
  1500. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1501. if len(keywords) > 0 {
  1502. likeKey := "%" + keywords + "%"
  1503. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1504. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1505. 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")
  1506. }
  1507. if startTime > 0 {
  1508. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1509. }
  1510. if endTime > 0 {
  1511. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1512. }
  1513. db = db.Count(&total)
  1514. offset := (page - 1) * limit
  1515. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1516. return
  1517. }
  1518. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1519. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1520. return list, err
  1521. }
  1522. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1523. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1524. return list, err
  1525. }
  1526. type DrugCancelStockInfo struct {
  1527. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1528. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1529. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1530. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1531. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1532. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1533. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1534. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1535. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1536. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1537. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1538. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1539. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1540. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1541. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1542. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1543. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1544. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1545. Number string `gorm:"column:number" json:"number" form:"number"`
  1546. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1547. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1548. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1549. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1550. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1551. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1552. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1553. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1554. }
  1555. func (DrugCancelStockInfo) TableName() string {
  1556. return "xt_drug_cancel_stock_info"
  1557. }
  1558. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1559. 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
  1560. return list, err
  1561. }
  1562. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1563. db := readDb.Model(&models.WarehousingInfo{})
  1564. db = db.Where("org_id = ? AND status = 1", orgId)
  1565. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1566. if startTime > 0 {
  1567. db = db.Where("ctime >=?", startTime)
  1568. }
  1569. if endTime > 0 {
  1570. db = db.Where("ctime<= ?", endTime)
  1571. }
  1572. db = db.Count(&total)
  1573. offset := (page - 1) * limit
  1574. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1575. return list, total, err
  1576. }
  1577. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1578. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1579. return total, err
  1580. }
  1581. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1582. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1583. return total, err
  1584. }
  1585. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1586. err = tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1587. if err != gorm.ErrRecordNotFound {
  1588. if err != nil {
  1589. tx.Rollback()
  1590. return
  1591. }
  1592. }
  1593. return total, err
  1594. }
  1595. func AddSigleWarehouseOutOne(warehouseOut *models.WarehouseOut) error {
  1596. err := XTWriteDB().Create(&warehouseOut).Error
  1597. return err
  1598. }
  1599. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1600. err := tx.Create(&warehouseOut).Error
  1601. if err != nil {
  1602. tx.Rollback()
  1603. return err
  1604. }
  1605. return err
  1606. }
  1607. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1608. err := tx.Create(&warehouseOut).Error
  1609. if err != nil {
  1610. tx.Rollback()
  1611. return err
  1612. }
  1613. return err
  1614. }
  1615. func UpdateSingleWarehoseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1616. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1617. if err != nil {
  1618. tx.Rollback()
  1619. return err
  1620. }
  1621. return err
  1622. }
  1623. func GetDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string) (models.DrugWarehouseOut, error) {
  1624. drugWarehouseOut := models.DrugWarehouseOut{}
  1625. 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
  1626. return drugWarehouseOut, err
  1627. }
  1628. func GetNewDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  1629. drugWarehouseOut := models.DrugWarehouseOut{}
  1630. 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
  1631. if err != gorm.ErrRecordNotFound {
  1632. if err != nil {
  1633. tx.Rollback()
  1634. return drugWarehouseOut, err
  1635. }
  1636. }
  1637. return drugWarehouseOut, err
  1638. }
  1639. func GetNewDrugWarehouseOutIsExitOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB, id int64) (models.DrugWarehouseOut, error) {
  1640. drugWarehouseOut := models.DrugWarehouseOut{}
  1641. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND id = ?", is_sys, org_id, id).Find(&drugWarehouseOut).Error
  1642. if err != gorm.ErrRecordNotFound {
  1643. if err != nil {
  1644. tx.Rollback()
  1645. return drugWarehouseOut, err
  1646. }
  1647. }
  1648. return drugWarehouseOut, err
  1649. }
  1650. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1651. err := writeDb.Create(&warehouseOut).Error
  1652. return err
  1653. }
  1654. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1655. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1656. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1657. if err != gorm.ErrRecordNotFound {
  1658. if err != nil {
  1659. tx.Rollback()
  1660. return selfDrugWarehouse, err
  1661. }
  1662. }
  1663. return selfDrugWarehouse, err
  1664. }
  1665. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1666. err := tx.Create(&warehouseOut).Error
  1667. if err != nil {
  1668. tx.Rollback()
  1669. return err
  1670. }
  1671. return err
  1672. }
  1673. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1674. err := tx.Create(&warehouseOut).Error
  1675. if err != nil {
  1676. tx.Rollback()
  1677. return err
  1678. }
  1679. return err
  1680. }
  1681. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1682. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1683. return err
  1684. }
  1685. func UpdateNewSingleDrugWarehouseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1686. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1687. if err != nil {
  1688. tx.Rollback()
  1689. return err
  1690. }
  1691. return err
  1692. }
  1693. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1694. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1695. return err
  1696. }
  1697. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1698. if len(warehouseOutInfo) > 0 {
  1699. utx := writeDb.Begin()
  1700. if len(warehouseOutInfo) > 0 {
  1701. 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 "
  1702. insertParams := make([]string, 0)
  1703. insertData := make([]interface{}, 0)
  1704. for _, info := range warehouseOutInfo {
  1705. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1706. insertData = append(insertData, info.WarehouseOutId)
  1707. insertData = append(insertData, info.GoodId)
  1708. insertData = append(insertData, info.GoodTypeId)
  1709. insertData = append(insertData, info.ProductDate)
  1710. insertData = append(insertData, info.ExpiryDate)
  1711. insertData = append(insertData, info.Count)
  1712. insertData = append(insertData, info.Price)
  1713. insertData = append(insertData, info.TotalPrice)
  1714. insertData = append(insertData, info.Remark)
  1715. insertData = append(insertData, info.Ctime)
  1716. insertData = append(insertData, info.Status)
  1717. insertData = append(insertData, info.OrgId)
  1718. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1719. insertData = append(insertData, info.Type)
  1720. insertData = append(insertData, info.Dealer)
  1721. insertData = append(insertData, info.Manufacturer)
  1722. insertData = append(insertData, info.Number)
  1723. insertData = append(insertData, info.LicenseNumber)
  1724. }
  1725. thisSQL += strings.Join(insertParams, ", ")
  1726. err = utx.Exec(thisSQL, insertData...).Error
  1727. if err != nil {
  1728. utx.Rollback()
  1729. return
  1730. }
  1731. }
  1732. utx.Commit()
  1733. }
  1734. return
  1735. }
  1736. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1737. if len(warehouseOutInfo) > 0 {
  1738. utx := writeDb.Begin()
  1739. if len(warehouseOutInfo) > 0 {
  1740. 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 "
  1741. insertParams := make([]string, 0)
  1742. insertData := make([]interface{}, 0)
  1743. for _, info := range warehouseOutInfo {
  1744. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1745. insertData = append(insertData, info.WarehouseOutId)
  1746. insertData = append(insertData, info.DrugId)
  1747. insertData = append(insertData, info.ProductDate)
  1748. insertData = append(insertData, info.ExpiryDate)
  1749. insertData = append(insertData, info.Count)
  1750. insertData = append(insertData, info.Price)
  1751. insertData = append(insertData, info.TotalPrice)
  1752. insertData = append(insertData, info.Remark)
  1753. insertData = append(insertData, info.Ctime)
  1754. insertData = append(insertData, info.Status)
  1755. insertData = append(insertData, info.OrgId)
  1756. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1757. insertData = append(insertData, info.Type)
  1758. insertData = append(insertData, info.Dealer)
  1759. insertData = append(insertData, info.Manufacturer)
  1760. insertData = append(insertData, info.RetailPrice)
  1761. insertData = append(insertData, info.RetailTotalPrice)
  1762. insertData = append(insertData, info.Number)
  1763. insertData = append(insertData, info.BatchNumber)
  1764. insertData = append(insertData, info.CountUnit)
  1765. }
  1766. thisSQL += strings.Join(insertParams, ", ")
  1767. err = utx.Exec(thisSQL, insertData...).Error
  1768. if err != nil {
  1769. utx.Rollback()
  1770. return
  1771. }
  1772. }
  1773. utx.Commit()
  1774. }
  1775. return
  1776. }
  1777. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1778. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1779. return info, err
  1780. }
  1781. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1782. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1783. return info, err
  1784. }
  1785. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1786. db := readDb.Model(&models.WarehouseOut{})
  1787. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1788. if len(keywords) > 0 {
  1789. likeKey := "%" + keywords + "%"
  1790. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1791. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1792. 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")
  1793. }
  1794. if startTime > 0 {
  1795. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1796. }
  1797. if endTime > 0 {
  1798. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1799. }
  1800. db = db.Count(&total)
  1801. offset := (page - 1) * limit
  1802. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1803. return list, total, err
  1804. }
  1805. 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) {
  1806. db := readDb.Model(&models.WarehouseOut{})
  1807. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1808. if len(keywords) > 0 {
  1809. likeKey := "%" + keywords + "%"
  1810. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1811. 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")
  1812. }
  1813. if startTime > 0 {
  1814. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1815. }
  1816. if endTime > 0 {
  1817. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1818. }
  1819. if storehouse_id > 0 {
  1820. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1821. }
  1822. if way_type == 0 {
  1823. 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")
  1824. }
  1825. if way_type == 1 {
  1826. db = db.Where("xt_warehouse_out.is_sys = 0")
  1827. }
  1828. if way_type == 2 {
  1829. db = db.Where("xt_warehouse_out.is_sys = 1")
  1830. }
  1831. if way_type == 3 {
  1832. db = db.Where("xt_warehouse_out.is_sys = 12")
  1833. }
  1834. if way_type == 4 {
  1835. db = db.Where("xt_warehouse_out.is_sys = 5")
  1836. }
  1837. if check_type > 0 {
  1838. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1839. }
  1840. if check_type == 0 {
  1841. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1842. }
  1843. db = db.Count(&total)
  1844. offset := (page - 1) * limit
  1845. if orgId == 10191 {
  1846. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1847. } else {
  1848. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1849. }
  1850. return list, total, err
  1851. }
  1852. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1853. db := readDb.Model(&models.DrugWarehouseOut{})
  1854. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1855. if len(keywords) > 0 {
  1856. likeKey := "%" + keywords + "%"
  1857. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1858. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1859. 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")
  1860. }
  1861. if startTime > 0 {
  1862. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1863. }
  1864. if endTime > 0 {
  1865. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1866. }
  1867. db = db.Count(&total)
  1868. offset := (page - 1) * limit
  1869. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1870. return list, total, err
  1871. }
  1872. 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) {
  1873. db := readDb.Model(&models.DrugWarehouseOut{})
  1874. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1875. if len(keywords) > 0 {
  1876. likeKey := "%" + keywords + "%"
  1877. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1878. 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")
  1879. }
  1880. if startTime > 0 {
  1881. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1882. }
  1883. if endTime > 0 {
  1884. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1885. }
  1886. if storehouse_id > 0 {
  1887. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1888. }
  1889. if check_type > 0 {
  1890. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1891. }
  1892. if way_type == 0 {
  1893. 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")
  1894. }
  1895. if way_type == 1 {
  1896. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1897. }
  1898. if way_type == 2 {
  1899. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1900. }
  1901. if way_type == 3 {
  1902. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1903. }
  1904. if way_type == 4 {
  1905. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1906. }
  1907. db = db.Count(&total)
  1908. offset := (page - 1) * limit
  1909. if orgId == 10191 {
  1910. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1911. } else {
  1912. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1913. }
  1914. return list, total, err
  1915. }
  1916. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1917. 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
  1918. return list, err
  1919. }
  1920. type DrugWarehouseOutInfo struct {
  1921. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1922. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1923. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1924. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1925. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1926. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1927. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1928. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1929. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1930. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1931. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1932. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1933. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1934. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1935. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1936. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1937. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1938. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1939. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1940. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1941. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1942. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1943. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1944. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1945. Number string `gorm:"column:number" json:"number" form:"number"`
  1946. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1947. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1948. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1949. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1950. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1951. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1952. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1953. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1954. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1955. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1956. }
  1957. func (DrugWarehouseOutInfo) TableName() string {
  1958. return "xt_drug_warehouse_out_info"
  1959. }
  1960. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1961. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1962. return list, err
  1963. }
  1964. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1965. info := &models.WarehousingInfo{}
  1966. type WarehouseInfoCountAmount struct {
  1967. Total int64
  1968. }
  1969. var result WarehouseInfoCountAmount
  1970. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1971. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1972. return result.Total, info
  1973. }
  1974. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1975. type SalesReturnCountAmount struct {
  1976. Total int64
  1977. }
  1978. var result SalesReturnCountAmount
  1979. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1980. return result.Total, err
  1981. }
  1982. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1983. type WarehouseOutInfoCountAmount struct {
  1984. Total int64
  1985. }
  1986. var result WarehouseOutInfoCountAmount
  1987. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1988. return result.Total, err
  1989. }
  1990. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1991. type CancleStockoCountAmount struct {
  1992. Total int64
  1993. }
  1994. var result CancleStockoCountAmount
  1995. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1996. return result.Total, err
  1997. }
  1998. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1999. info := models.WarehousingInfo{}
  2000. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  2001. return info, err
  2002. }
  2003. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  2004. stock := models.CancelStock{}
  2005. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  2006. if err == gorm.ErrRecordNotFound {
  2007. return nil, err
  2008. }
  2009. if err != nil {
  2010. return nil, err
  2011. }
  2012. return &stock, nil
  2013. }
  2014. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  2015. stock := models.CancelStock{}
  2016. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2017. if err == gorm.ErrRecordNotFound {
  2018. return nil, err
  2019. }
  2020. if err != nil {
  2021. return nil, err
  2022. }
  2023. return &stock, nil
  2024. }
  2025. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  2026. stock := models.CancelStock{}
  2027. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2028. if err != gorm.ErrRecordNotFound {
  2029. if err != nil {
  2030. tx.Rollback()
  2031. return &stock, err
  2032. }
  2033. }
  2034. return &stock, err
  2035. }
  2036. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  2037. err := writeDb.Create(&cancelStock).Error
  2038. return err
  2039. }
  2040. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  2041. err := tx.Create(&cancelStock).Error
  2042. if err != nil {
  2043. tx.Rollback()
  2044. return err
  2045. }
  2046. return err
  2047. }
  2048. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  2049. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2050. return err
  2051. }
  2052. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  2053. stock := models.CancelStock{}
  2054. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2055. return stock, err
  2056. }
  2057. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  2058. stock := models.CancelStock{}
  2059. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2060. if err != gorm.ErrRecordNotFound {
  2061. if err != nil {
  2062. tx.Rollback()
  2063. return stock, err
  2064. }
  2065. }
  2066. return stock, err
  2067. }
  2068. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  2069. stock := models.DrugCancelStock{}
  2070. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2071. return stock, err
  2072. }
  2073. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  2074. stock := models.DrugCancelStock{}
  2075. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2076. if err != gorm.ErrRecordNotFound {
  2077. if err != nil {
  2078. tx.Rollback()
  2079. return stock, err
  2080. }
  2081. }
  2082. return stock, err
  2083. }
  2084. func GetManufactureById(id int64) (models.Manufacturer, error) {
  2085. manufacturer := models.Manufacturer{}
  2086. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2087. return manufacturer, err
  2088. }
  2089. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  2090. manufacturer := models.Manufacturer{}
  2091. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2092. if err != gorm.ErrRecordNotFound {
  2093. if err != nil {
  2094. tx.Rollback()
  2095. return manufacturer, err
  2096. }
  2097. }
  2098. return manufacturer, err
  2099. }
  2100. func GetDealerById(id int64) (models.Dealer, error) {
  2101. dealer := models.Dealer{}
  2102. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2103. return dealer, err
  2104. }
  2105. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  2106. dealer := models.Dealer{}
  2107. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2108. if err != gorm.ErrRecordNotFound {
  2109. if err != nil {
  2110. tx.Rollback()
  2111. return dealer, err
  2112. }
  2113. }
  2114. return dealer, err
  2115. }
  2116. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  2117. err := writeDb.Create(&cancelStock).Error
  2118. return err
  2119. }
  2120. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  2121. err := tx.Create(&cancelStock).Error
  2122. if err != nil {
  2123. tx.Rollback()
  2124. return err
  2125. }
  2126. return err
  2127. }
  2128. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  2129. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2130. return err
  2131. }
  2132. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  2133. // err := tx.Create(&cancelStock).Error
  2134. // return err
  2135. //}
  2136. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  2137. if len(cancelStockInfo) > 0 {
  2138. utx := writeDb.Begin()
  2139. if len(cancelStockInfo) > 0 {
  2140. 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 "
  2141. insertParams := make([]string, 0)
  2142. insertData := make([]interface{}, 0)
  2143. for _, info := range cancelStockInfo {
  2144. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2145. insertData = append(insertData, info.GoodId)
  2146. insertData = append(insertData, info.CancelStockId)
  2147. insertData = append(insertData, info.GoodTypeId)
  2148. insertData = append(insertData, info.Count)
  2149. insertData = append(insertData, info.Price)
  2150. insertData = append(insertData, info.Total)
  2151. insertData = append(insertData, info.Ctime)
  2152. insertData = append(insertData, info.Status)
  2153. insertData = append(insertData, info.OrgId)
  2154. insertData = append(insertData, info.OrderNumber)
  2155. insertData = append(insertData, info.Type)
  2156. insertData = append(insertData, info.Dealer)
  2157. insertData = append(insertData, info.Manufacturer)
  2158. insertData = append(insertData, info.Number)
  2159. insertData = append(insertData, info.RegisterAccount)
  2160. insertData = append(insertData, info.Remark)
  2161. insertData = append(insertData, info.ProductDate)
  2162. insertData = append(insertData, info.ExpiryDate)
  2163. insertData = append(insertData, info.WarehouseInfoId)
  2164. insertData = append(insertData, info.WarehouseInfoId)
  2165. }
  2166. thisSQL += strings.Join(insertParams, ", ")
  2167. err = utx.Exec(thisSQL, insertData...).Error
  2168. if err != nil {
  2169. utx.Rollback()
  2170. return
  2171. }
  2172. }
  2173. utx.Commit()
  2174. }
  2175. return
  2176. }
  2177. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  2178. err := XTWriteDB().Create(&info).Error
  2179. return err
  2180. }
  2181. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  2182. // err := tx.Create(&info).Error
  2183. // return err
  2184. //}
  2185. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  2186. info := models.DrugCancelStockInfo{}
  2187. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2188. return info, err
  2189. }
  2190. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  2191. // info := models.DrugCancelStockInfo{}
  2192. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2193. // return info, err
  2194. //}
  2195. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  2196. err := XTWriteDB().Create(&info).Error
  2197. return err
  2198. }
  2199. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  2200. err := XTWriteDB().Create(&info).Error
  2201. if err != nil {
  2202. tx.Rollback()
  2203. return err
  2204. }
  2205. return err
  2206. }
  2207. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  2208. info := models.CancelStockInfo{}
  2209. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2210. return info, err
  2211. }
  2212. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  2213. info := models.CancelStockInfo{}
  2214. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2215. if err != gorm.ErrRecordNotFound {
  2216. if err != nil {
  2217. tx.Rollback()
  2218. return info, err
  2219. }
  2220. }
  2221. return info, err
  2222. }
  2223. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  2224. info := models.DrugCancelStockInfo{}
  2225. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  2226. return info, err
  2227. }
  2228. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  2229. if len(cancelStockInfo) > 0 {
  2230. utx := writeDb.Begin()
  2231. if len(cancelStockInfo) > 0 {
  2232. 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 "
  2233. insertParams := make([]string, 0)
  2234. insertData := make([]interface{}, 0)
  2235. for _, info := range cancelStockInfo {
  2236. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2237. insertData = append(insertData, info.DrugId)
  2238. insertData = append(insertData, info.CancelStockId)
  2239. insertData = append(insertData, info.Count)
  2240. insertData = append(insertData, info.Price)
  2241. insertData = append(insertData, info.Total)
  2242. insertData = append(insertData, info.Ctime)
  2243. insertData = append(insertData, info.Status)
  2244. insertData = append(insertData, info.OrgId)
  2245. insertData = append(insertData, info.OrderNumber)
  2246. insertData = append(insertData, info.Type)
  2247. insertData = append(insertData, info.Dealer)
  2248. insertData = append(insertData, info.Manufacturer)
  2249. insertData = append(insertData, info.RetailPrice)
  2250. insertData = append(insertData, info.RetailTotalPrice)
  2251. insertData = append(insertData, info.RegisterAccount)
  2252. insertData = append(insertData, info.BatchNumber)
  2253. insertData = append(insertData, info.Remark)
  2254. insertData = append(insertData, info.MaxUnit)
  2255. insertData = append(insertData, info.ProductDate)
  2256. insertData = append(insertData, info.ExpiryDate)
  2257. insertData = append(insertData, info.BatchNumberId)
  2258. }
  2259. thisSQL += strings.Join(insertParams, ", ")
  2260. err = utx.Exec(thisSQL, insertData...).Error
  2261. if err != nil {
  2262. utx.Rollback()
  2263. return
  2264. }
  2265. }
  2266. utx.Commit()
  2267. }
  2268. return
  2269. }
  2270. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  2271. db := readDb.Model(&models.CancelStock{})
  2272. db = db.Where("org_id = ? AND status = 1", orgId)
  2273. err = db.Order("ctime desc").Find(&list).Error
  2274. return
  2275. }
  2276. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2277. db := readDb.Model(&models.WarehousingInfo{})
  2278. db = db.Where("org_id = ? AND status = 1", orgId)
  2279. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2280. if startTime > 0 {
  2281. db = db.Where("ctime >=?", startTime)
  2282. }
  2283. if endTime > 0 {
  2284. db = db.Where("ctime<= ?", endTime)
  2285. }
  2286. db = db.Count(&total)
  2287. offset := (page - 1) * limit
  2288. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2289. return
  2290. }
  2291. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2292. db := XTReadDB().Model(&models.StockInfo{})
  2293. if startime > 0 {
  2294. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2295. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2296. })
  2297. }
  2298. if startime == 0 || endtime == 0 {
  2299. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2300. return db.Where("org_id = ? AND status = 1", orgId)
  2301. })
  2302. }
  2303. if endtime > 0 {
  2304. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2305. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2306. })
  2307. }
  2308. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2309. db = db.Group("xt_good_information.id")
  2310. if startime == 0 || endtime == 0 {
  2311. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2312. return db.Where("org_id = ? AND status = 1", orgId)
  2313. })
  2314. }
  2315. if startime > 0 {
  2316. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2317. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2318. })
  2319. }
  2320. if endtime > 0 {
  2321. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2322. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2323. })
  2324. }
  2325. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2326. if startime == 0 || endtime == 0 {
  2327. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2328. }
  2329. if startime > 0 {
  2330. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2331. }
  2332. if endtime > 0 {
  2333. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2334. }
  2335. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2336. if len(keyword) > 0 {
  2337. likeKey := "%" + keyword + "%"
  2338. 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)
  2339. 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")
  2340. }
  2341. if type_name > 0 {
  2342. 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)
  2343. }
  2344. db = db.Count(&total)
  2345. offset := (page - 1) * limit
  2346. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2347. return
  2348. }
  2349. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2350. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2351. return
  2352. }
  2353. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2354. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2355. return
  2356. }
  2357. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2358. 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
  2359. return
  2360. }
  2361. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2362. 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
  2363. return
  2364. }
  2365. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2366. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2367. "good_id": warehouseInfo.GoodId,
  2368. "number": warehouseInfo.Number,
  2369. "product_date": warehouseInfo.ProductDate,
  2370. "expiry_date": warehouseInfo.ExpiryDate,
  2371. "warehousing_count": warehouseInfo.WarehousingCount,
  2372. "price": warehouseInfo.Price,
  2373. "total_price": warehouseInfo.TotalPrice,
  2374. "mtime": warehouseInfo.Mtime,
  2375. "remark": warehouseInfo.Remark,
  2376. }).Error
  2377. return
  2378. }
  2379. func UpDateWarehouseStatus(id int64) (err error) {
  2380. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2381. return
  2382. }
  2383. func UpdatedStockFlowById(id int64) (err error) {
  2384. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2385. return
  2386. }
  2387. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2388. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2389. return
  2390. }
  2391. func UpdateDrugFlowById(id int64) (err error) {
  2392. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2393. return
  2394. }
  2395. func DeleteWarehouse(ids []string) (err error) {
  2396. ut := writeDb.Begin()
  2397. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2398. if err != nil {
  2399. ut.Rollback()
  2400. return
  2401. }
  2402. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2403. if err != nil {
  2404. ut.Rollback()
  2405. return
  2406. }
  2407. ut.Commit()
  2408. return
  2409. }
  2410. func DeleteStockFlow(ids []string) (err error) {
  2411. ut := writeDb.Begin()
  2412. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2413. if err != nil {
  2414. ut.Rollback()
  2415. return
  2416. }
  2417. ut.Commit()
  2418. return
  2419. }
  2420. func DeleteDrugWarehouse(ids []string) (err error) {
  2421. ut := writeDb.Begin()
  2422. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2423. if err != nil {
  2424. ut.Rollback()
  2425. return
  2426. }
  2427. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2428. if err != nil {
  2429. ut.Rollback()
  2430. return
  2431. }
  2432. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2433. if err != nil {
  2434. ut.Rollback()
  2435. return
  2436. }
  2437. ut.Commit()
  2438. return
  2439. }
  2440. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2441. warehouse := &models.Warehousing{}
  2442. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2443. return warehouse, err
  2444. }
  2445. func FindWareHouseOutByIdOne(id int64) (*models.WarehouseOut, error) {
  2446. warehouseOut := &models.WarehouseOut{}
  2447. err := XTReadDB().Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2448. return warehouseOut, err
  2449. }
  2450. func FindWareHouseOutById(id int64, tx *gorm.DB) (*models.WarehouseOut, error) {
  2451. warehouseOut := &models.WarehouseOut{}
  2452. err := tx.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2453. if err != gorm.ErrRecordNotFound {
  2454. if err != nil {
  2455. tx.Rollback()
  2456. return warehouseOut, err
  2457. }
  2458. }
  2459. return warehouseOut, err
  2460. }
  2461. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2462. warehouseOut := &models.DrugWarehouseOut{}
  2463. 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
  2464. return warehouseOut, err
  2465. }
  2466. func FindNewDrugWareHouseOutById(id int64, org_id int64, tx *gorm.DB) (*models.DrugWarehouseOut, error) {
  2467. warehouseOut := &models.DrugWarehouseOut{}
  2468. 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
  2469. if err != nil {
  2470. tx.Rollback()
  2471. return warehouseOut, err
  2472. }
  2473. return warehouseOut, err
  2474. }
  2475. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2476. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2477. return
  2478. }
  2479. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2480. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2481. return
  2482. }
  2483. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2484. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2485. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2486. return
  2487. }
  2488. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2489. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2490. return
  2491. }
  2492. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2493. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2494. if manufacturer_id > 0 {
  2495. db = db.Where("manufacturer = ?", manufacturer_id)
  2496. }
  2497. if dealer_id > 0 {
  2498. db = db.Where("dealer = ?", dealer_id)
  2499. }
  2500. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2501. return goodInfo, err
  2502. }
  2503. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2504. db := readDb.Model(&models.Warehousing{})
  2505. likekey := "%" + keywords + "%"
  2506. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2507. db = db.Where("warehousing_order LIKE ? ", likekey)
  2508. db = db.Count(&total)
  2509. offset := (page - 1) * limit
  2510. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2511. return
  2512. }
  2513. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2514. info := models.WarehousingInfo{}
  2515. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2516. return info, err
  2517. }
  2518. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2519. info := models.WarehousingInfo{}
  2520. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2521. return info, err
  2522. }
  2523. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2524. err = writeDb.Save(&info).Error
  2525. return err
  2526. }
  2527. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2528. err = writeDb.Save(&info).Error
  2529. return err
  2530. }
  2531. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2532. 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
  2533. return err
  2534. }
  2535. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2536. 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
  2537. return err
  2538. }
  2539. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2540. info := models.DrugWarehouseInfo{}
  2541. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2542. return info, err
  2543. }
  2544. func EditWarehousing(warehouse models.Warehousing) {
  2545. 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
  2546. }
  2547. func EditNewWarehousing(warehouse models.Warehousing, tx *gorm.DB) {
  2548. 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
  2549. if err != nil {
  2550. tx.Rollback()
  2551. return
  2552. }
  2553. return
  2554. }
  2555. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2556. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2557. 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)
  2558. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2559. return list, err
  2560. }
  2561. func DeleteSalesReturn(ids []string) (err error) {
  2562. ut := writeDb.Begin()
  2563. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2564. if err != nil {
  2565. ut.Rollback()
  2566. return
  2567. }
  2568. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2569. if err != nil {
  2570. ut.Rollback()
  2571. return
  2572. }
  2573. ut.Commit()
  2574. return
  2575. }
  2576. func DeleteDrugSalesReturn(ids []string) (err error) {
  2577. ut := writeDb.Begin()
  2578. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2579. if err != nil {
  2580. ut.Rollback()
  2581. return
  2582. }
  2583. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2584. if err != nil {
  2585. ut.Rollback()
  2586. return
  2587. }
  2588. ut.Commit()
  2589. return
  2590. }
  2591. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2592. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2593. return
  2594. }
  2595. type DrugSalesReturnInfo struct {
  2596. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2597. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2598. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2599. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2600. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2601. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2602. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2603. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2604. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2605. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2606. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2607. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2608. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2609. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2610. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2611. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2612. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2613. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2614. Number string `gorm:"column:number" json:"number" form:"number"`
  2615. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2616. }
  2617. func (DrugSalesReturnInfo) TableName() string {
  2618. return "xt_drug_sales_return_info"
  2619. }
  2620. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2621. 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
  2622. return
  2623. }
  2624. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2625. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2626. return
  2627. }
  2628. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2629. 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
  2630. return
  2631. }
  2632. func UpDateSaleReturnStatus(id int64) (err error) {
  2633. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2634. return
  2635. }
  2636. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2637. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2638. return
  2639. }
  2640. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2641. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2642. return salesReturn, err
  2643. }
  2644. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2645. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2646. return salesReturn, err
  2647. }
  2648. func EditSaleReturn(sales models.SalesReturn) {
  2649. 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
  2650. }
  2651. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2652. 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
  2653. }
  2654. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2655. err = writeDb.Save(&info).Error
  2656. return err
  2657. }
  2658. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2659. err = writeDb.Save(&info).Error
  2660. return err
  2661. }
  2662. func DeleteWarehouseOut(ids []string) (err error) {
  2663. ut := writeDb.Begin()
  2664. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2665. if err != nil {
  2666. ut.Rollback()
  2667. return
  2668. }
  2669. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2670. if err != nil {
  2671. ut.Rollback()
  2672. return
  2673. }
  2674. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2675. if err != nil {
  2676. ut.Rollback()
  2677. return
  2678. }
  2679. ut.Commit()
  2680. return
  2681. }
  2682. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2683. ut := writeDb.Begin()
  2684. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2685. if err != nil {
  2686. ut.Rollback()
  2687. return
  2688. }
  2689. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2690. if err != nil {
  2691. ut.Rollback()
  2692. return
  2693. }
  2694. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2695. if err != nil {
  2696. ut.Rollback()
  2697. return
  2698. }
  2699. ut.Commit()
  2700. return
  2701. }
  2702. func UpDateWarehouseOutStatus(id int64) (err error) {
  2703. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2704. return
  2705. }
  2706. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2707. ut := writeDb.Begin()
  2708. err = ut.Save(&info).Error
  2709. if err != nil {
  2710. ut.Rollback()
  2711. return err
  2712. }
  2713. ut.Commit()
  2714. return err
  2715. }
  2716. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2717. err = tx.Save(&info).Error
  2718. if err != nil {
  2719. tx.Rollback()
  2720. return err
  2721. }
  2722. return err
  2723. }
  2724. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2725. ut := writeDb.Begin()
  2726. err = ut.Save(&info).Error
  2727. if err != nil {
  2728. ut.Rollback()
  2729. return err
  2730. }
  2731. ut.Commit()
  2732. return err
  2733. }
  2734. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2735. err = tx.Save(&info).Error
  2736. if err != nil {
  2737. tx.Rollback()
  2738. return err
  2739. }
  2740. return err
  2741. }
  2742. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2743. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2744. return
  2745. }
  2746. func EditWarehouseOut(warehouseOut models.WarehouseOut, tx *gorm.DB) {
  2747. 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
  2748. if err != nil {
  2749. tx.Rollback()
  2750. return
  2751. }
  2752. return
  2753. }
  2754. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2755. 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
  2756. }
  2757. func EditNewDrugWarehouseOut(warehouseOut models.DrugWarehouseOut, tx *gorm.DB) {
  2758. 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
  2759. if err != nil {
  2760. tx.Rollback()
  2761. return
  2762. }
  2763. return
  2764. }
  2765. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) (err error) {
  2766. err = tx.Save(&info).Error
  2767. if err != nil {
  2768. tx.Rollback()
  2769. return err
  2770. }
  2771. return err
  2772. }
  2773. 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) {
  2774. db := readDb.Model(&models.CancelStock{})
  2775. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2776. if len(keywords) > 0 {
  2777. likeKey := "%" + keywords + "%"
  2778. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2779. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2780. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2781. }
  2782. if startTime > 0 {
  2783. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2784. }
  2785. if endTime > 0 {
  2786. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2787. }
  2788. if storehouse_id > 0 {
  2789. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2790. }
  2791. if check_type > 0 {
  2792. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2793. }
  2794. db = db.Count(&total)
  2795. offset := (page - 1) * limit
  2796. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2797. return
  2798. }
  2799. 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) {
  2800. db := readDb.Model(&models.DrugCancelStock{})
  2801. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2802. if len(keywords) > 0 {
  2803. likeKey := "%" + keywords + "%"
  2804. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2805. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2806. 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")
  2807. }
  2808. if startTime > 0 {
  2809. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2810. }
  2811. if endTime > 0 {
  2812. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2813. }
  2814. if storehoue_id > 0 {
  2815. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2816. }
  2817. if check_type > 0 {
  2818. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2819. }
  2820. db = db.Count(&total)
  2821. offset := (page - 1) * limit
  2822. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2823. return
  2824. }
  2825. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2826. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2827. return info, err
  2828. }
  2829. func DeleteCancelStock(ids []string) (err error) {
  2830. ut := writeDb.Begin()
  2831. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2832. if err != nil {
  2833. ut.Rollback()
  2834. return
  2835. }
  2836. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2837. if err != nil {
  2838. ut.Rollback()
  2839. return
  2840. }
  2841. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2842. if err != nil {
  2843. ut.Rollback()
  2844. return
  2845. }
  2846. ut.Commit()
  2847. return
  2848. }
  2849. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2850. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2851. return info, err
  2852. }
  2853. func DeleteDrugCancelStock(ids []string) (err error) {
  2854. ut := writeDb.Begin()
  2855. err = ut.Model(&models.DrugCancelStock{}).Where("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. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2861. if err != nil {
  2862. ut.Rollback()
  2863. return
  2864. }
  2865. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2866. if err != nil {
  2867. ut.Rollback()
  2868. return
  2869. }
  2870. ut.Commit()
  2871. return
  2872. }
  2873. func UpDateCancleStockStatus(id int64) (err error) {
  2874. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2875. return
  2876. }
  2877. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2878. info := models.CancelStockInfo{}
  2879. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2880. return info, err
  2881. }
  2882. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2883. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2884. return err
  2885. }
  2886. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2887. 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
  2888. return
  2889. }
  2890. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2891. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2892. return
  2893. }
  2894. func UpDateDrugCancelStockById(id int64) (err error) {
  2895. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2896. return
  2897. }
  2898. func EditCancelStock(sales models.CancelStock) {
  2899. 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
  2900. }
  2901. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2902. 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
  2903. }
  2904. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2905. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2906. return cancelStock, err
  2907. }
  2908. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2909. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2910. return cancelStock, err
  2911. }
  2912. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2913. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2914. return cancelStock, err
  2915. }
  2916. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2917. err = writeDb.Save(&info).Error
  2918. return err
  2919. }
  2920. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2921. err = writeDb.Save(&info).Error
  2922. return err
  2923. }
  2924. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2925. 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
  2926. return list, err
  2927. }
  2928. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2929. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2930. 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)
  2931. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2932. return list, err
  2933. }
  2934. type StockDetailModel struct {
  2935. GoodId int64 `gorm:"good_id" json:"good_id"`
  2936. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2937. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2938. Price float64 `gorm:"price" json:"price"`
  2939. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2940. Type int `gorm:"type" json:"type"`
  2941. Creater int64 `gorm:"creater" json:"creater"`
  2942. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2943. Types int64 `gorm:"types" json:"types"`
  2944. }
  2945. 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) {
  2946. db := readDb.Model(&models.WarehousingInfo{})
  2947. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2948. if good_id > 0 {
  2949. db = db.Where("xt_warehouse_info.good_id = ?", good_id)
  2950. }
  2951. if len(keywords) > 0 {
  2952. likeKey := "%" + keywords + "%"
  2953. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2954. 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)
  2955. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2956. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2957. 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")
  2958. } else {
  2959. 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)
  2960. }
  2961. if manufacturer > 0 {
  2962. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2963. }
  2964. if storehouse_id > 0 {
  2965. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2966. }
  2967. if order_type > 0 {
  2968. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2969. }
  2970. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2971. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2972. db = db.Count(&total)
  2973. offset := (page - 1) * limit
  2974. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2975. return
  2976. }
  2977. 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) {
  2978. db := readDb.Model(&models.WarehousingInfo{})
  2979. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2980. if good_id > 0 {
  2981. db = db.Where("good_id = ?", good_id)
  2982. }
  2983. if len(keywords) > 0 {
  2984. likeKey := "%" + keywords + "%"
  2985. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2986. 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)
  2987. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2988. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2989. 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")
  2990. } else {
  2991. 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)
  2992. }
  2993. if manufacturer > 0 {
  2994. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2995. }
  2996. if order_type > 0 {
  2997. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2998. }
  2999. if storehouse_id > 0 {
  3000. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  3001. }
  3002. type PriceAmount struct {
  3003. Total float64
  3004. }
  3005. var result []PriceAmount
  3006. err = db.Select("total_price as total").Scan(&result).Error
  3007. var total float64
  3008. for _, value := range result {
  3009. total = total + value.Total
  3010. }
  3011. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3012. return err, total
  3013. }
  3014. func FloatRound(f float64, n int) float64 {
  3015. format := "%." + strconv.Itoa(n) + "f"
  3016. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  3017. return res
  3018. }
  3019. 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) {
  3020. if orgId == 9671 {
  3021. db := readDb.Model(&models.WarehouseOutInfo{})
  3022. 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)
  3023. if len(keywords) > 0 {
  3024. likeKey := "%" + keywords + "%"
  3025. 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)
  3026. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3027. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3028. 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")
  3029. } else {
  3030. 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)
  3031. }
  3032. if manufacturer > 0 {
  3033. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3034. }
  3035. if order_type > 0 {
  3036. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3037. }
  3038. if storehouse_id > 0 {
  3039. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3040. }
  3041. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  3042. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  3043. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3044. db = db.Count(&total)
  3045. offset := (page - 1) * limit
  3046. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3047. return
  3048. } else {
  3049. db := readDb.Model(&models.WarehouseOutInfo{})
  3050. 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)
  3051. if good_id > 0 {
  3052. db = db.Where("good_id = ?", good_id)
  3053. }
  3054. if order_way == 1 {
  3055. db = db.Where("xt_warehouse_out_info.is_sys =0")
  3056. }
  3057. if order_way == 2 {
  3058. db = db.Where("xt_warehouse_out_info.is_sys !=0")
  3059. }
  3060. if len(keywords) > 0 {
  3061. likeKey := "%" + keywords + "%"
  3062. 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)
  3063. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3064. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3065. 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")
  3066. } else {
  3067. 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)
  3068. }
  3069. if manufacturer > 0 {
  3070. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3071. }
  3072. if order_type > 0 {
  3073. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3074. }
  3075. if storehouse_id > 0 {
  3076. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3077. }
  3078. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ? and is_check =1", orgId)
  3079. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ? and is_check =1", orgId)
  3080. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3081. db = db.Count(&total)
  3082. offset := (page - 1) * limit
  3083. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3084. return
  3085. }
  3086. }
  3087. 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) {
  3088. db := readDb.Model(&models.WarehouseOutInfo{})
  3089. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.count > 0", orgId)
  3090. if len(keywords) > 0 {
  3091. likeKey := "%" + keywords + "%"
  3092. 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)
  3093. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3094. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3095. 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")
  3096. } else {
  3097. 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)
  3098. }
  3099. if good_id > 0 {
  3100. db = db.Where("xt_warehouse_out_info.good_id = ?", good_id)
  3101. }
  3102. if order_way == 1 {
  3103. db = db.Where("xt_warehouse_out_info.is_sys = ?", 0)
  3104. }
  3105. if order_way == 2 {
  3106. db = db.Where("xt_warehouse_out_info.is_sys != ?", 0)
  3107. }
  3108. if manufacturer > 0 {
  3109. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3110. }
  3111. if order_type > 0 {
  3112. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3113. }
  3114. if storehouse_id > 0 {
  3115. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3116. }
  3117. type PriceAmount struct {
  3118. Total float64
  3119. }
  3120. var result []PriceAmount
  3121. err = db.Select("(count * price) as total").Scan(&result).Error
  3122. var total float64
  3123. for _, value := range result {
  3124. total = total + value.Total
  3125. }
  3126. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3127. fmt.Println(total)
  3128. return err, total
  3129. }
  3130. 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) {
  3131. db := readDb.Model(&models.SalesReturnInfo{})
  3132. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  3133. if len(keywords) > 0 {
  3134. likeKey := "%" + keywords + "%"
  3135. 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)
  3136. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  3137. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  3138. 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")
  3139. } else {
  3140. 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)
  3141. }
  3142. if manufacturer > 0 {
  3143. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  3144. }
  3145. if order_type > 0 {
  3146. db = db.Where("xt_sales_return_info.type = ?", order_type)
  3147. }
  3148. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  3149. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3150. offset := (page - 1) * limit
  3151. db = db.Count(&total)
  3152. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  3153. return
  3154. }
  3155. 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) {
  3156. db := readDb.Model(&models.CancelStockInfo{})
  3157. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3158. if len(keywords) > 0 {
  3159. likeKey := "%" + keywords + "%"
  3160. 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)
  3161. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3162. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3163. 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")
  3164. } else {
  3165. 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)
  3166. }
  3167. if manufacturer > 0 {
  3168. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3169. }
  3170. if order_type > 0 {
  3171. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3172. }
  3173. if storehouse_id > 0 {
  3174. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  3175. }
  3176. if good_id > 0 {
  3177. db = db.Where("xt_cancel_stock_info.good_id = ?", good_id)
  3178. }
  3179. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3180. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3181. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  3182. return
  3183. }
  3184. 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) {
  3185. db := readDb.Model(&models.CancelStockInfo{})
  3186. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3187. if len(keywords) > 0 {
  3188. likeKey := "%" + keywords + "%"
  3189. 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)
  3190. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3191. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3192. 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")
  3193. } else {
  3194. 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)
  3195. }
  3196. if manufacturer > 0 {
  3197. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3198. }
  3199. if order_type > 0 {
  3200. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3201. }
  3202. if storehouse_id > 0 {
  3203. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  3204. }
  3205. if good_id > 0 {
  3206. db = db.Where("xt_cancel_stock_info.good_id =?", good_id)
  3207. }
  3208. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3209. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3210. db = db.Count(&total)
  3211. offset := (page - 1) * limit
  3212. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  3213. return
  3214. }
  3215. func FindStockInByDealerId(id int64) (total int64, err error) {
  3216. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  3217. return
  3218. }
  3219. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  3220. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  3221. return
  3222. }
  3223. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  3224. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  3225. return
  3226. }
  3227. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  3228. 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)
  3229. return
  3230. }
  3231. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  3232. 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)
  3233. return
  3234. }
  3235. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  3236. 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)
  3237. return
  3238. }
  3239. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  3240. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  3241. return goodType, err
  3242. }
  3243. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  3244. 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
  3245. return info, err
  3246. }
  3247. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  3248. 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
  3249. return
  3250. }
  3251. func FindStockOutByIsSysOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (out models.WarehouseOut, err error) {
  3252. 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
  3253. if err != gorm.ErrRecordNotFound {
  3254. if err != nil {
  3255. tx.Rollback()
  3256. return
  3257. }
  3258. }
  3259. return
  3260. }
  3261. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id 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 = ? AND second_warehouse_id=?", is_sys, org_id, record_time, second_warehouse_id).First(&out).Error
  3263. return
  3264. }
  3265. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3266. 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
  3267. return
  3268. }
  3269. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  3270. 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
  3271. if err != gorm.ErrRecordNotFound {
  3272. if err != nil {
  3273. tx.Rollback()
  3274. return
  3275. }
  3276. }
  3277. return
  3278. }
  3279. func FindDrugSettleStockOutByIsSys(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 FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  3284. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  3285. return
  3286. }
  3287. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  3288. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  3289. return
  3290. }
  3291. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3292. info := models.WarehouseOutInfo{}
  3293. 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
  3294. if err == gorm.ErrRecordNotFound {
  3295. return nil, err
  3296. }
  3297. if err != nil {
  3298. return nil, err
  3299. }
  3300. return &info, nil
  3301. }
  3302. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3303. info := models.WarehouseOutInfo{}
  3304. 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
  3305. if err == gorm.ErrRecordNotFound {
  3306. return nil, err
  3307. }
  3308. if err != nil {
  3309. return nil, err
  3310. }
  3311. return &info, nil
  3312. }
  3313. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3314. 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
  3315. return err
  3316. }
  3317. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3318. 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
  3319. return err
  3320. }
  3321. func GetGoodsIsExsit(good_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  3322. outInfo := models.WarehouseOutInfo{}
  3323. 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
  3324. if err != gorm.ErrRecordNotFound {
  3325. if err != nil {
  3326. tx.Rollback()
  3327. return outInfo, err
  3328. }
  3329. }
  3330. return outInfo, err
  3331. }
  3332. func GetDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64) (models.XtDrugWarehouseOutInfo, error) {
  3333. outInfo := models.XtDrugWarehouseOutInfo{}
  3334. 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
  3335. return outInfo, err
  3336. }
  3337. func GetNewDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.XtDrugWarehouseOutInfo, error) {
  3338. outInfo := models.XtDrugWarehouseOutInfo{}
  3339. 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
  3340. if err != gorm.ErrRecordNotFound {
  3341. if err != nil {
  3342. tx.Rollback()
  3343. return outInfo, err
  3344. }
  3345. }
  3346. return outInfo, err
  3347. }
  3348. func AddSigleWarehouseOutInfoOne(info *models.WarehouseOutInfo) error {
  3349. err := XTWriteDB().Create(&info).Error
  3350. return err
  3351. }
  3352. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3353. err := tx.Create(&info).Error
  3354. if err != nil {
  3355. tx.Rollback()
  3356. return err
  3357. }
  3358. return err
  3359. }
  3360. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3361. err := tx.Create(&info).Error
  3362. if err != nil {
  3363. tx.Rollback()
  3364. return err
  3365. }
  3366. return err
  3367. }
  3368. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  3369. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3370. return err
  3371. }
  3372. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  3373. err := writeDb.Create(&info).Error
  3374. return err
  3375. }
  3376. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, tx *gorm.DB) error {
  3377. err := tx.Create(&info).Error
  3378. if err != nil {
  3379. tx.Rollback()
  3380. return err
  3381. }
  3382. return err
  3383. }
  3384. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  3385. err := tx.Create(&info).Error
  3386. if err != nil {
  3387. tx.Rollback()
  3388. return err
  3389. }
  3390. return err
  3391. }
  3392. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  3393. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3394. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  3395. if err != nil {
  3396. tx.Rollback()
  3397. return err
  3398. }
  3399. return err
  3400. }
  3401. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3402. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3403. return err
  3404. }
  3405. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3406. ut := writeDb.Begin()
  3407. err := ut.Create(&detail).Error
  3408. if err != nil {
  3409. ut.Rollback()
  3410. return err
  3411. }
  3412. ut.Commit()
  3413. return err
  3414. }
  3415. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3416. err := writeDb.Create(&detail).Error
  3417. return err
  3418. }
  3419. 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) {
  3420. detail := models.DrugAutomaticReduceDetail{}
  3421. 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
  3422. if err != gorm.ErrRecordNotFound {
  3423. if err != nil {
  3424. tx.Rollback()
  3425. return detail, err
  3426. }
  3427. }
  3428. return detail, err
  3429. }
  3430. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3431. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3432. if err != nil {
  3433. tx.Rollback()
  3434. return err
  3435. }
  3436. return err
  3437. }
  3438. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3439. err := tx.Create(&detail).Error
  3440. if err != nil {
  3441. tx.Rollback()
  3442. return err
  3443. }
  3444. return err
  3445. }
  3446. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3447. 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
  3448. return
  3449. }
  3450. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3451. 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
  3452. return
  3453. }
  3454. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3455. 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
  3456. return
  3457. }
  3458. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3459. info := models.WarehouseOutInfo{}
  3460. 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
  3461. return info, err
  3462. }
  3463. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3464. 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
  3465. return
  3466. }
  3467. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3468. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3469. }
  3470. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3471. db := readDb.Model(&models.AutomaticReduceDetail{})
  3472. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3473. db = db.Preload("GoodsType", "status = 1")
  3474. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3475. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3476. db = db.Count(&total)
  3477. err = db.Find(&user).Error
  3478. return
  3479. }
  3480. type DrugAutomaticReduceDetail struct {
  3481. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3482. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3483. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3484. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3485. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3486. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3487. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3488. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3489. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3490. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3491. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3492. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3493. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3494. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3495. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3496. }
  3497. func (DrugAutomaticReduceDetail) TableName() string {
  3498. return "xt_drug_automatic_reduce_detail"
  3499. }
  3500. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3501. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3502. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3503. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3504. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3505. db = db.Count(&total)
  3506. err = db.Find(&user).Error
  3507. return
  3508. }
  3509. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3510. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3511. }
  3512. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3513. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3514. }
  3515. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3516. 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
  3517. return
  3518. }
  3519. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3520. 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
  3521. return
  3522. }
  3523. type VMWarehousingInfo struct {
  3524. ID int64 `gorm:"column:id" json:"id"`
  3525. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3526. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3527. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3528. Status int64 `gorm:"column:status" json:"status"`
  3529. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3530. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3531. }
  3532. func (VMWarehousingInfo) TableName() string {
  3533. return "xt_warehouse_info"
  3534. }
  3535. type GoodInfo struct {
  3536. ID int64 `gorm:"column:id" json:"id"`
  3537. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3538. Status int64 `gorm:"column:status" json:"status"`
  3539. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3540. }
  3541. func (GoodInfo) TableName() string {
  3542. return "xt_good_information"
  3543. }
  3544. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3545. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3546. return
  3547. }
  3548. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3549. 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
  3550. return
  3551. }
  3552. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3553. likeKey := "%" + keyword + "%"
  3554. db := XTReadDB().Model(&goods)
  3555. if len(keyword) > 0 {
  3556. db = db.Where("type_name like ?", likeKey)
  3557. }
  3558. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3559. return goods, err
  3560. }
  3561. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3562. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3563. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3564. return goodstype, err
  3565. }
  3566. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3567. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3568. if orgid > 0 {
  3569. db = db.Where("x.user_org_id = ?", orgid)
  3570. }
  3571. if recordtime > 0 {
  3572. db = db.Where("x.record_date = ?", recordtime)
  3573. }
  3574. if patientId > 0 {
  3575. db = db.Where("x.patient_id = ?", patientId)
  3576. }
  3577. 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
  3578. return prepare, err
  3579. }
  3580. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3581. detail := models.XtAutomaticReduceDetail{}
  3582. 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
  3583. if err == gorm.ErrRecordNotFound {
  3584. return nil, err
  3585. }
  3586. if err != nil {
  3587. return nil, err
  3588. }
  3589. return &detail, nil
  3590. }
  3591. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3592. 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
  3593. return autoMatic, err
  3594. }
  3595. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3596. 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
  3597. return autoMatic, err
  3598. }
  3599. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3600. var manufacturer models.Manufacturer
  3601. var err error
  3602. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3603. if err == gorm.ErrRecordNotFound {
  3604. return nil, err
  3605. }
  3606. if err != nil {
  3607. return nil, err
  3608. }
  3609. return &manufacturer, nil
  3610. }
  3611. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3612. err := XTWriteDB().Create(manufacturer).Error
  3613. return err
  3614. }
  3615. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3616. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3617. return err
  3618. }
  3619. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3620. var good models.GoodInfo
  3621. var err error
  3622. 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
  3623. if err == gorm.ErrRecordNotFound {
  3624. return nil, err
  3625. }
  3626. if err != nil {
  3627. return nil, err
  3628. }
  3629. return &good, nil
  3630. }
  3631. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3632. err := XTWriteDB().Create(&info).Error
  3633. return err
  3634. }
  3635. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3636. 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
  3637. return err
  3638. }
  3639. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3640. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3641. 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
  3642. return houseOutInfo, err
  3643. }
  3644. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3645. key := "%" + keyword + "%"
  3646. if len(keyword) == 0 {
  3647. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3648. } else {
  3649. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3650. }
  3651. return goodInfo, err
  3652. }
  3653. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3654. var drug models.BaseDrugLib
  3655. var err error
  3656. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3657. if err == gorm.ErrRecordNotFound {
  3658. return nil, err
  3659. }
  3660. if err != nil {
  3661. return nil, err
  3662. }
  3663. return &drug, nil
  3664. }
  3665. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3666. var drug models.BaseDrugLib
  3667. var err error
  3668. 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
  3669. if err == gorm.ErrRecordNotFound {
  3670. return nil, err
  3671. }
  3672. if err != nil {
  3673. return nil, err
  3674. }
  3675. return &drug, nil
  3676. }
  3677. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3678. err := XTWriteDB().Create(&info).Error
  3679. return err
  3680. }
  3681. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3682. 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
  3683. return err
  3684. }
  3685. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3686. var projoect models.HisProject
  3687. var err error
  3688. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3689. if err == gorm.ErrRecordNotFound {
  3690. return nil, err
  3691. }
  3692. if err != nil {
  3693. return nil, err
  3694. }
  3695. return &projoect, nil
  3696. }
  3697. func CreateProjectInfomation(info *models.HisProject) error {
  3698. err := XTWriteDB().Create(&info).Error
  3699. return err
  3700. }
  3701. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3702. 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
  3703. return err
  3704. }
  3705. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3706. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3707. return warehouseinfo, err
  3708. }
  3709. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3710. 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
  3711. return err
  3712. }
  3713. func EditNewDrugWarehousingOne(warehouse models.DrugWarehouse, id int64, tx *gorm.DB) error {
  3714. 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
  3715. if err != nil {
  3716. tx.Rollback()
  3717. return err
  3718. }
  3719. return err
  3720. }
  3721. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3722. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3723. return list, err
  3724. }
  3725. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3726. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3727. return list, err
  3728. }
  3729. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3730. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3731. return list, err
  3732. }
  3733. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3734. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3735. // return list, err
  3736. // }
  3737. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3738. var dealer models.Dealer
  3739. var err error
  3740. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3741. if err == gorm.ErrRecordNotFound {
  3742. return nil, err
  3743. }
  3744. if err != nil {
  3745. return nil, err
  3746. }
  3747. return &dealer, nil
  3748. }
  3749. func CreateDealer(dealer *models.Dealer) error {
  3750. err := XTWriteDB().Create(&dealer).Error
  3751. return err
  3752. }
  3753. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3754. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3755. return err
  3756. }
  3757. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3758. dataconfig := models.DictDataconfig{}
  3759. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3760. return dataconfig, err
  3761. }
  3762. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3763. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3764. return dataconfig, err
  3765. }
  3766. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3767. var dataconfig models.DictDataconfig
  3768. var err error
  3769. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3770. if err == gorm.ErrRecordNotFound {
  3771. return nil, err
  3772. }
  3773. if err != nil {
  3774. return nil, err
  3775. }
  3776. return &dataconfig, nil
  3777. }
  3778. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3779. dataconfig := models.DictDataconfig{}
  3780. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3781. return dataconfig, err
  3782. }
  3783. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3784. err := XTWriteDB().Create(&dataconfig).Error
  3785. return err
  3786. }
  3787. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3788. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3789. return err
  3790. }
  3791. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3792. var goodType models.GoodsType
  3793. var err error
  3794. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3795. if err == gorm.ErrRecordNotFound {
  3796. return nil, err
  3797. }
  3798. if err != nil {
  3799. return nil, err
  3800. }
  3801. return &goodType, nil
  3802. }
  3803. func CreatedGoodType(goodsType *models.GoodsType) error {
  3804. err := XTWriteDB().Create(&goodsType).Error
  3805. return err
  3806. }
  3807. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3808. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3809. return goodsType, err
  3810. }
  3811. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3812. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3813. return goodsType, err
  3814. }
  3815. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3816. var department models.XtHisDepartment
  3817. var err error
  3818. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3819. if err == gorm.ErrRecordNotFound {
  3820. return nil, err
  3821. }
  3822. if err != nil {
  3823. return nil, err
  3824. }
  3825. return &department, nil
  3826. }
  3827. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3828. dataconfig := models.Dataconfig{}
  3829. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3830. return dataconfig, err
  3831. }
  3832. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3833. var dataconfig models.Dataconfig
  3834. var err error
  3835. 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
  3836. if err == gorm.ErrRecordNotFound {
  3837. return nil, err
  3838. }
  3839. if err != nil {
  3840. return nil, err
  3841. }
  3842. return &dataconfig, nil
  3843. }
  3844. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3845. dataconfig := models.Dataconfig{}
  3846. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3847. return dataconfig, err
  3848. }
  3849. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3850. err := XTWriteDB().Create(&dataconfig).Error
  3851. return err
  3852. }
  3853. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3854. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3855. return dataconfig, err
  3856. }
  3857. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3858. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3859. return list, err
  3860. }
  3861. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3862. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3863. if len(keywords) > 0 {
  3864. likekey := "%" + keywords + "%"
  3865. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3866. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3867. }).Find(&drug).Error
  3868. } else {
  3869. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3870. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3871. }).Find(&drug).Error
  3872. }
  3873. return
  3874. }
  3875. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id 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("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 {
  3880. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3881. }).Find(&drug).Error
  3882. } else {
  3883. 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 {
  3884. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3885. }).Find(&drug).Error
  3886. }
  3887. return
  3888. }
  3889. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3890. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3891. if len(keywords) > 0 {
  3892. likekey := "%" + keywords + "%"
  3893. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3894. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3895. }).Find(&good).Error
  3896. } else {
  3897. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3898. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3899. }).Find(&good).Error
  3900. }
  3901. return
  3902. }
  3903. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3904. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3905. if len(keywords) > 0 {
  3906. likekey := "%" + keywords + "%"
  3907. 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 {
  3908. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3909. }).Find(&good).Error
  3910. } else {
  3911. 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 {
  3912. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3913. }).Find(&good).Error
  3914. }
  3915. return
  3916. }
  3917. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3918. db := writeDb.Begin()
  3919. err := db.Create(&detail).Error
  3920. if err != nil {
  3921. db.Rollback()
  3922. return err
  3923. }
  3924. db.Commit()
  3925. return err
  3926. }
  3927. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3928. var autoreduece models.BloodAutomaticReduceDetail
  3929. var err error
  3930. 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
  3931. if err == gorm.ErrRecordNotFound {
  3932. return nil, err
  3933. }
  3934. if err != nil {
  3935. return nil, err
  3936. }
  3937. return &autoreduece, nil
  3938. }
  3939. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3940. auto := models.BloodAutomaticReduceDetail{}
  3941. 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
  3942. return auto, err
  3943. }
  3944. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3945. detail := models.BloodAutomaticReduceDetail{}
  3946. 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
  3947. return err
  3948. }
  3949. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3950. 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
  3951. return stockflow, err
  3952. }
  3953. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3954. 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
  3955. 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
  3956. 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
  3957. return err
  3958. }
  3959. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3960. detail := models.BloodAutomaticReduceDetail{}
  3961. 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
  3962. info := models.WarehouseOutInfo{}
  3963. 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
  3964. prepare := models.DialysisBeforePrepare{}
  3965. 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
  3966. flow := models.VmStockFlow{}
  3967. 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
  3968. return err
  3969. }
  3970. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3971. detail := models.BloodAutomaticReduceDetail{}
  3972. 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
  3973. info := models.WarehouseOutInfo{}
  3974. 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
  3975. prepare := models.DialysisBeforePrepare{}
  3976. 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
  3977. flow := models.VmStockFlow{}
  3978. 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
  3979. return err
  3980. }
  3981. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3982. detail := models.BloodAutomaticReduceDetail{}
  3983. 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
  3984. info := models.WarehouseOutInfo{}
  3985. 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
  3986. prepare := models.DialysisBeforePrepare{}
  3987. 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
  3988. flow := models.VmStockFlow{}
  3989. 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
  3990. return err
  3991. }
  3992. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3993. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3994. //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
  3995. return err
  3996. }
  3997. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3998. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3999. return info, err
  4000. }
  4001. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  4002. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  4003. return list, err
  4004. }
  4005. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  4006. 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
  4007. return err
  4008. }
  4009. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  4010. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4011. return err
  4012. }
  4013. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  4014. var autoreduece models.BloodAutomaticReduceDetail
  4015. var err error
  4016. 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
  4017. if err == gorm.ErrRecordNotFound {
  4018. return nil, err
  4019. }
  4020. if err != nil {
  4021. return nil, err
  4022. }
  4023. return &autoreduece, nil
  4024. }
  4025. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  4026. detail := models.WarehouseOutInfoSeven{}
  4027. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4028. return detail, err
  4029. }
  4030. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  4031. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4032. return detail, err
  4033. }
  4034. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  4035. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  4036. return project, err
  4037. }
  4038. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  4039. project := models.HisPrescriptionProject{}
  4040. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  4041. return project, err
  4042. }
  4043. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  4044. 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
  4045. return auto, err
  4046. }
  4047. func ModefyWarehouseInfo(count int64, id int64) error {
  4048. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4049. return err
  4050. }
  4051. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  4052. 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
  4053. 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
  4054. 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
  4055. //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
  4056. return err
  4057. }
  4058. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  4059. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4060. return
  4061. }
  4062. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  4063. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4064. return
  4065. }
  4066. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  4067. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4068. return info, err
  4069. }
  4070. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4071. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4072. return info, err
  4073. }
  4074. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4075. 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
  4076. return info, err
  4077. }
  4078. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  4079. offset := (page - 1) * limit
  4080. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  4081. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4082. fmt.Println(table)
  4083. if startime > 0 {
  4084. db = db.Where("x.ctime>=?", startime)
  4085. }
  4086. if endtime > 0 {
  4087. db = db.Where("x.ctime<=?", endtime)
  4088. }
  4089. likeKey := "%" + keyword + "%"
  4090. if len(likeKey) > 0 {
  4091. db = db.Where("t.good_name like ?", likeKey)
  4092. }
  4093. if orgid > 0 {
  4094. db = db.Where("x.org_id = ?", orgid)
  4095. }
  4096. if good_type > 0 {
  4097. db = db.Where("t.good_type_id = ?", good_type)
  4098. }
  4099. 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
  4100. return info, total, err
  4101. }
  4102. 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) {
  4103. offset := (page - 1) * limit
  4104. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  4105. likeKey := "%" + keyword + "%"
  4106. if len(likeKey) > 0 {
  4107. db = db.Where("x.good_name like ?", likeKey)
  4108. }
  4109. if orgid > 0 {
  4110. db = db.Where("x.org_id = ?", orgid)
  4111. }
  4112. if good_type > 0 {
  4113. db = db.Where("x.good_type_id = ?", good_type)
  4114. }
  4115. if good_id > 0 {
  4116. db = db.Where("x.id = ?", good_id)
  4117. }
  4118. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4119. if startime > 0 {
  4120. db = db.Where("ctime>=?", startime)
  4121. }
  4122. if endtime > 0 {
  4123. db = db.Where("ctime<=?", endtime)
  4124. }
  4125. if good_id > 0 {
  4126. db = db.Where("good_id = ?", good_id)
  4127. }
  4128. if storehouse_id > 0 {
  4129. db = db.Where("storehouse_id = ?", storehouse_id)
  4130. }
  4131. return db.Where("status = 1 and is_check = 1")
  4132. }).Preload("WarehousingInfo", 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").Group("good_id,storehouse_id")
  4146. }).Preload("CancelStockInfo", 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. return db.Where("status = 1")
  4154. }).Find(&info).Error
  4155. return info, total, err
  4156. }
  4157. 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) {
  4158. offset := (page - 1) * limit
  4159. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  4160. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4161. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  4162. fmt.Println(table, tables)
  4163. if startime > 0 {
  4164. db = db.Where("x.ctime>=?", startime)
  4165. }
  4166. if endtime > 0 {
  4167. db = db.Where("x.ctime<=?", endtime)
  4168. }
  4169. if startfirsttime > 0 {
  4170. db = db.Where("x.expiry_date >=?", startfirsttime)
  4171. }
  4172. if endfirsttime > 0 {
  4173. db = db.Where("x.expiry_date <=?", endfirsttime)
  4174. }
  4175. if orgid > 0 {
  4176. db = db.Where("x.org_id = ?", orgid)
  4177. }
  4178. if good_id > 0 {
  4179. db = db.Where("x.good_id = ?", good_id)
  4180. }
  4181. 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
  4182. return info, total, err
  4183. }
  4184. 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) {
  4185. offset := (page - 1) * limit
  4186. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  4187. if startime > 0 {
  4188. db = db.Where("x.ctime>=?", startime)
  4189. }
  4190. if endtime > 0 {
  4191. db = db.Where("x.ctime<=?", endtime)
  4192. }
  4193. if orgid > 0 {
  4194. db = db.Where("x.org_id = ?", orgid)
  4195. }
  4196. if good_id > 0 {
  4197. db = db.Where("x.good_id = ?", good_id)
  4198. }
  4199. if is_sys == 2 {
  4200. db = db.Where("x.is_sys = 1")
  4201. }
  4202. if is_sys == 3 {
  4203. db = db.Where("x.is_sys = 0")
  4204. }
  4205. 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
  4206. return info, total, err
  4207. }
  4208. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4209. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4210. if startime > 0 {
  4211. db = db.Where("x.ctime >=?", startime)
  4212. }
  4213. if endtime > 0 {
  4214. db = db.Where("x.ctime<=?", endtime)
  4215. }
  4216. if orgid > 0 {
  4217. db = db.Where("x.org_id = ?", orgid)
  4218. }
  4219. 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
  4220. return info, err
  4221. }
  4222. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4223. db := XTReadDB().Table("xt_warehouse_out_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.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  4234. return info, err
  4235. }
  4236. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  4237. if endtime > 0 {
  4238. 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
  4239. }
  4240. if endtime == 0 {
  4241. 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
  4242. }
  4243. return autoMatic, err
  4244. }
  4245. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  4246. out := models.WarehouseOut{}
  4247. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  4248. return out, err
  4249. }
  4250. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4251. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4252. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4253. fmt.Println(table)
  4254. if id > 0 {
  4255. db = db.Where("x.warehouse_out_id = ?", id)
  4256. }
  4257. if orgid > 0 {
  4258. db = db.Where("x.org_id = ?", orgid)
  4259. }
  4260. 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
  4261. return out, err
  4262. }
  4263. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  4264. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  4265. if len(id) > 0 {
  4266. db = db.Where("x.id in(?)", id)
  4267. }
  4268. if orgid > 0 {
  4269. db = db.Where("x.org_id= ?", orgid)
  4270. }
  4271. 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
  4272. return out, err
  4273. }
  4274. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4275. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4276. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4277. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  4278. fmt.Println(table, tableOne)
  4279. if len(id) > 0 {
  4280. db = db.Where("x.warehouse_out_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_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
  4286. return out, err
  4287. }
  4288. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4289. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4290. if len(id) > 0 {
  4291. db = db.Where("warehouse_out_id in (?)", id)
  4292. }
  4293. if orgid > 0 {
  4294. db = db.Where("user_org_id = ?", orgid)
  4295. }
  4296. err = db.Group("warehousing_id").Find(&list).Error
  4297. return list, err
  4298. }
  4299. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4300. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4301. if len(id) > 0 {
  4302. db = db.Where("warehouse_out_id in (?)", id)
  4303. }
  4304. if orgid > 0 {
  4305. db = db.Where("user_org_id = ?", orgid)
  4306. }
  4307. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4308. return list, err
  4309. }
  4310. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4311. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  4312. if len(id) > 0 {
  4313. db = db.Where("warehouse_out_id in (?)", id)
  4314. }
  4315. if orgid > 0 {
  4316. db = db.Where("user_org_id = ?", orgid)
  4317. }
  4318. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4319. return list, err
  4320. }
  4321. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  4322. db := XTReadDB().Model(&info)
  4323. db = db.Preload("GoodInfo", "status = 1")
  4324. 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
  4325. return info, err
  4326. }
  4327. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  4328. db := readDb.Model(&models.AutomaticReduceDetail{})
  4329. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  4330. db = db.Preload("GoodsType", "status = 1")
  4331. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  4332. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  4333. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  4334. db = db.Count(&total)
  4335. err = db.Find(&user).Error
  4336. return
  4337. }
  4338. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  4339. info := models.GoodInfo{}
  4340. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4341. return info, err
  4342. }
  4343. func GetNewGoodInformationByGoodId(good_id int64, tx *gorm.DB) (models.GoodInfo, error) {
  4344. info := models.GoodInfo{}
  4345. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4346. if err != gorm.ErrRecordNotFound {
  4347. if err != nil {
  4348. tx.Rollback()
  4349. return info, err
  4350. }
  4351. }
  4352. return info, err
  4353. }
  4354. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  4355. info := models.GoodInformationThirty{}
  4356. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4357. return info, err
  4358. }
  4359. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  4360. info := models.GoodInformationThirty{}
  4361. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4362. if err != gorm.ErrRecordNotFound {
  4363. if err != nil {
  4364. tx.Rollback()
  4365. return info, err
  4366. }
  4367. }
  4368. return info, err
  4369. }
  4370. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  4371. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  4372. return err
  4373. }
  4374. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  4375. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4376. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4377. fmt.Println(table)
  4378. if orgid > 0 {
  4379. db = db.Where("x.org_id = ?", orgid)
  4380. }
  4381. if len(orderid) > 0 {
  4382. db = db.Where("x.warehousing_id in(?)", orderid)
  4383. }
  4384. if startime > 0 {
  4385. db = db.Where("x.ctime>=?", startime)
  4386. }
  4387. if endtime > 0 {
  4388. db = db.Where("x.ctime<=?", endtime)
  4389. }
  4390. 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
  4391. return info, err
  4392. }
  4393. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  4394. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4395. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4396. fmt.Println(table)
  4397. if orgid > 0 {
  4398. db = db.Where("x.org_id = ?", orgid)
  4399. }
  4400. if len(orderid) > 0 {
  4401. db = db.Where("x.warehousing_id in(?)", orderid)
  4402. }
  4403. if startime > 0 {
  4404. db = db.Where("x.ctime>=?", startime)
  4405. }
  4406. if endtime > 0 {
  4407. db = db.Where("x.ctime<=?", endtime)
  4408. }
  4409. if storehouse_id > 0 {
  4410. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4411. }
  4412. 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
  4413. return info, err
  4414. }
  4415. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4416. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4417. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  4418. fmt.Println(table)
  4419. if startime > 0 {
  4420. db = db.Where("x.ctime>=?", startime)
  4421. }
  4422. if endtime > 0 {
  4423. db = db.Where("x.ctime<=?", endtime)
  4424. }
  4425. if orgid > 0 {
  4426. db = db.Where("x.org_id = ?", orgid)
  4427. }
  4428. 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
  4429. return info, err
  4430. }
  4431. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4432. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4433. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4434. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4435. fmt.Println(table, tableOne)
  4436. if orgid > 0 {
  4437. db = db.Where("x.org_id = ?", orgid)
  4438. }
  4439. if orgid > 0 {
  4440. db = db.Where("t.org_id = ?", orgid)
  4441. }
  4442. if orgid > 0 {
  4443. db = db.Where("p.org_id = ?", orgid)
  4444. }
  4445. if len(ids) > 0 {
  4446. db = db.Where("x.warehouse_out_id in (?)", ids)
  4447. }
  4448. if startime > 0 {
  4449. db = db.Where("x.ctime>=?", startime)
  4450. }
  4451. if endtime > 0 {
  4452. db = db.Where("x.ctime<=?", endtime)
  4453. }
  4454. 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
  4455. return out, err
  4456. }
  4457. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4458. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4459. if len(ids) > 0 {
  4460. db = db.Where("x.id in(?)", ids)
  4461. }
  4462. if orgid > 0 {
  4463. db = db.Where("x.org_id = ?", orgid)
  4464. }
  4465. 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
  4466. return info, err
  4467. }
  4468. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4469. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4470. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4471. fmt.Println(table)
  4472. if len(ids) > 0 {
  4473. db = db.Where("x.warehouse_out_id in(?)", ids)
  4474. }
  4475. if orgid > 0 {
  4476. db = db.Where("x.org_id = ?", orgid)
  4477. }
  4478. 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
  4479. return info, err
  4480. }
  4481. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4482. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4483. likeKey := "%" + keyword + "%"
  4484. if len(keyword) > 0 {
  4485. db = db.Where("x.drug_name like ?", likeKey)
  4486. }
  4487. if orgid > 0 {
  4488. db = db.Where("x.org_id = ?", orgid)
  4489. }
  4490. err = db.Find(&basedrug).Error
  4491. return basedrug, err
  4492. }
  4493. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4494. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4495. if len(ids) > 0 {
  4496. db = db.Where("x.drug_id in(?)", ids)
  4497. }
  4498. if orgid > 0 {
  4499. db = db.Where("x.org_id = ?", orgid)
  4500. }
  4501. if startime > 0 {
  4502. db = db.Where("x.ctime>=?", startime)
  4503. }
  4504. if endtime > 0 {
  4505. db = db.Where("x.ctime<=?", endtime)
  4506. }
  4507. if storehouse_id > 0 {
  4508. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4509. }
  4510. err = db.Find(&info).Error
  4511. return info, err
  4512. }
  4513. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4514. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4515. if len(ids) > 0 {
  4516. db = db.Where("x.drug_id in(?)", ids)
  4517. }
  4518. if orgid > 0 {
  4519. db = db.Where("x.org_id = ?", orgid)
  4520. }
  4521. if startime > 0 {
  4522. db = db.Where("x.ctime>=?", startime)
  4523. }
  4524. if endtime > 0 {
  4525. db = db.Where("x.ctime<=?", endtime)
  4526. }
  4527. if storehouse_id > 0 {
  4528. db = db.Where("storehouse_id = ?", storehouse_id)
  4529. }
  4530. err = db.Find(&info).Error
  4531. return info, err
  4532. }
  4533. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4534. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4535. if drugid > 0 {
  4536. db = db.Where("x.drug_id = ?", drugid)
  4537. }
  4538. if orgid > 0 {
  4539. db = db.Where("x.org_id = ?", orgid)
  4540. }
  4541. if storehouse_id > 0 {
  4542. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4543. }
  4544. 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
  4545. return info, err
  4546. }
  4547. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4548. db := tx.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. if err != gorm.ErrRecordNotFound {
  4560. if err != nil {
  4561. tx.Rollback()
  4562. return info, err
  4563. }
  4564. }
  4565. return info, err
  4566. }
  4567. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4568. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4569. if drugid > 0 {
  4570. db = db.Where("x.drug_id = ?", drugid)
  4571. }
  4572. if orgid > 0 {
  4573. db = db.Where("x.org_id = ?", orgid)
  4574. }
  4575. if storehouse_id > 0 {
  4576. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4577. }
  4578. 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
  4579. return info, err
  4580. }
  4581. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4582. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4583. if id > 0 {
  4584. db = db.Where("x.id = ?", id)
  4585. }
  4586. if orgid > 0 {
  4587. db = db.Where("x.org_id = ?", orgid)
  4588. }
  4589. 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
  4590. return info, err
  4591. }
  4592. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4593. info := models.VmDrugWarehouseInfo{}
  4594. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4595. if id == 0 {
  4596. db = db.Where("id = ?", id)
  4597. }
  4598. if drugid > 0 {
  4599. db = db.Where("x.drug_id = ?", drugid)
  4600. }
  4601. if orgid > 0 {
  4602. db = db.Where("x.org_id = ?", orgid)
  4603. }
  4604. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4605. return info, err
  4606. }
  4607. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4608. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4609. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4610. fmt.Println(table)
  4611. if drugid > 0 {
  4612. db = db.Where("x.drug_id = ?", drugid)
  4613. }
  4614. if storehouse_id > 0 {
  4615. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4616. }
  4617. 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
  4618. return list, err
  4619. }
  4620. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4621. lib := models.BaseDrugLib{}
  4622. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4623. return lib, err
  4624. }
  4625. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4626. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4627. if goodid > 0 {
  4628. db = db.Where("x.good_id = ?", goodid)
  4629. }
  4630. if orgid > 0 {
  4631. db = db.Where("x.org_id = ?", orgid)
  4632. }
  4633. 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
  4634. return info, err
  4635. }
  4636. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4637. 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
  4638. return err
  4639. }
  4640. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4641. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4642. return info, err
  4643. }
  4644. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4645. //遍历循环入库数据
  4646. for i, item := range list {
  4647. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4648. if (item.WarehousingCount - item.StockCount) >= count {
  4649. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4650. info := models.WarehousingInfo{
  4651. GoodId: item.GoodId,
  4652. OrgId: item.OrgId,
  4653. WarehousingId: item.WarehousingId,
  4654. StockCount: item.StockCount + count,
  4655. }
  4656. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4657. fmt.Println("2323233232323232323232323232323", err2)
  4658. return err
  4659. }
  4660. if (item.WarehousingCount - item.StockCount) < count {
  4661. //如果该批次小于退库数量,则先出库该相差数量
  4662. var countOne = item.WarehousingCount - item.StockCount
  4663. infoOne := models.WarehousingInfo{
  4664. GoodId: item.GoodId,
  4665. OrgId: item.OrgId,
  4666. WarehousingId: item.WarehousingId,
  4667. StockCount: countOne + item.StockCount,
  4668. }
  4669. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4670. if err == nil {
  4671. //剩余的出库到第二个批次
  4672. var countTwo = count - countOne
  4673. infos := append(list, list[i+1])
  4674. var idArray []*models.WarehousingInfo
  4675. for _, it := range infos {
  4676. if it.WarehousingId != item.WarehousingId {
  4677. idArray = append(idArray, it)
  4678. }
  4679. }
  4680. //查询更新后的库存
  4681. UpdateCancelOut(idArray, countTwo)
  4682. return err
  4683. } else {
  4684. return err
  4685. }
  4686. }
  4687. }
  4688. return err
  4689. }
  4690. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4691. if len(idstr) > 0 {
  4692. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4693. } else {
  4694. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4695. }
  4696. return info, err
  4697. }
  4698. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4699. if len(idstr) > 0 {
  4700. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4701. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4702. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4703. }).Find(&info).Error
  4704. }
  4705. return info, err
  4706. }
  4707. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4708. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4709. if startime > 0 {
  4710. db = db.Where("x.ctime >=?", startime)
  4711. }
  4712. if endtime > 0 {
  4713. db = db.Where("x.ctime <= ?", endtime)
  4714. }
  4715. if orgid > 0 {
  4716. db = db.Where("x.org_id = ?", orgid)
  4717. }
  4718. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4719. return info, err
  4720. }
  4721. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4722. info := models.WarehouseOutInfo{}
  4723. var err error
  4724. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4725. if err == gorm.ErrRecordNotFound {
  4726. return nil, err
  4727. }
  4728. if err != nil {
  4729. return nil, err
  4730. }
  4731. return &info, nil
  4732. }
  4733. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4734. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4735. if goodid > 0 {
  4736. db = db.Where("x.good_id = ?", goodid)
  4737. }
  4738. if orgid > 0 {
  4739. db = db.Where("x.org_id = ?", orgid)
  4740. }
  4741. if id > 0 {
  4742. db = db.Where("x.warehouse_info_id = ?", id)
  4743. }
  4744. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4745. return info, err
  4746. }
  4747. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4748. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4749. if goodid > 0 {
  4750. db = db.Where("x.good_id = ?", goodid)
  4751. }
  4752. if orgid > 0 {
  4753. db = db.Where("x.user_org_id = ?", orgid)
  4754. }
  4755. if id > 0 {
  4756. db = db.Where("x.warehousing_id = ?", id)
  4757. }
  4758. if storehouse_id > 0 {
  4759. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4760. }
  4761. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4762. return info, err
  4763. }
  4764. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4765. stock := models.CancelStock{}
  4766. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4767. return stock, err
  4768. }
  4769. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4770. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4771. return info, err
  4772. }
  4773. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4774. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4775. return info, err
  4776. }
  4777. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4778. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4779. return stock, err
  4780. }
  4781. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4782. 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)")
  4783. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4784. fmt.Println(table)
  4785. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4786. fmt.Println(tableone)
  4787. if id > 0 {
  4788. db = db.Where("x.warehouse_out_id = ?", id)
  4789. }
  4790. if goodid > 0 {
  4791. db = db.Where("x.good_id = ?", goodid)
  4792. }
  4793. 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
  4794. return stock, err
  4795. }
  4796. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4797. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4798. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4799. fmt.Println(table)
  4800. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4801. fmt.Println(tableone)
  4802. if id > 0 {
  4803. db = db.Where("x.warehouse_out_id = ?", id)
  4804. }
  4805. if goodid > 0 {
  4806. db = db.Where("x.good_id = ?", goodid)
  4807. }
  4808. 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
  4809. return stock, err
  4810. }
  4811. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4812. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4813. return info, err
  4814. }
  4815. func ExportDrugList(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseInfo, err error) {
  4816. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1 AND x.org_id=?", org_id)
  4817. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4818. tableOne := XTReadDB().Table("xt_drug_warehouse as p").Where("p.status=1 and p.org_id=?", org_id)
  4819. fmt.Println("table", table)
  4820. fmt.Println("tableOne", tableOne)
  4821. if len(warehouse_out_id) > 0 {
  4822. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4823. }
  4824. 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
  4825. return out, err
  4826. }
  4827. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4828. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4829. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4830. fmt.Println("table", table)
  4831. if len(warehouse_out_id) > 0 {
  4832. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4833. }
  4834. 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
  4835. return out, err
  4836. }
  4837. func GetExportOutOrderDrugListOne(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseOutInfo, err error) {
  4838. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1 and x.user_org_id =?", org_id)
  4839. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4840. fmt.Println("table", table)
  4841. if len(warehouse_out_id) > 0 {
  4842. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4843. }
  4844. 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
  4845. return out, err
  4846. }
  4847. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4848. db := XTReadDB().Model(&info).Where("status = 1")
  4849. if len(warehouse_out_id) > 0 {
  4850. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4851. }
  4852. if orgid > 0 {
  4853. db = db.Where("org_id = ?", orgid)
  4854. }
  4855. err = db.Find(&info).Error
  4856. return info, err
  4857. }
  4858. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4859. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4860. return out, err
  4861. }
  4862. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4863. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4864. 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)
  4865. }).Find(&out).Error
  4866. return out, err
  4867. }
  4868. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4869. info := models.WarehouseOutInfo{}
  4870. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4871. return info, err
  4872. }
  4873. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4874. info := models.WarehousingInfo{}
  4875. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4876. if goodid > 0 {
  4877. db = db.Where("x.good_id = ?", goodid)
  4878. }
  4879. if storehouse_id > 0 {
  4880. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4881. }
  4882. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4883. return info, err
  4884. }
  4885. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4886. info := models.WarehousingInfo{}
  4887. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4888. return info, err
  4889. }
  4890. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4891. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4892. return err
  4893. }
  4894. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4895. if storehouse_id == 0 {
  4896. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4897. }
  4898. if storehouse_id > 0 {
  4899. 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
  4900. }
  4901. return info, err
  4902. }
  4903. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4904. if len(warehousingInfo) > 0 {
  4905. utx := writeDb.Begin()
  4906. if len(warehousingInfo) > 0 {
  4907. 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 "
  4908. insertParams := make([]string, 0)
  4909. insertData := make([]interface{}, 0)
  4910. for _, info := range warehousingInfo {
  4911. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4912. insertData = append(insertData, info.WarehousingId)
  4913. insertData = append(insertData, info.GoodId)
  4914. insertData = append(insertData, info.Number)
  4915. insertData = append(insertData, info.LicenseNumber)
  4916. insertData = append(insertData, info.Count)
  4917. insertData = append(insertData, info.UserOrgId)
  4918. insertData = append(insertData, info.PatientId)
  4919. insertData = append(insertData, info.SystemTime)
  4920. insertData = append(insertData, info.ConsumableType)
  4921. insertData = append(insertData, info.IsSys)
  4922. insertData = append(insertData, info.WarehousingOrder)
  4923. insertData = append(insertData, info.WarehouseOutId)
  4924. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4925. insertData = append(insertData, info.IsEdit)
  4926. insertData = append(insertData, info.CancelStockId)
  4927. insertData = append(insertData, info.CancelOrderNumber)
  4928. insertData = append(insertData, info.Manufacturer)
  4929. insertData = append(insertData, info.Dealer)
  4930. insertData = append(insertData, info.Creator)
  4931. insertData = append(insertData, info.UpdateCreator)
  4932. insertData = append(insertData, info.Status)
  4933. insertData = append(insertData, info.Ctime)
  4934. insertData = append(insertData, info.Mtime)
  4935. insertData = append(insertData, info.Price)
  4936. insertData = append(insertData, info.WarehousingDetailId)
  4937. insertData = append(insertData, info.WarehouseOutDetailId)
  4938. insertData = append(insertData, info.CancelOutDetailId)
  4939. insertData = append(insertData, info.ExpireDate)
  4940. insertData = append(insertData, info.ProductDate)
  4941. }
  4942. thisSQL += strings.Join(insertParams, ", ")
  4943. err = utx.Exec(thisSQL, insertData...).Error
  4944. if err != nil {
  4945. utx.Rollback()
  4946. return
  4947. }
  4948. }
  4949. utx.Commit()
  4950. }
  4951. return
  4952. }
  4953. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4954. utx := XTWriteDB().Begin()
  4955. err := utx.Create(&flow).Error
  4956. if err != nil {
  4957. utx.Rollback()
  4958. return err
  4959. }
  4960. utx.Commit()
  4961. return err
  4962. }
  4963. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4964. err := tx.Create(&flow).Error
  4965. if err != nil {
  4966. tx.Rollback()
  4967. return err
  4968. }
  4969. return err
  4970. }
  4971. func CreateOutInfoLog(log models.XtGoodWarehouseOutInfoLog, tx *gorm.DB) error {
  4972. err := tx.Create(&log).Error
  4973. if err != nil {
  4974. tx.Rollback()
  4975. return err
  4976. }
  4977. return err
  4978. }
  4979. func CreateGoodWarehouseInfoLog(log models.XtGoodWarehouseInfoLog, tx *gorm.DB) error {
  4980. err := tx.Create(&log).Error
  4981. if err != nil {
  4982. tx.Rollback()
  4983. return err
  4984. }
  4985. return err
  4986. }
  4987. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4988. err := XTWriteDB().Create(&flow).Error
  4989. return err
  4990. }
  4991. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4992. err := XTWriteDB().Save(&flow).Error
  4993. return err
  4994. }
  4995. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4996. 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
  4997. return err
  4998. }
  4999. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  5000. 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
  5001. return err
  5002. }
  5003. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  5004. stock := models.VmStockFlow{}
  5005. var err error
  5006. 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
  5007. if err == gorm.ErrRecordNotFound {
  5008. return nil, err
  5009. }
  5010. if err != nil {
  5011. return nil, err
  5012. }
  5013. return &stock, nil
  5014. }
  5015. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5016. 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
  5017. return flow, nil
  5018. }
  5019. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5020. 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
  5021. return flow, nil
  5022. }
  5023. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5024. 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
  5025. if err != gorm.ErrRecordNotFound {
  5026. if err != nil {
  5027. tx.Rollback()
  5028. return flow, nil
  5029. }
  5030. }
  5031. return flow, nil
  5032. }
  5033. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5034. 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
  5035. return flow, nil
  5036. }
  5037. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  5038. 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
  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 GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  5048. 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
  5049. return flow, nil
  5050. }
  5051. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  5052. 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
  5053. if err != gorm.ErrRecordNotFound {
  5054. if err != nil {
  5055. tx.Rollback()
  5056. return flow, err
  5057. }
  5058. }
  5059. return flow, err
  5060. }
  5061. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5062. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  5063. return flow, nil
  5064. }
  5065. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5066. 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
  5067. return flow, nil
  5068. }
  5069. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5070. 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
  5071. return flow, nil
  5072. }
  5073. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5074. 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
  5075. if err != gorm.ErrRecordNotFound {
  5076. if err != nil {
  5077. tx.Rollback()
  5078. return flow, nil
  5079. }
  5080. }
  5081. return flow, nil
  5082. }
  5083. func GetStockFlowIsBatchNumberFourty(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 GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  5088. info := models.WarehouseOutInfo{}
  5089. var err error
  5090. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5091. if err == gorm.ErrRecordNotFound {
  5092. return nil, err
  5093. }
  5094. if err != nil {
  5095. return nil, err
  5096. }
  5097. return &info, nil
  5098. }
  5099. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  5100. info := models.WarehouseOutInfo{}
  5101. var err error
  5102. 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
  5103. if err == gorm.ErrRecordNotFound {
  5104. return nil, err
  5105. }
  5106. if err != nil {
  5107. return nil, err
  5108. }
  5109. return &info, nil
  5110. }
  5111. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  5112. info := models.WarehouseOutInfo{}
  5113. 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
  5114. return info, err
  5115. }
  5116. 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) {
  5117. offset := (page - 1) * limit
  5118. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  5119. if consumable_type > 0 {
  5120. //入库
  5121. if consumable_type == 1 {
  5122. db = db.Where("consumable_type = ?", consumable_type)
  5123. }
  5124. //2 手动出库
  5125. if consumable_type == 2 {
  5126. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  5127. }
  5128. //3.自动出库
  5129. if consumable_type == 3 {
  5130. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  5131. }
  5132. //4.手动退库
  5133. if consumable_type == 4 {
  5134. db = db.Where(" consumable_type = ?", consumable_type)
  5135. }
  5136. //自动退库
  5137. if consumable_type == 7 {
  5138. db = db.Where(" consumable_type = ?", consumable_type)
  5139. }
  5140. if consumable_type == 10 {
  5141. db = db.Where(" consumable_type = ?", consumable_type)
  5142. }
  5143. if consumable_type == 11 {
  5144. db = db.Where(" consumable_type = ?", consumable_type)
  5145. }
  5146. if consumable_type == 12 {
  5147. db = db.Where(" consumable_type = ?", consumable_type)
  5148. }
  5149. if consumable_type == 13 {
  5150. db = db.Where(" consumable_type = ?", consumable_type)
  5151. }
  5152. if consumable_type == 15 {
  5153. db = db.Where(" consumable_type = ?", consumable_type)
  5154. }
  5155. }
  5156. if orgId > 0 {
  5157. db = db.Where("user_org_id = ?", orgId)
  5158. }
  5159. if startTime > 0 {
  5160. db = db.Where("ctime >= ?", startTime)
  5161. }
  5162. if endTime > 0 {
  5163. db = db.Where("ctime<=?", endTime)
  5164. }
  5165. if good_id > 0 {
  5166. db = db.Where("good_id = ?", good_id)
  5167. }
  5168. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  5169. return flow, total, err
  5170. }
  5171. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  5172. detail := models.AutomaticReduceDetail{}
  5173. 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
  5174. return detail, err
  5175. }
  5176. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  5177. detail := models.WarehouseOutInfo{}
  5178. 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
  5179. return detail, err
  5180. }
  5181. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  5182. outInfo := models.WarehouseOutInfo{}
  5183. 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
  5184. return err
  5185. }
  5186. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  5187. outInfo := models.WarehouseOutInfo{}
  5188. 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
  5189. if err != nil {
  5190. tx.Rollback()
  5191. return err
  5192. }
  5193. return err
  5194. }
  5195. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  5196. outInfo := models.WarehouseOutInfo{}
  5197. 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
  5198. return err
  5199. }
  5200. 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 {
  5201. outInfo := models.WarehouseOutInfo{}
  5202. 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
  5203. if err != nil {
  5204. tx.Rollback()
  5205. return err
  5206. }
  5207. return err
  5208. }
  5209. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  5210. info := models.WarehouseOutInfo{}
  5211. 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
  5212. return info, err
  5213. }
  5214. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  5215. info := models.WarehousingInfo{}
  5216. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5217. return info, err
  5218. }
  5219. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  5220. info := models.WarehousingInfo{}
  5221. db := XTReadDB().Model(&info)
  5222. if good_id > 0 {
  5223. db = db.Where("good_id = ? and status = 1", good_id)
  5224. }
  5225. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  5226. return info, err
  5227. }
  5228. 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) {
  5229. offset := (page - 1) * limit
  5230. db := XTReadDB().Model(drug_flow).Where("status = 1")
  5231. if drugid > 0 {
  5232. db = db.Where("drug_id = ?", drugid)
  5233. }
  5234. if orgid > 0 {
  5235. db = db.Where("user_org_id = ?", orgid)
  5236. }
  5237. if startTime > 0 {
  5238. db = db.Where("ctime >=? ", startTime)
  5239. }
  5240. if endTime > 0 {
  5241. db = db.Where("ctime<=?", endTime)
  5242. }
  5243. if stock_type > 0 {
  5244. //手动入库
  5245. if stock_type == 1 {
  5246. db = db.Where("consumable_type = 1")
  5247. }
  5248. //手动出库
  5249. if stock_type == 2 {
  5250. db = db.Where("consumable_type = 2 and is_sys = 0")
  5251. }
  5252. //自动出库
  5253. if stock_type == 3 {
  5254. db = db.Where("consumable_type = 3 and is_sys = 1")
  5255. }
  5256. if stock_type == 4 {
  5257. db = db.Where("consumable_type = 4")
  5258. }
  5259. if stock_type == 10 {
  5260. db = db.Where("consumable_type = 10")
  5261. }
  5262. if stock_type == 11 {
  5263. db = db.Where("consumable_type = 11")
  5264. }
  5265. if stock_type == 12 {
  5266. db = db.Where("consumable_type = 12")
  5267. }
  5268. if stock_type == 13 {
  5269. db = db.Where("consumable_type = 13")
  5270. }
  5271. if stock_type == 15 {
  5272. db = db.Where("consumable_type = 15")
  5273. }
  5274. }
  5275. 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
  5276. return drug_flow, total, err
  5277. }
  5278. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  5279. outInfo := models.DrugWarehouseOutInfo{}
  5280. var err error
  5281. 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
  5282. if err == gorm.ErrRecordNotFound {
  5283. return nil, err
  5284. }
  5285. if err != nil {
  5286. return nil, err
  5287. }
  5288. return &outInfo, nil
  5289. }
  5290. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  5291. 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
  5292. return outInfo, err
  5293. }
  5294. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  5295. outInfo := models.DrugWarehouseOutInfo{}
  5296. 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
  5297. return err
  5298. }
  5299. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  5300. db := XTReadDB().Model(&cancle).Where("status = 1")
  5301. if startime > 0 {
  5302. db = db.Where("ctime >=?", startime)
  5303. }
  5304. if endtime > 0 {
  5305. db = db.Where("ctime <=?", endtime)
  5306. }
  5307. if len(ids) > 0 {
  5308. db = db.Where("cancel_stock_id in(?)", ids)
  5309. }
  5310. if orgid > 0 {
  5311. db = db.Where("org_id = ?", orgid)
  5312. }
  5313. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  5314. err = db.Find(&cancle).Error
  5315. return cancle, err
  5316. }
  5317. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  5318. db := XTReadDB().Model(&info)
  5319. if id > 0 {
  5320. db = db.Where("cancel_stock_id = ? and status = 1", id)
  5321. }
  5322. db = db.Preload("BaseDrugLib")
  5323. err = db.Find(&info).Error
  5324. return info, err
  5325. }
  5326. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  5327. info := models.XtDrugWarehouseInfo{}
  5328. db := XTReadDB().Model(&info).Where("status = 1")
  5329. if id > 0 {
  5330. db = db.Where("id = ?", id)
  5331. }
  5332. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5333. return info, err
  5334. }
  5335. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  5336. info := models.XtDrugWarehouseInfo{}
  5337. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  5338. return info, err
  5339. }
  5340. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  5341. // info := models.XtDrugWarehouseInfo{}
  5342. // db := tx.Model(&info).Where("status = 1")
  5343. // if id > 0 {
  5344. // db = db.Where("id = ?", id)
  5345. // }
  5346. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5347. // return info, err
  5348. //}
  5349. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5350. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  5351. return info, err
  5352. }
  5353. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  5354. 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
  5355. return info, err
  5356. }
  5357. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  5358. info := models.HisDoctorAdviceInfo{}
  5359. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  5360. return info, err
  5361. }
  5362. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  5363. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5364. return info, err
  5365. }
  5366. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  5367. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5368. if err != gorm.ErrRecordNotFound {
  5369. if err != nil {
  5370. tx.Rollback()
  5371. return info, err
  5372. }
  5373. }
  5374. return info, err
  5375. }
  5376. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  5377. adviceInfo := models.HisDoctorAdviceInfo{}
  5378. 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
  5379. if err != gorm.ErrRecordNotFound {
  5380. if err != nil {
  5381. tx.Rollback()
  5382. return adviceInfo, err
  5383. }
  5384. }
  5385. return adviceInfo, err
  5386. }
  5387. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5388. info := models.HisDoctorAdviceInfo{}
  5389. 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
  5390. return info, err
  5391. }
  5392. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5393. info := models.HisDoctorAdviceInfo{}
  5394. 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
  5395. return info, err
  5396. }
  5397. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  5398. detail := models.DrugAutomaticReduceDetail{}
  5399. 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
  5400. return detail, err
  5401. }
  5402. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  5403. 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
  5404. return out, err
  5405. }
  5406. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  5407. 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
  5408. return out, err
  5409. }
  5410. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  5411. 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
  5412. if err != gorm.ErrRecordNotFound {
  5413. if err != nil {
  5414. tx.Rollback()
  5415. return out, err
  5416. }
  5417. }
  5418. return out, err
  5419. }
  5420. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  5421. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5422. return err
  5423. }
  5424. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  5425. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5426. return err
  5427. }
  5428. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  5429. 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
  5430. 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
  5431. 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
  5432. return err
  5433. }
  5434. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5435. 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
  5436. 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
  5437. return err
  5438. }
  5439. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5440. 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
  5441. 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
  5442. return err
  5443. }
  5444. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5445. info := models.XtDrugWarehouseInfo{}
  5446. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5447. return info, err
  5448. }
  5449. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5450. info := models.XtDrugWarehouseInfo{}
  5451. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5452. return info, err
  5453. }
  5454. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5455. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5456. return err
  5457. }
  5458. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5459. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5460. // return err
  5461. //}
  5462. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5463. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5464. //return err
  5465. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5466. return err
  5467. }
  5468. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5469. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5470. // return err
  5471. //}
  5472. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5473. info := models.DrugCancelStockInfo{}
  5474. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5475. return info, err
  5476. }
  5477. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5478. info := models.DrugCancelStockInfo{}
  5479. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5480. return info, err
  5481. }
  5482. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5483. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5484. return err
  5485. }
  5486. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5487. 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
  5488. return err
  5489. }
  5490. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5491. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5492. return err
  5493. }
  5494. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5495. 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
  5496. return err
  5497. }
  5498. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5499. 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
  5500. return err
  5501. }
  5502. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5503. 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
  5504. return err
  5505. }
  5506. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5507. 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
  5508. return err
  5509. }
  5510. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5511. info := models.WarehousingInfo{}
  5512. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5513. return info, err
  5514. }
  5515. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5516. info := models.WarehousingInfo{}
  5517. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5518. return info, err
  5519. }
  5520. func GetLastWarehouseInfoTwo(goodid int64, storehouse_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  5521. 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
  5522. return info, err
  5523. }
  5524. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5525. info := models.WarehousingInfo{}
  5526. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5527. return info, err
  5528. }
  5529. func CreateWarehouseOut(out models.WarehouseOut) error {
  5530. err := XTWriteDB().Create(&out).Error
  5531. return err
  5532. }
  5533. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5534. out := models.WarehouseOut{}
  5535. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5536. return out, err
  5537. }
  5538. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5539. err := XTWriteDB().Create(&info).Error
  5540. return err
  5541. }
  5542. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5543. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5544. return err
  5545. }
  5546. func UpdateWareInfoById(id int64, count int64) error {
  5547. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5548. return err
  5549. }
  5550. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5551. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5552. return err
  5553. }
  5554. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5555. info := models.CancelStockInfo{}
  5556. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5557. return info, err
  5558. }
  5559. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5560. info := models.CancelStockInfo{}
  5561. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5562. return info, err
  5563. }
  5564. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5565. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5566. return info, err
  5567. }
  5568. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5569. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5570. return info, err
  5571. }
  5572. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5573. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5574. return err
  5575. }
  5576. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5577. drugFlow := models.DrugFlow{}
  5578. 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
  5579. return err
  5580. }
  5581. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5582. drugFlow := models.DrugFlow{}
  5583. 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
  5584. return err
  5585. }
  5586. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5587. stock := models.DrugCancelStock{}
  5588. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5589. return stock, err
  5590. }
  5591. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5592. // stock := models.DrugCancelStock{}
  5593. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5594. // return stock, err
  5595. //}
  5596. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5597. db := XTReadDB().Model(&info)
  5598. if warehouse_info_id > 0 {
  5599. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5600. }
  5601. if drug_id > 0 {
  5602. db = db.Where("drug_id = ? and status =1", drug_id)
  5603. }
  5604. err = db.Find(&info).Error
  5605. return info, err
  5606. }
  5607. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5608. // db := tx.Model(&info)
  5609. // if warehouse_info_id > 0 {
  5610. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5611. // }
  5612. // if drug_id > 0 {
  5613. // db = db.Where("drug_id = ? and status =1", drug_id)
  5614. // }
  5615. // err = db.Find(&info).Error
  5616. // return info, err
  5617. //}
  5618. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5619. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5620. return info, err
  5621. }
  5622. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5623. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5624. // return info, err
  5625. //}
  5626. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5627. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5628. return config, err
  5629. }
  5630. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5631. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5632. return config, err
  5633. }
  5634. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5635. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5636. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5637. fmt.Println(table)
  5638. likeKey := "%" + keyword + "%"
  5639. if orgid > 0 {
  5640. db = db.Where("x.org_id = ?", orgid)
  5641. }
  5642. if storehouse_id > 0 {
  5643. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5644. }
  5645. 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
  5646. return info, err
  5647. }
  5648. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5649. err := XTWriteDB().Create(&adjustPrice).Error
  5650. return err
  5651. }
  5652. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5653. likeKey := "%" + keyword + "%"
  5654. offset := (page - 1) * limit
  5655. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5656. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5657. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5658. fmt.Println(table, tab)
  5659. if startime > 0 {
  5660. db = db.Where("x.start_time>=?", startime)
  5661. }
  5662. if endtime > 0 {
  5663. db = db.Where("x.start_time<=?", endtime)
  5664. }
  5665. if len(keyword) > 0 {
  5666. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5667. }
  5668. if orgid > 0 {
  5669. db = db.Where("x.user_org_id = ?", orgid)
  5670. }
  5671. 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
  5672. return list, total, err
  5673. }
  5674. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5675. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5676. return err
  5677. }
  5678. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5679. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5680. return err
  5681. }
  5682. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5683. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5684. return info, err
  5685. }
  5686. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5687. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5688. return adjust, err
  5689. }
  5690. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5691. err := XTWriteDB().Create(&price).Error
  5692. return err
  5693. }
  5694. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5695. likeKey := "%" + keyword + "%"
  5696. offset := (page - 1) * limit
  5697. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5698. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5699. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5700. fmt.Println(table, tab)
  5701. if startime > 0 {
  5702. db = db.Where("x.ctime>=?", startime)
  5703. }
  5704. if endtime > 0 {
  5705. db = db.Where("x.ctime<=?", endtime)
  5706. }
  5707. if len(keyword) > 0 {
  5708. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5709. }
  5710. if orgid > 0 {
  5711. db = db.Where("x.user_org_id = ?", orgid)
  5712. }
  5713. 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
  5714. return adjust, total, err
  5715. }
  5716. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5717. likeKey := "%" + keyword + "%"
  5718. offset := (page - 1) * limit
  5719. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5720. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5721. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5722. fmt.Println(table, tab)
  5723. if len(keyword) > 0 {
  5724. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5725. }
  5726. if orgid > 0 {
  5727. db = db.Where("x.user_org_id = ?", orgid)
  5728. }
  5729. if startime > 0 {
  5730. db = db.Where("x.ctime >=?", startime)
  5731. }
  5732. if endtime > 0 {
  5733. db = db.Where("x.ctime <=?", endtime)
  5734. }
  5735. if storehouse_id > 0 {
  5736. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5737. }
  5738. 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
  5739. return adjust, total, err
  5740. }
  5741. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5742. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5743. if inventory_type == 0 {
  5744. db = db.Where("x.inventory_type = 4")
  5745. }
  5746. if inventory_type > 0 {
  5747. db = db.Where("x.inventory_type = ?", inventory_type)
  5748. }
  5749. if len(keyword) > 0 {
  5750. likeKey := "%" + keyword + "%"
  5751. db = db.Where("x.warehousing_order like ?", likeKey)
  5752. }
  5753. if orgid > 0 {
  5754. db = db.Where("x.user_org_id = ?", orgid)
  5755. }
  5756. 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
  5757. return adjust, err
  5758. }
  5759. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5760. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5761. if orgid > 0 {
  5762. db = db.Where("x.user_org_id = ?", orgid)
  5763. }
  5764. 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
  5765. return adjust, err
  5766. }
  5767. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5768. 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
  5769. return err
  5770. }
  5771. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5772. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5773. return report, err
  5774. }
  5775. func CreateInentory(inventory models.XtStockInventory) error {
  5776. err = XTWriteDB().Create(&inventory).Error
  5777. return err
  5778. }
  5779. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5780. likeKey := "%" + keyword + "%"
  5781. offset := (page - 1) * limit
  5782. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5783. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5784. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5785. fmt.Println(table, tab)
  5786. if startime > 0 {
  5787. db = db.Where("x.ctime>=?", startime)
  5788. }
  5789. if endtime > 0 {
  5790. db = db.Where("x.ctime<=?", endtime)
  5791. }
  5792. if len(keyword) > 0 {
  5793. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5794. }
  5795. if orgid > 0 {
  5796. db = db.Where("x.user_org_id = ?", orgid)
  5797. }
  5798. if inventory_status > 0 {
  5799. db = db.Where("checker_status = ?", inventory_status)
  5800. }
  5801. 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
  5802. return adjust, total, err
  5803. }
  5804. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5805. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5806. return err
  5807. }
  5808. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5809. info := models.XtStockAdjustPrice{}
  5810. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5811. return info, err
  5812. }
  5813. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5814. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5815. return list, err
  5816. }
  5817. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5818. info := models.WarehousingInfo{}
  5819. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5820. return info, err
  5821. }
  5822. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5823. info := models.WarehousingInfo{}
  5824. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5825. //return info, err
  5826. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5827. if id > 0 {
  5828. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5829. }
  5830. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5831. return info, err
  5832. }
  5833. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5834. info := models.WarehousingInfo{}
  5835. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5836. if id > 0 {
  5837. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5838. }
  5839. if storehouse_id > 0 {
  5840. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5841. }
  5842. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5843. return info, err
  5844. }
  5845. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5846. 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
  5847. return err
  5848. }
  5849. func DeleteStockPrice(id int64) error {
  5850. info := models.XtStockAdjustPrice{}
  5851. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5852. return err
  5853. }
  5854. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5855. report := models.XtStockReportPrice{}
  5856. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5857. return report, err
  5858. }
  5859. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5860. 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
  5861. return err
  5862. }
  5863. func DeleteDamage(id int64) error {
  5864. adjust := models.XtStockReportPrice{}
  5865. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5866. return err
  5867. }
  5868. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5869. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5870. return adjust, err
  5871. }
  5872. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5873. inventory := models.XtStockInventory{}
  5874. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5875. return inventory, err
  5876. }
  5877. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5878. 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
  5879. return err
  5880. }
  5881. func DeleteInventory(id int64) error {
  5882. inventory := models.XtStockInventory{}
  5883. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5884. return err
  5885. }
  5886. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5887. likeKey := "%" + keyword + "%"
  5888. offset := (page - 1) * limit
  5889. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5890. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5891. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5892. fmt.Println(table, tab)
  5893. if len(keyword) > 0 {
  5894. db = db.Where("x.good_name like ?", likeKey)
  5895. }
  5896. if orgid > 0 {
  5897. db = db.Where("x.user_org_id = ?", orgid)
  5898. }
  5899. if storehouse_id > 0 {
  5900. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5901. }
  5902. 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
  5903. return list, total, err
  5904. }
  5905. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5906. likeKey := "%" + keyword + "%"
  5907. offset := (page - 1) * limit
  5908. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5909. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5910. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5911. fmt.Println(table, tab)
  5912. if len(keyword) > 0 {
  5913. db = db.Where("x.good_name like ?", likeKey)
  5914. }
  5915. if orgid > 0 {
  5916. db = db.Where("x.user_org_id = ?", orgid)
  5917. }
  5918. if storehouse_id > 0 {
  5919. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5920. }
  5921. if len(ids) > 0 {
  5922. db = db.Where("x.id in(?)", ids)
  5923. }
  5924. 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
  5925. return list, total, err
  5926. }
  5927. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5928. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5929. return list, err
  5930. }
  5931. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5932. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5933. return list, err
  5934. }
  5935. func DeleteDrugPrice(id int64) error {
  5936. info := models.XtDrugAdjustPrice{}
  5937. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5938. return err
  5939. }
  5940. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5941. info := models.WarehousingInfo{}
  5942. db := XTReadDB().Model(&info).Where("status = 1")
  5943. if goodid > 0 {
  5944. db = db.Where("good_id = ?", goodid)
  5945. }
  5946. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5947. return info, err
  5948. }
  5949. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5950. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5951. return err
  5952. }
  5953. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5954. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5955. return err
  5956. }
  5957. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5958. err := XTWriteDB().Create(&record).Error
  5959. return err
  5960. }
  5961. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5962. info := models.WarehousingInfo{}
  5963. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5964. //return info, err
  5965. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5966. if id > 0 {
  5967. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5968. }
  5969. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5970. return info, err
  5971. }
  5972. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5973. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5974. return info, err
  5975. }
  5976. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5977. info := models.DrugWarehouseInfo{}
  5978. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5979. return info, err
  5980. }
  5981. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5982. 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
  5983. return err
  5984. }
  5985. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5986. 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
  5987. return err
  5988. }
  5989. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5990. 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
  5991. return info, err
  5992. }
  5993. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  5994. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5995. return err
  5996. }
  5997. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  5998. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  5999. return err
  6000. }
  6001. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  6002. db := XTReadDB().Model(&list).Where(" status = 1")
  6003. if good_id > 0 {
  6004. db = db.Where("good_id = ?", good_id)
  6005. }
  6006. if len(keyword) > 0 {
  6007. likeKey := "%" + keyword + "%"
  6008. db = db.Where("warehousing_order like ?", likeKey)
  6009. }
  6010. if inventory_type > 0 {
  6011. db = db.Where("inventory_type = ?", inventory_type)
  6012. }
  6013. if inventory_type == 0 {
  6014. db = db.Where("inventory_type = 4")
  6015. }
  6016. err = db.Find(&list).Error
  6017. return list, err
  6018. }
  6019. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  6020. flow := models.DrugFlow{}
  6021. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  6022. if err == gorm.ErrRecordNotFound {
  6023. return nil, err
  6024. }
  6025. if err != nil {
  6026. return nil, err
  6027. }
  6028. return &flow, nil
  6029. }
  6030. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  6031. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  6032. return info, err
  6033. }
  6034. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  6035. info := models.XtDrugWarehouseInfo{}
  6036. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  6037. return info, err
  6038. }
  6039. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  6040. info := models.WarehousingInfo{}
  6041. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  6042. return info, err
  6043. }
  6044. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  6045. flow := models.VmStockFlow{}
  6046. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  6047. if err == gorm.ErrRecordNotFound {
  6048. return nil, err
  6049. }
  6050. if err != nil {
  6051. return nil, err
  6052. }
  6053. return &flow, nil
  6054. }
  6055. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  6056. info := models.GoodInfo{}
  6057. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  6058. return info, err
  6059. }
  6060. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  6061. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  6062. return err
  6063. }
  6064. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  6065. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  6066. return err
  6067. }
  6068. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  6069. 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
  6070. return err
  6071. }
  6072. func UpdateNewWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  6073. 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
  6074. if err != nil {
  6075. tx.Rollback()
  6076. return err
  6077. }
  6078. return err
  6079. }
  6080. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6081. 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
  6082. return err
  6083. }
  6084. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6085. 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
  6086. return err
  6087. }
  6088. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  6089. 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
  6090. return err
  6091. }
  6092. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  6093. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  6094. return err
  6095. }
  6096. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  6097. if orgid == 10653 {
  6098. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0 and x.stock_count>0")
  6099. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6100. fmt.Println(table)
  6101. if orgid > 0 {
  6102. db = db.Where("x.org_id = ?", orgid)
  6103. }
  6104. err = db.Select("x.id,x.good_id,x.number,x.number,x.product_date,x.expiry_date,x.warehousing_unit,x.dealer,x.manufacturer,t.good_name,t.specification_name,t.packing_unit,t.remark").Joins("left join xt_good_information as t on t.id = x.good_id").Group("x.good_id,x.number").Scan(&info).Error
  6105. return info, err
  6106. } else {
  6107. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0")
  6108. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6109. fmt.Println(table)
  6110. if orgid > 0 {
  6111. db = db.Where("x.org_id = ?", orgid)
  6112. }
  6113. err = db.Select("x.id,x.good_id,x.number,x.number,x.product_date,x.expiry_date,x.warehousing_unit,x.dealer,x.manufacturer,t.good_name,t.specification_name,t.packing_unit,t.remark").Joins("left join xt_good_information as t on t.id = x.good_id").Group("x.good_id,x.number").Scan(&info).Error
  6114. return info, err
  6115. }
  6116. }
  6117. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  6118. warehouseInfo := models.XtDrugWarehouseInfo{}
  6119. 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
  6120. return err
  6121. }
  6122. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  6123. err := XTWriteDB().Create(&info).Error
  6124. return err
  6125. }
  6126. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  6127. err := tx.Create(&info).Error
  6128. if err != nil {
  6129. tx.Rollback()
  6130. return err
  6131. }
  6132. return err
  6133. }
  6134. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  6135. flow := models.VmStockFlow{}
  6136. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  6137. if err == gorm.ErrRecordNotFound {
  6138. return nil, err
  6139. }
  6140. if err != nil {
  6141. return nil, err
  6142. }
  6143. return &flow, nil
  6144. }
  6145. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  6146. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  6147. return err
  6148. }
  6149. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  6150. 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
  6151. return err
  6152. }
  6153. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  6154. flow := models.DrugWarehouseOutInfo{}
  6155. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6156. return flow, err
  6157. }
  6158. func GetLastNewWarehouseOutInfoById(drugid int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  6159. flow := models.DrugWarehouseOutInfo{}
  6160. err := tx.Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6161. if err != gorm.ErrRecordNotFound {
  6162. if err != nil {
  6163. tx.Rollback()
  6164. return flow, err
  6165. }
  6166. }
  6167. return flow, err
  6168. }
  6169. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  6170. info := models.WarehouseOutInfo{}
  6171. 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
  6172. return info, err
  6173. }
  6174. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  6175. 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
  6176. return err
  6177. }
  6178. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  6179. 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
  6180. return err
  6181. }
  6182. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  6183. info := models.DrugWarehouseOutInfo{}
  6184. 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
  6185. if err == gorm.ErrRecordNotFound {
  6186. return nil, err
  6187. }
  6188. if err != nil {
  6189. return nil, err
  6190. }
  6191. return &info, nil
  6192. }
  6193. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  6194. drugFlow := models.DrugFlow{}
  6195. 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
  6196. if err == gorm.ErrRecordNotFound {
  6197. return nil, err
  6198. }
  6199. if err != nil {
  6200. return nil, err
  6201. }
  6202. return &drugFlow, nil
  6203. }
  6204. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  6205. 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
  6206. return err
  6207. }
  6208. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  6209. 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
  6210. return err
  6211. }
  6212. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  6213. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6214. return detail, err
  6215. }
  6216. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  6217. detail := models.DrugAutomaticReduceDetail{}
  6218. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6219. if err == gorm.ErrRecordNotFound {
  6220. return nil, err
  6221. }
  6222. if err != nil {
  6223. return nil, err
  6224. }
  6225. return &detail, nil
  6226. }
  6227. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  6228. 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
  6229. return err
  6230. }
  6231. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  6232. 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
  6233. return err
  6234. }
  6235. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  6236. info := models.DrugWarehouseInfo{}
  6237. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6238. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6239. return info, err
  6240. }
  6241. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  6242. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6243. return err
  6244. }
  6245. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  6246. info := models.DrugWarehouseInfo{}
  6247. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6248. return err
  6249. }
  6250. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  6251. info := models.DrugCancelStockInfo{}
  6252. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6253. return info, err
  6254. }
  6255. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  6256. info := models.DrugWarehouseInfo{}
  6257. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  6258. return err
  6259. }
  6260. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  6261. info := models.DrugWarehouseInfo{}
  6262. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  6263. return err
  6264. }
  6265. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  6266. 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
  6267. return advice, err
  6268. }
  6269. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  6270. 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
  6271. if err != gorm.ErrRecordNotFound {
  6272. if err != nil {
  6273. tx.Rollback()
  6274. return advice, err
  6275. }
  6276. }
  6277. return advice, err
  6278. }
  6279. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6280. 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
  6281. return info, err
  6282. }
  6283. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  6284. 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
  6285. if err != gorm.ErrRecordNotFound {
  6286. if err != nil {
  6287. tx.Rollback()
  6288. return info, err
  6289. }
  6290. }
  6291. return info, err
  6292. }
  6293. // 耗材加库存
  6294. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6295. info := models.GoodInfo{}
  6296. 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
  6297. return info, err
  6298. }
  6299. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6300. info := models.GoodInfo{}
  6301. 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
  6302. 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
  6303. return info, err
  6304. }
  6305. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6306. info := models.GoodInfo{}
  6307. 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
  6308. return info, err
  6309. }
  6310. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6311. info := models.GoodInfo{}
  6312. 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
  6313. 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
  6314. return info, err
  6315. }
  6316. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  6317. info := models.GoodInfo{}
  6318. 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
  6319. return info, err
  6320. }
  6321. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6322. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6323. return info, err
  6324. }
  6325. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6326. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6327. return info, err
  6328. }
  6329. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  6330. info := models.WarehousingInfo{}
  6331. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  6332. return info, err
  6333. }
  6334. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  6335. info := models.WarehousingInfo{}
  6336. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  6337. return info, err
  6338. }
  6339. // 药品加库存
  6340. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6341. info := models.BaseDrugLib{}
  6342. 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
  6343. return info, err
  6344. }
  6345. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6346. info := models.BaseDrugLib{}
  6347. 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
  6348. 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
  6349. return info, err
  6350. }
  6351. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6352. info := models.BaseDrugLib{}
  6353. 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
  6354. return info, err
  6355. }
  6356. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6357. info := models.BaseDrugLib{}
  6358. 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
  6359. 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
  6360. return info, err
  6361. }
  6362. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  6363. info := models.BaseDrugLib{}
  6364. 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
  6365. 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
  6366. return info, err
  6367. }
  6368. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6369. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6370. return info, err
  6371. }
  6372. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6373. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6374. return info, err
  6375. }
  6376. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  6377. info := models.WarehouseOutInfo{}
  6378. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6379. return info, err
  6380. }
  6381. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  6382. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  6383. return out, err
  6384. }
  6385. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  6386. info := models.WarehousingInfo{}
  6387. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6388. return info, err
  6389. }
  6390. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  6391. err = XTReadDB().Where("status = 1").Find(&template).Error
  6392. return template, err
  6393. }
  6394. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  6395. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  6396. return good, err
  6397. }
  6398. func GetMobileGoodList(org_id int64) (list []*models.GoodInformationThirty, err error) {
  6399. err = XTReadDB().Where("org_id = ? and status =1", org_id).Find(&list).Error
  6400. return list, err
  6401. }
  6402. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  6403. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  6404. return project, err
  6405. }
  6406. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6407. info := models.GoodInfo{}
  6408. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6409. return info, err
  6410. }
  6411. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  6412. db := XTReadDB().Where("status = 1")
  6413. if good_id > 0 {
  6414. db = db.Where("good_id = ?", good_id)
  6415. }
  6416. err = db.Find(&info).Error
  6417. return info, err
  6418. }
  6419. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6420. db := XTReadDB().Model(&info).Where("status = 1")
  6421. if drug_id > 0 {
  6422. db = db.Where("drug_id = ?", drug_id)
  6423. }
  6424. err = db.Find(&info).Error
  6425. return info, err
  6426. }
  6427. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6428. lib := models.BaseDrugLib{}
  6429. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6430. return lib, err
  6431. }
  6432. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  6433. info := models.Warehousing{}
  6434. 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
  6435. if err == gorm.ErrRecordNotFound {
  6436. return nil, err
  6437. }
  6438. if err != nil {
  6439. return nil, err
  6440. }
  6441. return &info, nil
  6442. }
  6443. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  6444. info := models.Warehousing{}
  6445. 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
  6446. return info, err
  6447. }
  6448. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  6449. out := models.DrugWarehouseOut{}
  6450. 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
  6451. if err == gorm.ErrRecordNotFound {
  6452. return nil, err
  6453. }
  6454. if err != nil {
  6455. return nil, err
  6456. }
  6457. return &out, nil
  6458. }
  6459. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  6460. info := models.DrugWarehouseOut{}
  6461. 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
  6462. return info, err
  6463. }
  6464. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  6465. info := models.DrugWarehouse{}
  6466. 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
  6467. if err == gorm.ErrRecordNotFound {
  6468. return nil, err
  6469. }
  6470. if err != nil {
  6471. return nil, err
  6472. }
  6473. return &info, nil
  6474. }
  6475. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  6476. err := XTWriteDB().Create(&warehouse).Error
  6477. return err
  6478. }
  6479. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  6480. info := models.DrugWarehouse{}
  6481. 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
  6482. return info, err
  6483. }
  6484. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6485. info := models.GoodInfo{}
  6486. 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
  6487. 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
  6488. return info, err
  6489. }
  6490. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6491. info := models.GoodInfo{}
  6492. 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
  6493. 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
  6494. return info, err
  6495. }
  6496. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6497. tx := XTWriteDB().Begin()
  6498. 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
  6499. if err != nil {
  6500. tx.Rollback()
  6501. return err
  6502. }
  6503. tx.Commit()
  6504. return err
  6505. }
  6506. func UpdateNewMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64, tx *gorm.DB) error {
  6507. 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
  6508. if err != nil {
  6509. tx.Rollback()
  6510. return err
  6511. }
  6512. return err
  6513. }
  6514. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6515. tx := XTWriteDB().Begin()
  6516. 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
  6517. if err != nil {
  6518. tx.Rollback()
  6519. return err
  6520. }
  6521. tx.Commit()
  6522. return err
  6523. }
  6524. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6525. // 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
  6526. // return err
  6527. //}
  6528. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6529. 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
  6530. return err
  6531. }
  6532. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6533. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6534. table := XTReadDB().Table("xt_base_drug as x d")
  6535. fmt.Println(table)
  6536. if startime > 0 {
  6537. db = db.Where("x.advice_date >= ?", startime)
  6538. }
  6539. if endtime > 0 {
  6540. db = db.Where("x.advice_date <= ?", endtime)
  6541. }
  6542. if orgid > 0 {
  6543. db = db.Where("x.user_org_id = ?", orgid)
  6544. }
  6545. if len(keyword) > 0 {
  6546. likeKey := "%" + keyword + "%"
  6547. 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)
  6548. }
  6549. offset := (page - 1) * limit
  6550. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6551. return adviceinfo, total, err
  6552. }
  6553. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6554. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6555. table := XTReadDB().Table("xt_base_drug as x d")
  6556. fmt.Println(table)
  6557. if startime > 0 {
  6558. db = db.Where("x.advice_date >= ?", startime)
  6559. }
  6560. if endtime > 0 {
  6561. db = db.Where("x.advice_date <= ?", endtime)
  6562. }
  6563. if orgid > 0 {
  6564. db = db.Where("x.user_org_id = ?", orgid)
  6565. }
  6566. if len(keyword) > 0 {
  6567. likeKey := "%" + keyword + "%"
  6568. 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)
  6569. }
  6570. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6571. return adviceinfo, total, err
  6572. }
  6573. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6574. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6575. return drug, err
  6576. }
  6577. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6578. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6579. table := XTReadDB().Table("xt_good_information as d")
  6580. fmt.Println(table)
  6581. if startime > 0 {
  6582. db = db.Where("x.record_date >= ?", startime)
  6583. }
  6584. if endtime > 0 {
  6585. db = db.Where("x.record_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.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")
  6593. }
  6594. offset := (page - 1) * limit
  6595. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6596. return project, total, err
  6597. }
  6598. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6599. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6600. table := XTReadDB().Table("xt_good_information as d")
  6601. fmt.Println(table)
  6602. if startime > 0 {
  6603. db = db.Where("x.record_date >= ?", startime)
  6604. }
  6605. if endtime > 0 {
  6606. db = db.Where("x.record_date <= ?", endtime)
  6607. }
  6608. if orgid > 0 {
  6609. db = db.Where("x.user_org_id = ?", orgid)
  6610. }
  6611. if len(keyword) > 0 {
  6612. likeKey := "%" + keyword + "%"
  6613. 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")
  6614. }
  6615. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6616. return project, total, err
  6617. }
  6618. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6619. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6620. return good, err
  6621. }
  6622. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6623. setting := models.XtStockSetting{}
  6624. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6625. if err == gorm.ErrRecordNotFound {
  6626. return nil, err
  6627. }
  6628. if err != nil {
  6629. return nil, err
  6630. }
  6631. return &setting, nil
  6632. }
  6633. func CreateStockSetting(setting models.XtStockSetting) error {
  6634. err := XTWriteDB().Create(&setting).Error
  6635. return err
  6636. }
  6637. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6638. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6639. return err
  6640. }
  6641. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6642. setting := models.XtStockSetting{}
  6643. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6644. return setting, err
  6645. }
  6646. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6647. detail := models.BloodAutomaticReduceDetail{}
  6648. 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
  6649. return detail, err
  6650. }
  6651. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6652. detail := models.WarehouseOutInfoNight{}
  6653. 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
  6654. return detail, err
  6655. }
  6656. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6657. setting := models.XtAdviceSetting{}
  6658. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6659. if err == gorm.ErrRecordNotFound {
  6660. return nil, err
  6661. }
  6662. if err != nil {
  6663. return nil, err
  6664. }
  6665. return &setting, nil
  6666. }
  6667. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6668. err := XTWriteDB().Create(&setting).Error
  6669. return err
  6670. }
  6671. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6672. 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
  6673. return err
  6674. }
  6675. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6676. setting := models.XtAdviceSetting{}
  6677. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6678. return setting, err
  6679. }
  6680. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6681. config := models.XtPrescriptionConfig{}
  6682. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6683. if err == gorm.ErrRecordNotFound {
  6684. return nil, err
  6685. }
  6686. if err != nil {
  6687. return nil, err
  6688. }
  6689. return &config, nil
  6690. }
  6691. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6692. err := XTWriteDB().Create(&prescriptionConfig).Error
  6693. return err
  6694. }
  6695. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6696. 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
  6697. return err
  6698. }
  6699. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6700. config := models.XtPrescriptionConfig{}
  6701. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6702. return config, err
  6703. }
  6704. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6705. var deliver_number int64 = 0
  6706. var stock_number int64 = 0
  6707. deliver_number = maxNumber
  6708. // 根据先进先出原则,查询最先入库的批次,进行出库
  6709. // 如果没有对应的库存,则报错
  6710. //开启事物
  6711. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6712. if err != nil {
  6713. return err
  6714. }
  6715. // 将该批次的剩余库存数量转换为拆零数量
  6716. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6717. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6718. if stock_number >= deliver_number {
  6719. var maxNumber int64 = 0
  6720. var minNumber int64 = 0
  6721. var stock_max_number int64 = 0
  6722. stock_max_number = warehouse.StockMaxNumber
  6723. maxNumber = deliver_number / drup.MinNumber
  6724. minNumber = deliver_number % drup.MinNumber
  6725. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6726. minNumber = maxNumber
  6727. }
  6728. if drup.MaxUnit != drup.MinUnit {
  6729. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6730. return errors.New("库存数量不足")
  6731. }
  6732. }
  6733. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6734. if warehouse.StockMaxNumber < 0 {
  6735. warehouse.StockMaxNumber = 0
  6736. }
  6737. if warehouse.StockMinNumber < 0 {
  6738. warehouse.StockMinNumber = 0
  6739. }
  6740. warehouse.Mtime = time.Now().Unix()
  6741. if warehouse.StockMinNumber < minNumber {
  6742. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6743. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6744. } else {
  6745. if minNumber > 0 {
  6746. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6747. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6748. } else {
  6749. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6750. }
  6751. }
  6752. if minNumber == 0 && maxNumber != 1 {
  6753. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6754. }
  6755. }
  6756. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6757. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6758. if warehouse.StockMinNumber == 0 {
  6759. warehouse.StockMaxNumber = stock_max_number
  6760. }
  6761. }
  6762. if drup.MaxUnit != drup.MinUnit {
  6763. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6764. return errors.New("库存数量不足")
  6765. }
  6766. }
  6767. if warehouse.StockMinNumber <= 0 {
  6768. warehouse.StockMinNumber = 0
  6769. }
  6770. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6771. fmt.Println(errThree)
  6772. //查询默认仓库
  6773. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6774. //查询默认仓库剩余多少库存
  6775. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6776. var sum_count int64
  6777. var sum_in_count int64
  6778. for _, it := range list {
  6779. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6780. if it.MaxUnit == baseDrug.MaxUnit {
  6781. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6782. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6783. sum_count += it.StockMaxNumber + it.StockMinNumber
  6784. sum_in_count += it.WarehousingCount
  6785. }
  6786. }
  6787. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6788. return nil
  6789. } else {
  6790. warehouse.StockMaxNumber = 0
  6791. warehouse.StockMinNumber = 0
  6792. warehouse.Mtime = time.Now().Unix()
  6793. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6794. fmt.Println(errThree)
  6795. //查询默认仓库
  6796. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6797. //查询默认仓库剩余多少库存
  6798. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6799. var sum_count int64
  6800. var sum_in_count int64
  6801. for _, it := range list {
  6802. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6803. if it.MaxUnit == baseDrug.MaxUnit {
  6804. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6805. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6806. sum_count += it.StockMaxNumber + it.StockMinNumber
  6807. sum_in_count += it.WarehousingCount
  6808. }
  6809. }
  6810. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6811. if errThree != nil {
  6812. return errThree
  6813. }
  6814. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6815. prescribingNumber_two_temp := deliver_number - stock_number
  6816. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6817. }
  6818. return
  6819. }
  6820. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6821. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6822. return err
  6823. }
  6824. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6825. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6826. return err
  6827. }
  6828. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6829. config := models.DrugOutConfig{}
  6830. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6831. if err == gorm.ErrRecordNotFound {
  6832. return nil, err
  6833. }
  6834. if err != nil {
  6835. return nil, err
  6836. }
  6837. return &config, nil
  6838. }
  6839. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6840. config := models.DrugOutConfig{}
  6841. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6842. return config, err
  6843. }
  6844. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6845. config := models.XtHisConfig{}
  6846. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6847. return config, err
  6848. }
  6849. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6850. err := XTWriteDB().Create(&config).Error
  6851. return err
  6852. }
  6853. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6854. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6855. return err
  6856. }
  6857. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6858. config := models.DrugOutConfig{}
  6859. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6860. return config, err
  6861. }
  6862. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6863. config := models.HisSettleStockConfig{}
  6864. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6865. return config, err
  6866. }
  6867. func UpdatedWarehouseOut(id int64) error {
  6868. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6869. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6870. return err
  6871. }
  6872. func UpdatedNewWarehouseOut(id int64, tx *gorm.DB) error {
  6873. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6874. if err != nil {
  6875. tx.Rollback()
  6876. return err
  6877. }
  6878. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6879. if err != nil {
  6880. tx.Rollback()
  6881. return err
  6882. }
  6883. return err
  6884. }
  6885. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6886. tx := XTWriteDB().Begin()
  6887. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6888. if err != nil {
  6889. tx.Rollback()
  6890. return err
  6891. }
  6892. tx.Commit()
  6893. return err
  6894. }
  6895. func UpdatedNewSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6896. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6897. if err != nil {
  6898. tx.Rollback()
  6899. return err
  6900. }
  6901. return err
  6902. }
  6903. func GetLastGoodWarehouseOutInfo(orgid int64, id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  6904. err = tx.Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6905. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6906. }).Find(&info).Error
  6907. if err != nil {
  6908. tx.Rollback()
  6909. return info, err
  6910. }
  6911. return info, err
  6912. }
  6913. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6914. info := models.WarehouseOutInfo{}
  6915. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6916. return info, err
  6917. }
  6918. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6919. 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
  6920. if err != nil {
  6921. tx.Rollback()
  6922. return err
  6923. }
  6924. return err
  6925. }
  6926. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6927. 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
  6928. return err
  6929. }
  6930. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6931. out := models.WarehouseOut{}
  6932. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6933. return out, err
  6934. }
  6935. func GetLastGoodWarehouseOutTwentyOne(orgid int64, tx *gorm.DB) (models.WarehouseOut, error) {
  6936. out := models.WarehouseOut{}
  6937. err := tx.Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6938. if err != gorm.ErrRecordNotFound {
  6939. if err != nil {
  6940. tx.Rollback()
  6941. return out, err
  6942. }
  6943. }
  6944. return out, err
  6945. }
  6946. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6947. ut := writeDb.Begin()
  6948. 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
  6949. if err != nil {
  6950. ut.Rollback()
  6951. return err
  6952. }
  6953. ut.Commit()
  6954. return err
  6955. }
  6956. func ModifyGoodOutInfoLog(good_id int64, id int64, tx *gorm.DB) (models.XtGoodWarehouseOutInfoLog, error) {
  6957. outInfoLog := models.XtGoodWarehouseOutInfoLog{}
  6958. err := tx.Model(&outInfoLog).Where("good_id = ? and warehouse_out_info_id = ?", good_id, id).Updates(map[string]interface{}{"status": 0}).Error
  6959. if err != nil {
  6960. tx.Rollback()
  6961. return outInfoLog, err
  6962. }
  6963. return outInfoLog, err
  6964. }
  6965. func UpdateaNewGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64, tx *gorm.DB) error {
  6966. 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
  6967. if err != nil {
  6968. tx.Rollback()
  6969. return err
  6970. }
  6971. return err
  6972. }
  6973. func ModifyGoodWarehouseOut(id int64) error {
  6974. ut := writeDb.Begin()
  6975. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6976. if err != nil {
  6977. ut.Rollback()
  6978. return err
  6979. }
  6980. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6981. if err != nil {
  6982. ut.Rollback()
  6983. return err
  6984. }
  6985. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6986. if err != nil {
  6987. ut.Rollback()
  6988. return err
  6989. }
  6990. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6991. if err != nil {
  6992. ut.Rollback()
  6993. return err
  6994. }
  6995. ut.Commit()
  6996. return err
  6997. }
  6998. func ModifyNewGoodWarehouseOut(id int64, tx *gorm.DB) error {
  6999. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7000. if err != nil {
  7001. tx.Rollback()
  7002. return err
  7003. }
  7004. err = tx.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. tx.Rollback()
  7007. return err
  7008. }
  7009. err = tx.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. tx.Rollback()
  7012. return err
  7013. }
  7014. err = tx.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7015. if err != nil {
  7016. tx.Rollback()
  7017. return err
  7018. }
  7019. return err
  7020. }
  7021. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7022. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7023. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7024. }).Find(&info).Error
  7025. return info, err
  7026. }
  7027. func GetNewGoodWarehouseInfoById(waresing_id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7028. err = tx.Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7029. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7030. }).Find(&info).Error
  7031. if err != gorm.ErrRecordNotFound {
  7032. if err != nil {
  7033. tx.Rollback()
  7034. return info, err
  7035. }
  7036. }
  7037. return info, err
  7038. }
  7039. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  7040. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  7041. return info, err
  7042. }
  7043. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  7044. 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
  7045. return err
  7046. }
  7047. func UpdateNewWarehouseDetail(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  7048. 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
  7049. if err != nil {
  7050. tx.Rollback()
  7051. return err
  7052. }
  7053. return err
  7054. }
  7055. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  7056. tx := XTWriteDB().Begin()
  7057. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7058. if err != nil {
  7059. tx.Rollback()
  7060. return err
  7061. }
  7062. tx.Commit()
  7063. return err
  7064. }
  7065. func UpdateNewCheckWarehouseInfo(id int64, checker int64, tx *gorm.DB) error {
  7066. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7067. if err != nil {
  7068. tx.Rollback()
  7069. return err
  7070. }
  7071. return err
  7072. }
  7073. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  7074. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7075. return info, err
  7076. }
  7077. func GetNewWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7078. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7079. if err != gorm.ErrRecordNotFound {
  7080. if err != nil {
  7081. tx.Rollback()
  7082. return info, err
  7083. }
  7084. }
  7085. return info, err
  7086. }
  7087. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  7088. tx := XTWriteDB().Begin()
  7089. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7090. if err != nil {
  7091. tx.Rollback()
  7092. return err
  7093. }
  7094. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7095. if err != nil {
  7096. tx.Rollback()
  7097. return err
  7098. }
  7099. tx.Commit()
  7100. return err
  7101. }
  7102. func UpdateNewWarehouseInfoByIdListThree(count int64, id int64, tx *gorm.DB) error {
  7103. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7104. if err != nil {
  7105. tx.Rollback()
  7106. return err
  7107. }
  7108. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7109. if err != nil {
  7110. tx.Rollback()
  7111. return err
  7112. }
  7113. return err
  7114. }
  7115. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  7116. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7117. return info, err
  7118. }
  7119. func FindNewLastWarehousingSix(org_id int64, tx *gorm.DB) (info models.Warehousing, err error) {
  7120. err = tx.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7121. if err != gorm.ErrRecordNotFound {
  7122. if err != nil {
  7123. tx.Rollback()
  7124. return info, err
  7125. }
  7126. }
  7127. return info, err
  7128. }
  7129. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  7130. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7131. return info, err
  7132. }
  7133. func FindNewLastWarehousingSeven(org_id int64, tx *gorm.DB) (info models.DrugWarehouse, err error) {
  7134. err = tx.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7135. if err != gorm.ErrRecordNotFound {
  7136. if err != nil {
  7137. tx.Rollback()
  7138. return info, err
  7139. }
  7140. }
  7141. return info, err
  7142. }
  7143. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7144. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7145. return info, err
  7146. }
  7147. func FindNewDrugWarehouseInfoListById(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7148. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7149. if err != gorm.ErrRecordNotFound {
  7150. if err != nil {
  7151. tx.Rollback()
  7152. return info, err
  7153. }
  7154. }
  7155. return info, err
  7156. }
  7157. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  7158. err := XTWriteDB().Create(&info).Error
  7159. return err
  7160. }
  7161. func CreatedNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7162. err := tx.Create(&info).Error
  7163. if err != nil {
  7164. tx.Rollback()
  7165. return err
  7166. }
  7167. return err
  7168. }
  7169. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  7170. err := XTWriteDB().Save(&info).Error
  7171. return err
  7172. }
  7173. func UpdateNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7174. err := tx.Save(&info).Error
  7175. if err != nil {
  7176. tx.Rollback()
  7177. return err
  7178. }
  7179. return err
  7180. }
  7181. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  7182. tx := XTWriteDB().Begin()
  7183. 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
  7184. if err != nil {
  7185. tx.Rollback()
  7186. return err
  7187. }
  7188. tx.Commit()
  7189. return err
  7190. }
  7191. func CheckNewWarehousingInfo(id int64, orgid int64, checker int64, tx *gorm.DB) error {
  7192. 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
  7193. if err != nil {
  7194. tx.Rollback()
  7195. return err
  7196. }
  7197. return err
  7198. }
  7199. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  7200. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7201. return info, err
  7202. }
  7203. func GetNewWarehousingInfoByList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7204. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7205. if err != gorm.ErrRecordNotFound {
  7206. if err != nil {
  7207. tx.Rollback()
  7208. return info, err
  7209. }
  7210. }
  7211. return info, err
  7212. }
  7213. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  7214. tx := XTWriteDB().Begin()
  7215. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7216. if err != nil {
  7217. tx.Rollback()
  7218. return err
  7219. }
  7220. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7221. if err != nil {
  7222. tx.Rollback()
  7223. return err
  7224. }
  7225. tx.Commit()
  7226. return err
  7227. }
  7228. func AddNewDrugWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7229. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7230. if err != nil {
  7231. tx.Rollback()
  7232. return err
  7233. }
  7234. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7235. if err != nil {
  7236. tx.Rollback()
  7237. return err
  7238. }
  7239. return err
  7240. }
  7241. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  7242. tx := XTWriteDB().Begin()
  7243. 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
  7244. if err != nil {
  7245. tx.Rollback()
  7246. return err
  7247. }
  7248. tx.Commit()
  7249. return err
  7250. }
  7251. func UpdateNewWarehousingInfoFlow(id int64, orgid int64, tx *gorm.DB) error {
  7252. 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
  7253. if err != nil {
  7254. tx.Rollback()
  7255. return err
  7256. }
  7257. return err
  7258. }
  7259. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  7260. tx := XTWriteDB().Begin()
  7261. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7262. if err != nil {
  7263. tx.Rollback()
  7264. return err
  7265. }
  7266. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7267. tx.Commit()
  7268. return err
  7269. }
  7270. func AddNewDrugWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7271. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7272. if err != nil {
  7273. tx.Rollback()
  7274. return err
  7275. }
  7276. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7277. if err != nil {
  7278. tx.Rollback()
  7279. return err
  7280. }
  7281. return err
  7282. }
  7283. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  7284. tx := XTWriteDB().Begin()
  7285. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7286. if err != nil {
  7287. tx.Rollback()
  7288. return err
  7289. }
  7290. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7291. if err != nil {
  7292. tx.Rollback()
  7293. return err
  7294. }
  7295. tx.Commit()
  7296. return err
  7297. }
  7298. func ReturnNewCheckWarehouseingInfo(id int64, orgid int64, tx *gorm.DB) error {
  7299. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7300. if err != nil {
  7301. tx.Rollback()
  7302. return err
  7303. }
  7304. err = tx.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7305. if err != nil {
  7306. tx.Rollback()
  7307. return err
  7308. }
  7309. err = tx.Model(models.XtDrugWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", id).Update(map[string]interface{}{"status": 0}).Error
  7310. if err != nil {
  7311. tx.Rollback()
  7312. return err
  7313. }
  7314. return err
  7315. }
  7316. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  7317. tx := XTWriteDB().Begin()
  7318. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7319. if err != nil {
  7320. tx.Rollback()
  7321. return err
  7322. }
  7323. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7324. if err != nil {
  7325. tx.Rollback()
  7326. return err
  7327. }
  7328. tx.Commit()
  7329. return err
  7330. }
  7331. func AddNewDrugReturnWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7332. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7333. if err != nil {
  7334. tx.Rollback()
  7335. return err
  7336. }
  7337. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7338. if err != nil {
  7339. tx.Rollback()
  7340. return err
  7341. }
  7342. return err
  7343. }
  7344. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  7345. tx := XTWriteDB().Begin()
  7346. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7347. if err != nil {
  7348. tx.Rollback()
  7349. return err
  7350. }
  7351. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7352. if err != nil {
  7353. tx.Rollback()
  7354. return err
  7355. }
  7356. tx.Commit()
  7357. return err
  7358. }
  7359. func AddNewDrugReturnWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7360. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7361. if err != nil {
  7362. tx.Rollback()
  7363. return err
  7364. }
  7365. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7366. if err != nil {
  7367. tx.Rollback()
  7368. return err
  7369. }
  7370. return err
  7371. }
  7372. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  7373. 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
  7374. return err
  7375. }
  7376. func UpdateNewDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7377. 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
  7378. if err != nil {
  7379. tx.Rollback()
  7380. return err
  7381. }
  7382. return err
  7383. }
  7384. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  7385. 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
  7386. return err
  7387. }
  7388. func UpdatedNewDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7389. 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
  7390. if err != nil {
  7391. tx.Rollback()
  7392. return err
  7393. }
  7394. return err
  7395. }
  7396. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7397. 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
  7398. if err != gorm.ErrRecordNotFound {
  7399. return err
  7400. }
  7401. return err
  7402. }
  7403. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  7404. out := models.DrugWarehouseOut{}
  7405. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7406. return out, err
  7407. }
  7408. func GetNewDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7409. out := models.DrugWarehouseOut{}
  7410. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7411. if err != gorm.ErrRecordNotFound {
  7412. if err != nil {
  7413. tx.Rollback()
  7414. return out, err
  7415. }
  7416. }
  7417. return out, err
  7418. }
  7419. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7420. out := models.XtSelfDrugWarehouseOut{}
  7421. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7422. if err != gorm.ErrRecordNotFound {
  7423. if err != nil {
  7424. tx.Rollback()
  7425. return out, err
  7426. }
  7427. }
  7428. return out, err
  7429. }
  7430. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7431. out := models.XtSelfDrugWarehouseOut{}
  7432. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7433. if err != gorm.ErrRecordNotFound {
  7434. if err != nil {
  7435. tx.Rollback()
  7436. return out, err
  7437. }
  7438. }
  7439. return out, err
  7440. }
  7441. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7442. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7443. return info, err
  7444. }
  7445. func GetNewDrugWarehouseOutListById(warehouse_out_id int64, orgId int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  7446. err = tx.Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7447. if err != gorm.ErrRecordNotFound {
  7448. if err != nil {
  7449. tx.Rollback()
  7450. return info, err
  7451. }
  7452. }
  7453. return info, err
  7454. }
  7455. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  7456. tx := XTWriteDB().Begin()
  7457. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7458. if err != nil {
  7459. tx.Rollback()
  7460. return err
  7461. }
  7462. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7463. if err != nil {
  7464. tx.Rollback()
  7465. return err
  7466. }
  7467. tx.Commit()
  7468. return err
  7469. }
  7470. func UpdateNewCheckDrugOut(out models.DrugWarehouseOut, id int64, tx *gorm.DB) error {
  7471. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7472. if err != nil {
  7473. tx.Rollback()
  7474. return err
  7475. }
  7476. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7477. if err != nil {
  7478. tx.Rollback()
  7479. return err
  7480. }
  7481. return err
  7482. }
  7483. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  7484. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7485. return err
  7486. }
  7487. func UpdateNewCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64, tx *gorm.DB) error {
  7488. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7489. if err != nil {
  7490. tx.Rollback()
  7491. return err
  7492. }
  7493. return err
  7494. }
  7495. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  7496. out := models.DrugWarehouseOut{}
  7497. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  7498. return out, err
  7499. }
  7500. func GetNewDrugWarehouseOutByLastId(id int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7501. out := models.DrugWarehouseOut{}
  7502. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7503. if err != gorm.ErrRecordNotFound {
  7504. if err != nil {
  7505. tx.Rollback()
  7506. return out, err
  7507. }
  7508. }
  7509. return out, err
  7510. }
  7511. func UpdateDrugMaxNumber(count int64, id int64) error {
  7512. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7513. return err
  7514. }
  7515. func UpdateNewDrugMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7516. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7517. if err != gorm.ErrRecordNotFound {
  7518. if err != nil {
  7519. tx.Rollback()
  7520. return err
  7521. }
  7522. }
  7523. return err
  7524. }
  7525. func UpdateNewDrugMaxNumberOne(count int64, id int64, tx *gorm.DB) error {
  7526. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7527. if err != gorm.ErrRecordNotFound {
  7528. if err != nil {
  7529. tx.Rollback()
  7530. return err
  7531. }
  7532. }
  7533. return err
  7534. }
  7535. func UpdateDrugMinNumber(count int64, id int64) error {
  7536. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7537. return err
  7538. }
  7539. func UpdateNewDrugMinNumber(count int64, id int64, tx *gorm.DB) error {
  7540. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7541. if err != nil {
  7542. tx.Rollback()
  7543. return err
  7544. }
  7545. return err
  7546. }
  7547. func DeleteDrugOutFlow(id int64, orgid int64) error {
  7548. 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
  7549. return err
  7550. }
  7551. func DeleteNewDrugOutFlow(id int64, orgid int64, tx *gorm.DB) error {
  7552. 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
  7553. if err != nil {
  7554. tx.Rollback()
  7555. return err
  7556. }
  7557. return err
  7558. }
  7559. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  7560. 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
  7561. return outInfo, err
  7562. }
  7563. func GetNewWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.WarehouseOutInfo, err error) {
  7564. 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
  7565. if err != gorm.ErrRecordNotFound {
  7566. if err != nil {
  7567. tx.Rollback()
  7568. return outInfo, err
  7569. }
  7570. }
  7571. return outInfo, err
  7572. }
  7573. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7574. 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
  7575. return outInfo, err
  7576. }
  7577. func GetNewDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7578. 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
  7579. if err != gorm.ErrRecordNotFound {
  7580. if err != nil {
  7581. tx.Rollback()
  7582. return outInfo, err
  7583. }
  7584. }
  7585. return outInfo, err
  7586. }
  7587. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7588. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7589. return info, err
  7590. }
  7591. func GetNewAllStockInList(id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7592. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7593. if err != gorm.ErrRecordNotFound {
  7594. if err != nil {
  7595. tx.Rollback()
  7596. return info, err
  7597. }
  7598. }
  7599. return info, err
  7600. }
  7601. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7602. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7603. return info, err
  7604. }
  7605. func GetNewDrugWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7606. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7607. if err != gorm.ErrRecordNotFound {
  7608. if err != nil {
  7609. tx.Rollback()
  7610. return info, err
  7611. }
  7612. }
  7613. return info, err
  7614. }
  7615. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  7616. 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
  7617. return info, err
  7618. }
  7619. func GetNewStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7620. 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
  7621. if err != gorm.ErrRecordNotFound {
  7622. if err != nil {
  7623. tx.Rollback()
  7624. return info, err
  7625. }
  7626. }
  7627. return info, err
  7628. }
  7629. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  7630. 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
  7631. 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
  7632. return err
  7633. }
  7634. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  7635. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7636. return err
  7637. }
  7638. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  7639. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7640. return err
  7641. }
  7642. func UpdateNewGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7643. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7644. if err != gorm.ErrRecordNotFound {
  7645. if err != nil {
  7646. tx.Rollback()
  7647. return err
  7648. }
  7649. }
  7650. return err
  7651. }
  7652. func GetGoodFlowByWarehouseOutId(good_id int64, user_org_id int64, id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  7653. 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
  7654. if err != gorm.ErrRecordNotFound {
  7655. if err != nil {
  7656. tx.Rollback()
  7657. return flow, err
  7658. }
  7659. }
  7660. return
  7661. }
  7662. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  7663. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7664. 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
  7665. return err
  7666. }
  7667. func UpdateNewGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7668. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7669. if err != nil {
  7670. tx.Rollback()
  7671. return err
  7672. }
  7673. 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
  7674. if err != nil {
  7675. tx.Rollback()
  7676. return err
  7677. }
  7678. return err
  7679. }
  7680. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  7681. 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
  7682. return stock, err
  7683. }
  7684. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  7685. 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
  7686. return err
  7687. }
  7688. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7689. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7690. return info, err
  7691. }
  7692. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  7693. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7694. return info, err
  7695. }
  7696. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  7697. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7698. return info, err
  7699. }
  7700. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  7701. 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
  7702. 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
  7703. return err
  7704. }
  7705. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7706. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  7707. return info, err
  7708. }
  7709. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  7710. 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
  7711. return err
  7712. }
  7713. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  7714. 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
  7715. return err
  7716. }
  7717. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  7718. 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
  7719. 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
  7720. return err
  7721. }
  7722. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  7723. 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
  7724. return err
  7725. }
  7726. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  7727. 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
  7728. 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
  7729. return err
  7730. }
  7731. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  7732. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7733. 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
  7734. return err
  7735. }
  7736. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  7737. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  7738. return info, err
  7739. }
  7740. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  7741. 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
  7742. 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
  7743. return err
  7744. }
  7745. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  7746. 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
  7747. return info, err
  7748. }
  7749. func GetSelfDrugAllStockInfoOne(patient_id int64, orgid int64, drugid int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  7750. 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
  7751. return info, err
  7752. }
  7753. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7754. 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
  7755. if err != gorm.ErrRecordNotFound {
  7756. if err != nil {
  7757. tx.Rollback()
  7758. return info, err
  7759. }
  7760. }
  7761. return info, err
  7762. }
  7763. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  7764. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7765. 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
  7766. return err
  7767. }
  7768. func UpdateNewBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7769. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7770. if err != gorm.ErrRecordNotFound {
  7771. if err != nil {
  7772. tx.Rollback()
  7773. return err
  7774. }
  7775. }
  7776. 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
  7777. if err != gorm.ErrRecordNotFound {
  7778. if err != nil {
  7779. tx.Rollback()
  7780. return err
  7781. }
  7782. }
  7783. return err
  7784. }
  7785. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  7786. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7787. return err
  7788. }
  7789. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7790. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7791. if err != nil {
  7792. tx.Rollback()
  7793. return err
  7794. }
  7795. return err
  7796. }
  7797. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7798. 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
  7799. return info, err
  7800. }
  7801. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7802. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7803. return info, err
  7804. }
  7805. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  7806. 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
  7807. 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
  7808. return err
  7809. }
  7810. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  7811. stock := models.DrugCancelStock{}
  7812. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  7813. return stock, err
  7814. }
  7815. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7816. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  7817. return stock, err
  7818. }
  7819. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7820. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  7821. return stock, err
  7822. }
  7823. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7824. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  7825. return info, err
  7826. }
  7827. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  7828. 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
  7829. return err
  7830. }
  7831. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7832. 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
  7833. if err != nil {
  7834. tx.Rollback()
  7835. return err
  7836. }
  7837. return err
  7838. }
  7839. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  7840. 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
  7841. return err
  7842. }
  7843. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7844. 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
  7845. if err != nil {
  7846. tx.Rollback()
  7847. return err
  7848. }
  7849. return err
  7850. }
  7851. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  7852. 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
  7853. return err
  7854. }
  7855. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  7856. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7857. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7858. return err
  7859. }
  7860. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7861. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  7862. return info, err
  7863. }
  7864. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  7865. 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
  7866. return err
  7867. }
  7868. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7869. 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
  7870. return err
  7871. }
  7872. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7873. 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
  7874. return err
  7875. }
  7876. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  7877. info := models.DrugWarehouseInfo{}
  7878. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7879. return info, err
  7880. }
  7881. func GetSelfDrugInfoSix(id int64, orgid int64) (models.XtSelfDrugWarehouseInfo, error) {
  7882. info := models.XtSelfDrugWarehouseInfo{}
  7883. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7884. return info, err
  7885. }
  7886. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  7887. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  7888. if storehouse_id > 0 {
  7889. db = db.Where("storehouse_id = ?", storehouse_id)
  7890. }
  7891. if orgid > 0 {
  7892. db = db.Where("user_org_id = ?", orgid)
  7893. }
  7894. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  7895. return storeinventory, err
  7896. }
  7897. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  7898. detail := models.AutomaticReduceDetail{}
  7899. 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
  7900. return detail, err
  7901. }
  7902. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  7903. detail := models.WarehouseOutInfo{}
  7904. 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
  7905. return detail, err
  7906. }
  7907. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  7908. stock := models.XtGoodStockCount{}
  7909. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  7910. return stock, err
  7911. }
  7912. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  7913. err := XTWriteDB().Create(&stock).Error
  7914. return err
  7915. }
  7916. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7917. 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
  7918. return err
  7919. }
  7920. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7921. ut := XTWriteDB().Begin()
  7922. 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
  7923. if err != nil {
  7924. ut.Rollback()
  7925. return err
  7926. }
  7927. ut.Commit()
  7928. return err
  7929. }
  7930. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  7931. 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
  7932. return err
  7933. }
  7934. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  7935. 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
  7936. if err != nil {
  7937. tx.Rollback()
  7938. return err
  7939. }
  7940. return err
  7941. }
  7942. func CreateDrugStock(stock models.XtDrugStockCount) error {
  7943. err := XTWriteDB().Create(&stock).Error
  7944. return err
  7945. }
  7946. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  7947. ut := XTWriteDB().Begin()
  7948. 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
  7949. if err != nil {
  7950. ut.Rollback()
  7951. return err
  7952. }
  7953. 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
  7954. if err != nil {
  7955. ut.Rollback()
  7956. return err
  7957. }
  7958. ut.Commit()
  7959. return err
  7960. }
  7961. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  7962. ut := XTWriteDB().Begin()
  7963. 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
  7964. if err != nil {
  7965. ut.Rollback()
  7966. return err
  7967. }
  7968. ut.Commit()
  7969. return err
  7970. }
  7971. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  7972. ut := XTWriteDB().Begin()
  7973. 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
  7974. if err != nil {
  7975. ut.Rollback()
  7976. return err
  7977. }
  7978. 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
  7979. if err != nil {
  7980. ut.Rollback()
  7981. return err
  7982. }
  7983. ut.Commit()
  7984. return err
  7985. }
  7986. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  7987. ut := XTWriteDB().Begin()
  7988. 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
  7989. if err != nil {
  7990. ut.Rollback()
  7991. return err
  7992. }
  7993. ut.Commit()
  7994. return err
  7995. }
  7996. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7997. ut := XTWriteDB().Begin()
  7998. 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
  7999. if err != nil {
  8000. ut.Rollback()
  8001. return err
  8002. }
  8003. 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
  8004. if err != nil {
  8005. ut.Rollback()
  8006. return err
  8007. }
  8008. ut.Commit()
  8009. return err
  8010. }
  8011. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8012. ut := XTWriteDB().Begin()
  8013. 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
  8014. if err != nil {
  8015. ut.Rollback()
  8016. return err
  8017. }
  8018. 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
  8019. if err != nil {
  8020. ut.Rollback()
  8021. return err
  8022. }
  8023. ut.Commit()
  8024. return err
  8025. }
  8026. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  8027. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  8028. 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
  8029. return info, err
  8030. } else {
  8031. 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
  8032. return info, err
  8033. }
  8034. }
  8035. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  8036. config := models.HisSettleStockConfig{}
  8037. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8038. if err == gorm.ErrRecordNotFound {
  8039. return nil, err
  8040. }
  8041. if err != nil {
  8042. return nil, err
  8043. }
  8044. return &config, nil
  8045. }
  8046. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  8047. err := XTWriteDB().Create(&config).Error
  8048. return err
  8049. }
  8050. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  8051. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  8052. return err
  8053. }
  8054. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  8055. config := models.DrugStockConfig{}
  8056. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8057. return config, err
  8058. }
  8059. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  8060. stockConfig := models.PharmacyConfig{}
  8061. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8062. return stockConfig, err
  8063. }
  8064. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  8065. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8066. return err
  8067. }
  8068. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  8069. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8070. return err
  8071. }
  8072. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  8073. config := models.HisSettleStockConfig{}
  8074. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8075. return config, nil
  8076. }
  8077. func GetGoodWareHouseOutInfoByGood(good_id int64, sys_record_time int64, user_org_id int64) (info []*models.WarehouseOutInfo, err error) {
  8078. 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
  8079. return info, err
  8080. }
  8081. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  8082. info := models.GoodInformationFourty{}
  8083. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  8084. return info, err
  8085. }
  8086. func GetGoodWarehouseInfoByGoodList(good_id int64) (info []*models.WarehousingInfo, err error) {
  8087. err = XTReadDB().Where("good_id = ? and status =1 and stock_count>0 and is_check = 1", good_id).Find(&info).Error
  8088. return info, err
  8089. }
  8090. func GetAllWarehouseInfo(good_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  8091. err = XTReadDB().Where("good_id = ? and user_org_id =? and status=1 and is_check =1", good_id, user_org_id).Find(&info).Error
  8092. return info, err
  8093. }
  8094. func GetHisPrescriptionByGoodId(good_id int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  8095. err = XTReadDB().Where("project_id = ? and user_org_id =? and status=1 and type =3", good_id, user_org_id).Find(&project).Error
  8096. return project, err
  8097. }
  8098. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  8099. advice := models.HisDoctorAdvice{}
  8100. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8101. return advice, err
  8102. }
  8103. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  8104. advice := models.DoctorAdvice{}
  8105. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8106. return advice, err
  8107. }
  8108. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  8109. out := models.WarehouseOut{}
  8110. 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
  8111. return out, err
  8112. }
  8113. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  8114. config := models.XtGoodOutConfig{}
  8115. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8116. if err == gorm.ErrRecordNotFound {
  8117. return nil, err
  8118. }
  8119. if err != nil {
  8120. return nil, err
  8121. }
  8122. return &config, nil
  8123. }
  8124. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  8125. err := XTWriteDB().Create(&good).Error
  8126. return err
  8127. }
  8128. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  8129. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  8130. return err
  8131. }
  8132. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  8133. config := models.XtGoodOutConfig{}
  8134. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8135. return config, err
  8136. }
  8137. func UpdateGoodAutoConfig(user_org_id int64) error {
  8138. gobalConfig := models.GobalConfig{}
  8139. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8140. return err
  8141. }
  8142. func UpdateGoodOutConfig(user_org_id int64) error {
  8143. config := models.XtGoodOutConfig{}
  8144. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8145. return err
  8146. }
  8147. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  8148. 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
  8149. return info, err
  8150. }
  8151. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  8152. 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
  8153. return good, err
  8154. }
  8155. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  8156. 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
  8157. return err
  8158. }
  8159. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8160. 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
  8161. return err
  8162. }
  8163. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8164. 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
  8165. return err
  8166. }
  8167. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8168. out := models.WarehouseOut{}
  8169. 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
  8170. return out, err
  8171. }
  8172. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8173. out := models.WarehouseOut{}
  8174. 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
  8175. if err != gorm.ErrRecordNotFound {
  8176. if err != nil {
  8177. tx.Rollback()
  8178. return out, err
  8179. }
  8180. }
  8181. return out, err
  8182. }
  8183. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8184. out := models.WarehouseOut{}
  8185. 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
  8186. return out, err
  8187. }
  8188. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8189. out := models.WarehouseOut{}
  8190. 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
  8191. if err != gorm.ErrRecordNotFound {
  8192. if err != nil {
  8193. tx.Rollback()
  8194. return out, err
  8195. }
  8196. }
  8197. return out, err
  8198. }
  8199. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  8200. goodsType := models.GoodsType{}
  8201. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  8202. return goodsType, err
  8203. }
  8204. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  8205. goodsTypeList, _ := GetAllGoodType(org_id)
  8206. for _, item := range goodsTypeList {
  8207. if good_type_id == item.ID {
  8208. good_type_name = item.TypeName
  8209. }
  8210. }
  8211. return good_type_name
  8212. }
  8213. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  8214. information := models.GoodInformation{}
  8215. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  8216. return information, err
  8217. }
  8218. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  8219. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  8220. return info, err
  8221. }
  8222. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  8223. ut := writeDb.Begin()
  8224. 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
  8225. if err != nil {
  8226. ut.Rollback()
  8227. return err
  8228. }
  8229. ut.Commit()
  8230. return err
  8231. }
  8232. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  8233. 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
  8234. if err != nil {
  8235. tx.Rollback()
  8236. return err
  8237. }
  8238. return err
  8239. }
  8240. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  8241. info := models.WarehouseOutInfo{}
  8242. var err error
  8243. 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
  8244. if err == gorm.ErrRecordNotFound {
  8245. return nil, err
  8246. }
  8247. if err != nil {
  8248. return nil, err
  8249. }
  8250. return &info, nil
  8251. }
  8252. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  8253. info := models.WarehouseOutInfo{}
  8254. 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
  8255. return info, err
  8256. }
  8257. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8258. info := models.WarehouseOutInfo{}
  8259. 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
  8260. if err != gorm.ErrRecordNotFound {
  8261. if err != nil {
  8262. tx.Rollback()
  8263. return info, err
  8264. }
  8265. }
  8266. return info, err
  8267. }
  8268. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  8269. info := models.WarehouseOutInfo{}
  8270. 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
  8271. return info, err
  8272. }
  8273. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8274. info := models.WarehouseOutInfo{}
  8275. 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
  8276. if err != gorm.ErrRecordNotFound {
  8277. if err != nil {
  8278. tx.Rollback()
  8279. return info, err
  8280. }
  8281. }
  8282. return info, err
  8283. }
  8284. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  8285. info := models.WarehouseOutInfo{}
  8286. var err error
  8287. 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
  8288. if err == gorm.ErrRecordNotFound {
  8289. return nil, err
  8290. }
  8291. if err != nil {
  8292. return nil, err
  8293. }
  8294. return &info, nil
  8295. }
  8296. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  8297. info := models.WarehouseOutInfo{}
  8298. var err error
  8299. 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
  8300. if err != gorm.ErrRecordNotFound {
  8301. if err != nil {
  8302. tx.Rollback()
  8303. return &info, err
  8304. }
  8305. }
  8306. return &info, err
  8307. }
  8308. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  8309. db := XTReadDB().Model(&info).Where("status = 1")
  8310. if startime > 0 {
  8311. db = db.Where("ctime>=?", startime)
  8312. }
  8313. if endtime > 0 {
  8314. db = db.Where("ctime<=?", endtime)
  8315. }
  8316. if orgid > 0 {
  8317. db = db.Where("org_id = ?", orgid)
  8318. }
  8319. if len(good_id) > 0 {
  8320. db = db.Where("good_id in(?)", good_id)
  8321. }
  8322. if storehouse_id > 0 {
  8323. db = db.Where("storehouse_id = ?", storehouse_id)
  8324. }
  8325. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  8326. return info, err
  8327. }
  8328. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  8329. db := XTReadDB().Model(&info).Where("status= 1")
  8330. if start_time > 0 {
  8331. db = db.Where("ctime>=?", start_time)
  8332. }
  8333. if end_time > 0 {
  8334. db = db.Where("ctime <=?", end_time)
  8335. }
  8336. if orgid > 0 {
  8337. db = db.Where("org_id = ?", orgid)
  8338. }
  8339. if len(good_id) > 0 {
  8340. db = db.Where("good_id in(?)", good_id)
  8341. }
  8342. if storehouse_id > 0 {
  8343. db = db.Where("storehouse_id = ?", storehouse_id)
  8344. }
  8345. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  8346. return info, err
  8347. }
  8348. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  8349. config := models.XtGoodOutConfig{}
  8350. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8351. return config, err
  8352. }
  8353. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  8354. 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
  8355. 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
  8356. return err
  8357. }
  8358. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  8359. tx := XTWriteDB().Begin()
  8360. for _, item := range list {
  8361. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8362. if err != nil {
  8363. tx.Rollback()
  8364. return err
  8365. }
  8366. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8367. if err != nil {
  8368. tx.Rollback()
  8369. return err
  8370. }
  8371. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8372. if err != nil {
  8373. tx.Rollback()
  8374. return err
  8375. }
  8376. var goodList []*models.WarehousingInfo
  8377. 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
  8378. var flush_count int64
  8379. for _, it := range goodList {
  8380. flush_count += it.StockCount
  8381. }
  8382. 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
  8383. if err != nil {
  8384. tx.Rollback()
  8385. return err
  8386. }
  8387. 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
  8388. if err != nil {
  8389. tx.Rollback()
  8390. return err
  8391. }
  8392. storeHouseConfig := models.XtStorehouseConfig{}
  8393. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8394. var stockList []*models.WarehousingInfo
  8395. //查询该耗材剩余多少库存
  8396. 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
  8397. var sum_count int64
  8398. var sum_in_count int64
  8399. for _, it := range stockList {
  8400. sum_count += it.StockCount
  8401. sum_in_count += it.WarehousingCount
  8402. }
  8403. 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
  8404. if err != nil {
  8405. tx.Rollback()
  8406. return err
  8407. }
  8408. 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
  8409. if err != nil {
  8410. tx.Rollback()
  8411. return err
  8412. }
  8413. }
  8414. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8415. if err != nil {
  8416. tx.Rollback()
  8417. return err
  8418. }
  8419. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8420. tx.Commit()
  8421. return err
  8422. }
  8423. func UpdateNewWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64, tx *gorm.DB) error {
  8424. for _, item := range list {
  8425. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8426. if err != nil {
  8427. tx.Rollback()
  8428. return err
  8429. }
  8430. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8431. if err != nil {
  8432. tx.Rollback()
  8433. return err
  8434. }
  8435. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8436. if err != nil {
  8437. tx.Rollback()
  8438. return err
  8439. }
  8440. var goodList []*models.WarehousingInfo
  8441. 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
  8442. var flush_count int64
  8443. for _, it := range goodList {
  8444. flush_count += it.StockCount
  8445. }
  8446. 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
  8447. if err != nil {
  8448. tx.Rollback()
  8449. return err
  8450. }
  8451. 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
  8452. if err != nil {
  8453. tx.Rollback()
  8454. return err
  8455. }
  8456. storeHouseConfig := models.XtStorehouseConfig{}
  8457. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8458. var stockList []*models.WarehousingInfo
  8459. //查询该耗材剩余多少库存
  8460. 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
  8461. var sum_count int64
  8462. var sum_in_count int64
  8463. for _, it := range stockList {
  8464. sum_count += it.StockCount
  8465. sum_in_count += it.WarehousingCount
  8466. }
  8467. 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
  8468. if err != nil {
  8469. tx.Rollback()
  8470. return err
  8471. }
  8472. 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
  8473. if err != nil {
  8474. tx.Rollback()
  8475. return err
  8476. }
  8477. err = tx.Model(models.XtGoodWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", item.ID).Update(map[string]interface{}{"status": 0}).Error
  8478. if err != nil {
  8479. tx.Rollback()
  8480. return err
  8481. }
  8482. }
  8483. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8484. if err != nil {
  8485. tx.Rollback()
  8486. return err
  8487. }
  8488. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8489. return err
  8490. }
  8491. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8492. if storehouse_id == 0 {
  8493. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  8494. }
  8495. if storehouse_id > 0 {
  8496. 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
  8497. }
  8498. return info, err
  8499. }
  8500. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8501. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  8502. return info, err
  8503. }
  8504. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  8505. 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
  8506. return list, err
  8507. }
  8508. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  8509. 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
  8510. return infor, err
  8511. }
  8512. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  8513. info := models.WarehouseOutInfo{}
  8514. 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
  8515. return info, err
  8516. }
  8517. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  8518. info := models.WarehousingInfo{}
  8519. 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
  8520. return info, err
  8521. }
  8522. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8523. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8524. return list, err
  8525. }
  8526. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8527. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8528. return list, err
  8529. }
  8530. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  8531. templateL := models.HisPrescriptionProjectTemplateL{}
  8532. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  8533. return templateL, err
  8534. }
  8535. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  8536. 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
  8537. if err != nil {
  8538. tx.Rollback()
  8539. return err
  8540. }
  8541. return err
  8542. }
  8543. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  8544. 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
  8545. return info, err
  8546. }
  8547. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  8548. 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
  8549. return err
  8550. }
  8551. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  8552. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  8553. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  8554. return drugInfoConfig, err
  8555. }
  8556. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8557. err := XTWriteDB().Create(&infoConfig).Error
  8558. return err
  8559. }
  8560. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8561. err := XTWriteDB().Save(&infoConfig).Error
  8562. return err
  8563. }
  8564. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  8565. drugOutConfig := models.XtSelfDrugOutConfig{}
  8566. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  8567. return drugOutConfig, err
  8568. }
  8569. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8570. err := XTWriteDB().Create(&config).Error
  8571. return err
  8572. }
  8573. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8574. err := XTWriteDB().Save(&config).Error
  8575. return err
  8576. }
  8577. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  8578. err := tx.Create(&flow).Error
  8579. if err != nil {
  8580. tx.Rollback()
  8581. return err
  8582. }
  8583. return err
  8584. }
  8585. func UpdateWarehouseOutInfoByPrice(good_id int64, packing_price float64) error {
  8586. 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
  8587. return err
  8588. }
  8589. func GetWarehosueOutInfoByPrice(good_id int64, org_id int64) (out []*models.WarehouseOutInfo, err error) {
  8590. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and price =0", good_id, org_id).Find(&out).Error
  8591. return out, err
  8592. }
  8593. func UpdateWarehouseOut(good_id int64, packing_price float64) error {
  8594. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8595. return err
  8596. }
  8597. func UpdateStockFlowByList(good_id int64, packing_price float64) error {
  8598. 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
  8599. return err
  8600. }
  8601. func GetGoodStockInventoryByRecordate(good_id int64, record_date int64, user_org_id int64) (models.XtGoodStockInventoryList, error) {
  8602. goodStockInventoryList := models.XtGoodStockInventoryList{}
  8603. 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
  8604. return goodStockInventoryList, err
  8605. }
  8606. func CreateGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8607. err := XTWriteDB().Create(&list).Error
  8608. return err
  8609. }
  8610. func SaveGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8611. err := XTWriteDB().Save(&list).Error
  8612. return err
  8613. }
  8614. func GetGoodStockInventoryList(keyword string, limit int64, page int64, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8615. likeKey := "%" + keyword + "%"
  8616. offset := (page - 1) * limit
  8617. db := XTReadDB().Model(&models.XtGoodStockInventoryList{}).Where("status=1")
  8618. if storehouse_id > 0 {
  8619. db = db.Where("storehouse_id = ?", storehouse_id)
  8620. }
  8621. if startTime > 0 {
  8622. db = db.Where("record_date >=?", startTime)
  8623. }
  8624. if endTime > 0 {
  8625. db = db.Where("record_date<=?", endTime)
  8626. }
  8627. if len(keyword) > 0 {
  8628. db = db.Where("good_name like ? or specification_name like ?", likeKey, likeKey)
  8629. }
  8630. if orgId > 0 {
  8631. db = db.Where("user_org_id = ?", orgId)
  8632. }
  8633. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8634. return list, total, err
  8635. }
  8636. func GetDrugStockInventoryList(drug_id int64, record_date int64, user_org_id int64) (models.XtDrugStockInventoryList, error) {
  8637. drugStockInventoryList := models.XtDrugStockInventoryList{}
  8638. 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
  8639. return drugStockInventoryList, err
  8640. }
  8641. func CreatedDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8642. err := XTWriteDB().Create(&list).Error
  8643. return err
  8644. }
  8645. func SaveDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8646. err := XTWriteDB().Save(&list).Error
  8647. return err
  8648. }
  8649. func GetStockInventoryPrintList(storehouse_id int64, keyword string, limit int64, page int64, ids []string, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8650. db := XTReadDB().Model(&list).Where("status=1")
  8651. likeKey := "%" + keyword + "%"
  8652. offset := (page - 1) * limit
  8653. if storehouse_id > 0 {
  8654. db = db.Where("storehouse_id = ?", storehouse_id)
  8655. }
  8656. if len(keyword) > 0 {
  8657. db = db.Where("good_name like ?", likeKey)
  8658. }
  8659. if orgId > 0 {
  8660. db = db.Where("user_org_id = ?", orgId)
  8661. }
  8662. if len(ids) > 0 {
  8663. db = db.Where("id in(?)", ids)
  8664. }
  8665. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8666. return list, total, err
  8667. }
  8668. func GetGoodWarehouseInfoList(startTime int64, endTime int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  8669. db := XTReadDB().Model(&info).Where("status=1 and is_check=1")
  8670. if startTime > 0 {
  8671. db = db.Where("ctime>=?", startTime)
  8672. }
  8673. if endTime > 0 {
  8674. db = db.Where("ctime<=?", endTime)
  8675. }
  8676. if orgId > 0 {
  8677. db = db.Where("org_id = ?", orgId)
  8678. }
  8679. err = db.Find(&info).Error
  8680. return info, err
  8681. }
  8682. func GetGoodWarhouseOutList(startTime int64, endTime int64, orgId int64) (outinfo []*models.WarehouseOutInfo, err error) {
  8683. db := XTReadDB().Model(&outinfo).Where("status=1 and is_check =1")
  8684. if startTime > 0 {
  8685. db = db.Where("ctime>=?", startTime)
  8686. }
  8687. if endTime > 0 {
  8688. db = db.Where("ctime<=?", endTime)
  8689. }
  8690. if orgId > 0 {
  8691. db = db.Where("org_id = ?", orgId)
  8692. }
  8693. err = db.Find(&outinfo).Error
  8694. return outinfo, err
  8695. }
  8696. func GetGoodStockFlowProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8697. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 10")
  8698. if startTime > 0 {
  8699. db = db.Where("ctime>=?", startTime)
  8700. }
  8701. if endTime > 0 {
  8702. db = db.Where("ctime<=?", endTime)
  8703. }
  8704. if orgId > 0 {
  8705. db = db.Where("user_org_id = ?", orgId)
  8706. }
  8707. err = db.Find(&flow).Error
  8708. return flow, err
  8709. }
  8710. func GetGoodStockFlowNoProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8711. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 11")
  8712. if startTime > 0 {
  8713. db = db.Where("ctime>=?", startTime)
  8714. }
  8715. if endTime > 0 {
  8716. db = db.Where("ctime<=?", endTime)
  8717. }
  8718. if orgId > 0 {
  8719. db = db.Where("user_org_id = ?", orgId)
  8720. }
  8721. err = db.Find(&flow).Error
  8722. return flow, err
  8723. }
  8724. func GetCancelStockFlowList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8725. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 7")
  8726. if startTime > 0 {
  8727. db = db.Where("ctime>=?", startTime)
  8728. }
  8729. if endTime > 0 {
  8730. db = db.Where("ctime<=?", endTime)
  8731. }
  8732. if orgId > 0 {
  8733. db = db.Where("user_org_id = ?", orgId)
  8734. }
  8735. err = db.Find(&flow).Error
  8736. return flow, err
  8737. }
  8738. func GetLastMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8739. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 1 or consumable_type = 10)")
  8740. if startTime > 0 {
  8741. db = db.Where("ctime <?", startTime)
  8742. }
  8743. if orgId > 0 {
  8744. db = db.Where("user_org_id = ?", orgId)
  8745. }
  8746. err = db.Find(&flow).Error
  8747. return flow, err
  8748. }
  8749. func GetLastMonthOverCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8750. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 2 or consumable_type=3 or consumable_type=11)")
  8751. if startTime > 0 {
  8752. db = db.Where("ctime <?", startTime)
  8753. }
  8754. if orgId > 0 {
  8755. db = db.Where("user_org_id = ?", orgId)
  8756. }
  8757. err = db.Find(&flow).Error
  8758. return flow, err
  8759. }
  8760. func GetLastCancelMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8761. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 7)")
  8762. if startTime > 0 {
  8763. db = db.Where("ctime <?", startTime)
  8764. }
  8765. if orgId > 0 {
  8766. db = db.Where("user_org_id = ?", orgId)
  8767. }
  8768. err = db.Find(&flow).Error
  8769. return flow, err
  8770. }
  8771. func GetDrugCodeOpen(user_org_id int64) (models.XtDrugCodeOpen, error) {
  8772. drugCodeOpen := models.XtDrugCodeOpen{}
  8773. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  8774. return drugCodeOpen, err
  8775. }
  8776. func CreateDrugOpen(open models.XtDrugCodeOpen) error {
  8777. err := XTWriteDB().Create(&open).Error
  8778. return err
  8779. }
  8780. func SaveDrugOpen(open models.XtDrugCodeOpen) error {
  8781. err := XTWriteDB().Save(&open).Error
  8782. return err
  8783. }
  8784. func GetHisDrugCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64) (list []*models.HisDoctorAdviceInfo, total int64, err error) {
  8785. db := XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("status=1")
  8786. if orgId > 0 {
  8787. db = db.Where("user_org_id=?", orgId)
  8788. }
  8789. if start_time > 0 {
  8790. db = db.Where("advice_date >= ?", start_time)
  8791. }
  8792. if end_time > 0 {
  8793. db = db.Where("advice_date<=?", end_time)
  8794. }
  8795. offset := (page - 1) * limit
  8796. if is_sale == 1 {
  8797. db = db.Where("is_upload = 1")
  8798. }
  8799. if is_sale == 2 {
  8800. db = db.Where("is_upload = 2 or is_upload =0")
  8801. }
  8802. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8803. return list, total, err
  8804. }
  8805. func GetDrugWarehouseOutInfoByDrugCode(drug_id int64, patient_id int64, advice_date int64, user_org_id int64) (outinfo []*models.XtDrugWarehouseOutInfo, err error) {
  8806. 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
  8807. return outinfo, err
  8808. }
  8809. func GetNewGoodWarehouseOutInfo(good_id int64, user_org_id int64, warehouse_out_id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  8810. 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
  8811. if err != gorm.ErrRecordNotFound {
  8812. if err != nil {
  8813. tx.Rollback()
  8814. return info, err
  8815. }
  8816. }
  8817. return info, err
  8818. }
  8819. func GetInitGoodFlow(user_org_id int64, good_id int64, warehousing_detail_id int64, tx *gorm.DB) (list []*models.VmStockFlow, err error) {
  8820. 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
  8821. if err != gorm.ErrRecordNotFound {
  8822. if err != nil {
  8823. tx.Rollback()
  8824. return list, err
  8825. }
  8826. }
  8827. return list, err
  8828. }
  8829. func GetNewDrugFlowList(drug_id int64, user_org_id int64, warehousing_detail_id int64, tx *gorm.DB) (flow []*models.DrugFlow, err error) {
  8830. 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
  8831. if err != gorm.ErrRecordNotFound {
  8832. if err != nil {
  8833. tx.Rollback()
  8834. return flow, err
  8835. }
  8836. }
  8837. return flow, err
  8838. }
  8839. func GetNewPatientAdviceList(patient_id int64, start_time int64, end_time int64, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  8840. db := XTReadDB().Model(&advice).Where("status=1 and advice_type =2")
  8841. if patient_id > 0 {
  8842. db = db.Where("patient_id = ?", patient_id)
  8843. }
  8844. if start_time > 0 {
  8845. db = db.Where("advice_date>=?", start_time)
  8846. }
  8847. if end_time > 0 {
  8848. db = db.Where("advice_date<=?", end_time)
  8849. }
  8850. if user_org_id > 0 {
  8851. db = db.Where("user_org_id = ?", user_org_id)
  8852. }
  8853. err = db.Find(&advice).Error
  8854. return advice, err
  8855. }
  8856. func GetPatientAdviceListDetail(patient_id int64, start_time int64, end_time int64, user_org_id int64, advice_name string) (advice []*models.DoctorAdvice, err error) {
  8857. db := XTReadDB().Model(&advice).Where("status = 1")
  8858. if patient_id > 0 {
  8859. db = db.Where("patient_id = ?", patient_id)
  8860. }
  8861. if start_time > 0 {
  8862. db = db.Where("advice_date>=?", start_time)
  8863. }
  8864. if end_time > 0 {
  8865. db = db.Where("advice_date<=?", end_time)
  8866. }
  8867. if user_org_id > 0 {
  8868. db = db.Where("user_org_id = ?", user_org_id)
  8869. }
  8870. if len(advice_name) > 0 {
  8871. db = db.Where("advice_name =?", advice_name)
  8872. }
  8873. err = db.Order("advice_date asc").Find(&advice).Error
  8874. return advice, err
  8875. }