相关条件的统计
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

395 lines
16 KiB

  1. package zyos_go_condition_hdl
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db"
  4. "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db/model"
  5. zhios_condition_statistics_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/utils"
  6. zhios_condition_statistics_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/utils/logx"
  7. "fmt"
  8. "time"
  9. "xorm.io/xorm"
  10. )
  11. // 累计自购数
  12. func OwnBuyTotal(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  13. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Unix()
  14. etime := t.Unix()
  15. //联盟结算后的
  16. total, err := db.OrderListCountByUIDByOrderTypeByTime(engine, uid, 0, []string{"1", "2", "3", "5"}, stime, etime)
  17. if err != nil {
  18. zhios_condition_statistics_logx.Warn(err)
  19. return ""
  20. }
  21. mallStime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location())
  22. mallEtime := t
  23. //确认收货的才算
  24. mallTotal, err := db.MallOrdByTimeWithCount(engine, uid, 0, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"))
  25. if err != nil {
  26. zhios_condition_statistics_logx.Warn(err)
  27. return zhios_condition_statistics_utils.Int64ToStr(total)
  28. }
  29. total += mallTotal
  30. //确认收货的才算
  31. b2cTotal, err := db.B2cOrdByTimeWithCount(engine, uid, 0, []string{"3", "4"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"))
  32. if err != nil {
  33. zhios_condition_statistics_logx.Warn(err)
  34. return zhios_condition_statistics_utils.Int64ToStr(total)
  35. }
  36. total += b2cTotal
  37. return zhios_condition_statistics_utils.Int64ToStr(total)
  38. }
  39. // 累计已结算佣金
  40. func SettleOrderSum(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  41. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Unix()
  42. etime := t.Unix()
  43. // 用户分佣的订单
  44. idAmountMap, err := db.OrderRelateListByTimeByState(engine, uid, []string{"1", "2", "3", "5"}, stime, etime)
  45. if err != nil {
  46. zhios_condition_statistics_logx.Warn(err)
  47. return ""
  48. }
  49. var sum float64
  50. for _, item := range *idAmountMap {
  51. sum += item
  52. }
  53. mallStime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location())
  54. mallEtime := t
  55. _, mallSum, err := db.MallOrderRelateListByTimeByState(engine, uid, 3, mallStime, mallEtime)
  56. if err != nil {
  57. zhios_condition_statistics_logx.Warn(err)
  58. return zhios_condition_statistics_utils.Float64ToStr(sum)
  59. }
  60. sum += mallSum
  61. return zhios_condition_statistics_utils.Float64ToStr(sum)
  62. }
  63. func ExtendCount(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  64. // 累计直推人数
  65. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  66. etime := t.Format("2006-1-02 15:04:05")
  67. sqlTpl := `SELECT count(*) as count
  68. FROM user_relate as up
  69. LEFT JOIN user as u ON u.uid=up.uid
  70. WHERE %s and up.level=1;`
  71. condStr := fmt.Sprintf("up.parent_uid=%s AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), stime, etime)
  72. sql := fmt.Sprintf(sqlTpl, condStr)
  73. results, err := db.QueryNativeString(engine, sql)
  74. if err != nil {
  75. zhios_condition_statistics_logx.Warn(err)
  76. return ""
  77. }
  78. count := ""
  79. if len(results) > 0 {
  80. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  81. }
  82. return count
  83. }
  84. func TeamCount(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  85. // 累计团队有效直推人数
  86. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  87. etime := t.Format("2006-1-02 15:04:05")
  88. sqlTpl := `SELECT count(*) as count
  89. FROM user_relate as up
  90. LEFT JOIN user as u ON u.uid=up.uid
  91. LEFT JOIN user_profile as ups ON up.uid=ups.uid
  92. WHERE %s and up.level=1;`
  93. condStr := fmt.Sprintf("up.parent_uid=%s AND u.create_at>'%s' AND u.create_at<'%s' and ups.is_verify=%d", zhios_condition_statistics_utils.AnyToString(uid), stime, etime, 1)
  94. sql := fmt.Sprintf(sqlTpl, condStr)
  95. results, err := db.QueryNativeString(engine, sql)
  96. if err != nil {
  97. zhios_condition_statistics_logx.Warn(err)
  98. return ""
  99. }
  100. count := ""
  101. if len(results) > 0 {
  102. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  103. }
  104. return count
  105. }
  106. // 累计团队符合相应等级的人数
  107. func TeamByLvCount(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  108. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  109. etime := t.Format("2006-1-02 15:04:05")
  110. sqlTpl := `SELECT count(*) as count
  111. FROM user_relate as ur
  112. LEFT JOIN user as u ON u.uid=ur.uid
  113. WHERE %s;`
  114. condStr := fmt.Sprintf("ur.parent_uid=%s AND u.level='%s' AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), task["task_type_level_id"], stime, etime)
  115. sql := fmt.Sprintf(sqlTpl, condStr)
  116. results, err := db.QueryNativeString(engine, sql)
  117. if err != nil {
  118. zhios_condition_statistics_logx.Warn(err)
  119. return ""
  120. }
  121. count := ""
  122. if len(results) > 0 {
  123. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  124. }
  125. return count
  126. }
  127. // 累计直推符合相应等级的人数
  128. func ExtendByLvCount(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  129. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Format("2006-1-02 15:04:05")
  130. etime := t.Format("2006-1-02 15:04:05")
  131. sqlTpl := `SELECT count(*) as count
  132. FROM user_relate as ur
  133. LEFT JOIN user as u ON u.uid=ur.uid
  134. WHERE %s;`
  135. condStr := fmt.Sprintf("ur.parent_uid=%s AND ur.level=%s AND u.level='%s' AND u.create_at>'%s' AND u.create_at<'%s'", zhios_condition_statistics_utils.AnyToString(uid), "1", task["TaskTypeLevelId"], stime, etime)
  136. sql := fmt.Sprintf(sqlTpl, condStr)
  137. results, err := db.QueryNativeString(engine, sql)
  138. if err != nil {
  139. zhios_condition_statistics_logx.Warn(err)
  140. return ""
  141. }
  142. count := ""
  143. if len(results) > 0 {
  144. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  145. }
  146. return count
  147. }
  148. // 累计自购消费金额
  149. func OwnbuySum(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  150. var total float64 = 0
  151. var err error
  152. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "1"}) {
  153. stime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location()).Unix()
  154. etime := t.Unix()
  155. total, err = db.OrderListPaidPriceByUIDByOrderTypeByTime(engine, uid, 0, []string{"1", "2", "3", "5"}, stime, etime, 0)
  156. if err != nil {
  157. zhios_condition_statistics_logx.Warn(err)
  158. return ""
  159. }
  160. }
  161. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "2", "8", "9"}) {
  162. mallStime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location())
  163. mallEtime := t
  164. orderType := 0
  165. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"8", "9"}) {
  166. orderType = zhios_condition_statistics_utils.StrToInt(task["task_type_pvd"])
  167. }
  168. //确认收货的才算
  169. mallTotal, err := db.MallOrdByTime(engine, uid, orderType, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"), "", 0)
  170. if err != nil {
  171. zhios_condition_statistics_logx.Warn(err)
  172. return zhios_condition_statistics_utils.Float64ToStr(total)
  173. }
  174. total += mallTotal
  175. }
  176. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "3"}) {
  177. mallStime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location())
  178. mallEtime := t
  179. //确认收货的才算
  180. o2oTotal, err := db.O2oOrdByTime(engine, uid, 0, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"), 0)
  181. if err != nil {
  182. zhios_condition_statistics_logx.Warn(err)
  183. return zhios_condition_statistics_utils.Float64ToStr(total)
  184. }
  185. total += o2oTotal
  186. o2oTotal1, err := db.O2oOrdPayToMerchantByTime(engine, uid, 0, "1", mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"), 0)
  187. if err != nil {
  188. zhios_condition_statistics_logx.Warn(err)
  189. return zhios_condition_statistics_utils.Float64ToStr(total)
  190. }
  191. total += o2oTotal1
  192. }
  193. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "4"}) {
  194. mallStime := time.Date(t.Year(), t.Month(), t.Day()-zhios_condition_statistics_utils.StrToInt(task["within_days"]), t.Hour(), 0, 0, 0, t.Location())
  195. mallEtime := t
  196. //确认收货的才算
  197. b2cTotal, err := db.B2cOrdByTime(engine, uid, 0, []string{"3"}, mallStime.Format("2006-01-02 15:04:05"), mallEtime.Format("2006-01-02 15:04:05"), 0)
  198. if err != nil {
  199. zhios_condition_statistics_logx.Warn(err)
  200. return zhios_condition_statistics_utils.Float64ToStr(total)
  201. }
  202. total += b2cTotal
  203. }
  204. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2))
  205. }
  206. //累计团队消费金额
  207. func TeamBuySum(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  208. var total float64 = 0
  209. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "2", "8", "9"}) {
  210. sqlTpl := `SELECT SUM(ol.cost_price) AS amount
  211. FROM mall_ord_list_relate olr
  212. JOIN mall_ord ol ON olr.oid = ol.ord_id
  213. WHERE olr.uid = ?
  214. AND ol.state =3 and olr.level>0 ?;
  215. `
  216. str := ""
  217. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"8", "9"}) {
  218. str += " and ol.order_type=" + task["task_type_pvd"]
  219. }
  220. result, err := db.QueryNativeString(engine, sqlTpl, uid, str)
  221. if err == nil {
  222. total += zhios_condition_statistics_utils.StrToFloat64(result[0]["amount"])
  223. }
  224. }
  225. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "3"}) {
  226. o2oSqlTpl := `SELECT SUM(ol.cost_price) AS amount
  227. FROM o2o_ord_list_relate olr
  228. JOIN o2o_ord ol ON olr.oid = ol.ord_id
  229. WHERE olr.uid = ?
  230. AND ol.state =3 and olr.level>0;
  231. `
  232. o2oResult, err := db.QueryNativeString(engine, o2oSqlTpl, uid)
  233. if err == nil {
  234. total += zhios_condition_statistics_utils.StrToFloat64(o2oResult[0]["amount"])
  235. }
  236. }
  237. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "4"}) {
  238. b2cSqlTpl := `SELECT SUM(ol.cost_price) AS amount
  239. FROM b2c_ord_list_relate olr
  240. JOIN b2c_ord ol ON olr.oid = ol.ord_id
  241. WHERE olr.uid = ?
  242. AND ol.state =3 and olr.level>0;
  243. `
  244. b2cResult, err := db.QueryNativeString(engine, b2cSqlTpl, uid)
  245. if err == nil {
  246. total += zhios_condition_statistics_utils.StrToFloat64(b2cResult[0]["amount"])
  247. }
  248. }
  249. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"0", "1"}) {
  250. guideSqlTpl := `SELECT SUM(ol.paid_price) AS amount
  251. FROM ord_list_relate olr
  252. JOIN ord_list ol ON olr.oid = ol.ord_id
  253. WHERE olr.uid = ?
  254. AND ol.state in(1,2,3,5) and olr.level>0;
  255. `
  256. guideResult, err := db.QueryNativeString(engine, guideSqlTpl, uid)
  257. if err == nil {
  258. total += zhios_condition_statistics_utils.StrToFloat64(guideResult[0]["amount"])
  259. }
  260. }
  261. return zhios_condition_statistics_utils.Float64ToStr(zhios_condition_statistics_utils.FloatFormat(total, 2))
  262. }
  263. //小市场团队符合条件人数
  264. func SmallTeamSum(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  265. //小市场团队符合条件人数
  266. smallUid := TotalSmallTeam(engine, zhios_condition_statistics_utils.AnyToString(uid))
  267. sqlTpl := `SELECT count(*) as count
  268. FROM user_relate as ur
  269. LEFT JOIN user as u ON u.uid=ur.uid
  270. WHERE %s;`
  271. condStr := fmt.Sprintf("ur.parent_uid=%s AND u.level='%s' ", smallUid, task["task_type_level_id"])
  272. sql := fmt.Sprintf(sqlTpl, condStr)
  273. results, err := db.QueryNativeString(engine, sql)
  274. if err != nil {
  275. zhios_condition_statistics_logx.Warn(err)
  276. return ""
  277. }
  278. count := ""
  279. if len(results) > 0 {
  280. count = zhios_condition_statistics_utils.AnyToString(results[0]["count"])
  281. }
  282. return count
  283. }
  284. func ExtendStoreCount(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time) string {
  285. if zhios_condition_statistics_utils.StrToInt(task["task_type_count"]) == 0 {
  286. return "0"
  287. }
  288. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"1"}) { //o2o
  289. sqlTpl := `SELECT os.id
  290. FROM user_relate ur
  291. LEFT JOIN o2o_merchant om ON ur.uid = om.uid
  292. LEFT JOIN o2o_store os on om.id=os.store_manager
  293. WHERE ur.parent_uid = ?
  294. AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?;
  295. `
  296. var ids = make([]string, 0)
  297. result, err := db.QueryNativeString(engine, sqlTpl, uid, task["task_type_count"])
  298. if err == nil {
  299. for _, v := range result {
  300. ids = append(ids, v["id"])
  301. }
  302. }
  303. if len(ids) > 0 {
  304. sum, _ := engine.In("belong_store_id", ids).In("state", []string{"3", "4"}).Sum(&model.O2oOrd{}, "cost_price")
  305. sum1, _ := engine.In("belong_store_id", ids).Sum(&model.O2oPayToMerchant{}, "actual_pay_amount")
  306. return zhios_condition_statistics_utils.Float64ToStrByPrec(sum+sum1, 4)
  307. }
  308. }
  309. if zhios_condition_statistics_utils.InArr(task["task_type_pvd"], []string{"2"}) { //b2c
  310. sqlTpl := `SELECT os.id
  311. FROM user_relate ur
  312. LEFT JOIN o2o_merchant om ON ur.uid = om.uid
  313. LEFT JOIN b2c_ord os on om.id=os.store_manager
  314. WHERE ur.parent_uid = ?
  315. AND ur.level =1 ORDER BY ur.invite_time asc LIMIT 0,?;
  316. `
  317. var ids = make([]string, 0)
  318. result, err := db.QueryNativeString(engine, sqlTpl, uid, task["task_type_count"])
  319. if err == nil {
  320. for _, v := range result {
  321. ids = append(ids, v["id"])
  322. }
  323. }
  324. if len(ids) > 0 {
  325. sum, _ := engine.In("belong_store_id", ids).In("state", []string{"4"}).Sum(&model.B2cOrd{}, "cost_price")
  326. return zhios_condition_statistics_utils.Float64ToStrByPrec(sum, 4)
  327. }
  328. }
  329. return "0"
  330. }
  331. func PayData(engine *xorm.Engine, uid interface{}, task map[string]string, t time.Time, hasPay bool, payOrdId int64) (string, bool, int64) {
  332. hasPay = true
  333. //TODO::根据 RegionalAgentSchemeTask 表中的 task_id 判断是否完成付费任务
  334. regionalAgentUserOrd, err := db.UserLevelOrderByTaskId(engine, zhios_condition_statistics_utils.AnyToString(uid), task["id"])
  335. if err != nil {
  336. _ = zhios_condition_statistics_logx.Warn(err)
  337. return "", hasPay, payOrdId
  338. }
  339. if regionalAgentUserOrd != nil {
  340. if regionalAgentUserOrd.ExpireAt.Unix() < t.Unix() && regionalAgentUserOrd.CreateAt.Format("2006-01-02 15:04:05") != regionalAgentUserOrd.ExpireAt.Format("2006-01-02 15:04:05") && regionalAgentUserOrd.DateType != 4 {
  341. return "0", hasPay, payOrdId
  342. }
  343. payOrdId = regionalAgentUserOrd.Id
  344. return "1", hasPay, payOrdId
  345. } else {
  346. return "0", hasPay, payOrdId
  347. }
  348. return "1", hasPay, payOrdId
  349. }
  350. func TotalSmallTeam(eg *xorm.Engine, uid string) string {
  351. sqlTpl := `SELECT sum(cstp.team_all_ord_price)+sum(cstp.my_ord_price) as amount,u.uid
  352. FROM user_profile u
  353. LEFT JOIN more_mall_outstanding_achievement cstp ON (cstp.uid =u.uid)
  354. WHERE u.parent_uid=%s GROUP BY u.uid ORDER BY amount desc,u.uid asc`
  355. sql := fmt.Sprintf(sqlTpl, uid)
  356. results, err := db.QueryNativeString(eg, sql)
  357. if err != nil {
  358. return "0"
  359. }
  360. if len(results) > 1 {
  361. return results[1]["uid"]
  362. }
  363. return "0"
  364. }