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.

368 lines
11 KiB

  1. package couponusers
  2. import (
  3. "CouponBackendGo/api/v1/couponusers"
  4. "CouponBackendGo/internal/dao"
  5. "CouponBackendGo/internal/service"
  6. "context"
  7. "database/sql"
  8. "errors"
  9. "github.com/gogf/gf/v2/frame/g"
  10. "github.com/gogf/gf/v2/os/gtime"
  11. "github.com/xuri/excelize/v2"
  12. "mime/multipart"
  13. "strconv"
  14. "strings"
  15. )
  16. type sCouponUsers struct{}
  17. func init() {
  18. service.RegisterCouponUsers(&sCouponUsers{})
  19. }
  20. // 根据优惠券id查看拥有优惠券的用户
  21. func (s *sCouponUsers) GetCouponUsersByCondition(ctx context.Context, couponId, jwcode int, name string, pageNo, pageSize int) (users []couponusers.GetCouponUsersRes, err error) {
  22. //没有条件查询时,分页
  23. if jwcode == 0 && name == "" {
  24. /*查询所有数据*/
  25. //在coupon_users中查询出对应的jwcode
  26. err = dao.CouponUsers.Ctx(ctx).Fields("jwcode").
  27. Where("coupon_id = ", couponId).Page(pageNo, pageSize).Scan(&users)
  28. if err != nil {
  29. return nil, errors.New("根据卡券id搜索jwcode失败")
  30. }
  31. //根据jwcode查询用户信息
  32. for i, userInfo := range users {
  33. err = dao.MemberInfo.Ctx(ctx).Fields("jwcode", "name", "deptName", "shopName").
  34. Where("jwcode = ?", userInfo.Jwcode).Scan(&users[i])
  35. if err != nil {
  36. return nil, errors.New("根据jwcode搜索用户信息失败")
  37. }
  38. }
  39. /*查询所有数据*/
  40. return users, err
  41. } else { //有条件查询时,不在分页,全查后筛选
  42. /*查询所有数据*/
  43. //在coupon_users中查询出对应的jwcode
  44. err = dao.CouponUsers.Ctx(ctx).Fields("jwcode").
  45. Where("coupon_id = ", couponId).Scan(&users)
  46. if err != nil {
  47. return nil, errors.New("根据卡券id搜索jwcode失败")
  48. }
  49. //根据jwcode查询用户信息
  50. for i, userInfo := range users {
  51. err = dao.MemberInfo.Ctx(ctx).Fields("jwcode", "name", "deptName", "shopName").
  52. Where("jwcode = ?", userInfo.Jwcode).Scan(&users[i])
  53. if err != nil {
  54. return nil, errors.New("根据jwcode搜索用户信息失败")
  55. }
  56. }
  57. /*查询所有数据*/
  58. //条件查询
  59. if jwcode != 0 && name == "" {
  60. var result []couponusers.GetCouponUsersRes
  61. for _, user := range users {
  62. if user.Jwcode == jwcode {
  63. result = append(result, user)
  64. }
  65. }
  66. return result, err
  67. } else if jwcode == 0 && name != "" {
  68. var result []couponusers.GetCouponUsersRes
  69. for _, user := range users {
  70. if strings.Contains(user.Name, name) {
  71. result = append(result, user)
  72. }
  73. }
  74. return result, err
  75. } else if jwcode != 0 && name != "" {
  76. var result []couponusers.GetCouponUsersRes
  77. for _, user := range users {
  78. if user.Jwcode == jwcode && strings.Contains(user.Name, name) {
  79. result = append(result, user)
  80. }
  81. }
  82. return result, err
  83. }
  84. return users, err
  85. }
  86. }
  87. // 根据jwcode,优惠券id删除用户
  88. func (s *sCouponUsers) DeleteCouponUserByJwcode(ctx context.Context, couponId, jwcode int) (result sql.Result, err error) {
  89. if jwcode == 0 {
  90. return nil, errors.New("jwcode不能为空")
  91. }
  92. if couponId == 0 {
  93. return nil, errors.New("couponId不能为空")
  94. }
  95. result, err = dao.CouponUsers.Ctx(ctx).Where("coupon_id = ?", couponId).Where("jwcode = ?", jwcode).Delete()
  96. if err != nil {
  97. return nil, errors.New("删除用户失败")
  98. }
  99. return result, err
  100. }
  101. // 通过excel导入精网号
  102. func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, err error) {
  103. //打开文件并返回一个excelize.File对象,用于读取和操作Excel文件的内容
  104. f, err := excelize.OpenReader(file)
  105. if err != nil {
  106. return nil, errors.New("打开文件失败")
  107. }
  108. // 延时关闭文件
  109. defer func(f *excelize.File) {
  110. err := f.Close()
  111. if err != nil {
  112. return // 返回错误
  113. }
  114. }(f)
  115. //读取所有工作表名称
  116. GetSheetMap := f.GetSheetMap()
  117. if len(GetSheetMap) == 0 {
  118. return nil, errors.New("没有工作表")
  119. }
  120. rows, err := f.GetRows("Sheet1")
  121. if err != nil {
  122. return nil, err
  123. }
  124. // 将每行的第一列转换为int,并添加到切片中
  125. for i, row := range rows {
  126. //跳过第一行
  127. if i == 0 {
  128. continue
  129. }
  130. // 假设jwcode在每行的第一列
  131. /*参数校验*/
  132. //参数校验,检查每行是否有足够数据
  133. if len(row) == 0 {
  134. continue // 跳过空行
  135. }
  136. //参数校验,检查jwcode是否为非空字符串
  137. jwcodeStr := row[0]
  138. if jwcodeStr == "" {
  139. continue // 跳过空行
  140. }
  141. //将字符串转换为整数
  142. jwcode, err := strconv.Atoi(jwcodeStr)
  143. if err != nil {
  144. return nil, errors.New("参数转换失败")
  145. }
  146. jwcodes = append(jwcodes, jwcode)
  147. /*参数校验*/
  148. }
  149. return //返回jwcodes切片
  150. }
  151. // 根据精网号发放用户优惠券
  152. func (s *sCouponUsers) InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num int, err error) {
  153. //去重
  154. m := make(map[int]bool)
  155. var uniqueJwcodes []int //存放去重后的精网号
  156. for _, jwcode := range jwcodes {
  157. if _, exist := m[jwcode]; !exist {
  158. m[jwcode] = true
  159. uniqueJwcodes = append(uniqueJwcodes, jwcode)
  160. }
  161. }
  162. //插入数据
  163. for _, jwcode := range uniqueJwcodes {
  164. //检查数据库中是否存在
  165. count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count()
  166. if err != nil {
  167. return num, errors.New("检索数据库中是否已存在数据失败")
  168. }
  169. //不存在,可以插入
  170. if count == 0 {
  171. result, err := dao.CouponUsers.Ctx(ctx).Insert(g.Map{
  172. "jwcode": jwcode,
  173. "coupon_id": couponId,
  174. "time": gtime.Now().Unix(),
  175. })
  176. if err != nil {
  177. return num, errors.New("插入数据库失败")
  178. }
  179. //获取受影响的行数
  180. affected, err := result.RowsAffected()
  181. num += int(affected)
  182. }
  183. }
  184. return num, err //返回受影响的行数,即新增的条数
  185. }
  186. // 导入满足条件的用户jwcode到redis //不上传到redis了,上传到数据库表,到时候直接从表里查
  187. func (s *sCouponUsers) InsertJwcodesToRedisByExcel(file multipart.File) (err error) {
  188. var ctx g.Ctx
  189. //打开文件并返回一个excelize.File对象,用于读取和操作Excel文件的内容
  190. f, err := excelize.OpenReader(file)
  191. if err != nil {
  192. return errors.New("打开文件失败")
  193. }
  194. // 延时关闭文件
  195. defer func(f *excelize.File) {
  196. err := f.Close()
  197. if err != nil {
  198. // 处理关闭文件时的错误
  199. return
  200. }
  201. }(f)
  202. //读取所有工作表名称
  203. GetSheetMap := f.GetSheetMap()
  204. if len(GetSheetMap) == 0 {
  205. return errors.New("没有工作表")
  206. }
  207. // 读取第一个工作表
  208. rows, err := f.GetRows("Sheet1")
  209. if err != nil {
  210. return err
  211. }
  212. // 将每行的第一列转换为int,并添加到切片中
  213. //var jwcodes []int
  214. for i, row := range rows {
  215. //跳过第一行
  216. if i == 0 {
  217. continue
  218. }
  219. // 假设jwcode在每行的第一列
  220. /*参数校验*/
  221. //参数校验,检查每行是否有足够数据
  222. if len(row) == 0 {
  223. continue // 跳过空行
  224. }
  225. //参数校验,检查jwcode是否为非空字符串
  226. jwcodeStr := row[0]
  227. if jwcodeStr == "" {
  228. continue // 跳过空行
  229. }
  230. //将字符串转换为整数
  231. jwcode, err := strconv.Atoi(jwcodeStr)
  232. if err != nil {
  233. return errors.New("参数转换失败")
  234. }
  235. // 判断jwcode是否在表coupon_qualified_users中
  236. count, err := dao.CouponQualifiedUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Count()
  237. if err != nil {
  238. return errors.New("检索数据库中是否已存在数据失败")
  239. }
  240. if count == 0 { //不存在,插入
  241. _, err = dao.CouponQualifiedUsers.Ctx(ctx).Insert(g.Map{
  242. "jwcode": jwcode,
  243. })
  244. }
  245. //jwcodes = append(jwcodes, jwcode)
  246. /*参数校验*/
  247. }
  248. return //返回nil表示成功
  249. }
  250. // 判断某用户能否抽到卡券
  251. func (s *sCouponUsers) IsEligibleUser(ctx context.Context, jwcode int, couponIds []int) (img string, err error) {
  252. //从数据库中获取符合条件的精网号EligibleJwcodes
  253. var EligibleJwcodes []couponusers.InsertCouponUserRes
  254. err = dao.CouponQualifiedUsers.Ctx(ctx).Fields("jwcode").Scan(&EligibleJwcodes)
  255. if err != nil {
  256. return "", errors.New("从数据库中获取符合条件的精网号失败")
  257. }
  258. if len(EligibleJwcodes) == 0 {
  259. return "", errors.New("核验数据库(满足条件)中数据为空")
  260. }
  261. //检查jwcode是否在EligibleJwcodes中
  262. for _, EligibleJwcode := range EligibleJwcodes {
  263. if EligibleJwcode.Jwcode == jwcode {
  264. //存在,有资格,判断是否抽取过
  265. for _, couponId := range couponIds {
  266. count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count()
  267. if err != nil {
  268. return "", errors.New("检索数据库中是否已存在数据失败")
  269. }
  270. //有记录,抽取过
  271. if count > 0 {
  272. err = dao.CouponUsers.Ctx(ctx).Fields("img_url").Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Scan(&img)
  273. return img, errors.New("该用户已领取过该卡券")
  274. }
  275. }
  276. //所有的都没有记录,没有抽取过
  277. return "", nil
  278. }
  279. }
  280. return "", errors.New("该用户不满足领取条件")
  281. }
  282. // 给单个用户发放卡券
  283. func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId int) (err error) {
  284. //查看库中是否已经存在
  285. count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count()
  286. if err != nil {
  287. return errors.New("检索数据库中是否已存在数据失败")
  288. }
  289. //已存在 不添加,直接返回
  290. if count > 0 {
  291. return errors.New("该用户已领取该卡券")
  292. }
  293. //不存在 查看是否满足条件,满足就添加,不满足就返回
  294. //从数据库中获取符合条件的精网号EligibleJwcodes
  295. var EligibleJwcodes []couponusers.InsertCouponUserRes
  296. err = dao.CouponQualifiedUsers.Ctx(ctx).Fields("jwcode").Scan(&EligibleJwcodes)
  297. if err != nil {
  298. return errors.New("从数据库中获取符合条件的精网号失败")
  299. }
  300. if len(EligibleJwcodes) == 0 {
  301. return errors.New("核验数据库(满足条件)中数据为空")
  302. }
  303. //检查jwcode是否在EligibleJwcodes中
  304. for _, EligibleJwcode := range EligibleJwcodes {
  305. if EligibleJwcode.Jwcode == jwcode {
  306. //存在,可以插入
  307. _, err := dao.CouponUsers.Ctx(ctx).Insert(g.Map{
  308. "jwcode": jwcode,
  309. "coupon_id": couponId,
  310. "time": gtime.Now().Unix(),
  311. })
  312. if err != nil {
  313. return errors.New("插入数据库失败")
  314. }
  315. return err
  316. }
  317. }
  318. return errors.New("该用户精网号不符合领取条件") //遍历完了所有满足条件的用户,发现不在其中,不符合条件
  319. }
  320. /*未编写*/
  321. // 导出拥有卡券的用户列表
  322. func (s *sCouponUsers) ExportCouponUsers() {
  323. }
  324. // 添加用户选择武器记录
  325. func (s *sCouponUsers) AddRecord(ctx context.Context, jwcode int, id int, name string) (err error) {
  326. _, err = dao.CouponUsers.Ctx(ctx).Data(g.Map{
  327. "record": name,
  328. }).Where("jwcode = ? and coupon_id = ?", jwcode, id).Update()
  329. if err != nil {
  330. return errors.New("添加武器记录失败")
  331. }
  332. return
  333. }