StoreBargainUserServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\bargain;
  13. use app\Request;
  14. use app\services\BaseServices;
  15. use app\dao\activity\bargain\StoreBargainUserDao;
  16. /**
  17. *
  18. * Class StoreBargainUserServices
  19. * @package app\services\activity
  20. * @method getAllCount(array $where)
  21. * @method count(array $where)
  22. * @method value(array $where, ?string $field)
  23. * @method getBargainUserTableId(int $bargainId, int $bargainUserUid)
  24. * @method update(int $bargainId, array $data)
  25. * @method getOne(array $where, ?string $field = '*', array $with = [])
  26. * @method updateBargainStatus(int $id, ?int $status = 3)
  27. */
  28. class StoreBargainUserServices extends BaseServices
  29. {
  30. /**
  31. * StoreBargainUserServices constructor.
  32. * @param StoreBargainUserDao $dao
  33. */
  34. public function __construct(StoreBargainUserDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * TODO 根据砍价商品编号获取正在参与人的编号
  40. * @param int $bargainId $bargainId 砍价商品ID
  41. * @param int $status $status 状态 1 进行中 2 结束失败 3结束成功
  42. * @return array
  43. */
  44. public function getUserIdList($bargainId = 0, $status = 1)
  45. {
  46. $ids = $this->dao->getColumn(['bargain_id' => $bargainId], 'id');
  47. /** @var StoreBargainUserHelpServices $bargainHelp */
  48. $bargainHelp = app()->make(StoreBargainUserHelpServices::class);
  49. return $bargainHelp->getCount([['bargain_user_id', 'in', $ids], ['bargain_id', '=', $bargainId], ['type', '=', 1]]);
  50. }
  51. /**
  52. * 获取砍价
  53. * @param Request $request
  54. * @param int $bargainId
  55. * @param int $bargainUserUid
  56. * @return mixed
  57. */
  58. public function helpCount(Request $request, int $bargainId, int $bargainUserUid)
  59. {
  60. $bargainUserTableId = $this->dao->value(['bargain_id' => $bargainId, 'uid' => $bargainUserUid, 'is_del' => 0, 'status' => 1]);//TODO 获取用户参与砍价表编号
  61. $data['userBargainStatus'] = $this->isBargainUserHelpCount($bargainId, $request->uid(), $bargainUserTableId);
  62. /** @var StoreBargainUserHelpServices $helpService */
  63. $helpService = app()->make(StoreBargainUserHelpServices::class);
  64. if ($bargainUserTableId) {
  65. $count = $helpService->count(['bargain_user_id' => $bargainUserTableId, 'bargain_id' => $bargainId]);//TODO 获取砍价帮总人数
  66. $price = $this->getSurplusPrice($bargainUserTableId, 1);//TODO 获取砍价剩余金额
  67. $alreadyPrice = $this->dao->value(['id' => $bargainUserTableId], 'price');//TODO 用户已经砍掉的价格 好友砍价之后获取用户已经砍掉的价格
  68. $pricePercent = $this->getSurplusPrice($bargainUserTableId, 2);//TODO 获取砍价进度条
  69. $data['count'] = $count;
  70. $data['price'] = $price;
  71. $data['status'] = $this->dao->value(['id' => $bargainUserTableId], 'status') ?? 0;
  72. $data['alreadyPrice'] = $alreadyPrice;
  73. $data['pricePercent'] = $pricePercent > 10 ? $pricePercent : 10;
  74. } else {
  75. /** @var StoreBargainServices $bargainService */
  76. $bargainService = app()->make(StoreBargainServices::class);
  77. $data['count'] = 0;
  78. $data['price'] = $bargainService->value(['id' => $bargainId], 'price - min_price');
  79. $data['status'] = $this->dao->value(['id' => $bargainUserTableId], 'status') ?? 0;
  80. $data['alreadyPrice'] = 0;
  81. $data['pricePercent'] = 0;
  82. }
  83. return $data;
  84. }
  85. /**
  86. * 获取砍价状态
  87. * @param int $bargainId
  88. * @param int $bargainUserUid
  89. * @param int $bargainUserHelpUid
  90. * @param $bargainUserTableId
  91. * @return bool
  92. */
  93. public function isBargainUserHelpCount($bargainId, $bargainUserHelpUid, $bargainUserTableId)
  94. {
  95. /** @var StoreBargainUserHelpServices $userHelp */
  96. $userHelp = app()->make(StoreBargainUserHelpServices::class);
  97. $count = $userHelp->count(['bargain_id' => $bargainId, 'bargain_user_id' => $bargainUserTableId, 'uid' => $bargainUserHelpUid]);
  98. if (!$count) return true;
  99. else return false;
  100. }
  101. /**
  102. * 获取砍价剩余金额 或者 砍价百分比
  103. * @param $bargainUserTableId
  104. * @param $type
  105. * @return float
  106. */
  107. public function getSurplusPrice($bargainUserTableId, $type)
  108. {
  109. $coverPrice = $this->getBargainUserDiffPriceFloat($bargainUserTableId);//TODO 获取用户可以砍掉的金额 好友砍价之后获取砍价金额
  110. $alreadyPrice = $this->dao->value(['id' => $bargainUserTableId], 'price');//TODO 用户已经砍掉的价格 好友砍价之后获取用户已经砍掉的价格
  111. if ($type == 1) {
  112. return (float)bcsub((string)$coverPrice, (string)$alreadyPrice, 2);//TODO 用户剩余要砍掉的价格
  113. } else {
  114. if ($alreadyPrice) return (int)bcmul((string)bcdiv((string)$alreadyPrice, (string)$coverPrice, 2), '100', 0);
  115. else return 100;
  116. }
  117. }
  118. /**
  119. * 获取用户可以砍掉的金额 好友砍价之后获取砍价金额
  120. * @param $id
  121. * @return float
  122. */
  123. public function getBargainUserDiffPriceFloat($id)
  124. {
  125. $price = $this->dao->get($id);
  126. return (float)bcsub((string)$price['bargain_price'], (string)$price['bargain_price_min'], 2);
  127. }
  128. /**
  129. * 添加砍价信息
  130. * @param int $bargainId
  131. * @param int $bargainUserUid
  132. * @param array $bargainInfo
  133. * @return mixed
  134. */
  135. public function setBargain(int $bargainId, int $bargainUserUid, array $bargainInfo)
  136. {
  137. $data['bargain_id'] = $bargainId;
  138. $data['uid'] = $bargainUserUid;
  139. $data['bargain_price_min'] = $bargainInfo['min_price'];
  140. $data['bargain_price'] = $bargainInfo['price'];
  141. $data['price'] = 0;
  142. $data['status'] = 1;
  143. $data['is_del'] = 0;
  144. $data['add_time'] = time();
  145. return $this->dao->save($data);
  146. }
  147. /**
  148. * 修改砍价状态
  149. * @param $uid
  150. * @return bool
  151. */
  152. public function editBargainUserStatus($uid)
  153. {
  154. $currentBargain = $this->dao->getColumn(['uid' => $uid, 'is_del' => 0, 'status' => 1], 'bargain_id');
  155. /** @var StoreBargainServices $bargainService */
  156. $bargainService = app()->make(StoreBargainServices::class);
  157. $bargainProduct = $bargainService->validWhere()->column('id');
  158. $closeBargain = [];
  159. foreach ($currentBargain as $key => &$item) {
  160. if (!in_array($item, $bargainProduct)) {
  161. $closeBargain[] = $item;
  162. }
  163. }// TODO 获取已经结束的砍价商品
  164. if (count($closeBargain)) $this->dao->update([['uid', '=', $uid], ['status', '=', 1], ['bargain_id', 'in', implode(',', $closeBargain)]], ['status' => 2]);
  165. }
  166. /**
  167. * TODO 获取用户的砍价商品
  168. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  169. * @return array
  170. */
  171. public function getBargainUserAll(int $bargainUserUid)
  172. {
  173. if (!$bargainUserUid) return [];
  174. [$page, $limit] = $this->getPageValue();
  175. $list = $this->dao->userAll($bargainUserUid, $page, $limit);
  176. $bargainHelpServices = app()->make(StoreBargainUserHelpServices::class);
  177. foreach ($list as &$item) {
  178. $item['residue_price'] = bcsub((string)$item['bargain_price'], (string)$item['price'], 2);
  179. if ($item['status'] == 3) {
  180. $item['success_time'] = date('Y-m-d H:i:s', (int)$bargainHelpServices->getMax(['bargain_user_id' => $item['id']], 'add_time'));
  181. } else {
  182. $item['success_time'] = '';
  183. }
  184. }
  185. return $list;
  186. }
  187. /**
  188. * 取消砍价
  189. * @param $bargainId
  190. * @param $uid
  191. * @return mixed
  192. */
  193. public function cancelBargain($bargainId, $uid)
  194. {
  195. $status = $this->dao->getBargainUserStatus($bargainId, $uid);
  196. if ($status != 1) return app('json')->fail(100020);
  197. $id = $this->dao->value(['bargain_id' => $bargainId, 'uid' => $uid, 'is_del' => 0], 'id');
  198. return $this->dao->update($id, ['is_del' => 1, 'status' => 2]);
  199. }
  200. /**
  201. * 下架删除砍价时修改砍价状态 砍价失败
  202. * @param $bargain_id
  203. */
  204. public function userBargainStatusFail($bargain_id, $is_true)
  205. {
  206. if ($is_true) {
  207. $this->dao->delete(['bargain_id' => $bargain_id]);
  208. /** @var StoreBargainUserHelpServices $service */
  209. $service = app()->make(StoreBargainUserHelpServices::class);
  210. $service->delete(['bargain_id' => $bargain_id]);
  211. } else {
  212. $this->dao->update(['bargain_id' => $bargain_id, 'status' => 1], ['status' => 2]);
  213. }
  214. }
  215. /**
  216. * 砍价列表
  217. * @param $where
  218. * @return array
  219. * @throws \think\db\exception\DataNotFoundException
  220. * @throws \think\db\exception\DbException
  221. * @throws \think\db\exception\ModelNotFoundException
  222. */
  223. public function bargainUserList($where)
  224. {
  225. [$page, $limit] = $this->getPageValue();
  226. $list = $this->dao->bargainUserList($where, $page, $limit);
  227. $count = $this->dao->count($where);
  228. /** @var StoreBargainUserHelpServices $bargainUserHelpService */
  229. $bargainUserHelpService = app()->make(StoreBargainUserHelpServices::class);
  230. $nums = $bargainUserHelpService->getNums();
  231. foreach ($list as &$item) {
  232. $item['num'] = $item['people_num'] - ($nums[$item['id']] ?? 0);
  233. $item['already_num'] = $nums[$item['id']] ?? 0;
  234. $item['now_price'] = bcsub((string)$item['bargain_price'], (string)$item['price'], 2);
  235. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', (int)$item['add_time']) : '';
  236. $item['datatime'] = $item['datatime'] ? date('Y-m-d H:i:s', (int)$item['datatime']) : '';
  237. }
  238. return compact('list', 'count');
  239. }
  240. }