123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- package Stock
- import (
- "ERP_storage/logs"
- "ERP_storage/models/Basic"
- "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"
- "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_remark string
- T_project string
- T_project_log string
- // ---------产品信息-----------
- T_product_id int
- T_product_name string
- T_product_class_name string
- T_product_model string
- T_product_spec string
- T_product_img string
- }
- func DeviceToDevice_R(t Device) (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_State
- r.T_remark = t.T_remark
- r.T_project = t.T_project
- r.T_project_log = t.T_project_log
- r.T_product_id = t.T_product_id
- product, _ := Basic.Read_Product_ById(t.T_product_id)
- r.T_product_name = product.T_name
- r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class)
- r.T_product_model = product.T_model
- r.T_product_spec = product.T_spec
- r.T_product_img = product.T_img
- 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) {
- 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))
- err = qs.Filter("T_sn", T_sn).One(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- 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 string, T_State, page, page_z int) (r []Device_R, cnt int64) {
- qs := dao.orm.QueryTable(new(Device))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- // 过滤
- cond := orm.NewCondition()
- cond1 := cond.And("T_State__gt", 0)
- if len(T_name) > 0 {
- cond1 = cond1.AndCond(
- cond.Or("T_contract_number__icontains", T_name).
- Or("T_out_number__icontains", T_name).
- Or("T_sn__icontains", T_name).
- Or("T_iccid__icontains", T_name).
- Or("T_project__icontains", T_name))
- }
- if T_State > 0 {
- cond1 = cond1.And("T_State", T_State)
- }
- var maps []Device
- _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range maps {
- r = append(r, DeviceToDevice_R(v))
- }
- return
- }
|