123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955 |
- package util
- import (
- "../logging"
- "bytes"
- "encoding/xml"
- "fmt"
- "github.com/pkg/errors"
- "github.com/robfig/cron"
- "io/ioutil"
- "log"
- "net/http"
- "strings"
- "time"
- "../../models"
- )
- //eas webservice 目前使用字符串拼接的方式模拟xml发送
- var (
- LoginStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:log="http://login.webservice.bos.kingdee.com">
- <soapenv:Header/>
- <soapenv:Body>
- <log:login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
- <userName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">userName?</userName>
- <password xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">password?</password>
- <slnName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">slnName?</slnName>
- <dcName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">dcName?</dcName>
- <language xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">language?</language>
- <dbType xsi:type="xsd:int">dbType?</dbType>
- </log:login>
- </soapenv:Body>
- </soapenv:Envelope>
- `
- LogoutStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:log="http://login.webservice.bos.kingdee.com">
- <soapenv:Header/>
- <soapenv:Body>
- <log:logout soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
- <userName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">userName?</userName>
- <slnName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">slnName?</slnName>
- <dcName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">dcName?</dcName>
- <language xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">language?</language>
- </log:logout>
- </soapenv:Body>
- </soapenv:Envelope>
- `
- InsertPartsStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.synfacade.modernfarming.eas.kingdee.com">
- <soapenv:Header>
- <ns1:SessionId
- xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
- </ns1:SessionId>
- </soapenv:Header>
- <soapenv:Body>
- <web:synMaterial soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
- <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">strJson?</request>
- </web:synMaterial>
- </soapenv:Body>
- </soapenv:Envelope>
- `
- QureyPartsStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.synfacade.modernfarming.eas.kingdee.com">
- <soapenv:Header>
- <ns1:SessionId
- xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
- </ns1:SessionId>
- </soapenv:Header>
- <soapenv:Body>
- <web:queryMaterialData soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
- <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">{"number":"strJson?"}</request>
- </web:queryMaterialData>
- </soapenv:Body>
- </soapenv:Envelope>`
- InsertPartsuselaidStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="http://app.ws.scm.eas.kingdee.com">
- <soapenv:Header>
- <ns1:SessionId
- xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
- </ns1:SessionId>
- </soapenv:Header>
- <soapenv:Body>
- <app:importBill soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
- <billType xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">billType?</billType>
- <billDataJSON xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">strJson?</billDataJSON>
- <billStatus xsi:type="xsd:int">2</billStatus>
- </app:importBill>
- </soapenv:Body>
- </soapenv:Envelope>
- `
- QureyBill = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="http://app.ws.scm.eas.kingdee.com">
- <soapenv:Header>
- <ns1:SessionId
- xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
- </ns1:SessionId>
- </soapenv:Header>
- <soapenv:Body>
- <app:queryScmBillData soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
- <billType xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">billType?</billType>
- <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">{"number":"strJson?"}</request>
- </app:queryScmBillData>
- </soapenv:Body>
- </soapenv:Envelope>`
- )
- var (
- singelChan chan struct{}
- lasttime time.Time
- sessionId string
- sessionidChan chan string
- )
- var user EASLoginInfo
- type EASLoginInfo struct {
- UserName string `json:"userName"`
- Password string `json:"password"`
- SlnName string `json:"slnName"`
- DcName string `json:"dcName"`
- Language string `json:"language"`
- DbType string `json:"dbType"`
- Url string `json:"dbType"`
- }
- type Response struct {
- Body rspBody `xml:"Body" json:"body"`
- }
- type rspBody struct {
- Record []rqRecord `xml:"multiRef" json:"multiRef"`
- SynMaterialResponse SMReturn `xml:"synMaterialResponse" json:"SynMaterialResponse"`
- ImportBillResponse ImportBillResponse `xml:"importBillResponse" json:"importBillResponse"`
- QueryMaterialDataResponse QureyParts `xml:"queryMaterialDataResponse" json:"queryMaterialDataResponse"`
- QueryScmBillDataResponse QureyBills `xml:"queryScmBillDataResponse" json:"queryScmBillDataResponse"`
- }
- type ImportBillResponse struct {
- ImportBillReturn string `xml:"importBillReturn" json:"importBillReturn"`
- }
- type QureyParts struct {
- QueryMaterialDataReturn string `xml:"queryMaterialDataReturn" json:"queryMaterialDataReturn"`
- }
- type QureyBills struct {
- QueryScmBillDataReturn string `xml:"queryScmBillDataReturn" json:"queryScmBillDataReturn"`
- }
- type SMReturn struct {
- SynMaterialReturn string `xml:"synMaterialReturn" json:"synMaterialReturn"`
- }
- type rqRecord struct {
- SessionId string `xml:"sessionId" json:"sessionId"`
- }
- func InitWebS(use, pass, url string) {
- user.Password = pass
- user.UserName = use
- user.DbType = "1"
- //user.DcName = "MASM" //正式环境是 XDMY ,备件先手动改为XDMY,后面全部切换正式环境后统一修改
- user.DcName = "XDMY" //正式环境是 XDMY ,备件先手动改为XDMY,后面全部切换正式环境后统一修改
- user.Language = "L2"
- user.SlnName = "eas"
- user.Url = url
- singelChan = make(chan struct{} ,100)
- sessionidChan = make(chan string ,100)
- lasttime = time.Now().Add(-6*time.Minute)
- go Runsessionid()
- }
- // sessionId 获取,本次请求与上次间隔5分钟以上 就重新登录
- func Runsessionid(){
- for {
- func() {
- defer func() {
- if err := recover();err != nil {
- logging.Error("Runsessionid recover err",err)
- }
- }()
- select {
- case <- singelChan:
- if time.Now().Sub(lasttime) > 5*time.Minute{ // 大于五分钟重新退出再登录
- println("重新登录")
- //EASWebServiceLogout()
- sd, err := EASWebServiceLogin("")
- if err != nil {
- sessionidChan <- "error: "+err.Error()
- }else{
- lasttime = time.Now()
- sessionId = sd
- sessionidChan <- sessionId
- }
- }else{
- sessionidChan <- sessionId
- }
- }
- }()
- }
- }
- //登录
- func EASWebServiceLogin(url string ) (string, error) {
- client := &http.Client{}
- resstr := ReplaceStrWebSevice("login", user.UserName, user.Password, user.SlnName, user.DcName, user.Language, user.DbType, "", "", "")
- if url == ""{
- url = user.Url
- }else{
- resstr = strings.ReplaceAll(resstr, "MASM", "XDMY") // dcname正式和测试环境不一致
- }
- body := bytes.NewBuffer([]byte(resstr))
- req, err := http.NewRequest("POST",
- url+"/ormrpc/services/EASLogin?wsdl",
- body,
- )
- req.Header.Set("Content-Type", "text/xml")
- req.Header.Set("SOAPAction", "application/soap+xml")
- if err != nil {
- return "", err
- }
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println(err)
- return "", err
- }
- defer resp.Body.Close()
- result_body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println(err)
- return "", err
- }
- v := Response{}
- err = xml.Unmarshal([]byte(string(result_body)), &v)
- if err != nil {
- fmt.Println(err.Error())
- }
- if len(v.Body.Record) > 0 {
- return v.Body.Record[0].SessionId, nil
- }
- return "", err
- }
- //注销
- func EASWebServiceLogout() error {
- client := &http.Client{}
- resstr := ReplaceStrWebSevice("logout", user.UserName, "", user.SlnName, user.DcName, user.Language, "", "", "", "")
- body := bytes.NewBuffer([]byte(resstr))
- req, err := http.NewRequest("POST",
- user.Url+"/ormrpc/services/EASLogin?wsdl",
- body,
- )
- req.Header.Set("Content-Type", "text/xml")
- req.Header.Set("SOAPAction", "application/soap+xml")
- if err != nil {
- // handle error
- }
- resp, err := client.Do(req)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- _, err = ioutil.ReadAll(resp.Body)
- if err != nil {
- return err
- }
- return nil
- //fmt.Println(string(result_body))
- }
- func EASWebServiceQureyParts(SessionId, PartCode string) error {
- client := &http.Client{}
- resstr := ReplaceStrWebSevice("QureyPartsStr", "", "", "", "", "", "", SessionId, PartCode, "")
- body := bytes.NewBuffer([]byte(resstr))
- req, err := http.NewRequest("POST",
- user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
- body,
- )
- req.Header.Set("Content-Type", "text/xml")
- req.Header.Set("SOAPAction", "application/soap+xml")
- if err != nil {
- // handle error
- }
- resp, err := client.Do(req)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- result_body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return err
- }
- v := Response{}
- err = xml.Unmarshal(result_body, &v)
- if err != nil {
- fmt.Println(err.Error())
- }
- resmap := make(map[string]interface{}, 0)
- err = json.Unmarshal([]byte(v.Body.QueryMaterialDataResponse.QueryMaterialDataReturn), &resmap)
- //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
- switch resmap["data"].(type) {
- case []interface{}:
- if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
- return errors.New("单据未查询到")
- }
- default:
- return errors.New("单据未查询到")
- }
- return nil
- }
- func EASWebServiceQureyBill(SessionId, BillCode,billType string) error {
- client := &http.Client{}
- resstr := ReplaceStrWebSevice("QureyBill", "", "", "", "", "", "", SessionId, BillCode, billType)
- body := bytes.NewBuffer([]byte(resstr))
- req, err := http.NewRequest("POST",
- user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
- body,
- )
- req.Header.Set("Content-Type", "text/xml")
- req.Header.Set("SOAPAction", "application/soap+xml")
- if err != nil {
- return err
- }
- resp, err := client.Do(req)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- result_body, err := ioutil.ReadAll(resp.Body)
- fmt.Println(string(result_body))
- if err != nil {
- fmt.Println(err)
- return err
- }
- v := Response{}
- err = xml.Unmarshal(result_body, &v)
- if err != nil {
- fmt.Println(err.Error())
- }
- resmap := make(map[string]interface{}, 0)
- err = json.Unmarshal([]byte(v.Body.QueryScmBillDataResponse.QueryScmBillDataReturn), &resmap)
- //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
- switch resmap["data"].(type) {
- case []interface{}:
- if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
- return errors.New("单据未查询到")
- }
- default:
- return errors.New("单据未查询到")
- }
- return nil
- }
- func EASWebServicInsertParts(SessionId string, parts Parts,url string) error {
- client := &http.Client{}
- soapencJson, err := json.Marshal(parts)
- if err != nil {
- return err
- }
- if url == ""{
- url = user.Url
- }
- println(string(soapencJson))
- resstr := ReplaceStrWebSevice("InsertParts", "", "", "", "", "", "", SessionId, string(soapencJson), "")
- body := bytes.NewBuffer([]byte(resstr))
- req, err := http.NewRequest("POST",
- url+"/ormrpc/services/WSSynMasterDataFacade?wsdl",
- body,
- )
- req.Header.Set("Content-Type", "text/xml")
- req.Header.Set("SOAPAction", "application/soap+xml")
- if err != nil {
- // handle error
- }
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println(err)
- return err
- }
- defer resp.Body.Close()
- result_body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println(err)
- return err
- }
- v := Response{}
- err = xml.Unmarshal(result_body, &v)
- if err != nil {
- fmt.Println(err.Error())
- }
- resmap := make(map[string]string, 0)
- err = json.Unmarshal([]byte(v.Body.SynMaterialResponse.SynMaterialReturn), &resmap)
- _,ok := resmap["success"]
- if !ok {
- return errors.New(string(result_body))
- }
- if resmap["success"] == "0" {
- return errors.New(resmap["message"])
- }
- return nil
- }
- // 登录系统写入数据
- func EASWebServicInsertPartsuselaid(SessionId, strJson, billType string) error { //billType采购入库单:006 销售出库单:007 其他入库单:008 其他出库单:009 领料出库单:100 生产入库单:101
- client := &http.Client{}
- resstr := ReplaceStrWebSevice("InsertPartsuselaidStr", "", "", "", "", "", "", SessionId, strJson, billType)
- body := bytes.NewBuffer([]byte(resstr))
- req, err := http.NewRequest("POST",
- user.Url+"/ormrpc/services/WSSCMWebServiceFacade?wsdl",
- body,
- )
- req.Header.Set("Content-Type", "text/xml")
- req.Header.Set("SOAPAction", "application/soap+xml")
- if err != nil {
- // handle error
- }
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println(err)
- return err
- }
- defer resp.Body.Close()
- result_body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println(err)
- return err
- }
- v := Response{}
- err = xml.Unmarshal(result_body, &v)
- if err != nil {
- fmt.Println(err.Error())
- }
- resmap := make(map[string]interface{}, 0)
- if v.Body.ImportBillResponse.ImportBillReturn != "" {
- err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
- fmt.Println(v.Body.ImportBillResponse.ImportBillReturn)
- //_,ok := resmap["billErrors"]
- //if !ok {
- // println(string(result_body))
- // return errors.New(string(result_body))
- //}
- _,ok := resmap["status"]
- if !ok {
- println(string(result_body))
- return errors.New(string(result_body))
- }
- //if len(resmap["billErrors"].([]interface{})) > 0 || resmap["status"] != "1" {
- if resmap["status"] != "0" {
- resmap := make(map[string]interface{}, 0)
- err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
- switch resmap["billErrors"].(type) {
- case []interface{}:
- if len(resmap["billErrors"].([]interface{}))>0{
- switch resmap["billErrors"].([]interface{})[0].(type) {
- case map[string]interface{} :
- str := resmap["billErrors"].([]interface{})[0].(map[string]interface{})["errorMsgs"]
- switch str.(type) {
- case []interface{}:
- if len(str.([]interface{})) >0 {
- s := str.([]interface{})[0]
- switch s.(type) {
- case string :
- return errors.New(s.(string))
- }
- }
- }
- }
- }
- }
- return errors.New(v.Body.ImportBillResponse.ImportBillReturn)
- }
- }
- return nil
- }
- //
- func ReplaceStrWebSevice(typeIn, userName, password, slnName, dcName, language, dbType, SessionId, strJson, billType string) string {
- res := ""
- switch typeIn {
- case "login":
- res = LoginStr
- res = strings.ReplaceAll(res, "userName?", userName)
- res = strings.ReplaceAll(res, "password?", password)
- res = strings.ReplaceAll(res, "slnName?", slnName)
- res = strings.ReplaceAll(res, "dcName?", dcName)
- res = strings.ReplaceAll(res, "language?", language)
- res = strings.ReplaceAll(res, "dbType?", dbType)
- case "logout":
- res = LogoutStr
- res = strings.ReplaceAll(res, "userName?", userName)
- res = strings.ReplaceAll(res, "slnName?", slnName)
- res = strings.ReplaceAll(res, "dcName?", dcName)
- res = strings.ReplaceAll(res, "language?", language)
- case "InsertParts":
- res = InsertPartsStr
- res = strings.ReplaceAll(res, "SessionId?", SessionId)
- res = strings.ReplaceAll(res, "strJson?", strJson)
- case "QureyPartsStr":
- res = QureyPartsStr
- res = strings.ReplaceAll(res, "SessionId?", SessionId)
- res = strings.ReplaceAll(res, "strJson?", strJson)
- case "InsertPartsuselaidStr":
- res = InsertPartsuselaidStr
- res = strings.ReplaceAll(res, "SessionId?", SessionId)
- res = strings.ReplaceAll(res, "strJson?", strJson)
- res = strings.ReplaceAll(res, "billType?", billType)
- case "QureyBill":
- res = QureyBill
- res = strings.ReplaceAll(res, "SessionId?", SessionId)
- res = strings.ReplaceAll(res, "strJson?", strJson)
- res = strings.ReplaceAll(res, "billType?", billType)
- }
- return res
- }
- func CronServer(){
- c := cron.New()
- log.Println("Starting CronWXwork...")
- _, err := c.AddFunc("0 1 1 * *", Pastures) // 每月1号1点
- _, err = c.AddFunc("1 1 1 * *", PartClass) // 每月1号1点1分
- if err != nil{
- log.Print("定时任务出错___",err.Error())
- }
- c.Start()
- }
- func Pastures(){
- //fmt.Println("Dingshi")
- tx := models.Engine.NewSession()
- err1 := tx.Begin()
- defer func() {
- switch {
- case err1 != nil:
- println("tx.Begin 事务启动失败__error:", err1.Error())
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err1 = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- queryPasture,err := tx.SQL("select sum(p_r.reportery) reportery, w2.pastureId,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime " +
- " from part_repertory p_r "+
- " left join warehouse w2 on w2.id = p_r.locationId "+
- " GROUP BY w2.pastureId ").Query().List()
- fmt.Println(queryPasture)
- if len(queryPasture)>0{
- for i:=0;i<len(queryPasture);i++{
- reportery:=queryPasture[i]["reportery"]
- pastureId:=queryPasture[i]["pastureId"]
- dateTime:=queryPasture[i]["dateTime"]
- _, err = tx.Exec("insert into inventory_temporary(currentInventory,pastureId,inventoryDate) VALUES(?,?,?)",reportery,pastureId,dateTime)
- }
- }
- go YuechuRuku()
- go Yuechuchuku()
- go Yuemochuku()
- go Yuemoruku()
- if err != nil{
- logging.Error("查询当前的不同牧场的库存__error:", err.Error())
- }
- }
- func PartClass(){
- tx := models.Engine.NewSession()
- err1 := tx.Begin()
- defer func() {
- switch {
- case err1 != nil:
- println("tx.Begin 事务启动失败__error:", err1.Error())
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err1 = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- queryPasture,err := tx.SQL("select sum(pr.reportery) reportery,ps.categoryId2,w2.pastureId,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime " +
- " from part_repertory pr "+
- " left join warehouse w2 on w2.id = pr.locationId "+
- " INNER JOIN parts ps ON ps.id = pr.partId " +
- " GROUP BY ps.categoryId2,w2.pastureId").Query().List()
- if len(queryPasture)>0{
- for i:=0;i<len(queryPasture);i++{
- reportery:=queryPasture[i]["reportery"]
- pastureId:=queryPasture[i]["pastureId"]
- categoryId2:=queryPasture[i]["categoryId2"]
- dateTime:=queryPasture[i]["dateTime"]
- _, err = tx.Exec("insert into inventory_temporary(currentInventory,pastureId,inventoryDate,partClassId) VALUES(?,?,?,?)",reportery,
- pastureId,dateTime,categoryId2)
- }
- }
- go YuechuPasture()
- go YuechuchukuPasture()
- go YuemochukuPasture()
- go YuemorukuPasture()
- if err !=nil{
- fmt.Println(err.Error())
- }
- }
- func YuechuRuku(){
- tx := models.Engine.NewSession()
- err1 := tx.Begin()
- defer func() {
- switch {
- case err1 != nil:
- println("tx.Begin 事务启动失败__error:", err1.Error())
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err1 = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- // 用于月初入库的备件数量根据牧场
- queryPartYcRK,err := tx.SQL("select sum(storageAmount) yuechusum,w1.pastureId,DATE_FORMAT(DATE_SUB(now()" +
- " ,INTERVAL 1 MONTH),'%Y-%m') dateTime from partlaid pl " +
- " inner JOIN bigpartlaid bpl ON bpl.`id` = pl.`bigId` left join warehouse w1 on w1.id = pl.locationId " +
- " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') " +
- " GROUP BY w1.pastureId ").Query().List()
- fmt.Println(queryPartYcRK)
- if len(queryPartYcRK)>0{
- for i:=0;i<len(queryPartYcRK);i++{
- yuechusum:=queryPartYcRK[i]["yuechusum"]
- pastureId:=queryPartYcRK[i]["pastureId"]
- dateTime:=queryPartYcRK[i]["dateTime"]
- _,err = tx.Exec("update inventory_temporary set beginningInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId is null",yuechusum,pastureId,dateTime)
- }
- }
- if err !=nil{
- fmt.Println(err.Error())
- }
- }
- func Yuechuchuku(){
- tx := models.Engine.NewSession()
- err1 := tx.Begin()
- defer func() {
- switch {
- case err1 != nil:
- println("tx.Begin 事务启动失败__error:", err1.Error())
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err1 = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- //// 用于月初出库的备件数量根据牧场
- queryPartYcCK,err := tx.SQL("select sum(pu.`checkoutNumber` - pu.`quitNumber`) yuechuchuku, bpu.pastureId," +
- " DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime from partuse pu "+
- " inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` " +
- " where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') ) "+
- " GROUP BY bpu.pastureId ").Query().List()
- fmt.Println(queryPartYcCK)
- if len(queryPartYcCK)>0{
- //fmt.Println("jinglaile")
- fmt.Printf("%T\n",queryPartYcCK[0]["yuechuchuku"])
- for i:=0;i<len(queryPartYcCK);i++{
- yuechuchuku:=queryPartYcCK[i]["yuechuchuku"]
- pastureId:=queryPartYcCK[i]["pastureId"]
- dateTime:=queryPartYcCK[i]["dateTime"]
- fmt.Println(pastureId)
- _,err = tx.Exec("update inventory_temporary set beginningInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId is null",yuechuchuku,pastureId,dateTime)
- }
- }
- if err !=nil{
- fmt.Println(err.Error())
- }
- }
- func Yuemochuku(){
- tx := models.Engine.NewSession()
- err1 := tx.Begin()
- defer func() {
- switch {
- case err1 != nil:
- println("tx.Begin 事务启动失败__error:", err1.Error())
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err1 = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- //// 用于月末出库的备件数量根据牧场
- queryPartYmCK,err := tx.SQL("select sum(pu.`checkoutNumber` - pu.`quitNumber`) yumochuku, bpu.pastureId ," +
- " DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime " +
- "from partuse pu "+
- " inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') ) "+
- " GROUP BY bpu.pastureId").Query().List()
- //
- if len(queryPartYmCK)>0{
- for i:=0;i<len(queryPartYmCK);i++{
- yumochuku :=queryPartYmCK[i]["yumochuku"]
- pastureId :=queryPartYmCK[i]["pastureId"]
- dateTime :=queryPartYmCK[i]["dateTime"]
- _,err = tx.Exec("update inventory_temporary set endInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId is null",yumochuku,pastureId,dateTime)
- }
- }
- if err !=nil{
- fmt.Println(err.Error())
- }
- }
- func Yuemoruku(){
- tx := models.Engine.NewSession()
- err1 := tx.Begin()
- defer func() {
- switch {
- case err1 != nil:
- println("tx.Begin 事务启动失败__error:", err1.Error())
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err1 = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- //// 用于月末入库的备件数量根据牧场
- queryPartYmRK,err := tx.SQL("select sum(storageAmount)yumoruku,w1.pastureId, DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') "+
- " from partlaid pl inner JOIN bigpartlaid bpl ON bpl.`id` = pl.`bigId` left join warehouse w1 on w1.id = pl.locationId "+
- " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') "+
- " GROUP BY w1.pastureId ").Query().List()
- //
- if len(queryPartYmRK)>0{
- for i:=0;i<len(queryPartYmRK);i++{
- yumoruku:=queryPartYmRK[i]["yumoruku"]
- pastureId:=queryPartYmRK[i]["pastureId"]
- dateTime:=queryPartYmRK[i]["dateTime"]
- _,err = tx.Exec("update inventory_temporary set endInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId is null ",yumoruku,pastureId,dateTime)
- }
- }
- if err !=nil{
- fmt.Println(err.Error())
- }
- }
- func YuechuPasture(){
- tx := models.Engine.NewSession()
- err1 := tx.Begin()
- defer func() {
- switch {
- case err1 != nil:
- println("tx.Begin 事务启动失败__error:", err1.Error())
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err1 = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- // 用于月初入库的备件数量根据牧场
- queryPartYcRK,err := tx.SQL(" select sum(storageAmount) storageAmount,p1.categoryId2,w1.pastureId,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime " +
- "from partlaid pl inner JOIN bigpartlaid bpl ON bpl.`id` = pl.`bigId` left join parts p1 on p1.id = pl.partId left join warehouse w1 " +
- " on w1.id = pl.locationId "+
- " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') "+
- " GROUP BY p1.categoryId2 , w1.pastureId ").Query().List()
- if len(queryPartYcRK)>0{
- for i:=0;i<len(queryPartYcRK);i++{
- storageAmount:=queryPartYcRK[i]["storageAmount"]
- pastureId:=queryPartYcRK[i]["pastureId"]
- dateTime:=queryPartYcRK[i]["dateTime"]
- categoryId2:=queryPartYcRK[i]["categoryId2"]
- _,err = tx.Exec("update inventory_temporary set beginningInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId =?",storageAmount,pastureId,dateTime,categoryId2)
- }
- }
- if err !=nil{
- fmt.Println(err.Error())
- }
- }
- func YuechuchukuPasture(){
- tx := models.Engine.NewSession()
- err1 := tx.Begin()
- defer func() {
- switch {
- case err1 != nil:
- println("tx.Begin 事务启动失败__error:", err1.Error())
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err1 = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- //// 用于月初出库的备件数量根据牧场
- queryPartYcCK,err := tx.SQL("select sum(pu.`checkoutNumber` - pu.`quitNumber`) yuechuchuku,bpu.pastureId, "+
- "p1.categoryId2,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime from partuse pu "+
- " inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` left join parts p1 on p1.id = pu.partId " +
- " where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') ) "+
- " GROUP BY bpu.pastureId,p1.categoryId2 ").Query().List()
- if len(queryPartYcCK)>0{
- //fmt.Println("jinglaile")
- //fmt.Printf("%T\n",queryPartYcCK[0]["yuechuchuku"])
- for i:=0;i<len(queryPartYcCK);i++{
- yuechuchuku:=queryPartYcCK[i]["yuechuchuku"]
- pastureId:=queryPartYcCK[i]["pastureId"]
- dateTime:=queryPartYcCK[i]["dateTime"]
- categoryId2:=queryPartYcCK[i]["categoryId2"]
- fmt.Println(pastureId)
- _,err = tx.Exec("update inventory_temporary set beginningInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId =?",yuechuchuku,pastureId,dateTime,categoryId2)
- }
- }
- if err !=nil{
- fmt.Println(err.Error())
- }
- }
- func YuemochukuPasture(){
- tx := models.Engine.NewSession()
- err1 := tx.Begin()
- defer func() {
- switch {
- case err1 != nil:
- println("tx.Begin 事务启动失败__error:", err1.Error())
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err1 = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- //// 用于月末出库的备件数量根据牧场
- queryPartYmCK,err := tx.SQL("select sum(pu.`checkoutNumber` - pu.`quitNumber`) yumochuku,bpu.pastureId, "+
- " p1.categoryId2,DATE_FORMAT(now(),'%Y-%m') dateTime "+
- " from partuse pu inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` left join parts p1 on p1.id = pu.partId "+
- " where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') ) "+
- "GROUP BY bpu.pastureId,p1.categoryId2 ").Query().List()
- if len(queryPartYmCK)>0{
- for i:=0;i<len(queryPartYmCK);i++{
- yumochuku :=queryPartYmCK[i]["yumochuku"]
- pastureId :=queryPartYmCK[i]["pastureId"]
- dateTime :=queryPartYmCK[i]["dateTime"]
- categoryId2:=queryPartYmCK[i]["categoryId2"]
- _,err = tx.Exec("update inventory_temporary set endInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId =?",yumochuku,pastureId,dateTime,categoryId2)
- }
- }
- if err !=nil{
- fmt.Println(err.Error())
- }
- }
- func YuemorukuPasture(){
- tx := models.Engine.NewSession()
- err1 := tx.Begin()
- defer func() {
- switch {
- case err1 != nil:
- println("tx.Begin 事务启动失败__error:", err1.Error())
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err1 = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- //// 用于月末入库的备件数量根据牧场
- queryPartYmRK,err := tx.SQL("select sum(storageAmount) yumoruku, p1.categoryId2,w1.pastureId , DATE_FORMAT(now(),'%Y-%m') dateTime" +
- " from partlaid pl inner JOIN bigpartlaid bpl "+
- " ON bpl.`id` = pl.`bigId` left join warehouse w1 on w1.id = pl.locationId left join parts p1 on p1.id = pl.partId "+
- " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') GROUP BY p1.categoryId2,w1.pastureId ").Query().List()
- //
- if len(queryPartYmRK)>0{
- for i:=0;i<len(queryPartYmRK);i++{
- yumoruku:=queryPartYmRK[i]["yumoruku"]
- pastureId:=queryPartYmRK[i]["pastureId"]
- dateTime:=queryPartYmRK[i]["dateTime"]
- categoryId2:=queryPartYmRK[i]["categoryId2"]
- _,err = tx.Exec("update inventory_temporary set endInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId=? ",yumoruku,pastureId,dateTime,categoryId2)
- }
- }
- if err !=nil{
- fmt.Println(err.Error())
- }
- }
|