TestPaperObtain.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\model\questions;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use service\UtilService as Util;
  15. use app\merchant\model\questions\Questions as QuestionsModel;
  16. use app\merchant\model\questions\TestPaper as TestPaperModel;
  17. use app\merchant\model\special\Special;
  18. use app\merchant\model\special\SpecialSource;
  19. /**
  20. * 获得试卷 Model
  21. * Class TestPaperObtain
  22. * @package app\admin\model\questions
  23. */
  24. class TestPaperObtain extends ModelBasic
  25. {
  26. use ModelTrait;
  27. /**给用户单独发送试卷
  28. * @param $uid
  29. * @param $data
  30. * @return bool
  31. */
  32. public static function addUidSend($uid, $data)
  33. {
  34. foreach ($data as $key => $value) {
  35. $type = TestPaperModel::where(['id' => $value])->value('type');
  36. $item['uid'] = $uid;
  37. $item['test_id'] = $value;
  38. $item['type'] = $type;
  39. if (self::be($item)) continue;
  40. $item['source'] = 3;
  41. $item['add_time'] = time();
  42. self::set($item);
  43. }
  44. return true;
  45. }
  46. /**购买专题获得试卷
  47. * @param $order_id
  48. * @param $uid
  49. * @param $special_id
  50. * @return bool
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @throws \think\exception\DbException
  54. */
  55. public static function setTestPaper($order_id, $uid, $special_id, $source)
  56. {
  57. if (!$uid || !$special_id) return false;
  58. $special = Special::PreWhere()->where(['id'=>$special_id])->find();
  59. if ($special['type'] == 5) {
  60. $special_source = SpecialSource::getSpecialSource($special['id']);
  61. if (!$special_source) return false;
  62. foreach ($special_source as $k => $v) {
  63. $task_special = Special::PreWhere()->where(['id'=>$v['source_id']])->find();
  64. if (!$task_special) continue;
  65. if ($task_special['is_show'] != 1) continue;
  66. $test_ids = Relation::setWhere(2, $task_special['id'])->column('relation_id');
  67. if (count($test_ids) <= 0) continue;
  68. foreach ($test_ids as $ks => $value) {
  69. self::setUserTestPaper($order_id, $value, $uid, 2, $source);
  70. }
  71. }
  72. }
  73. $test_ids = Relation::setWhere(2, $special_id)->column('relation_id');
  74. if (count($test_ids) <= 0) return false;
  75. foreach ($test_ids as $kf => $value) {
  76. self::setUserTestPaper($order_id, $value, $uid, 2, $source);
  77. }
  78. }
  79. /**记录用户获取试卷
  80. * @param $order_id
  81. * @param $test_id
  82. * @param $uid
  83. * @param $type
  84. * @param $source
  85. * @return object
  86. */
  87. public static function setUserTestPaper($order_id, $test_id, $uid, $type, $source)
  88. {
  89. $add_time = time();
  90. if (self::be(['uid' => $uid, 'test_id' => $test_id, 'type' => $type, 'is_del' => 0])) return false;
  91. return self::set(compact('order_id', 'test_id', 'uid', 'type', 'source', 'add_time'));
  92. }
  93. /**删除专题时清除试卷
  94. * @param $order_id
  95. * @param $uid
  96. * @param $special_id
  97. * @param $source
  98. * @return bool
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. * @throws \think\exception\DbException
  102. */
  103. public static function delTestPaper($order_id, $uid, $special_id, $source)
  104. {
  105. if (!$uid || !$special_id) return false;
  106. $special = Special::PreWhere()->where(['id'=>$special_id])->find();
  107. if ($special['type'] == SPECIAL_COLUMN) {
  108. $special_source = SpecialSource::getSpecialSource($special['id']);
  109. if (!$special_source) return false;
  110. foreach ($special_source as $k => $v) {
  111. $task_special = Special::PreWhere()->where(['id'=>$v['source_id']])->find();
  112. if (!$task_special) continue;
  113. if ($task_special['is_show'] != 1) continue;
  114. $test_ids = Relation::setWhere(2, $task_special['id'])->column('relation_id');
  115. if (count($test_ids) <= 0) continue;
  116. foreach ($test_ids as $ks => $value) {
  117. self::delUserTestPaper($order_id, $value, $uid, 2, $source);
  118. }
  119. }
  120. }
  121. $test_ids = Relation::setWhere(2, $special_id)->column('relation_id');
  122. if (count($test_ids) <= 0) return false;
  123. foreach ($test_ids as $kf => $value) {
  124. self::delUserTestPaper($order_id, $value, $uid, 2, $source);
  125. }
  126. }
  127. /**删除试卷
  128. * @param $order_id
  129. * @param $test_id
  130. * @param $uid
  131. * @param $type
  132. * @param $source
  133. * @return bool|object
  134. */
  135. public static function delUserTestPaper($order_id, $test_id, $uid, $type, $source)
  136. {
  137. if (!self::be(['order_id' => $order_id, 'uid' => $uid, 'test_id' => $test_id, 'type' => $type, 'is_del' => 0, 'source' => $source])) return false;
  138. return self::where(['order_id' => $order_id, 'uid' => $uid, 'test_id' => $test_id, 'is_del' => 0, 'type' => $type, 'source' => $source])->update(['is_del' => 1]);
  139. }
  140. }