瀏覽代碼

wip:公司部门id查询判断

wangkaiyue 1 年之前
父節點
當前提交
ac0b19b550
共有 1 個文件被更改,包括 45 次插入25 次删除
  1. 45 25
      service/balanceService.go

+ 45 - 25
service/balanceService.go

@@ -52,26 +52,31 @@ func (service *BalanceService) PurchaseUserBalance(balanceData *resourcesCenter.
 		}
 		remarks = string(jsonData)
 	}
-	if producMap[balanceData.ResourceType] != nil {
-		fool, err := orm.Table(AccountResources).
-			Select("*").
-			Where("accountId=? and  companyId=? and  departmentId=? and  ResourceType=? and  endTime=? and  sourceType=?",
-				balanceData.AccountId, balanceData.CompanyId, balanceData.DepartmentId, balanceData.ResourceType, balanceData.EndTime, userType).
-			Get(&balance)
-		if err != nil && !fool {
-			fmt.Println("结存查询失败:", err)
-			return entity.ErrorCode, "结存查询失败"
-		}
-	} else {
-		fool, err := orm.Table(AccountResources).
-			Select("*").
-			Where("accountId=? and  companyId=? and  departmentId=? and  ResourceType=? and  endTime=?  and  sourceType=?", balanceData.AccountId, balanceData.CompanyId, balanceData.DepartmentId, balanceData.ResourceType, balanceData.EndTime, userType).
-			Get(&balance)
-		if err != nil && !fool {
-			fmt.Println("结存查询失败:", err)
-			return entity.ErrorCode, "结存查询失败"
+
+	var (
+		sql = "accountId=? and  ResourceType=? and  endTime=? and  sourceType=? "
+		val = []interface{}{balanceData.AccountId, balanceData.ResourceType, balanceData.EndTime, userType}
+	)
+	//当传的有公司id和部门id时查新
+	if balanceData.CompanyId != 0 {
+		sql += " and  companyId=? "
+		val = append(val, balanceData.CompanyId)
+
+		if balanceData.CompanyId != 0 {
+			sql += " and  departmentId=? "
+			val = append(val, balanceData.DepartmentId)
 		}
 	}
+
+	fool, err := orm.Table(AccountResources).
+		Select("*").
+		Where(sql, val...).
+		Get(&balance)
+	if err != nil && !fool {
+		fmt.Println("结存查询失败:", err)
+		return entity.ErrorCode, "结存查询失败"
+	}
+
 	//新增流水记录
 	detailed := entity.Detailed{
 		AccountId:    detailedData.AccountId,
@@ -186,20 +191,35 @@ func (service *BalanceService) UseUserDetailed(duplicateRemoval int64, balanceDa
 	type SumStruct struct {
 		Age int `xorm:"age"`
 	}
-	sumStruct := "0"
-	now1 := time.Now()
-	nowTime := now1.Format("2006-01-02")
+
+	var (
+		sumStruct = "0"
+		nowTime   = time.Now().Format("2006-01-02")
+		sql       = "accountId=?  and  ResourceType=? and endTime>= ? "
+		val       = []interface{}{balanceData.AccountId, balanceData.ResourceType, nowTime}
+	)
+	//当传的有公司id和部门id时查新
+	if balanceData.CompanyId != 0 {
+		sql += " and  companyId=? "
+		val = append(val, balanceData.CompanyId)
+
+		if balanceData.CompanyId != 0 {
+			sql += " and  departmentId=? "
+			val = append(val, balanceData.DepartmentId)
+		}
+	}
+
 	if producMap[balanceData.ResourceType] != nil {
+		sql += " and  sourceType=? "
+		val = append(val, sourceTypeStr)
 		_, err = orm.Table(AccountResources).
 			Select("sum(number) as  number").
-			Where("accountId=? and  companyId=? and  departmentId=? and  ResourceType=? "+
-				"and endTime>= ? and sourceType !=?",
-				balanceData.AccountId, balanceData.CompanyId, balanceData.DepartmentId, balanceData.ResourceType, nowTime, sourceTypeStr).
+			Where(sql, val...).
 			Get(&sumStruct)
 	} else {
 		_, err = orm.Table(AccountResources).
 			Select("sum(number) as  number").
-			Where("accountId=? and  companyId=? and  departmentId=? and  ResourceType=? and  endTime>=?", balanceData.AccountId, balanceData.CompanyId, balanceData.DepartmentId, balanceData.ResourceType, time.Now().Format("2006-01-02")).
+			Where(sql, val...).
 			Get(&sumStruct)
 	}
 	if err != nil {