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 "gogs.baozhida.cn/zoie/ERP_libs/Nats" userlibs "gogs.baozhida.cn/zoie/ERP_libs/User" "gogs.baozhida.cn/zoie/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 }