| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- <?php
- namespace addons\ReservationService\common\services;
- use addons\ReservationService\common\enums\ReservationServiceEnum;
- use addons\ReservationService\common\models\ReservationServiceGoods;
- use addons\ReservationService\common\models\ReservationServiceStoreGoods;
- use addons\ReservationService\common\models\ReservationServiceUser;
- use addons\Store\common\models\StoreGoods;
- use addons\Store\common\models\StoreGoodsCate;
- use addons\Store\common\models\StoreGoodsCateRelate;
- use common\enums\GoodsTypeEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\goods\GoodsCate;
- use common\models\goods\GoodsCateRelate;
- use common\models\goods\GoodsReserve;
- use Yii;
- class GoodsService
- {
- /**
- * 获取所有类目
- * @param int $mall_id
- * @param int $user_id
- * @param int $parent_id
- * @return array|false|mixed
- */
- public function cate(int $mall_id, int $user_id, int $parent_id = 0)
- {
- // 取出类目
- $cats = GoodsCate::lists([
- 'where' => [
- // ['id' => $cat_ids],
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['parent_id' => $parent_id]
- ],
- 'select'=> 'id, name, pic'
- ]);
- if (!empty($cats)) {
- $b_goods_ids = $this->getUserBindGoodsIds($mall_id, $user_id);
- foreach ($cats as $key => &$cat) {
- $ggids = array_unique(GoodsCateRelate::find()
- ->where(['cate_id' => GoodsCate::getAllCate($mall_id, [$cat['id']])])
- ->select('goods_id')->column());
- $ggids = Goods::find()->where(['id' => $ggids])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['goods_type' => 3])
- ->andWhere(['is_on_sale' => StatusEnum::ENABLED])
- ->select('id')->column();
- $ggids = GoodsReserve::find()->where(['goods_id' => $ggids])
- ->andWhere(['reserve_type' => StatusEnum::ENABLED])->select('goods_id')->column();
- if (empty($ggids)) unset($cats[$key]);
- $cat['total_project'] = count($ggids);
- $cat['total_add'] = count(array_intersect($ggids, $b_goods_ids));
- }
- }
- return $cats;
- // 取出商品
- // 通过商品ID取出类目
- $gids = $this->getReserveGoodsIds($mall_id);
- $cats = [];
- if (empty($parent_id)) {
- if (!empty($gids)) {
- // 取出类目ID
- $g_m_c = GoodsCateRelate::lists([
- 'where' => [
- ['goods_id' => $gids]
- ],
- 'select'=> 'goods_id, cate_id',
- 'index' => 'goods_id'
- ]);
- if (!empty($g_m_c)) {
- $b_goods_ids = $this->getUserBindGoodsIds($mall_id, $user_id);
- $cat_ids = array_unique(array_column($g_m_c, 'cate_id'));
- // 如果上级ID为0那么
- $cats = GoodsCate::lists([
- 'where' => [
- ['id' => $cat_ids],
- ['status' => StatusEnum::ENABLED],
- ['parent_id' => $parent_id]
- ],
- 'select'=> 'id, name, pic'
- ]);
- foreach ($cats as &$cat) {
- $ggids = array_unique(GoodsCateRelate::find()
- ->where(['cate_id' => GoodsCate::getAllCate($mall_id, [$cat['id']])])
- ->select('goods_id')->column());
- $ggids = Goods::find()->where(['id' => $ggids])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['goods_type' => 3])
- ->andWhere(['is_on_sale' => StatusEnum::ENABLED])
- ->select('id')->column();
- $ggids = GoodsReserve::find()->where(['goods_id' => $ggids])
- ->andWhere(['reserve_type' => StatusEnum::ENABLED])->select('goods_id')->column();
- $cat['total_project'] = count($ggids);
- $cat['total_add'] = count(array_intersect($ggids, $b_goods_ids));
- }
- }
- }
- } else {
- $cats = GoodsCate::lists([
- 'where' => [
- ['status' => StatusEnum::ENABLED],
- ['parent_id' => $parent_id]
- ],
- 'select'=> 'id, name, pic'
- ]);
- }
- return $cats;
- }
- /**
- * 获取商品
- * @param int $mall_id
- * @param int $user_id
- * @param int $cate_id
- * @return array|false|mixed
- */
- public function goods(int $mall_id, int $user_id, int $cate_id = 0)
- {
- $ids = $this->getUserBindGoodsIds($mall_id, $user_id);
- if (empty($cate_id)) {
- $goods_ids = $this->getReserveGoodsIds($mall_id);
- } else {
- // 类目下面的所有商品ID
- $goods_ids = GoodsCateRelate::find()->where(['cate_id' => GoodsCate::getAllCate($mall_id, [$cate_id])])->select('goods_id')->column();
- // 过滤掉非预约项目的商品
- if (!empty($goods_ids)) {
- $goods_ids = GoodsReserve::find()->where(['goods_id' => $goods_ids])
- ->andWhere(['reserve_type' => StatusEnum::ENABLED])->select('goods_id')->column();
- }
- }
- if (empty($goods_ids)) return [];
- $where = [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['is_on_sale' => StatusEnum::ENABLED],
- ['id' => $goods_ids],
- ];
- if (!empty($ids)) {
- // $where[] = ['not in', 'id', $ids];
- }
- // 取出商品数据
- $goods = Goods::lists([
- 'where' => $where,
- 'select' => 'id, goods_name, price, original_price, unit, cover_pic',
- ]);
- if (!empty($goods)) {
- foreach ($goods as &$item) {
- $item['is_selected'] = in_array($item['id'], $ids) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
- }
- }
- return $goods;
- }
- /**
- * @param int $mall_id
- * @param int $user_id
- * @return array
- */
- public function getUserBindGoodsIds(int $mall_id, int $user_id)
- {
- return ReservationServiceGoods::find()->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->select('goods_id')->column();
- }
- /**
- * 移除已经绑定的商品
- * @param int $mall_id
- * @param int $user_id
- * @param array $goods_ids
- * @return int
- */
- public function remove(int $mall_id, int $user_id, array $goods_ids)
- {
- return ReservationServiceGoods::updateAll(['status' => StatusEnum::DELETE],
- ['and', ['mall_id' => $mall_id], ['user_id' => $user_id], ['goods_id' => $goods_ids]]);
- }
- /**
- * 绑定项目
- * @param int $mall_id
- * @param int $user_id
- * @param array $goods_ids
- * @param array $remove_goods_ids
- * @return string|bool
- */
- public function create(int $mall_id, int $user_id, array $goods_ids, array $remove_goods_ids = [])
- {
- if (!empty($goods_ids)) {
- // 判断是否为预约项目
- $t_goods_ids = GoodsReserve::find()->where(['mall_id' => $mall_id])
- ->andWhere(['reserve_type' => StatusEnum::ENABLED])
- ->andWhere(['goods_id' => $goods_ids])
- ->andWhere(['mch_id' => 0])->select('goods_id')->column();
- if (count($t_goods_ids) != count($goods_ids)) return '非法操作';
- /**
- * @var $data ReservationServiceGoods[]
- */
- $data = ReservationServiceGoods::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['goods_id' => $goods_ids],
- ['user_id' => $user_id]
- ],
- 'index' => 'goods_id',
- 'select'=> 'id, user_id, goods_id, status, mall_id'
- ], false);
- }
- $t = \Yii::$app->db->beginTransaction();
- try {
- // 有需要删除的那么直接删除
- if (!empty($remove_goods_ids)) {
- ReservationServiceGoods::updateAll(['status' => StatusEnum::DELETE],
- ['and', ['mall_id' => $mall_id], ['user_id' => $user_id], ['goods_id' => $remove_goods_ids]]);
- }
- $insert_arr = [];
- foreach ($goods_ids as $goods_id) {
- if (!empty($data[$goods_id])) {
- $data[$goods_id]->status = StatusEnum::ENABLED;
- if (!$data[$goods_id]->save()) throw new \Exception($data[$goods_id]->getErrorMessage());
- } else {
- $insert_arr[] = [
- 'created_at' => time(),
- 'updated_at' => time(),
- 'user_id' => $user_id,
- 'goods_id' => $goods_id,
- 'mall_id' => $mall_id
- ];
- }
- }
- if (!empty($insert_arr)) {
- ReservationServiceGoods::find()->createCommand()
- ->batchInsert(ReservationServiceGoods::tableName(), array_keys($insert_arr[0]), $insert_arr)->execute();
- }
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- return $e->getMessage();
- }
- return true;
- }
- /**
- * 获取预约项目的商品ID
- * @param int $mall_id
- * @return array
- */
- public function getReserveGoodsIds(int $mall_id)
- {
- $gids = GoodsReserve::find()->where(['mall_id' => $mall_id])
- ->andWhere(['reserve_type' => StatusEnum::ENABLED])->andWhere(['mch_id' => 0])->select('goods_id')->column();
- if (!empty($gids)) {
- $gids = Goods::find()->where(['id' => $gids])->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['is_on_sale' => StatusEnum::ENABLED])->select('id')->column();
- }
- return $gids;
- }
- /**
- * 获取技师绑定的商品
- * @param int $mall_id
- * @param int $id
- * @return array|false|mixed
- */
- public function getStaffBinGoods(int $mall_id, int $id)
- {
- // 获取用户ID
- /**
- * @var $user ReservationServiceUser
- */
- $user = ReservationServiceUser::getOne([
- ['mall_id' => $mall_id],
- ['id' => $id],
- ['status' => StatusEnum::ENABLED],
- ['check_status' => StatusEnum::ENABLED]
- ], false, [], 'id asc', 'user_id');
- if (empty($user)) return '技师不存在';
- $goods_ids = $this->getUserBindGoodsIds($mall_id, $user->user_id);
- if (empty($goods_ids)) return [];
- $where = [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['is_on_sale' => StatusEnum::ENABLED],
- ['id' => $goods_ids],
- ];
- if (!empty($ids)) {
- $where[] = ['not in', 'id', $ids];
- }
- // 取出商品数据
- $list = Goods::lists([
- 'where' => $where,
- 'select' => 'id, goods_name, price, original_price, unit, cover_pic, virtual_sales, sales_num',
- 'with' => ['reserve' => function ($query) {
- $query->select('goods_id, service_type');
- }]
- ]);
- if (!empty(Goods::getStaticError())) return Goods::getStaticError();
- if (!empty($list)) {
- foreach ($list as &$item) {
- $item['sales_num'] += $item['virtual_sales'];
- $item['service_type'] = [];
- switch ($item['reserve']['service_type']) {
- case 1:
- $item['service_type'][] = '上门服务';
- break;
- case 2:
- $item['service_type'][] = '到店服务';
- break;
- case 3:
- $item['service_type'][] = '上门服务';
- $item['service_type'][] = '到店服务';
- break;
- }
- unset($item['virtual_sales'], $item['reserve']);
- }
- }
- return $list;
- }
- public function globalSave(array $params)
- {
- // 后台页面组件展示
- if ($params['type'] == 'show_component') {
- return [
- 'component' => 'com-reservation-service-goods',
- 'path' => Yii::$app->basePath . '/../addons/ReservationService/backend/views/components',
- ];
- } else {
- if (empty($params['setting'][ReservationServiceEnum::ADDONS_NAME])) return true;
- $post = $params['setting'][ReservationServiceEnum::ADDONS_NAME];
- $post['mall_id'] = $params['mall_id'];
- $mall_id = $params['mall_id'];
- $goods_id = $params['goods_id'];
- $status = $post['status'];
- $model = ValetOrderGoods::getOne([
- ['mall_id' => $mall_id],
- ['goods_id' => $goods_id]
- ]);
- if (empty($model)) {
- $model = new ValetOrderGoods();
- $model->mall_id = $mall_id;
- $model->goods_id = $goods_id;
- }
- $model->status = $status;
- if (!$model->save()) return $model->getErrorMessage();
- return true;
- }
- }
- public function cateStore(int $mall_id, int $user_id, int $parent_id = 0)
- {
- // 取出类目
- $staff_user = ReservationServiceUser::getOne([
- ['user_id'=>$user_id],
- ['mall_id'=>$mall_id],
- ['status'=>StatusEnum::ENABLED]
- ]);
- if(empty($staff_user['store_id'])){
- return [];
- }
- $user_goods_ids = ReservationServiceStoreGoods::find()->where(['user_id'=>$user_id, 'status'=>1])
- ->select('goods_id')->column();
- $user_goods_list = ReservationServiceStoreGoods::find()->where(['rssg.user_id'=>$user_id, 'rssg.status'=>1])
- ->alias('rssg')
- ->leftJoin(StoreGoodsCateRelate::tableName().' as sgcr','sgcr.goods_id=rssg.goods_id')
- ->groupBy('sgcr.cate_id')
- ->select('count(sgcr.goods_id) as counts,sgcr.cate_id')
- ->asArray()
- ->all();
- if(!empty($user_goods_list)){
- $user_goods_list = array_column($user_goods_list,null,'cate_id');
- }
- $where = [
- ['goods_id' => $user_goods_ids]
- ];
- if ($parent_id > 0) {
- $where[] = ['cate_id' => $parent_id];
- }
- $list = StoreGoodsCateRelate::lists([
- 'where' => $where,
- 'select'=>'cate_id',
- 'group'=>'cate_id'
- ]);
- $store_goods_cate_list = StoreGoodsCate::lists([
- 'where'=>[
- ['id'=>array_column($list,'cate_id')]
- ],
- 'select'=>'id,name',
- 'index'=>'id'
- ]);
- $goods_ids = StoreGoods::find()->where(['store_id'=>$staff_user['store_id'],
- 'goods_type'=>GoodsTypeEnum::GoodsTypeReserved,'status'=>StatusEnum::ENABLED])
- ->select('id')->column();
- $goods_list = StoreGoods::find()->where(['sg.id'=>$goods_ids])
- ->alias('sg')
- ->leftJoin(StoreGoodsCateRelate::tableName().' as sgcr','sgcr.goods_id=sg.id')
- ->groupBy('sgcr.cate_id')
- ->select('count(sgcr.goods_id) as counts,sgcr.cate_id')
- ->asArray()
- ->all();
- if(!empty($goods_list)){
- $goods_list = array_column($goods_list,null,'cate_id');
- }
- foreach ($list as &$item){
- $item['add_count'] = $user_goods_list[$item['cate_id']]['counts']??0;
- $item['total_count'] = $goods_list[$item['cate_id']]['counts']??0;
- $item['name'] = $store_goods_cate_list[$item['cate_id']]['name']??'';
- }
- return $list;
- }
- public function goodsStore(int $mall_id, int $user_id, int $cate_id = 0)
- {
- // 取出类目
- $staff_user = ReservationServiceUser::getOne([
- ['user_id'=>$user_id],
- ['mall_id'=>$mall_id],
- ['status'=>StatusEnum::ENABLED]
- ]);
- if(empty($staff_user['store_id'])){
- return [];
- }
- $user_goods_ids = ReservationServiceStoreGoods::find()->where(['rssg.user_id'=>$user_id,'rssg.status'=>1,'sgcr.cate_id'=>$cate_id])
- ->alias('rssg')
- ->leftJoin(StoreGoodsCateRelate::tableName().' as sgcr','sgcr.goods_id=rssg.goods_id')
- ->select('sgcr.goods_id')->column();
- $where = [
- ['store_id'=>$staff_user['store_id']],
- ['id'=>$user_goods_ids]
- ];
- // 取出商品数据
- $goods = StoreGoods::lists([
- 'where' => $where,
- 'select' => 'id, goods_name, cover_pic',
- 'order'=>'created_at desc'
- ]);
- return $goods;
- }
- }
|