123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- /**
- * 优惠券记录
- * Created by PhpStorm.
- * User: XiaoMing
- * Date: 2019/12/13
- * Time: 16:20
- */
- namespace JinDouYun\Model\Market;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Cache\GoodsBasicRelevant;
- use JinDouYun\Dao\Market\DUserCoupon;
- use JinDouYun\Model\Customer\MCustomer;
- use JinDouYun\Model\MBaseModel;
- use Mall\Framework\Core\StatusCode;
- class MUserCoupon extends MBaseModel
- {
- private $onlineUserId;
- private $onlineEnterpriseId;
- private $objDUserCoupon;
- /**
- * MUserCoupon constructor.
- * @param $onlineUserId
- * @param $onlineEnterpriseId
- */
- public function __construct($onlineUserId, $onlineEnterpriseId)
- {
- $this->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],
- ];
- }
- }
|