123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // 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"
- "fmt"
- "go.uber.org/zap"
- "gorm.io/gorm"
- "strings"
- "time"
- )
- // 处理任务5的删除
- func handleTask14Delete(ads14 *model.Ads14) error {
- if ads14.C2Id == 0 {
- return nil
- }
- err := ads14.Delete(clinit.DB(),map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id})
- if err == gorm.ErrRecordNotFound{
- return nil
- }
- return err
- }
- // 处理任务14的插入
- func handleTask14InsertOrUpdate(ads14 *model.Ads14,db *gorm.DB) (err error){
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- l.Error("err",
- zap.String("run_task", err.Error()),
- zap.Stack("stacktrace"))
- }
- }()
- if ads14.FrontWheelSpecification != ""{
- oldAds14 := &model.Ads14{}
- ads14.C2Id = 1001
- ads14.AttributeRule = ads14.FrontWheelSpecification
- err = oldAds14.Query(db,map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id})
- if err != nil{
- if err == gorm.ErrRecordNotFound{
- err = ads14.Insert(db)
- if err != nil && !strings.Contains(err.Error(), "Duplicate") {
- return err
- }
- }else{
- return err
- }
- }
- }
- if ads14.BackWheelSpecification != ""{
- oldAds14 := &model.Ads14{}
- ads14.C2Id = 1002
- ads14.ID = 0
- ads14.AttributeRule = ads14.BackWheelSpecification
- err = oldAds14.Query(db,map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id})
- if err != nil{
- if err == gorm.ErrRecordNotFound{
- err = ads14.Insert(db)
- if err != nil && !strings.Contains(err.Error(), "Duplicate") {
- return err
- }
- }else{
- return err
- }
- }
- }
- /*if ads14.TyreSize != ""{
- ads14.C2Id = 1001
- ads14.AttributeRule = ads14.TyreSize
- err = oldAds14.Query(db,map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id})
- if err != nil{
- if err == gorm.ErrRecordNotFound{
- err = ads14.Insert(db)
- if err != nil && !strings.Contains(err.Error(), "Duplicate") {
- return err
- }
- }else{
- return err
- }
- }
- ads14.ID = 0
- ads14.C2Id = 1002
- oldAds14.ID = 0
- err = oldAds14.Query(db,map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id })
- if err != nil{
- if err == gorm.ErrRecordNotFound{
- err = ads14.Insert(db)
- if err != nil && !strings.Contains(err.Error(), "Duplicate") {
- return err
- }
- }else{
- return err
- }
- }
- }*/
- return nil
- }
- // 获取维保配件项目
- func Task14(adsMsg *apis.AdsMessage) (err error) {
- ads14 := &model.Ads14{}
- json.Unmarshal([]byte(adsMsg.Content),&ads14)
- if ads14.StyleId == ""{
- return nil
- }
- timeNow := time.Now().Unix()
- ads14.CreatedAt = timeNow
- ads14.UpdatedAt = timeNow
- if adsMsg.Action == consts.ACTIONDELETE{
- // 删除
- err = handleTask14Delete(ads14)
- }else{
- // 新增或插入
- db := clinit.DB().Begin()
- err = handleTask14InsertOrUpdate(ads14,db)
- if err == nil {
- db.Commit()
- }else{
- db.Rollback()
- }
- }
- return err
- }
|