package util import ( "fmt" "log" "strconv" "github.com/pkg/errors" "kpt.xdmy/apiserver/routers/restful" "kpt.xdmy/pkg/logging" "kpt.xdmy/pkg/setting" ) // @Summary 现代牧业EAS对接 备件,出入库 type BillLaid struct { // 销售出库&其它出库 &采购入库 & 其他入库 BizType BillNumber `json:"bizType"` //业务类型 BizDate string `json:"bizDate"` //业务日期 Description string `json:"description"` //摘要 PaymentType BillNumber `json:"paymentType"` // 付款方式 TransactionType BillNumber `json:"transactionType"` // 事务类型 Number string `json:"number"` // 单据编号 Entry []BillEntry `json:"entry"` // StorageOrgUnit BillNumber `json:"storageOrgUnit"` //库存组织 Supplier BillNumber `json:"supplier"` //供应商 Currency BillNumber `json:"currency"` // 币别 Creator BillNumber `json:"creator"` //创建者 } type BillNumber struct { Number string `json:"number"` } type BillEntry struct { ActualPrice string `json:"actualPrice"` //实际单价 ActualTaxPrice string `json:"actualTaxPrice"` //实际含税单价 UnitActualCost string `json:"unitActualCost"` //单位实际成本 UnitPurchaseCost string `json:"unitPurchaseCost"` //单位采购成本 TaxPrice string `json:"taxPrice"` //含税单价 BalanceSupplier BillNumber `json:"balanceSupplier"` //结算供应商 Price string `json:"price"` //单价 ActualCost string `json:"actualCost"` //实际成本 PurchaseCost string `json:"purchaseCost"` //采购成本 Amount string `json:"amount"` // 金额 TaxAmount string `json:"taxAmount"` // 价税合计 ContractNumber string `json:"contractNumber"` // 合同号 Warehouse BillNumber `json:"warehouse"` // 仓库 Unit BillNumber `json:"unit"` // 单位 Material BillNumber `json:"material"` //物料 Qty string `json:"qty"` //数量 StoreType BillNumber `json:"storeType"` //库存类型 StoreStatus BillNumber `json:"storeStatus"` //库存状态 } type BillUse struct { //领料出库&生产入库 Head BillUseHead `json:"head"` Entrys []BillUseEntrys `json:"entrys"` } type BillUseHead struct { BillNumber string `json:"billNumber"` //单据编号 BizDate string `json:"bizDate"` //业务日期 BizTypeNumber string `json:"bizTypeNumber"` //业务类型 TransactionType string `json:"transactionType"` //事务类型 Department string `json:"department"` //部门 SupplyStorageOrg string `json:"supplyStorageOrg"` //供方库存组织 StorageOrg string `json:"storageOrg"` //需方库存组织 CostCenter string `json:"costCenter"` //成本中心 Remark string `json:"remark"` //摘要 Creator string `json:"creator"` //创建者 } type BillUseEntrys struct { MaterielNumber string `json:"materielNumber"` // 物料 Lot string `json:"lot"` //批次 Amount string `json:"amount"` //数量 MeteredUnit string `json:"meteredUnit"` //单位 WareHouse string `json:"wareHouse"` //需方仓库 Location string `json:"location"` //需方库位 Picker string `json:"picker"` // 领料员 EntryRemark string `json:"entryRemark"` //备注 MasterDepartment string `json:"masterDepartment"` //主制部门 EntryCostCenter string `json:"entryCostCenter"` //成本中心 } type Parts struct { // 备件 MaterialNumber string `json:"materialNumber"` MaterialName string `json:"materialName"` MaterialGroup string `json:"materialGroup"` Model string `json:"model"` BaseUnit string `json:"baseUnit"` Status int `json:"status"` KaClass string `json:"kaClass"` //kaClass int32 `json:"kaClass"` //planningMode int32 `json:"planningMode"` } func EASBillSync(typeIN int, billCode interface{}, statue string) error { defer func() { if err := recover(); err != nil { logging.Error("EASBillSync recover err :", err) } }() fmt.Println("++++++++++++++++", typeIN, billCode) var err error schema := "" id := "" switch typeIN { case 1: // 入库 schema = "bigpartlaid" id, err = BigpartLaid(billCode, "syncEASPpartlaid", "006") case 2: // 出库 schema = "bigpartUse" id, err = BigpartUse(billCode, "syncEASPpartuse", "100") case 3, 10: //退库 schema = "bigpartquit" if typeIN == 3 { sqlstr := fmt.Sprintf("select id from bigpartquit where useForm='%s' and issync = 0 order by id limit 1", billCode) restful.BasicList(sqlstr, nil, &billCode) if err != nil { log.Default().Printf("EASBillSync:%v", err) } } id, err = BigpartUse(billCode, "syncEASPpartquit", "100") case 4: //退货 schema = "bigPartRefund" id, err = BigpartLaid(billCode, "syncEASPPartRefund", "006") case 5: //备件 schema = "parts" id = billCode.(string) // sql = "SELECT id billCode FROM parts WHERE issync = 0 and statue = 1" err = CheckEASWebServiceParts(statue, "syncEASParts", billCode.(string)) case 6: //调拨入库&出库 schema = "bigparttransfer" id, err = BigpartLaid(billCode, "syncEASPpartlaid", "008") case 7: //仅调拨出库 schema = "bigpartUse" id, err = BigpartUseTransfer(billCode, "syncEASPpartuse", "009") default: return errors.New("未知处理类型") } tx, err := restful.Dbs.Beginx() if err != nil { logging.Error("tx.Begin 事务启动失败__error:", err) } defer func() { switch { case err != nil: if tx != nil { tx.Rollback() } default: if tx != nil { err = tx.Commit() } } }() if err != nil { logging.Info("EASBillSync err", err) fmt.Println("EASBillSync err", err) sql := "update " + schema + " set msg = ? where id =?" _, err = tx.Exec(sql, err.Error(), id) return err } if id == "" { return nil } sql := "update " + schema + " set issync = 1 where id =?" _, err = tx.Exec(sql, id) return err } func CheckEASWebServiceParts(statue, sqlname, id string) error { if statue == "1" { var sqls, params string sqlstr := fmt.Sprintf("SELECT sqlstr,params FROM apisql WHERE sqlname = '%s' limit 1", sqlname) err := restful.BasicList(sqlstr, nil, &sqls, ¶ms) if err != nil { log.Default().Printf("CheckEASWebServiceParts query error: %v", err) } m, err := restful.MapStr(sqls, &id) if err != nil { logging.Error("CheckEASWebServiceParts select err", err) return err } if len(m) == 0 { logging.Error("CheckEASWebServiceParts select len is 0") return errors.New("CheckEASWebServiceParts 查询结果为空") } var parts = Parts{} parts.BaseUnit = m["baseUnit"] parts.MaterialGroup = m["materialGroup"] parts.MaterialName = m["materialName"] parts.MaterialNumber = m["materialNumber"] parts.KaClass = m["kaClass"] parts.Model = m["model"] parts.Status = 0 sessionid := "" // 先判断备件与单据是否要分两个地址同步(一个正式线一个测试线) if setting.ServerSetting.EasUrlTem != "" { sessionid, err = EASWebServiceLogin(setting.ServerSetting.EasUrlTem) if err != nil || len(sessionid) < 7 { log.Default().Printf("EASWebServiceLogin :%s,%v", sessionid, err) return errors.New("EASWebServiceLogin faild") } } else { singelChan <- struct{}{} sessionid = <-sessionidChan if len(sessionid) < 7 || sessionid[:6] == "error:" { return errors.New(fmt.Sprintf("eas登录失败,sessionid:%s", sessionid)) } } err = EASWebServicInsertParts(sessionid, parts, setting.ServerSetting.EasUrlTem) if err != nil { logging.Error("EASWebServicInsertParts", err) return err } } return nil } func BigpartUse(billCode interface{}, sqlname, billType string) (string, error) { var sqls string pu := BillUse{} sqlstr := fmt.Sprintf("SELECT sqlstr FROM apisql WHERE sqlname = '%s' limit 1", sqlname) err := restful.BasicList(sqlstr, nil, &sqls) if err != nil { log.Default().Printf("BigpartUse :%v", err) return "", err } ress, _ := restful.MapStrList(sqls, billCode) if len(ress) == 0 { logging.Error("BigpartUse select len is 0") return "", errors.New("查询结果为空") } id := ress[0]["id"] pu.Head.BillNumber = ress[0]["BillNumber"] pu.Head.BizDate = ress[0]["BizDate"] pu.Head.BizTypeNumber = ress[0]["BizTypeNumber"] pu.Head.TransactionType = ress[0]["TransactionType"] pu.Head.Department = ress[0]["Department"] pu.Head.SupplyStorageOrg = ress[0]["SupplyStorageOrg"] pu.Head.StorageOrg = ress[0]["StorageOrg"] pu.Head.Creator = ress[0]["Creator"] pu.Head.Remark = ress[0]["Remark"] pu.Head.CostCenter = ress[0]["Department"] isZeroStock := false //是否零库存 en := make([]BillUseEntrys, len(ress)) count := 0 for key, res := range ress { if res["isZeroStock"] == "1" { isZeroStock = true count++ } en[key].Amount = res["Amount"] en[key].EntryRemark = res["EntryRemark"] en[key].Lot = res["Lot"] en[key].MaterielNumber = res["MaterielNumber"] en[key].MeteredUnit = res["MeteredUnit"] en[key].Picker = res["Picker"] en[key].WareHouse = res["WareHouse"] en[key].Location = res["Location"] en[key].MasterDepartment = ress[0]["Department"] en[key].EntryCostCenter = ress[0]["Department"] } pu.Entrys = en pujson, _ := json.Marshal(pu) singelChan <- struct{}{} sessionid := <-sessionidChan if len(sessionid) < 7 || sessionid[:6] == "error:" { return id, errors.New("eas登录失败") } //零库存出库,先进行入库,每个供应商一个入库单 if isZeroStock { pls := make([]BillLaid, 0) pl := BillLaid{} pl.Number = ress[0]["BillNumber"] + "-RK" pl.TransactionType = BillNumber{ Number: "001", } pl.BizDate = ress[0]["BizDate1"] pl.BizType = BillNumber{ Number: "110", } pl.Currency = BillNumber{ Number: "BB01", } pl.PaymentType = BillNumber{ Number: "004", } pl.StorageOrgUnit = BillNumber{ Number: ress[0]["SupplyStorageOrg"], } pl.Supplier = BillNumber{ Number: ress[0]["BalanceSupplier"], } pl.Creator = BillNumber{ Number: ress[0]["Creator"], } en := make([]BillEntry, 0) count = 0 temp := "" cou := 1 code := pl.Number for _, res := range ress { if res["isZeroStock"] == "0" { continue } if temp == "" { temp = res["BalanceSupplier"] } if temp != res["BalanceSupplier"] { pl.Entry = en pls = append(pls, pl) pl.Number = code + "-" + strconv.Itoa(cou) pl.Supplier = BillNumber{ Number: res["BalanceSupplier"], } cou++ temp = res["BalanceSupplier"] en = make([]BillEntry, 0) } pl.Supplier = BillNumber{ Number: res["BalanceSupplier"], } ent := BillEntry{} ent.Amount = res["SumPrice"] ent.Qty = res["Amount"] ent.ActualCost = res["SumPrice"] ent.ActualPrice = res["Price"] ent.ActualTaxPrice = res["Price"] ent.BalanceSupplier = BillNumber{ Number: res["BalanceSupplier"], } ent.ContractNumber = res["ContractNumber"] ent.Material = BillNumber{ Number: res["MaterielNumber"], } ent.Price = res["Price"] ent.PurchaseCost = res["SumPrice"] ent.TaxAmount = res["SumPrice"] ent.TaxPrice = res["Price"] ent.Unit = BillNumber{ Number: res["MeteredUnit"], } ent.UnitActualCost = res["SumPrice"] ent.UnitPurchaseCost = res["Price"] ent.Warehouse = BillNumber{ Number: res["WareHouse"], } en = append(en, ent) } pl.Entry = en pls = append(pls, pl) pljson, _ := json.Marshal(pls) //println(string(pljson)) err = EASWebServicInsertPartsuselaid(sessionid, string(pljson), "006") if err != nil { logging.Error("EASWebServicInsertPartsuselaid", err) return id, err } } err = EASWebServicInsertPartsuselaid(sessionid, string(pujson), billType) if err != nil { logging.Error("EASWebServicInsertPartsuselaid", err) return id, err } //EASWebServiceLogout() return id, nil } // func BigpartUseTransfer(billCode interface{}, sqlname, billType string) (string, error) { var sqls, params string sqlstr := fmt.Sprintf("SELECT sqlstr,params FROM apisql WHERE sqlname = '%s' limit 1", sqlname) err := restful.BasicList(sqlstr, nil, &sqls, ¶ms) if err != nil { log.Default().Printf("BigpartUseTransfer basiclist: %v", err) } ress, _ := restful.MapStrList(sqls, billCode) if len(ress) == 0 { logging.Error("BigpartUse select len is 0", len(ress)) return "", errors.New("查询结果为空") } id := ress[0]["id"] pus := make([]BillLaid, 0) pu := BillLaid{} pu.Number = ress[0]["BillNumber"] pu.TransactionType = BillNumber{ Number: ress[0]["TransactionType"], } pu.BizDate = ress[0]["BizDate1"] pu.BizType = BillNumber{ Number: ress[0]["BizTypeNumber"], } pu.Currency = BillNumber{ Number: "BB01", } pu.PaymentType = BillNumber{ Number: ress[0]["PaymentType"], } pu.StorageOrgUnit = BillNumber{ Number: ress[0]["SupplyStorageOrg"], } pu.Supplier = BillNumber{ Number: ress[0]["BalanceSupplier"], } pu.Description = ress[0]["Description"] pu.Creator = BillNumber{ Number: ress[0]["Creator"], } en := make([]BillEntry, len(ress)) isZeroStock := false for key, res := range ress { isZeroStock = true en[key].Amount = res["SumPrice"] en[key].Qty = res["Amount"] en[key].ActualCost = res["SumPrice"] en[key].ActualPrice = res["Price"] en[key].ActualTaxPrice = res["Price"] en[key].BalanceSupplier = BillNumber{ Number: res["BalanceSupplier"], } en[key].ContractNumber = res["ContractNumber"] en[key].Material = BillNumber{ Number: res["MaterielNumber"], } en[key].Price = res["Price"] en[key].PurchaseCost = res["SumPrice"] en[key].TaxAmount = res["SumPrice"] en[key].TaxPrice = res["Price"] en[key].Unit = BillNumber{ Number: res["MeteredUnit"], } en[key].UnitActualCost = res["SumPrice"] en[key].UnitPurchaseCost = res["Price"] en[key].Warehouse = BillNumber{ Number: res["WareHouse"], } en[key].StoreType = BillNumber{ Number: "G", } en[key].StoreStatus = BillNumber{ Number: "1", } } if isZeroStock { pu.Entry = en pus = append(pus, pu) pujson, _ := json.Marshal(pus) singelChan <- struct{}{} sessionid := <-sessionidChan if len(sessionid) < 7 || sessionid[:6] == "error:" { return id, errors.New("eas登录失败") } err = EASWebServicInsertPartsuselaid(sessionid, string(pujson), billType) if err != nil { logging.Error("EASWebServicInsertPartsuselaid", err) return id, err } } tx, err := restful.Dbs.Beginx() if err != nil { logging.Error("tx.Begin 事务启动失败__error:", err) } defer func() { switch { case err != nil: fmt.Println("__error:", err) if tx != nil { tx.Rollback() } default: if tx != nil { err = tx.Commit() } } }() sql := "update bigpartUse set issync = 1 where id =?" _, err = tx.Exec(sql, id) return id, err } func BigpartLaid(billCode interface{}, sqlname, billtype string) (string, error) { var sqls, params string sqlstr := fmt.Sprintf("SELECT sqlstr,params FROM apisql WHERE sqlname = '%s' limit 1", sqlname) err := restful.BasicList(sqlstr, nil, &sqls, ¶ms) if err != nil { log.Default().Printf("BigpartLaid basiclist: %v", err) } ress, _ := restful.MapStrList(sqls, billCode) if len(ress) == 0 { logging.Error("BigpartLaid select len is 0", len(ress)) return "", errors.New("查询结果为空") } id := ress[0]["id"] pls := make([]BillLaid, 0) pl := BillLaid{} pl.Number = ress[0]["BillNumber"] pl.TransactionType = BillNumber{ Number: ress[0]["TransactionType"], } pl.BizDate = ress[0]["BizDate"] pl.BizType = BillNumber{ Number: ress[0]["BizType"], } pl.Currency = BillNumber{ Number: ress[0]["Currency"], } pl.PaymentType = BillNumber{ Number: ress[0]["PaymentType"], } pl.StorageOrgUnit = BillNumber{ Number: ress[0]["StorageOrgUnit"], } pl.Supplier = BillNumber{ Number: ress[0]["BalanceSupplier"], } pl.Description = ress[0]["Description"] pl.Creator = BillNumber{ Number: ress[0]["Creator"], } en := make([]BillEntry, len(ress)) isZeroStock := false for key, res := range ress { // 零库存入库跳过 if res["isZeroStock"] == "1" { continue } else { isZeroStock = true } pl.Supplier = BillNumber{ Number: res["BalanceSupplier"], } en[key].Amount = res["Amount"] en[key].Qty = res["Qty"] en[key].ActualCost = res["ActualCost"] en[key].ActualPrice = res["ActualPrice"] en[key].ActualTaxPrice = res["ActualTaxPrice"] en[key].BalanceSupplier = BillNumber{ Number: res["BalanceSupplier"], } en[key].ContractNumber = res["ContractNumber"] en[key].Material = BillNumber{ Number: res["Material"], } en[key].Price = res["Price"] en[key].PurchaseCost = res["PurchaseCost"] en[key].TaxAmount = res["TaxAmount"] en[key].TaxPrice = res["TaxPrice"] en[key].Unit = BillNumber{ Number: res["Unit"], } en[key].UnitActualCost = res["UnitActualCost"] en[key].UnitPurchaseCost = res["UnitPurchaseCost"] en[key].Warehouse = BillNumber{ Number: res["Warehouse"], } } if isZeroStock { pl.Entry = en pls = append(pls, pl) pujson, _ := json.Marshal(pls) singelChan <- struct{}{} sessionid := <-sessionidChan if len(sessionid) < 7 || sessionid[:6] == "error:" { return id, errors.New("eas登录失败") } err = EASWebServicInsertPartsuselaid(sessionid, string(pujson), billtype) if err != nil { logging.Error("EASWebServicInsertPartsuselaid", err) return id, err } if billtype == "008" { // 为调拨入库时 _, err = BigpartUseTransfer(ress[0]["useCode"], "syncEASPpartuse", "009") } } return id, nil }