package Stock import ( "ERP_storage/logs" "ERP_storage/models/Basic" "errors" "fmt" "git.baozhida.cn/ERP_libs/lib" _ "github.com/astaxie/beego/cache/redis" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "strconv" "time" ) type Device struct { Id int `orm:"column(ID);size(11);auto;pk"` T_contract_number string `orm:"size(256);null"` // 合同编号 T_product_id int `orm:"size(20);null"` // 产品id T_in_number string `orm:"size(256);null"` // 入库编号 T_out_number string `orm:"size(256);null"` // 出库编号 T_sn string `orm:"size(256);null"` // 设备sn T_iccid string `orm:"size(256);null"` // sim卡号 T_imei string `orm:"size(256);null"` // 模组imei T_State int `orm:"size(2);default(2)"` // 1-已出库 2-未出库/入库 T_remark string `orm:"type(text);null"` // 备注 T_project string `orm:"type(text);null"` // 出库项目 T_project_log string `orm:"type(text);null"` // 出库项目 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新 } func (t *Device) TableName() string { return "device" // 数据库名称 // ************** 替换 FormulaList ************** } type DeviceDaoImpl struct { orm orm.Ormer } func NewDevice(orm orm.Ormer) *DeviceDaoImpl { return &DeviceDaoImpl{orm: orm} } func init() { //注册模型 orm.RegisterModel(new(Device)) } type Device_R struct { Id int T_contract_number string // 合同编号 T_in_number string // 入库编号 T_out_number string // 出库编号 T_sn string // 设备sn T_iccid string // 物联网卡号 T_imei string // 模组imei T_State int // 1-已出库 2-未出库 T_device_state int // 1-已出库 2-未出库 T_remark string T_project string T_project_log string // ---------产品信息----------- T_product_id int T_product_name string T_product_class int T_product_class_name string T_product_model string T_product_spec string T_product_img string } func DeviceToDevice_R(t Device_R) (r Device_R) { r.Id = t.Id r.T_contract_number = t.T_contract_number r.T_product_id = t.T_product_id r.T_in_number = t.T_in_number r.T_out_number = t.T_out_number r.T_sn = t.T_sn r.T_iccid = t.T_iccid r.T_imei = t.T_imei r.T_State = t.T_device_state r.T_device_state = t.T_device_state r.T_remark = t.T_remark r.T_project = t.T_project r.T_project_log = t.T_project_log r.T_product_name = t.T_product_name r.T_product_class = t.T_product_class r.T_product_model = t.T_product_model r.T_product_spec = t.T_product_spec r.T_product_img = t.T_product_img r.T_product_class_name = Basic.Read_ProductClass_Get(t.T_product_class) return r } // 添加 func (dao *DeviceDaoImpl) Add_Device(r Device) (id int64, err error) { id, err = dao.orm.Insert(&r) if err != nil { logs.Error(lib.FuncName(), err) } return id, err } // T_type 1-出库 2-入库 func (dao *DeviceDaoImpl) AddOrUpdate_Device(r Device, T_type int) (id int64, err error) { id, err = dao.orm.Insert(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } func (dao *DeviceDaoImpl) AddOrUpdate_Device1(r Device, T_type int) (id int64, err error) { qs := dao.orm.QueryTable(new(Device)) var device Device err = qs.Filter("T_sn", r.T_sn).One(&device) if err != nil { if err.Error() == orm.ErrNoRows.Error() { r.T_remark = fmt.Sprintf("%s:%s(%s)|", time.Now().Format("2006-01-02"), "入库", r.T_in_number) if len(r.T_project) > 0 { r.T_project_log += r.T_project + "|" } id, err = dao.orm.Insert(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } else { logs.Error(lib.FuncName(), err) return } } T_remark := "" if T_type == 1 { T_remark = fmt.Sprintf("%s:%s(%s)|", time.Now().Format("2006-01-02"), "出库", r.T_out_number) } if T_type == 2 { T_remark = fmt.Sprintf("%s:%s(%s)|", time.Now().Format("2006-01-02"), "入库", device.T_in_number) } r.Id = device.Id // T_State 1-出库 2-入库 r.T_State = T_type // 出库时将入库编号服务给update结构体 if len(r.T_in_number) == 0 && len(device.T_in_number) > 0 { r.T_in_number = device.T_in_number } r.T_remark = device.T_remark + T_remark if len(r.T_project) > 0 { r.T_project_log = device.T_project_log + r.T_project + "|" } else { r.T_project_log = device.T_project_log r.T_project = device.T_project } _, err = dao.orm.Update(&r, "T_contract_number", "T_in_number", "T_out_number", "T_State", "T_remark", "T_project", "T_project_log") if err != nil { logs.Error(lib.FuncName(), err) return } return } func (dao *DeviceDaoImpl) Read_Device_ByT_sn(T_sn string) (r Device, err error) { qs := dao.orm.QueryTable(new(Device)) var list []Device _, err = qs.Limit(1, 0).Filter("T_sn", T_sn).OrderBy("-CreateTime").All(&list) if err != nil { logs.Error(lib.FuncName(), err) } if len(list) == 0 { return r, orm.ErrNoRows } if len(list) > 0 { r = list[0] } return } func (dao *DeviceDaoImpl) Read_DeviceSn_List(T_contract_number string, T_product_id int, T_in_number, T_out_number string) (r []string, err error) { qs := dao.orm.QueryTable(new(Device)) // 过滤 cond := orm.NewCondition() cond.And("T_product_id", T_product_id) if len(T_contract_number) > 0 { cond = cond.And("T_contract_number", T_contract_number) } if len(T_in_number) > 0 { cond = cond.And("T_in_number", T_in_number) } if len(T_out_number) > 0 { cond = cond.And("T_out_number", T_out_number) } var maps []Device _, err = qs.SetCond((*orm2.Condition)(cond)).All(&maps) if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range maps { r = append(r, v.T_sn) } return } func (dao *DeviceDaoImpl) Read_Device_List(T_name, T_product_name, T_product_model string, T_State, page, page_z int) (r []Device_R, cnt int64) { var offset int if page <= 1 { offset = 0 } else { offset = (page - 1) * page_z } // 过滤 sqlWhere := " WHERE d.t__state > 0" if len(T_name) > 0 { sqlWhere += " AND (d.t_contract_number like \"%" + T_name + "%\" or d.t_out_number like \"%" + T_name + "%\" or d.t_sn like \"%" + T_name + "%\" or d.t_iccid like \"%" + T_name + "%\" or d.t_project like \"%" + T_name + "%\")" } if T_State > 0 { sqlWhere += fmt.Sprintf(" AND d.t__state = %d", T_State) } if len(T_product_name) > 0 { sqlWhere += " AND p.t_name like \"%" + T_product_name + "%\"" } if len(T_product_model) > 0 { sqlWhere += " AND p.t_model like \"%" + T_product_model + "%\"" } var maps_z []orm2.ParamsList // 获取总条数 sql := "SELECT COUNT(*) FROM device d JOIN product p ON d.t_product_id = p.ID JOIN (SELECT t_sn, MAX(create_time) AS max_create_time FROM device GROUP BY t_sn) latest " + "ON d.t_sn = latest.t_sn AND d.create_time = latest.max_create_time" sql = sql + sqlWhere fmt.Println(sql) _, err := dao.orm.Raw(sql).ValuesList(&maps_z) if err != nil { return r, 0 } if len(maps_z) == 0 { return r, 0 } sql = "SELECT *,latest.t__state as t_device_state,p.t_name AS t_product_name,p.t_model AS t_product_model,p.t_class AS t_product_class,p.t_spec AS t_product_spec " + "FROM device d JOIN product p ON d.t_product_id = p.ID JOIN (SELECT t_sn,t__state, MAX(create_time) AS max_create_time FROM device GROUP BY t_sn) latest " + "ON d.t_sn = latest.t_sn AND d.create_time = latest.max_create_time" sql = sql + sqlWhere sql += " ORDER BY d.create_time DESC" sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z) fmt.Println(sql) var maps []Device_R _, err = dao.orm.Raw(sql).QueryRows(&maps) if err != nil { return r, 0 } for _, v := range maps { v.T_remark, v.T_project_log = dao.Read_Device_History(v.T_sn) r = append(r, DeviceToDevice_R(v)) } count, _ := strconv.Atoi(maps_z[0][0].(string)) return r, int64(count) } func (dao *DeviceDaoImpl) Read_Device_History(T_sn string) (T_remark string, T_project string) { qs := dao.orm.QueryTable(new(Device)) var maps []Device _, err := qs.Filter("T_sn", T_sn).OrderBy("CreateTime").All(&maps) if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range maps { if v.T_State == 1 { T_remark += fmt.Sprintf("%s:%s(%s)|", v.CreateTime.Format("2006-01-02"), "出库", v.T_out_number) if len(v.T_project) > 0 { T_project += fmt.Sprintf("%s:%s(%s):%s|", v.CreateTime.Format("2006-01-02"), "出库", v.T_out_number, v.T_project) } } if v.T_State == 2 { T_remark += fmt.Sprintf("%s:%s(%s)|", v.CreateTime.Format("2006-01-02"), "入库", v.T_in_number) } } return } func (dao *DeviceDaoImpl) Delete_Device_ByT_in_number(T_number, T_sn string) (err error) { qs := dao.orm.QueryTable(new(Device)) var device Device err = qs.Filter("T_sn", T_sn).Filter("T_in_number", T_number).One(&device) if err != nil { return } var count int64 count, err = qs.Filter("T_sn", T_sn).Filter("CreateTime__gt", device.CreateTime).Count() if err != nil { logs.Error(lib.FuncName(), err) return } if count > 1 { return errors.New("设备已出库,无法删除") } _, err = dao.orm.Delete(&device) if err != nil { logs.Error(lib.FuncName(), err) return } return err } func (dao *DeviceDaoImpl) Delete_Device_ByT_out_number(T_number, T_sn string) (err error) { qs := dao.orm.QueryTable(new(Device)) var device Device err = qs.Filter("T_sn", T_sn).Filter("T_out_number", T_number).One(&device) if err != nil { return } var count int64 count, err = qs.Filter("T_sn", T_sn).Filter("CreateTime__gt", device.CreateTime).Count() if err != nil { logs.Error(lib.FuncName(), err) return } if count > 1 { return errors.New("设备已入库,无法删除") } _, err = dao.orm.Delete(&device) if err != nil { logs.Error(lib.FuncName(), err) return } return err } func (dao *DeviceDaoImpl) Update_Device_ByT_out_number_T_project(T_number, T_project string) error { qs := dao.orm.QueryTable(new(Device)) var list []Device _, err := qs.Filter("T_out_number", T_number).All(&list) if err != nil { logs.Error(lib.FuncName(), err) return err } for _, deivce := range list { deivce.T_project = T_project _, err = dao.orm.Update(&deivce, "T_project") if err != nil { logs.Error(lib.FuncName(), err) return err } } return nil } func (dao *DeviceDaoImpl) Update_Device_CreateTimeByT_out_number(T_number, T_date string) error { now := time.Now().Format("15:04:05") date, _ := lib.TimeStrToTime(T_date + " " + now) qs := dao.orm.QueryTable(new(Device)) var list []Device _, err := qs.Filter("T_out_number", T_number).All(&list) if err != nil { logs.Error(lib.FuncName(), err) return err } for _, device := range list { //device.CreateTime = date //_, err = dao.orm.Update(&device, "CreateTime") _, err = dao.orm.Raw("UPDATE device SET create_time = ? WHERE id = ?", date, device.Id).Exec() if err != nil { logs.Error(lib.FuncName(), err) return err } } return nil } func (dao *DeviceDaoImpl) Update_Device_CreateTimeByT_in_number(T_number, T_date string) error { now := time.Now().Format("15:04:05") date, _ := lib.TimeStrToTime(T_date + " " + now) qs := dao.orm.QueryTable(new(Device)) var list []Device _, err := qs.Filter("T_in_number", T_number).All(&list) if err != nil { logs.Error(lib.FuncName(), err) return err } for _, device := range list { //device.CreateTime = date //_, err = dao.orm.Update(&device, "CreateTime") _, err = dao.orm.Raw("UPDATE device SET create_time = ? WHERE id = ?", date, device.Id).Exec() if err != nil { logs.Error(lib.FuncName(), err) return err } } return nil }