onlineUserId = $onlineUserId; $this->onlineEnterpriseId = $onlineEnterpriseId; parent::__construct($this->onlineEnterpriseId, $onlineUserId); $this->objDUserCoupon = new DUserCoupon('default'); $this->objDUserCoupon->setTable($this->objDUserCoupon->get_Table() . '_' . $this->onlineEnterpriseId); //$this->objDUserCoupon->setSearchIndex('user_coupon_search')->setType('user_coupon'); } /** * @param $params * @param bool $multiple * @return ResultWrapper */ public function add($params, $multiple = false) { $dbResult = $this->objDUserCoupon->insert($params, $multiple); if ($dbResult === false) { return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 列表 * @param $selectParams * @return ResultWrapper */ public function getAll($selectParams) { $pageData = pageToOffset($selectParams['page'], $selectParams['pageSize']); $condition = null; if ($selectParams['keyword']) { $condition = "name like '%" . $selectParams['keyword'] . "%'"; } if ($selectParams['customerId']) { if ($condition !== null) { $condition .= 'and '; } $condition .= "customerId = " . $selectParams['customerId']; } if ($selectParams['startTime'] && $selectParams['endTime']) { if ($condition !== null) { $condition .= 'and '; } $condition .= "createTime >= " . $selectParams['startTime'] . " and createTime <=" . $selectParams['endTime']; } if ($selectParams['isUse']) { if ($condition !== null) { $condition .= 'and '; } $condition .= "isUse = " . $selectParams['isUse']; } $dbResult = $this->objDUserCoupon->select($condition, '*', 'createTime DESC', $pageData['limit'], $pageData['offset']); if ($dbResult === false) { return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror); } $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; if ($condition !== null) { $sql .= ' WHERE ' . $condition; } $statistics = $this->objDUserCoupon->query($sql); if ($statistics === false) { return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror); } $statistics = array_pop($statistics); $return = [ 'data' => self::formatAll($dbResult), 'total' => $statistics['total'], 'useTotal' => $statistics['useTotal'], 'reducePrice' => $statistics['reducePrice'], 'useReducePrice' => $statistics['useReducePrice'], ]; return ResultWrapper::success($return); } /** * 格式化列表页数据 */ public function formatAll($data) { if (empty($data)) return $data; $objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId); $dbResult = $objMCustomer->getCustomer(['id' => array_column($data, 'customerId')], 'name,id'); $customer = []; if ($dbResult->isSuccess()) { $customer = $dbResult->getData(); } foreach ($data as &$val) { $val['minPrice'] = ($val['minPrice'] == 0) ? '不限金额' : $val['minPrice']; $val['useShopName'] = ($val['useShop'] === 0) ? ['全店铺'] : array_values(self::getNameByShopId(explode(',', $val['useShop']))); $val['customerName']= isset($customer[$val['customerId']]['name']) ? $customer[$val['customerId']]['name'] : ''; $val['orderData'] = isset($val['orderData']) && !empty($val['orderData']) ? json_decode($val['orderData'],true):[]; } return $data; } /** * @param $selectParams * @param bool $isQuery * @param string $fields * @param string $countQuery * @return ResultWrapper */ public function getUserCoupon($selectParams, $isQuery = false, $fields = "*", $countQuery = null) { if ($isQuery) { $dbResult = $this->objDUserCoupon->query($selectParams); } else { $dbResult = $this->objDUserCoupon->select($selectParams, $fields); } if ($dbResult === false) { return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror); } if ($isQuery) { $total = $this->objDUserCoupon->query($countQuery); $dbResult = [ 'data' => $dbResult, 'total' => count($total), ]; } return ResultWrapper::success($dbResult); } /** * 店铺数据 * @param $shopIds * @return array */ public function getNameByShopId($shopIds) { $objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->onlineEnterpriseId); $result = []; foreach ($shopIds as $shopId) { if (!empty($shopId)) $result[] = $objGoodsBasicRelevantCache->getNameByShopId($shopId); } return $result; } /** * 获取可用的优惠券 * @param $selectParams * @return ResultWrapper */ public function availableCoupon($selectParams) { // $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'] . ')'; $nowTime = time(); $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'] . ')'; $dbResult = $this->objDUserCoupon->query($sql); if ($dbResult === false) { return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * Doc: (des="获取用户可以使用的优惠券数量") * User: XMing * Date: 2021/3/17 * Time: 5:07 下午 * @param int $userCenterId * @return ResultWrapper */ public function getCouponNum(int $userCenterId): ResultWrapper { $nowTime = time(); $sql = 'SELECT COUNt(id) as total FROM qianniao_user_coupon_' . $this->onlineEnterpriseId . ' WHERE isUse=' . StatusCode::$standard . ' AND userId = ' . $userCenterId . ' AND endTime>' . $nowTime; $dbResult = $this->objDUserCoupon->query($sql); if ($dbResult === false) { return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror); } $total = isset($dbResult[0]['total']) ? (int)$dbResult[0]['total'] : 0; return ResultWrapper::success($total); } /** * @param $selectParams * @param $data * @return ResultWrapper */ public function updateIsUse($selectParams, $data) { $dbResult = $this->objDUserCoupon->update($data, $selectParams); if ($dbResult === false) { return ResultWrapper::fail($this->objDUserCoupon->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 搜索 * @param $selectParams */ public function search($selectParams) { $defaultDSL = [ 'from' => $selectParams['offset'], 'size' => $selectParams['limit'], 'sort' => [ 'createTime' => [ 'order' => 'desc' ], ], ]; $dsl = []; $dsl['query']['bool']['must'][] = [ 'term' => ['enterpriseId' => $this->onlineEnterpriseId], ]; } }