Stock.go 155 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927
  1. package controllers
  2. import (
  3. "ERP_storage/Nats"
  4. "ERP_storage/Nats/NatsServer"
  5. "ERP_storage/conf"
  6. "ERP_storage/dto"
  7. "ERP_storage/logs"
  8. "ERP_storage/models/Account"
  9. "ERP_storage/models/Basic"
  10. "ERP_storage/models/Contract"
  11. "ERP_storage/models/Stock"
  12. "errors"
  13. "fmt"
  14. "math"
  15. "os"
  16. "strconv"
  17. "strings"
  18. "time"
  19. "github.com/beego/beego/v2/adapter/orm"
  20. beego "github.com/beego/beego/v2/server/web"
  21. "github.com/robfig/cron/v3"
  22. "github.com/xuri/excelize/v2"
  23. natslibs "gogs.baozhida.cn/zoie/ERP_libs/Nats"
  24. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  25. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  26. )
  27. type StockController struct {
  28. beego.Controller
  29. User userlibs.User
  30. }
  31. func (c *StockController) Prepare() {
  32. c.User = *Account.User_r
  33. }
  34. func (c *StockController) Device_List() {
  35. // 分页参数 初始化
  36. page, _ := c.GetInt("page")
  37. if page < 1 {
  38. page = 1
  39. }
  40. page_z, _ := c.GetInt("page_z")
  41. if page_z < 1 {
  42. page_z = conf.Page_size
  43. }
  44. T_state, _ := c.GetInt("T_state")
  45. T_name := c.GetString("T_name")
  46. T_product_name := c.GetString("T_product_name") // 产品名称
  47. T_product_model := c.GetString("T_product_model") // 产品型号
  48. DeviceDao := Stock.NewDevice(orm.NewOrm())
  49. R_List, R_cnt := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, page, page_z)
  50. var r_jsons lib.R_JSONS
  51. r_jsons.Num = R_cnt
  52. r_jsons.Data = R_List
  53. r_jsons.Page = page
  54. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  55. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  56. c.ServeJSON()
  57. return
  58. }
  59. func (c *StockController) Device_Excel() {
  60. T_state, _ := c.GetInt("T_state")
  61. T_name := c.GetString("T_name")
  62. T_product_name := c.GetString("T_product_name") // 产品名称
  63. T_product_model := c.GetString("T_product_model") // 产品型号
  64. DeviceDao := Stock.NewDevice(orm.NewOrm())
  65. R_List, _ := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, 0, 9999)
  66. // 生成Excel文件名
  67. filename := fmt.Sprintf("设备列表(%s)", lib.GetRandstring(6, "0123456789", 0))
  68. // 创建Excel文件
  69. f := excelize.NewFile()
  70. // 设置样式
  71. Style1, _ := f.NewStyle(
  72. &excelize.Style{
  73. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  74. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  75. })
  76. Style2, _ := f.NewStyle(
  77. &excelize.Style{
  78. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  79. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  80. Border: []excelize.Border{
  81. {Type: "left", Color: "000000", Style: 1},
  82. {Type: "top", Color: "000000", Style: 1},
  83. {Type: "bottom", Color: "000000", Style: 1},
  84. {Type: "right", Color: "000000", Style: 1},
  85. },
  86. })
  87. Style3, _ := f.NewStyle(
  88. &excelize.Style{
  89. Font: &excelize.Font{Size: 10, Family: "宋体"},
  90. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  91. Border: []excelize.Border{
  92. {Type: "left", Color: "000000", Style: 1},
  93. {Type: "top", Color: "000000", Style: 1},
  94. {Type: "bottom", Color: "000000", Style: 1},
  95. {Type: "right", Color: "000000", Style: 1},
  96. },
  97. })
  98. // 设置工作表名称
  99. f.SetSheetName("Sheet1", "设备列表")
  100. // 设置标题
  101. f.MergeCell("设备列表", "A1", "M1")
  102. f.SetRowStyle("设备列表", 1, 1, Style1)
  103. f.SetCellValue("设备列表", "A1", "宝智达科技设备列表")
  104. f.SetRowHeight("设备列表", 1, 30)
  105. // 设置表头
  106. f.SetCellStyle("设备列表", "A2", "M2", Style2)
  107. f.SetRowHeight("设备列表", 2, 20)
  108. // 设置表头内容
  109. f.SetCellValue("设备列表", "A2", "序号")
  110. f.SetCellValue("设备列表", "B2", "合同编号")
  111. f.SetCellValue("设备列表", "C2", "出库单号")
  112. f.SetCellValue("设备列表", "D2", "关联项目")
  113. f.SetCellValue("设备列表", "E2", "产品名称")
  114. f.SetCellValue("设备列表", "F2", "产品分类")
  115. f.SetCellValue("设备列表", "G2", "产品型号")
  116. f.SetCellValue("设备列表", "H2", "产品规格")
  117. f.SetCellValue("设备列表", "I2", "设备SN")
  118. f.SetCellValue("设备列表", "J2", "模组imei")
  119. f.SetCellValue("设备列表", "K2", "物联网卡号")
  120. f.SetCellValue("设备列表", "L2", "设备编号")
  121. f.SetCellValue("设备列表", "M2", "状态")
  122. // 设置列宽
  123. f.SetColWidth("设备列表", "A", "A", 8) // 序号
  124. f.SetColWidth("设备列表", "B", "B", 15) // 合同编号
  125. f.SetColWidth("设备列表", "C", "C", 15) // 出库单号
  126. f.SetColWidth("设备列表", "D", "D", 20) // 关联项目
  127. f.SetColWidth("设备列表", "E", "E", 20) // 产品名称
  128. f.SetColWidth("设备列表", "F", "F", 15) // 产品分类
  129. f.SetColWidth("设备列表", "G", "G", 15) // 产品型号
  130. f.SetColWidth("设备列表", "H", "H", 15) // 产品规格
  131. f.SetColWidth("设备列表", "I", "I", 20) // 设备SN
  132. f.SetColWidth("设备列表", "J", "J", 15) // 模组imei
  133. f.SetColWidth("设备列表", "K", "K", 20) // 物联网卡号
  134. f.SetColWidth("设备列表", "L", "L", 18) // 设备编号
  135. f.SetColWidth("设备列表", "M", "M", 10) // 状态
  136. // 填充数据
  137. for i, device := range R_List {
  138. row := i + 3 // 从第3行开始填充数据
  139. // 状态转换
  140. stateText := "未出库"
  141. if device.T_State == 1 {
  142. stateText = "已出库"
  143. }
  144. f.SetCellStyle("设备列表", fmt.Sprintf("A%d", row), fmt.Sprintf("M%d", row), Style3)
  145. //f.SetRowHeight("设备列表", row, 20)
  146. f.SetCellValue("设备列表", fmt.Sprintf("A%d", row), i+1) // 序号
  147. f.SetCellValue("设备列表", fmt.Sprintf("B%d", row), device.T_contract_number) // 合同编号
  148. f.SetCellValue("设备列表", fmt.Sprintf("C%d", row), device.T_out_number) // 出库单号
  149. f.SetCellValue("设备列表", fmt.Sprintf("D%d", row), device.T_project) // 关联项目
  150. f.SetCellValue("设备列表", fmt.Sprintf("E%d", row), device.T_product_name) // 产品名称
  151. f.SetCellValue("设备列表", fmt.Sprintf("F%d", row), device.T_product_class_name) // 产品分类
  152. f.SetCellValue("设备列表", fmt.Sprintf("G%d", row), device.T_product_model) // 产品型号
  153. f.SetCellValue("设备列表", fmt.Sprintf("H%d", row), device.T_product_spec) // 产品规格
  154. f.SetCellValue("设备列表", fmt.Sprintf("I%d", row), device.T_sn) // 设备SN
  155. f.SetCellValue("设备列表", fmt.Sprintf("J%d", row), device.T_imei) // 模组imei
  156. f.SetCellValue("设备列表", fmt.Sprintf("K%d", row), device.T_iccid) // 物联网卡号
  157. f.SetCellValue("设备列表", fmt.Sprintf("L%d", row), device.T_device_number) // 设备编号
  158. f.SetCellValue("设备列表", fmt.Sprintf("M%d", row), stateText) // 状态
  159. }
  160. // 保存文件
  161. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  162. fmt.Println(err)
  163. }
  164. var url string
  165. //// 上传 OSS
  166. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  167. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  168. if !is {
  169. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  170. c.ServeJSON()
  171. return
  172. }
  173. //删除目录
  174. err := os.Remove("ofile/" + filename + ".xlsx")
  175. if err != nil {
  176. logs.Error(lib.FuncName(), err)
  177. }
  178. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  179. c.ServeJSON()
  180. return
  181. }
  182. func (c *StockController) Device_Check() {
  183. T_sn := c.GetString("T_sn")
  184. T_type, _ := c.GetInt("T_type") //1-出库 2-入库
  185. T_product_id, _ := c.GetInt("T_product_id")
  186. T_date := c.GetString("T_date")
  187. // 检查MQTT授权
  188. mqtt := Stock.Read_MqttUser(T_sn)
  189. if len(mqtt.Username) == 0 {
  190. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未授权,请先授权!", T_sn)}
  191. c.ServeJSON()
  192. return
  193. }
  194. DeviceDao := Stock.NewDevice(orm.NewOrm())
  195. date, is := lib.DateStrToTime(T_date)
  196. if !is {
  197. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  198. c.ServeJSON()
  199. return
  200. }
  201. checkTime := date.Format("2006-01-02 15:04:05")
  202. // T_State 1-已出库 2-未出库/入库
  203. // 查询checkTime时间前最后一条记录
  204. lastBeforeCheckTime, errBefore := DeviceDao.Read_Device_LastBeforeCheckTime(T_sn, checkTime)
  205. // 查询checkTime时间后第一条记录
  206. firstAfterCheckTime, errAfter := DeviceDao.Read_Device_FirstAfterCheckTime(T_sn, checkTime)
  207. // 如果是入库操作
  208. if T_type == 2 {
  209. // 根据表格逻辑判断:
  210. // 1. 时间前无数据,时间后有入库记录
  211. if (errBefore != nil || lastBeforeCheckTime.Id == 0) &&
  212. errAfter == nil && firstAfterCheckTime.T_State == 2 && len(firstAfterCheckTime.T_in_number) > 0 {
  213. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  214. c.ServeJSON()
  215. return
  216. }
  217. // 2. 时间前已入库
  218. if errBefore == nil && lastBeforeCheckTime.T_State == 2 && len(lastBeforeCheckTime.T_in_number) > 0 {
  219. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  220. c.ServeJSON()
  221. return
  222. }
  223. // 3. 时间前已出库,时间后有入库记录
  224. if errBefore == nil && lastBeforeCheckTime.T_State == 1 && len(lastBeforeCheckTime.T_out_number) > 0 &&
  225. errAfter == nil && firstAfterCheckTime.T_State == 2 && len(firstAfterCheckTime.T_in_number) > 0 {
  226. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  227. c.ServeJSON()
  228. return
  229. }
  230. }
  231. // 如果是出库操作
  232. if T_type == 1 {
  233. // 根据表格逻辑判断:
  234. // 1. 时间前无数据 -> 未入库
  235. if errBefore != nil || lastBeforeCheckTime.Id == 0 {
  236. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未入库,请先入库!", T_sn)}
  237. c.ServeJSON()
  238. return
  239. }
  240. // 2. 时间前已入库,时间后有出库记录
  241. if errBefore == nil && lastBeforeCheckTime.T_State == 2 && len(lastBeforeCheckTime.T_in_number) > 0 &&
  242. errAfter == nil && firstAfterCheckTime.T_State == 1 && len(firstAfterCheckTime.T_out_number) > 0 {
  243. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  244. c.ServeJSON()
  245. return
  246. }
  247. // 3. 时间前已出库
  248. if errBefore == nil && lastBeforeCheckTime.T_State == 1 && len(lastBeforeCheckTime.T_out_number) > 0 {
  249. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  250. c.ServeJSON()
  251. return
  252. }
  253. // 检查产品是否一致
  254. if lastBeforeCheckTime.T_product_id != T_product_id {
  255. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库产品与出库产品不一致,请检查!"}
  256. c.ServeJSON()
  257. return
  258. }
  259. }
  260. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  261. c.ServeJSON()
  262. return
  263. }
  264. func (c *StockController) Device_Take_Stock() {
  265. T_product_name := c.GetString("T_product_name") // 产品名称
  266. T_product_model := c.GetString("T_product_model") // 产品型号
  267. T_sn_list := c.GetString("T_sn_list") // sn列表
  268. DeviceDao := Stock.NewDevice(orm.NewOrm())
  269. // 获取库存中的所有设备(如果有产品名称和型号过滤)
  270. R_List, _ := DeviceDao.Read_Device_List("", T_product_name, T_product_model, 0, 0, 9999)
  271. // 创建设备SN到设备信息的映射,用于快速查找
  272. deviceMap := make(map[string]Stock.Device_R)
  273. // 保存在库设备列表
  274. var inStockDevices []Stock.Device_R
  275. for _, device := range R_List {
  276. deviceMap[device.T_sn] = device
  277. // 收集所有在库设备(状态为2)
  278. if device.T_State == 2 {
  279. inStockDevices = append(inStockDevices, device)
  280. }
  281. }
  282. // 创建结果数据结构
  283. type DeviceCheckResult struct {
  284. T_sn string `json:"t_sn"` // 设备SN
  285. T_exists bool `json:"t_exists"` // 是否存在于库存
  286. T_state int `json:"t_state"` // 设备状态:1-已出库 2-未出库/入库
  287. T_state_desc string `json:"t_state_desc"` // 状态描述
  288. T_product_id int `json:"t_product_id"` // 产品ID
  289. T_product_name string `json:"t_product_name"` // 产品名称
  290. T_product_model string `json:"t_product_model"` // 产品型号
  291. T_iccid string `json:"t_iccid"` // SIM卡号
  292. T_imei string `json:"t_imei"` // 模组IMEI
  293. T_in_number string `json:"t_in_number"` // 入库编号
  294. T_out_number string `json:"t_out_number"` // 出库编号
  295. T_from_input bool `json:"t_from_input"` // 是否来自输入列表
  296. }
  297. // 处理每个输入的SN
  298. var results []DeviceCheckResult
  299. var processedSNs = make(map[string]bool) // 用于跟踪已处理的SN
  300. // 解析输入的SN列表(如果有)
  301. var snList []string
  302. if len(T_sn_list) > 0 {
  303. snList = lib.SplitString(T_sn_list, ",")
  304. for _, sn := range snList {
  305. sn = strings.TrimSpace(sn)
  306. if len(sn) == 0 {
  307. continue
  308. }
  309. result := DeviceCheckResult{
  310. T_sn: sn,
  311. T_exists: false,
  312. T_from_input: true, // 标记为来自输入列表
  313. }
  314. // 检查设备是否存在于库存
  315. if device, exists := deviceMap[sn]; exists {
  316. result.T_exists = true
  317. result.T_state = device.T_State
  318. result.T_product_id = device.T_product_id
  319. result.T_product_name = device.T_product_name
  320. result.T_product_model = device.T_product_model
  321. result.T_iccid = device.T_iccid
  322. result.T_imei = device.T_imei
  323. result.T_in_number = device.T_in_number
  324. result.T_out_number = device.T_out_number
  325. // 设置状态描述
  326. if device.T_State == 1 {
  327. result.T_state_desc = "已出库"
  328. } else if device.T_State == 2 {
  329. result.T_state_desc = "在库"
  330. } else {
  331. result.T_state_desc = "未知状态"
  332. }
  333. } else {
  334. // 设备不存在于库存
  335. result.T_state_desc = "未入库"
  336. }
  337. results = append(results, result)
  338. processedSNs[sn] = true
  339. }
  340. }
  341. // 添加所有在库设备(如果还没有处理过)
  342. for _, device := range inStockDevices {
  343. // 如果这个SN已经处理过(来自输入列表),则跳过
  344. if _, processed := processedSNs[device.T_sn]; processed {
  345. continue
  346. }
  347. result := DeviceCheckResult{
  348. T_sn: device.T_sn,
  349. T_exists: true,
  350. T_state: device.T_State,
  351. T_state_desc: "在库",
  352. T_product_id: device.T_product_id,
  353. T_product_name: device.T_product_name,
  354. T_product_model: device.T_product_model,
  355. T_iccid: device.T_iccid,
  356. T_imei: device.T_imei,
  357. T_in_number: device.T_in_number,
  358. T_out_number: device.T_out_number,
  359. T_from_input: false, // 标记为不是来自输入列表
  360. }
  361. results = append(results, result)
  362. }
  363. // 统计结果
  364. totalInputCount := 0
  365. existsCount := 0
  366. inStockCount := 0
  367. outStockCount := 0
  368. notExistsCount := 0
  369. for _, result := range results {
  370. if result.T_from_input {
  371. totalInputCount++
  372. if result.T_exists {
  373. existsCount++
  374. if result.T_state == 2 {
  375. inStockCount++
  376. } else if result.T_state == 1 {
  377. outStockCount++
  378. }
  379. } else {
  380. notExistsCount++
  381. }
  382. }
  383. }
  384. // 组装返回数据
  385. r_jsons := map[string]interface{}{
  386. "devices": results,
  387. "stats": map[string]interface{}{
  388. "total_input": totalInputCount,
  389. "exists": existsCount,
  390. "in_stock": inStockCount,
  391. "out_stock": outStockCount,
  392. "not_exists": notExistsCount,
  393. "total_in_stock": len(inStockDevices),
  394. },
  395. }
  396. c.Data["json"] = lib.JSONS{Code: 200, Msg: "盘点完成", Data: r_jsons}
  397. c.ServeJSON()
  398. return
  399. }
  400. func (c *StockController) Stock_List() {
  401. // 分页参数 初始化
  402. page, _ := c.GetInt("page")
  403. if page < 1 {
  404. page = 1
  405. }
  406. page_z, _ := c.GetInt("page_z")
  407. if page_z < 1 {
  408. page_z = conf.Page_size
  409. }
  410. // 查询
  411. T_depot_id, _ := c.GetInt("T_depot_id")
  412. // 查询
  413. T_product_name := c.GetString("T_product_name")
  414. T_product_model := c.GetString("T_product_model")
  415. T_product_class, _ := c.GetInt("T_product_class")
  416. userList, _ := NatsServer.Read_User_List_All()
  417. Account.Read_User_All_Map(userList)
  418. Basic.Read_Depot_All_Map()
  419. StockDao := Stock.NewStock(orm.NewOrm())
  420. R_List, R_cnt := StockDao.Read_Stock_List(T_depot_id, T_product_class, T_product_name, T_product_model, page, page_z)
  421. var r_jsons lib.R_JSONS
  422. r_jsons.Num = R_cnt
  423. r_jsons.Data = R_List
  424. r_jsons.Page = page
  425. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  426. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  427. c.ServeJSON()
  428. return
  429. }
  430. // 修改库存排序
  431. func (c *StockController) Stock_Edit_Sort() {
  432. T_id, _ := c.GetInt("T_id")
  433. T_sort, _ := c.GetInt("T_sort")
  434. StockDao := Stock.NewStock(orm.NewOrm())
  435. Stock_r, err := StockDao.Read_Stock_ById(T_id)
  436. if err != nil {
  437. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  438. c.ServeJSON()
  439. return
  440. }
  441. Stock_r.T_sort = T_sort
  442. if err = StockDao.Update_Stock(Stock_r, "T_sort"); err != nil {
  443. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  444. c.ServeJSON()
  445. return
  446. }
  447. NatsServer.AddUserLogs(c.User.T_uuid, "库存", "修改", Stock_r)
  448. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  449. c.ServeJSON()
  450. return
  451. }
  452. func (c *StockController) Stock_Detail_List() {
  453. // 查询
  454. T_depot_id, _ := c.GetInt("T_depot_id")
  455. // 查询
  456. T_product_id, _ := c.GetInt("T_product_id")
  457. T_start_date := c.GetString("T_start_date")
  458. T_end_date := c.GetString("T_end_date")
  459. now := time.Now()
  460. if len(T_start_date) == 0 {
  461. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  462. } else {
  463. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  464. T_start_date = T_start_date_t.Format("2006-01")
  465. }
  466. if len(T_end_date) == 0 {
  467. T_end_date = now.Format("2006-01")
  468. } else {
  469. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  470. T_end_date = T_end_date_t.Format("2006-01")
  471. }
  472. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  473. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  474. R_List := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, 0, T_start_date, T_end_date)
  475. for i := 0; i < len(R_List); i++ {
  476. R_List[i].T_project = strings.Join(StockOutDao.Read_StockOut_T_project(T_depot_id, R_List[i].T_product_id, R_List[i].T_month), "|")
  477. }
  478. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  479. c.ServeJSON()
  480. return
  481. }
  482. func (c *StockController) Stock_Detail_Excel() {
  483. // 查询
  484. T_depot_id, _ := c.GetInt("T_depot_id")
  485. T_product_id, _ := c.GetInt("T_product_id")
  486. // 查询
  487. T_start_date := c.GetString("T_start_date")
  488. T_end_date := c.GetString("T_end_date")
  489. now := time.Now()
  490. if len(T_start_date) == 0 {
  491. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  492. } else {
  493. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  494. T_start_date = T_start_date_t.Format("2006-01")
  495. }
  496. if len(T_end_date) == 0 {
  497. T_end_date = now.Format("2006-01")
  498. } else {
  499. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  500. T_end_date = T_end_date_t.Format("2006-01")
  501. }
  502. var class_List []Basic.ProductClass_R
  503. filename := fmt.Sprintf("进销存(%s)", lib.GetRandstring(6, "0123456789", 0))
  504. if T_product_id > 0 {
  505. product, _ := Basic.Read_Product_ById(T_product_id)
  506. filename = fmt.Sprintf("进销存-%s明细(%s)", product.T_name, lib.GetRandstring(6, "0123456789", 0))
  507. class, _ := Basic.Read_ProductClass_ById(product.T_class)
  508. class_List = append(class_List, Basic.ProductClassToProductClass_R(class))
  509. } else {
  510. class_List, _ = Basic.Read_ProductClass_List("", 0, 0, 9999)
  511. }
  512. f := excelize.NewFile() // 设置单元格的值
  513. Style1, _ := f.NewStyle(
  514. &excelize.Style{
  515. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  516. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  517. })
  518. Style2, _ := f.NewStyle(
  519. &excelize.Style{
  520. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  521. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  522. Border: []excelize.Border{
  523. {Type: "left", Color: "000000", Style: 1},
  524. {Type: "top", Color: "000000", Style: 1},
  525. {Type: "bottom", Color: "000000", Style: 1},
  526. {Type: "right", Color: "000000", Style: 1},
  527. },
  528. })
  529. Style3, _ := f.NewStyle(
  530. &excelize.Style{
  531. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体", Color: "FF0000"},
  532. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  533. Border: []excelize.Border{
  534. {Type: "left", Color: "000000", Style: 1},
  535. {Type: "top", Color: "000000", Style: 1},
  536. {Type: "bottom", Color: "000000", Style: 1},
  537. {Type: "right", Color: "000000", Style: 1},
  538. },
  539. })
  540. Style4, _ := f.NewStyle(
  541. &excelize.Style{
  542. Font: &excelize.Font{Size: 10, Family: "宋体"},
  543. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  544. Border: []excelize.Border{
  545. {Type: "left", Color: "000000", Style: 1},
  546. {Type: "top", Color: "000000", Style: 1},
  547. {Type: "bottom", Color: "000000", Style: 1},
  548. {Type: "right", Color: "000000", Style: 1},
  549. },
  550. })
  551. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  552. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  553. var j = 0
  554. for _, r := range class_List {
  555. StockList := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, r.Id, T_start_date, T_end_date)
  556. if len(StockList) == 0 {
  557. continue
  558. }
  559. if j == 0 {
  560. f.SetSheetName("Sheet1", r.T_name)
  561. } else {
  562. f.NewSheet(r.T_name)
  563. }
  564. j += 1
  565. f.MergeCell(r.T_name, "A1", "K1")
  566. f.SetRowStyle(r.T_name, 1, 1, Style1)
  567. f.SetCellValue(r.T_name, "A1", fmt.Sprintf("宝智达科技产品进销存统计表"))
  568. f.SetRowHeight(r.T_name, 1, 30)
  569. // 这里设置表头
  570. f.SetCellStyle(r.T_name, "A2", "K2", Style2)
  571. f.SetRowHeight(r.T_name, 2, 20)
  572. f.SetCellValue(r.T_name, "A2", "序号")
  573. f.SetCellValue(r.T_name, "B2", "产品名称")
  574. f.SetCellValue(r.T_name, "C2", "型号")
  575. f.SetCellValue(r.T_name, "D2", "规格")
  576. f.SetCellValue(r.T_name, "E2", "月份")
  577. f.SetCellValue(r.T_name, "F2", "期初库存")
  578. f.SetCellValue(r.T_name, "G2", "当月入库(正常生产新购入库产品)")
  579. f.SetCellValue(r.T_name, "H2", "退货入库(退还重新入库产品)")
  580. f.SetCellValue(r.T_name, "I2", "当月出库")
  581. f.SetCellValue(r.T_name, "J2", "期末库存")
  582. f.SetCellValue(r.T_name, "K2", "出库项目")
  583. //f.SetCellValue(r.T_name, "K2", "备注")
  584. // 设置列宽
  585. f.SetColWidth(r.T_name, "A", "A", 10)
  586. f.SetColWidth(r.T_name, "B", "B", 10)
  587. f.SetColWidth(r.T_name, "C", "D", 10)
  588. f.SetColWidth(r.T_name, "D", "D", 10)
  589. f.SetColWidth(r.T_name, "E", "E", 10)
  590. f.SetColWidth(r.T_name, "F", "F", 10)
  591. f.SetColWidth(r.T_name, "G", "G", 10)
  592. f.SetColWidth(r.T_name, "H", "H", 10)
  593. f.SetColWidth(r.T_name, "I", "I", 10)
  594. f.SetColWidth(r.T_name, "J", "J", 10)
  595. f.SetColWidth(r.T_name, "K", "K", 70)
  596. //f.SetColWidth(r.T_name, "K", "K", 10)
  597. line := 2
  598. // 循环写入数据
  599. for _, v := range StockList {
  600. line++
  601. product, _ := Basic.Read_Product_ById(v.T_product_id)
  602. f.SetCellValue(r.T_name, fmt.Sprintf("A%d", line), line-2)
  603. f.SetCellValue(r.T_name, fmt.Sprintf("B%d", line), product.T_name)
  604. f.SetCellValue(r.T_name, fmt.Sprintf("C%d", line), product.T_model)
  605. f.SetCellValue(r.T_name, fmt.Sprintf("D%d", line), product.T_spec)
  606. f.SetCellValue(r.T_name, fmt.Sprintf("E%d", line), v.T_month)
  607. f.SetCellValue(r.T_name, fmt.Sprintf("F%d", line), v.T_beginning)
  608. f.SetCellValue(r.T_name, fmt.Sprintf("G%d", line), v.T_in)
  609. f.SetCellValue(r.T_name, fmt.Sprintf("H%d", line), v.T_return)
  610. f.SetCellValue(r.T_name, fmt.Sprintf("I%d", line), v.T_out)
  611. f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_ending)
  612. if product.T_early_warning > v.T_ending {
  613. f.SetCellStyle(r.T_name, fmt.Sprintf("J%d", line), fmt.Sprintf("J%d", line), Style3)
  614. } else {
  615. f.SetCellStyle(r.T_name, fmt.Sprintf("J%d", line), fmt.Sprintf("J%d", line), Style4)
  616. }
  617. T_project := StockOutDao.Read_StockOut_T_project(T_depot_id, product.Id, v.T_month)
  618. //f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_project)
  619. f.SetCellValue(r.T_name, fmt.Sprintf("K%d", line), strings.Join(T_project, "\r\n"))
  620. }
  621. f.SetCellStyle(r.T_name, "A2", fmt.Sprintf("I%d", line), Style4)
  622. Style5, _ := f.NewStyle(
  623. &excelize.Style{
  624. Font: &excelize.Font{Size: 10, Family: "宋体"},
  625. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  626. Border: []excelize.Border{
  627. {Type: "left", Color: "000000", Style: 1},
  628. {Type: "top", Color: "000000", Style: 1},
  629. {Type: "bottom", Color: "000000", Style: 1},
  630. {Type: "right", Color: "000000", Style: 1},
  631. },
  632. })
  633. f.SetCellStyle(r.T_name, "K2", fmt.Sprintf("K%d", line), Style5)
  634. }
  635. // 保存文件
  636. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  637. fmt.Println(err)
  638. }
  639. var url string
  640. //// 上传 OSS
  641. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  642. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  643. if !is {
  644. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  645. c.ServeJSON()
  646. return
  647. }
  648. //删除目录
  649. err := os.Remove("ofile/" + filename + ".xlsx")
  650. if err != nil {
  651. logs.Error(lib.FuncName(), err)
  652. }
  653. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  654. c.ServeJSON()
  655. return
  656. }
  657. func (c *StockController) StockIn_List() {
  658. // 分页参数 初始化
  659. page, _ := c.GetInt("page")
  660. if page < 1 {
  661. page = 1
  662. }
  663. page_z, _ := c.GetInt("page_z")
  664. if page_z < 1 {
  665. page_z = conf.Page_size
  666. }
  667. // 查询
  668. T_depot_id, _ := c.GetInt("T_depot_id")
  669. T_start_date := c.GetString("T_start_date")
  670. T_end_date := c.GetString("T_end_date")
  671. T_name := c.GetString("T_name")
  672. T_batch_number := c.GetString("T_batch_number")
  673. userList, _ := NatsServer.Read_User_List_All()
  674. Account.Read_User_All_Map(userList)
  675. Basic.Read_Depot_All_Map()
  676. StockInDao := Stock.NewStockIn(orm.NewOrm())
  677. R_List, R_cnt := StockInDao.Read_StockIn_List(T_batch_number, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  678. var r_jsons lib.R_JSONS
  679. r_jsons.Num = R_cnt
  680. r_jsons.Data = R_List
  681. r_jsons.Page = page
  682. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  683. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  684. c.ServeJSON()
  685. return
  686. }
  687. func (c *StockController) StockIn_List_Product() {
  688. // 分页参数 初始化
  689. page, _ := c.GetInt("page")
  690. if page < 1 {
  691. page = 1
  692. }
  693. page_z, _ := c.GetInt("page_z")
  694. if page_z < 1 {
  695. page_z = conf.Page_size
  696. }
  697. // 查询
  698. T_depot_id, _ := c.GetInt("T_depot_id")
  699. T_name := c.GetString("T_name")
  700. T_start_date := c.GetString("T_start_date")
  701. T_end_date := c.GetString("T_end_date")
  702. T_product_name := c.GetString("T_product_name") // 产品名称
  703. T_product_model := c.GetString("T_product_model") // 产品型号
  704. userList, _ := NatsServer.Read_User_List_All()
  705. Account.Read_User_All_Map(userList)
  706. Basic.Read_Depot_All_Map()
  707. StockInDao := Stock.NewStockIn(orm.NewOrm())
  708. R_List, R_cnt := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, page, page_z)
  709. var r_jsons lib.R_JSONS
  710. r_jsons.Num = R_cnt
  711. r_jsons.Data = R_List
  712. r_jsons.Page = page
  713. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  714. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  715. c.ServeJSON()
  716. return
  717. }
  718. func (c *StockController) StockIn_List_Product_Excel() {
  719. // 查询
  720. T_depot_id, _ := c.GetInt("T_depot_id")
  721. T_name := c.GetString("T_name")
  722. T_start_date := c.GetString("T_start_date")
  723. T_end_date := c.GetString("T_end_date")
  724. T_product_name := c.GetString("T_product_name") // 产品名称
  725. T_product_model := c.GetString("T_product_model") // 产品型号
  726. userList, _ := NatsServer.Read_User_List_All()
  727. Account.Read_User_All_Map(userList)
  728. Basic.Read_Depot_All_Map()
  729. StockInDao := Stock.NewStockIn(orm.NewOrm())
  730. R_List, _ := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, 0, 9999)
  731. filename := fmt.Sprintf("入库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  732. f := excelize.NewFile() // 设置单元格的值
  733. Style1, _ := f.NewStyle(
  734. &excelize.Style{
  735. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  736. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  737. })
  738. Style2, _ := f.NewStyle(
  739. &excelize.Style{
  740. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  741. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  742. Border: []excelize.Border{
  743. {Type: "left", Color: "000000", Style: 1},
  744. {Type: "top", Color: "000000", Style: 1},
  745. {Type: "bottom", Color: "000000", Style: 1},
  746. {Type: "right", Color: "000000", Style: 1},
  747. },
  748. })
  749. Style3, _ := f.NewStyle(
  750. &excelize.Style{
  751. Font: &excelize.Font{Size: 11, Family: "宋体"},
  752. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  753. Border: []excelize.Border{
  754. {Type: "left", Color: "000000", Style: 1},
  755. {Type: "top", Color: "000000", Style: 1},
  756. {Type: "bottom", Color: "000000", Style: 1},
  757. {Type: "right", Color: "000000", Style: 1},
  758. },
  759. })
  760. f.MergeCell("Sheet1", "A1", "L1")
  761. f.SetRowStyle("Sheet1", 1, 1, Style1)
  762. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("入库明细单"))
  763. f.SetRowHeight("Sheet1", 1, 30)
  764. // 这里设置表头
  765. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  766. f.SetRowHeight("Sheet1", 2, 20)
  767. f.SetCellValue("Sheet1", "A2", "序号")
  768. f.SetCellValue("Sheet1", "B2", "入库单号")
  769. f.SetCellValue("Sheet1", "C2", "经办人")
  770. f.SetCellValue("Sheet1", "D2", "入库日期")
  771. f.SetCellValue("Sheet1", "E2", "入库仓库")
  772. f.SetCellValue("Sheet1", "F2", "入库类型")
  773. f.SetCellValue("Sheet1", "G2", "产品名称")
  774. f.SetCellValue("Sheet1", "H2", "产品型号")
  775. f.SetCellValue("Sheet1", "I2", "数量")
  776. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  777. f.SetCellValue("Sheet1", "K2", "备注")
  778. f.SetCellValue("Sheet1", "L2", "SN")
  779. // 设置列宽
  780. f.SetColWidth("Sheet1", "A", "A", 10)
  781. f.SetColWidth("Sheet1", "B", "B", 15)
  782. f.SetColWidth("Sheet1", "C", "D", 10)
  783. f.SetColWidth("Sheet1", "D", "D", 10)
  784. f.SetColWidth("Sheet1", "E", "E", 10)
  785. f.SetColWidth("Sheet1", "F", "F", 10)
  786. f.SetColWidth("Sheet1", "G", "G", 20)
  787. f.SetColWidth("Sheet1", "H", "H", 15)
  788. f.SetColWidth("Sheet1", "I", "I", 10)
  789. f.SetColWidth("Sheet1", "J", "J", 12)
  790. f.SetColWidth("Sheet1", "K", "K", 30)
  791. f.SetColWidth("Sheet1", "L", "L", 100)
  792. line := 2
  793. for _, product := range R_List {
  794. line += 1
  795. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  796. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  797. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_submit_name)
  798. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  799. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  800. var type_str string
  801. if product.T_type == 1 {
  802. type_str = "入库"
  803. } else {
  804. type_str = "退库"
  805. }
  806. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), type_str)
  807. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  808. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  809. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  810. var relation_sn string
  811. if product.T_product_relation_sn == 1 {
  812. relation_sn = "是"
  813. } else {
  814. relation_sn = "否"
  815. }
  816. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  817. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  818. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  819. }
  820. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  821. // 保存文件
  822. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  823. fmt.Println(err)
  824. }
  825. var url string
  826. //// 上传 OSS
  827. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  828. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  829. if !is {
  830. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  831. c.ServeJSON()
  832. return
  833. }
  834. //删除目录
  835. err := os.Remove("ofile/" + filename + ".xlsx")
  836. if err != nil {
  837. logs.Error(lib.FuncName(), err)
  838. }
  839. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  840. c.ServeJSON()
  841. return
  842. }
  843. func (c *StockController) StockIn_Get() {
  844. // 查询
  845. T_number := c.GetString("T_number")
  846. o := orm.NewOrm()
  847. StockInDao := Stock.NewStockIn(o)
  848. StockInProductDao := Stock.NewStockInProduct(o)
  849. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  850. if err != nil {
  851. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  852. c.ServeJSON()
  853. return
  854. }
  855. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  856. var pList []Stock.StockInProduct_R
  857. for _, v := range productList {
  858. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  859. }
  860. userList, _ := NatsServer.Read_User_List_All()
  861. Account.Read_User_All_Map(userList)
  862. Basic.Read_Depot_All_Map()
  863. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockInToStockIn_Detail(stockIn, pList)}
  864. c.ServeJSON()
  865. return
  866. }
  867. func (c *StockController) StockIn_generate_number() {
  868. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  869. rand_x := 0
  870. T_number := ""
  871. for true {
  872. T_number = "RK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  873. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  874. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  875. break
  876. }
  877. rand_x += 1
  878. }
  879. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  880. c.ServeJSON()
  881. return
  882. }
  883. func (c *StockController) StockIn_Add() {
  884. o := orm.NewOrm()
  885. o.Begin()
  886. StockInDao := Stock.NewStockIn(o)
  887. T_number := c.GetString("T_number")
  888. // 检查前端传入的T_number是否已提交过
  889. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  890. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  891. // 数据库查询错误
  892. o.Rollback()
  893. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  894. c.ServeJSON()
  895. return
  896. }
  897. if err == nil {
  898. // 入库编号已存在,不需要做任何处理
  899. o.Rollback()
  900. c.Data["json"] = lib.JSONS{Code: 200, Msg: "入库编号已存在,无需重复提交!", Data: T_number}
  901. c.ServeJSON()
  902. return
  903. }
  904. T_depot_id, _ := c.GetInt("T_depot_id")
  905. T_type, _ := c.GetInt("T_type") // 1-入库 2-退库
  906. T_date := c.GetString("T_date")
  907. T_product := c.GetString("T_product")
  908. T_remark := c.GetString("T_remark")
  909. T_project := c.GetString("T_project")
  910. T_return_user := c.GetString("T_return_user")
  911. T_batch_number := c.GetString("T_batch_number")
  912. date, is := lib.DateStrToTime(T_date)
  913. if !is {
  914. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  915. c.ServeJSON()
  916. return
  917. }
  918. if T_type == 2 && len(T_project) == 0 {
  919. c.Data["json"] = lib.JSONS{Code: 202, Msg: "退库时,项目不能为空!"}
  920. c.ServeJSON()
  921. return
  922. }
  923. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
  924. var_ := Stock.StockIn{
  925. T_number: T_number,
  926. T_depot_id: T_depot_id,
  927. T_type: T_type,
  928. T_date: T_date,
  929. T_remark: T_remark,
  930. T_submit: c.User.T_uuid,
  931. T_project: T_project,
  932. T_return_user: T_return_user,
  933. T_batch_number: T_batch_number,
  934. T_state: Stock.StockInWarehouse,
  935. T_warehouse: c.User.T_uuid,
  936. }
  937. StockInProductDao := Stock.NewStockInProduct(o)
  938. StockDao := Stock.NewStock(o)
  939. DeviceDao := Stock.NewDevice(o)
  940. _, err = StockInDao.Add_StockIn(var_)
  941. if err != nil {
  942. o.Rollback()
  943. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  944. c.ServeJSON()
  945. return
  946. }
  947. if len(T_product) == 0 {
  948. o.Rollback()
  949. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  950. c.ServeJSON()
  951. return
  952. }
  953. productList := lib.SplitString(T_product, "|")
  954. allProductList := []int{}
  955. for _, v := range productList {
  956. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  957. allProductList = append(allProductList, product_id)
  958. product, _ := Basic.Read_Product_ById(product_id)
  959. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  960. T_relation_sn := strings.Split(v, "-")[2]
  961. if T_relation_sn == "" && product.T_relation_sn == 1 {
  962. o.Rollback()
  963. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  964. c.ServeJSON()
  965. return
  966. }
  967. if len(T_relation_sn) > 0 {
  968. snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
  969. num = len(snList)
  970. for _, sn := range snList {
  971. mqtt := Stock.Read_MqttUser(sn)
  972. // 添加设备
  973. device := Stock.Device{
  974. T_contract_number: "",
  975. T_out_number: "",
  976. T_product_id: product_id,
  977. T_in_number: T_number,
  978. T_sn: sn,
  979. T_iccid: mqtt.Iccid,
  980. T_imei: mqtt.Imei,
  981. T_State: 2,
  982. T_batch_number: T_batch_number,
  983. CreateTime: date,
  984. }
  985. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  986. if err != nil {
  987. o.Rollback()
  988. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  989. c.ServeJSON()
  990. return
  991. }
  992. }
  993. }
  994. stockInProduct := Stock.StockInProduct{
  995. T_number: T_number,
  996. T_product_id: product_id,
  997. T_depot_id: T_depot_id,
  998. T_num: num, // 入库数量
  999. T_date: T_date, // 入库日期
  1000. T_relation_sn: T_relation_sn,
  1001. T_batch_number: T_batch_number,
  1002. }
  1003. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  1004. if err != nil {
  1005. o.Rollback()
  1006. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1007. c.ServeJSON()
  1008. return
  1009. }
  1010. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  1011. if err != nil {
  1012. o.Rollback()
  1013. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1014. c.ServeJSON()
  1015. return
  1016. }
  1017. }
  1018. // 批量更新所有相关设备的批次号
  1019. err = DeviceDao.Update_Device_BatchNumber_ByInNumber(T_number, T_batch_number)
  1020. if err != nil {
  1021. o.Rollback()
  1022. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新设备批次号失败"}
  1023. c.ServeJSON()
  1024. return
  1025. }
  1026. o.Commit()
  1027. StockIn_Edit_StockMonth(T_date, T_depot_id, allProductList)
  1028. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
  1029. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1030. c.ServeJSON()
  1031. return
  1032. }
  1033. func StockIn_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  1034. if len(T_date) == 0 {
  1035. return errors.New("日期不能为空")
  1036. }
  1037. date, _ := lib.DateStrToTime(T_date)
  1038. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  1039. // // 修改本月数据不同步库存
  1040. // return nil
  1041. //}
  1042. //T_month := date.Format("2006-01")
  1043. months := generateMonthList(date, "in")
  1044. o := orm.NewOrm()
  1045. StockOutDao := Stock.NewStockOut(o)
  1046. StockMonthDao := Stock.NewStockMonth(o)
  1047. StockOutProductDao := Stock.NewStockOutProduct(o)
  1048. StockInProductDao := Stock.NewStockInProduct(o)
  1049. for _, T_product_id := range allProductList {
  1050. for _, T_month := range months {
  1051. // 获取当前产品本月出入库数量
  1052. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  1053. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  1054. if stockMonth.Id > 0 {
  1055. // 修改
  1056. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  1057. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  1058. if len(lastMonth) > 0 {
  1059. stockMonth.T_beginning = lastMonth[0].T_ending
  1060. } else {
  1061. stockMonth.T_beginning = 0
  1062. }
  1063. // 期末库存 = 期初库存+入库-出库
  1064. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  1065. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_return", "T_beginning", "T_ending")
  1066. if err != nil {
  1067. return err
  1068. }
  1069. } else {
  1070. product, _ := Basic.Read_Product_ById(T_product_id)
  1071. // 添加库存记录
  1072. stockMonth = Stock.StockMonth{
  1073. T_depot_id: T_depot_id,
  1074. T_product_id: T_product_id,
  1075. T_product_class: product.T_class,
  1076. T_month: T_month,
  1077. }
  1078. // 获取当前产品本月出入库数量
  1079. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  1080. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  1081. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  1082. if len(lastMonth) > 0 {
  1083. stockMonth.T_beginning = lastMonth[0].T_ending
  1084. } else {
  1085. stockMonth.T_beginning = 0
  1086. }
  1087. // 期末库存 = 期初库存+入库-出库
  1088. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  1089. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  1090. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  1091. if err != nil {
  1092. return err
  1093. }
  1094. }
  1095. }
  1096. }
  1097. return nil
  1098. }
  1099. func (c *StockController) StockIn_Edit() {
  1100. T_number := c.GetString("T_number") // 入库单号
  1101. //T_depot_id, _ := c.GetInt("T_depot_id")
  1102. T_date := c.GetString("T_date")
  1103. T_product := c.GetString("T_product")
  1104. T_remark := c.GetString("T_remark")
  1105. T_project := c.GetString("T_project")
  1106. T_return_user := c.GetString("T_return_user")
  1107. T_batch_number := c.GetString("T_batch_number")
  1108. date, is := lib.DateStrToTime(T_date)
  1109. if !is {
  1110. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1111. c.ServeJSON()
  1112. return
  1113. }
  1114. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  1115. T_date = ""
  1116. }
  1117. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  1118. o := orm.NewOrm()
  1119. o.Begin()
  1120. StockInDao := Stock.NewStockIn(o)
  1121. StockInProductDao := Stock.NewStockInProduct(o)
  1122. StockDao := Stock.NewStock(o)
  1123. DeviceDao := Stock.NewDevice(o)
  1124. // 查询入库信息
  1125. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1126. if err != nil {
  1127. o.Rollback()
  1128. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1129. c.ServeJSON()
  1130. return
  1131. }
  1132. T_old_date := stockIn.T_date
  1133. // 查询入库产品信息
  1134. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1135. var oldProductList []dto.StockProduct
  1136. var newProductList []dto.StockProduct
  1137. allProductListMap := make(map[int]struct{})
  1138. productOldMap := map[int]dto.StockProduct{}
  1139. for _, product := range productOldList {
  1140. stockProduct := dto.StockProduct{
  1141. T_product_id: product.T_product_id,
  1142. T_num: product.T_num,
  1143. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1144. }
  1145. oldProductList = append(oldProductList, stockProduct)
  1146. productOldMap[product.T_product_id] = stockProduct
  1147. if _, ok := allProductListMap[product.T_product_id]; !ok {
  1148. allProductListMap[product.T_product_id] = struct{}{}
  1149. }
  1150. }
  1151. productNewList := lib.SplitString(T_product, "|")
  1152. productNewMap := map[int]dto.StockProduct{}
  1153. for _, v := range productNewList {
  1154. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1155. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1156. T_relation_sn := strings.Split(v, "-")[2]
  1157. snList := lib.SplitString(T_relation_sn, ",")
  1158. if len(snList) > 0 {
  1159. num = len(snList)
  1160. }
  1161. stockProduct := dto.StockProduct{
  1162. T_product_id: product_id,
  1163. T_num: num,
  1164. T_relation_sn: snList,
  1165. }
  1166. newProductList = append(newProductList, stockProduct)
  1167. productNewMap[product_id] = stockProduct
  1168. if _, ok := allProductListMap[product_id]; !ok {
  1169. allProductListMap[product_id] = struct{}{}
  1170. }
  1171. }
  1172. // 判断产品列表信息是否相同
  1173. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  1174. // 两次提交的数据相同,则不用修改
  1175. if !StockProductListIsEqual {
  1176. if len(needDelete) > 0 {
  1177. // 删除入库产品列表
  1178. for _, v := range needDelete {
  1179. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1180. // 删除入库产品列表
  1181. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1182. if err != nil {
  1183. o.Rollback()
  1184. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1185. c.ServeJSON()
  1186. return
  1187. }
  1188. if product.T_relation_sn == 1 {
  1189. // 删除该入库单关联sn
  1190. for _, sn := range v.T_relation_sn {
  1191. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1192. if err != nil {
  1193. o.Rollback()
  1194. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1195. c.ServeJSON()
  1196. return
  1197. }
  1198. }
  1199. }
  1200. // 减少库存
  1201. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1202. if err != nil {
  1203. o.Rollback()
  1204. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1205. c.ServeJSON()
  1206. return
  1207. }
  1208. }
  1209. }
  1210. if len(needAdd) > 0 {
  1211. // 新增入库产品列表
  1212. for _, v := range needAdd {
  1213. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1214. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  1215. o.Rollback()
  1216. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  1217. c.ServeJSON()
  1218. return
  1219. }
  1220. // 添加该入库单关联sn
  1221. for _, sn := range v.T_relation_sn {
  1222. mqtt := Stock.Read_MqttUser(sn)
  1223. // 添加设备
  1224. device := Stock.Device{
  1225. T_contract_number: "",
  1226. T_out_number: "",
  1227. T_product_id: v.T_product_id,
  1228. T_in_number: stockIn.T_number,
  1229. T_sn: sn,
  1230. T_iccid: mqtt.Iccid,
  1231. T_imei: mqtt.Imei,
  1232. T_State: 2,
  1233. T_batch_number: T_batch_number,
  1234. CreateTime: date,
  1235. UpdateTime: stockIn.UpdateTime,
  1236. }
  1237. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  1238. if err != nil {
  1239. o.Rollback()
  1240. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1241. c.ServeJSON()
  1242. return
  1243. }
  1244. }
  1245. stockInProduct := Stock.StockInProduct{
  1246. T_number: T_number,
  1247. T_product_id: v.T_product_id,
  1248. T_depot_id: stockIn.T_depot_id,
  1249. T_num: v.T_num, // 入库数量
  1250. T_date: stockIn.T_date, // 入库日期
  1251. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  1252. }
  1253. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  1254. if err != nil {
  1255. o.Rollback()
  1256. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1257. c.ServeJSON()
  1258. return
  1259. }
  1260. // 增加库存
  1261. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  1262. if err != nil {
  1263. o.Rollback()
  1264. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  1265. c.ServeJSON()
  1266. return
  1267. }
  1268. }
  1269. }
  1270. if len(needEdit) > 0 {
  1271. for _, v := range needEdit {
  1272. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1273. stockInProduct := Stock.StockInProduct{
  1274. T_number: stockIn.T_number,
  1275. T_product_id: v.T_product_id,
  1276. T_depot_id: stockIn.T_depot_id,
  1277. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  1278. T_date: stockIn.T_date, // 入库日期
  1279. }
  1280. // 入库数量比之前多,增加库存
  1281. var T_type int
  1282. var T_num int
  1283. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  1284. // 增加库存
  1285. T_type = 2
  1286. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  1287. } else {
  1288. // 减少库存
  1289. T_type = 1
  1290. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  1291. }
  1292. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  1293. if err != nil {
  1294. o.Rollback()
  1295. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1296. c.ServeJSON()
  1297. return
  1298. }
  1299. // 更新产品库存表
  1300. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1301. if err != nil {
  1302. o.Rollback()
  1303. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1304. c.ServeJSON()
  1305. return
  1306. }
  1307. }
  1308. }
  1309. for _, diff := range snDiff {
  1310. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  1311. // 删除减少的sn
  1312. if len(diff.T_delete_relation_sn) > 0 {
  1313. for _, sn := range diff.T_delete_relation_sn {
  1314. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1315. if err != nil {
  1316. o.Rollback()
  1317. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1318. c.ServeJSON()
  1319. return
  1320. }
  1321. }
  1322. // 减少库存
  1323. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_delete_relation_sn), 1)
  1324. if err != nil {
  1325. o.Rollback()
  1326. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1327. c.ServeJSON()
  1328. return
  1329. }
  1330. }
  1331. if len(diff.T_add_relation_sn) > 0 {
  1332. for _, sn := range diff.T_add_relation_sn {
  1333. mqtt := Stock.Read_MqttUser(sn)
  1334. // 添加设备
  1335. device := Stock.Device{
  1336. T_contract_number: "",
  1337. T_out_number: "",
  1338. T_product_id: diff.T_product_id,
  1339. T_in_number: stockIn.T_number,
  1340. T_sn: sn,
  1341. T_iccid: mqtt.Iccid,
  1342. T_imei: mqtt.Imei,
  1343. T_State: 2,
  1344. CreateTime: date,
  1345. UpdateTime: stockIn.UpdateTime,
  1346. }
  1347. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  1348. if err != nil {
  1349. o.Rollback()
  1350. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1351. c.ServeJSON()
  1352. return
  1353. }
  1354. }
  1355. // 增加库存
  1356. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_add_relation_sn), 2)
  1357. if err != nil {
  1358. o.Rollback()
  1359. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1360. c.ServeJSON()
  1361. return
  1362. }
  1363. }
  1364. stockInProduct := Stock.StockInProduct{
  1365. T_number: stockIn.T_number,
  1366. T_product_id: diff.T_product_id,
  1367. T_depot_id: stockIn.T_depot_id,
  1368. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  1369. T_date: stockIn.T_date, // 入库日期
  1370. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  1371. }
  1372. // 更新产品库存表
  1373. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1374. if err != nil {
  1375. o.Rollback()
  1376. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1377. c.ServeJSON()
  1378. return
  1379. }
  1380. }
  1381. }
  1382. if len(T_remark) > 0 {
  1383. stockIn.T_remark = T_remark
  1384. }
  1385. if len(T_date) > 0 {
  1386. stockIn.T_date = T_date
  1387. }
  1388. if len(T_project) > 0 {
  1389. stockIn.T_project = T_project
  1390. }
  1391. if len(T_return_user) > 0 {
  1392. stockIn.T_return_user = T_return_user
  1393. }
  1394. if len(T_batch_number) > 0 {
  1395. stockIn.T_batch_number = T_batch_number
  1396. }
  1397. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_date", "T_project", "T_return_user", "T_batch_number")
  1398. if err != nil {
  1399. o.Rollback()
  1400. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  1401. c.ServeJSON()
  1402. return
  1403. }
  1404. o.Commit()
  1405. if len(T_date) > 0 && T_old_date != T_date {
  1406. // 修改出库产品日期
  1407. StockInProductDao.Update_StockInProduct_T_date(stockIn.T_number, T_date)
  1408. DeviceDao.Update_Device_CreateTimeByT_in_number(stockIn.T_number, T_date)
  1409. }
  1410. // 修改出库产品日期
  1411. StockInProductDao.Update_StockInProduct_T_batch_number(stockIn.T_number, T_batch_number)
  1412. // 修改设备批次号
  1413. DeviceDao.Update_Device_BatchNumber_ByInNumber(stockIn.T_number, T_batch_number)
  1414. // 更新月份统计表
  1415. allProductList := []int{}
  1416. for i, _ := range allProductListMap {
  1417. allProductList = append(allProductList, i)
  1418. }
  1419. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1420. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "修改", stockIn)
  1421. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1422. c.ServeJSON()
  1423. return
  1424. }
  1425. // 删除入库
  1426. func (c *StockController) StockIn_Del() {
  1427. T_number := c.GetString("T_number") // 入库单号
  1428. o := orm.NewOrm()
  1429. o.Begin()
  1430. StockInDao := Stock.NewStockIn(o)
  1431. StockInProductDao := Stock.NewStockInProduct(o)
  1432. StockDao := Stock.NewStock(o)
  1433. DeviceDao := Stock.NewDevice(o)
  1434. // 查询入库信息
  1435. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1436. if err != nil {
  1437. o.Rollback()
  1438. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1439. c.ServeJSON()
  1440. return
  1441. }
  1442. // 查询入库产品信息
  1443. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1444. var oldProductList []dto.StockProduct
  1445. allProductList := []int{}
  1446. for _, product := range productOldList {
  1447. oldProductList = append(oldProductList, dto.StockProduct{
  1448. T_product_id: product.T_product_id,
  1449. T_num: product.T_num,
  1450. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1451. })
  1452. allProductList = append(allProductList, product.T_product_id)
  1453. }
  1454. // 删除入库产品列表
  1455. for _, v := range oldProductList {
  1456. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1457. // 删除入库产品列表
  1458. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1459. if err != nil {
  1460. o.Rollback()
  1461. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1462. c.ServeJSON()
  1463. return
  1464. }
  1465. if product.T_relation_sn == 1 {
  1466. // 删除该入库单关联sn
  1467. for _, sn := range v.T_relation_sn {
  1468. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1469. if err != nil {
  1470. o.Rollback()
  1471. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1472. c.ServeJSON()
  1473. return
  1474. }
  1475. }
  1476. }
  1477. // 减少库存
  1478. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1479. if err != nil {
  1480. o.Rollback()
  1481. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1482. c.ServeJSON()
  1483. return
  1484. }
  1485. }
  1486. err = StockInDao.Delete_StockIn(stockIn)
  1487. if err != nil {
  1488. o.Rollback()
  1489. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  1490. c.ServeJSON()
  1491. return
  1492. }
  1493. o.Commit()
  1494. // 更新月份统计表
  1495. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1496. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "删除", stockIn)
  1497. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1498. c.ServeJSON()
  1499. return
  1500. }
  1501. // 导出出库单
  1502. func (c *StockController) StockIn_Excel() {
  1503. // 查询
  1504. T_number := c.GetString("T_number")
  1505. o := orm.NewOrm()
  1506. StockInDao := Stock.NewStockIn(o)
  1507. StockInProductDao := Stock.NewStockInProduct(o)
  1508. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1509. if err != nil {
  1510. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1511. c.ServeJSON()
  1512. return
  1513. }
  1514. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1515. var pList []Stock.StockInProduct_R
  1516. for _, v := range productList {
  1517. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1518. }
  1519. userList, _ := NatsServer.Read_User_List_All()
  1520. Account.Read_User_All_Map(userList)
  1521. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1522. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1523. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1524. f := excelize.NewFile() // 设置单元格的值
  1525. Style1, _ := f.NewStyle(
  1526. &excelize.Style{
  1527. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1528. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1529. })
  1530. Style2, _ := f.NewStyle(
  1531. &excelize.Style{
  1532. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1533. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1534. })
  1535. Style3, _ := f.NewStyle(
  1536. &excelize.Style{
  1537. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1538. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1539. Border: []excelize.Border{
  1540. {Type: "left", Color: "000000", Style: 1},
  1541. {Type: "top", Color: "000000", Style: 1},
  1542. {Type: "bottom", Color: "000000", Style: 1},
  1543. {Type: "right", Color: "000000", Style: 1},
  1544. },
  1545. })
  1546. Style4, _ := f.NewStyle(
  1547. &excelize.Style{
  1548. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1549. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1550. Border: []excelize.Border{
  1551. {Type: "left", Color: "000000", Style: 1},
  1552. {Type: "top", Color: "000000", Style: 1},
  1553. {Type: "bottom", Color: "000000", Style: 1},
  1554. {Type: "right", Color: "000000", Style: 1},
  1555. },
  1556. })
  1557. productList2 := lib.ChunkBy(productList, 10)
  1558. for i, products := range productList2 {
  1559. if len(products) < 10 {
  1560. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1561. }
  1562. row := i*17 + 1
  1563. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1564. f.SetRowStyle("Sheet1", row, row, Style1)
  1565. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1566. f.SetRowHeight("Sheet1", row, 20)
  1567. row += 1
  1568. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1569. f.SetRowStyle("Sheet1", row, row, Style1)
  1570. title := "硬件入库单"
  1571. if len(productList2) > 1 {
  1572. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1573. }
  1574. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1575. f.SetRowHeight("Sheet1", row, 20)
  1576. row += 1
  1577. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1578. f.SetRowStyle("Sheet1", row, row, Style2)
  1579. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1580. f.SetRowHeight("Sheet1", row, 20)
  1581. row += 1
  1582. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1583. f.SetRowStyle("Sheet1", row, row, Style2)
  1584. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1585. f.SetRowHeight("Sheet1", row, 20)
  1586. row += 1
  1587. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1588. f.SetRowStyle("Sheet1", row, row, Style2)
  1589. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1590. f.SetRowHeight("Sheet1", row, 20)
  1591. // 这里设置表头
  1592. row += 1
  1593. f.SetRowHeight("Sheet1", row, 20)
  1594. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1595. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1596. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1597. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1598. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1599. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1600. // 设置列宽
  1601. f.SetColWidth("Sheet1", "A", "A", 5)
  1602. f.SetColWidth("Sheet1", "B", "B", 20)
  1603. f.SetColWidth("Sheet1", "C", "C", 12)
  1604. f.SetColWidth("Sheet1", "D", "D", 10)
  1605. f.SetColWidth("Sheet1", "E", "E", 10)
  1606. f.SetColWidth("Sheet1", "F", "F", 15)
  1607. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1608. sRow := row
  1609. // 循环写入数据
  1610. for j, v := range products {
  1611. row += 1
  1612. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1613. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1614. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1615. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1616. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1617. if v.T_num > 0 {
  1618. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1619. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1620. }
  1621. }
  1622. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1623. row += 1
  1624. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1625. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1626. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1627. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1628. f.SetRowStyle("Sheet1", row, row, Style2)
  1629. f.SetRowHeight("Sheet1", row, 20)
  1630. }
  1631. // 保存文件
  1632. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1633. fmt.Println(err)
  1634. }
  1635. var url string
  1636. //// 上传 OSS
  1637. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1638. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1639. if !is {
  1640. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1641. c.ServeJSON()
  1642. return
  1643. }
  1644. //删除目录
  1645. err = os.Remove("ofile/" + filename + ".xlsx")
  1646. if err != nil {
  1647. logs.Error(lib.FuncName(), err)
  1648. }
  1649. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1650. c.ServeJSON()
  1651. return
  1652. }
  1653. // 批量导出出库单
  1654. func (c *StockController) StockIn_Excel_Batch() {
  1655. // 查询
  1656. T_number_list := c.GetString("T_number_list")
  1657. o := orm.NewOrm()
  1658. StockInDao := Stock.NewStockIn(o)
  1659. StockInProductDao := Stock.NewStockInProduct(o)
  1660. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  1661. userList, _ := NatsServer.Read_User_List_All()
  1662. Account.Read_User_All_Map(userList)
  1663. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1664. f := excelize.NewFile() // 设置单元格的值
  1665. Style1, _ := f.NewStyle(
  1666. &excelize.Style{
  1667. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1668. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1669. })
  1670. Style2, _ := f.NewStyle(
  1671. &excelize.Style{
  1672. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1673. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1674. })
  1675. Style3, _ := f.NewStyle(
  1676. &excelize.Style{
  1677. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1678. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1679. Border: []excelize.Border{
  1680. {Type: "left", Color: "000000", Style: 1},
  1681. {Type: "top", Color: "000000", Style: 1},
  1682. {Type: "bottom", Color: "000000", Style: 1},
  1683. {Type: "right", Color: "000000", Style: 1},
  1684. },
  1685. })
  1686. Style4, _ := f.NewStyle(
  1687. &excelize.Style{
  1688. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1689. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1690. Border: []excelize.Border{
  1691. {Type: "left", Color: "000000", Style: 1},
  1692. {Type: "top", Color: "000000", Style: 1},
  1693. {Type: "bottom", Color: "000000", Style: 1},
  1694. {Type: "right", Color: "000000", Style: 1},
  1695. },
  1696. })
  1697. var num int
  1698. for _, T_number := range list {
  1699. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1700. if err != nil {
  1701. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1702. c.ServeJSON()
  1703. return
  1704. }
  1705. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1706. var pList []Stock.StockInProduct_R
  1707. for _, v := range productList {
  1708. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1709. }
  1710. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1711. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1712. productList2 := lib.ChunkBy(productList, 10)
  1713. for i, products := range productList2 {
  1714. if len(products) < 10 {
  1715. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1716. }
  1717. row := num*18 + 1
  1718. num += 1
  1719. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1720. f.SetRowStyle("Sheet1", row, row, Style1)
  1721. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1722. f.SetRowHeight("Sheet1", row, 20)
  1723. row += 1
  1724. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1725. f.SetRowStyle("Sheet1", row, row, Style1)
  1726. title := "硬件入库单"
  1727. if len(productList2) > 1 {
  1728. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1729. }
  1730. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1731. f.SetRowHeight("Sheet1", row, 20)
  1732. row += 1
  1733. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1734. f.SetRowStyle("Sheet1", row, row, Style2)
  1735. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1736. f.SetRowHeight("Sheet1", row, 20)
  1737. row += 1
  1738. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1739. f.SetRowStyle("Sheet1", row, row, Style2)
  1740. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1741. f.SetRowHeight("Sheet1", row, 20)
  1742. row += 1
  1743. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1744. f.SetRowStyle("Sheet1", row, row, Style2)
  1745. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1746. f.SetRowHeight("Sheet1", row, 20)
  1747. // 这里设置表头
  1748. row += 1
  1749. f.SetRowHeight("Sheet1", row, 20)
  1750. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1751. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1752. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1753. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1754. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1755. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1756. // 设置列宽
  1757. f.SetColWidth("Sheet1", "A", "A", 5)
  1758. f.SetColWidth("Sheet1", "B", "B", 20)
  1759. f.SetColWidth("Sheet1", "C", "C", 12)
  1760. f.SetColWidth("Sheet1", "D", "D", 10)
  1761. f.SetColWidth("Sheet1", "E", "E", 10)
  1762. f.SetColWidth("Sheet1", "F", "F", 15)
  1763. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1764. sRow := row
  1765. // 循环写入数据
  1766. for j, v := range products {
  1767. row += 1
  1768. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1769. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1770. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1771. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1772. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1773. if v.T_num > 0 {
  1774. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1775. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1776. }
  1777. }
  1778. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1779. row += 1
  1780. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1781. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1782. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1783. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1784. f.SetRowStyle("Sheet1", row, row, Style2)
  1785. f.SetRowHeight("Sheet1", row, 20)
  1786. // 空一行
  1787. row += 1
  1788. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1789. //f.SetRowStyle("Sheet1", row, row, Style1)
  1790. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  1791. // 插入下一页分页符
  1792. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  1793. }
  1794. }
  1795. // 保存文件
  1796. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1797. fmt.Println(err)
  1798. }
  1799. var url string
  1800. //// 上传 OSS
  1801. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1802. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1803. if !is {
  1804. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1805. c.ServeJSON()
  1806. return
  1807. }
  1808. //删除目录
  1809. err := os.Remove("ofile/" + filename + ".xlsx")
  1810. if err != nil {
  1811. logs.Error(lib.FuncName(), err)
  1812. }
  1813. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1814. c.ServeJSON()
  1815. return
  1816. }
  1817. // 退库申请
  1818. func (c *StockController) StockIn_Apply_List() {
  1819. // 分页参数 初始化
  1820. page, _ := c.GetInt("page")
  1821. if page < 1 {
  1822. page = 1
  1823. }
  1824. page_z, _ := c.GetInt("page_z")
  1825. if page_z < 1 {
  1826. page_z = conf.Page_size
  1827. }
  1828. // 查询
  1829. T_depot_id, _ := c.GetInt("T_depot_id")
  1830. T_start_date := c.GetString("T_start_date")
  1831. T_end_date := c.GetString("T_end_date")
  1832. T_name := c.GetString("T_name")
  1833. T_state := c.GetString("T_state")
  1834. userList, _ := NatsServer.Read_User_List_All()
  1835. Account.Read_User_All_Map(userList)
  1836. Basic.Read_Depot_All_Map()
  1837. StockInDao := Stock.NewStockIn(orm.NewOrm())
  1838. R_List, R_cnt := StockInDao.Read_StockIn_Apply_List(Stock.StockInTypeApply, c.User.T_uuid, T_state, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  1839. var r_jsons lib.R_JSONS
  1840. r_jsons.Num = R_cnt
  1841. r_jsons.Data = R_List
  1842. r_jsons.Page = page
  1843. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1844. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1845. c.ServeJSON()
  1846. return
  1847. }
  1848. func (c *StockController) StockIn_Apply_Warehouse_List() {
  1849. // 分页参数 初始化
  1850. page, _ := c.GetInt("page")
  1851. if page < 1 {
  1852. page = 1
  1853. }
  1854. page_z, _ := c.GetInt("page_z")
  1855. if page_z < 1 {
  1856. page_z = conf.Page_size
  1857. }
  1858. // 查询
  1859. T_depot_id, _ := c.GetInt("T_depot_id")
  1860. T_start_date := c.GetString("T_start_date")
  1861. T_end_date := c.GetString("T_end_date")
  1862. T_name := c.GetString("T_name")
  1863. T_state := c.GetString("T_state")
  1864. T_uuid := c.GetString("T_uuid")
  1865. userList, _ := NatsServer.Read_User_List_All()
  1866. Account.Read_User_All_Map(userList)
  1867. Basic.Read_Depot_All_Map()
  1868. StockInDao := Stock.NewStockIn(orm.NewOrm())
  1869. R_List, R_cnt := StockInDao.Read_StockIn_Apply_List(Stock.StockInTypeWarehouse, T_uuid, T_state, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  1870. var r_jsons lib.R_JSONS
  1871. r_jsons.Num = R_cnt
  1872. r_jsons.Data = R_List
  1873. r_jsons.Page = page
  1874. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1875. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1876. c.ServeJSON()
  1877. return
  1878. }
  1879. func (c *StockController) StockIn_Apply() {
  1880. o := orm.NewOrm()
  1881. o.Begin()
  1882. StockInDao := Stock.NewStockIn(o)
  1883. T_number := c.GetString("T_number")
  1884. // 检查前端传入的T_number是否已提交过
  1885. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1886. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  1887. // 数据库查询错误
  1888. o.Rollback()
  1889. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  1890. c.ServeJSON()
  1891. return
  1892. }
  1893. if err == nil {
  1894. // 入库编号已存在,不需要做任何处理
  1895. o.Rollback()
  1896. c.Data["json"] = lib.JSONS{Code: 200, Msg: "入库编号已存在,无需重复提交!", Data: T_number}
  1897. c.ServeJSON()
  1898. return
  1899. }
  1900. T_depot_id, _ := c.GetInt("T_depot_id")
  1901. T_product := c.GetString("T_product")
  1902. T_remark := c.GetString("T_remark")
  1903. T_project := c.GetString("T_project")
  1904. T_return_user := c.GetString("T_return_user")
  1905. if len(T_return_user) == 0 {
  1906. T_return_user = c.User.T_uuid
  1907. }
  1908. if len(T_project) == 0 {
  1909. c.Data["json"] = lib.JSONS{Code: 202, Msg: "项目不能为空!"}
  1910. c.ServeJSON()
  1911. return
  1912. }
  1913. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "退库", T_product)
  1914. var_ := Stock.StockIn{
  1915. T_number: T_number,
  1916. T_depot_id: T_depot_id,
  1917. T_type: 2,
  1918. T_remark: T_remark,
  1919. T_submit: c.User.T_uuid,
  1920. T_project: T_project,
  1921. T_return_user: T_return_user,
  1922. T_state: 1,
  1923. T_application_date: time.Now().Format("2006-01-02"),
  1924. }
  1925. StockInProductDao := Stock.NewStockInProduct(o)
  1926. _, err = StockInDao.Add_StockIn(var_)
  1927. if err != nil {
  1928. o.Rollback()
  1929. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1930. c.ServeJSON()
  1931. return
  1932. }
  1933. if len(T_product) == 0 {
  1934. o.Rollback()
  1935. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  1936. c.ServeJSON()
  1937. return
  1938. }
  1939. productList := lib.SplitString(T_product, "|")
  1940. allProductList := []int{}
  1941. for _, v := range productList {
  1942. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1943. allProductList = append(allProductList, product_id)
  1944. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1945. stockInProduct := Stock.StockInProduct{
  1946. T_number: T_number,
  1947. T_product_id: product_id,
  1948. T_depot_id: T_depot_id,
  1949. T_num: num, // 入库数量
  1950. }
  1951. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  1952. if err != nil {
  1953. o.Rollback()
  1954. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1955. c.ServeJSON()
  1956. return
  1957. }
  1958. }
  1959. o.Commit()
  1960. NatsServer.AddUserLogs(c.User.T_uuid, "退库申请", "退库申请", var_)
  1961. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1962. c.ServeJSON()
  1963. return
  1964. }
  1965. func (c *StockController) StockIn_Apply_Edit() {
  1966. T_number := c.GetString("T_number") // 入库单号
  1967. //T_depot_id, _ := c.GetInt("T_depot_id")
  1968. T_product := c.GetString("T_product")
  1969. T_remark := c.GetString("T_remark")
  1970. T_project := c.GetString("T_project")
  1971. T_batch_number := c.GetString("T_batch_number")
  1972. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  1973. o := orm.NewOrm()
  1974. o.Begin()
  1975. StockInDao := Stock.NewStockIn(o)
  1976. StockInProductDao := Stock.NewStockInProduct(o)
  1977. // 查询入库信息
  1978. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1979. if err != nil {
  1980. o.Rollback()
  1981. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1982. c.ServeJSON()
  1983. return
  1984. }
  1985. // 查询入库产品信息
  1986. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1987. var oldProductList []dto.StockProduct
  1988. var newProductList []dto.StockProduct
  1989. allProductListMap := make(map[int]struct{})
  1990. productOldMap := map[int]dto.StockProduct{}
  1991. for _, product := range productOldList {
  1992. stockProduct := dto.StockProduct{
  1993. T_product_id: product.T_product_id,
  1994. T_num: product.T_num,
  1995. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1996. }
  1997. oldProductList = append(oldProductList, stockProduct)
  1998. productOldMap[product.T_product_id] = stockProduct
  1999. if _, ok := allProductListMap[product.T_product_id]; !ok {
  2000. allProductListMap[product.T_product_id] = struct{}{}
  2001. }
  2002. }
  2003. productNewList := lib.SplitString(T_product, "|")
  2004. productNewMap := map[int]dto.StockProduct{}
  2005. for _, v := range productNewList {
  2006. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2007. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2008. T_relation_sn := strings.Split(v, "-")[2]
  2009. snList := lib.SplitString(T_relation_sn, ",")
  2010. if len(snList) > 0 {
  2011. num = len(snList)
  2012. }
  2013. stockProduct := dto.StockProduct{
  2014. T_product_id: product_id,
  2015. T_num: num,
  2016. T_relation_sn: snList,
  2017. }
  2018. newProductList = append(newProductList, stockProduct)
  2019. productNewMap[product_id] = stockProduct
  2020. if _, ok := allProductListMap[product_id]; !ok {
  2021. allProductListMap[product_id] = struct{}{}
  2022. }
  2023. }
  2024. // 判断产品列表信息是否相同
  2025. StockProductListIsEqual, needDelete, needAdd, needEdit, _ := dto.StockProductListsEqual(oldProductList, newProductList)
  2026. // 两次提交的数据相同,则不用修改
  2027. if !StockProductListIsEqual {
  2028. if len(needDelete) > 0 {
  2029. // 删除入库产品列表
  2030. for _, v := range needDelete {
  2031. // 删除入库产品列表
  2032. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  2033. if err != nil {
  2034. o.Rollback()
  2035. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2036. c.ServeJSON()
  2037. return
  2038. }
  2039. }
  2040. }
  2041. if len(needAdd) > 0 {
  2042. // 新增入库产品列表
  2043. for _, v := range needAdd {
  2044. stockInProduct := Stock.StockInProduct{
  2045. T_number: T_number,
  2046. T_product_id: v.T_product_id,
  2047. T_depot_id: stockIn.T_depot_id,
  2048. T_num: v.T_num, // 入库数量
  2049. T_date: stockIn.T_date, // 入库日期
  2050. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  2051. }
  2052. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  2053. if err != nil {
  2054. o.Rollback()
  2055. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2056. c.ServeJSON()
  2057. return
  2058. }
  2059. }
  2060. }
  2061. if len(needEdit) > 0 {
  2062. for _, v := range needEdit {
  2063. stockInProduct := Stock.StockInProduct{
  2064. T_number: stockIn.T_number,
  2065. T_product_id: v.T_product_id,
  2066. T_depot_id: stockIn.T_depot_id,
  2067. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  2068. T_date: stockIn.T_date, // 入库日期
  2069. }
  2070. // 更新产品库存表
  2071. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  2072. if err != nil {
  2073. o.Rollback()
  2074. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  2075. c.ServeJSON()
  2076. return
  2077. }
  2078. }
  2079. }
  2080. }
  2081. if len(T_remark) > 0 {
  2082. stockIn.T_remark = T_remark
  2083. }
  2084. if len(T_project) > 0 {
  2085. stockIn.T_project = T_project
  2086. }
  2087. if len(T_batch_number) > 0 {
  2088. stockIn.T_batch_number = T_batch_number
  2089. }
  2090. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_project", "T_batch_number")
  2091. if err != nil {
  2092. o.Rollback()
  2093. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  2094. c.ServeJSON()
  2095. return
  2096. }
  2097. o.Commit()
  2098. NatsServer.AddUserLogs(c.User.T_uuid, "退库", "修改", stockIn)
  2099. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2100. c.ServeJSON()
  2101. return
  2102. }
  2103. // 删除入库
  2104. func (c *StockController) StockIn_Apply_Del() {
  2105. T_number := c.GetString("T_number") // 入库单号
  2106. o := orm.NewOrm()
  2107. o.Begin()
  2108. StockInDao := Stock.NewStockIn(o)
  2109. StockInProductDao := Stock.NewStockInProduct(o)
  2110. // 查询入库信息
  2111. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  2112. if err != nil {
  2113. o.Rollback()
  2114. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2115. c.ServeJSON()
  2116. return
  2117. }
  2118. // 查询入库产品信息
  2119. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  2120. var oldProductList []dto.StockProduct
  2121. allProductList := []int{}
  2122. for _, product := range productOldList {
  2123. oldProductList = append(oldProductList, dto.StockProduct{
  2124. T_product_id: product.T_product_id,
  2125. T_num: product.T_num,
  2126. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2127. })
  2128. allProductList = append(allProductList, product.T_product_id)
  2129. }
  2130. // 删除入库产品列表
  2131. for _, v := range oldProductList {
  2132. // 删除入库产品列表
  2133. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  2134. if err != nil {
  2135. o.Rollback()
  2136. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2137. c.ServeJSON()
  2138. return
  2139. }
  2140. }
  2141. err = StockInDao.Delete_StockIn(stockIn)
  2142. if err != nil {
  2143. o.Rollback()
  2144. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  2145. c.ServeJSON()
  2146. return
  2147. }
  2148. o.Commit()
  2149. NatsServer.AddUserLogs(c.User.T_uuid, "入库申请", "删除", stockIn)
  2150. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2151. c.ServeJSON()
  2152. return
  2153. }
  2154. func (c *StockController) StockIn_Warehouse() {
  2155. T_number := c.GetString("T_number")
  2156. T_date := c.GetString("T_date")
  2157. T_remark := c.GetString("T_remark")
  2158. T_project := c.GetString("T_project")
  2159. T_product := c.GetString("T_product")
  2160. date, is := lib.DateStrToTime(T_date)
  2161. if !is {
  2162. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2163. c.ServeJSON()
  2164. return
  2165. }
  2166. if len(T_project) == 0 {
  2167. c.Data["json"] = lib.JSONS{Code: 202, Msg: "项目不能为空!"}
  2168. c.ServeJSON()
  2169. return
  2170. }
  2171. NatsServer.AddUserLogs(c.User.T_uuid, "退库申请", "确认入库", T_product)
  2172. o := orm.NewOrm()
  2173. o.Begin()
  2174. StockInDao := Stock.NewStockIn(o)
  2175. StockInProductDao := Stock.NewStockInProduct(o)
  2176. StockDao := Stock.NewStock(o)
  2177. DeviceDao := Stock.NewDevice(o)
  2178. // 查询入库信息
  2179. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  2180. if err != nil {
  2181. o.Rollback()
  2182. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2183. c.ServeJSON()
  2184. return
  2185. }
  2186. productList := lib.SplitString(T_product, "|")
  2187. allProductList := []int{}
  2188. for _, v := range productList {
  2189. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2190. allProductList = append(allProductList, product_id)
  2191. product, _ := Basic.Read_Product_ById(product_id)
  2192. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2193. if num == 0 {
  2194. o.Rollback()
  2195. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  2196. c.ServeJSON()
  2197. return
  2198. }
  2199. T_relation_sn := strings.Split(v, "-")[2]
  2200. if T_relation_sn == "" && product.T_relation_sn == 1 {
  2201. o.Rollback()
  2202. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2203. c.ServeJSON()
  2204. return
  2205. }
  2206. if len(T_relation_sn) > 0 {
  2207. snList := strings.Split(T_relation_sn, ",")
  2208. if product.T_relation_sn == 1 && num != len(snList) {
  2209. o.Rollback()
  2210. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s SN数量与出库数量不一致,请核查!", product.T_name)}
  2211. c.ServeJSON()
  2212. return
  2213. }
  2214. if len(snList) > 0 {
  2215. for _, sn := range snList {
  2216. mqtt := Stock.Read_MqttUser(sn)
  2217. // 根据IMEI查询上次入库的批次号
  2218. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  2219. if err != nil {
  2220. logs.Error(lib.FuncName(), err)
  2221. }
  2222. device := Stock.Device{
  2223. T_product_id: product_id,
  2224. T_out_number: T_number,
  2225. T_sn: sn,
  2226. T_iccid: mqtt.Iccid,
  2227. T_imei: mqtt.Imei,
  2228. T_State: 1,
  2229. T_project: stockIn.T_project,
  2230. T_batch_number: batchNumber,
  2231. CreateTime: date,
  2232. }
  2233. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2234. if err != nil {
  2235. o.Rollback()
  2236. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  2237. c.ServeJSON()
  2238. return
  2239. }
  2240. }
  2241. }
  2242. }
  2243. // 3、添加出库产品清单
  2244. stockInProduct := Stock.StockInProduct{
  2245. T_number: T_number,
  2246. T_product_id: product_id,
  2247. T_depot_id: stockIn.T_depot_id,
  2248. T_num: num, // 入库数量
  2249. T_date: T_date, // 入库日期
  2250. T_relation_sn: T_relation_sn,
  2251. T_state: 1,
  2252. }
  2253. // 更新产品表
  2254. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  2255. if err != nil {
  2256. o.Rollback()
  2257. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2258. c.ServeJSON()
  2259. return
  2260. }
  2261. // 5、更新产品库存列表、占用库存
  2262. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  2263. if err != nil {
  2264. o.Rollback()
  2265. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2266. c.ServeJSON()
  2267. return
  2268. }
  2269. }
  2270. if len(T_remark) > 0 {
  2271. stockIn.T_remark = T_remark
  2272. }
  2273. if len(T_project) > 0 {
  2274. stockIn.T_project = T_project
  2275. }
  2276. stockIn.T_warehouse = c.User.T_uuid
  2277. stockIn.T_state = Stock.StockInWarehouse
  2278. stockIn.T_date = T_date
  2279. err = StockInDao.Update_StockIn(stockIn, "T_state", "T_date", "T_warehouse", "T_project", "T_remark")
  2280. if err != nil {
  2281. o.Rollback()
  2282. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  2283. c.ServeJSON()
  2284. return
  2285. }
  2286. o.Commit()
  2287. // 更新月份统计表
  2288. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  2289. NatsServer.AddUserLogs(c.User.T_uuid, "入库申请", "确认出库", stockIn)
  2290. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2291. c.ServeJSON()
  2292. return
  2293. }
  2294. func (c *StockController) StockOut_List() {
  2295. // 分页参数 初始化
  2296. page, _ := c.GetInt("page")
  2297. if page < 1 {
  2298. page = 1
  2299. }
  2300. page_z, _ := c.GetInt("page_z")
  2301. if page_z < 1 {
  2302. page_z = conf.Page_size
  2303. }
  2304. // 查询
  2305. T_depot_id, _ := c.GetInt("T_depot_id")
  2306. T_contract_number := c.GetString("T_contract_number")
  2307. T_start_date := c.GetString("T_start_date")
  2308. T_end_date := c.GetString("T_end_date")
  2309. T_name := c.GetString("T_name")
  2310. userList, _ := NatsServer.Read_User_List_All()
  2311. Account.Read_User_All_Map(userList)
  2312. Basic.Read_Depot_All_Map()
  2313. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2314. R_List, R_cnt := StockOutDao.Read_StockOut_List(T_name, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  2315. var r_jsons lib.R_JSONS
  2316. r_jsons.Num = R_cnt
  2317. r_jsons.Data = R_List
  2318. r_jsons.Page = page
  2319. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  2320. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2321. c.ServeJSON()
  2322. return
  2323. }
  2324. func (c *StockController) StockOut_List_Product() {
  2325. // 分页参数 初始化
  2326. page, _ := c.GetInt("page")
  2327. if page < 1 {
  2328. page = 1
  2329. }
  2330. page_z, _ := c.GetInt("page_z")
  2331. if page_z < 1 {
  2332. page_z = conf.Page_size
  2333. }
  2334. // 查询
  2335. T_depot_id, _ := c.GetInt("T_depot_id")
  2336. T_name := c.GetString("T_name")
  2337. T_start_date := c.GetString("T_start_date")
  2338. T_end_date := c.GetString("T_end_date")
  2339. T_product_name := c.GetString("T_product_name") // 产品名称
  2340. T_product_model := c.GetString("T_product_model") // 产品型号
  2341. userList, _ := NatsServer.Read_User_List_All()
  2342. Account.Read_User_All_Map(userList)
  2343. Basic.Read_Depot_All_Map()
  2344. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2345. R_List, R_cnt := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, page, page_z)
  2346. var r_jsons lib.R_JSONS
  2347. r_jsons.Num = R_cnt
  2348. r_jsons.Data = R_List
  2349. r_jsons.Page = page
  2350. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  2351. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2352. c.ServeJSON()
  2353. return
  2354. }
  2355. func (c *StockController) StockOut_List_Product_Excel() {
  2356. // 查询
  2357. T_depot_id, _ := c.GetInt("T_depot_id")
  2358. T_name := c.GetString("T_name")
  2359. T_start_date := c.GetString("T_start_date")
  2360. T_end_date := c.GetString("T_end_date")
  2361. T_product_name := c.GetString("T_product_name") // 产品名称
  2362. T_product_model := c.GetString("T_product_model") // 产品型号
  2363. userList, _ := NatsServer.Read_User_List_All()
  2364. Account.Read_User_All_Map(userList)
  2365. Basic.Read_Depot_All_Map()
  2366. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2367. R_List, _ := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, 0, 9999)
  2368. filename := fmt.Sprintf("出库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  2369. f := excelize.NewFile() // 设置单元格的值
  2370. Style1, _ := f.NewStyle(
  2371. &excelize.Style{
  2372. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  2373. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2374. })
  2375. Style2, _ := f.NewStyle(
  2376. &excelize.Style{
  2377. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  2378. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2379. Border: []excelize.Border{
  2380. {Type: "left", Color: "000000", Style: 1},
  2381. {Type: "top", Color: "000000", Style: 1},
  2382. {Type: "bottom", Color: "000000", Style: 1},
  2383. {Type: "right", Color: "000000", Style: 1},
  2384. },
  2385. })
  2386. Style3, _ := f.NewStyle(
  2387. &excelize.Style{
  2388. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2389. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  2390. Border: []excelize.Border{
  2391. {Type: "left", Color: "000000", Style: 1},
  2392. {Type: "top", Color: "000000", Style: 1},
  2393. {Type: "bottom", Color: "000000", Style: 1},
  2394. {Type: "right", Color: "000000", Style: 1},
  2395. },
  2396. })
  2397. f.MergeCell("Sheet1", "A1", "L1")
  2398. f.SetRowStyle("Sheet1", 1, 1, Style1)
  2399. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("出库明细单"))
  2400. f.SetRowHeight("Sheet1", 1, 30)
  2401. // 这里设置表头
  2402. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  2403. f.SetRowHeight("Sheet1", 2, 20)
  2404. f.SetCellValue("Sheet1", "A2", "序号")
  2405. f.SetCellValue("Sheet1", "B2", "出库单号")
  2406. f.SetCellValue("Sheet1", "C2", "领取人")
  2407. f.SetCellValue("Sheet1", "D2", "出库日期")
  2408. f.SetCellValue("Sheet1", "E2", "出库仓库")
  2409. f.SetCellValue("Sheet1", "F2", "关联项目")
  2410. f.SetCellValue("Sheet1", "G2", "产品名称")
  2411. f.SetCellValue("Sheet1", "H2", "产品型号")
  2412. f.SetCellValue("Sheet1", "I2", "数量")
  2413. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  2414. f.SetCellValue("Sheet1", "K2", "备注")
  2415. f.SetCellValue("Sheet1", "L2", "SN")
  2416. // 设置列宽
  2417. f.SetColWidth("Sheet1", "A", "A", 10)
  2418. f.SetColWidth("Sheet1", "B", "B", 15)
  2419. f.SetColWidth("Sheet1", "C", "D", 10)
  2420. f.SetColWidth("Sheet1", "D", "D", 10)
  2421. f.SetColWidth("Sheet1", "E", "E", 10)
  2422. f.SetColWidth("Sheet1", "F", "F", 20)
  2423. f.SetColWidth("Sheet1", "G", "G", 20)
  2424. f.SetColWidth("Sheet1", "H", "H", 15)
  2425. f.SetColWidth("Sheet1", "I", "I", 10)
  2426. f.SetColWidth("Sheet1", "J", "J", 12)
  2427. f.SetColWidth("Sheet1", "K", "K", 30)
  2428. f.SetColWidth("Sheet1", "L", "L", 100)
  2429. line := 2
  2430. for _, product := range R_List {
  2431. line += 1
  2432. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  2433. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  2434. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_receive_name)
  2435. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  2436. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  2437. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), product.T_project)
  2438. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  2439. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  2440. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  2441. var relation_sn string
  2442. if product.T_product_relation_sn == 1 {
  2443. relation_sn = "是"
  2444. } else {
  2445. relation_sn = "否"
  2446. }
  2447. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  2448. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  2449. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  2450. }
  2451. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  2452. // 保存文件
  2453. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  2454. fmt.Println(err)
  2455. }
  2456. var url string
  2457. //// 上传 OSS
  2458. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  2459. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  2460. if !is {
  2461. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2462. c.ServeJSON()
  2463. return
  2464. }
  2465. //删除目录
  2466. err := os.Remove("ofile/" + filename + ".xlsx")
  2467. if err != nil {
  2468. logs.Error(lib.FuncName(), err)
  2469. }
  2470. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2471. c.ServeJSON()
  2472. return
  2473. }
  2474. func (c *StockController) StockOut_Get() {
  2475. // 查询
  2476. T_number := c.GetString("T_number")
  2477. o := orm.NewOrm()
  2478. StockOutDao := Stock.NewStockOut(o)
  2479. StockOutProductDao := Stock.NewStockOutProduct(o)
  2480. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2481. if err != nil {
  2482. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2483. c.ServeJSON()
  2484. return
  2485. }
  2486. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2487. var pList []Stock.StockOutProduct_R
  2488. for _, v := range productList {
  2489. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2490. }
  2491. userList, _ := NatsServer.Read_User_List_All()
  2492. Account.Read_User_All_Map(userList)
  2493. Basic.Read_Depot_All_Map()
  2494. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockOutToStockOut_Detail(stockOut, pList)}
  2495. c.ServeJSON()
  2496. return
  2497. }
  2498. func (c *StockController) StockOut_generate_number() {
  2499. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2500. rand_x := 0
  2501. T_number := ""
  2502. for true {
  2503. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  2504. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2505. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  2506. break
  2507. }
  2508. rand_x += 1
  2509. }
  2510. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2511. c.ServeJSON()
  2512. return
  2513. }
  2514. func (c *StockController) StockOut_Add() {
  2515. o := orm.NewOrm()
  2516. o.Begin()
  2517. StockOutDao := Stock.NewStockOut(o)
  2518. // 检查前端传入的T_number是否已提交过
  2519. T_number := c.GetString("T_number")
  2520. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2521. if err == nil {
  2522. // 入库编号已存在,不需要做任何处理
  2523. o.Rollback()
  2524. c.Data["json"] = lib.JSONS{Code: 200, Msg: "出库编号已存在,无需重复提交!", Data: T_number}
  2525. c.ServeJSON()
  2526. return
  2527. }
  2528. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  2529. // 数据库查询错误
  2530. o.Rollback()
  2531. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  2532. c.ServeJSON()
  2533. return
  2534. }
  2535. T_contract_number := c.GetString("T_contract_number")
  2536. T_depot_id, _ := c.GetInt("T_depot_id")
  2537. T_type, _ := c.GetInt("T_type") // 出库类型 1-领料出库 2-销售出库
  2538. T_date := c.GetString("T_date")
  2539. T_receive := c.GetString("T_receive")
  2540. T_product := c.GetString("T_product")
  2541. T_remark := c.GetString("T_remark")
  2542. T_project := c.GetString("T_project")
  2543. T_delivery_type, _ := c.GetInt("T_delivery_type")
  2544. T_signer_unit := c.GetString("T_signer_unit")
  2545. T_signer := c.GetString("T_signer")
  2546. T_signer_phone := c.GetString("T_signer_phone")
  2547. T_signer_date := c.GetString("T_signer_date")
  2548. T_courier_number := c.GetString("T_courier_number")
  2549. date, is := lib.DateStrToTime(T_date)
  2550. if !is {
  2551. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2552. c.ServeJSON()
  2553. return
  2554. }
  2555. var_ := Stock.StockOut{
  2556. T_number: T_number,
  2557. T_contract_number: T_contract_number,
  2558. T_depot_id: T_depot_id,
  2559. T_type: T_type,
  2560. T_date: T_date,
  2561. T_receive: T_receive,
  2562. T_remark: T_remark,
  2563. T_project: T_project,
  2564. T_submit: c.User.T_uuid,
  2565. // 销售出库
  2566. T_delivery_type: T_delivery_type, // 1-自送 2-自提 3-快递
  2567. T_signer_unit: T_signer_unit,
  2568. T_signer: T_signer,
  2569. T_signer_phone: T_signer_phone,
  2570. T_signer_date: T_signer_date,
  2571. T_courier_number: T_courier_number,
  2572. T_state: Stock.StockOutAlreadyOut,
  2573. }
  2574. StockOutProductDao := Stock.NewStockOutProduct(o)
  2575. StockDao := Stock.NewStock(o)
  2576. DeviceDao := Stock.NewDevice(o)
  2577. ContractDao := Contract.NewContract(o)
  2578. ContractProductDao := Contract.NewContractProduct(o)
  2579. _, err = StockOutDao.Add_StockOut(var_)
  2580. if err != nil {
  2581. o.Rollback()
  2582. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2583. c.ServeJSON()
  2584. return
  2585. }
  2586. // 1、添加出库单
  2587. contract, _ := ContractDao.Read_Contract_ByT_number(T_contract_number)
  2588. if contract.Id == 0 && T_type == 2 {
  2589. o.Rollback()
  2590. c.Data["json"] = lib.JSONS{Code: 203, Msg: "合同编号错误!"}
  2591. c.ServeJSON()
  2592. return
  2593. }
  2594. if len(T_product) == 0 {
  2595. o.Rollback()
  2596. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  2597. c.ServeJSON()
  2598. return
  2599. }
  2600. productList := lib.SplitString(T_product, "|")
  2601. allProductList := []int{}
  2602. for _, v := range productList {
  2603. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2604. allProductList = append(allProductList, product_id)
  2605. product, _ := Basic.Read_Product_ById(product_id)
  2606. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2607. T_relation_sn := strings.Split(v, "-")[2]
  2608. if T_relation_sn == "" && product.T_relation_sn == 1 {
  2609. o.Rollback()
  2610. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2611. c.ServeJSON()
  2612. return
  2613. }
  2614. if len(T_relation_sn) > 0 {
  2615. snList := lib.SplitString(T_relation_sn, ",")
  2616. num = len(snList)
  2617. for _, sn := range snList {
  2618. mqtt := Stock.Read_MqttUser(sn)
  2619. // 根据IMEI查询上次入库的批次号
  2620. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  2621. if err != nil {
  2622. logs.Error(lib.FuncName(), err)
  2623. }
  2624. device := Stock.Device{
  2625. T_contract_number: T_contract_number,
  2626. T_product_id: product_id,
  2627. T_out_number: T_number,
  2628. T_sn: sn,
  2629. T_iccid: mqtt.Iccid,
  2630. T_imei: mqtt.Imei,
  2631. T_State: 1,
  2632. T_project: T_project,
  2633. T_batch_number: batchNumber,
  2634. CreateTime: date,
  2635. }
  2636. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2637. if err != nil {
  2638. o.Rollback()
  2639. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  2640. c.ServeJSON()
  2641. return
  2642. }
  2643. }
  2644. }
  2645. // 3、添加出库产品清单
  2646. stockOutProduct := Stock.StockOutProduct{
  2647. T_number: T_number,
  2648. T_product_id: product_id,
  2649. T_depot_id: T_depot_id,
  2650. T_num: num, // 出库数量
  2651. T_date: T_date, // 出库日期
  2652. T_relation_sn: T_relation_sn,
  2653. T_state: 1,
  2654. }
  2655. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  2656. if err != nil {
  2657. o.Rollback()
  2658. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2659. c.ServeJSON()
  2660. return
  2661. }
  2662. // 4、更改合同产品列表清单
  2663. contractProduct, _ := ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_contract_number, product_id)
  2664. contractProduct.T_product_out += num
  2665. if contractProduct.T_product_out > contractProduct.T_product_total && T_type == 2 {
  2666. o.Rollback()
  2667. p, _ := Basic.Read_Product_ById(product_id)
  2668. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】出库数量超过预计出库数量!", p.T_name)}
  2669. c.ServeJSON()
  2670. return
  2671. }
  2672. contractProduct.T_State = 2
  2673. if contractProduct.T_product_out == 0 {
  2674. contractProduct.T_State = 1
  2675. }
  2676. if contractProduct.T_product_out == contractProduct.T_product_total {
  2677. contractProduct.T_State = 3
  2678. }
  2679. err = ContractProductDao.Update_ContractProduct(contractProduct, "T_product_out", "T_State")
  2680. if err != nil {
  2681. o.Rollback()
  2682. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同产品清单失败"}
  2683. c.ServeJSON()
  2684. return
  2685. }
  2686. // 5、更新产品库存列表
  2687. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 1)
  2688. if err != nil {
  2689. o.Rollback()
  2690. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存失败" + err.Error()}
  2691. c.ServeJSON()
  2692. return
  2693. }
  2694. }
  2695. o.Commit()
  2696. o2 := orm.NewOrm()
  2697. o2.Begin()
  2698. ContractDao2 := Contract.NewContract(o2)
  2699. ContractProductDao2 := Contract.NewContractProduct(o2)
  2700. // 5、更新合同出库状态
  2701. var T_out int
  2702. if contract.T_State == 1 {
  2703. T_out = 2
  2704. // 查询合同产品清单是否全部都为已出库
  2705. state := ContractProductDao2.Read_ContractProduct_T_State_List(contract.T_number)
  2706. if state == 1 {
  2707. T_out = 1
  2708. }
  2709. if state == 3 {
  2710. T_out = 3
  2711. }
  2712. }
  2713. if T_out != contract.T_out {
  2714. contract.T_out = T_out
  2715. err = ContractDao2.Update_Contract(contract, "T_out")
  2716. if err != nil {
  2717. o2.Rollback()
  2718. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  2719. c.ServeJSON()
  2720. return
  2721. }
  2722. }
  2723. o2.Commit()
  2724. StockOut_Edit_StockMonth(T_date, T_depot_id, allProductList)
  2725. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "出库", var_)
  2726. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2727. c.ServeJSON()
  2728. return
  2729. }
  2730. func (c *StockController) StockOut_Edit_Delivery() {
  2731. T_number := c.GetString("T_number")
  2732. T_remark := c.GetString("T_remark")
  2733. T_project := c.GetString("T_project")
  2734. T_delivery_type, _ := c.GetInt("T_delivery_type")
  2735. T_signer_unit := c.GetString("T_signer_unit")
  2736. T_signer := c.GetString("T_signer")
  2737. T_signer_phone := c.GetString("T_signer_phone")
  2738. T_signer_date := c.GetString("T_signer_date")
  2739. T_courier_number := c.GetString("T_courier_number")
  2740. o := orm.NewOrm()
  2741. StockOutDao := Stock.NewStockOut(o)
  2742. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2743. if err != nil {
  2744. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库编号错误!"}
  2745. c.ServeJSON()
  2746. return
  2747. }
  2748. if len(T_remark) > 0 {
  2749. stockOut.T_remark = T_remark
  2750. }
  2751. if len(T_project) > 0 {
  2752. stockOut.T_project = T_project
  2753. }
  2754. if T_delivery_type > 0 {
  2755. stockOut.T_delivery_type = T_delivery_type
  2756. }
  2757. if len(T_signer_unit) > 0 {
  2758. stockOut.T_signer_unit = T_signer_unit
  2759. }
  2760. if len(T_signer) > 0 {
  2761. stockOut.T_signer = T_signer
  2762. }
  2763. if len(T_signer_phone) > 0 {
  2764. stockOut.T_signer_phone = T_signer_phone
  2765. }
  2766. if len(T_signer_date) > 0 {
  2767. stockOut.T_signer_date = T_signer_date
  2768. }
  2769. if len(T_courier_number) > 0 {
  2770. stockOut.T_courier_number = T_courier_number
  2771. }
  2772. err = StockOutDao.Update_StockOut(stockOut, "T_remark", "T_delivery_type", "T_signer_unit", "T_signer", "T_signer_phone", "T_signer_date", "T_courier_number", "T_project")
  2773. if err != nil {
  2774. o.Rollback()
  2775. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2776. c.ServeJSON()
  2777. return
  2778. }
  2779. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改发货单", stockOut)
  2780. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2781. c.ServeJSON()
  2782. return
  2783. }
  2784. func (c *StockController) StockOut_Edit() {
  2785. T_number := c.GetString("T_number") // 出库单号
  2786. //T_depot_id, _ := c.GetInt("T_depot_id")
  2787. T_date := c.GetString("T_date")
  2788. T_receive := c.GetString("T_receive")
  2789. T_project := c.GetString("T_project")
  2790. T_product := c.GetString("T_product")
  2791. T_remark := c.GetString("T_remark")
  2792. date, is := lib.DateStrToTime(T_date)
  2793. if !is {
  2794. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2795. c.ServeJSON()
  2796. return
  2797. }
  2798. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  2799. T_date = ""
  2800. }
  2801. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  2802. o := orm.NewOrm()
  2803. o.Begin()
  2804. StockOutDao := Stock.NewStockOut(o)
  2805. StockOutProductDao := Stock.NewStockOutProduct(o)
  2806. StockDao := Stock.NewStock(o)
  2807. DeviceDao := Stock.NewDevice(o)
  2808. // 查询出库信息
  2809. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2810. if err != nil {
  2811. o.Rollback()
  2812. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2813. c.ServeJSON()
  2814. return
  2815. }
  2816. T_old_date := StockOut.T_date
  2817. // 查询出库产品信息
  2818. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  2819. var oldProductList []dto.StockProduct
  2820. var newProductList []dto.StockProduct
  2821. productOldMap := map[int]dto.StockProduct{}
  2822. allProductListMap := make(map[int]struct{})
  2823. for _, product := range productOldList {
  2824. stockProduct := dto.StockProduct{
  2825. T_product_id: product.T_product_id,
  2826. T_num: product.T_num,
  2827. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2828. }
  2829. oldProductList = append(oldProductList, stockProduct)
  2830. productOldMap[product.T_product_id] = stockProduct
  2831. if _, ok := allProductListMap[product.T_product_id]; !ok {
  2832. allProductListMap[product.T_product_id] = struct{}{}
  2833. }
  2834. }
  2835. productNewList := lib.SplitString(T_product, "|")
  2836. productNewMap := map[int]dto.StockProduct{}
  2837. for _, v := range productNewList {
  2838. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2839. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2840. T_relation_sn := strings.Split(v, "-")[2]
  2841. snList := lib.SplitString(T_relation_sn, ",")
  2842. if len(snList) > 0 {
  2843. num = len(snList)
  2844. }
  2845. stockProduct := dto.StockProduct{
  2846. T_product_id: product_id,
  2847. T_num: num,
  2848. T_relation_sn: snList,
  2849. }
  2850. newProductList = append(newProductList, stockProduct)
  2851. productNewMap[product_id] = stockProduct
  2852. if _, ok := allProductListMap[product_id]; !ok {
  2853. allProductListMap[product_id] = struct{}{}
  2854. }
  2855. }
  2856. // 判断产品列表信息是否相同
  2857. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  2858. // 两次提交的数据相同,则不用修改
  2859. if !StockProductListIsEqual {
  2860. if len(needDelete) > 0 {
  2861. // 删除入库产品列表
  2862. for _, v := range needDelete {
  2863. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2864. // 删除入库产品列表
  2865. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  2866. if err != nil {
  2867. o.Rollback()
  2868. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2869. c.ServeJSON()
  2870. return
  2871. }
  2872. if product.T_relation_sn == 1 {
  2873. // 删除该入库单关联sn
  2874. for _, sn := range v.T_relation_sn {
  2875. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  2876. if err != nil {
  2877. o.Rollback()
  2878. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  2879. c.ServeJSON()
  2880. return
  2881. }
  2882. }
  2883. }
  2884. // 增加库存
  2885. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  2886. if err != nil {
  2887. o.Rollback()
  2888. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  2889. c.ServeJSON()
  2890. return
  2891. }
  2892. }
  2893. }
  2894. if len(needAdd) > 0 {
  2895. // 新增入库产品列表
  2896. for _, v := range needAdd {
  2897. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2898. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  2899. o.Rollback()
  2900. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2901. c.ServeJSON()
  2902. return
  2903. }
  2904. // 添加该入库单关联sn
  2905. for _, sn := range v.T_relation_sn {
  2906. mqtt := Stock.Read_MqttUser(sn)
  2907. // 添加设备
  2908. device := Stock.Device{
  2909. T_contract_number: "",
  2910. T_out_number: StockOut.T_number,
  2911. T_product_id: v.T_product_id,
  2912. T_in_number: "",
  2913. T_sn: sn,
  2914. T_iccid: mqtt.Iccid,
  2915. T_imei: mqtt.Imei,
  2916. T_State: 1,
  2917. CreateTime: date,
  2918. UpdateTime: StockOut.UpdateTime,
  2919. }
  2920. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2921. if err != nil {
  2922. o.Rollback()
  2923. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  2924. c.ServeJSON()
  2925. return
  2926. }
  2927. }
  2928. StockOutProduct := Stock.StockOutProduct{
  2929. T_number: T_number,
  2930. T_product_id: v.T_product_id,
  2931. T_depot_id: StockOut.T_depot_id,
  2932. T_num: v.T_num, // 入库数量
  2933. T_date: StockOut.T_date, // 入库日期
  2934. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  2935. T_state: 1,
  2936. }
  2937. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  2938. if err != nil {
  2939. o.Rollback()
  2940. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2941. c.ServeJSON()
  2942. return
  2943. }
  2944. // 减少库存
  2945. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  2946. if err != nil {
  2947. o.Rollback()
  2948. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  2949. c.ServeJSON()
  2950. return
  2951. }
  2952. }
  2953. }
  2954. if len(needEdit) > 0 {
  2955. for _, v := range needEdit {
  2956. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2957. StockOutProduct := Stock.StockOutProduct{
  2958. T_number: StockOut.T_number,
  2959. T_product_id: v.T_product_id,
  2960. T_depot_id: StockOut.T_depot_id,
  2961. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  2962. T_date: StockOut.T_date, // 入库日期
  2963. T_state: 1,
  2964. }
  2965. // 出库数量比之前多,减少库存
  2966. var T_type int
  2967. var T_num int
  2968. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  2969. // 减少库存
  2970. T_type = 1
  2971. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  2972. } else {
  2973. // 增加库存
  2974. T_type = 2
  2975. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  2976. }
  2977. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  2978. if err != nil {
  2979. o.Rollback()
  2980. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2981. c.ServeJSON()
  2982. return
  2983. }
  2984. // 更新产品库存表
  2985. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  2986. if err != nil {
  2987. o.Rollback()
  2988. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2989. c.ServeJSON()
  2990. return
  2991. }
  2992. }
  2993. }
  2994. for _, diff := range snDiff {
  2995. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  2996. // 删除减少的sn
  2997. if len(diff.T_delete_relation_sn) > 0 {
  2998. for _, sn := range diff.T_delete_relation_sn {
  2999. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  3000. if err != nil {
  3001. o.Rollback()
  3002. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  3003. c.ServeJSON()
  3004. return
  3005. }
  3006. }
  3007. // 增加库存
  3008. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_delete_relation_sn), 2)
  3009. if err != nil {
  3010. o.Rollback()
  3011. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  3012. c.ServeJSON()
  3013. return
  3014. }
  3015. }
  3016. if len(diff.T_add_relation_sn) > 0 {
  3017. for _, sn := range diff.T_add_relation_sn {
  3018. mqtt := Stock.Read_MqttUser(sn)
  3019. // 添加设备
  3020. device := Stock.Device{
  3021. T_contract_number: "",
  3022. T_out_number: StockOut.T_number,
  3023. T_product_id: diff.T_product_id,
  3024. T_in_number: "",
  3025. T_sn: sn,
  3026. T_iccid: mqtt.Iccid,
  3027. T_imei: mqtt.Imei,
  3028. T_State: 1,
  3029. CreateTime: date,
  3030. UpdateTime: StockOut.UpdateTime,
  3031. }
  3032. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  3033. if err != nil {
  3034. o.Rollback()
  3035. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  3036. c.ServeJSON()
  3037. return
  3038. }
  3039. }
  3040. // 减少库存
  3041. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_add_relation_sn), 1)
  3042. if err != nil {
  3043. o.Rollback()
  3044. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  3045. c.ServeJSON()
  3046. return
  3047. }
  3048. }
  3049. StockOutProduct := Stock.StockOutProduct{
  3050. T_number: StockOut.T_number,
  3051. T_product_id: diff.T_product_id,
  3052. T_depot_id: StockOut.T_depot_id,
  3053. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  3054. T_date: StockOut.T_date, // 入库日期
  3055. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  3056. T_state: 1,
  3057. }
  3058. // 更新产品库存表
  3059. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  3060. if err != nil {
  3061. o.Rollback()
  3062. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  3063. c.ServeJSON()
  3064. return
  3065. }
  3066. }
  3067. }
  3068. if len(T_remark) > 0 {
  3069. StockOut.T_remark = T_remark
  3070. }
  3071. if len(T_receive) > 0 {
  3072. StockOut.T_receive = T_receive
  3073. }
  3074. if len(T_project) > 0 {
  3075. StockOut.T_project = T_project
  3076. }
  3077. if len(T_date) > 0 {
  3078. StockOut.T_date = T_date
  3079. }
  3080. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_date")
  3081. if err != nil {
  3082. o.Rollback()
  3083. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  3084. c.ServeJSON()
  3085. return
  3086. }
  3087. o.Commit()
  3088. if len(T_date) > 0 && T_old_date != T_date {
  3089. // 修改出库产品日期
  3090. StockOutProductDao.Update_StockOutProduct_T_date(StockOut.T_number, T_date)
  3091. DeviceDao.Update_Device_CreateTimeByT_out_number(StockOut.T_number, T_date)
  3092. }
  3093. if len(T_project) > 0 {
  3094. DeviceDao.Update_Device_ByT_out_number_T_project(StockOut.T_number, T_project)
  3095. }
  3096. // 更新月份统计表
  3097. allProductList := []int{}
  3098. for i, _ := range allProductListMap {
  3099. allProductList = append(allProductList, i)
  3100. }
  3101. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  3102. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改", StockOut)
  3103. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3104. c.ServeJSON()
  3105. return
  3106. }
  3107. // 删除出库
  3108. func (c *StockController) StockOut_Del() {
  3109. T_number := c.GetString("T_number") // 入库单号
  3110. o := orm.NewOrm()
  3111. o.Begin()
  3112. StockOutDao := Stock.NewStockOut(o)
  3113. StockOutProductDao := Stock.NewStockOutProduct(o)
  3114. StockDao := Stock.NewStock(o)
  3115. DeviceDao := Stock.NewDevice(o)
  3116. // 查询出库信息
  3117. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3118. if err != nil {
  3119. o.Rollback()
  3120. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3121. c.ServeJSON()
  3122. return
  3123. }
  3124. // 查询出库产品信息
  3125. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3126. var oldProductList []dto.StockProduct
  3127. allProductList := []int{}
  3128. for _, product := range productOldList {
  3129. oldProductList = append(oldProductList, dto.StockProduct{
  3130. T_product_id: product.T_product_id,
  3131. T_num: product.T_num,
  3132. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3133. })
  3134. allProductList = append(allProductList, product.T_product_id)
  3135. }
  3136. // 删除入库产品列表
  3137. for _, v := range oldProductList {
  3138. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3139. // 删除入库产品列表
  3140. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  3141. if err != nil {
  3142. o.Rollback()
  3143. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  3144. c.ServeJSON()
  3145. return
  3146. }
  3147. if product.T_relation_sn == 1 {
  3148. // 删除该入库单关联sn
  3149. for _, sn := range v.T_relation_sn {
  3150. err = DeviceDao.Delete_Device_ByT_out_number(stockOut.T_number, sn)
  3151. if err != nil {
  3152. o.Rollback()
  3153. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库设备失败"}
  3154. c.ServeJSON()
  3155. return
  3156. }
  3157. }
  3158. }
  3159. // 增加库存
  3160. _, err = StockDao.AddOrUpdate_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  3161. if err != nil {
  3162. o.Rollback()
  3163. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  3164. c.ServeJSON()
  3165. return
  3166. }
  3167. }
  3168. err = StockOutDao.Delete_StockOut(stockOut)
  3169. if err != nil {
  3170. o.Rollback()
  3171. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  3172. c.ServeJSON()
  3173. return
  3174. }
  3175. o.Commit()
  3176. // 更新月份统计表
  3177. StockOut_Edit_StockMonth(stockOut.T_date, stockOut.T_depot_id, allProductList)
  3178. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "删除", stockOut)
  3179. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3180. c.ServeJSON()
  3181. return
  3182. }
  3183. // 导出出库单
  3184. func (c *StockController) StockOut_Excel() {
  3185. // 查询
  3186. T_number := c.GetString("T_number")
  3187. o := orm.NewOrm()
  3188. StockOutDao := Stock.NewStockOut(o)
  3189. StockOutProductDao := Stock.NewStockOutProduct(o)
  3190. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3191. if err != nil {
  3192. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3193. c.ServeJSON()
  3194. return
  3195. }
  3196. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3197. //productList = append(productList, productList...)
  3198. //productList = append(productList, productList...)
  3199. //productList = append(productList, productList...)
  3200. var pList []Stock.StockOutProduct_R
  3201. for _, v := range productList {
  3202. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  3203. }
  3204. userList, _ := NatsServer.Read_User_List_All()
  3205. Account.Read_User_All_Map(userList)
  3206. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  3207. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  3208. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  3209. f := excelize.NewFile() // 设置单元格的值
  3210. Style1, _ := f.NewStyle(
  3211. &excelize.Style{
  3212. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  3213. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  3214. })
  3215. Style2, _ := f.NewStyle(
  3216. &excelize.Style{
  3217. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3218. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  3219. })
  3220. Style3, _ := f.NewStyle(
  3221. &excelize.Style{
  3222. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  3223. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3224. Border: []excelize.Border{
  3225. {Type: "left", Color: "000000", Style: 1},
  3226. {Type: "top", Color: "000000", Style: 1},
  3227. {Type: "bottom", Color: "000000", Style: 1},
  3228. {Type: "right", Color: "000000", Style: 1},
  3229. },
  3230. })
  3231. Style4, _ := f.NewStyle(
  3232. &excelize.Style{
  3233. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3234. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3235. Border: []excelize.Border{
  3236. {Type: "left", Color: "000000", Style: 1},
  3237. {Type: "top", Color: "000000", Style: 1},
  3238. {Type: "bottom", Color: "000000", Style: 1},
  3239. {Type: "right", Color: "000000", Style: 1},
  3240. },
  3241. })
  3242. productList2 := lib.ChunkBy(productList, 10)
  3243. for i, products := range productList2 {
  3244. if len(products) < 10 {
  3245. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  3246. }
  3247. row := i*17 + 1
  3248. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3249. f.SetRowStyle("Sheet1", row, row, Style1)
  3250. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  3251. f.SetRowHeight("Sheet1", row, 20)
  3252. row += 1
  3253. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3254. f.SetRowStyle("Sheet1", row, row, Style1)
  3255. title := "硬件出库单"
  3256. if len(productList2) > 1 {
  3257. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  3258. }
  3259. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  3260. f.SetRowHeight("Sheet1", row, 20)
  3261. row += 1
  3262. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3263. f.SetRowStyle("Sheet1", row, row, Style2)
  3264. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  3265. f.SetRowHeight("Sheet1", row, 20)
  3266. row += 1
  3267. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3268. f.SetRowStyle("Sheet1", row, row, Style2)
  3269. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  3270. f.SetRowHeight("Sheet1", row, 20)
  3271. row += 1
  3272. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3273. f.SetRowStyle("Sheet1", row, row, Style2)
  3274. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  3275. f.SetRowHeight("Sheet1", row, 20)
  3276. // 这里设置表头
  3277. row += 1
  3278. f.SetRowHeight("Sheet1", row, 20)
  3279. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  3280. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  3281. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  3282. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  3283. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  3284. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  3285. // 设置列宽
  3286. f.SetColWidth("Sheet1", "A", "A", 5)
  3287. f.SetColWidth("Sheet1", "B", "B", 20)
  3288. f.SetColWidth("Sheet1", "C", "C", 12)
  3289. f.SetColWidth("Sheet1", "D", "D", 10)
  3290. f.SetColWidth("Sheet1", "E", "E", 10)
  3291. f.SetColWidth("Sheet1", "F", "F", 15)
  3292. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  3293. sRow := row
  3294. // 循环写入数据
  3295. for j, v := range products {
  3296. row += 1
  3297. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3298. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  3299. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  3300. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  3301. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  3302. if v.T_num > 0 {
  3303. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  3304. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  3305. }
  3306. }
  3307. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  3308. row += 1
  3309. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  3310. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  3311. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  3312. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  3313. f.SetRowStyle("Sheet1", row, row, Style2)
  3314. f.SetRowHeight("Sheet1", row, 20)
  3315. }
  3316. // 保存文件
  3317. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  3318. fmt.Println(err)
  3319. }
  3320. var url string
  3321. //// 上传 OSS
  3322. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  3323. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  3324. if !is {
  3325. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  3326. c.ServeJSON()
  3327. return
  3328. }
  3329. //删除目录
  3330. err = os.Remove("ofile/" + filename + ".xlsx")
  3331. if err != nil {
  3332. logs.Error(lib.FuncName(), err)
  3333. }
  3334. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  3335. c.ServeJSON()
  3336. return
  3337. }
  3338. func (c *StockController) StockOut_Excel_Batch() {
  3339. // 查询
  3340. T_number_list := c.GetString("T_number_list")
  3341. o := orm.NewOrm()
  3342. StockOutDao := Stock.NewStockOut(o)
  3343. StockOutProductDao := Stock.NewStockOutProduct(o)
  3344. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  3345. userList, _ := NatsServer.Read_User_List_All()
  3346. Account.Read_User_All_Map(userList)
  3347. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  3348. f := excelize.NewFile() // 设置单元格的值
  3349. Style1, _ := f.NewStyle(
  3350. &excelize.Style{
  3351. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  3352. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  3353. })
  3354. Style2, _ := f.NewStyle(
  3355. &excelize.Style{
  3356. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3357. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  3358. })
  3359. Style3, _ := f.NewStyle(
  3360. &excelize.Style{
  3361. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  3362. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3363. Border: []excelize.Border{
  3364. {Type: "left", Color: "000000", Style: 1},
  3365. {Type: "top", Color: "000000", Style: 1},
  3366. {Type: "bottom", Color: "000000", Style: 1},
  3367. {Type: "right", Color: "000000", Style: 1},
  3368. },
  3369. })
  3370. Style4, _ := f.NewStyle(
  3371. &excelize.Style{
  3372. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3373. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3374. Border: []excelize.Border{
  3375. {Type: "left", Color: "000000", Style: 1},
  3376. {Type: "top", Color: "000000", Style: 1},
  3377. {Type: "bottom", Color: "000000", Style: 1},
  3378. {Type: "right", Color: "000000", Style: 1},
  3379. },
  3380. })
  3381. var num int
  3382. for _, T_number := range list {
  3383. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3384. if err != nil {
  3385. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3386. c.ServeJSON()
  3387. return
  3388. }
  3389. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3390. var pList []Stock.StockOutProduct_R
  3391. for _, v := range productList {
  3392. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  3393. }
  3394. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  3395. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  3396. productList2 := lib.ChunkBy(productList, 10)
  3397. for i, products := range productList2 {
  3398. if len(products) < 10 {
  3399. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  3400. }
  3401. row := num*18 + 1
  3402. num += 1
  3403. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3404. f.SetRowStyle("Sheet1", row, row, Style1)
  3405. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  3406. f.SetRowHeight("Sheet1", row, 20)
  3407. row += 1
  3408. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3409. f.SetRowStyle("Sheet1", row, row, Style1)
  3410. title := "硬件出库单"
  3411. if len(productList2) > 1 {
  3412. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  3413. }
  3414. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  3415. f.SetRowHeight("Sheet1", row, 20)
  3416. row += 1
  3417. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3418. f.SetRowStyle("Sheet1", row, row, Style2)
  3419. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  3420. f.SetRowHeight("Sheet1", row, 20)
  3421. row += 1
  3422. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3423. f.SetRowStyle("Sheet1", row, row, Style2)
  3424. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  3425. f.SetRowHeight("Sheet1", row, 20)
  3426. row += 1
  3427. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3428. f.SetRowStyle("Sheet1", row, row, Style2)
  3429. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  3430. f.SetRowHeight("Sheet1", row, 20)
  3431. // 这里设置表头
  3432. row += 1
  3433. f.SetRowHeight("Sheet1", row, 20)
  3434. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  3435. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  3436. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  3437. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  3438. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  3439. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  3440. // 设置列宽
  3441. f.SetColWidth("Sheet1", "A", "A", 5)
  3442. f.SetColWidth("Sheet1", "B", "B", 20)
  3443. f.SetColWidth("Sheet1", "C", "C", 12)
  3444. f.SetColWidth("Sheet1", "D", "D", 10)
  3445. f.SetColWidth("Sheet1", "E", "E", 10)
  3446. f.SetColWidth("Sheet1", "F", "F", 15)
  3447. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  3448. sRow := row
  3449. // 循环写入数据
  3450. for j, v := range products {
  3451. row += 1
  3452. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3453. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  3454. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  3455. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  3456. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  3457. if v.T_num > 0 {
  3458. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  3459. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  3460. }
  3461. }
  3462. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  3463. row += 1
  3464. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  3465. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  3466. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  3467. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  3468. f.SetRowStyle("Sheet1", row, row, Style2)
  3469. f.SetRowHeight("Sheet1", row, 20)
  3470. // 空一行
  3471. row += 1
  3472. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3473. //f.SetRowStyle("Sheet1", row, row, Style1)
  3474. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  3475. // 插入下一页分页符
  3476. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  3477. }
  3478. }
  3479. // 保存文件
  3480. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  3481. fmt.Println(err)
  3482. }
  3483. var url string
  3484. //// 上传 OSS
  3485. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  3486. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  3487. if !is {
  3488. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  3489. c.ServeJSON()
  3490. return
  3491. }
  3492. //删除目录
  3493. err := os.Remove("ofile/" + filename + ".xlsx")
  3494. if err != nil {
  3495. logs.Error(lib.FuncName(), err)
  3496. }
  3497. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  3498. c.ServeJSON()
  3499. return
  3500. }
  3501. func StockOut_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  3502. date, _ := lib.DateStrToTime(T_date)
  3503. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  3504. // // 修改本月数据不同步库存
  3505. // return nil
  3506. //}
  3507. //T_month := date.Format("2006-01")
  3508. months := generateMonthList(date, "out")
  3509. o := orm.NewOrm()
  3510. StockOutDao := Stock.NewStockOut(o)
  3511. StockMonthDao := Stock.NewStockMonth(o)
  3512. StockOutProductDao := Stock.NewStockOutProduct(o)
  3513. StockInProductDao := Stock.NewStockInProduct(o)
  3514. for _, T_product_id := range allProductList {
  3515. for _, T_month := range months {
  3516. // 获取当前产品本月出入库数量
  3517. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  3518. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  3519. if stockMonth.Id > 0 {
  3520. // 修改
  3521. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  3522. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  3523. if len(lastMonth) > 0 {
  3524. stockMonth.T_beginning = lastMonth[0].T_ending
  3525. } else {
  3526. stockMonth.T_beginning = 0
  3527. }
  3528. // 期末库存 = 期初库存+入库-出库
  3529. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3530. err := StockMonthDao.Update_StockMonth(stockMonth, "T_out", "T_return", "T_beginning", "T_ending")
  3531. if err != nil {
  3532. return err
  3533. }
  3534. } else {
  3535. product, _ := Basic.Read_Product_ById(T_product_id)
  3536. // 添加库存记录
  3537. stockMonth = Stock.StockMonth{
  3538. T_depot_id: T_depot_id,
  3539. T_product_id: T_product_id,
  3540. T_product_class: product.T_class,
  3541. T_month: T_month,
  3542. }
  3543. // 获取当前产品本月出入库数量
  3544. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  3545. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  3546. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  3547. if len(lastMonth) > 0 {
  3548. stockMonth.T_beginning = lastMonth[0].T_ending
  3549. } else {
  3550. stockMonth.T_beginning = 0
  3551. }
  3552. // 期末库存 = 期初库存+入库-出库
  3553. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3554. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  3555. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  3556. if err != nil {
  3557. return err
  3558. }
  3559. }
  3560. }
  3561. }
  3562. return nil
  3563. }
  3564. func Cron_StockMonth() {
  3565. //创建一个定时任务对象
  3566. c := cron.New(cron.WithSeconds())
  3567. //给对象增加定时任务
  3568. // @monthly 每月运行一次,每月第一天午夜 0 0 0 1 * *
  3569. //c.AddFunc("0 */1 * * * ?", Cron_StockMonth_Add)
  3570. c.AddFunc("@monthly", Cron_StockMonth_Add)
  3571. //启动定时任务
  3572. c.Start()
  3573. defer c.Stop()
  3574. //查询语句,阻塞,让main函数不退出,保持程序运行
  3575. select {}
  3576. }
  3577. // 保存每月入库出库明细
  3578. func Cron_StockMonth_Add() {
  3579. T_month := time.Now().Format("2006-01")
  3580. //T_month := time.Now().AddDate(0, -1, 0).Format("2006-01")
  3581. logs.Info("开始统计" + T_month + "库存明细数据")
  3582. o := orm.NewOrm()
  3583. StockDao := Stock.NewStock(o)
  3584. StockOutDao := Stock.NewStockOut(o)
  3585. StockMonthDao := Stock.NewStockMonth(o)
  3586. StockOutProductDao := Stock.NewStockOutProduct(o)
  3587. StockInProductDao := Stock.NewStockInProduct(o)
  3588. stockList, _ := StockDao.Read_Stock_List(0, 0, "", "", 0, 9999)
  3589. for _, stock := range stockList {
  3590. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  3591. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  3592. if stockMonth.Id > 0 {
  3593. // 修改
  3594. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3595. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3596. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3597. if len(lastMonth) > 0 {
  3598. stockMonth.T_beginning = lastMonth[0].T_ending
  3599. } else {
  3600. stockMonth.T_beginning = 0
  3601. }
  3602. // 期末库存 = 期初库存+入库-出库
  3603. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3604. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_out", "T_beginning", "T_ending", "T_return")
  3605. if err != nil {
  3606. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  3607. }
  3608. } else {
  3609. stockMonth = Stock.StockMonth{
  3610. T_depot_id: stock.T_depot_id,
  3611. T_product_id: stock.T_product_id,
  3612. T_product_class: stock.T_product_class,
  3613. T_month: T_month,
  3614. }
  3615. // 获取当前产品本月出入库数量
  3616. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3617. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3618. if len(lastMonth) > 0 {
  3619. stockMonth.T_beginning = lastMonth[0].T_ending
  3620. } else {
  3621. stockMonth.T_beginning = 0
  3622. }
  3623. // 期末库存 = 期初库存+入库-出库
  3624. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3625. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(stock.T_depot_id, stock.T_product_id, T_month)
  3626. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  3627. if err != nil {
  3628. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  3629. }
  3630. }
  3631. }
  3632. }
  3633. func generateMonthList(startMonth time.Time, t_type string) []string {
  3634. var months []string
  3635. endMonth := time.Now()
  3636. if t_type == "in" {
  3637. endMonth = endMonth.AddDate(0, 1, 0)
  3638. }
  3639. for month := startMonth; month.Before(endMonth); month = month.AddDate(0, 1, 0) {
  3640. months = append(months, month.Format("2006-01"))
  3641. }
  3642. return months
  3643. }
  3644. // 我的申请列表
  3645. func (c *StockController) StockOut_Apply_List() {
  3646. // 分页参数 初始化
  3647. page, _ := c.GetInt("page")
  3648. if page < 1 {
  3649. page = 1
  3650. }
  3651. page_z, _ := c.GetInt("page_z")
  3652. if page_z < 1 {
  3653. page_z = conf.Page_size
  3654. }
  3655. // 查询
  3656. T_depot_id, _ := c.GetInt("T_depot_id")
  3657. T_state, _ := c.GetInt("T_state")
  3658. T_contract_number := c.GetString("T_contract_number")
  3659. T_start_date := c.GetString("T_start_date")
  3660. T_end_date := c.GetString("T_end_date")
  3661. T_name := c.GetString("T_name")
  3662. userList, _ := NatsServer.Read_User_List_All()
  3663. Account.Read_User_All_Map(userList)
  3664. Basic.Read_Depot_All_Map()
  3665. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  3666. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List(c.User.T_uuid, T_name, Stock.StockInTypeApply, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  3667. var r_jsons lib.R_JSONS
  3668. r_jsons.Num = R_cnt
  3669. r_jsons.Data = R_List
  3670. r_jsons.Page = page
  3671. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  3672. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  3673. c.ServeJSON()
  3674. return
  3675. }
  3676. // 出库申请
  3677. func (c *StockController) StockOut_Apply() {
  3678. rand_x := 0
  3679. T_number := ""
  3680. o := orm.NewOrm()
  3681. StockOutDao := Stock.NewStockOut(o)
  3682. for true {
  3683. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  3684. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3685. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  3686. break
  3687. }
  3688. rand_x += 1
  3689. }
  3690. T_contract_number := c.GetString("T_contract_number")
  3691. T_depot_id, _ := c.GetInt("T_depot_id")
  3692. T_product := c.GetString("T_product")
  3693. T_remark := c.GetString("T_remark")
  3694. T_project := c.GetString("T_project")
  3695. T_company_name := c.GetString("T_company_name")
  3696. T_payment_method := c.GetString("T_payment_method")
  3697. T_receive := c.GetString("T_receive")
  3698. if len(T_company_name) == 0 {
  3699. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司名称不能为空"}
  3700. c.ServeJSON()
  3701. return
  3702. }
  3703. if len(T_payment_method) == 0 {
  3704. c.Data["json"] = lib.JSONS{Code: 202, Msg: "付款方式不能为空"}
  3705. c.ServeJSON()
  3706. return
  3707. }
  3708. if len(T_receive) == 0 {
  3709. T_receive = c.User.T_uuid
  3710. }
  3711. var_ := Stock.StockOut{
  3712. T_number: T_number,
  3713. T_contract_number: T_contract_number,
  3714. T_depot_id: T_depot_id,
  3715. T_type: 1,
  3716. T_application_date: time.Now().Format("2006-01-02"),
  3717. T_receive: T_receive,
  3718. T_remark: T_remark,
  3719. T_project: T_project,
  3720. T_submit: c.User.T_uuid,
  3721. T_state: Stock.StockOutWaitAudit,
  3722. T_company_name: T_company_name,
  3723. T_payment_method: T_payment_method,
  3724. }
  3725. o.Begin()
  3726. StockOutProductDao := Stock.NewStockOutProduct(o)
  3727. StockDao := Stock.NewStock(o)
  3728. _, err := StockOutDao.Add_StockOut(var_)
  3729. if err != nil {
  3730. o.Rollback()
  3731. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  3732. c.ServeJSON()
  3733. return
  3734. }
  3735. // 1、添加出库单
  3736. if len(T_product) == 0 {
  3737. o.Rollback()
  3738. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  3739. c.ServeJSON()
  3740. return
  3741. }
  3742. productList := lib.SplitString(T_product, "|")
  3743. allProductList := []int{}
  3744. for _, v := range productList {
  3745. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3746. allProductList = append(allProductList, product_id)
  3747. product, _ := Basic.Read_Product_ById(product_id)
  3748. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3749. if num == 0 {
  3750. o.Rollback()
  3751. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3752. c.ServeJSON()
  3753. return
  3754. }
  3755. // 2、添加出库产品清单
  3756. stockOutProduct := Stock.StockOutProduct{
  3757. T_number: T_number,
  3758. T_product_id: product_id,
  3759. T_depot_id: T_depot_id,
  3760. T_num: num, // 出库数量
  3761. T_state: 2,
  3762. }
  3763. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  3764. if err != nil {
  3765. o.Rollback()
  3766. c.Data["json"] = lib.JSONS{Code: 203, Msg: "申请出库失败"}
  3767. c.ServeJSON()
  3768. return
  3769. }
  3770. // 3、增加占用库存
  3771. _, err = StockDao.AddOrUpdate_Occupy_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  3772. if err != nil {
  3773. o.Rollback()
  3774. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败" + err.Error()}
  3775. c.ServeJSON()
  3776. return
  3777. }
  3778. }
  3779. o.Commit()
  3780. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "出库申请", var_)
  3781. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3782. c.ServeJSON()
  3783. return
  3784. }
  3785. // 修改出库申请
  3786. func (c *StockController) StockOut_Apply_Edit() {
  3787. T_number := c.GetString("T_number") // 出库单号
  3788. //T_depot_id, _ := c.GetInt("T_depot_id")
  3789. T_project := c.GetString("T_project")
  3790. T_product := c.GetString("T_product")
  3791. T_remark := c.GetString("T_remark")
  3792. T_receive := c.GetString("T_receive")
  3793. T_company_name := c.GetString("T_company_name")
  3794. T_payment_method := c.GetString("T_payment_method")
  3795. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  3796. o := orm.NewOrm()
  3797. o.Begin()
  3798. StockOutDao := Stock.NewStockOut(o)
  3799. StockOutProductDao := Stock.NewStockOutProduct(o)
  3800. StockDao := Stock.NewStock(o)
  3801. // 查询入库信息
  3802. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3803. if err != nil {
  3804. o.Rollback()
  3805. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3806. c.ServeJSON()
  3807. return
  3808. }
  3809. if StockOut.T_state == Stock.StockOutAlreadyOut {
  3810. o.Rollback()
  3811. c.Data["json"] = lib.JSONS{Code: 202, Msg: "当前出库单已出库,不能修改!"}
  3812. c.ServeJSON()
  3813. return
  3814. }
  3815. // 查询入库产品信息
  3816. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  3817. var oldProductList []dto.StockProduct
  3818. var newProductList []dto.StockProduct
  3819. productOldMap := map[int]dto.StockProduct{}
  3820. allProductListMap := make(map[int]struct{})
  3821. for _, product := range productOldList {
  3822. stockProduct := dto.StockProduct{
  3823. T_product_id: product.T_product_id,
  3824. T_num: product.T_num,
  3825. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3826. }
  3827. oldProductList = append(oldProductList, stockProduct)
  3828. productOldMap[product.T_product_id] = stockProduct
  3829. if _, ok := allProductListMap[product.T_product_id]; !ok {
  3830. allProductListMap[product.T_product_id] = struct{}{}
  3831. }
  3832. if StockOut.T_state != Stock.StockOutAuditManagerUnPass && StockOut.T_state != Stock.StockOutAuditFinanceUnPass {
  3833. // 减少占用库存
  3834. product2, _ := Basic.Read_Product_ById(product.T_product_id)
  3835. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product2.Id, product2.T_class, product2.T_name, product2.T_model, product.T_num, 1)
  3836. if err != nil {
  3837. o.Rollback()
  3838. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3839. c.ServeJSON()
  3840. return
  3841. }
  3842. }
  3843. }
  3844. productNewList := lib.SplitString(T_product, "|")
  3845. productNewMap := map[int]dto.StockProduct{}
  3846. for _, v := range productNewList {
  3847. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3848. product, _ := Basic.Read_Product_ById(product_id)
  3849. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3850. if num == 0 {
  3851. o.Rollback()
  3852. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3853. c.ServeJSON()
  3854. return
  3855. }
  3856. T_relation_sn := strings.Split(v, "-")[2]
  3857. stockProduct := dto.StockProduct{
  3858. T_product_id: product_id,
  3859. T_num: num,
  3860. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  3861. }
  3862. newProductList = append(newProductList, stockProduct)
  3863. productNewMap[product_id] = stockProduct
  3864. if _, ok := allProductListMap[product_id]; !ok {
  3865. allProductListMap[product_id] = struct{}{}
  3866. }
  3867. // 增加占用库存
  3868. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  3869. if err != nil {
  3870. o.Rollback()
  3871. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3872. c.ServeJSON()
  3873. return
  3874. }
  3875. }
  3876. // 判断产品列表信息是否相同
  3877. StockProductListIsEqual, needDelete, needAdd, needEdit, _ := dto.StockProductListsEqual(oldProductList, newProductList)
  3878. // 两次提交的数据相同,则不用修改
  3879. if !StockProductListIsEqual {
  3880. if len(needDelete) > 0 {
  3881. // 删除入库产品列表
  3882. for _, v := range needDelete {
  3883. // 删除入库产品列表
  3884. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  3885. if err != nil {
  3886. o.Rollback()
  3887. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  3888. c.ServeJSON()
  3889. return
  3890. }
  3891. }
  3892. }
  3893. if len(needAdd) > 0 {
  3894. // 新增入库产品列表
  3895. for _, v := range needAdd {
  3896. StockOutProduct := Stock.StockOutProduct{
  3897. T_number: T_number,
  3898. T_product_id: v.T_product_id,
  3899. T_depot_id: StockOut.T_depot_id,
  3900. T_num: v.T_num, // 出库数量
  3901. T_state: 1,
  3902. }
  3903. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  3904. if err != nil {
  3905. o.Rollback()
  3906. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  3907. c.ServeJSON()
  3908. return
  3909. }
  3910. }
  3911. }
  3912. if len(needEdit) > 0 {
  3913. for _, v := range needEdit {
  3914. StockOutProduct := Stock.StockOutProduct{
  3915. T_number: StockOut.T_number,
  3916. T_product_id: v.T_product_id,
  3917. T_depot_id: StockOut.T_depot_id,
  3918. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  3919. T_state: 1,
  3920. }
  3921. // 更新产品表
  3922. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  3923. if err != nil {
  3924. o.Rollback()
  3925. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  3926. c.ServeJSON()
  3927. return
  3928. }
  3929. }
  3930. }
  3931. }
  3932. if len(T_remark) > 0 {
  3933. StockOut.T_remark = T_remark
  3934. }
  3935. if len(T_project) > 0 {
  3936. StockOut.T_project = T_project
  3937. }
  3938. if len(T_company_name) > 0 {
  3939. StockOut.T_company_name = T_company_name
  3940. }
  3941. if len(T_payment_method) > 0 {
  3942. StockOut.T_payment_method = T_payment_method
  3943. }
  3944. if len(T_receive) > 0 {
  3945. StockOut.T_receive = T_receive
  3946. }
  3947. StockOut.T_state = Stock.StockOutWaitAudit
  3948. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_company_name", "T_payment_method", "T_state")
  3949. if err != nil {
  3950. o.Rollback()
  3951. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  3952. c.ServeJSON()
  3953. return
  3954. }
  3955. o.Commit()
  3956. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "修改", StockOut)
  3957. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3958. c.ServeJSON()
  3959. return
  3960. }
  3961. // 删除出库申请
  3962. func (c *StockController) StockOut_Apply_Del() {
  3963. T_number := c.GetString("T_number") // 入库单号
  3964. o := orm.NewOrm()
  3965. o.Begin()
  3966. StockOutDao := Stock.NewStockOut(o)
  3967. StockOutProductDao := Stock.NewStockOutProduct(o)
  3968. StockDao := Stock.NewStock(o)
  3969. // 查询出库信息
  3970. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3971. if err != nil {
  3972. o.Rollback()
  3973. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3974. c.ServeJSON()
  3975. return
  3976. }
  3977. // 查询出库产品信息
  3978. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3979. var oldProductList []dto.StockProduct
  3980. allProductList := []int{}
  3981. for _, product := range productOldList {
  3982. oldProductList = append(oldProductList, dto.StockProduct{
  3983. T_product_id: product.T_product_id,
  3984. T_num: product.T_num,
  3985. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3986. })
  3987. allProductList = append(allProductList, product.T_product_id)
  3988. }
  3989. // 删除入库产品列表
  3990. for _, v := range oldProductList {
  3991. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3992. // 删除入库产品列表
  3993. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  3994. if err != nil {
  3995. o.Rollback()
  3996. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  3997. c.ServeJSON()
  3998. return
  3999. }
  4000. // 减少占用库存
  4001. _, err = StockDao.AddOrUpdate_Occupy_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  4002. if err != nil {
  4003. o.Rollback()
  4004. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败" + err.Error()}
  4005. c.ServeJSON()
  4006. return
  4007. }
  4008. }
  4009. err = StockOutDao.Delete_StockOut(stockOut)
  4010. if err != nil {
  4011. o.Rollback()
  4012. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  4013. c.ServeJSON()
  4014. return
  4015. }
  4016. o.Commit()
  4017. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "删除", stockOut)
  4018. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  4019. c.ServeJSON()
  4020. return
  4021. }
  4022. // 财务
  4023. func (c *StockController) StockOut_Finance_List() {
  4024. // 分页参数 初始化
  4025. page, _ := c.GetInt("page")
  4026. if page < 1 {
  4027. page = 1
  4028. }
  4029. page_z, _ := c.GetInt("page_z")
  4030. if page_z < 1 {
  4031. page_z = conf.Page_size
  4032. }
  4033. // 查询
  4034. T_depot_id, _ := c.GetInt("T_depot_id")
  4035. T_state, _ := c.GetInt("T_state")
  4036. T_contract_number := c.GetString("T_contract_number")
  4037. T_start_date := c.GetString("T_start_date")
  4038. T_end_date := c.GetString("T_end_date")
  4039. T_name := c.GetString("T_name")
  4040. userList, _ := NatsServer.Read_User_List_All()
  4041. Account.Read_User_All_Map(userList)
  4042. Basic.Read_Depot_All_Map()
  4043. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4044. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List("", T_name, Stock.StockOutTypeFinance, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  4045. var r_jsons lib.R_JSONS
  4046. r_jsons.Num = R_cnt
  4047. r_jsons.Data = R_List
  4048. r_jsons.Page = page
  4049. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  4050. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  4051. c.ServeJSON()
  4052. return
  4053. }
  4054. // 管理员
  4055. func (c *StockController) StockOut_Manager_List() {
  4056. // 分页参数 初始化
  4057. page, _ := c.GetInt("page")
  4058. if page < 1 {
  4059. page = 1
  4060. }
  4061. page_z, _ := c.GetInt("page_z")
  4062. if page_z < 1 {
  4063. page_z = conf.Page_size
  4064. }
  4065. // 查询
  4066. T_depot_id, _ := c.GetInt("T_depot_id")
  4067. T_state, _ := c.GetInt("T_state")
  4068. T_contract_number := c.GetString("T_contract_number")
  4069. T_start_date := c.GetString("T_start_date")
  4070. T_end_date := c.GetString("T_end_date")
  4071. T_name := c.GetString("T_name")
  4072. userList, _ := NatsServer.Read_User_List_All()
  4073. Account.Read_User_All_Map(userList)
  4074. Basic.Read_Depot_All_Map()
  4075. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4076. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List("", T_name, Stock.StockOutTypeManager, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  4077. var r_jsons lib.R_JSONS
  4078. r_jsons.Num = R_cnt
  4079. r_jsons.Data = R_List
  4080. r_jsons.Page = page
  4081. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  4082. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  4083. c.ServeJSON()
  4084. return
  4085. }
  4086. // 仓管
  4087. func (c *StockController) StockOut_Warehouse_List() {
  4088. // 分页参数 初始化
  4089. page, _ := c.GetInt("page")
  4090. if page < 1 {
  4091. page = 1
  4092. }
  4093. page_z, _ := c.GetInt("page_z")
  4094. if page_z < 1 {
  4095. page_z = conf.Page_size
  4096. }
  4097. // 查询
  4098. T_depot_id, _ := c.GetInt("T_depot_id")
  4099. T_state, _ := c.GetInt("T_state")
  4100. T_contract_number := c.GetString("T_contract_number")
  4101. T_start_date := c.GetString("T_start_date")
  4102. T_end_date := c.GetString("T_end_date")
  4103. T_name := c.GetString("T_name")
  4104. userList, _ := NatsServer.Read_User_List_All()
  4105. Account.Read_User_All_Map(userList)
  4106. Basic.Read_Depot_All_Map()
  4107. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4108. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List(c.User.T_uuid, T_name, Stock.StockOutTypeWarehouse, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  4109. var r_jsons lib.R_JSONS
  4110. r_jsons.Num = R_cnt
  4111. r_jsons.Data = R_List
  4112. r_jsons.Page = page
  4113. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  4114. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  4115. c.ServeJSON()
  4116. return
  4117. }
  4118. // 财务/总经理审核
  4119. func (c *StockController) StockOut_Audit() {
  4120. T_number := c.GetString("T_number") // 出库单号
  4121. //T_depot_id, _ := c.GetInt("T_depot_id")
  4122. T_audit, _ := c.GetInt("T_audit") // 审核 2财务通过 3财务不通过 4总经理通过 5总经理不通过 6已出库
  4123. T_approval_opinion := c.GetString("T_approval_opinion") // 审批意见
  4124. T_approval_img := c.GetString("T_approval_img") // 审批意见
  4125. T_type := c.GetString("T_type") // 类型 Finance Manager
  4126. o := orm.NewOrm()
  4127. o.Begin()
  4128. StockOutDao := Stock.NewStockOut(o)
  4129. StockDao := Stock.NewStock(o)
  4130. StockOutProductDao := Stock.NewStockOutProduct(o)
  4131. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  4132. if err != nil {
  4133. o.Rollback()
  4134. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  4135. c.ServeJSON()
  4136. return
  4137. }
  4138. StockOut.T_state = T_audit
  4139. if T_type == Stock.StockOutTypeFinance {
  4140. StockOut.T_finance_approval_opinion = T_approval_opinion
  4141. StockOut.T_finance_approval_img = T_approval_img
  4142. } else {
  4143. StockOut.T_manager_approval_opinion = T_approval_opinion
  4144. StockOut.T_manager_approval_img = T_approval_img
  4145. }
  4146. if T_audit == Stock.StockOutAuditFinanceUnPass || T_audit == Stock.StockOutAuditManagerUnPass {
  4147. productList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  4148. for _, v := range productList {
  4149. product, _ := Basic.Read_Product_ById(v.T_product_id)
  4150. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  4151. if err != nil {
  4152. o.Rollback()
  4153. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  4154. c.ServeJSON()
  4155. return
  4156. }
  4157. }
  4158. }
  4159. err = StockOutDao.Update_StockOut(StockOut, "T_state", "T_finance_approval_opinion", "T_manager_approval_opinion", "T_finance_approval_img", "T_manager_approval_img")
  4160. if err != nil {
  4161. o.Rollback()
  4162. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  4163. c.ServeJSON()
  4164. return
  4165. }
  4166. o.Commit()
  4167. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "审批", StockOut)
  4168. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  4169. c.ServeJSON()
  4170. return
  4171. }
  4172. // 仓管出库
  4173. func (c *StockController) StockOut_Warehouse() {
  4174. T_number := c.GetString("T_number") // 出库单号
  4175. T_date := c.GetString("T_date")
  4176. date, is := lib.DateStrToTime(T_date)
  4177. if !is {
  4178. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  4179. c.ServeJSON()
  4180. return
  4181. }
  4182. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "确认出库", T_number)
  4183. o := orm.NewOrm()
  4184. o.Begin()
  4185. StockOutDao := Stock.NewStockOut(o)
  4186. StockOutProductDao := Stock.NewStockOutProduct(o)
  4187. DeviceDao := Stock.NewDevice(o)
  4188. StockDao := Stock.NewStock(o)
  4189. // 查询入库信息
  4190. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  4191. if err != nil {
  4192. o.Rollback()
  4193. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  4194. c.ServeJSON()
  4195. return
  4196. }
  4197. T_product := c.GetString("T_product")
  4198. productList := lib.SplitString(T_product, "|")
  4199. allProductList := []int{}
  4200. for _, v := range productList {
  4201. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  4202. allProductList = append(allProductList, product_id)
  4203. product, _ := Basic.Read_Product_ById(product_id)
  4204. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  4205. if num == 0 {
  4206. o.Rollback()
  4207. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  4208. c.ServeJSON()
  4209. return
  4210. }
  4211. T_relation_sn := strings.Split(v, "-")[2]
  4212. if T_relation_sn == "" && product.T_relation_sn == 1 {
  4213. o.Rollback()
  4214. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  4215. c.ServeJSON()
  4216. return
  4217. }
  4218. if len(T_relation_sn) > 0 {
  4219. snList := strings.Split(T_relation_sn, ",")
  4220. if product.T_relation_sn == 1 && num != len(snList) {
  4221. o.Rollback()
  4222. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s SN数量与出库数量不一致,请核查!", product.T_name)}
  4223. c.ServeJSON()
  4224. return
  4225. }
  4226. if len(snList) > 0 {
  4227. for _, sn := range snList {
  4228. mqtt := Stock.Read_MqttUser(sn)
  4229. // 根据IMEI查询上次入库的批次号
  4230. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  4231. if err != nil {
  4232. logs.Error(lib.FuncName(), err)
  4233. }
  4234. device := Stock.Device{
  4235. T_contract_number: StockOut.T_contract_number,
  4236. T_product_id: product_id,
  4237. T_out_number: T_number,
  4238. T_sn: sn,
  4239. T_iccid: mqtt.Iccid,
  4240. T_imei: mqtt.Imei,
  4241. T_State: 1,
  4242. T_project: StockOut.T_project,
  4243. T_batch_number: batchNumber,
  4244. CreateTime: date,
  4245. }
  4246. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  4247. if err != nil {
  4248. o.Rollback()
  4249. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  4250. c.ServeJSON()
  4251. return
  4252. }
  4253. }
  4254. }
  4255. }
  4256. // 3、添加出库产品清单
  4257. stockOutProduct := Stock.StockOutProduct{
  4258. T_number: T_number,
  4259. T_product_id: product_id,
  4260. T_depot_id: StockOut.T_depot_id,
  4261. T_num: num, // 出库数量
  4262. T_date: T_date, // 出库日期
  4263. T_relation_sn: T_relation_sn,
  4264. T_state: 1,
  4265. }
  4266. // 更新产品表
  4267. err = StockOutProductDao.Update_StockOutProduct(stockOutProduct)
  4268. if err != nil {
  4269. o.Rollback()
  4270. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  4271. c.ServeJSON()
  4272. return
  4273. }
  4274. // 5、更新产品库存列表、占用库存
  4275. _, err = StockDao.Stock_Warehouse(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num)
  4276. if err != nil {
  4277. o.Rollback()
  4278. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  4279. c.ServeJSON()
  4280. return
  4281. }
  4282. }
  4283. StockOut.T_warehouse = c.User.T_uuid
  4284. StockOut.T_state = Stock.StockOutAlreadyOut
  4285. StockOut.T_date = T_date
  4286. err = StockOutDao.Update_StockOut(StockOut, "T_state", "T_date", "T_warehouse")
  4287. if err != nil {
  4288. o.Rollback()
  4289. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  4290. c.ServeJSON()
  4291. return
  4292. }
  4293. o.Commit()
  4294. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  4295. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "出库", StockOut)
  4296. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  4297. c.ServeJSON()
  4298. return
  4299. }
  4300. // StockMonth_Update_Return 批量更新所有StockMonth的退库数据
  4301. func (c *StockController) StockMonth_Update_Return() {
  4302. // 可选参数:指定仓库和时间范围
  4303. T_depot_id, _ := c.GetInt("T_depot_id")
  4304. T_start_date := c.GetString("T_start_date") // 格式:2024-01
  4305. T_end_date := c.GetString("T_end_date") // 格式:2024-12
  4306. // 调用后台更新函数
  4307. count, err := UpdateAllStockMonthReturn(T_depot_id, T_start_date, T_end_date)
  4308. if err != nil {
  4309. c.Data["json"] = lib.JSONS{Code: 203, Msg: fmt.Sprintf("更新失败:%v", err)}
  4310. c.ServeJSON()
  4311. return
  4312. }
  4313. NatsServer.AddUserLogs(c.User.T_uuid, "库存月度统计", "批量更新退库数据", fmt.Sprintf("更新了%d条记录", count))
  4314. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: fmt.Sprintf("成功更新%d条记录", count)}
  4315. c.ServeJSON()
  4316. }
  4317. // UpdateAllStockMonthReturn 批量更新所有StockMonth记录的退库数量
  4318. // T_depot_id: 仓库ID,0表示所有仓库
  4319. // T_start_date: 开始月份,格式:2024-01,为空则不限制
  4320. // T_end_date: 结束月份,格式:2024-12,为空则不限制
  4321. func UpdateAllStockMonthReturn(T_depot_id int, T_start_date, T_end_date string) (count int, err error) {
  4322. o := orm.NewOrm()
  4323. StockMonthDao := Stock.NewStockMonth(o)
  4324. StockInProductDao := Stock.NewStockInProduct(o)
  4325. // 读取所有StockMonth记录
  4326. allStockMonths, err := StockMonthDao.Read_All_StockMonth()
  4327. if err != nil {
  4328. logs.Error(lib.FuncName(), err)
  4329. return 0, err
  4330. }
  4331. count = 0
  4332. for _, stockMonth := range allStockMonths {
  4333. // 如果指定了仓库ID,则过滤
  4334. if T_depot_id > 0 && stockMonth.T_depot_id != T_depot_id {
  4335. continue
  4336. }
  4337. // 如果指定了开始月份,则过滤
  4338. if len(T_start_date) > 0 && stockMonth.T_month < T_start_date {
  4339. continue
  4340. }
  4341. // 如果指定了结束月份,则过滤
  4342. if len(T_end_date) > 0 && stockMonth.T_month > T_end_date {
  4343. continue
  4344. }
  4345. // 查询该产品在该月份的退库数量
  4346. returnTotal := StockInProductDao.Read_StockReturn_Total(stockMonth.T_depot_id, stockMonth.T_product_id, stockMonth.T_month)
  4347. // 如果退库数量与原值不同,则更新
  4348. if stockMonth.T_return != returnTotal {
  4349. stockMonth.T_return = returnTotal
  4350. err = StockMonthDao.Update_StockMonth(stockMonth, "T_return")
  4351. if err != nil {
  4352. logs.Error(lib.FuncName(), fmt.Sprintf("更新失败:仓库%d,产品%d,月份%s", stockMonth.T_depot_id, stockMonth.T_product_id, stockMonth.T_month), err)
  4353. continue
  4354. }
  4355. count++
  4356. }
  4357. }
  4358. logs.Info(fmt.Sprintf("批量更新退库数据完成,共更新%d条记录", count))
  4359. return count, nil
  4360. }