stock_service.go 251KB

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