SystemSignRewardServices.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use app\dao\system\SystemSignRewardDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\FormBuilder as Form;
  16. use think\facade\Route as Url;
  17. /**
  18. * @author: 吴汐
  19. * @email: 442384644@qq.com
  20. * @date: 2023/7/28
  21. */
  22. class SystemSignRewardServices extends BaseServices
  23. {
  24. /**
  25. * @param SystemSignRewardDao $dao
  26. */
  27. public function __construct(SystemSignRewardDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. /**
  32. * 签到奖励列表
  33. * @param int $type
  34. * @return array
  35. * @throws \ReflectionException
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @author: 吴汐
  40. * @email: 442384644@qq.com
  41. * @date: 2023/7/31
  42. */
  43. public function getList($type = 0)
  44. {
  45. [$page, $limit] = $this->getPageValue();
  46. $list = $this->dao->selectList(['type' => $type], '*', $page, $limit, 'days');
  47. $count = $this->dao->count(['type' => $type]);
  48. return compact('list', 'count');
  49. }
  50. /**
  51. * 新增修改签到奖励表单
  52. * @param int $id
  53. * @param int $type
  54. * @return array
  55. * @throws \FormBuilder\Exception\FormBuilderException
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. * @author: 吴汐
  60. * @email: 442384644@qq.com
  61. * @date: 2023/7/31
  62. */
  63. public function rewardsForm($id = 0, $type = 0)
  64. {
  65. $info = $this->dao->get($id);
  66. if ($info) $type = $info['type'];
  67. $form[] = Form::hidden('type', $type);
  68. $form[] = Form::number('days', $type == 1 ? '累积签到天数' : '连续签到天数', (int)($info['days'] ?? 0))->max(sys_config('sign_mode') == 1 ? 7 : 30);
  69. $form[] = Form::number('point', '赠送积分', (int)($info['point'] ?? 0))->controls(false)->max(999)->min(0);
  70. $form[] = Form::number('exp', '赠送经验', (int)($info['exp'] ?? 0))->controls(false)->max(999)->min(0);
  71. return create_form($type == 1 ? '累积签到奖励' : '连续签到奖励', $form, Url::buildUrl('/marketing/sign/save_rewards/' . $id), 'POST');
  72. }
  73. /**
  74. * 保存签到奖励
  75. * @param $id
  76. * @param $data
  77. * @return bool
  78. * @throws \ReflectionException
  79. * @author: 吴汐
  80. * @email: 442384644@qq.com
  81. * @date: 2023/8/10
  82. */
  83. public function saveRewards($id, $data)
  84. {
  85. if ($id) {
  86. $this->dao->update($id, $data);
  87. } else {
  88. if ($this->dao->count(['type' => $data['type'], 'days' => $data['days']])) {
  89. throw new AdminException('签到奖励已存在');
  90. } else {
  91. $this->dao->save($data);
  92. }
  93. }
  94. return true;
  95. }
  96. /**
  97. * 获取累积或者连续签到奖励数据
  98. * @param $type
  99. * @param $days
  100. * @return array|\think\Model|null
  101. * @throws \think\db\exception\DataNotFoundException
  102. * @throws \think\db\exception\DbException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. * @author: 吴汐
  105. * @email: 442384644@qq.com
  106. * @date: 2023/8/1
  107. */
  108. public function getSignRewards($type, $days)
  109. {
  110. $info = $this->dao->get(['type' => $type, 'days' => $days]);
  111. if ($info) return [true, $info['point'], $info['exp']];
  112. return [false, 0, 0];
  113. }
  114. }