123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package task
- import (
- "adm-ads/apis"
- "adm-ads/consts"
- "adm-ads/model"
- "encoding/json"
- "strings"
- "adm-ads/common.in/clinit"
- "gorm.io/gorm"
- )
- // 处理任务6的删除
- func handleTask15Delete(ads15 *model.Ads15) error {
- err := ads15.Delete(clinit.DB(), map[string]interface{}{"vin": ads15.Vin})
- if err == gorm.ErrRecordNotFound {
- return nil
- }
- return err
- }
- func supplementAds15(old, new *model.Ads15) bool {
- isSupple := false
- if old.InsuranceFirstDate == "" && new.InsuranceFirstDate != "" {
- isSupple = true
- old.InsuranceFirstDate = new.InsuranceFirstDate
- } else if old.InsuranceFirstDate != "" && new.InsuranceFirstDate != "" {
- isSupple = true
- old.InsuranceFirstDate = new.InsuranceFirstDate
- }
- if old.Province == "" && new.Province != "" {
- isSupple = true
- old.Province = new.Province
- }
- if old.City == "" && new.City != "" {
- isSupple = true
- old.City = new.City
- }
- if old.District == "" && new.District != "" {
- isSupple = true
- old.District = new.District
- }
- if old.UsePropertyDetail == "" && new.UsePropertyDetail != "" {
- isSupple = true
- old.UsePropertyDetail = new.UsePropertyDetail
- }
- return isSupple
- }
- // 处理任务5的插入
- func handleTask15InsertOrUpdate(ads15 *model.Ads15) error {
- oldAds15 := &model.Ads15{}
- err := oldAds15.Query(clinit.DB(), map[string]interface{}{"vin": ads15.Vin})
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- err = ads15.Insert(clinit.DB())
- if err != nil && !strings.Contains(err.Error(), "Duplicate") {
- return err
- }
- return nil
- }
- return err
- } else {
- // 更新时间大于消息时间表示是后面的消息,不处理
- if oldAds15.UpdatedAt > ads15.UpdatedAt {
- return nil
- }
- isSupple := supplementAds15(oldAds15, ads15)
- if isSupple {
- oldAds15.UpdatedAt = ads15.UpdatedAt
- err = oldAds15.Update(clinit.DB())
- if err != nil {
- return err
- }
- } else {
- return nil
- }
- }
- return nil
- }
- // 交强险首保日期
- func Task15(adsMsg *apis.AdsMessage) (err error) {
- ads15 := &model.Ads15{}
- json.Unmarshal([]byte(adsMsg.Content), &ads15)
- if (ads15.Vin == "") || (ads15.InsuranceFirstDate == "" && ads15.UsePropertyDetail == "") {
- return nil
- }
- if ads15.InsuranceFirstDate != "" {
- ads15.InsuranceFirstDate = strings.Split(ads15.InsuranceFirstDate, " ")[0]
- }
- ads15.CreatedAt = adsMsg.Timestamp
- ads15.UpdatedAt = adsMsg.Timestamp
- if adsMsg.Action == consts.ACTIONDELETE {
- // 删除
- err = handleTask15Delete(ads15)
- } else {
- // 新增或插入
- err = handleTask15InsertOrUpdate(ads15)
- }
- return err
- }
|