package controllers import ( "ERP_storage/Nats" "ERP_storage/Nats/NatsServer" "ERP_storage/conf" "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_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") } if len(T_end_date) == 0 { T_start_date = now.Format("2006-01") } StockMonthDao := Stock.NewStockMonth(orm.NewOrm()) R_List := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, 0, T_start_date, T_end_date) 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") } if len(T_end_date) == 0 { T_start_date = now.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()) 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", 10) //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) f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_project) } 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("J%d", line), Style4) } // 保存文件 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") if _, is := lib.DateStrToTime(T_date); !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, "|") for _, v := range productList { product_id, _ := strconv.Atoi(strings.Split(v, "-")[0]) 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!", T_relation_sn)} 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() 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) StockIn_Edit() { o := orm.NewOrm() o.Begin() StockInDao := Stock.NewStockIn(o) 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") if _, is := lib.DateStrToTime(T_date); !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, "|") for _, v := range productList { product_id, _ := strconv.Atoi(strings.Split(v, "-")[0]) 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!", T_relation_sn)} 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() 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_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) stockIn, 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(stockIn.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(stockIn, 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") if _, is := lib.DateStrToTime(T_date); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"} c.ServeJSON() return } 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, "|") for _, v := range productList { product_id, _ := strconv.Atoi(strings.Split(v, "-")[0]) 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() 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() { 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 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().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 { 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) s := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month) if len(s) > 0 { stockMonth.T_beginning = s[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) } } }