package controllers import ( "Cold_GoodsOrder/Nats/NatsServer" "Cold_GoodsOrder/conf" "Cold_GoodsOrder/lib" "Cold_GoodsOrder/models/Account" "Cold_GoodsOrder/models/Device" "Cold_GoodsOrder/models/Function" "fmt" beego "github.com/beego/beego/v2/server/web" "github.com/signintech/gopdf" "math" "os" "strconv" "strings" "time" ) // Handle type GoodsOrderController struct { beego.Controller User_r Account.User // 登陆的用户 T_pid int // 公司id } func (c *GoodsOrderController) Prepare() { c.User_r = *Account.User_r c.T_pid = *Account.T_pid } func (c *GoodsOrderController) GoodsOrder_List() { type R_JSONS struct { //必须的大写开头 Data []Function.GoodsOrderR Num int64 Page int Page_size int } var r_jsons R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } Name := c.GetString("T_name") c.Data["Name"] = Name r_jsons.Data, r_jsons.Num = Function.Read_GoodsOrder_List(c.T_pid, page, page_z, Name) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *GoodsOrderController) GoodsOrder_Get() { id, _ := c.GetInt("T_id") r := Function.Read_GoodsOrder_ById(id) // 记录不存在 if r.Id == 0 { c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"} c.ServeJSON() return } if c.T_pid != r.T_pid { c.Data["json"] = lib.JSONS{Code: 205, Msg: "没有权限!"} c.ServeJSON() return } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Function.GoodsOrderToGoodsOrderR(r)} c.ServeJSON() return } func (c *GoodsOrderController) GoodsOrder_Add() { T_orderid := c.GetString("T_orderid") T_outorderid := c.GetString("T_outorderid") T_sn := c.GetString("T_sn") T_receiving := c.GetString("T_receiving") T_time := c.GetString("T_time") T_text := c.GetString("T_text") T_start_Ut_T, _ := c.GetFloat("T_start_Ut_T") T_end_Ut_T, _ := c.GetFloat("T_end_Ut_T") T_time_s := strings.Split(T_time, "|") if len(T_time_s) != 2 { c.Data["json"] = lib.JSONS{Code: 207, Msg: "时间参数异常!"} c.ServeJSON() return } T_start_Ut_, err := time.ParseInLocation("2006-01-02 15:04:05", T_time_s[0], time.Local) T_end_Ut_, err := time.ParseInLocation("2006-01-02 15:04:05", T_time_s[1], time.Local) if err != nil { c.Data["json"] = lib.JSONS{Code: 207, Msg: "时间参数异常!"} c.ServeJSON() return } var_ := Function.GoodsOrder{ T_pid: c.T_pid, T_orderid: T_orderid, T_outorderid: T_outorderid, T_sn: T_sn, T_receiving: T_receiving, T_start_Ut: T_start_Ut_, T_end_Ut: T_end_Ut_, T_text: T_text, T_State: 1, T_start_Ut_T: float32(T_start_Ut_T), T_end_Ut_T: float32(T_end_Ut_T), } id, err := Function.Add_GoodsOrder(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"} c.ServeJSON() return } NatsServer.AddUserLogs(Account.User_r.T_uuid, "订单系统", "添加", var_) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id} c.ServeJSON() return } func (c *GoodsOrderController) GoodsOrder_Edit() { id, _ := c.GetInt("T_id") r := Function.Read_GoodsOrder_ById(id) if r.Id == 0 { c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id !"} c.ServeJSON() return } if c.T_pid != r.T_pid { c.Data["json"] = lib.JSONS{Code: 205, Msg: "没有权限!"} c.ServeJSON() return } T_orderid := c.GetString("T_orderid") T_outorderid := c.GetString("T_outorderid") T_sn := c.GetString("T_sn") T_receiving := c.GetString("T_receiving") T_time := c.GetString("T_time") T_text := c.GetString("T_text") T_start_Ut_T, T_start_Ut_T_Err := c.GetFloat("T_start_Ut_T") T_end_Ut_T, T_end_Ut_T_Err := c.GetFloat("T_end_Ut_T") T_time_s := strings.Split(T_time, "|") if len(T_time_s) != 2 { c.Data["json"] = lib.JSONS{Code: 207, Msg: "时间参数异常!"} c.ServeJSON() return } println("T_time_s:", T_time_s) T_start_Ut_, T_start_Ut_Err := time.ParseInLocation("2006-01-02 15:04:05", T_time_s[0], time.Local) T_end_Ut_, T_end_Ut_Err := time.ParseInLocation("2006-01-02 15:04:05", T_time_s[1], time.Local) if len(T_orderid) > 0 { r.T_orderid = T_orderid } if len(T_outorderid) > 0 { r.T_outorderid = T_outorderid } if len(T_sn) > 0 { r.T_sn = T_sn } if len(T_receiving) > 0 { r.T_receiving = T_receiving } if len(T_text) > 0 { r.T_text = T_text } if T_start_Ut_T_Err == nil { r.T_start_Ut_T = float32(T_start_Ut_T) } if T_end_Ut_T_Err == nil { r.T_end_Ut_T = float32(T_end_Ut_T) } if T_start_Ut_Err == nil { r.T_start_Ut = T_start_Ut_ } if T_end_Ut_Err == nil { r.T_end_Ut = T_end_Ut_ } is := Function.Update_GoodsOrder(r, "T_orderid", "T_outorderid", "T_sn", "T_receiving", "T_start_Ut", "T_end_Ut", "T_start_Ut_T", "T_end_Ut_T", "T_text") if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"} c.ServeJSON() return } NatsServer.AddUserLogs(Account.User_r.T_uuid, "订单系统", "修改", r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } func (c *GoodsOrderController) GoodsOrder_Del() { Id, _ := c.GetInt("T_id") r := Function.Read_GoodsOrder_ById(Id) if r.Id == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } if c.T_pid != r.T_pid { c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"} c.ServeJSON() return } if is := Function.Delete_GoodsOrder(r); !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } NatsServer.AddUserLogs(Account.User_r.T_uuid, "订单系统", "删除", r) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"} c.ServeJSON() return } // 列表 - 接口 func (c *GoodsOrderController) GoodsOrder_PDF() { var err error Id, _ := c.GetInt("T_id") if Id == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"} c.ServeJSON() return } GoodsOrder_r := Function.Read_GoodsOrder_ById(Id) if GoodsOrder_r.Id == 0 { c.Data["json"] = lib.JSONS{Code: 207, Msg: "Id Err!"} c.ServeJSON() return } Device_r, err := NatsServer.ReadDeviceByT_sn(GoodsOrder_r.T_sn) if err != nil { c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_sn Err!"} c.ServeJSON() return } DeviceSensor_r := NatsServer.ReadDeviceSensorALLByT_sn(GoodsOrder_r.T_sn) if len(DeviceSensor_r) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: GoodsOrder_r.T_sn + "设备没有数据!"} c.ServeJSON() return } var DeviceSensor_data []Device.DeviceData_R DeviceSensor_data, _ = NatsServer.ReadDeviceDataListBy_T_snid(GoodsOrder_r.T_sn+","+strconv.Itoa(DeviceSensor_r[0].T_id), GoodsOrder_r.T_start_Ut.Format("2006-01-02 15:04:05"), GoodsOrder_r.T_end_Ut.Format("2006-01-02 15:04:05"), 0, 9999) if len(DeviceSensor_data) == 0 { c.Data["json"] = lib.JSONS{Code: 202, Msg: GoodsOrder_r.T_sn + "设备没有数据!"} c.ServeJSON() return } pdf := &gopdf.GoPdf{} pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4 //err = GetFont(pdf, "LiberationSerif-Regular.ttf") //if err != nil { // log.Fatalln(err) //} //err = pdf.SetFont("Ubuntu-L", "", 14) //if err != nil { // log.Fatalln(err) //} err = pdf.AddTTFFont("simsun", "static/fonts/三极行楷简体-粗.ttf") if err != nil { c.Data["json"] = lib.JSONS{Code: 204, Msg: err.Error()} c.ServeJSON() return } err = pdf.SetFont("simsun", "", 24) if err != nil { c.Data["json"] = lib.JSONS{Code: 205, Msg: err.Error()} c.ServeJSON() return } pdf.SetGrayFill(0.5) pdf.SetMargins(0, 20, 0, 20) pdf.AddPage() //use path //pdf.Image("logo.png", 100, 50, &gopdf.Rect{W: 50, H: 50}) company, _ := NatsServer.Read_Company_ById(c.T_pid) textw, _ := pdf.MeasureTextWidth(company.T_name) pdf.SetX((595 / 2) - (textw / 2)) pdf.SetY(40) pdf.Text(company.T_name) // 线 pdf.SetLineWidth(2) pdf.SetLineType("dashed") pdf.Line(10, 60, 585, 60) err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf") if err != nil { c.Data["json"] = lib.JSONS{Code: 206, Msg: err.Error()} c.ServeJSON() return } err = pdf.SetFont("wts", "", 10) if err != nil { c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()} c.ServeJSON() return } //fmt.Sprintf(" %.1f ", v.T_t) //lib.RectFillColor(pdf, "历史数据["+Time_start+" / "+Time_end+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) // ------------- x := 22.0 w1 := 80.0 w2 := 195.0 lib.RectFillColor(pdf, "订单号:", 12, x, 80, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, GoodsOrder_r.T_orderid, 12, x, 80, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 lib.RectFillColor(pdf, "出库订单号:", 12, x, 80, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, GoodsOrder_r.T_outorderid, 12, x, 80, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 // ------------- x = 22.0 w1 = 80.0 w2 = 195.0 lib.RectFillColor(pdf, "设备名称:", 12, x, 100, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, Device_r.T_devName, 12, x, 100, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 lib.RectFillColor(pdf, "设备编号:", 12, x, 100, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, Device_r.T_sn, 12, x, 100, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 // ------------- x = 22.0 w1 = 80.0 w2 = 240.0 T_time := fmt.Sprintf("%s", GoodsOrder_r.T_start_Ut.Format("2006-01-02 15:04:05")+" - "+GoodsOrder_r.T_end_Ut.Format("2006-01-02 15:04:05")) lib.RectFillColor(pdf, "送货时间:", 12, x, 120, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, T_time, 12, x, 120, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 T_time = fmt.Sprintf("%s", GoodsOrder_r.CreateTime.Format("2006-01-02 15:04:05")) lib.RectFillColor(pdf, "订单时间:", 12, x, 120, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, T_time, 12, x, 120, 150, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) // ------------- x = 22.0 w1 = 80.0 w2 = 195.0*2 + 80 T_text := fmt.Sprintf("%s", GoodsOrder_r.T_receiving) lib.RectFillColor(pdf, "收货单位:", 12, x, 140, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, T_text, 12, x, 140, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 // 数据汇总-------------------------------- // 最低温度 var T_t_l float32 = 100.0 // 最高温度 var T_t_u float32 = 0.0 var T_t_v float32 = 0.0 for n := 0; len(DeviceSensor_data) > n; n++ { // -计算温度 if DeviceSensor_data[n].T_t < T_t_l { T_t_l = DeviceSensor_data[n].T_t } if DeviceSensor_data[n].T_t > T_t_u { T_t_u = DeviceSensor_data[n].T_t } T_t_v += DeviceSensor_data[n].T_t } var y float64 = 160 x = 22.0 w1 = 60.0 w2 = 50.0 err = pdf.SetFont("wts", "", 10) if err != nil { c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()} c.ServeJSON() return } T_t := fmt.Sprintf(" %.1f℃ ", DeviceSensor_data[len(DeviceSensor_data)-1].T_t) lib.RectFillColor(pdf, "起送温度", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, T_t, 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 T_t = fmt.Sprintf(" %.1f℃ ", DeviceSensor_data[0].T_t) lib.RectFillColor(pdf, "送达温度", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, T_t, 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 T_t = fmt.Sprintf(" %.1f℃ ", T_t_l) lib.RectFillColor(pdf, "最低温度", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, T_t, 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 T_t = fmt.Sprintf(" %.1f℃ ", T_t_v/float32(len(DeviceSensor_data))) lib.RectFillColor(pdf, "平均温度", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, T_t, 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 T_t = fmt.Sprintf(" %.1f℃ ", T_t_u) lib.RectFillColor(pdf, "最高温度", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, T_t, 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 // ----------- textw, _ = pdf.MeasureTextWidth("历史数据") pdf.SetX(20) pdf.SetY(210) pdf.Text("历史数据:") // 数据展示-------------------------------- //var y float64 = 220 y = 220 x = 22.0 w1 = 94.0 w2 = 43.0 lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 lib.RectFillColor(pdf, "记录时间", 10, x, y, w1, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, "温度℃", 10, x, y, w2, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 // 数据展示-------------------------------- y = 240 err = pdf.SetFont("wts", "", 8) if err != nil { c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()} c.ServeJSON() return } nln := 0 x = 22.0 for n := 0; len(DeviceSensor_data) > n; n++ { var textH float64 = 25 // if text height is 25px. pdf.SetNewY(y, textH) y = pdf.GetY() nln++ if nln > 4 { nln = 1 x = 22.0 y += 18 } if y >= 794 { pdf.AddPage() x = 22.0 y = 20 } // ------------------ T_t = fmt.Sprintf(" %.1f ", DeviceSensor_data[n].T_t) T_time = fmt.Sprintf("%s", DeviceSensor_data[n].T_time) lib.RectFillColor(pdf, T_time, 10, x, y, w1, 18, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w1 lib.RectFillColor(pdf, T_t, 10, x, y, w2, 18, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle) x += w2 } err = pdf.SetFont("wts", "", 10) if err != nil { c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()} c.ServeJSON() return } //---------------- timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf" err = pdf.WritePdf(timeStr) if err != nil { c.Data["json"] = lib.JSONS{Code: 207, Msg: err.Error()} c.ServeJSON() return } //上传 OSS url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr) if !is { c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"} c.ServeJSON() return } //删除目录 err = os.Remove(timeStr) if err != nil { fmt.Println(err) } c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url} c.ServeJSON() return }