stock_service.go 296KB

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