stock_service.go 286KB

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