123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559 |
- 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")
- DeviceDao := Stock.NewDevice(orm.NewOrm())
- R_List, R_cnt := DeviceDao.Read_Device_List(T_name, 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")
- 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, 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_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")
- 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, 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_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
- }
|