UserActionService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace app\api\service;
  3. use app\admin\model\Agency;
  4. use app\admin\model\system\GrantVoucher;
  5. use app\api\model\massager\Collect;
  6. use app\api\model\massager\Comment;
  7. use app\api\model\massager\Massager;
  8. use app\api\model\User;
  9. use app\api\model\user\Area;
  10. use app\api\model\system\Feedback;
  11. use app\api\model\user\Wallet;
  12. use app\api\model\Voucher;
  13. use redis\RedLock;
  14. use think\Db;
  15. use think\Exception;
  16. class UserActionService extends BaseService
  17. {
  18. private $model;
  19. private $massagerModel;
  20. private $colletModel;
  21. private $commentModel;
  22. private $userAreaModel;
  23. public function __construct()
  24. {
  25. $this->model = new User();
  26. $this->massagerModel = new Massager();
  27. $this->colletModel = new Collect();
  28. $this->commentModel = new Comment();
  29. $this->userAreaModel = new Area();
  30. }
  31. public function modify($user_id, $params)
  32. {
  33. $this->model->allowField(true)->update(
  34. $params,
  35. ["id" => $user_id]
  36. );
  37. return $this->model->get($user_id);
  38. }
  39. public function cancel($user_id) {
  40. $this->model->where("id", $user_id)->delete();
  41. $this->ok();
  42. }
  43. /**
  44. * 收藏助教
  45. * @param $user_id
  46. * @param $massager_id
  47. * @return \SResult
  48. */
  49. public function colletMassager($user_id, $massager_id): \SResult
  50. {
  51. $relation = $this->colletModel->getByUserIdAndMassagerId($user_id, $massager_id);
  52. if ($relation)
  53. return $this->fail('您已经收藏!');
  54. Db::startTrans();
  55. try {
  56. $this->colletModel->save([
  57. 'user_id' => $user_id,
  58. 'massager_id' => $massager_id,
  59. 'createtime' => time(),
  60. 'updatetime' => time()
  61. ]);
  62. $this->massagerModel->where('id', $massager_id)->setInc("collect_count");
  63. Db::commit();
  64. } catch (Exception $e) {
  65. Db::rollback();
  66. return $this->fail($e->getMessage());
  67. } finally {
  68. return $this->ok(true);
  69. }
  70. }
  71. /**
  72. * 取消助教
  73. * @param $user_id
  74. * @param $massager_id
  75. * @return \SResult
  76. */
  77. public function cancelColletMassager($user_id, $massager_id): \SResult
  78. {
  79. $relation = $this->colletModel->getByUserIdAndMassagerId($user_id, $massager_id);
  80. if (null === $relation)
  81. return $this->ok(true);
  82. Db::startTrans();
  83. try {
  84. $relation->delete();
  85. $this->massagerModel->where('id', $massager_id)->setDec("collect_count");
  86. Db::commit();
  87. } catch (Exception $e) {
  88. Db::rollback();
  89. return $this->fail($e->getMessage());
  90. } finally {
  91. return $this->ok(true);
  92. }
  93. }
  94. /**
  95. * 评论助教
  96. * @param array $params
  97. * @return \SResult
  98. */
  99. public function commentMassager(array $params): \SResult
  100. {
  101. $total_count = $this->commentModel->where([
  102. "massager_id" => $params["massager_id"],
  103. "status" => \E_BASE_STATUS::Normal
  104. ])->count();
  105. $total_count += 1;
  106. $gte_3_count = $this->commentModel->where([
  107. 'massager_id' => $params['massager_id'],
  108. "status" => \E_BASE_STATUS::Normal
  109. ])->where("star", ">=", 3)->count();
  110. if ($params['star'] >= 3)
  111. $gte_3_count += 1;
  112. $praise_rate = 100;
  113. if ($total_count > 0 && $gte_3_count > 0) {
  114. $praise_rate = fixed2Float((($gte_3_count / $total_count)) * 100);
  115. }
  116. Db::startTrans();
  117. try {
  118. $this->commentModel->save([
  119. "user_id" => $params['user_id'],
  120. "massager_id" => $params["massager_id"],
  121. "is_anonymity" => $params["is_anonymity"],
  122. "tags" => $params["tags"],
  123. "star" => $params["star"],
  124. "content" => $params["content"],
  125. "status" => \E_BASE_STATUS::Normal,
  126. "createtime" => time(),
  127. "updatetime" => time()
  128. ]);
  129. $this->massagerModel
  130. ->where('id', $params["massager_id"])
  131. ->inc("comment_count")
  132. ->update(['praise_rate' => $praise_rate]);
  133. Db::commit();
  134. } catch (Exception $e) {
  135. Db::rollback();
  136. return $this->fail($e->getMessage());
  137. } finally {
  138. return $this->ok(true);
  139. }
  140. }
  141. /**
  142. * 添加地址
  143. * @param $user_id
  144. * @param $params
  145. * @return Area
  146. */
  147. public function addArea($user_id, $params)
  148. {
  149. $params['user_id'] = $user_id;
  150. return $this->userAreaModel->allowField(true)->create($params);
  151. }
  152. /**
  153. * 删除地址
  154. * @param $user_id
  155. * @param $area_id
  156. */
  157. public function delArea($user_id, $area_id)
  158. {
  159. return $this->userAreaModel->where([
  160. "id" => $area_id,
  161. "user_id" => $user_id
  162. ])->delete();
  163. }
  164. /**
  165. * 获取默认地址
  166. * @param $user_id
  167. * @return array|bool|false|\PDOStatement|string|\think\Model|null
  168. */
  169. public function getEnableArea($user_id)
  170. {
  171. return $this->userAreaModel->getEnableArea($user_id);
  172. }
  173. /**
  174. * 获取地址列表
  175. * @param $user_id
  176. * @return bool|false|\PDOStatement|string|\think\Collection
  177. */
  178. public function fetchArea($user_id)
  179. {
  180. return $this->userAreaModel->fetchArea($user_id);
  181. }
  182. /**
  183. * @param $user_id
  184. * @param $area_id
  185. * @return \SResult
  186. * @throws \think\db\exception\DataNotFoundException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. * @throws \think\exception\DbException
  189. */
  190. public function changeAreaEnable($user_id, $area_id)
  191. {
  192. $area = $this->userAreaModel->where([
  193. "id" => $area_id,
  194. "user_id" => $user_id
  195. ])->find();
  196. if (!$area)
  197. return $this->fail("地址不存在");
  198. $this->userAreaModel->where("user_id", $user_id)->update(["enable" => 0]);
  199. $area->enable = 1;
  200. $area->update(["enable" => 1], [
  201. "id" => $area_id,
  202. "user_id" => $user_id
  203. ]);
  204. return $this->ok($area);
  205. }
  206. public function applyBeMassager($user_id, $params)
  207. {
  208. $m = $this->massagerModel->findByMobile($params["mobile"]);
  209. if ($m)
  210. return $this->fail("该手机号码已经申请等待审核!");
  211. $params["status"] = \E_MASSAGER_STATUS::Checking;
  212. $params["type"] = \E_SERVICE_TYPE::App;
  213. $this->massagerModel->allowField(true)->save($params);
  214. return $this->ok(true);
  215. }
  216. public function applyBeAgency($params)
  217. {
  218. $model = new Agency();
  219. $applyRecord = $model->where([
  220. "mobile" => $params["mobile"]
  221. ])->where("status", "in", [\E_BASE_STATUS::Default, "allow"])->find();
  222. if ($applyRecord)
  223. return $this->fail("该手机号码已经申请等待审核!");
  224. $model->save([
  225. "name" => $params["name"],
  226. "mobile" => $params["mobile"],
  227. "id_card" => isset($params["id_card"]) ? $params["id_card"] : null,
  228. "description" => isset($params["description"]) ? $params["description"] : null,
  229. "budget" => isset($params["budget"]) ? $params["budget"] : null,
  230. "license_images" => isset($params["license_images"]) ? $params["license_images"] : null,
  231. "area_ids" => isset($params["area_ids"]) ? $params["area_ids"] : null,
  232. "status" => \E_BASE_STATUS::Default,
  233. "createtime" => time(),
  234. "updatetime" => time()
  235. ]);
  236. return $this->ok(true);
  237. }
  238. public function feedback($user_id, $type, $content, $images, $reason, $mobile, $relation_type, $relation_id, $relation_name)
  239. {
  240. return (new Feedback())->save([
  241. "user_id" => $user_id,
  242. "type" => $type,
  243. "reason" => $reason,
  244. "mobile" => $mobile,
  245. "content" => $content,
  246. "images" => $images,
  247. "relation_type" => $relation_type,
  248. "relation_id" => $relation_id,
  249. "relation_name" => $relation_name,
  250. "status" => \E_BASE_STATUS::Default,
  251. "createtime" => time(),
  252. "updatetime" => time()
  253. ]);
  254. }
  255. public function fetchGrantVoucher($user_id)
  256. {
  257. $model = new GrantVoucher();
  258. $last = $model->where([
  259. "to_user_id" => $user_id,
  260. ])->order("id", "desc")->find();
  261. if (!$last || $last["draw"] == 1)
  262. return null;
  263. $last["vouchers"] = (new Voucher())->where("id", "in", explode(",", $last["voucher_ids"]))->order("takeAmount", "ASC")->select();
  264. $model->update(["is_read" => 1], ["id" => $last["id"]]);
  265. return $last;
  266. }
  267. public function grantVoucher($user_id, $gVoucherId)
  268. {
  269. $walletLock = new RedLock();
  270. $wLock = $walletLock->lock(Wallet::UWKey($user_id));
  271. if (!is_array($wLock))
  272. return $this->fail("请稍后再试");
  273. $model = new GrantVoucher();
  274. $last = $model->where([
  275. "id" => $gVoucherId,
  276. ])->find();
  277. if (!$last || $last["draw"] == 1)
  278. return $this->fail("数据异常!");
  279. $take_effect_time = Voucher::getTakeEffectTime($last["take_effect"]);
  280. $vouchers = (new Voucher())->where("id", "in", explode(",", $last["voucher_ids"]))->order("takeAmount", "ASC")->select();
  281. Db::startTrans();
  282. try {
  283. $uVoucher = [];
  284. foreach ($vouchers as $voucher) {
  285. array_push($uVoucher, [
  286. "user_id" => $user_id,
  287. "voucher_id" => $voucher["id"],
  288. "takeAmount" => $voucher["takeAmount"],
  289. "fullAmount" => $voucher["fullAmount"],
  290. "status" => \E_BASE_STATUS::Normal,
  291. "take_effect_time" => $take_effect_time,
  292. "expiretime" => $take_effect_time + (24 * 60 * 60) * $voucher["indate_day"],
  293. "createtime" => time(),
  294. "updatetime" => time()
  295. ]);
  296. }
  297. if (count($uVoucher) > 0) {
  298. (new \app\api\model\user\Voucher())->insertAll($uVoucher);
  299. }
  300. (new GrantVoucher())->update(["is_read" => 1, "draw" => 1], ["id" => $last["id"]]);
  301. Db::commit();
  302. return $this->ok(true);
  303. } catch (Exception $e) {
  304. Db::rollback();
  305. return $this->fail($e->getMessage());
  306. } finally {
  307. $walletLock->unlock($wLock);
  308. }
  309. }
  310. }