123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- package data_manage
- import (
- "eta/eta_mobile/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "strconv"
- "strings"
- "time"
- )
- func AddEdbDataLzBySql(sqlStr string) (err error) {
- o := orm.NewOrmUsingDB("data")
- _, err = o.Raw(sqlStr).Exec()
- return
- }
- func GetEdbDataLzByEdbCode(edbCode string) (items []*EdbInfoSearchData, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_data_lz WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
- _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
- return
- }
- func GetEdbDataLzMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_lz WHERE edb_code=? `
- err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
- return
- }
- 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.NewOrmUsingDB("edb")
- if condition != "" {
- sql += condition
- }
- sql += ` ORDER BY a.data_time DESC `
- _, err = o.Raw(sql, pars).QueryRows(&item)
- return
- }
- func GetEdbDataLzByCode(edbCode string) (items []*EdbInfoSearchData, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_data_lz WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
- _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
- return
- }
- func GetEdbDataByLz(edbCode, startDate, endDate string) (searchItem *EdbInfoSearch, err error) {
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- var condition string
- var pars []interface{}
- if edbCode != "" {
- condition += " AND b.lz_code=? "
- pars = append(pars, edbCode)
- }
- lzDataList, err := GetLzSurveyDataByTradeCode(condition, pars)
- if err != nil {
- return
- }
- searchItem = new(EdbInfoSearch)
- searchItem.EdbCode = edbCode
- dataLen := len(lzDataList)
- if dataLen > 0 {
- var isAdd bool
- addSql := ` INSERT INTO edb_data_lz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
- nowStr := time.Now().Format(utils.FormatDateTime)
- dataList := make([]*EdbInfoSearchData, 0)
- for i := 0; i < dataLen; i++ {
- item := lzDataList[i]
- eDate := item.DataTime
- sValue := item.InputValue
- dataTime, err := time.Parse(utils.FormatDate, eDate)
- if err != nil {
- return nil, err
- }
- timestamp := dataTime.UnixNano() / 1e6
- timeStr := fmt.Sprintf("%d", timestamp)
- addSql += "("
- addSql += "0," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
- "," + "'" + nowStr + "'" + "," + "0" + "," + "'" + timeStr + "'"
- addSql += "),"
- isAdd = true
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- err = AddEdbDataLzBySql(addSql)
- if err != nil {
- utils.FileLogData.Info("AddEdbDataLzBySql Err:%s", err.Error())
- return
- }
- }
- dataList, err := GetEdbDataLzByCode(edbCode)
- if err != nil {
- utils.FileLogData.Info("GetEdbDataLzByCode Err:%s", err.Error())
- return searchItem, err
- }
- minDate, maxDate, err := GetEdbDataLzMaxOrMinDate(edbCode)
- if err != nil {
- return searchItem, err
- }
- searchItem.DataList = dataList
- searchItem.StartDate = minDate
- searchItem.EndDate = maxDate
- }
- if searchItem.DataList == nil {
- searchItem.DataList = make([]*EdbInfoSearchData, 0)
- }
- return
- }
- func ModifyEdbDataLzStatus(edbInfoId int64, edbCode string) (err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` UPDATE edb_data_lz SET edb_info_id=?, status=1,modify_time=NOW() WHERE edb_code=? `
- _, err = o.Raw(sql, edbInfoId, edbCode).Exec()
- return
- }
- func GetEdbDataLzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
- o := orm.NewOrmUsingDB("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.NewOrmUsingDB("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.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.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 = to.Raw(addSql).Exec()
- if err != nil {
- return err
- }
- }
- return
- }
- // 全部刷新隆众数据
- func RefreshAllEdbDataByLz(edbInfoId, source, subSource int, edbCode, startDate, endDate string) (err error) {
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.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)
- //获取指标所有数据
- dataList := make([]*EdbDataBase, 0)
- dataTableName := GetEdbDataTableName(source, subSource)
- sql := `SELECT * FROM %s WHERE edb_info_id=? `
- sql = fmt.Sprintf(sql, dataTableName)
- _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
- if err != nil {
- return err
- }
- dataMap := make(map[string]string)
- for _, v := range dataList {
- dataMap[v.DataTime] = v.Value
- }
- 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
- 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)
- saveValue := sValue
- if existVal, ok := dataMap[eDate]; !ok {
- addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
- isAdd = true
- } else {
- if existVal != saveValue {
- sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
- sql = fmt.Sprintf(sql, dataTableName)
- _, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
- if err != nil {
- return err
- }
- }
- }
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- _, err = to.Raw(addSql).Exec()
- if err != nil {
- return err
- }
- }
- return
- }
|