package service import ( "encoding/json" "fmt" _ "github.com/go-sql-driver/mysql" "github.com/k0kubun/pp/v3" log "github.com/sirupsen/logrus" "gorm.io/gorm" "kpt.xdmy/apiserver/config" "kpt.xdmy/apiserver/model" "kpt.xdmy/apiserver/model/http" "kpt.xdmy/pkg/util" "strconv" "strings" ) // 凭证冲销 func (s *Service) ProofOff(p *model.ProofReq) (hrp *http.Response) { r := new(http.ChargeOffReq) rp := new(http.SapResp) hrp = new(http.Response) hrp.Code = 200 hrp.Msg = "fail" r.Dest.DestID = "EQMAN" r.Dest.BussTp = "MM015" //r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM015/CancelMblnr" //r.Dest.Url = "http://192.168.61.117/SAPP0/Common/MM015/CancelMblnr" r.Dest.Url = fmt.Sprintf("%s/Common/MM015/CancelMblnr", config.Conf.Http.Routing) r.Data.ProofCode = p.ProofCode r.Data.ProofYear = p.ProofYear r.Data.ChargeDate = p.ChargeDate r.Data.OrderNumber = p.OrderNumber r.Data.HasTicket = util.ZeroStr(p.HasTicket == 1, "X") log.Infof("proofcode: %s", r.Data.ProofCode) sl := r.Data.RowsNumber for i := range p.RowsNumber { sl = append(sl, http.ChargeOffDetail{RowNumber: p.RowsNumber[i]}) } tx := s.d.DB.Begin() err := UpdateProof(p, tx) if err != nil { hrp.Message += fmt.Sprintf("%s . bigid = %d ,rows=%v", err.Error(), p.OrderNumberId, p.RowsNumber) tx.Rollback() } r.Data.RowsNumber = sl rbyte, _ := json.Marshal(r) //rbyte, _ := json.Marshal(r) fmt.Println(string(rbyte)) e := s.SyncSap(r, rp, rbyte) pp.Print(r) if e == nil { if rp.Data.Status == "S" { hrp.Message = fmt.Sprintf("sap物料冲销成功:%d %s %s", p.NumberType, rp.Data.ProofNumber, rp.Data.EqNumber) log.Info(hrp.Message) } else { hrp.Message = fmt.Sprintf("sap凭证冲销失败:%s", rp.Dest.MessText) log.Infoln(hrp.Message, r, p) tx.Rollback() return } } else { hrp.Message = fmt.Sprintf("sap冲销接口报错:error:%s", e) log.Infoln(hrp.Message, r, p) tx.Rollback() return } tx.Commit() hrp.Msg = "ok" return } //凭证冲销后状态更新 func UpdateProof(p *model.ProofReq, tx *gorm.DB) error { var tname string switch p.NumberType { case 1: // 入库 tname = "partlaid" case 2: // 退货 tname = "partrefund" case 3: // 出库 tname = "partuse" case 4: // 退库 tname = "partquit" } //strings.Join(p.RowsNumber, ",") rowsNumbers := []string{} for _, rowsNumber := range p.RowsNumber { rowsNumbers = append(rowsNumbers, strconv.Itoa(rowsNumber)) } if p.NumberType == 1 { //for _, item := range p.ProofPart { if e := tx.Table(tname).Where("bigid=? ", p.OrderNumberId).Where(" FIND_IN_SET(rowNumber,? ) ", strings.Join(rowsNumbers, ",")).Update("dflag", 1).Error; e != nil { log.Errorln(e, p.OrderNumberId, p.RowsNumber) tx.Rollback() return e } //} //select id,name from user where FIND_IN_SET(id,#{strlist}) if e := tx.Exec(fmt.Sprintf(` update big%s set sterilisation = 1 where id = ? `, tname), p.OrderNumberId).Error; e != nil { log.Errorln(e, p.OrderNumberId, p.RowsNumber) } } else if p.NumberType == 3 { if e := tx.Table(tname).Where("bigid=? ", p.OrderNumberId).Where(" FIND_IN_SET(rowNumber,? ) ", strings.Join(rowsNumbers, ",")). Update("dflag", 1).Update("hasTicket", p.HasTicket).Error; e != nil { log.Errorln(e, p.OrderNumberId, p.RowsNumber) } if e := tx.Exec(fmt.Sprintf(` update big%s set sterilisation = 1 where id = ? `, tname), p.OrderNumberId).Error; e != nil { log.Errorln(e, p.OrderNumberId, p.RowsNumber) } } else if p.NumberType == 2 { if e := tx.Table(tname).Where("bigid=? ", p.OrderNumberId).Where(" FIND_IN_SET(rowNumber,? ) ", strings.Join(rowsNumbers, ",")). Update("dflag", 1).Update("hasTicket", p.HasTicket).Error; e != nil { log.Errorln(e, p.OrderNumberId, p.RowsNumber) } if e := tx.Exec(fmt.Sprintf(` update big%s set sterilisation = 1 where id = ? `, tname), p.OrderNumberId).Error; e != nil { log.Errorln(e, p.OrderNumberId, p.RowsNumber) } } if p.NumberType == 1 { partlaidList := make([]*model.PartLaid, 0) err := tx.Raw(`select * from partlaid where bigid = ? and FIND_IN_SET(rowNumber,? ) `, p.OrderNumberId, strings.Join(rowsNumbers, ",")).Find(&partlaidList).Error if err != nil { log.Error(err) return err } for _, part := range partlaidList { err := tx.Exec(` update part_repertory pr set pr.reportery = pr.reportery - ? where pr.partCode = ? and pr.contractId = ? and (select pastureid from warehouse where id = pr.locationId and status = 1 limit 1 ) = (select pastureid from bigpartlaid where id = ? ) `, part.StorageAmount, part.PartCode, part.ContractID, p.OrderNumberId).Error if err != nil { log.Error(err) return err } } } else if p.NumberType == 3 { partUseList := make([]*model.PartUse, 0) err := tx.Raw(`select * from partuse where bigid = ? and FIND_IN_SET(rowNumber,? ) `, p.OrderNumberId, strings.Join(rowsNumbers, ",")).Find(&partUseList).Error if err != nil { log.Error(err) return err } for _, part := range partUseList { err := tx.Exec(` update part_repertory pr set pr.reportery = pr.reportery + ? where pr.partCode = ? and pr.contractId = ? and (select pastureid from warehouse where id = pr.locationId and status = 1 limit 1 ) = (select pastureid from bigpartuse where id = ? ) `, part.CheckoutNumber, part.PartCode, part.ContractID, p.OrderNumberId).Error if err != nil { log.Error(err) return err } } } else if p.NumberType == 2 { partRefundList := make([]*model.PartRefund, 0) err := tx.Raw(`select * from partrefund where bigid = ? and FIND_IN_SET(rowNumber,? ) `, p.OrderNumberId, strings.Join(rowsNumbers, ",")). Find(&partRefundList).Error if err != nil { log.Error(err) return err } for _, part := range partRefundList { err := tx.Exec(` update part_repertory pr set pr.reportery = pr.reportery + ? where pr.partCode = ? and pr.contractId = ? and (select pastureid from warehouse where id = pr.locationId and status = 1 limit 1 ) = (select pastureId from bigpartrefund where id = ? ) `, part.RefundNumber, part.PartCode, part.ContractID, p.OrderNumberId).Error if err != nil { log.Error(err) return err } } } return nil } // 查询 单据 对应备件明细 func ProofList(p *model.ProofReq) (hrp *http.Response) { switch p.NumberType { case 1: // 入库 return s.laidProofList(p) case 2: // 退货 return s.returnProofList(p) case 3: // 出库 return s.useProofList(p) case 4: // 退库 return s.quitProofList(p) } return } // 入库冲销 func (s *Service) laidProofList(p *model.ProofReq) (hrp *http.Response) { bp := new(model.BigPartLaid) proofs := make([]model.ProofPart, 0) var e error hrp = new(http.Response) hrp.Code = 200 hrp.Msg = "fail" if e = s.d.DB.First(bp, model.BigPartLaid{LaidCode: p.OrderNumber}).Error; e != nil { hrp.Message = fmt.Sprintf("%s ,error=%v,", p.OrderNumber, e) return } p.OrderNumberId = bp.ID p.ProofCode = bp.ProofCode p.ChargeDate = bp.CreatDate.Format("2006-01-02") p.ProofYear = bp.CreatDate.Format("2006") parts := make([]model.PartLaid, 0) if e = s.d.DB.Debug().Raw(` select * from partlaid where bigid = ? and ifnull(dflag,0) != 1 order by rowNumber,id desc `, bp.ID).Order("rowNumber,id desc "). Find(&parts).Error; e != nil || len(parts) == 0 { hrp.Message = fmt.Sprintf("入库单明细查询报错或结果为空:bigid=%v error=%v,", bp.ID, e) hrp.Msg = "fail" return } for i, v := range parts { mp := new(model.ProofPart) br := new(model.Brand) w := new(model.Warehouse) pv := new(model.Provider) if e := s.d.DB.First(&pv, model.Provider{ID: v.ProviderID}).Error; e != nil { log.Errorln(v.ProviderID, e) //continue } if e := s.d.DB.First(&br, model.Brand{ID: v.BrandID}).Error; e != nil { log.Errorln(e, v.BrandID) //continue } if e := s.d.DB.First(&w, model.Warehouse{ID: v.LocationID}).Error; e != nil { log.Errorln(v.LocationID, e) //continue } if v.RowNumber > 0 { mp.RowNumber = v.RowNumber } else { mp.RowNumber = len(parts) - i } mp.PartCode = v.PartCode mp.PartName = v.PartName mp.Specification = v.Specification mp.ChangeCount = v.StorageAmount mp.UnitPrice = v.Price mp.TotalPrice = v.SumPrice mp.PartBrand = br.BrandName mp.Supplier = pv.SapCode mp.Warehouse = w.SapCode if v.Dflag == 0 { proofs = append(proofs, *mp) } } p.ProofPart = proofs hrp.Msg = "ok" hrp.Data = p hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber) return } // 退货冲销 func (s *Service) returnProofList(p *model.ProofReq) (hrp *http.Response) { hrp = new(http.Response) bp := new(model.BigPartRefund) proofs := make([]model.ProofPart, 0) hrp.Code = 200 var e error if e = s.d.DB.First(bp, model.BigPartRefund{UseForm: p.OrderNumber}).Error; e != nil { hrp.Message = fmt.Sprintf("bigpartrefund : %d,%s", p.NumberType, p.OrderNumber) hrp.Msg = "fail" return } p.OrderNumberId = bp.ID p.ProofCode = bp.ProofCode p.ChargeDate = bp.CreateTime.Format("2006-01-02") p.ProofYear = bp.CreateTime.Format("2006") funds := []model.PartRefund{} if e = s.d.DB.Where(" bigId = ? ", bp.ID).Where(" dflag != 1 ").Order("rowNumber,id desc ").Find(&funds).Error; e != nil || len(funds) == 0 { hrp.Message = fmt.Sprintf("PartRefund query error . bigid=%v error=%v,", bp.ID, e) hrp.Msg = "fail" return } for i, v := range funds { mp := new(model.ProofPart) pv := new(model.Provider) if e := s.d.DB.First(&pv, model.Provider{ID: bp.ProviderID}).Error; e != nil { log.Errorln(bp.ProviderID, e) continue } if v.RowNumber > 0 { mp.RowNumber = v.RowNumber } else { mp.RowNumber = len(funds) - i } mp.Supplier = pv.SapCode mp.PartBrand = v.Brand mp.Warehouse = "1004" mp.PartCode = v.PartCode mp.PartName = v.PartName mp.Specification = v.Specification mp.ChangeCount = v.RefundNumber mp.UnitPrice = v.Price mp.TotalPrice = v.SumPrice proofs = append(proofs, *mp) } p.ProofPart = proofs hrp.Msg = "ok" hrp.Data = p hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber) return } // 出库冲销 func (s *Service) useProofList(p *model.ProofReq) (hrp *http.Response) { hrp = new(http.Response) bp := new(model.BigPartUse) proofs := make([]model.ProofPart, 0) hrp.Code = 200 hrp.Msg = "fail" var e error if e = s.d.DB.First(bp, model.BigPartUse{UseForm: p.OrderNumber}).Error; e != nil { log.Error(e) hrp.Message = fmt.Sprintf("bigpartuse useform=%s", p.OrderNumber) return } p.OrderNumberId = bp.ID p.ProofCode = bp.ProofCode p.ChargeDate = bp.ReceiveTime.Format("2006-01-02") p.ProofYear = bp.ReceiveTime.Format("2006") uses := make([]model.PartUse, 0) if e = s.d.DB.Where("bigId = ?", bp.ID).Where(" dflag != 1 ").Order("rowNumber,id desc ").Find(&uses).Error; e != nil || len(uses) == 0 { log.Error(e) hrp.Message = fmt.Sprintf("PartUse bigid = %v ", bp.ID) return } for i, v := range uses { mp := new(model.ProofPart) br := new(model.Brand) w := new(model.Warehouse) c := new(model.Contract) bc := new(model.BigContract) pv := new(model.Provider) pa := new(model.PartRepertory) if v.ContractID > 0 { if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil { log.Error(e) hrp.Message = fmt.Sprintf("contract id =%d", v.ContractID) //return } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Error(e) hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID) //return } if e := s.d.DB.First(&pv, model.Provider{ID: bc.ProviderID}).Error; e != nil { log.Errorln(bc.ProviderID, e) //continue } } else { if e := s.d.DB.First(pa, model.PartRepertory{ContractID: v.ContractID, PartCode: v.PartCode, LocationID: v.LocationID}).Error; e != nil { log.Error(e) hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID) //return } if e := s.d.DB.First(&pv, model.Provider{ID: pa.ProviderID}).Error; e != nil { log.Errorln(pa.ProviderID, e) //continue } } if e := s.d.DB.First(&br, model.Brand{ID: v.BrandID}).Error; e != nil { log.Error(e) hrp.Message = fmt.Sprintf("brand id =%d", v.BrandID) return } if e := s.d.DB.First(&w, model.Warehouse{ID: v.LocationID}).Error; e != nil { log.Errorln(v.LocationID, e) return } mp.PartBrand = br.BrandName mp.Warehouse = w.SapCode mp.Supplier = pv.SapCode if v.RowNumber > 0 { mp.RowNumber = v.RowNumber } else { mp.RowNumber = len(uses) - i } mp.PartCode = v.PartCode mp.PartName = v.PartName mp.Specification = v.Specification mp.ChangeCount = v.CheckoutNumber mp.UnitPrice = v.Price mp.TotalPrice = v.SumPrice proofs = append(proofs, *mp) } p.ProofPart = proofs hrp.Msg = "ok" hrp.Data = p hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber) return } // 退库冲销 func (s *Service) quitProofList(p *model.ProofReq) (hrp *http.Response) { hrp = new(http.Response) bp := new(model.BigPartquit) proofs := make([]model.ProofPart, 0) hrp.Code = 200 var e error if e = s.d.DB.First(bp, model.BigPartquit{UseForm: p.OrderNumber}).Error; e != nil { log.Error(e) hrp.Message = fmt.Sprintf("bigpartquit %d,%s", p.NumberType, p.OrderNumber) hrp.Msg = "fail" return } p.OrderNumberId = bp.ID p.ProofCode = bp.ProofCode p.ChargeDate = bp.QuitDate.Format("2006-01-02") p.ProofYear = bp.QuitDate.Format("2006") parts := make([]model.Partquit, 0) if e = s.d.DB.Where("bigId = ?", bp.ID).Where(" dflag != 1 ").Order("rowNumber,id desc ").Find(&parts).Error; e != nil || len(parts) == 0 { hrp.Message = fmt.Sprintf("partquit bigid = %d ", bp.ID) hrp.Msg = "fail" return } for i, v := range parts { mp := new(model.ProofPart) c := new(model.Contract) bc := new(model.BigContract) pv := new(model.Provider) if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil { log.Error(e) hrp.Message = fmt.Sprintf("contract id =%d", v.ContractID) return } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Error(e) hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID) return } if e := s.d.DB.First(&pv, model.Provider{ID: bc.ProviderID}).Error; e != nil { log.Errorln(bc.ProviderID, e) continue } mp.Supplier = pv.SapCode mp.PartBrand = v.Brand mp.Warehouse = "1004" if v.RowNumber > 0 { mp.RowNumber = v.RowNumber } else { mp.RowNumber = len(parts) - i } mp.PartCode = v.PartCode mp.PartName = v.PartName mp.Specification = v.Specification mp.ChangeCount = v.QuitNumber mp.UnitPrice = v.Price mp.TotalPrice = v.SumPrice proofs = append(proofs, *mp) } p.ProofPart = proofs hrp.Msg = "ok" hrp.Data = p hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber) return } // 柴油凭证冲销 func (s *Service) DieselOff(id int64) (hrp *http.Response) { r := new(http.ChargeOffReq) rp := new(http.SapResp) hrp = new(http.Response) hrp.Code = 200 hrp.Msg = "fail" p := new(model.ProofReq) tx := s.d.DB.Begin() diesel := new(model.Diesel) err := tx.Where("id = ? ", id).First(&diesel).Error if err != nil { hrp.Message += err.Error() tx.Rollback() return } r.Dest.DestID = "EQMAN" r.Dest.BussTp = "MM015" //r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM015/CancelMblnr" //r.Dest.Url = "http://192.168.61.117/SAPP0/Common/MM015/CancelMblnr" r.Dest.Url = fmt.Sprintf("%s/Common/MM015/CancelMblnr", config.Conf.Http.Routing) r.Data.ProofCode = diesel.ProofCode r.Data.ProofYear = diesel.SelTime[:4] r.Data.ChargeDate = diesel.SelTime r.Data.OrderNumber = diesel.DieselCode r.Data.HasTicket = util.ZeroStr(p.HasTicket == 1, "X") log.Infof("proofcode: %s", r.Data.ProofCode) sl := r.Data.RowsNumber sl = append(sl, http.ChargeOffDetail{RowNumber: int(diesel.Sort)}) dieselOff := &model.DieselOff{ Id: diesel.Id, PastureId: diesel.PastureId, DeptId: diesel.DeptId, EqId: diesel.EqId, EqCode: diesel.EqCode, OilcardId: diesel.OilcardId, CardNumber: diesel.CardNumber, OilClass: diesel.OilClass, OilAmount: diesel.OilAmount, NowPrice: diesel.NowPrice, Price: diesel.Price, EmpId: diesel.EmpId, InputId: diesel.InputId, SelTime: diesel.SelTime, Note: diesel.Note, OilType: diesel.OilType, OilName: diesel.OilName, DieselCode: diesel.DieselCode, Sort: diesel.Sort, ProofCode: diesel.ProofCode, } err = tx.Exec(`delete from diesel where id = ? `, id).Error if err != nil { hrp.Message += err.Error() tx.Rollback() return } err = tx.Create(&dieselOff).Error if err != nil { hrp.Message += err.Error() tx.Rollback() return } //err = tx.Exec(`update diesel set ProofCode = null where id = ? `, id).Error //if err != nil { // hrp.Message += err.Error() // tx.Rollback() // return //} r.Data.RowsNumber = sl rbyte, _ := json.Marshal(r) //rbyte, _ := json.Marshal(r) fmt.Println(string(rbyte)) e := s.SyncSap(r, rp, rbyte) pp.Print(r) if e == nil { if rp.Data.Status == "S" { hrp.Message = fmt.Sprintf("sap物料冲销成功:%d %s %s", p.NumberType, rp.Data.ProofNumber, rp.Data.EqNumber) log.Info(hrp.Message) } else { hrp.Message = fmt.Sprintf("sap凭证冲销失败:%s", rp.Dest.MessText) log.Infoln(hrp.Message, r, p) tx.Rollback() return } } else { hrp.Message = fmt.Sprintf("sap冲销接口报错:error:%s", e) log.Infoln(hrp.Message, r, p) tx.Rollback() return } tx.Commit() hrp.Msg = "ok" return } func (s *Service) zdDieselOff() { dieselList := make([]*model.Diesel, 0) s.d.DB.Where("dieselCode is not null ").Limit(500).Find(&dieselList) fmt.Println(len(dieselList)) for i, diesel := range dieselList { fmt.Println(i, diesel) s.DieselOff(diesel.Id) } for i, diesel := range dieselList { fmt.Println(i, diesel) } }