UserEquityLogic.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\equity;
  15. use app\adminapi\logic\user\UserLogic;
  16. use app\common\model\equity\EquityConfig;
  17. use app\common\model\equity\UserEquity;
  18. use app\common\logic\BaseLogic;
  19. use app\common\model\user\User;
  20. use app\common\model\works\ServiceWork;
  21. use app\common\model\works\ServiceWorkLog;
  22. use think\facade\Db;
  23. use think\facade\Log;
  24. use think\facade\Validate;
  25. /**
  26. * UserEquity逻辑
  27. * Class UserEquityLogic
  28. * @package app\adminapi\logic
  29. */
  30. class UserEquityLogic extends BaseLogic
  31. {
  32. /**
  33. * @notes 添加
  34. * @param array $params
  35. * @return bool
  36. * @author likeadmin
  37. * @date 2024/12/19 11:43
  38. */
  39. public static function add(array $params): bool
  40. {
  41. Db::startTrans();
  42. try {
  43. $equityConfigInfo = EquityConfig::find($params['equity_id']);
  44. $max_id = UserEquity::max('id');
  45. if((int)$params['is_binding'] === 1){
  46. if(!Validate::checkRule($params['mobile'], 'require|mobile')){
  47. throw new \Exception('请填写正确手机号');
  48. }
  49. // 通过手机号查询用户是否注册 - 已注册绑定id ,未注册注册再绑定id
  50. $userId = UserLogic::getUserIdByMobile($params['mobile']);
  51. $max_id++;
  52. UserEquity::create([
  53. 'user_id' => $userId,
  54. 'equity_id' => $params['equity_id'],
  55. 'remark' => $params['remark'],
  56. 'code' => generateRandomString((8-strlen($max_id)),2).$max_id,
  57. 'goods_id' => $equityConfigInfo['goods_id'],
  58. 'number' => $equityConfigInfo['number'],
  59. 'end_time' => (time()+$equityConfigInfo['day_num']*86400),
  60. ]);
  61. }else{
  62. for ($i=0;$i<$params['quantity'];$i++){
  63. $max_id++;
  64. $list[] = [
  65. 'user_id' => 0,
  66. 'equity_id' => $params['equity_id'],
  67. 'remark' => $params['remark'],
  68. 'code' => generateRandomString((8-strlen($max_id)),2).$max_id,
  69. 'goods_id' => $equityConfigInfo['goods_id'],
  70. 'number' => $equityConfigInfo['number'],
  71. 'end_time' => 0,
  72. 'create_time' => time(),
  73. 'update_time' => time(),
  74. ];
  75. }
  76. UserEquity::insertAll($list);
  77. }
  78. Db::commit();
  79. return true;
  80. } catch (\Exception $e) {
  81. Db::rollback();
  82. self::setError($e->getMessage());
  83. return false;
  84. }
  85. }
  86. /**
  87. * @notes 编辑
  88. * @param array $params
  89. * @return bool
  90. * @author likeadmin
  91. * @date 2024/12/19 11:43
  92. */
  93. public static function edit(array $params): bool
  94. {
  95. Db::startTrans();
  96. try {
  97. UserEquity::where('id', $params['id'])->update([
  98. 'number' => $params['number']??0,
  99. 'remark' => $params['remark'],
  100. ]);
  101. Db::commit();
  102. return true;
  103. } catch (\Exception $e) {
  104. Db::rollback();
  105. self::setError($e->getMessage());
  106. return false;
  107. }
  108. }
  109. /**
  110. * @notes 删除
  111. * @param array $params
  112. * @return bool
  113. * @author likeadmin
  114. * @date 2024/12/19 11:43
  115. */
  116. public static function delete(array $params): bool
  117. {
  118. return UserEquity::destroy($params['id']);
  119. }
  120. /**
  121. * @notes 获取详情
  122. * @param $params
  123. * @return array
  124. * @author likeadmin
  125. * @date 2024/12/19 11:43
  126. */
  127. public static function detail($params): array
  128. {
  129. return UserEquity::findOrEmpty($params['id'])->toArray();
  130. }
  131. public static function serviceWorkDetail($params): array
  132. {
  133. return ServiceWork::where('user_equity_id',$params['user_equity_id'])->append(['work_status_text'])->select()->toArray();
  134. }
  135. /**
  136. * 取消工单操作-恢复权益次数
  137. * @param array $params
  138. * @return bool
  139. */
  140. public static function cancelServiceWorkRestoredNumber(array $params): bool
  141. {
  142. Db::startTrans();
  143. try {
  144. $serviceWorkInfo = ServiceWork::find($params['id']);
  145. if(empty($serviceWorkInfo['user_equity_id'])){
  146. throw new \Exception('该工单非权益卡工单!!!');
  147. }
  148. if((int)$serviceWorkInfo['service_status'] == 4){
  149. // 恢复权益次数
  150. UserEquity::where('id', $serviceWorkInfo['user_equity_id'])->inc('number')->save();
  151. }else{
  152. throw new \Exception('该工单非权益卡工单!!!');
  153. }
  154. Db::commit();
  155. Log::info('权益卡取消工单'.json_encode([$serviceWorkInfo]));
  156. return true;
  157. } catch (\Exception $e) {
  158. Db::rollback();
  159. Log::info('权益卡取消工单-Error'.json_encode([$e->getMessage()]));
  160. return false;
  161. }
  162. }
  163. }