123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- // 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/common.in/clinit"
- "adm-ads/consts"
- "adm-ads/model"
- "encoding/json"
- "gorm.io/gorm"
- "strings"
- )
- // 处理任务5的删除
- func handleTask11Delete(ads11 *model.Ads11) error {
- err := ads11.Delete(clinit.DB(),map[string]interface{}{"third_style_id":ads11.ThirdStyleId,"source":ads11.Source})
- if err == gorm.ErrRecordNotFound{
- return nil
- }
- return err
- }
- func supplementAds11(old, new *model.Ads11) bool {
- isSupple := false
- if old.ThirdBrandId == "" && new.ThirdBrandId != "" {
- isSupple = true
- old.ThirdBrandId = new.ThirdBrandId
- }
- if old.ThirdSeriesId == "" && new.ThirdSeriesId != "" {
- isSupple = true
- old.ThirdSeriesId = new.ThirdSeriesId
- }
- if old.ThirdBrandName == "" && new.ThirdBrandName != "" {
- isSupple = true
- old.ThirdBrandName = new.ThirdBrandName
- }
- if old.ThirdSeriesName == "" && new.ThirdSeriesName != "" {
- isSupple = true
- old.ThirdSeriesName = new.ThirdSeriesName
- }
- if old.ThirdStyleName == "" && new.ThirdStyleName != "" {
- isSupple = true
- old.ThirdStyleName = new.ThirdStyleName
- }
- if old.ThirdMaker == "" && new.ThirdMaker != "" {
- isSupple = true
- old.ThirdMaker = new.ThirdMaker
- }
- if old.ModelYear == "" && new.ModelYear != "" {
- isSupple = true
- old.ModelYear = new.ModelYear
- }
- if old.StyleId == "" && new.StyleId != "" {
- isSupple = true
- old.StyleId = new.StyleId
- }
- return isSupple
- }
- // 处理任务5的插入
- func handleTask11InsertOrUpdate(ads11 *model.Ads11)error{
- oldAds11 := &model.Ads11{}
- db := clinit.DB()
- err := oldAds11.Query(db,map[string]interface{}{"third_style_id":ads11.ThirdStyleId,"source":ads11.Source})
- if err != nil{
- if err == gorm.ErrRecordNotFound{
- err = ads11.Insert(db)
- if err != nil && !strings.Contains(err.Error(), "Duplicate") {
- return err
- }
- return nil
- }
- return err
- }else{
- isSupple := supplementAds11(oldAds11,ads11)
- if isSupple {
- oldAds11.UpdatedAt = ads11.UpdatedAt
- err = oldAds11.Update(db)
- if err != nil{
- return err
- }
- }else{
- return nil
- }
- }
- return nil
- }
- // 车型映射
- func Task11(adsMsg *apis.AdsMessage) (err error) {
- ads11 := &model.Ads11{}
- json.Unmarshal([]byte(adsMsg.Content),&ads11)
- if ads11.ThirdStyleId == ""{
- return nil
- }
- ads11.CreatedAt = adsMsg.Timestamp
- ads11.UpdatedAt = adsMsg.Timestamp
- if adsMsg.Action == consts.ACTIONDELETE{
- // 删除
- err = handleTask11Delete(ads11)
- }else{
- // 新增或插入
- err = handleTask11InsertOrUpdate(ads11)
- }
- return err
- }
|