| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models\purchase;
- use app\models\PurchaseFavorite;
- use yii\base\Model;
- use yii\data\Pagination;
- use app\modules\admin\models\PlatformForm;
- class FavoriteForm extends Model
- {
- public $store_id;
- public $user_id = 0;
- public $saas_id = 0;
- public $goods_ids;
- public $type = 0;
- public $page = 1;
- public $limit = 999;
- public function rules()
- {
- return [
- [['type'], 'default', 'value' => 0],
- ['type', 'in', 'range' => [PurchaseFavorite::FAVORITE_GOODS, PurchaseFavorite::FAVORITE_SHOP]],
- [['goods_ids', 'page', 'limit', 'saas_id', 'store_id'], 'safe'],
- ];
- }
-
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- if ($this->type == PurchaseFavorite::FAVORITE_GOODS) {
- $query = PurchaseFavorite::find()->where(['type' => PurchaseFavorite::FAVORITE_GOODS, 'is_delete' => 0, 'saas_id' => $this->saas_id]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
- $list = $query->select('*')->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all();
- foreach ($list as $i => $favorite) {
- $form = new PlatformForm();
- $form->id = $favorite['goods_id'];
- $glist = $form->goodsInfo();
- $goods = '';
- if($glist['code'] == 0 && $glist['data']['count'] == 1){
- $goods = $glist['data']['list'][0];
- }
- if (!$goods) {
- continue;
- }
- if ($goods->is_negotiable) {
- $goods->price = Goods::GOODS_NEGOTIABLE;
- }
- $list[$i]['goods'] = $goods;
- }
- $newList = [];
- foreach ($list as $i => $v){
- $supid = $v['goods']['supplier_id'];
- $newList[$supid]['supplier'] = $v['goods']['supplier'];
- $newList[$supid]['goods_list'][] = $v;
- }
- return [
- 'code' => 0,
- 'data' => (object)[
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => array_values($newList),
- ],
- ];
- }
- }
- public function add()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- $errors = 0;
- foreach ($this->goods_ids as $gid) {
- $existFavorite = PurchaseFavorite::findOne([
- 'saas_id' => $this->saas_id,
- 'goods_id' => $gid,
- 'type' => $this->type,
- ]);
- if ($existFavorite) {
- $existFavorite->is_delete = 0;
- if (!$existFavorite->save()) {
- $errors++;
- }
- }else{
- $favorite = new PurchaseFavorite();
- $favorite->store_id = $this->store_id;
- $favorite->user_id = $this->user_id;
- $favorite->saas_id = $this->saas_id;
- $favorite->goods_id = $gid;
- $favorite->type = $this->type;
- $favorite->created_at = time();
- if (!$favorite->save()) {
- $errors++;
- }
- }
- }
- if($errors == count($this->goods_ids)){
- return [
- 'code' => 1,
- 'msg' => 'fail',
- ];
- }
- if($errors){
- return [
- 'code' => 0,
- 'msg' => '收藏失败数量:' . $errors,
- ];
- }else{
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- }
- }
- public function remove()
- {
- $res = PurchaseFavorite::updateAll(['is_delete' => 1], ['saas_id' => $this->saas_id, 'goods_id' => $this->goods_ids, 'is_delete' => 0]);
- if ($res) {
- return [
- 'code' => 0,
- 'msg' => '删除成功',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => 'fail',
- 'err' => $res
- ];
- }
- }
- }
|