|
@@ -3087,14 +3087,13 @@ func UpdateFTdetail(c *gin.Context) {
|
|
|
IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ftid AND ftdetail.pastureid=fp.pastureid ),0)*ccountratio,
|
|
|
IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ftid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=0),0)*ccountratio +
|
|
|
IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ftid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=1),0)*ccount
|
|
|
- ),0) +fp.supplyweight
|
|
|
+ ),0) +ifnull(fp.supplyweight,0)
|
|
|
WHERE fp.pastureid = ? AND fp.ftid =?`, parammaps["pastureid"], parammaps["ftid"]).Execute()
|
|
|
|
|
|
_, err = tx.SQL(`UPDATE fpdetail fpd
|
|
|
INNER JOIN feedp fp
|
|
|
ON fpd.barid = fp.barid AND fpd.pastureid = fp.pastureid
|
|
|
- SET fpd.weight = fp.feedweight*fpd.tratio/100,
|
|
|
- ptsrate=fp.supplyweight/(fp.feedweight)
|
|
|
+ SET fpd.weight = fp.feedweight*fpd.tratio/100
|
|
|
WHERE fp.pastureid = ? AND fp.ftid =?`, parammaps["pastureid"], parammaps["ftid"]).Execute()
|
|
|
|
|
|
} else {
|
|
@@ -3110,14 +3109,13 @@ func UpdateFTdetail(c *gin.Context) {
|
|
|
IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ptsfid AND ftdetail.pastureid=fp.pastureid ),0)*ccountratio,
|
|
|
IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ptsfid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=0),0)*ccountratio +
|
|
|
IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ptsfid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=1),0)*ccount
|
|
|
- ),0) +fp.supplyweight
|
|
|
+ ),0) +ifnull(fp.supplyweight,0)
|
|
|
WHERE fp.pastureid = ? AND fp.ptsfid =?`, parammaps["pastureid"], parammaps["ftid"]).Execute()
|
|
|
|
|
|
_, err = tx.SQL(`UPDATE fpdetail fpd
|
|
|
INNER JOIN feedp fp
|
|
|
ON fpd.barid = fp.barid AND fpd.pastureid = fp.pastureid
|
|
|
- SET fpd.weight = fp.feedweight*fpd.tratio/100,
|
|
|
- ptsrate=fp.supplyweight/(fp.feedweight)
|
|
|
+ SET fpd.weight = fp.feedweight*fpd.tratio/100
|
|
|
WHERE fp.pastureid = ? AND fp.ptsfid =?`, parammaps["pastureid"], parammaps["ftid"]).Execute()
|
|
|
}
|
|
|
|
|
@@ -3889,3 +3887,708 @@ func ExitsChildrenQuery(fasion map[string]interface{}, tempValParam interface{},
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func FpdImportExcel(c *gin.Context) {
|
|
|
+ appG := app.Gin{C: c}
|
|
|
+
|
|
|
+ inputdata := ""
|
|
|
+ c.Request.ParseForm()
|
|
|
+
|
|
|
+ params := c.Request.Form
|
|
|
+ file, _, err := c.Request.FormFile("file")
|
|
|
+ if err != nil {
|
|
|
+ appG.Response(http.StatusOK, e.ERROR, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ sqlnamestrs := params.Get("name")
|
|
|
+ sheetname := params.Get("sheetname")
|
|
|
+ pastureid := params.Get("pastureid")
|
|
|
+
|
|
|
+ logging.Info("ImportExcel ", c.Keys, c.Request.RemoteAddr, sqlnamestrs)
|
|
|
+
|
|
|
+
|
|
|
+ requiredParams := params.Get("requiredParams")
|
|
|
+
|
|
|
+ dateParams := params.Get("dateParams")
|
|
|
+
|
|
|
+ numParams := params.Get("numParams")
|
|
|
+
|
|
|
+ dateParamslist := strings.Split(dateParams, ",")
|
|
|
+ requiredParamslist := strings.Split(requiredParams, ",")
|
|
|
+ numParamslist := strings.Split(numParams, ",")
|
|
|
+ queryData := make([]interface{}, 0)
|
|
|
+ sqlslist := strings.Split(sqlnamestrs, ",")
|
|
|
+ xlsx1, err := excelize.OpenReader(file)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ok := 0
|
|
|
+ error := 0
|
|
|
+ isdone := true
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+ oldFpdList, err := tx.SQL(` select ifnull(ROUND((1-ptsrate)*weight,2)-ptuse,"") fweight,fpd.id,fpd.barname,fpd.weight,fpd.times,fp.ftweight,fp.supplyweight from fpdetail fpd
|
|
|
+ join feedp fp on fp.barid = fpd.barid and fpd.pastureid = fp.pastureid where fpd.pastureid = ?`, pastureid).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, item := range oldFpdList {
|
|
|
+ fmt.Println(item)
|
|
|
+ }
|
|
|
+ rows, _ := xlsx1.GetRows(sheetname)
|
|
|
+
|
|
|
+ if len(rows) > 10000 {
|
|
|
+ rows = rows[:10000]
|
|
|
+ }
|
|
|
+
|
|
|
+ for i, row := range rows {
|
|
|
+ if i > 0 {
|
|
|
+
|
|
|
+ entry := make(map[string]interface{})
|
|
|
+ outentry := make(map[string]interface{})
|
|
|
+ for j, colCell := range rows[0] {
|
|
|
+ for k, colCell1 := range row {
|
|
|
+ if j == k {
|
|
|
+ outentry[colCell] = colCell1
|
|
|
+ entry[colCell] = colCell1
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ inputdata = ""
|
|
|
+ a := ""
|
|
|
+
|
|
|
+
|
|
|
+ for _, v := range dateParamslist {
|
|
|
+ if v == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if d, ok := outentry[strings.Trim(v, " ")]; ok {
|
|
|
+ dtime, err := TransfTime(d.(string))
|
|
|
+ if err != nil {
|
|
|
+ a = strings.Trim(v, " ")
|
|
|
+ break
|
|
|
+ }
|
|
|
+ outentry[strings.Trim(v, " ")] = dtime
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if a != "" {
|
|
|
+ outentry["error_msg"] = a + " 格式错误"
|
|
|
+ error++
|
|
|
+ queryData = append(queryData, outentry)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ a = ""
|
|
|
+
|
|
|
+ for _, v := range requiredParamslist {
|
|
|
+ if v == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ if d, ok := outentry[strings.Trim(v, " ")]; !ok || d == "" {
|
|
|
+ a = strings.Trim(v, " ")
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if a != "" {
|
|
|
+ outentry["error_msg"] = a + " 未填或格式错误"
|
|
|
+ error++
|
|
|
+ queryData = append(queryData, outentry)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ a = ""
|
|
|
+
|
|
|
+ for _, v := range numParamslist {
|
|
|
+ if v == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ d, ok := outentry[strings.Trim(v, " ")]
|
|
|
+ if !ok || d == "" {
|
|
|
+ outentry[strings.Trim(v, " ")] = 0
|
|
|
+ } else {
|
|
|
+ var fl float64
|
|
|
+ switch d.(type) {
|
|
|
+ case string:
|
|
|
+ fl, err = strconv.ParseFloat(d.(string), 64)
|
|
|
+ }
|
|
|
+ if err != nil || fl < 0 {
|
|
|
+ a = strings.Trim(v, " ")
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if a != "" {
|
|
|
+ outentry["error_msg"] = a + " 为数值类型且不可为负数"
|
|
|
+ error++
|
|
|
+ queryData = append(queryData, outentry)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ errExist := false
|
|
|
+ for _, sqlnamestr := range sqlslist {
|
|
|
+ sql, paramslists := restful.GetSqlByNameDB(strings.Replace(sqlnamestr, " ", "", -1))
|
|
|
+ paramslist := strings.Split(paramslists, ",")
|
|
|
+ if len(paramslist) > 0 {
|
|
|
+ s_params := make([]interface{}, 0)
|
|
|
+
|
|
|
+ for _, value := range paramslist {
|
|
|
+ if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
|
|
|
+ if tempv, exists := c.Get("jwt_username"); exists {
|
|
|
+ s_params = append(s_params, tempv.(string))
|
|
|
+ } else {
|
|
|
+ s_params = append(s_params, "")
|
|
|
+ }
|
|
|
+ } else if strings.ToLower(strings.Trim(value, " ")) == "pastureid" {
|
|
|
+ s_params = append(s_params, pastureid)
|
|
|
+
|
|
|
+ } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
|
|
|
+ ids, err := setting.SnowIds.NextId()
|
|
|
+ if err != nil {
|
|
|
+ ids = time.Now().UnixNano()
|
|
|
+ logging.Info("create SnowIds err", err)
|
|
|
+ }
|
|
|
+ s_params = append(s_params, ids)
|
|
|
+ } else {
|
|
|
+ if outentry[strings.Trim(value, " ")] == nil {
|
|
|
+ s_params = append(s_params, "")
|
|
|
+ } else {
|
|
|
+
|
|
|
+ s_params = append(s_params, outentry[strings.Trim(value, " ")])
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ outentry["input"] = inputdata
|
|
|
+ returnmsgmap1, err := tx.SQL(sql, s_params...).Query().List()
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ errExist = true
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ msg := geterrmsg(err.Error())
|
|
|
+ outentry["error_msg"] = msg
|
|
|
+ error++
|
|
|
+ queryData = append(queryData, outentry)
|
|
|
+ isdone = true
|
|
|
+
|
|
|
+ break
|
|
|
+ } else {
|
|
|
+ if len(returnmsgmap1) > 0 {
|
|
|
+ if va, ok := returnmsgmap1[0]["msg"]; ok && va != "" {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ msg := geterrmsg(va.(string))
|
|
|
+ outentry["error_msg"] = msg
|
|
|
+ error++
|
|
|
+ queryData = append(queryData, outentry)
|
|
|
+ isdone = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ isdone = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !errExist {
|
|
|
+
|
|
|
+ dataexcel := make(map[string]interface{}, 0)
|
|
|
+ if _, ok := outentry["栏舍名称"]; ok {
|
|
|
+ dataexcel["barname"] = outentry["栏舍名称"]
|
|
|
+ }
|
|
|
+ if _, ok := outentry["实际牛头数"]; ok {
|
|
|
+ dataexcel["ccount"] = outentry["实际牛头数"]
|
|
|
+ }
|
|
|
+ if _, ok := outentry["系数(%)"]; ok {
|
|
|
+ dataexcel["ratio"] = outentry["系数(%)"]
|
|
|
+ }
|
|
|
+ if _, ok := outentry["配方模板"]; ok {
|
|
|
+ dataexcel["ftname"] = outentry["配方模板"]
|
|
|
+ }
|
|
|
+ if _, ok := outentry["补料配方"]; ok {
|
|
|
+ dataexcel["ptsfname"] = outentry["补料配方"]
|
|
|
+ }
|
|
|
+ if _, ok := outentry["第一班比例(%)"]; ok {
|
|
|
+ dataexcel["1"] = outentry["第一班比例(%)"]
|
|
|
+ }
|
|
|
+ if _, ok := outentry["第二班比例(%)"]; ok {
|
|
|
+ dataexcel["2"] = outentry["第二班比例(%)"]
|
|
|
+ }
|
|
|
+
|
|
|
+ if _, ok := outentry["第三班比例(%)"]; ok {
|
|
|
+ dataexcel["3"] = outentry["第三班比例(%)"]
|
|
|
+ }
|
|
|
+ if _, ok := outentry["第四班比例(%)"]; ok {
|
|
|
+ dataexcel["4"] = outentry["第四班比例(%)"]
|
|
|
+ }
|
|
|
+
|
|
|
+ exist, err := tx.SQL(` select id from feedp where barname = ? and pastureid = ? `, dataexcel["barname"], pastureid).Exist()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !exist {
|
|
|
+ _, err := tx.SQL(` insert into feedp(pastureid,barid,barname,ccount)values(?,(select id from bar where bname = ? and pastureid = ? ),?,?) `,
|
|
|
+ pastureid, dataexcel["barname"], pastureid, dataexcel["barname"], 0).Execute()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var feedpargs []interface{}
|
|
|
+ upFeedpSql := `update feedp set ccount = ? ,ratio = ? ,ccountratio = ?*(?*0.01) `
|
|
|
+ feedpargs = append(feedpargs, dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"])
|
|
|
+ if _, ok := dataexcel["ftname"]; ok {
|
|
|
+ feedpargs = append(feedpargs, dataexcel["ftname"], pastureid, dataexcel["ftname"])
|
|
|
+ upFeedpSql += ` , ftid = (select id from feedtemplet where tname = ? and pastureid = ? ),ftname = ? `
|
|
|
+
|
|
|
+ upFeedpSql += `,ftweight = (select sum(fweight) fweight from ftdetail
|
|
|
+ where ftid = (select id from feedtemplet where tname = ? and pastureid = ? )) * (?*(?*0.01)) `
|
|
|
+ feedpargs = append(feedpargs, dataexcel["ftname"], pastureid, dataexcel["ccount"], dataexcel["ratio"])
|
|
|
+ } else {
|
|
|
+ upFeedpSql += ` , ftid = -1,ftname = '' ,ftweight = 0 `
|
|
|
+ }
|
|
|
+
|
|
|
+ if _, ok := dataexcel["ptsfname"]; ok {
|
|
|
+ feedpargs = append(feedpargs, dataexcel["ptsfname"], pastureid, dataexcel["ptsfname"])
|
|
|
+ upFeedpSql += ` , ptsfid = (select id from feedtemplet where tname = ? and pastureid = ? ),ptsfname = ? `
|
|
|
+
|
|
|
+ upFeedpSql += `,supplyweight = (select sum(fweight) fweight from ftdetail
|
|
|
+ where ftid = (select id from feedtemplet where tname = ? and pastureid = ? )) * (?*(?*0.01))`
|
|
|
+ feedpargs = append(feedpargs, dataexcel["ptsfname"], pastureid, dataexcel["ccount"], dataexcel["ratio"])
|
|
|
+ } else {
|
|
|
+ upFeedpSql += ` , ptsfid = -1,ptsfname = '',supplyweight = 0 `
|
|
|
+ }
|
|
|
+ upFeedpSql += " where barname = ? and pastureid = ? "
|
|
|
+ feedpargs = append(feedpargs, dataexcel["barname"], pastureid)
|
|
|
+ _, err = tx.SQL(upFeedpSql, feedpargs...).Execute()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ exist1, err := tx.SQL(` select id from fpdetail where barname = ? and pastureid = ? `, dataexcel["barname"], pastureid).Exist()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !exist1 {
|
|
|
+ _, err := tx.SQL(` insert into fpdetail(pastureid,barid,barname,times,ptuse,ptsuse,ptsrate,ptsid,ptid)values(?,(select id from bar where bname = ? and pastureid = ? ),?,1,0,0,0,-1,-1) `,
|
|
|
+ pastureid, dataexcel["barname"], pastureid, dataexcel["barname"]).Execute()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = tx.SQL(` insert into fpdetail(pastureid,barid,barname,times,ptuse,ptsuse,ptsrate,ptsid,ptid)values(?,(select id from bar where bname = ? and pastureid = ? ),?,2,0,0,0,-1,-1) `,
|
|
|
+ pastureid, dataexcel["barname"], pastureid, dataexcel["barname"]).Execute()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = tx.SQL(` insert into fpdetail(pastureid,barid,barname,times,ptuse,ptsuse,ptsrate,ptsid,ptid)values(?,(select id from bar where bname = ? and pastureid = ? ),?,3,0,0,0,-1,-1) `,
|
|
|
+ pastureid, dataexcel["barname"], pastureid, dataexcel["barname"]).Execute()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = tx.SQL(` insert into fpdetail(pastureid,barid,barname,times,ptuse,ptsuse,ptsrate,ptsid,ptid)values(?,(select id from bar where bname = ? and pastureid = ? ),?,4,0,0,0,-1,-1) `,
|
|
|
+ pastureid, dataexcel["barname"], pastureid, dataexcel["barname"]).Execute()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ upFpdSql := ` update fpdetail set tratio = ? ,weight = (? *0.01) * ((select sum(fweight) fweight from ftdetail
|
|
|
+ where ftid = (select id from feedtemplet where tname = ? and pastureid = ? )) * (?*(?*0.01))) ,cowcount = ? ,
|
|
|
+ ccountradio= ?,ptid= (select id from feedtemplet where tname = ? and pastureid = ? ) where barname = ?
|
|
|
+ and pastureid = ? and times = ?`
|
|
|
+ var fpdargs1 []interface{}
|
|
|
+ fpdargs1 = append(fpdargs1, dataexcel["1"], dataexcel["1"], dataexcel["ftname"], pastureid,
|
|
|
+ dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"],
|
|
|
+ dataexcel["ftname"], pastureid, dataexcel["barname"], pastureid, 1)
|
|
|
+ _, err = tx.SQL(upFpdSql, fpdargs1...).Execute()
|
|
|
+ if err != nil {
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ var fpdargs2 []interface{}
|
|
|
+ fpdargs2 = append(fpdargs2, dataexcel["2"], dataexcel["2"], dataexcel["ftname"], pastureid,
|
|
|
+ dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"],
|
|
|
+ dataexcel["ftname"], pastureid, dataexcel["barname"], pastureid, 2)
|
|
|
+ _, err = tx.SQL(upFpdSql, fpdargs2...).Execute()
|
|
|
+ if err != nil {
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var fpdargs3 []interface{}
|
|
|
+ fpdargs3 = append(fpdargs3, dataexcel["3"], dataexcel["3"], dataexcel["ftname"], pastureid,
|
|
|
+ dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"],
|
|
|
+ dataexcel["ftname"], pastureid, dataexcel["barname"], pastureid, 3)
|
|
|
+ _, err = tx.SQL(upFpdSql, fpdargs3...).Execute()
|
|
|
+ if err != nil {
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var fpdargs4 []interface{}
|
|
|
+ fpdargs4 = append(fpdargs4, dataexcel["4"], dataexcel["4"], dataexcel["ftname"], pastureid,
|
|
|
+ dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"],
|
|
|
+ dataexcel["ftname"], pastureid, dataexcel["barname"], pastureid, 4)
|
|
|
+ _, err = tx.SQL(upFpdSql, fpdargs4...).Execute()
|
|
|
+ if err != nil {
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ newList, err := tx.SQL(` select ifnull(ROUND((1-ptsrate)*weight,2)-ptuse,"") fweight,id,ptid,weight,times from fpdetail where barname = ? and pastureid = ? `, dataexcel["barname"], pastureid).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ upFpdetailList := make([]*upFpdetail, 0)
|
|
|
+ for _, newfpd := range newList {
|
|
|
+ for _, oldfpd := range oldFpdList {
|
|
|
+ if oldfpd["id"].(int64) == newfpd["id"].(int64) {
|
|
|
+ var oldweight, newweight float64
|
|
|
+ if oldfpd["weight"] != nil {
|
|
|
+ oldweight, _ = strconv.ParseFloat(oldfpd["weight"].(string), 64)
|
|
|
+ }
|
|
|
+ if newfpd["weight"] != nil {
|
|
|
+ newweight, _ = strconv.ParseFloat(newfpd["weight"].(string), 64)
|
|
|
+ }
|
|
|
+ fweight, _ := strconv.ParseFloat(newfpd["fweight"].(string), 64)
|
|
|
+ upFpdetailList = append(upFpdetailList, &upFpdetail{
|
|
|
+ Old: oldweight,
|
|
|
+ New: newweight,
|
|
|
+ Times: newfpd["times"].(int64),
|
|
|
+ Ptid: newfpd["ptid"].(int64),
|
|
|
+ Fweight: fweight,
|
|
|
+ Id: oldfpd["id"].(int64),
|
|
|
+ })
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ updateFpdetailByBar(pastureid, dataexcel["barname"].(string), upFpdetailList)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if !isdone {
|
|
|
+ ok++
|
|
|
+ isdone = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ data := make(map[string]interface{})
|
|
|
+ data["result"] = queryData
|
|
|
+ data["success"] = ok
|
|
|
+ data["err_count"] = error
|
|
|
+ appG.Response(http.StatusOK, e.SUCCESS, data)
|
|
|
+}
|
|
|
+
|
|
|
+type upFpdetail struct {
|
|
|
+ Old float64
|
|
|
+ New float64
|
|
|
+ Times int64
|
|
|
+ Ptid int64
|
|
|
+ Fweight float64
|
|
|
+ Id int64
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func updateFpdetailByBar(pastureid, barname string, dataList []*upFpdetail) error {
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+ tx.Begin()
|
|
|
+ for _, data := range dataList {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ fttype := -1
|
|
|
+ if data.Ptid != -1 {
|
|
|
+ fttype = 1
|
|
|
+ }
|
|
|
+
|
|
|
+ var fweight float64
|
|
|
+ var lweight float64
|
|
|
+ lpplandtl1List := make([]*lpplandtlInfo, 0)
|
|
|
+ err := tx.SQL(`select lppland.*,lpp.maxweight from lpplan lpp
|
|
|
+ join lpplandtl1 lppland on lpp.id = lppland.lppid where lppland.fpdid =?
|
|
|
+ and lppland.fttype = ? and lpp.pastureid = ? and lpp.times = ? order by lpp.sort desc `,
|
|
|
+ data.Id, fttype, pastureid, data.Times).Find(&lpplandtl1List)
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ weight := data.Fweight
|
|
|
+
|
|
|
+
|
|
|
+ var w float64
|
|
|
+ var status int
|
|
|
+ if data.New < data.Old {
|
|
|
+ if weight >= 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ w = weight
|
|
|
+ status = 0
|
|
|
+ } else {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ w = data.New - data.Old
|
|
|
+ status = 1
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ for _, lppland := range lpplandtl1List {
|
|
|
+ var lppdata lpplandtlInfo
|
|
|
+ tx.SQL(`select sum(lweight) as lweight from lpplandtl1 lpp where lppid = ? and lpp.pastureid = ? `,
|
|
|
+ lppland.Lppid, pastureid).GetFirst(&lppdata)
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ lweight = lppdata.Lweight
|
|
|
+ if status == 1 {
|
|
|
+
|
|
|
+ if lppland.Maxweight <= lweight {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if w+lweight <= lppland.Maxweight {
|
|
|
+ lppland.Lweight += w
|
|
|
+ lweight += w
|
|
|
+ fweight += w
|
|
|
+ w = 0
|
|
|
+ } else {
|
|
|
+
|
|
|
+ w = w - (lppland.Maxweight - lweight)
|
|
|
+ n := (lppland.Maxweight - lweight)
|
|
|
+ lppland.Lweight = lppland.Lweight + n
|
|
|
+ lweight += n
|
|
|
+ fweight += n
|
|
|
+
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if lppland.Lweight == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if w+lppland.Lweight >= 0 {
|
|
|
+ lppland.Lweight = lppland.Lweight + w
|
|
|
+ lweight = lweight + w
|
|
|
+ fweight = fweight + w
|
|
|
+ w = 0
|
|
|
+ } else {
|
|
|
+ w = w + lppland.Lweight
|
|
|
+ lweight = lweight - lppland.Lweight
|
|
|
+ fweight = fweight - lppland.Lweight
|
|
|
+ lppland.Lweight = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = tx.SQL(`update lpplandtl1 set Lweight = ? where id = ? and pastureid = ?`, lppland.Lweight, lppland.Id, pastureid).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ _, err = tx.SQL(`UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),
|
|
|
+ ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
|
|
|
+ WHERE pastureid=? AND id=?`, fttype, fweight, fweight, fttype, fweight, fweight, pastureid, data.Id).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ err := tx.Commit()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ tx.Rollback()
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func DeleteFTdetail(c *gin.Context) {
|
|
|
+
|
|
|
+ appG := app.Gin{C: c}
|
|
|
+ dataByte, _ := ioutil.ReadAll(c.Request.Body)
|
|
|
+ fsion := gofasion.NewFasion(string(dataByte))
|
|
|
+ parammaps := fsion.Get("parammaps")
|
|
|
+ pastureid := parammaps.Get("pastureid").ValueStr()
|
|
|
+ ftid := parammaps.Get("ftid").ValueStr()
|
|
|
+
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+
|
|
|
+ tx.Begin()
|
|
|
+ _, err := tx.SQL(`delete from ftdetail where ftid = ? and pastureid = ? `, ftid, pastureid).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if parammaps.Get("type").ValueStr() == "1" {
|
|
|
+
|
|
|
+ _, err := tx.SQL(`UPDATE feedp fp
|
|
|
+ set
|
|
|
+ fp.ftweight= IFNULL(IF(
|
|
|
+ (SELECT sysopt.inforvalue FROM sysopt WHERE sysopt.pastureid= fp.pastureid AND sysopt.inforname='isLockCount')=0,
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ftid AND ftdetail.pastureid=fp.pastureid ),0)*ccountratio,
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ftid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=0),0)*ccountratio +
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ftid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=1),0)*ccount
|
|
|
+ ),0),fp.feedweight = IFNULL(IF(
|
|
|
+ (SELECT sysopt.inforvalue FROM sysopt WHERE sysopt.pastureid= fp.pastureid AND sysopt.inforname='isLockCount')=0,
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ftid AND ftdetail.pastureid=fp.pastureid ),0)*ccountratio,
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ftid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=0),0)*ccountratio +
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ftid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=1),0)*ccount
|
|
|
+ ),0) +ifnull(fp.supplyweight,0)
|
|
|
+ WHERE fp.pastureid = ? AND fp.ftid =?`, pastureid, ftid).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = tx.SQL(`
|
|
|
+ UPDATE fpdetail fpd
|
|
|
+ INNER JOIN feedp fp
|
|
|
+ ON fpd.barid = fp.barid AND fpd.pastureid = fp.pastureid
|
|
|
+ SET fpd.weight = fp.feedweight*fpd.tratio/100,
|
|
|
+ ptuse= 0
|
|
|
+ WHERE fp.pastureid = ? AND fp.ftid =?`, pastureid, ftid).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ _, err := tx.SQL(`UPDATE feedp fp
|
|
|
+ SET
|
|
|
+ fp.supplyweight= IFNULL(IF(
|
|
|
+ (SELECT sysopt.inforvalue FROM sysopt WHERE sysopt.pastureid= fp.pastureid AND sysopt.inforname='isLockCount')=0,
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ptsfid AND ftdetail.pastureid=fp.pastureid ),0)*ccountratio,
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ptsfid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=0),0)*ccountratio +
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ptsfid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=1),0)*ccount
|
|
|
+ ),0),fp.feedweight = IFNULL(IF(
|
|
|
+ (SELECT sysopt.inforvalue FROM sysopt WHERE sysopt.pastureid= fp.pastureid AND sysopt.inforname='isLockCount')=0,
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ptsfid AND ftdetail.pastureid=fp.pastureid ),0)*ccountratio,
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ptsfid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=0),0)*ccountratio +
|
|
|
+ IFNULL((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =fp.ptsfid AND ftdetail.pastureid=fp.pastureid AND ftdetail.islockcount=1),0)*ccount
|
|
|
+ ),0) + ifnull(fp.supplyweight,0)
|
|
|
+ WHERE fp.pastureid = ? AND fp.ptsfid =?`, pastureid, ftid).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = tx.SQL(`
|
|
|
+ UPDATE fpdetail fpd
|
|
|
+ INNER JOIN feedp fp
|
|
|
+ ON fpd.barid = fp.barid AND fpd.pastureid = fp.pastureid
|
|
|
+ SET fpd.weight = fp.feedweight*fpd.tratio/100,
|
|
|
+ ptsrate = 0
|
|
|
+ WHERE fp.pastureid = ? AND fp.ptsfid =?`, pastureid, ftid).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ _, err = tx.SQL(` update lpplandtl1 set lweight = 0 where lppid in(select id from lpplan where ftid = ? and pastureid = ? )`, ftid, pastureid).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = tx.Commit()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error(err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ appG.Response(http.StatusOK, e.SUCCESS, true)
|
|
|
+}
|