SystemSignRewardServices.php 3.5 KB

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