stock_service.go 253KB

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