[ '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; } }