MUserCoupon.Class.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * 优惠券记录
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/12/13
  7. * Time: 16:20
  8. */
  9. namespace JinDouYun\Model\Market;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use JinDouYun\Cache\GoodsBasicRelevant;
  13. use JinDouYun\Dao\Market\DUserCoupon;
  14. use JinDouYun\Model\Customer\MCustomer;
  15. use JinDouYun\Model\MBaseModel;
  16. use Mall\Framework\Core\StatusCode;
  17. class MUserCoupon extends MBaseModel
  18. {
  19. private $onlineUserId;
  20. private $onlineEnterpriseId;
  21. private $objDUserCoupon;
  22. /**
  23. * MUserCoupon constructor.
  24. * @param $onlineUserId
  25. * @param $onlineEnterpriseId
  26. */
  27. public function __construct($onlineUserId, $onlineEnterpriseId)
  28. {
  29. $this->onlineUserId = $onlineUserId;
  30. $this->onlineEnterpriseId = $onlineEnterpriseId;
  31. parent::__construct($this->onlineEnterpriseId, $onlineUserId);
  32. $this->objDUserCoupon = new DUserCoupon('default');
  33. $this->objDUserCoupon->setTable($this->objDUserCoupon->get_Table() . '_' . $this->onlineEnterpriseId);
  34. //$this->objDUserCoupon->setSearchIndex('user_coupon_search')->setType('user_coupon');
  35. }
  36. /**
  37. * @param $params
  38. * @param bool $multiple
  39. * @return ResultWrapper
  40. */
  41. public function add($params, $multiple = false)
  42. {
  43. $dbResult = $this->objDUserCoupon->insert($params, $multiple);
  44. if ($dbResult === false) {
  45. return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror);
  46. }
  47. return ResultWrapper::success($dbResult);
  48. }
  49. /**
  50. * 列表
  51. * @param $selectParams
  52. * @return ResultWrapper
  53. */
  54. public function getAll($selectParams)
  55. {
  56. $pageData = pageToOffset($selectParams['page'], $selectParams['pageSize']);
  57. $condition = null;
  58. if ($selectParams['keyword']) {
  59. $condition = "name like '%" . $selectParams['keyword'] . "%'";
  60. }
  61. if ($selectParams['customerId']) {
  62. if ($condition !== null) {
  63. $condition .= 'and ';
  64. }
  65. $condition .= "customerId = " . $selectParams['customerId'];
  66. }
  67. if ($selectParams['startTime'] && $selectParams['endTime']) {
  68. if ($condition !== null) {
  69. $condition .= 'and ';
  70. }
  71. $condition .= "createTime >= " . $selectParams['startTime'] . " and createTime <=" . $selectParams['endTime'];
  72. }
  73. if ($selectParams['isUse']) {
  74. if ($condition !== null) {
  75. $condition .= 'and ';
  76. }
  77. $condition .= "isUse = " . $selectParams['isUse'];
  78. }
  79. $dbResult = $this->objDUserCoupon->select($condition, '*', 'createTime DESC', $pageData['limit'], $pageData['offset']);
  80. if ($dbResult === false) {
  81. return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror);
  82. }
  83. $sql = 'SELECT COUNT(id) as total,COUNT(IF(isUse=' . StatusCode::$delete . ',isUse,null)) as useTotal,SUM(reducePrice) as reducePrice,SUM(IF(isUse=' . StatusCode::$delete . ',reducePrice,0)) as useReducePrice FROM qianniao_user_coupon_' . $this->onlineEnterpriseId;
  84. if ($condition !== null) {
  85. $sql .= ' WHERE ' . $condition;
  86. }
  87. $statistics = $this->objDUserCoupon->query($sql);
  88. if ($statistics === false) {
  89. return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror);
  90. }
  91. $statistics = array_pop($statistics);
  92. $return = [
  93. 'data' => self::formatAll($dbResult),
  94. 'total' => $statistics['total'],
  95. 'useTotal' => $statistics['useTotal'],
  96. 'reducePrice' => $statistics['reducePrice'],
  97. 'useReducePrice' => $statistics['useReducePrice'],
  98. ];
  99. return ResultWrapper::success($return);
  100. }
  101. /**
  102. * 格式化列表页数据
  103. */
  104. public function formatAll($data)
  105. {
  106. if (empty($data)) return $data;
  107. $objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
  108. $dbResult = $objMCustomer->getCustomer(['id' => array_column($data, 'customerId')], 'name,id');
  109. $customer = [];
  110. if ($dbResult->isSuccess()) {
  111. $customer = $dbResult->getData();
  112. }
  113. foreach ($data as &$val) {
  114. $val['minPrice'] = ($val['minPrice'] == 0) ? '不限金额' : $val['minPrice'];
  115. $val['useShopName'] = ($val['useShop'] === 0) ? ['全店铺'] : array_values(self::getNameByShopId(explode(',', $val['useShop'])));
  116. $val['customerName']= isset($customer[$val['customerId']]['name']) ? $customer[$val['customerId']]['name'] : '';
  117. $val['orderData'] = isset($val['orderData']) && !empty($val['orderData']) ? json_decode($val['orderData'],true):[];
  118. }
  119. return $data;
  120. }
  121. /**
  122. * @param $selectParams
  123. * @param bool $isQuery
  124. * @param string $fields
  125. * @param string $countQuery
  126. * @return ResultWrapper
  127. */
  128. public function getUserCoupon($selectParams, $isQuery = false, $fields = "*", $countQuery = null)
  129. {
  130. if ($isQuery) {
  131. $dbResult = $this->objDUserCoupon->query($selectParams);
  132. } else {
  133. $dbResult = $this->objDUserCoupon->select($selectParams, $fields);
  134. }
  135. if ($dbResult === false) {
  136. return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror);
  137. }
  138. if ($isQuery) {
  139. $total = $this->objDUserCoupon->query($countQuery);
  140. $dbResult = [
  141. 'data' => $dbResult,
  142. 'total' => count($total),
  143. ];
  144. }
  145. return ResultWrapper::success($dbResult);
  146. }
  147. /**
  148. * 店铺数据
  149. * @param $shopIds
  150. * @return array
  151. */
  152. public function getNameByShopId($shopIds)
  153. {
  154. $objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->onlineEnterpriseId);
  155. $result = [];
  156. foreach ($shopIds as $shopId) {
  157. if (!empty($shopId)) $result[] = $objGoodsBasicRelevantCache->getNameByShopId($shopId);
  158. }
  159. return $result;
  160. }
  161. /**
  162. * 获取可用的优惠券
  163. * @param $selectParams
  164. * @return ResultWrapper
  165. */
  166. public function availableCoupon($selectParams)
  167. {
  168. // $sql = 'SELECT * FROM qianniao_user_coupon_' . $this->onlineEnterpriseId . ' WHERE isUse=' . StatusCode::$standard . ' AND userId=' . $this->onlineUserId . ' AND startTime<' . $nowTime . ' AND endTime>' . $nowTime . ' AND FIND_IN_SET(' . $selectParams['shopId'] . ',useShop) AND (categoryCollect="" OR FIND_IN_SET(' . $selectParams['categoryId'] . ',categoryCollect)) AND (brandCollect="" OR FIND_IN_SET(' . $selectParams['brandId'] . ',brandCollect)) AND (minPrice=0 OR minPrice<' . $selectParams['payAmount'] . ')';
  169. $nowTime = time();
  170. $sql = 'SELECT * FROM qianniao_user_coupon_' . $this->onlineEnterpriseId . ' WHERE isUse=' . StatusCode::$standard . ' AND userId=' . $this->onlineUserId . ' AND startTime<' . $nowTime . ' AND endTime>' . $nowTime . ' AND (minPrice=0 OR minPrice<=' . $selectParams['payAmount'] . ')';
  171. $dbResult = $this->objDUserCoupon->query($sql);
  172. if ($dbResult === false) {
  173. return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror);
  174. }
  175. return ResultWrapper::success($dbResult);
  176. }
  177. /**
  178. * Doc: (des="获取用户可以使用的优惠券数量")
  179. * User: XMing
  180. * Date: 2021/3/17
  181. * Time: 5:07 下午
  182. * @param int $userCenterId
  183. * @return ResultWrapper
  184. */
  185. public function getCouponNum(int $userCenterId): ResultWrapper
  186. {
  187. $nowTime = time();
  188. $sql = 'SELECT COUNt(id) as total FROM qianniao_user_coupon_' . $this->onlineEnterpriseId . ' WHERE isUse=' . StatusCode::$standard . ' AND userId = ' . $userCenterId . ' AND endTime>' . $nowTime;
  189. $dbResult = $this->objDUserCoupon->query($sql);
  190. if ($dbResult === false) {
  191. return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror);
  192. }
  193. $total = isset($dbResult[0]['total']) ? (int)$dbResult[0]['total'] : 0;
  194. return ResultWrapper::success($total);
  195. }
  196. /**
  197. * @param $selectParams
  198. * @param $data
  199. * @return ResultWrapper
  200. */
  201. public function updateIsUse($selectParams, $data)
  202. {
  203. $dbResult = $this->objDUserCoupon->update($data, $selectParams);
  204. if ($dbResult === false) {
  205. return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror);
  206. }
  207. return ResultWrapper::success($dbResult);
  208. }
  209. /**
  210. * 搜索
  211. * @param $selectParams
  212. */
  213. public function search($selectParams)
  214. {
  215. $defaultDSL = [
  216. 'from' => $selectParams['offset'],
  217. 'size' => $selectParams['limit'],
  218. 'sort' => [
  219. 'createTime' => [
  220. 'order' => 'desc'
  221. ],
  222. ],
  223. ];
  224. $dsl = [];
  225. $dsl['query']['bool']['must'][] = [
  226. 'term' => ['enterpriseId' => $this->onlineEnterpriseId],
  227. ];
  228. }
  229. }