DataDownloadBuy.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\admin\model\download;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use app\admin\model\questions\Relation;
  15. use app\admin\model\download\DataDownload;
  16. use app\admin\model\order\DataDownloadOrder;
  17. use app\admin\model\special\Special;
  18. use app\admin\model\special\SpecialSource;
  19. /**
  20. * 获得资料 Model
  21. */
  22. class DataDownloadBuy extends ModelBasic
  23. {
  24. use ModelTrait;
  25. protected function getAddTimeAttr($value)
  26. {
  27. return date('Y-m-d H:i:s', $value);
  28. }
  29. protected function getTypeAttr($value)
  30. {
  31. $name = '';
  32. switch ($value) {
  33. case 0:
  34. $name = '支付获得';
  35. break;
  36. case 1:
  37. $name = '领取获得';
  38. break;
  39. case 2:
  40. $name = '赠送获得';
  41. break;
  42. }
  43. return $name;
  44. }
  45. /**购买专题获得资料
  46. * @param $order_id
  47. * @param $uid
  48. * @param $special_id
  49. * @return bool
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. * @throws \think\exception\DbException
  53. */
  54. public static function setDataDownload($order_id, $uid, $special_id, $type = 0)
  55. {
  56. if (!$uid || !$special_id) return false;
  57. $special = Special::PreWhere()->where(['id'=>$special_id])->find();
  58. if ($special['type'] == 5) {
  59. $special_source = SpecialSource::getSpecialSource($special['id']);
  60. if (!$special_source) return false;
  61. foreach ($special_source as $k => $v) {
  62. $task_special = Special::PreWhere()->where(['id'=>$v['source_id']])->find();
  63. if (!$task_special) continue;
  64. if ($task_special['is_show'] != 1) continue;
  65. $data_ids = Relation::setWhere(4, $task_special['id'])->column('relation_id');
  66. if (count($data_ids) <= 0) continue;
  67. foreach ($data_ids as $ks => $value) {
  68. self::setUserDataDownload($order_id, $value, $uid, $type);
  69. }
  70. }
  71. } else {
  72. $data_ids = Relation::setWhere(4, $special_id)->column('relation_id');
  73. if (count($data_ids) <= 0) return false;
  74. foreach ($data_ids as $kf => $value) {
  75. self::setUserDataDownload($order_id, $value, $uid, $type);
  76. }
  77. }
  78. }
  79. /** 用户获得资料
  80. * @param $order_id
  81. * @param $data_id
  82. * @param $uid
  83. * @param $type
  84. * @return bool|object
  85. */
  86. public static function setUserDataDownload($order_id, $data_id, $uid, $type)
  87. {
  88. $add_time = time();
  89. if (self::be(['uid' => $uid, 'data_id' => $data_id, 'type' => $type, 'is_del' => 0])) return false;
  90. return self::set(compact('order_id', 'data_id', 'uid', 'type', 'add_time'));
  91. }
  92. /**删除专题时清除资料
  93. * @param $order_id
  94. * @param $uid
  95. * @param $special_id
  96. * @return bool
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. * @throws \think\exception\DbException
  100. */
  101. public static function delDataDownload($order_id, $uid, $special_id, $type)
  102. {
  103. if (!$uid || !$special_id) return false;
  104. $special = Special::PreWhere()->where(['id'=>$special_id])->find();
  105. if ($special['type'] == SPECIAL_COLUMN) {
  106. $special_source = SpecialSource::getSpecialSource($special['id']);
  107. if (!$special_source) return false;
  108. foreach ($special_source as $k => $v) {
  109. $task_special = Special::PreWhere()->where(['id'=>$v['source_id']])->find();
  110. if (!$task_special) continue;
  111. if ($task_special['is_show'] != 1) continue;
  112. $data_ids = Relation::setWhere(4, $task_special['id'])->column('relation_id');
  113. if (count($data_ids) <= 0) continue;
  114. foreach ($data_ids as $ks => $value) {
  115. self::delUserDataDownload($order_id, $value, $uid, $type);
  116. }
  117. }
  118. } else {
  119. $data_ids = Relation::setWhere(4, $special_id)->column('relation_id');
  120. if (count($data_ids) <= 0) return false;
  121. foreach ($data_ids as $kf => $value) {
  122. self::delUserDataDownload($order_id, $value, $uid, $type);
  123. }
  124. }
  125. }
  126. /** 清除资料
  127. * @param $order_id
  128. * @param $data_id
  129. * @param $uid
  130. * @param $type
  131. * @return bool|object
  132. */
  133. public static function delUserDataDownload($order_id, $data_id, $uid, $type)
  134. {
  135. if (!self::be(['order_id' => $order_id, 'uid' => $uid, 'data_id' => $data_id, 'type' => $type, 'is_del' => 0])) return false;
  136. return self::where(['order_id' => $order_id, 'uid' => $uid, 'data_id' => $data_id, 'type' => $type, 'is_del' => 0])->update(['is_del' => 1]);
  137. }
  138. }