|
@@ -0,0 +1,135 @@
|
|
|
+package data_manage
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "hongze/hongze_task/utils"
|
|
|
+ "rdluck_tools/orm"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+type lzSurveyData struct {
|
|
|
+ DataTime string `orm:"column(data_time)" description:"日期"`
|
|
|
+ InputValue string `orm:"column(input_value)" description:"值"`
|
|
|
+}
|
|
|
+
|
|
|
+func GetLzSurveyDataByTradeCode(condition string, pars []interface{}) (item []*lzSurveyData, err error) {
|
|
|
+ sql := ` SELECT a.* FROM longzhong_survey_data AS a
|
|
|
+ INNER JOIN longzhong_survey_product AS b ON a.survey_product_id=b.survey_product_id
|
|
|
+ WHERE 1=1 `
|
|
|
+ o := orm.NewOrm()
|
|
|
+ o.Using("edb")
|
|
|
+ if condition != "" {
|
|
|
+ sql += condition
|
|
|
+ }
|
|
|
+ sql += ` ORDER BY a.data_time DESC `
|
|
|
+ _, err = o.Raw(sql, pars).QueryRows(&item)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func GetEdbDataLzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
|
|
|
+ o := orm.NewOrm()
|
|
|
+ o.Using("data")
|
|
|
+ sql := ` SELECT COUNT(1) AS count FROM edb_data_lz WHERE edb_code=? AND data_time=? `
|
|
|
+ err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func ModifyEdbDataLz(edbInfoId int64, dataTime, value string) (err error) {
|
|
|
+ o := orm.NewOrm()
|
|
|
+ o.Using("data")
|
|
|
+ sql := ` UPDATE edb_data_lz SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
|
|
|
+ _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+//刷新隆众指标数据
|
|
|
+func RefreshEdbDataByLz(edbInfoId int, edbCode, startDate, endDate string) (err error) {
|
|
|
+ o := orm.NewOrm()
|
|
|
+ o.Using("data")
|
|
|
+ o.Begin()
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ o.Rollback()
|
|
|
+ } else {
|
|
|
+ o.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ edbInfoIdStr := strconv.Itoa(edbInfoId)
|
|
|
+ //计算数据
|
|
|
+ var condition string
|
|
|
+ var pars []interface{}
|
|
|
+
|
|
|
+ if edbCode != "" {
|
|
|
+ condition += " AND b.lz_code=? "
|
|
|
+ pars = append(pars, edbCode)
|
|
|
+ }
|
|
|
+
|
|
|
+ if startDate != "" {
|
|
|
+ condition += " AND a.data_time>=? "
|
|
|
+ pars = append(pars, startDate)
|
|
|
+ }
|
|
|
+
|
|
|
+ if endDate != "" {
|
|
|
+ condition += " AND a.data_time<=? "
|
|
|
+ pars = append(pars, endDate)
|
|
|
+ }
|
|
|
+
|
|
|
+ lzDataList, err := GetLzSurveyDataByTradeCode(condition, pars)
|
|
|
+
|
|
|
+ addSql := ` INSERT INTO edb_data_lz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
|
|
|
+ var isAdd bool
|
|
|
+ for _, v := range lzDataList {
|
|
|
+ item := v
|
|
|
+ count, err := GetEdbDataLzByCodeAndDate(edbCode, v.DataTime)
|
|
|
+ if err != nil && err.Error() != utils.ErrNoRow() {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if count <= 0 {
|
|
|
+ nowStr := time.Now().Format(utils.FormatDateTime)
|
|
|
+ eDate := item.DataTime
|
|
|
+ sValue := item.InputValue
|
|
|
+ dataTime, err := time.Parse(utils.FormatDate, eDate)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ timestamp := dataTime.UnixNano() / 1e6
|
|
|
+ timeStr := fmt.Sprintf("%d", timestamp)
|
|
|
+ addSql += "("
|
|
|
+ addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
|
|
|
+ "," + "'" + nowStr + "'" + "," + "1" + "," + "'" + timeStr + "'"
|
|
|
+ addSql += "),"
|
|
|
+ isAdd = true
|
|
|
+ } else {
|
|
|
+ err = ModifyEdbDataLz(int64(edbInfoId), v.DataTime, v.InputValue)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if isAdd {
|
|
|
+ addSql = strings.TrimRight(addSql, ",")
|
|
|
+ _, err = o.Raw(addSql).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(utils.DATA_SOURCE_LZ, edbCode)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if maxAndMinItem != nil {
|
|
|
+ err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|