12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825 |
- package controllers
- import (
- "ERP_storage/Nats"
- "ERP_storage/Nats/NatsServer"
- "ERP_storage/conf"
- "ERP_storage/dto"
- "ERP_storage/logs"
- "ERP_storage/models/Account"
- "ERP_storage/models/Basic"
- "ERP_storage/models/Contract"
- "ERP_storage/models/Property"
- "ERP_storage/models/Stock"
- "fmt"
- natslibs "git.baozhida.cn/ERP_libs/Nats"
- userlibs "git.baozhida.cn/ERP_libs/User"
- "git.baozhida.cn/ERP_libs/lib"
- "github.com/beego/beego/v2/adapter/orm"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/robfig/cron/v3"
- "github.com/xuri/excelize/v2"
- "math"
- "os"
- "strconv"
- "strings"
- "time"
- )
- type StockController struct {
- beego.Controller
- User userlibs.User
- }
- func (c *StockController) Prepare() {
- c.User = *Account.User_r
- }
- func (c *StockController) Device_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_state, _ := c.GetInt("T_state")
- T_name := c.GetString("T_name")
- T_product_name := c.GetString("T_product_name") // 产品名称
- T_product_model := c.GetString("T_product_model") // 产品型号
- DeviceDao := Stock.NewDevice(orm.NewOrm())
- R_List, R_cnt := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *StockController) Device_Check() {
- T_sn := c.GetString("T_sn")
- T_type, _ := c.GetInt("T_type") //1-出库 2-入库
- DeviceDao := Stock.NewDevice(orm.NewOrm())
- device, err := DeviceDao.Read_Device_ByT_sn(T_sn)
- if err != nil && err.Error() != orm.ErrNoRows.Error() {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "请稍后重试!"}
- c.ServeJSON()
- return
- }
- // T_State 1-已出库 2-未出库/入库
- if len(device.T_in_number) > 0 && device.T_State == 2 && T_type == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
- c.ServeJSON()
- return
- }
- if T_type == 2 {
- mqtt := Stock.Read_MqttUser(T_sn)
- if len(mqtt.Username) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未授权,请先授权!", T_sn)}
- c.ServeJSON()
- return
- }
- }
- if len(device.T_out_number) > 0 && device.T_State == 1 && T_type == 1 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
- c.ServeJSON()
- return
- }
- if T_type == 1 {
- if device.Id == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未入库,请先入库!", T_sn)}
- c.ServeJSON()
- return
- }
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *StockController) Stock_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_depot_id, _ := c.GetInt("T_depot_id")
- // 查询
- T_product_name := c.GetString("T_product_name")
- T_product_model := c.GetString("T_product_model")
- T_product_class, _ := c.GetInt("T_product_class")
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Basic.Read_Depot_All_Map()
- StockDao := Stock.NewStock(orm.NewOrm())
- R_List, R_cnt := StockDao.Read_Stock_List(T_depot_id, T_product_class, T_product_name, T_product_model, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 修改库存排序
- func (c *StockController) Stock_Edit_Sort() {
- T_id, _ := c.GetInt("T_id")
- T_sort, _ := c.GetInt("T_sort")
- StockDao := Stock.NewStock(orm.NewOrm())
- Stock_r, err := StockDao.Read_Stock_ById(T_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- Stock_r.T_sort = T_sort
- if err = StockDao.Update_Stock(Stock_r, "T_sort"); err != nil {
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
- c.ServeJSON()
- return
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "库存", "修改", Stock_r)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *StockController) Stock_Detail_List() {
- // 查询
- T_depot_id, _ := c.GetInt("T_depot_id")
- // 查询
- T_product_id, _ := c.GetInt("T_product_id")
- T_start_date := c.GetString("T_start_date")
- T_end_date := c.GetString("T_end_date")
- now := time.Now()
- if len(T_start_date) == 0 {
- T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
- } else {
- T_start_date_t, _ := lib.DateStrToTime(T_start_date)
- T_start_date = T_start_date_t.Format("2006-01")
- }
- if len(T_end_date) == 0 {
- T_end_date = now.Format("2006-01")
- } else {
- T_end_date_t, _ := lib.DateStrToTime(T_end_date)
- T_end_date = T_end_date_t.Format("2006-01")
- }
- StockOutDao := Stock.NewStockOut(orm.NewOrm())
- StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
- R_List := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, 0, T_start_date, T_end_date)
- for i := 0; i < len(R_List); i++ {
- 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), "|")
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
- c.ServeJSON()
- return
- }
- func (c *StockController) Stock_Detail_Excel() {
- // 查询
- T_depot_id, _ := c.GetInt("T_depot_id")
- T_product_id, _ := c.GetInt("T_product_id")
- // 查询
- T_start_date := c.GetString("T_start_date")
- T_end_date := c.GetString("T_end_date")
- now := time.Now()
- if len(T_start_date) == 0 {
- T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
- } else {
- T_start_date_t, _ := lib.DateStrToTime(T_start_date)
- T_start_date = T_start_date_t.Format("2006-01")
- }
- if len(T_end_date) == 0 {
- T_end_date = now.Format("2006-01")
- } else {
- T_end_date_t, _ := lib.DateStrToTime(T_end_date)
- T_end_date = T_end_date_t.Format("2006-01")
- }
- var class_List []Basic.ProductClass_R
- filename := fmt.Sprintf("进销存(%s)", lib.GetRandstring(6, "0123456789", 0))
- if T_product_id > 0 {
- product, _ := Basic.Read_Product_ById(T_product_id)
- filename = fmt.Sprintf("进销存-%s明细(%s)", product.T_name, lib.GetRandstring(6, "0123456789", 0))
- class, _ := Basic.Read_ProductClass_ById(product.T_class)
- class_List = append(class_List, Basic.ProductClassToProductClass_R(class))
- } else {
- class_List, _ = Basic.Read_ProductClass_List("", 0, 9999)
- }
- f := excelize.NewFile() // 设置单元格的值
- Style1, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- })
- Style2, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
- StockOutDao := Stock.NewStockOut(orm.NewOrm())
- var j = 0
- for _, r := range class_List {
- StockList := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, r.Id, T_start_date, T_end_date)
- if len(StockList) == 0 {
- continue
- }
- if j == 0 {
- f.SetSheetName("Sheet1", r.T_name)
- } else {
- f.NewSheet(r.T_name)
- }
- j += 1
- f.MergeCell(r.T_name, "A1", "J1")
- f.SetRowStyle(r.T_name, 1, 1, Style1)
- f.SetCellValue(r.T_name, "A1", fmt.Sprintf("宝智达科技产品进销存统计表"))
- f.SetRowHeight(r.T_name, 1, 30)
- // 这里设置表头
- f.SetCellStyle(r.T_name, "A2", "J2", Style2)
- f.SetRowHeight(r.T_name, 2, 20)
- f.SetCellValue(r.T_name, "A2", "序号")
- f.SetCellValue(r.T_name, "B2", "产品名称")
- f.SetCellValue(r.T_name, "C2", "型号")
- f.SetCellValue(r.T_name, "D2", "规格")
- f.SetCellValue(r.T_name, "E2", "月份")
- f.SetCellValue(r.T_name, "F2", "期初库存")
- f.SetCellValue(r.T_name, "G2", "当月入库")
- f.SetCellValue(r.T_name, "H2", "当月出库")
- f.SetCellValue(r.T_name, "I2", "期末库存")
- f.SetCellValue(r.T_name, "J2", "出库项目")
- //f.SetCellValue(r.T_name, "K2", "备注")
- // 设置列宽
- f.SetColWidth(r.T_name, "A", "A", 10)
- f.SetColWidth(r.T_name, "B", "B", 10)
- f.SetColWidth(r.T_name, "C", "D", 10)
- f.SetColWidth(r.T_name, "D", "D", 10)
- f.SetColWidth(r.T_name, "E", "E", 10)
- f.SetColWidth(r.T_name, "F", "F", 10)
- f.SetColWidth(r.T_name, "G", "G", 10)
- f.SetColWidth(r.T_name, "H", "H", 10)
- f.SetColWidth(r.T_name, "I", "I", 10)
- f.SetColWidth(r.T_name, "J", "J", 70)
- //f.SetColWidth(r.T_name, "K", "K", 10)
- line := 2
- // 循环写入数据
- for _, v := range StockList {
- line++
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- f.SetCellValue(r.T_name, fmt.Sprintf("A%d", line), line-2)
- f.SetCellValue(r.T_name, fmt.Sprintf("B%d", line), product.T_name)
- f.SetCellValue(r.T_name, fmt.Sprintf("C%d", line), product.T_model)
- f.SetCellValue(r.T_name, fmt.Sprintf("D%d", line), product.T_spec)
- f.SetCellValue(r.T_name, fmt.Sprintf("E%d", line), v.T_month)
- f.SetCellValue(r.T_name, fmt.Sprintf("F%d", line), v.T_beginning)
- f.SetCellValue(r.T_name, fmt.Sprintf("G%d", line), v.T_in)
- f.SetCellValue(r.T_name, fmt.Sprintf("H%d", line), v.T_out)
- f.SetCellValue(r.T_name, fmt.Sprintf("I%d", line), v.T_ending)
- T_project := StockOutDao.Read_StockOut_T_project(T_depot_id, product.Id, v.T_month)
- //f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_project)
- f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), strings.Join(T_project, "\r\n"))
- }
- Style4, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 10, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- f.SetCellStyle(r.T_name, "A2", fmt.Sprintf("I%d", line), Style4)
- Style5, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 10, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- f.SetCellStyle(r.T_name, "J2", fmt.Sprintf("J%d", line), Style5)
- }
- // 保存文件
- if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
- fmt.Println(err)
- }
- var url string
- //// 上传 OSS
- nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
- url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err := os.Remove("ofile/" + filename + ".xlsx")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockIn_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_depot_id, _ := c.GetInt("T_depot_id")
- T_start_date := c.GetString("T_start_date")
- T_end_date := c.GetString("T_end_date")
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Basic.Read_Depot_All_Map()
- StockInDao := Stock.NewStockIn(orm.NewOrm())
- R_List, R_cnt := StockInDao.Read_StockIn_List(T_depot_id, T_start_date, T_end_date, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockIn_List_Product() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_depot_id, _ := c.GetInt("T_depot_id")
- T_name := c.GetString("T_name")
- T_start_date := c.GetString("T_start_date")
- T_end_date := c.GetString("T_end_date")
- T_product_name := c.GetString("T_product_name") // 产品名称
- T_product_model := c.GetString("T_product_model") // 产品型号
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Basic.Read_Depot_All_Map()
- StockInDao := Stock.NewStockIn(orm.NewOrm())
- 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)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockIn_List_Product_Excel() {
- // 查询
- T_depot_id, _ := c.GetInt("T_depot_id")
- T_name := c.GetString("T_name")
- T_start_date := c.GetString("T_start_date")
- T_end_date := c.GetString("T_end_date")
- T_product_name := c.GetString("T_product_name") // 产品名称
- T_product_model := c.GetString("T_product_model") // 产品型号
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Basic.Read_Depot_All_Map()
- StockInDao := Stock.NewStockIn(orm.NewOrm())
- 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)
- filename := fmt.Sprintf("入库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
- f := excelize.NewFile() // 设置单元格的值
- Style1, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- })
- Style2, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- Style3, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- f.MergeCell("Sheet1", "A1", "K1")
- f.SetRowStyle("Sheet1", 1, 1, Style1)
- f.SetCellValue("Sheet1", "A1", fmt.Sprintf("入库明细单"))
- f.SetRowHeight("Sheet1", 1, 30)
- // 这里设置表头
- f.SetCellStyle("Sheet1", "A2", "K2", Style2)
- f.SetRowHeight("Sheet1", 2, 20)
- f.SetCellValue("Sheet1", "A2", "序号")
- f.SetCellValue("Sheet1", "B2", "入库单号")
- f.SetCellValue("Sheet1", "C2", "经办人")
- f.SetCellValue("Sheet1", "D2", "入库日期")
- f.SetCellValue("Sheet1", "E2", "入库仓库")
- f.SetCellValue("Sheet1", "F2", "产品名称")
- f.SetCellValue("Sheet1", "G2", "产品型号")
- f.SetCellValue("Sheet1", "H2", "数量")
- f.SetCellValue("Sheet1", "I2", "是否关联SN")
- f.SetCellValue("Sheet1", "J2", "备注")
- f.SetCellValue("Sheet1", "K2", "SN")
- // 设置列宽
- f.SetColWidth("Sheet1", "A", "A", 10)
- f.SetColWidth("Sheet1", "B", "B", 15)
- f.SetColWidth("Sheet1", "C", "D", 10)
- f.SetColWidth("Sheet1", "D", "D", 10)
- f.SetColWidth("Sheet1", "E", "E", 10)
- f.SetColWidth("Sheet1", "F", "F", 20)
- f.SetColWidth("Sheet1", "G", "G", 15)
- f.SetColWidth("Sheet1", "H", "H", 10)
- f.SetColWidth("Sheet1", "I", "I", 12)
- f.SetColWidth("Sheet1", "J", "J", 30)
- f.SetColWidth("Sheet1", "K", "K", 100)
- line := 2
- for _, product := range R_List {
- line += 1
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_submit_name)
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), product.T_product_name)
- f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_model)
- f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_num)
- var relation_sn string
- if product.T_product_relation_sn == 1 {
- relation_sn = "是"
- } else {
- relation_sn = "否"
- }
- f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), relation_sn)
- f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), product.T_remark)
- f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_relation_sn)
- }
- f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("K%d", line), Style3)
- // 保存文件
- if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
- fmt.Println(err)
- }
- var url string
- //// 上传 OSS
- nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
- url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err := os.Remove("ofile/" + filename + ".xlsx")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockIn_Get() {
- // 查询
- T_number := c.GetString("T_number")
- o := orm.NewOrm()
- StockInDao := Stock.NewStockIn(o)
- StockInProductDao := Stock.NewStockInProduct(o)
- stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
- var pList []Stock.StockInProduct_R
- for _, v := range productList {
- pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
- }
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Basic.Read_Depot_All_Map()
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockInToStockIn_Detail(stockIn, pList)}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockIn_Add() {
- rand_x := 0
- T_number := ""
- o := orm.NewOrm()
- o.Begin()
- StockInDao := Stock.NewStockIn(o)
- for true {
- T_number = "RK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
- _, err := StockInDao.Read_StockIn_ByT_number(T_number)
- if err != nil && err.Error() == orm.ErrNoRows.Error() {
- break
- }
- rand_x += 1
- }
- T_depot_id, _ := c.GetInt("T_depot_id")
- T_date := c.GetString("T_date")
- T_product := c.GetString("T_product")
- T_remark := c.GetString("T_remark")
- _, is := lib.DateStrToTime(T_date)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
- c.ServeJSON()
- return
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
- var_ := Stock.StockIn{
- T_number: T_number,
- T_depot_id: T_depot_id,
- T_date: T_date,
- T_remark: T_remark,
- T_submit: c.User.T_uuid,
- }
- StockInProductDao := Stock.NewStockInProduct(o)
- StockDao := Stock.NewStock(o)
- DeviceDao := Stock.NewDevice(o)
- IotCardDao := Property.NewIotCard(&o)
- _, err := StockInDao.Add_StockIn(var_)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- if len(T_product) == 0 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
- c.ServeJSON()
- return
- }
- productList := lib.SplitString(T_product, "|")
- allProductList := []int{}
- for _, v := range productList {
- product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
- allProductList = append(allProductList, product_id)
- product, _ := Basic.Read_Product_ById(product_id)
- num, _ := strconv.Atoi(strings.Split(v, "-")[1])
- T_relation_sn := strings.Split(v, "-")[2]
- if T_relation_sn == "" && product.T_relation_sn == 1 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
- c.ServeJSON()
- return
- }
- if len(T_relation_sn) > 0 {
- snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
- for _, sn := range snList {
- mqtt := Stock.Read_MqttUser(sn)
- // 添加设备
- device := Stock.Device{
- T_contract_number: "",
- T_out_number: "",
- T_product_id: product_id,
- T_in_number: T_number,
- T_sn: sn,
- T_iccid: mqtt.Iccid,
- T_imei: mqtt.Imei,
- T_State: 2,
- }
- _, err = DeviceDao.AddOrUpdate_Device(device, 2)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
- c.ServeJSON()
- return
- }
- // 添加物联网卡
- iotCard := Property.IotCard{
- T_iccid: mqtt.Iccid,
- T_sn: sn,
- T_State: 2, //1-未使用 2-已使用 3-已作废
- T_type: "4G",
- }
- _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- }
- }
- stockInProduct := Stock.StockInProduct{
- T_number: T_number,
- T_product_id: product_id,
- T_depot_id: T_depot_id,
- T_num: num, // 入库数量
- T_date: T_date, // 入库日期
- T_relation_sn: T_relation_sn,
- }
- _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- }
- o.Commit()
- StockIn_Edit_StockMonth(T_date, T_depot_id, allProductList)
- NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- func StockIn_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
- date, _ := lib.DateStrToTime(T_date)
- //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
- // // 修改本月数据不同步库存
- // return nil
- //}
- //T_month := date.Format("2006-01")
- months := generateMonthList(date)
- o := orm.NewOrm()
- StockOutDao := Stock.NewStockOut(o)
- StockMonthDao := Stock.NewStockMonth(o)
- StockOutProductDao := Stock.NewStockOutProduct(o)
- StockInProductDao := Stock.NewStockInProduct(o)
- for _, T_product_id := range allProductList {
- for _, T_month := range months {
- // 获取当前产品本月出入库数量
- lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
- stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
- if stockMonth.Id > 0 {
- // 修改
- stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
- if len(lastMonth) > 0 {
- stockMonth.T_beginning = lastMonth[0].T_ending
- } else {
- stockMonth.T_beginning = 0
- }
- // 期末库存 = 期初库存+入库-出库
- stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
- err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_beginning", "T_ending")
- if err != nil {
- return err
- }
- } else {
- product, _ := Basic.Read_Product_ById(T_product_id)
- // 添加库存记录
- stockMonth = Stock.StockMonth{
- T_depot_id: T_depot_id,
- T_product_id: T_product_id,
- T_product_class: product.T_class,
- T_month: T_month,
- }
- // 获取当前产品本月出入库数量
- stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
- stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
- if len(lastMonth) > 0 {
- stockMonth.T_beginning = lastMonth[0].T_ending
- } else {
- stockMonth.T_beginning = 0
- }
- // 期末库存 = 期初库存+入库-出库
- stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
- stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
- _, err := StockMonthDao.Add_StockMonth(stockMonth)
- if err != nil {
- return err
- }
- }
- }
- }
- return nil
- }
- func (c *StockController) StockIn_Edit() {
- T_number := c.GetString("T_number") // 入库单号
- //T_depot_id, _ := c.GetInt("T_depot_id")
- T_date := c.GetString("T_date")
- T_product := c.GetString("T_product")
- T_remark := c.GetString("T_remark")
- date, is := lib.DateStrToTime(T_date)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
- c.ServeJSON()
- return
- }
- if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
- T_date = ""
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
- o := orm.NewOrm()
- o.Begin()
- StockInDao := Stock.NewStockIn(o)
- StockInProductDao := Stock.NewStockInProduct(o)
- StockDao := Stock.NewStock(o)
- DeviceDao := Stock.NewDevice(o)
- IotCardDao := Property.NewIotCard(&o)
- // 查询入库信息
- stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
- T_old_date := stockIn.T_date
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- // 查询入库产品信息
- productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
- var oldProductList []dto.StockProduct
- var newProductList []dto.StockProduct
- allProductListMap := make(map[int]struct{})
- productOldMap := map[int]dto.StockProduct{}
- for _, product := range productOldList {
- stockProduct := dto.StockProduct{
- T_product_id: product.T_product_id,
- T_num: product.T_num,
- T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
- }
- oldProductList = append(oldProductList, stockProduct)
- productOldMap[product.T_product_id] = stockProduct
- if _, ok := allProductListMap[product.T_product_id]; !ok {
- allProductListMap[product.T_product_id] = struct{}{}
- }
- }
- productNewList := lib.SplitString(T_product, "|")
- productNewMap := map[int]dto.StockProduct{}
- for _, v := range productNewList {
- product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
- num, _ := strconv.Atoi(strings.Split(v, "-")[1])
- T_relation_sn := strings.Split(v, "-")[2]
- stockProduct := dto.StockProduct{
- T_product_id: product_id,
- T_num: num,
- T_relation_sn: lib.SplitString(T_relation_sn, ","),
- }
- newProductList = append(newProductList, stockProduct)
- productNewMap[product_id] = stockProduct
- if _, ok := allProductListMap[product_id]; !ok {
- allProductListMap[product_id] = struct{}{}
- }
- }
- // 判断产品列表信息是否相同
- StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
- // 两次提交的数据相同,则不用修改
- if !StockProductListIsEqual {
- if len(needDelete) > 0 {
- // 删除入库产品列表
- for _, v := range needDelete {
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- // 删除入库产品列表
- err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
- c.ServeJSON()
- return
- }
- if product.T_relation_sn == 1 {
- // 删除该入库单关联sn
- for _, sn := range v.T_relation_sn {
- err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
- c.ServeJSON()
- return
- }
- }
- }
- // 减少库存
- _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
- c.ServeJSON()
- return
- }
- }
- }
- if len(needAdd) > 0 {
- // 新增入库产品列表
- for _, v := range needAdd {
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
- c.ServeJSON()
- return
- }
- // 添加该入库单关联sn
- for _, sn := range v.T_relation_sn {
- mqtt := Stock.Read_MqttUser(sn)
- // 添加设备
- device := Stock.Device{
- T_contract_number: "",
- T_out_number: "",
- T_product_id: v.T_product_id,
- T_in_number: stockIn.T_number,
- T_sn: sn,
- T_iccid: mqtt.Iccid,
- T_imei: mqtt.Imei,
- T_State: 2,
- CreateTime: stockIn.CreateTime,
- UpdateTime: stockIn.UpdateTime,
- }
- _, err = DeviceDao.AddOrUpdate_Device(device, 2)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
- c.ServeJSON()
- return
- }
- // 添加物联网卡
- iotCard := Property.IotCard{
- T_iccid: mqtt.Iccid,
- T_sn: sn,
- T_State: 2, //1-未使用 2-已使用 3-已作废
- T_type: "4G",
- }
- _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- }
- stockInProduct := Stock.StockInProduct{
- T_number: T_number,
- T_product_id: v.T_product_id,
- T_depot_id: stockIn.T_depot_id,
- T_num: v.T_num, // 入库数量
- T_date: stockIn.T_date, // 入库日期
- T_relation_sn: strings.Join(v.T_relation_sn, ","),
- }
- _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- // 增加库存
- _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
- c.ServeJSON()
- return
- }
- }
- }
- if len(needEdit) > 0 {
- for _, v := range needEdit {
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- stockInProduct := Stock.StockInProduct{
- T_number: stockIn.T_number,
- T_product_id: v.T_product_id,
- T_depot_id: stockIn.T_depot_id,
- T_num: productNewMap[v.T_product_id].T_num, // 入库数量
- T_date: stockIn.T_date, // 入库日期
- }
- // 入库数量比之前多,增加库存
- var T_type int
- var T_num int
- if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
- // 增加库存
- T_type = 2
- T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
- } else {
- // 减少库存
- T_type = 1
- T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
- }
- _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- // 更新产品库存表
- err = StockInProductDao.Update_StockInProduct(stockInProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
- c.ServeJSON()
- return
- }
- }
- }
- for _, diff := range snDiff {
- product, _ := Basic.Read_Product_ById(diff.T_product_id)
- // 删除减少的sn
- if len(diff.T_delete_relation_sn) > 0 {
- for _, sn := range diff.T_delete_relation_sn {
- err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- }
- // 减少库存
- _, 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)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- }
- if len(diff.T_add_relation_sn) > 0 {
- for _, sn := range diff.T_add_relation_sn {
- mqtt := Stock.Read_MqttUser(sn)
- // 添加设备
- device := Stock.Device{
- T_contract_number: "",
- T_out_number: "",
- T_product_id: diff.T_product_id,
- T_in_number: stockIn.T_number,
- T_sn: sn,
- T_iccid: mqtt.Iccid,
- T_imei: mqtt.Imei,
- T_State: 2,
- CreateTime: stockIn.CreateTime,
- UpdateTime: stockIn.UpdateTime,
- }
- _, err = DeviceDao.AddOrUpdate_Device(device, 2)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
- c.ServeJSON()
- return
- }
- // 添加物联网卡
- iotCard := Property.IotCard{
- T_iccid: mqtt.Iccid,
- T_sn: sn,
- T_State: 2, //1-未使用 2-已使用 3-已作废
- T_type: "4G",
- }
- _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- }
- // 增加库存
- _, 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)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- }
- stockInProduct := Stock.StockInProduct{
- T_number: stockIn.T_number,
- T_product_id: diff.T_product_id,
- T_depot_id: stockIn.T_depot_id,
- T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
- T_date: stockIn.T_date, // 入库日期
- T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
- }
- // 更新产品库存表
- err = StockInProductDao.Update_StockInProduct(stockInProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
- c.ServeJSON()
- return
- }
- }
- }
- if len(T_remark) > 0 {
- stockIn.T_remark = T_remark
- }
- if len(T_date) > 0 {
- stockIn.T_date = T_date
- }
- err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_date")
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
- c.ServeJSON()
- return
- }
- o.Commit()
- if len(T_date) > 0 && T_old_date != T_date {
- // 修改出库产品日期
- StockInProductDao.Update_StockInProduct_T_date(stockIn.T_number, T_date)
- DeviceDao.Update_Device_CreateTimeByT_in_number(stockIn.T_number, T_date)
- }
- // 更新月份统计表
- allProductList := []int{}
- for i, _ := range allProductListMap {
- allProductList = append(allProductList, i)
- }
- StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
- NatsServer.AddUserLogs(c.User.T_uuid, "入库", "修改", stockIn)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- // 删除入库
- func (c *StockController) StockIn_Del() {
- T_number := c.GetString("T_number") // 入库单号
- o := orm.NewOrm()
- o.Begin()
- StockInDao := Stock.NewStockIn(o)
- StockInProductDao := Stock.NewStockInProduct(o)
- StockDao := Stock.NewStock(o)
- DeviceDao := Stock.NewDevice(o)
- // 查询入库信息
- stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- // 查询入库产品信息
- productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
- var oldProductList []dto.StockProduct
- allProductList := []int{}
- for _, product := range productOldList {
- oldProductList = append(oldProductList, dto.StockProduct{
- T_product_id: product.T_product_id,
- T_num: product.T_num,
- T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
- })
- allProductList = append(allProductList, product.T_product_id)
- }
- // 删除入库产品列表
- for _, v := range oldProductList {
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- // 删除入库产品列表
- err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
- c.ServeJSON()
- return
- }
- if product.T_relation_sn == 1 {
- // 删除该入库单关联sn
- for _, sn := range v.T_relation_sn {
- err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
- c.ServeJSON()
- return
- }
- }
- }
- // 减少库存
- _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
- c.ServeJSON()
- return
- }
- }
- err = StockInDao.Delete_StockIn(stockIn)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
- c.ServeJSON()
- return
- }
- o.Commit()
- // 更新月份统计表
- StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
- NatsServer.AddUserLogs(c.User.T_uuid, "入库", "删除", stockIn)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- // 导出出库单
- func (c *StockController) StockIn_Excel() {
- // 查询
- T_number := c.GetString("T_number")
- o := orm.NewOrm()
- StockInDao := Stock.NewStockIn(o)
- StockInProductDao := Stock.NewStockInProduct(o)
- stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
- var pList []Stock.StockInProduct_R
- for _, v := range productList {
- pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
- }
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
- T_date, _ := lib.DateStrToTime(stockIn.T_date)
- filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
- f := excelize.NewFile() // 设置单元格的值
- Style1, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- })
- Style2, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
- })
- Style3, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- Style4, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- productList2 := lib.ChunkBy(productList, 10)
- for i, products := range productList2 {
- if len(products) < 10 {
- products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
- }
- row := i*17 + 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
- f.SetRowStyle("Sheet1", row, row, Style1)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
- f.SetRowHeight("Sheet1", row, 20)
- row += 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
- f.SetRowStyle("Sheet1", row, row, Style1)
- title := "硬件入库单"
- if len(productList2) > 1 {
- title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
- }
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
- f.SetRowHeight("Sheet1", row, 20)
- row += 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
- f.SetRowStyle("Sheet1", row, row, Style2)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
- f.SetRowHeight("Sheet1", row, 20)
- row += 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
- f.SetRowStyle("Sheet1", row, row, Style2)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
- f.SetRowHeight("Sheet1", row, 20)
- row += 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
- f.SetRowStyle("Sheet1", row, row, Style2)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
- f.SetRowHeight("Sheet1", row, 20)
- // 这里设置表头
- row += 1
- f.SetRowHeight("Sheet1", row, 20)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
- // 设置列宽
- f.SetColWidth("Sheet1", "A", "A", 5)
- f.SetColWidth("Sheet1", "B", "B", 20)
- f.SetColWidth("Sheet1", "C", "C", 12)
- f.SetColWidth("Sheet1", "D", "D", 10)
- f.SetColWidth("Sheet1", "E", "E", 10)
- f.SetColWidth("Sheet1", "F", "F", 15)
- f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
- sRow := row
- // 循环写入数据
- for j, v := range products {
- row += 1
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
- if v.T_num > 0 {
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
- }
- }
- f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
- row += 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
- f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
- f.SetRowStyle("Sheet1", row, row, Style2)
- f.SetRowHeight("Sheet1", row, 20)
- }
- // 保存文件
- if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
- fmt.Println(err)
- }
- var url string
- //// 上传 OSS
- nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
- url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err = os.Remove("ofile/" + filename + ".xlsx")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockOut_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_depot_id, _ := c.GetInt("T_depot_id")
- T_contract_number := c.GetString("T_contract_number")
- T_start_date := c.GetString("T_start_date")
- T_end_date := c.GetString("T_end_date")
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Basic.Read_Depot_All_Map()
- StockOutDao := Stock.NewStockOut(orm.NewOrm())
- R_List, R_cnt := StockOutDao.Read_StockOut_List(T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockOut_List_Product() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_depot_id, _ := c.GetInt("T_depot_id")
- T_name := c.GetString("T_name")
- T_start_date := c.GetString("T_start_date")
- T_end_date := c.GetString("T_end_date")
- T_product_name := c.GetString("T_product_name") // 产品名称
- T_product_model := c.GetString("T_product_model") // 产品型号
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Basic.Read_Depot_All_Map()
- StockOutDao := Stock.NewStockOut(orm.NewOrm())
- 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)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockOut_List_Product_Excel() {
- // 查询
- T_depot_id, _ := c.GetInt("T_depot_id")
- T_name := c.GetString("T_name")
- T_start_date := c.GetString("T_start_date")
- T_end_date := c.GetString("T_end_date")
- T_product_name := c.GetString("T_product_name") // 产品名称
- T_product_model := c.GetString("T_product_model") // 产品型号
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Basic.Read_Depot_All_Map()
- StockOutDao := Stock.NewStockOut(orm.NewOrm())
- 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)
- filename := fmt.Sprintf("出库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
- f := excelize.NewFile() // 设置单元格的值
- Style1, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- })
- Style2, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- Style3, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- f.MergeCell("Sheet1", "A1", "L1")
- f.SetRowStyle("Sheet1", 1, 1, Style1)
- f.SetCellValue("Sheet1", "A1", fmt.Sprintf("出库明细单"))
- f.SetRowHeight("Sheet1", 1, 30)
- // 这里设置表头
- f.SetCellStyle("Sheet1", "A2", "L2", Style2)
- f.SetRowHeight("Sheet1", 2, 20)
- f.SetCellValue("Sheet1", "A2", "序号")
- f.SetCellValue("Sheet1", "B2", "出库单号")
- f.SetCellValue("Sheet1", "C2", "领取人")
- f.SetCellValue("Sheet1", "D2", "出库日期")
- f.SetCellValue("Sheet1", "E2", "出库仓库")
- f.SetCellValue("Sheet1", "F2", "关联项目")
- f.SetCellValue("Sheet1", "G2", "产品名称")
- f.SetCellValue("Sheet1", "H2", "产品型号")
- f.SetCellValue("Sheet1", "I2", "数量")
- f.SetCellValue("Sheet1", "J2", "是否关联SN")
- f.SetCellValue("Sheet1", "K2", "备注")
- f.SetCellValue("Sheet1", "L2", "SN")
- // 设置列宽
- f.SetColWidth("Sheet1", "A", "A", 10)
- f.SetColWidth("Sheet1", "B", "B", 15)
- f.SetColWidth("Sheet1", "C", "D", 10)
- f.SetColWidth("Sheet1", "D", "D", 10)
- f.SetColWidth("Sheet1", "E", "E", 10)
- f.SetColWidth("Sheet1", "F", "F", 20)
- f.SetColWidth("Sheet1", "G", "G", 20)
- f.SetColWidth("Sheet1", "H", "H", 15)
- f.SetColWidth("Sheet1", "I", "I", 10)
- f.SetColWidth("Sheet1", "J", "J", 12)
- f.SetColWidth("Sheet1", "K", "K", 30)
- f.SetColWidth("Sheet1", "L", "L", 100)
- line := 2
- for _, product := range R_List {
- line += 1
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_receive_name)
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), product.T_project)
- f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
- f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
- f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
- var relation_sn string
- if product.T_product_relation_sn == 1 {
- relation_sn = "是"
- } else {
- relation_sn = "否"
- }
- f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
- f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
- f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
- }
- f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
- // 保存文件
- if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
- fmt.Println(err)
- }
- var url string
- //// 上传 OSS
- nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
- url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err := os.Remove("ofile/" + filename + ".xlsx")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockOut_Get() {
- // 查询
- T_number := c.GetString("T_number")
- o := orm.NewOrm()
- StockOutDao := Stock.NewStockOut(o)
- StockOutProductDao := Stock.NewStockOutProduct(o)
- stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
- var pList []Stock.StockOutProduct_R
- for _, v := range productList {
- pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
- }
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- Basic.Read_Depot_All_Map()
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockOutToStockOut_Detail(stockOut, pList)}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockOut_Add() {
- rand_x := 0
- T_number := ""
- o := orm.NewOrm()
- o.Begin()
- StockOutDao := Stock.NewStockOut(o)
- for true {
- T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
- _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
- if err != nil && err.Error() == orm.ErrNoRows.Error() {
- break
- }
- rand_x += 1
- }
- T_contract_number := c.GetString("T_contract_number")
- T_depot_id, _ := c.GetInt("T_depot_id")
- T_type, _ := c.GetInt("T_type") // 出库类型 1-领料出库 2-销售出库
- T_date := c.GetString("T_date")
- T_receive := c.GetString("T_receive")
- T_product := c.GetString("T_product")
- T_remark := c.GetString("T_remark")
- T_project := c.GetString("T_project")
- T_delivery_type, _ := c.GetInt("T_delivery_type")
- T_signer_unit := c.GetString("T_signer_unit")
- T_signer := c.GetString("T_signer")
- T_signer_phone := c.GetString("T_signer_phone")
- T_signer_date := c.GetString("T_signer_date")
- T_courier_number := c.GetString("T_courier_number")
- var_ := Stock.StockOut{
- T_number: T_number,
- T_contract_number: T_contract_number,
- T_depot_id: T_depot_id,
- T_type: T_type,
- T_date: T_date,
- T_receive: T_receive,
- T_remark: T_remark,
- T_project: T_project,
- T_submit: c.User.T_uuid,
- // 销售出库
- T_delivery_type: T_delivery_type, // 1-自送 2-自提 3-快递
- T_signer_unit: T_signer_unit,
- T_signer: T_signer,
- T_signer_phone: T_signer_phone,
- T_signer_date: T_signer_date,
- T_courier_number: T_courier_number,
- }
- StockOutProductDao := Stock.NewStockOutProduct(o)
- StockDao := Stock.NewStock(o)
- DeviceDao := Stock.NewDevice(o)
- ContractDao := Contract.NewContract(o)
- ContractProductDao := Contract.NewContractProduct(o)
- _, err := StockOutDao.Add_StockOut(var_)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
- c.ServeJSON()
- return
- }
- // 1、添加出库单
- contract, _ := ContractDao.Read_Contract_ByT_number(T_contract_number)
- if contract.Id == 0 && T_type == 2 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "合同编号错误!"}
- c.ServeJSON()
- return
- }
- if len(T_product) == 0 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
- c.ServeJSON()
- return
- }
- productList := lib.SplitString(T_product, "|")
- allProductList := []int{}
- for _, v := range productList {
- product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
- allProductList = append(allProductList, product_id)
- product, _ := Basic.Read_Product_ById(product_id)
- num, _ := strconv.Atoi(strings.Split(v, "-")[1])
- T_relation_sn := strings.Split(v, "-")[2]
- if T_relation_sn == "" && product.T_relation_sn == 1 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
- c.ServeJSON()
- return
- }
- // 2、更新设备状态为已出库
- if len(T_relation_sn) > 0 {
- snList := strings.Split(T_relation_sn, ",")
- for _, sn := range snList {
- mqtt := Stock.Read_MqttUser(sn)
- device := Stock.Device{
- T_contract_number: T_contract_number,
- T_product_id: product_id,
- T_out_number: T_number,
- T_sn: sn,
- T_iccid: mqtt.Iccid,
- T_imei: mqtt.Imei,
- T_State: 1,
- T_project: T_project,
- }
- _, err = DeviceDao.AddOrUpdate_Device(device, 1)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
- c.ServeJSON()
- return
- }
- }
- }
- // 3、添加出库产品清单
- stockOutProduct := Stock.StockOutProduct{
- T_number: T_number,
- T_product_id: product_id,
- T_depot_id: T_depot_id,
- T_num: num, // 出库数量
- T_date: T_date, // 出库数量
- T_relation_sn: T_relation_sn,
- }
- _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
- c.ServeJSON()
- return
- }
- // 4、更改合同产品列表清单
- contractProduct, _ := ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_contract_number, product_id)
- contractProduct.T_product_out += num
- if contractProduct.T_product_out > contractProduct.T_product_total && T_type == 2 {
- o.Rollback()
- p, _ := Basic.Read_Product_ById(product_id)
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】出库数量超过预计出库数量!", p.T_name)}
- c.ServeJSON()
- return
- }
- contractProduct.T_State = 2
- if contractProduct.T_product_out == 0 {
- contractProduct.T_State = 1
- }
- if contractProduct.T_product_out == contractProduct.T_product_total {
- contractProduct.T_State = 3
- }
- err = ContractProductDao.Update_ContractProduct(contractProduct, "T_product_out", "T_State")
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同产品清单失败"}
- c.ServeJSON()
- return
- }
- // 5、更新产品库存列表
- _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 1)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存失败"}
- c.ServeJSON()
- return
- }
- }
- o.Commit()
- o2 := orm.NewOrm()
- o2.Begin()
- ContractDao2 := Contract.NewContract(o2)
- ContractProductDao2 := Contract.NewContractProduct(o2)
- // 5、更新合同出库状态
- var T_out int
- if contract.T_State == 1 {
- T_out = 2
- // 查询合同产品清单是否全部都为已出库
- state := ContractProductDao2.Read_ContractProduct_T_State_List(contract.T_number)
- if state == 1 {
- T_out = 1
- }
- if state == 3 {
- T_out = 3
- }
- }
- if T_out != contract.T_out {
- contract.T_out = T_out
- err = ContractDao2.Update_Contract(contract, "T_out")
- if err != nil {
- o2.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
- c.ServeJSON()
- return
- }
- }
- o2.Commit()
- StockOut_Edit_StockMonth(T_date, T_depot_id, allProductList)
- NatsServer.AddUserLogs(c.User.T_uuid, "出库", "出库", var_)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockOut_Edit_Delivery() {
- T_number := c.GetString("T_number")
- T_remark := c.GetString("T_remark")
- T_project := c.GetString("T_project")
- T_delivery_type, _ := c.GetInt("T_delivery_type")
- T_signer_unit := c.GetString("T_signer_unit")
- T_signer := c.GetString("T_signer")
- T_signer_phone := c.GetString("T_signer_phone")
- T_signer_date := c.GetString("T_signer_date")
- T_courier_number := c.GetString("T_courier_number")
- o := orm.NewOrm()
- StockOutDao := Stock.NewStockOut(o)
- stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库编号错误!"}
- c.ServeJSON()
- return
- }
- if len(T_remark) > 0 {
- stockOut.T_remark = T_remark
- }
- if len(T_project) > 0 {
- stockOut.T_project = T_project
- }
- if T_delivery_type > 0 {
- stockOut.T_delivery_type = T_delivery_type
- }
- if len(T_signer_unit) > 0 {
- stockOut.T_signer_unit = T_signer_unit
- }
- if len(T_signer) > 0 {
- stockOut.T_signer = T_signer
- }
- if len(T_signer_phone) > 0 {
- stockOut.T_signer_phone = T_signer_phone
- }
- if len(T_signer_date) > 0 {
- stockOut.T_signer_date = T_signer_date
- }
- if len(T_courier_number) > 0 {
- stockOut.T_courier_number = T_courier_number
- }
- 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")
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
- c.ServeJSON()
- return
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改发货单", stockOut)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- func (c *StockController) StockOut_Edit() {
- T_number := c.GetString("T_number") // 出库单号
- //T_depot_id, _ := c.GetInt("T_depot_id")
- T_date := c.GetString("T_date")
- T_receive := c.GetString("T_receive")
- T_project := c.GetString("T_project")
- T_product := c.GetString("T_product")
- T_remark := c.GetString("T_remark")
- date, is := lib.DateStrToTime(T_date)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
- c.ServeJSON()
- return
- }
- if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
- T_date = ""
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
- o := orm.NewOrm()
- o.Begin()
- StockOutDao := Stock.NewStockOut(o)
- StockOutProductDao := Stock.NewStockOutProduct(o)
- StockDao := Stock.NewStock(o)
- DeviceDao := Stock.NewDevice(o)
- IotCardDao := Property.NewIotCard(&o)
- // 查询入库信息
- StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
- T_old_date := StockOut.T_date
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- // 查询入库产品信息
- productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
- var oldProductList []dto.StockProduct
- var newProductList []dto.StockProduct
- productOldMap := map[int]dto.StockProduct{}
- allProductListMap := make(map[int]struct{})
- for _, product := range productOldList {
- stockProduct := dto.StockProduct{
- T_product_id: product.T_product_id,
- T_num: product.T_num,
- T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
- }
- oldProductList = append(oldProductList, stockProduct)
- productOldMap[product.T_product_id] = stockProduct
- if _, ok := allProductListMap[product.T_product_id]; !ok {
- allProductListMap[product.T_product_id] = struct{}{}
- }
- }
- productNewList := lib.SplitString(T_product, "|")
- productNewMap := map[int]dto.StockProduct{}
- for _, v := range productNewList {
- product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
- num, _ := strconv.Atoi(strings.Split(v, "-")[1])
- T_relation_sn := strings.Split(v, "-")[2]
- stockProduct := dto.StockProduct{
- T_product_id: product_id,
- T_num: num,
- T_relation_sn: lib.SplitString(T_relation_sn, ","),
- }
- newProductList = append(newProductList, stockProduct)
- productNewMap[product_id] = stockProduct
- if _, ok := allProductListMap[product_id]; !ok {
- allProductListMap[product_id] = struct{}{}
- }
- }
- // 判断产品列表信息是否相同
- StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
- // 两次提交的数据相同,则不用修改
- if !StockProductListIsEqual {
- if len(needDelete) > 0 {
- // 删除入库产品列表
- for _, v := range needDelete {
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- // 删除入库产品列表
- err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
- c.ServeJSON()
- return
- }
- if product.T_relation_sn == 1 {
- // 删除该入库单关联sn
- for _, sn := range v.T_relation_sn {
- err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
- c.ServeJSON()
- return
- }
- }
- }
- // 增加库存
- _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
- c.ServeJSON()
- return
- }
- }
- }
- if len(needAdd) > 0 {
- // 新增入库产品列表
- for _, v := range needAdd {
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
- c.ServeJSON()
- return
- }
- // 添加该入库单关联sn
- for _, sn := range v.T_relation_sn {
- mqtt := Stock.Read_MqttUser(sn)
- // 添加设备
- device := Stock.Device{
- T_contract_number: "",
- T_out_number: StockOut.T_number,
- T_product_id: v.T_product_id,
- T_in_number: "",
- T_sn: sn,
- T_iccid: mqtt.Iccid,
- T_imei: mqtt.Imei,
- T_State: 1,
- CreateTime: StockOut.CreateTime,
- UpdateTime: StockOut.UpdateTime,
- }
- _, err = DeviceDao.AddOrUpdate_Device(device, 1)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
- c.ServeJSON()
- return
- }
- // 添加物联网卡
- iotCard := Property.IotCard{
- T_iccid: mqtt.Iccid,
- T_sn: sn,
- T_State: 2, //1-未使用 2-已使用 3-已作废
- T_type: "4G",
- }
- _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- }
- StockOutProduct := Stock.StockOutProduct{
- T_number: T_number,
- T_product_id: v.T_product_id,
- T_depot_id: StockOut.T_depot_id,
- T_num: v.T_num, // 入库数量
- T_date: StockOut.T_date, // 入库日期
- T_relation_sn: strings.Join(v.T_relation_sn, ","),
- }
- _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- // 减少库存
- _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
- c.ServeJSON()
- return
- }
- }
- }
- if len(needEdit) > 0 {
- for _, v := range needEdit {
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- StockOutProduct := Stock.StockOutProduct{
- T_number: StockOut.T_number,
- T_product_id: v.T_product_id,
- T_depot_id: StockOut.T_depot_id,
- T_num: productNewMap[v.T_product_id].T_num, // 入库数量
- T_date: StockOut.T_date, // 入库日期
- }
- // 出库数量比之前多,减少库存
- var T_type int
- var T_num int
- if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
- // 减少库存
- T_type = 1
- T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
- } else {
- // 增加库存
- T_type = 2
- T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
- }
- _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- // 更新产品库存表
- err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
- c.ServeJSON()
- return
- }
- }
- }
- for _, diff := range snDiff {
- product, _ := Basic.Read_Product_ById(diff.T_product_id)
- // 删除减少的sn
- if len(diff.T_delete_relation_sn) > 0 {
- for _, sn := range diff.T_delete_relation_sn {
- err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- }
- // 增加库存
- _, 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)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- }
- if len(diff.T_add_relation_sn) > 0 {
- for _, sn := range diff.T_add_relation_sn {
- mqtt := Stock.Read_MqttUser(sn)
- // 添加设备
- device := Stock.Device{
- T_contract_number: "",
- T_out_number: StockOut.T_number,
- T_product_id: diff.T_product_id,
- T_in_number: "",
- T_sn: sn,
- T_iccid: mqtt.Iccid,
- T_imei: mqtt.Imei,
- T_State: 1,
- CreateTime: StockOut.CreateTime,
- UpdateTime: StockOut.UpdateTime,
- }
- _, err = DeviceDao.AddOrUpdate_Device(device, 1)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
- c.ServeJSON()
- return
- }
- // 添加物联网卡
- iotCard := Property.IotCard{
- T_iccid: mqtt.Iccid,
- T_sn: sn,
- T_State: 2, //1-未使用 2-已使用 3-已作废
- T_type: "4G",
- }
- _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- }
- // 减少库存
- _, 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)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
- c.ServeJSON()
- return
- }
- }
- StockOutProduct := Stock.StockOutProduct{
- T_number: StockOut.T_number,
- T_product_id: diff.T_product_id,
- T_depot_id: StockOut.T_depot_id,
- T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
- T_date: StockOut.T_date, // 入库日期
- T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
- }
- // 更新产品库存表
- err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
- c.ServeJSON()
- return
- }
- }
- }
- if len(T_remark) > 0 {
- StockOut.T_remark = T_remark
- }
- if len(T_receive) > 0 {
- StockOut.T_receive = T_receive
- }
- if len(T_project) > 0 {
- StockOut.T_project = T_project
- }
- if len(T_date) > 0 {
- StockOut.T_date = T_date
- }
- err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_date")
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
- c.ServeJSON()
- return
- }
- o.Commit()
- if len(T_date) > 0 && T_old_date != T_date {
- // 修改出库产品日期
- StockOutProductDao.Update_StockOutProduct_T_date(StockOut.T_number, T_date)
- DeviceDao.Update_Device_CreateTimeByT_out_number(StockOut.T_number, T_date)
- }
- if len(T_project) > 0 {
- DeviceDao.Update_Device_ByT_out_number_T_project(StockOut.T_number, T_project)
- }
- // 更新月份统计表
- allProductList := []int{}
- for i, _ := range allProductListMap {
- allProductList = append(allProductList, i)
- }
- StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
- NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改", StockOut)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- // 删除入库
- func (c *StockController) StockOut_Del() {
- T_number := c.GetString("T_number") // 入库单号
- o := orm.NewOrm()
- o.Begin()
- StockOutDao := Stock.NewStockOut(o)
- StockOutProductDao := Stock.NewStockOutProduct(o)
- StockDao := Stock.NewStock(o)
- DeviceDao := Stock.NewDevice(o)
- // 查询入库信息
- stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- // 查询出库产品信息
- productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
- var oldProductList []dto.StockProduct
- allProductList := []int{}
- for _, product := range productOldList {
- oldProductList = append(oldProductList, dto.StockProduct{
- T_product_id: product.T_product_id,
- T_num: product.T_num,
- T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
- })
- allProductList = append(allProductList, product.T_product_id)
- }
- // 删除入库产品列表
- for _, v := range oldProductList {
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- // 删除入库产品列表
- err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
- c.ServeJSON()
- return
- }
- if product.T_relation_sn == 1 {
- // 删除该入库单关联sn
- for _, sn := range v.T_relation_sn {
- err = DeviceDao.Delete_Device_ByT_out_number(stockOut.T_number, sn)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库设备失败"}
- c.ServeJSON()
- return
- }
- }
- }
- // 增加库存
- _, err = StockDao.AddOrUpdate_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
- c.ServeJSON()
- return
- }
- }
- err = StockOutDao.Delete_StockOut(stockOut)
- if err != nil {
- o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
- c.ServeJSON()
- return
- }
- o.Commit()
- // 更新月份统计表
- StockOut_Edit_StockMonth(stockOut.T_date, stockOut.T_depot_id, allProductList)
- NatsServer.AddUserLogs(c.User.T_uuid, "出库", "删除", stockOut)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
- c.ServeJSON()
- return
- }
- // 导出出库单
- func (c *StockController) StockOut_Excel() {
- // 查询
- T_number := c.GetString("T_number")
- o := orm.NewOrm()
- StockOutDao := Stock.NewStockOut(o)
- StockOutProductDao := Stock.NewStockOutProduct(o)
- stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
- //productList = append(productList, productList...)
- //productList = append(productList, productList...)
- //productList = append(productList, productList...)
- var pList []Stock.StockOutProduct_R
- for _, v := range productList {
- pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
- }
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
- T_date, _ := lib.DateStrToTime(stockOut.T_date)
- filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
- f := excelize.NewFile() // 设置单元格的值
- Style1, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- })
- Style2, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
- })
- Style3, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- Style4, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- productList2 := lib.ChunkBy(productList, 10)
- for i, products := range productList2 {
- if len(products) < 10 {
- products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
- }
- row := i*17 + 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
- f.SetRowStyle("Sheet1", row, row, Style1)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
- f.SetRowHeight("Sheet1", row, 20)
- row += 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
- f.SetRowStyle("Sheet1", row, row, Style1)
- title := "硬件出库单"
- if len(productList2) > 1 {
- title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
- }
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
- f.SetRowHeight("Sheet1", row, 20)
- row += 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
- f.SetRowStyle("Sheet1", row, row, Style2)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
- f.SetRowHeight("Sheet1", row, 20)
- row += 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
- f.SetRowStyle("Sheet1", row, row, Style2)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
- f.SetRowHeight("Sheet1", row, 20)
- row += 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
- f.SetRowStyle("Sheet1", row, row, Style2)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
- f.SetRowHeight("Sheet1", row, 20)
- // 这里设置表头
- row += 1
- f.SetRowHeight("Sheet1", row, 20)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
- // 设置列宽
- f.SetColWidth("Sheet1", "A", "A", 5)
- f.SetColWidth("Sheet1", "B", "B", 20)
- f.SetColWidth("Sheet1", "C", "C", 12)
- f.SetColWidth("Sheet1", "D", "D", 10)
- f.SetColWidth("Sheet1", "E", "E", 10)
- f.SetColWidth("Sheet1", "F", "F", 15)
- f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
- sRow := row
- // 循环写入数据
- for j, v := range products {
- row += 1
- product, _ := Basic.Read_Product_ById(v.T_product_id)
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
- if v.T_num > 0 {
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
- }
- }
- f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
- row += 1
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
- f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
- f.SetRowStyle("Sheet1", row, row, Style2)
- f.SetRowHeight("Sheet1", row, 20)
- }
- // 保存文件
- if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
- fmt.Println(err)
- }
- var url string
- //// 上传 OSS
- nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
- url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err = os.Remove("ofile/" + filename + ".xlsx")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- func StockOut_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
- date, _ := lib.DateStrToTime(T_date)
- //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
- // // 修改本月数据不同步库存
- // return nil
- //}
- //T_month := date.Format("2006-01")
- months := generateMonthList(date)
- o := orm.NewOrm()
- StockOutDao := Stock.NewStockOut(o)
- StockMonthDao := Stock.NewStockMonth(o)
- StockOutProductDao := Stock.NewStockOutProduct(o)
- StockInProductDao := Stock.NewStockInProduct(o)
- for _, T_product_id := range allProductList {
- for _, T_month := range months {
- // 获取当前产品本月出入库数量
- lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
- stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
- if stockMonth.Id > 0 {
- // 修改
- stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
- if len(lastMonth) > 0 {
- stockMonth.T_beginning = lastMonth[0].T_ending
- } else {
- stockMonth.T_beginning = 0
- }
- // 期末库存 = 期初库存+入库-出库
- stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
- err := StockMonthDao.Update_StockMonth(stockMonth, "T_out", "T_beginning", "T_ending")
- if err != nil {
- return err
- }
- } else {
- product, _ := Basic.Read_Product_ById(T_product_id)
- // 添加库存记录
- stockMonth = Stock.StockMonth{
- T_depot_id: T_depot_id,
- T_product_id: T_product_id,
- T_product_class: product.T_class,
- T_month: T_month,
- }
- // 获取当前产品本月出入库数量
- stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
- stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
- if len(lastMonth) > 0 {
- stockMonth.T_beginning = lastMonth[0].T_ending
- } else {
- stockMonth.T_beginning = 0
- }
- // 期末库存 = 期初库存+入库-出库
- stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
- stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
- _, err := StockMonthDao.Add_StockMonth(stockMonth)
- if err != nil {
- return err
- }
- }
- }
- }
- return nil
- }
- func Cron_StockMonth() {
- //创建一个定时任务对象
- c := cron.New(cron.WithSeconds())
- //给对象增加定时任务
- // @monthly 每月运行一次,每月第一天午夜 0 0 0 1 * *
- //c.AddFunc("0 */1 * * * ?", Cron_StockMonth_Add)
- c.AddFunc("@monthly", Cron_StockMonth_Add)
- //启动定时任务
- c.Start()
- defer c.Stop()
- //查询语句,阻塞,让main函数不退出,保持程序运行
- select {}
- }
- // 保存每月入库出库明细
- func Cron_StockMonth_Add() {
- T_month := time.Now().Format("2006-01")
- //T_month := time.Now().AddDate(0, -1, 0).Format("2006-01")
- logs.Info("开始统计" + T_month + "库存明细数据")
- o := orm.NewOrm()
- StockDao := Stock.NewStock(o)
- StockOutDao := Stock.NewStockOut(o)
- StockMonthDao := Stock.NewStockMonth(o)
- StockOutProductDao := Stock.NewStockOutProduct(o)
- StockInProductDao := Stock.NewStockInProduct(o)
- stockList, _ := StockDao.Read_Stock_List(0, 0, "", "", 0, 9999)
- for _, stock := range stockList {
- lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
- stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
- if stockMonth.Id > 0 {
- // 修改
- stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
- stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
- if len(lastMonth) > 0 {
- stockMonth.T_beginning = lastMonth[0].T_ending
- } else {
- stockMonth.T_beginning = 0
- }
- // 期末库存 = 期初库存+入库-出库
- stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
- err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_out", "T_beginning", "T_ending")
- if err != nil {
- NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
- }
- } else {
- stockMonth = Stock.StockMonth{
- T_depot_id: stock.T_depot_id,
- T_product_id: stock.T_product_id,
- T_product_class: stock.T_product_class,
- T_month: T_month,
- }
- // 获取当前产品本月出入库数量
- stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
- stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
- if len(lastMonth) > 0 {
- stockMonth.T_beginning = lastMonth[0].T_ending
- } else {
- stockMonth.T_beginning = 0
- }
- // 期末库存 = 期初库存+入库-出库
- stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
- stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(stock.T_depot_id, stock.T_product_id, T_month)
- _, err := StockMonthDao.Add_StockMonth(stockMonth)
- if err != nil {
- NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
- }
- }
- }
- }
- // 重写设备出入库记录
- func Cron_Device_Add() {
- logs.Info("开始统计写入设备出入库数据")
- o := orm.NewOrm()
- o.Begin()
- DeviceDao := Stock.NewDevice(o)
- StockOutDao := Stock.NewStockOut(o)
- StockInDao := Stock.NewStockIn(o)
- StockOutProductDao := Stock.NewStockOutProduct(o)
- StockInProductDao := Stock.NewStockInProduct(o)
- // 添加入库设备信息
- StockInList, _ := StockInDao.Read_StockIn_List(0, "", "", 0, 9999)
- for _, stockIn := range StockInList {
- productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
- for _, product := range productList {
- if len(product.T_relation_sn) == 0 {
- continue
- }
- for _, sn := range lib.SplitString(product.T_relation_sn, ",") {
- mqtt := Stock.Read_MqttUser(sn)
- // 添加设备
- device := Stock.Device{
- T_contract_number: "",
- T_out_number: "",
- T_product_id: product.T_product_id,
- T_in_number: stockIn.T_number,
- T_sn: sn,
- T_iccid: mqtt.Iccid,
- T_imei: mqtt.Imei,
- T_State: 2,
- CreateTime: product.CreateTime,
- UpdateTime: product.UpdateTime,
- }
- _, err := DeviceDao.AddOrUpdate_Device(device, 2)
- if err != nil {
- o.Rollback()
- return
- }
- }
- }
- }
- // 添加出库设备信息
- StockOutList, _ := StockOutDao.Read_StockOut_List(0, "", "", "", 0, 9999)
- for _, stockOut := range StockOutList {
- productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
- for _, product := range productList {
- if len(product.T_relation_sn) == 0 {
- continue
- }
- for _, sn := range lib.SplitString(product.T_relation_sn, ",") {
- mqtt := Stock.Read_MqttUser(sn)
- // 添加设备
- device := Stock.Device{
- T_contract_number: stockOut.T_contract_number,
- T_product_id: product.T_product_id,
- T_out_number: stockOut.T_number,
- T_sn: sn,
- T_iccid: mqtt.Iccid,
- T_imei: mqtt.Imei,
- T_State: 1,
- T_project: stockOut.T_project,
- CreateTime: product.CreateTime,
- UpdateTime: product.UpdateTime,
- }
- _, err := DeviceDao.AddOrUpdate_Device(device, 1)
- if err != nil {
- o.Rollback()
- return
- }
- }
- }
- }
- o.Commit()
- }
- func generateMonthList(startMonth time.Time) []string {
- var months []string
- endMonth := time.Now().AddDate(0, 0, 0)
- for month := startMonth; month.Before(endMonth); month = month.AddDate(0, 1, 0) {
- fmt.Println("=========================", month.Format("2006-01"))
- months = append(months, month.Format("2006-01"))
- }
- return months
- }
|