|
package models
|
|
|
|
import (
|
|
"LAPP_SJA_ME/db"
|
|
"LAPP_SJA_ME/utils"
|
|
"xorm.io/core"
|
|
"errors"
|
|
)
|
|
|
|
//零件供应组
|
|
type MeSupplyGroup struct {
|
|
Finr int `xorm:"pk comment('工厂号') int(0)" json:"me_supplygroup-finr"`
|
|
Supplygroupid string `xorm:"pk comment('供应组Id') VARCHAR(20)" json:"me_supplygroup-supplygroupid"`
|
|
Descr string `xorm:"comment('描述') VARCHAR(40)" json:"me_supplygroup-descr"`
|
|
Sgtype string `xorm:"comment('供应组类型') VARCHAR(20)" json:"me_supplygroup-sgtype"`
|
|
Shippablesg int `xorm:"comment('用于包装发运') int(0)" json:"me_supplygroup-shippablesg"`
|
|
Multioutput int `xorm:"comment('生成多个WD') int(0)" json:"me_supplygroup-multioutput"`
|
|
Modfactor int `xorm:"comment('除余因子') int(0)" json:"me_supplygroup-modfactor"`
|
|
Fsupplygroupid string `xorm:"comment('父零件族Id') VARCHAR(20)" json:"me_supplygroup-fsupplygroupid"`
|
|
Partfamilyid string `xorm:"comment('零件族Id') VARCHAR(20)" json:"me_supplygroup-partfamilyid"`
|
|
Worklineid string `xorm:"comment('生产线ID') Int" json:"me_supplygroup-worklineid"`
|
|
SgCust1 string `xorm:"comment('生产提前期/秒') VARCHAR(40)" json:"me_supplygroup-sg_cust1"`
|
|
SgCust2 string `xorm:"comment('供应组特性2') VARCHAR(40)" json:"me_supplygroup-sg_cust2"`
|
|
SgCust3 string `xorm:"comment('供应组特性3') VARCHAR(40)" json:"me_supplygroup-sg_cust3"`
|
|
SgCust4 string `xorm:"comment('供应组特性4') VARCHAR(40)" json:"me_supplygroup-sg_cust4"`
|
|
Lastmodif string `xorm:"comment('最近一次更改时间') VARCHAR(14)" json:"me_supplygroup-lastmodif"`
|
|
Lastuser string `xorm:"comment('最近一次更改人') VARCHAR(20)" json:"me_supplygroup-lastuser"`
|
|
Credatuz string `xorm:"comment('创建时间') VARCHAR(14)" json:"me_supplygroup-credatuz"`
|
|
Valst []MeAttribute `json:"valst" xorm:"-"`
|
|
UnValst []MeAttribute `json:"unvalst" xorm:"-"`
|
|
FValst []MeSupplyGroup `json:"f_valst" xorm:"-"`
|
|
FUnvalst []MeSupplyGroup `json:"f_unvalst" xorm:"-"`
|
|
Assignqty int `json:"assignqty" xorm:"-"`
|
|
Assignfactor1 string `json:"assignfactor1" xorm:"-"`
|
|
Assignfactor2 string `json:"assignfactor2" xorm:"-"`
|
|
}
|
|
|
|
func (t *MeSupplyGroup) TableName() string {
|
|
return "me_supplygroup"
|
|
}
|
|
|
|
// 清除string字段的右侧空格
|
|
func (t *MeSupplyGroup) Clipped() {
|
|
utils.TrimStruct(t, *t)
|
|
}
|
|
|
|
//增
|
|
func (t *MeSupplyGroup) Add() error {
|
|
e := db.Eloquent.Master()
|
|
countrole := new(MeSupplyGroup)
|
|
affw, err := e.Table("me_supplygroup").ID(core.PK{t.Finr, t.Supplygroupid}).Count(countrole)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if affw > 0 {
|
|
return errors.New("数据已经存在!")
|
|
}
|
|
_, err = e.Table("me_supplygroup").Insert(t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
//批量插入
|
|
alldata := make([]MeSupplyGroupAtcodlst, 0)
|
|
for _, v := range t.Valst {
|
|
onedata := MeSupplyGroupAtcodlst{}
|
|
onedata.Finr = t.Finr
|
|
onedata.Supplygroupid = t.Supplygroupid
|
|
onedata.Attrvalue1 = v.Attrvalue1
|
|
onedata.Attrvalue2 = v.Attrvalue2
|
|
onedata.Attrcode = v.Attrcode
|
|
onedata.Mandatory = v.Mandatory
|
|
onedata.Verifyrule = v.Verifyrule
|
|
onedata.Credatuz = t.Credatuz
|
|
onedata.Lastuser = t.Lastuser
|
|
onedata.Lastmodif = t.Lastmodif
|
|
alldata = append(alldata, onedata)
|
|
}
|
|
_, err = e.Table("me_supplygroup_atcodlst").Insert(&alldata)
|
|
//判断是否是父供应组
|
|
if t.Sgtype == "F" {
|
|
//批量插入
|
|
fdata := make([]MeSupplyGroupAssignment, 0)
|
|
for k, v := range t.FValst {
|
|
onedata := MeSupplyGroupAssignment{}
|
|
onedata.Finr = t.Finr
|
|
onedata.Supplygroupid = v.Supplygroupid
|
|
onedata.Fsupplygroupid = t.Supplygroupid
|
|
onedata.Pos = k + 1
|
|
onedata.Assignfactor1 = v.Assignfactor1
|
|
onedata.Assignfactor2 = v.Assignfactor2
|
|
onedata.Credatuz = t.Credatuz
|
|
onedata.Lastuser = t.Lastuser
|
|
onedata.Lastmodif = t.Lastmodif
|
|
fdata = append(fdata, onedata)
|
|
}
|
|
_, err = e.Table("me_supplygroup_assignment").Insert(&fdata)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//删
|
|
func (t *MeSupplyGroup) Del() bool {
|
|
e := db.Eloquent.Master()
|
|
_, err := e.Table("me_supplygroup").Where("finr = ? and supplygroupid = ?", t.Finr, t.Supplygroupid).Delete(&MeSupplyGroup{})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
countrole := new(MeSupplyGroupAtcodlst)
|
|
affw, _ := e.Table("me_supplygroup_atcodlst").Where("finr = ? and supplygroupid = ?", t.Finr, t.Supplygroupid).Count(countrole)
|
|
if affw > 0 {
|
|
_, err = e.Table("me_supplygroup_atcodlst").Where("finr = ? and supplygroupid = ?", t.Finr, t.Supplygroupid).Delete(&MeSupplyGroupAtcodlst{})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
//改
|
|
func (t *MeSupplyGroup) Update() bool {
|
|
e := db.Eloquent.Master()
|
|
_, err := e.ID(core.PK{t.Finr, t.Supplygroupid}).Update(t)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
//批量删除
|
|
_, err = e.Table("me_supplygroup_atcodlst").Where("finr = ? and supplygroupid = ?", t.Finr, t.Supplygroupid).Delete(&MeSupplyGroupAtcodlst{})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
//批量插入
|
|
alldata := make([]MeSupplyGroupAtcodlst, 0)
|
|
for _, v := range t.Valst {
|
|
onedata := MeSupplyGroupAtcodlst{}
|
|
onedata.Finr = t.Finr
|
|
onedata.Supplygroupid = t.Supplygroupid
|
|
onedata.Attrvalue1 = v.Attrvalue1
|
|
onedata.Attrvalue2 = v.Attrvalue2
|
|
onedata.Attrcode = v.Attrcode
|
|
onedata.Mandatory = v.Mandatory
|
|
onedata.Verifyrule = v.Verifyrule
|
|
onedata.Credatuz = t.Credatuz
|
|
onedata.Lastuser = t.Lastuser
|
|
onedata.Lastmodif = t.Lastmodif
|
|
alldata = append(alldata, onedata)
|
|
}
|
|
_, err = e.Table("me_supplygroup_atcodlst").Insert(&alldata)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
//判断是否是父供应组
|
|
if t.Sgtype == "F" {
|
|
//批量删除
|
|
_, err = e.Table("me_supplygroup_assignment").Where("finr = ? and fsupplygroupid = ?", t.Finr, t.Supplygroupid).Delete(&MeSupplyGroupAssignment{})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
//批量插入
|
|
fdata := make([]MeSupplyGroupAssignment, 0)
|
|
for k, v := range t.FValst {
|
|
onedata := MeSupplyGroupAssignment{}
|
|
onedata.Finr = t.Finr
|
|
onedata.Supplygroupid = v.Supplygroupid
|
|
onedata.Fsupplygroupid = t.Supplygroupid
|
|
onedata.Pos = k + 1
|
|
onedata.Assignfactor1 = v.Assignfactor1
|
|
onedata.Assignfactor2 = v.Assignfactor2
|
|
onedata.Credatuz = t.Credatuz
|
|
onedata.Lastuser = t.Lastuser
|
|
onedata.Lastmodif = t.Lastmodif
|
|
fdata = append(fdata, onedata)
|
|
}
|
|
_, err = e.Table("me_supplygroup_assignment").Insert(&fdata)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
type MesupplygroupAttribute struct {
|
|
MeAttribute `xorm:"extends"`
|
|
MeSupplyGroupAtcodlst `xorm:"extends"`
|
|
}
|
|
|
|
type MesupplygroupAssignment struct {
|
|
MeSupplyGroup `xorm:"extends"`
|
|
MeSupplyGroupAssignment `xorm:"extends"`
|
|
}
|
|
|
|
//查
|
|
func (t *MeSupplyGroup) SelectOne() (MeSupplyGroup, error) {
|
|
e := db.Eloquent.Master()
|
|
var data MeSupplyGroup
|
|
_, err := e.ID(core.PK{t.Finr, t.Supplygroupid}).Get(&data)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
data.Clipped()
|
|
var datalist []MeAttribute
|
|
var alllist []MesupplygroupAttribute
|
|
e.Table("me_attribute").Join("LEFT", "me_supplygroup_atcodlst", "me_supplygroup_atcodlst.attrcode = me_attribute.attrcode").Where("me_attribute.finr = ? and me_supplygroup_atcodlst.supplygroupid = ?", t.Finr, t.Supplygroupid).Find(&alllist)
|
|
var attrcodes []int
|
|
for _, v := range alllist {
|
|
v.MeAttribute.Clipped()
|
|
v.MeSupplyGroupAtcodlst.Clipped()
|
|
one := MeAttribute{}
|
|
one = v.MeAttribute
|
|
one.Mandatory = v.MeSupplyGroupAtcodlst.Mandatory
|
|
one.Attrvalue1 = v.MeSupplyGroupAtcodlst.Attrvalue1
|
|
one.Attrvalue2 = v.MeSupplyGroupAtcodlst.Attrvalue2
|
|
one.Verifyrule = v.MeSupplyGroupAtcodlst.Verifyrule
|
|
datalist = append(datalist, one)
|
|
attrcodes = append(attrcodes, v.MeAttribute.Attrcode)
|
|
}
|
|
|
|
data.Valst = datalist
|
|
var me MeAttribute
|
|
me.Finr = t.Finr
|
|
UnValst, _ := me.SelectUnArr(attrcodes)
|
|
for k, _ := range UnValst {
|
|
UnValst[k].Clipped()
|
|
}
|
|
data.UnValst = UnValst
|
|
//判断是否是父供应组
|
|
if data.Sgtype == "F" {
|
|
fdatalist := make([]MeSupplyGroup, 0)
|
|
falllist := make([]MesupplygroupAssignment, 0)
|
|
err := e.Table("me_supplygroup").Join("LEFT", "me_supplygroup_assignment", "me_supplygroup_assignment.supplygroupid = me_supplygroup.supplygroupid and me_supplygroup_assignment.finr = me_supplygroup.finr").Where("me_supplygroup.finr = ? and me_supplygroup_assignment.fsupplygroupid = ?", t.Finr, t.Supplygroupid).Find(&falllist)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
for _, v := range falllist {
|
|
v.MeSupplyGroup.Clipped()
|
|
v.MeSupplyGroupAssignment.Clipped()
|
|
one := MeSupplyGroup{}
|
|
one = v.MeSupplyGroup
|
|
fdatalist = append(fdatalist, one)
|
|
}
|
|
data.FValst = fdatalist
|
|
UnValst, _ := t.SelectUnArr()
|
|
data.FUnvalst = UnValst
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func (t *MeSupplyGroup) SelectUnArr() (data []MeSupplyGroup, err error) {
|
|
e := db.Eloquent.Master()
|
|
var groupids []string
|
|
datalist := make([]MeSupplyGroupAssignment, 0)
|
|
err = e.Table("me_supplygroup_assignment").Where("finr = ?", t.Finr).Find(&datalist)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
for _, v := range datalist {
|
|
groupids = append(groupids, v.Supplygroupid)
|
|
}
|
|
err = e.Where("finr = ? and sgtype = ?", t.Finr, "C").NotIn("supplygroupid", groupids).Find(&data)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
for k, _ := range data {
|
|
data[k].Clipped()
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
//分页
|
|
func (t *MeSupplyGroup) GetPage(pageSize int, pageIndex int) ([]MeSupplyGroup, int, error) {
|
|
data := make([]MeSupplyGroup, 0)
|
|
e := db.Eloquent.Master()
|
|
table := e.Table("me_supplygroup").Where("finr = ? ", t.Finr)
|
|
where := "where finr = " + "'" + utils.ValueToString(t.Finr, "") + "'"
|
|
if t.Supplygroupid != "" {
|
|
table = table.And("supplygroupid = ?", t.Supplygroupid)
|
|
where += " and supplygroupid = " + "'" + utils.ValueToString(t.Supplygroupid, "") + "'"
|
|
}
|
|
|
|
Offset := (pageIndex - 1) * pageSize
|
|
err := e.SQL("SELECT TOP " + utils.ValueToString(pageSize, "") + " me_supplygroup.* FROM me_supplygroup " + where + " AND (convert(varchar(10),finr)+convert(varchar(40),supplygroupid) NOT IN (SELECT TOP " + utils.ValueToString(Offset, "") + " convert(varchar(10),finr)+convert(varchar(40),supplygroupid) FROM me_supplygroup " + where + " ORDER BY credatuz DESC)) ORDER BY credatuz DESC").Find(&data)
|
|
|
|
pcount := new(MeSupplyGroup)
|
|
count, err := table.Count(pcount)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
for k, _ := range data {
|
|
data[k].Clipped()
|
|
}
|
|
return data, int(count), nil
|
|
}
|