广告平台(站长使用)
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.
 
 
 
 
 

277 lines
8.6 KiB

  1. package svc
  2. import (
  3. "applet/app/e"
  4. "applet/app/md"
  5. "applet/app/utils"
  6. "applet/app/utils/cache"
  7. db "code.fnuoos.com/zhimeng/model.git/src"
  8. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  9. "code.fnuoos.com/zhimeng/model.git/src/super/model"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "time"
  13. )
  14. func FinancialDynamicsMediumSave(c *gin.Context, req md.FinancialDynamicsMediumSaveReq) {
  15. // 加锁 防止并发提取
  16. mutexKey := fmt.Sprintf("%s:FinancialDynamicsMediumSave", c.GetString("mid"))
  17. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  18. if err != nil {
  19. e.OutErr(c, 400, err)
  20. return
  21. }
  22. if withdrawAvailable != "OK" {
  23. e.OutErr(c, 400, e.NewErr(400000, "请求过于频繁,请稍后再试"))
  24. return
  25. }
  26. engine := db.Db
  27. sess := engine.NewSession()
  28. defer sess.Close()
  29. sess.Begin()
  30. var data model.MediumFinancialDynamics
  31. data.CreateAt = time.Now().Format("2006-01-02 15:04:05")
  32. data.MediumId = utils.StrToInt(req.Uid)
  33. data.Amount = req.Amount
  34. data.Memo = req.Memo
  35. data.Uuid = utils.StrToInt(c.GetString("mid"))
  36. data.BusinessKind = utils.StrToInt(req.BusinessKind)
  37. data.Certificate = req.Certificate
  38. data.PayMethod = utils.StrToInt(req.PayMethod)
  39. data.PayTime = time.Now().Format("2006-01-02 15:04:05")
  40. data.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
  41. insert, _ := engine.Insert(&data)
  42. if insert == 0 {
  43. sess.Rollback()
  44. e.OutErr(c, 400, e.NewErr(400, "创建失败"))
  45. return
  46. }
  47. err = DealMediumAmount(sess, md.DealMediumAmount{
  48. Mid: c.GetString("mid"),
  49. Type: md.FinMediumFlowDirectionIncome,
  50. Kind: md.FinancialDynamicsKindForMediumFlow,
  51. OrdId: utils.IntToStr(data.Id),
  52. MediumId: utils.StrToInt(req.Uid),
  53. Amount: utils.StrToFloat64(req.Amount),
  54. Memo: md.FinancialDynamicsTitleKindForMediumFlow,
  55. })
  56. if err != nil {
  57. sess.Rollback()
  58. e.OutErr(c, 400, e.NewErr(400, "创建失败"))
  59. return
  60. }
  61. sess.Commit()
  62. e.OutSuc(c, "success", nil)
  63. return
  64. }
  65. func FinancialDynamicsMediumList(c *gin.Context, req md.FinancialDynamicsMediumListReq) md.FinancialDynamicsMediumListRes {
  66. engine := db.Db
  67. NewMediumFinancialDynamicsDb := implement.NewMediumFinancialDynamicsDb(engine)
  68. list, total, _ := NewMediumFinancialDynamicsDb.FindMediumFinancialDynamics(c.GetString("mid"), req.Uid, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  69. data := make([]md.FinancialDynamicsMediumListData, 0)
  70. if len(list) > 0 {
  71. for _, v := range list {
  72. var tmp = md.FinancialDynamicsMediumListData{
  73. Id: utils.IntToStr(v.Id),
  74. PayTime: v.PayTime,
  75. Amount: v.Amount,
  76. PayMethod: utils.IntToStr(v.PayMethod),
  77. Certificate: v.Certificate,
  78. Memo: v.Memo,
  79. BusinessKind: utils.IntToStr(v.BusinessKind),
  80. }
  81. medium := GetMediumInfo(c, v.MediumId)
  82. if medium != nil {
  83. tmp.Name = medium["name"]
  84. }
  85. data = append(data, tmp)
  86. }
  87. }
  88. user := make([]md.SelectData, 0)
  89. res := md.FinancialDynamicsMediumListRes{
  90. List: data,
  91. Total: total,
  92. PayMethod: md.PayMethod,
  93. BusinessKind: md.BusinessKind,
  94. }
  95. var userList []model.MediumList
  96. db.Db.Where("settlement_type=4").Find(&userList)
  97. for _, v := range userList {
  98. tmp := md.SelectData{
  99. Name: v.CompanyName,
  100. Value: utils.IntToStr(v.MediumId),
  101. }
  102. if v.CompanyAbbreviation != "" {
  103. tmp.Name = v.CompanyAbbreviation
  104. }
  105. user = append(user, tmp)
  106. }
  107. res.User = user
  108. return res
  109. }
  110. func FinancialDynamicsMediumTotal(c *gin.Context, req md.FinancialDynamicsMediumTotalReq) md.FinancialDynamicsMediumTotalRes {
  111. engine := db.Db
  112. sql := `select sum(amount) as amount,COUNT(*) as count from %s where %s`
  113. where := "uuid=" + c.GetString("mid")
  114. if req.Uid != "" {
  115. where += " and medium_id=" + req.Uid
  116. }
  117. sql1 := fmt.Sprintf(sql, "medium_list", where)
  118. res := md.FinancialDynamicsMediumTotalRes{
  119. BalanceAmount: "0.00",
  120. AllAmount: "0.00",
  121. UseAmount: "0.00",
  122. PayCount: "0",
  123. }
  124. nativeString, _ := db.QueryNativeString(engine, sql1)
  125. for _, v := range nativeString {
  126. if utils.StrToFloat64(v["amount"]) > 0 {
  127. res.BalanceAmount = v["amount"]
  128. }
  129. }
  130. sql2 := fmt.Sprintf(sql, "medium_financial_dynamics", where)
  131. nativeString2, _ := db.QueryNativeString(engine, sql2)
  132. for _, v := range nativeString2 {
  133. if utils.StrToFloat64(v["amount"]) > 0 {
  134. res.AllAmount = v["amount"]
  135. }
  136. if utils.StrToFloat64(v["count"]) > 0 {
  137. res.PayCount = v["count"]
  138. }
  139. }
  140. res.UseAmount = utils.Float64ToStr(utils.StrToFloat64(res.AllAmount) - utils.StrToFloat64(res.BalanceAmount))
  141. return res
  142. }
  143. func FinancialDynamicsAgentSave(c *gin.Context, req md.FinancialDynamicsAgentSaveReq) {
  144. // 加锁 防止并发提取
  145. mutexKey := fmt.Sprintf("%s:FinancialDynamicsAgentSave", c.GetString("mid"))
  146. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  147. if err != nil {
  148. e.OutErr(c, 400, err)
  149. return
  150. }
  151. if withdrawAvailable != "OK" {
  152. e.OutErr(c, 400, e.NewErr(400000, "请求过于频繁,请稍后再试"))
  153. return
  154. }
  155. engine := db.Db
  156. sess := engine.NewSession()
  157. defer sess.Close()
  158. sess.Begin()
  159. var data model.AgentFinancialDynamics
  160. data.CreateAt = time.Now().Format("2006-01-02 15:04:05")
  161. data.AgentId = utils.StrToInt(req.Uid)
  162. data.Amount = req.Amount
  163. data.Memo = req.Memo
  164. data.Uuid = utils.StrToInt(c.GetString("mid"))
  165. data.BusinessKind = utils.StrToInt(req.BusinessKind)
  166. data.Certificate = req.Certificate
  167. data.PayMethod = utils.StrToInt(req.PayMethod)
  168. data.PayTime = time.Now().Format("2006-01-02 15:04:05")
  169. data.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
  170. insert, _ := engine.Insert(&data)
  171. if insert == 0 {
  172. sess.Rollback()
  173. e.OutErr(c, 400, e.NewErr(400, "创建失败"))
  174. return
  175. }
  176. err = DealAgentAmount(sess, md.DealAgentAmount{
  177. Mid: c.GetString("mid"),
  178. Type: md.FinAgentFlowDirectionIncome,
  179. Kind: md.FinancialDynamicsKindForAgentFlow,
  180. OrdId: utils.IntToStr(data.Id),
  181. AgentId: utils.StrToInt(req.Uid),
  182. Amount: utils.StrToFloat64(req.Amount),
  183. Memo: md.FinancialDynamicsTitleKindForAgentFlow,
  184. })
  185. if err != nil {
  186. sess.Rollback()
  187. e.OutErr(c, 400, e.NewErr(400, "创建失败"))
  188. return
  189. }
  190. sess.Commit()
  191. e.OutSuc(c, "success", nil)
  192. return
  193. }
  194. func FinancialDynamicsAgentList(c *gin.Context, req md.FinancialDynamicsAgentListReq) md.FinancialDynamicsAgentListRes {
  195. engine := db.Db
  196. NewAgentFinancialDynamicsDb := implement.NewAgentFinancialDynamicsDb(engine)
  197. list, total, _ := NewAgentFinancialDynamicsDb.FindAgentFinancialDynamics(c.GetString("mid"), req.Uid, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  198. data := make([]md.FinancialDynamicsAgentListData, 0)
  199. if len(list) > 0 {
  200. for _, v := range list {
  201. var tmp = md.FinancialDynamicsAgentListData{
  202. Id: utils.IntToStr(v.Id),
  203. PayTime: v.PayTime,
  204. Amount: v.Amount,
  205. PayMethod: utils.IntToStr(v.PayMethod),
  206. Certificate: v.Certificate,
  207. Memo: v.Memo,
  208. BusinessKind: utils.IntToStr(v.BusinessKind),
  209. }
  210. agent := GetAgentInfo(c, v.AgentId)
  211. if agent != nil {
  212. tmp.Name = agent["name"]
  213. }
  214. data = append(data, tmp)
  215. }
  216. }
  217. user := make([]md.SelectData, 0)
  218. res := md.FinancialDynamicsAgentListRes{
  219. List: data,
  220. Total: total,
  221. PayMethod: md.PayMethod,
  222. BusinessKind: md.BusinessKind,
  223. }
  224. var userList []model.AgentList
  225. db.Db.Where("settlement_type=4").Find(&userList)
  226. for _, v := range userList {
  227. tmp := md.SelectData{
  228. Name: v.CompanyName,
  229. Value: utils.IntToStr(v.AgentId),
  230. }
  231. if v.CompanyAbbreviation != "" {
  232. tmp.Name = v.CompanyAbbreviation
  233. }
  234. user = append(user, tmp)
  235. }
  236. res.User = user
  237. return res
  238. }
  239. func FinancialDynamicsAgentTotal(c *gin.Context, req md.FinancialDynamicsAgentTotalReq) md.FinancialDynamicsAgentTotalRes {
  240. engine := db.Db
  241. sql := `select sum(amount) as amount,COUNT(*) as count from %s where %s`
  242. where := "uuid=" + c.GetString("mid")
  243. if req.Uid != "" {
  244. where += " and agent_id=" + req.Uid
  245. }
  246. sql1 := fmt.Sprintf(sql, "agent_list", where)
  247. res := md.FinancialDynamicsAgentTotalRes{
  248. BalanceAmount: "0.00",
  249. AllAmount: "0.00",
  250. UseAmount: "0.00",
  251. PayCount: "0",
  252. }
  253. nativeString, _ := db.QueryNativeString(engine, sql1)
  254. for _, v := range nativeString {
  255. if utils.StrToFloat64(v["amount"]) > 0 {
  256. res.BalanceAmount = v["amount"]
  257. }
  258. }
  259. sql2 := fmt.Sprintf(sql, "agent_financial_dynamics", where)
  260. nativeString2, _ := db.QueryNativeString(engine, sql2)
  261. for _, v := range nativeString2 {
  262. if utils.StrToFloat64(v["amount"]) > 0 {
  263. res.AllAmount = v["amount"]
  264. }
  265. if utils.StrToFloat64(v["count"]) > 0 {
  266. res.PayCount = v["count"]
  267. }
  268. }
  269. res.UseAmount = utils.Float64ToStr(utils.StrToFloat64(res.AllAmount) - utils.StrToFloat64(res.BalanceAmount))
  270. return res
  271. }