| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace bizHd\promote\services;
- use bizHd\base\services\BaseService;
- use common\components\arrayUtil;
- use common\components\error;
- use common\components\util;
- use Yii;
- class CouponModelService extends BaseService
- {
-
- public static $baseFile = '\bizHd\promote\classes\CouponModelClass';
-
- //列出商家所有的卷模板
- public static function getCouponList($sjId)
- {
- $list = self::getAllList('*', ['sjId' => $sjId], 'level desc');
- return $list;
- }
-
- //按type、targetId作为键,列出商家所有的卷模板
- public static function getKeySortCouponList()
- {
- $list = self::getCouponList();
- if (empty($list)) {
- return [];
- }
- $data = [];
- foreach ($list as $val) {
- $key = $val['type'] . '_' . $val['targetId'];
- $data[$key][] = $val;
- }
- return $data;
- }
-
- //获取通用的优惠卷,用于首次、二次和三次消费时送卷 ssh 2019.9.18
- public static function getCommonCoupon()
- {
- $list = self::getCouponList();
- if (empty($list)) {
- return [];
- }
- $coupon = [];
- foreach ($list as $key => $val) {
- $type = $val['type'];
- $targetId = $val['targetId'];
- if ($type == 0) {
- if (in_array($targetId, [0, 1, 2])) {
- $coupon[$targetId] = $val;
- }
- }
- }
- return $coupon;
- }
-
- //根据分类和用途取可以得到的优惠卷,并且按优先级排序 ssh
- public static function getAvailableCouponListById($categoryId, $usageId)
- {
- $rightList = [];
- $couponList = self::getKeySortCouponList();
- /**找出分类卷**/
- $categoryKey = '1_' . $categoryId;
- if (isset($couponList[$categoryKey])) {
- $rightList = array_merge($rightList, $couponList[$categoryKey]);
- }
- /**找出用途卷**/
- $usageKey = '2_' . $usageId;
- if (isset($couponList[$usageKey])) {
- $rightList = array_merge($rightList, $couponList[$usageKey]);
- }
- /**找出通用卷 0_开头**/
- foreach ($couponList as $key => $val) {
- if (strstr($key, '0_')) {
- $rightList = array_merge($rightList, $val);
- }
- }
- if (empty($rightList)) {
- return [];
- }
- /**按优先级排序**/
- $rightList = arrayUtil::arraySort($rightList, 'level');
- return $rightList;
- }
-
- //添加或更新卷模板 ssh 2019.8.18
- public static function replaceModel($data, $sjId)
- {
- $valid = array_keys($data);
- $diff = array_diff(['type', 'targetId', 'amount', 'meetAmount', 'reward', 'name', 'id'], $valid);
- if (!empty($diff)) {
- error::instance()->setError('参数错误');
- return false;
- }
- $type = $data['type'];
- $id = $data['id'];
- unset($data['id']);
- $data['validTime'] = !empty($data['validTime']) && is_numeric($data['validTime']) ? $data['validTime'] : 10;
- if (!empty($id)) {
- self::updateById($id, $data);
- }
- $targetId = $data['targetId'];
- //分类和用途类优惠卷不能重复添加 ssh 2019.8.17
- if (in_array($type, [1, 2])) {
- $has = self::getByCondition(['sjId' => $sjId, 'type' => $type, 'targetId' => $targetId]);
- if (!empty($has)) {
- error::instance()->setError('已经添加过了');
- return false;
- }
- }
- $time = time();
- $createTime = date("Y-m-d H:i:s");
- $data['sjId'] = $sjId;
- $data['addTime'] = $time;
- $data['createTime'] = $createTime;
- self::add($data);
- return true;
- }
-
- //取出按商品分类设置的优惠券 ssh 2019.8.18
- public static function getCategoryCoupon($sjId)
- {
- return self::getAllByCondition(['sjId' => $sjId, 'type' => 1], null, '*', 'targetId');
- }
-
- //取出按用途设置的优惠券 ssh 2019.8.18
- public static function getUsageCoupon($sjId)
- {
- return self::getAllByCondition(['sjId' => $sjId, 'type' => 2], null, '*', 'targetId');
- }
-
- //取优惠卷信息并确认是否商家自己的
- public static function getCouponInfo($id, $sjId)
- {
- $info = self::getById($id);
- if (empty($info)) {
- util::fail('没有优惠卷信息');
- }
- if ($info['sjId'] != $sjId) {
- util::fail('你无法操作');
- }
- return $info;
- }
-
- }
|