PointPlan.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace app\models\point_plan;
  3. use app\admin\model\system\SystemUserLevel;
  4. use app\models\mining\MiningMachine;
  5. use app\models\mining\UserMiningMachine;
  6. use app\models\system\SystemUserTask;
  7. use app\models\user\User;
  8. use app\models\user\UserLevel;
  9. use app\models\user\UserMoney;
  10. use app\models\user\UserTaskFinish;
  11. use crmeb\basic\BaseModel;
  12. use crmeb\traits\ModelTrait;
  13. use think\Collection;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. use think\Exception;
  18. class PointPlan extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'point_plan';
  30. use ModelTrait;
  31. /**
  32. * @param $id
  33. * @param $uid
  34. * @param $num
  35. * @return bool
  36. * @throws DataNotFoundException
  37. * @throws DbException
  38. * @throws ModelNotFoundException
  39. */
  40. public static function joinPlan($id, $uid, $num): bool
  41. {
  42. $info = self::where('status', 1)->where('id', $id)->find();
  43. $money = UserMoney::initialUserMoney($uid, $info['buy_money_type']);
  44. $money_num = bcmul($num, $info['buy_price'], 8);
  45. if ($money['money'] < $money_num) return self::setErrorInfo('账户余额不足!');
  46. BaseModel::beginTrans();
  47. try {
  48. $res = UserMoney::expendMoney($uid, $info['buy_money_type'], $money_num, 'join_point_plan', '认购节点', '认购' . $info['plan_name'] . $num . '股');
  49. if (!$res) {
  50. return self::setErrorInfo(UserMoney::getErrorInfo('支付失败'), true);
  51. }
  52. $res = UserPointPlan::create([
  53. 'buy_num' => $num,
  54. 'buy_all_price' => $money_num,
  55. 'release_money' => $info['release_money'],
  56. 'release_money_type' => $info['release_money_type'],
  57. 'release_days' => $info['release_days'],
  58. 'buy_money_type' => $info['buy_money_type'],
  59. 'ticket_ratio' => $info['ticket_ratio'],
  60. 'service_ratio' => $info['service_ratio'],
  61. 'add_time' => time(),
  62. 'uid' => $uid,
  63. 'release_time_all' => $info['release_days'],
  64. 'plan_id' => $info['id'],
  65. ]);
  66. $res = $res && self::giveLevel($uid, $info) && self::giveMachine($uid, $info, $num) && self::decStock($info, $num);
  67. if ($res) {
  68. BaseModel::commitTrans();
  69. return $res;
  70. } else {
  71. return self::setErrorInfo('认购失败,' . self::getErrorInfo(), true);
  72. }
  73. } catch (Exception $e) {
  74. return self::setErrorInfo($e->getMessage(), true);
  75. }
  76. }
  77. /**
  78. * @param $plan
  79. * @param $num
  80. * @return bool
  81. * @throws DataNotFoundException
  82. * @throws DbException
  83. * @throws ModelNotFoundException
  84. */
  85. public static function decStock($plan, $num): bool
  86. {
  87. $update = [
  88. 'stock' => $plan['stock'] - $num,
  89. ];
  90. if ($plan['stock'] == $num) {
  91. $update['status'] = 2;
  92. }
  93. $res = self::where('id', $plan['id'])->update($update);
  94. if ($res) {
  95. if ($plan['stock'] == $num) {
  96. if ($next = self::where('status', 0)->order('add_time asc,id asc')->find()) {
  97. $res = $res && self::where('id', $next['id'])->update(['status' => 1, 'start_time' => time()]);
  98. }
  99. }
  100. }
  101. return $res;
  102. }
  103. /**
  104. * @param $uid
  105. * @param $plan
  106. * @return bool
  107. */
  108. public static function giveLevel($uid, $plan): bool
  109. {
  110. if (!$uid || !$plan['give_level']) {
  111. return true;
  112. }
  113. //查询当前选择的会员等级
  114. $systemLevel = SystemUserLevel::where(['is_show' => 1, 'is_del' => 0, 'id' => $plan['give_level']])->find();
  115. if (!$systemLevel) return true;
  116. //检查是否拥有此会员等级
  117. $level = UserLevel::where(['uid' => $uid, 'level_id' => $plan['give_level'], 'is_del' => 0])->field('valid_time,is_forever')->find();
  118. if ($level) if (!(!$level['is_forever'] && time() < $level['valid_time'])) return true;
  119. //设置会员过期时间
  120. $add_valid_time = (int)$systemLevel->valid_date * 86400;
  121. $res = UserLevel::create([
  122. 'is_forever' => $systemLevel->is_forever,
  123. 'status' => 1,
  124. 'is_del' => 0,
  125. 'grade' => $systemLevel->grade,
  126. 'uid' => $uid,
  127. 'add_time' => time(),
  128. 'level_id' => $plan['give_level'],
  129. 'discount' => $systemLevel->discount,
  130. 'valid_time' => $systemLevel->is_forever ? $add_valid_time + time() : 0,
  131. 'mark' => '尊敬的用户【' . User::where('uid', $uid)->value('nickname') . '】在' . date('Y-m-d H:i:s', time()) . '购买节点股份赠送会员等级成为' . $systemLevel['name'] . '会员',
  132. ]);
  133. //提取等级任务并记录完成情况
  134. $levelIds = [$plan['give_level']];
  135. $lowGradeLevelIds = SystemUserLevel::where('grade', '<', $systemLevel->grade)->where(['is_show' => 1, 'is_del' => 0])->column('id', 'id');
  136. if (count($lowGradeLevelIds)) $levelIds = array_merge($levelIds, $lowGradeLevelIds);
  137. $taskIds = SystemUserTask::where('level_id', 'in', $levelIds)->column('id', 'id');
  138. $inserValue = [];
  139. foreach ($taskIds as $id) {
  140. $inserValue[] = ['uid' => $uid, 'task_id' => $id, 'status' => 1, 'add_time' => time()];
  141. }
  142. $res = $res && User::where('uid', $uid)->update(['level' => $plan['give_level']]);
  143. if ($inserValue) $res && UserTaskFinish::insertAll($inserValue);
  144. if ($res) {
  145. return true;
  146. } else {
  147. return self::setErrorInfo('赠送等级失败');
  148. }
  149. }
  150. /**
  151. * @param $uid
  152. * @param $plan
  153. * @return bool
  154. */
  155. public static function giveMachine($uid, $plan, $num): bool
  156. {
  157. if (!$uid || !$plan['give_mining_machine'] || $plan['give_mining_machine_num'] <= 0) {
  158. return true;
  159. }
  160. $info = MiningMachine::valid()->where('id', $plan['give_mining_machine'])->find();
  161. if (!$info) {
  162. return true;
  163. }
  164. if ($info['stock'] < $plan['give_mining_machine_num']) {
  165. return true;
  166. }
  167. $info['cost_money'] = bcmul($info['cost_money'], $plan['give_mining_machine_num']);
  168. $info['stand_money'] = bcmul($info['stand_money'], $plan['give_mining_machine_num']);
  169. $res = UserMiningMachine::create([
  170. 'uid' => $uid,
  171. 'mid' => $plan['give_mining_machine'],
  172. 'day_get' => $info['day_get'],
  173. 'get_money_type' => $info['get_money_type'],
  174. 'cost_money' => 0,
  175. 'cost_money_type' => $info['cost_money_type'],
  176. 'mortgage_money' => $info['mortgage_money'],
  177. 'mortgage_money_type' => $info['mortgage_money_type'],
  178. 'redeem_money' => $info['redeem_money'],
  179. 'redeem_money_type' => $info['redeem_money_type'],
  180. 'stand_money' => 0,
  181. 'add_time' => time(),
  182. 'pay_time' => time(),
  183. 'paid' => 1,
  184. 'num' => $plan['give_mining_machine_num'] * $num,
  185. 'cost_times' => 0,
  186. 'lala_id' => 0,
  187. 'mining_end_time' => bcadd(bcadd(time(), bcmul($info['first_step_time'] + $info['third_step_time'] + $info['second_step_time'], 3600 * 24)), bcmul($info['stand_time'], 3600 * 24)),
  188. 'mining_start_time' => bcadd(time(), bcmul($info['stand_time'], 3600 * 24)),
  189. 'second_step_start_time' => bcadd(bcadd(time(), bcmul($info['first_step_time'], 3600 * 24)), bcmul($info['stand_time'], 3600 * 24)),
  190. 'third_step_start_time' => bcadd(bcadd(time(), bcmul($info['first_step_time'] + $info['second_step_time'], 3600 * 24)), bcmul($info['stand_time'], 3600 * 24)),
  191. ]);
  192. if ($res) return true;
  193. else return self::setErrorInfo('赠送矿机失败');
  194. }
  195. /**
  196. * @param $where
  197. * @return array
  198. */
  199. public static function getList($where)
  200. {
  201. $model = new self();
  202. if (isset($where['title']) && $where['title'] != '') {
  203. $model->where('id|plan_name', 'like', "%{$where['title']}%");
  204. }
  205. if (isset($where['status']) && $where['status'] != '') {
  206. $model->where('status', $where['status']);
  207. }
  208. $count = $model->count();
  209. $data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) {
  210. $item['join_number'] = UserPointPlan::where('plan_id', $item['id'])->sum('buy_num');
  211. $item['_start_time'] = $item['start_time'] ? date('Y-m-d H:i:s', $item['start_time']) : '--';
  212. $item['_status'] = $item['status'] == 0 ? '等待中' : ($item['status'] == 1 ? '认购中' : '已结束');
  213. });
  214. return compact('count', 'data');
  215. }
  216. }