| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- namespace addons\Scratch\common\logic;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Scratch\common\enums\ScratchEnum;
- use addons\Scratch\common\models\ScratchCouponModel;
- use addons\Scratch\common\models\ScratchLogModel;
- use addons\Scratch\common\models\ScratchModel;
- use common\models\base\User;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use common\models\mall\MallAddons;
- /**
- * Class ScratchLogic
- * @package addons\Scratch\common\logic
- */
- class ScratchLogic
- {
- /**
- * 列表
- * @param int $mallId
- * @param int $userId
- * @param array $postData
- * @return mixed
- * @throws \ErrorException
- */
- public static function getList($mallId = 0, $userId = 0, $postData = [])
- {
- try {
- $params = [
- 'with' => [
- 'goods' => function($q){
- $q->select('id,goods_name');
- }
- ],
- 'where' => [
- ['=', 'mall_id', $mallId],
- ['in', 'status', [ScratchEnum::ENABLED, ScratchEnum::DISABLED]],
- ],
- 'page' => $postData['page'] ?? 1,
- 'limit' => $postData['limit'] ?? 15,
- 'order' => $postData['order'] ?? 'id desc',
- ];
- if(!empty($postData['type'])){
- $params['where'][] = ['=', 'type', $postData['type']];
- }
- if(!empty($postData['keyword'])){
- $params['where'][] = ['like', 'name', $postData['keyword']];
- }
- $pageData = ScratchModel::listPage($params, false);
- if ($pageData === false) throw new \ErrorException(ScratchModel::getStaticError());
- foreach ($pageData['list'] as &$vo) {
- if($vo['type'] == ScratchModel::TYPE_COUPON){
- $vo['coupon_data'] = ScratchCouponModel::getData($vo['coupon_id'], 'id,name');
- }
- }
- $pageData['types'] = ScratchModel::getTypeList($mallId);
- return $pageData;
- } catch (\ErrorException $errorException) {
- throw new \ErrorException($errorException->getMessage());
- } catch (\Exception $exception) {
- throw new \Exception($exception->getMessage());
- }
- }
- /**
- * 列表
- * @param int $mallId
- * @param int $userId
- * @param array $postData
- * @return mixed
- * @throws \ErrorException
- */
- public static function getLogList($mallId = 0, $userId = 0, $postData = [])
- {
- try {
- $params = [
- 'with' => [
- 'scratch',
- 'user' => function($q){
- $q->select('id,nickname,avatar_url,mobile');
- },
- 'goods' => function($q){
- $q->select('id,goods_name');
- },
- ],
- 'where' => [
- ['=', 'mall_id', $mallId],
- ],
- 'page' => $postData['page'] ?? 1,
- 'limit' => $postData['limit'] ?? 20,
- 'order' => $postData['order'] ?? 'id desc',
- ];
- if(!empty($postData['type'])){
- $params['where'][] = ['=', 'type', $postData['type']];
- }
- if(!empty($userId)){
- $params['where'][] = ['=', 'user_id', $userId];
- }
- $is_search = 0;
- $user_id_arr = [];
- if (!empty($postData['keyword'])) {
- $is_search = 1;
- $user_list = User::lists([
- 'where' => [
- ['mall_id' => $mallId],
- [
- 'or',
- // ['like', 'username', $postData['keyword']],
- ['like', 'nickname', $postData['keyword']],
- ['like', 'mobile', $postData['keyword']]
- ]
- ],
- 'select' => 'id']);
- $user_ids = array_column($user_list, 'id');
- if (!empty($user_id_arr)) {
- $user_id_arr = array_intersect($user_id_arr, $user_ids);
- } else {
- $user_id_arr = $user_ids;
- }
- }
- if ($is_search) $params['where'][] = ['in', 'user_id', $user_id_arr];
- $pageData = ScratchLogModel::listPage($params, false);
- if ($pageData === false) throw new \ErrorException(ScratchModel::getStaticError());
- foreach ($pageData['list'] as &$vo) {
- if($vo['type'] == ScratchModel::TYPE_COUPON){
- $vo['coupon_data'] = ScratchCouponModel::getData($vo['coupon_id'], 'id,name');
- }
- }
- $pageData['types'] = ScratchModel::getTypeList($mallId);
- return $pageData;
- } catch (\ErrorException $errorException) {
- throw new \ErrorException($errorException->getMessage());
- } catch (\Exception $exception) {
- throw new \Exception($exception->getMessage());
- }
- }
- /**
- * @param $id
- * @return array|false|mixed|void
- * @throws \ErrorException
- */
- public static function getEdit($id, $mallId)
- {
- try {
- //所有可用的优惠券
- $coupon = AddonsCoupon::getMallCoupon($mallId);
- $list = ScratchModel::find()
- ->with([
- 'attr.goods',
- 'coupon',
- ])
- ->where([
- 'mall_id' => $mallId,
- 'id' => $id
- ])
- ->asArray()
- ->one();
- if ($list && $list['type'] == ScratchModel::TYPE_GOODS && $list['attr']) {
- $goodsAttr = GoodsAttr::find()->where(['is_delete' => 0, 'goods_id' => $list['attr']['goods_id']])->asArray()->all();
- foreach ($goodsAttr as $key => $item) {
- $attr_list = (new Goods())->signToAttr($item['sign_id'], $list['attr']['goods']['attr_groups']);
- $attr_str = '';
- foreach ($attr_list as $item1) {
- $attr_str .= $item1['attr_group_name'] . ':' . $item1['attr_name'] . ';';
- }
- $goodsAttr[$key]['attr_str'] = $attr_str;
- }
- $list['attr_list'] = $goodsAttr;
- $list['goods_name'] = $list['attr']['goods']['name'];
- unset($list['attr']);
- }
- return [
- 'list' => $list,
- 'types' => ScratchModel::getTypeList($mallId),
- 'is_coupon' => MallAddons::isInstall($mallId, 'Coupon'),
- 'coupon' => $coupon,
- ];
- } catch (\ErrorException $errorException) {
- throw new \ErrorException($errorException->getMessage());
- } catch (\Exception $exception) {
- throw new \Exception($exception->getMessage());
- }
- }
- /**
- * @param $id
- * @return array|false|mixed|void
- * @throws \ErrorException
- */
- public static function searchGoods($mallId, $postData)
- {
- try {
- $params = [
- 'with' => [
- 'attr' => function($q){
- $q->select('id,goods_id,name,stock');
- }
- ],
- 'where' => [
- ['=', 'mall_id', $mallId],
- ['in', 'status', [ScratchEnum::ENABLED, ScratchEnum::DISABLED]],
- ],
- 'select' => 'id,mall_id,goods_name',
- 'page' => $postData['page'] ?? 1,
- 'limit' => $postData['limit'] ?? 20,
- 'order' => $postData['order'] ?? 'id desc',
- ];
- if(!empty($postData['keyword'])){
- $params['where'][] = ['like', 'goods_name', $postData['keyword']];
- }
- $list = Goods::getGoodsList($params, true);
- return $list;
- } catch (\ErrorException $errorException) {
- throw new \ErrorException($errorException->getMessage());
- } catch (\Exception $exception) {
- throw new \Exception($exception->getMessage());
- }
- }
- /**
- * @param $mallId
- * @param $params
- * @return array
- * @throws \ErrorException
- */
- public static function saveEdit($mallId, $params)
- {
- try {
- $model = ScratchModel::setData($mallId, $params);
- //商品
- if ($params['type'] == ScratchModel::TYPE_GOODS) {
- $model->goods_id = $params['goods_id'] ?? 0;
- $model->attr_id = $params['attr_id'] ?? 0;
- $attrData = GoodsAttr::findOne([
- 'id' => $model->attr_id,
- 'goods_id' => $model->goods_id,
- 'status' => ScratchEnum::ENABLED,
- ]);
- if(empty($attrData)){
- throw new \ErrorException('商品不存在或已删除');
- }
- }
- // 优惠券
- if($params['type'] == ScratchModel::TYPE_COUPON ){
- self::setCouponCount($model);
- }
- if (!$model->save()) {
- throw new \ErrorException($model->getErrorMessage());
- }
- return true;
- } catch (\ErrorException $errorException) {
- throw new \ErrorException($errorException->getMessage());
- } catch (\Exception $exception) {
- throw new \Exception($exception->getMessage());
- }
- }
- /**
- * @param $mallId
- * @param $params
- * @return array
- * @throws \ErrorException
- */
- public static function saveEditStatus($mallId, $params)
- {
- try {
- $id = $params['id'] ?? 0;
- $model = ScratchModel::findOne($id);
- if(empty($model)){
- throw new \ErrorException('信息不存在');
- }
- $model->status = $params['status'] ?? 0;
- if (!$model->save()) {
- throw new \ErrorException($model->getErrorMessage());
- }
- return true;
- } catch (\ErrorException $errorException) {
- throw new \ErrorException($errorException->getMessage());
- } catch (\Exception $exception) {
- throw new \Exception($exception->getMessage());
- }
- }
- /**
- * @param ScratchModel $model
- * @return bool
- */
- private static function setCouponCount(ScratchModel $model){
- $coupon = new ScratchCouponModel();
- $old_stock = $model->getOldAttribute('stock');
- $new_stock = $old_stock - $model->stock;
- if($model->isNewRecord){
- // 新增,减优惠券数据量
- $coupon->updateCount($model->stock, 'sub', $model->coupon_id);
- return true;
- }
- // 修改
- if($new_stock > 0){
- // 减少了优惠券数据量,要把它加回去
- $res = $coupon->updateCount(abs($new_stock), 'add', $model->coupon_id);
- }else{
- // 增加了优惠券数量,要减出来
- $res = $coupon->updateCount(abs($new_stock), 'sub', $model->coupon_id);
- }
- return true;
- }
- }
|