xuzhiheng 2 سال پیش
والد
کامیت
2fc1bb92e7
1فایلهای تغییر یافته به همراه36 افزوده شده و 18 حذف شده
  1. 36 18
      service/service.go

+ 36 - 18
service/service.go

@@ -21,32 +21,50 @@ import (
 )
 
 func AddUsePerson(this *entmanageapplication.AddUsePersonReq) *entmanageapplication.AddUsePersonResp {
-	timeStr, ok, id := time.Now().Format("2006-01-02 15:04:05"), false, int64(0)
-	for _, v := range strings.Split(this.EntUserId, ",") {
-		entUserId, _ := strconv.Atoi(v)
-		data := JyMysql.FindOne(EntnichePower, map[string]interface{}{"wait_empower_id": this.WaitEmpowerId, "ent_user_id": entUserId}, "", "")
-		if data != nil && len(*data) > 0 {
-			ok = JyMysql.Update(EntnichePower, map[string]interface{}{"wait_empower_id": this.WaitEmpowerId, "ent_user_id": this.EntUserId}, map[string]interface{}{"status": 1, "update_time": timeStr})
+	timeStr, ok, id, msg, code := time.Now().Format("2006-01-02 15:04:05"), false, int64(0), "", int64(0)
+	UserArr := strings.Split(this.EntUserId, ",")
+	sourceData := JyMysql.FindOne(EntnicheWaitEmpower, map[string]interface{}{"id": this.WaitEmpowerId}, "", "")
+	if sourceData != nil && len(*sourceData) > 0 {
+		empower_count := common.IntAll((*sourceData)["empower_count"])
+		use_count := common.IntAll((*sourceData)["use_count"])
+		if empower_count-use_count < len(UserArr) {
+			msg = "购买数量不足"
+			code = int64(1)
 		} else {
-			id = JyMysql.Insert(EntnichePower, map[string]interface{}{
-				"wait_empower_id": this.WaitEmpowerId,
-				"ent_id":          this.EntId,
-				"ent_user_id":     entUserId,
-				"status":          1,
-				"create_time":     timeStr,
-				"update_time":     timeStr,
-			})
+			for _, v := range UserArr {
+				entUserId, _ := strconv.Atoi(v)
+				data := JyMysql.FindOne(EntnichePower, map[string]interface{}{"wait_empower_id": this.WaitEmpowerId, "ent_user_id": entUserId}, "", "")
+				if data != nil && len(*data) > 0 {
+					dataStatus := common.IntAll((*data)["status"])
+					if dataStatus == -1 {
+						ok = JyMysql.Update(EntnichePower, map[string]interface{}{"wait_empower_id": this.WaitEmpowerId, "ent_user_id": this.EntUserId}, map[string]interface{}{"status": 1, "update_time": timeStr})
+					} else {
+						msg = "请勿重复添加"
+						code = int64(1)
+						break
+					}
+				} else {
+					id = JyMysql.Insert(EntnichePower, map[string]interface{}{
+						"wait_empower_id": this.WaitEmpowerId,
+						"ent_id":          this.EntId,
+						"ent_user_id":     entUserId,
+						"status":          1,
+						"create_time":     timeStr,
+						"update_time":     timeStr,
+					})
+				}
+				JyMysql.UpdateOrDeleteBySql(`update `+EntnicheWaitEmpower+` set use_count = use_count + 1 where id = ?`, this.WaitEmpowerId)
+				addPower(this.WaitEmpowerId, entUserId)
+			}
 		}
-		JyMysql.UpdateOrDeleteBySql(`update `+EntnicheWaitEmpower+` set use_count = use_count + 1 where id = ?`, this.WaitEmpowerId)
-		addPower(this.WaitEmpowerId, entUserId)
 	}
-
 	status := 0
 	if ok || id > 0 {
 		status = 1
 	}
 	return &entmanageapplication.AddUsePersonResp{
-		ErrorCode: 0,
+		ErrorCode: code,
+		ErrorMsg:  msg,
 		Data:      &entmanageapplication.AddUsePerson{Status: int64(status)},
 	}
 }