| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace app\modules\admin\models\team_grades;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\TeamGradesGoods;
- use yii\base\Model;
- class TeamGoodsForm extends Model
- {
- public $id;
- public $ids;
- public $store_id;
- public $goods_id;
- public $goods_team_grades;
- public $cat_id;
- public $goods_name;
- public function rules()
- {
- return [
- [['store_id', 'goods_id', 'goods_team_grades', 'id', 'cat_id'], 'integer'],
- [['goods_name', 'ids'], 'string']
- ];
- }
- //设置商品业绩分红值
- public function setGoods() {
- try {
- $id = $this->id;
- $store_id = $this->store_id;
- $goods_id = $this->goods_id;
- $goods_team_grades = $this->goods_team_grades;
- $goods = \app\models\Goods::findOne(['id' => $goods_id, 'is_delete' => 0]);
- if (!$goods) {
- throw new \Exception('商品不存在');
- }
- $form = TeamGradesGoods::findOne($id);
- if (!$form) {
- $form = TeamGradesGoods::findOne(['goods_id' => $goods_id, 'is_delete' => 0]);
- }
- $form = $form ?: new TeamGradesGoods();
- $form->goods_team_grades = $goods_team_grades;
- $form->goods_id = $goods_id;
- $form->store_id = $store_id;
- if (!$form->save()) {
- throw new \Exception('商品不存在');
- }
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function list() {
- $store_id = $this->store_id;
- $cat_id = $this->cat_id;
- $goods_name = $this->goods_name;
- $query = TeamGradesGoods::find()->alias('tg')
- ->leftJoin(['g' => Goods::tableName()], 'g.id=tg.goods_id')
- ->where(['tg.is_delete' => 0, 'tg.store_id' => $store_id, 'g.is_delete' => 0]);
- if (!empty(trim($goods_name))) {
- $query->andWhere(['LIKE', 'g.name', trim($goods_name)]);
- }
- if (isset($cat_id) && $cat_id > 0) {
- $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
- 'gc.cat_id' => Cat::getCatId($cat_id)
- ])->groupBy('gc.goods_id');
- }
- $query->orderBy('tg.id DESC')->select('tg.id, tg.goods_team_grades, g.id goods_id, g.name, g.cover_pic');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$list) {
- $goods_cat = GoodsCat::find()->alias('gc')
- ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
- ->where([ 'gc.goods_id' => $list['goods_id'], 'c.is_delete' => 0, 'gc.is_delete' => 0, 'c.store_id' => $store_id ])
- ->select(['c.name', 'c.id'])
- ->asArray()
- ->all();
- $list['cat'] = $goods_cat;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $pagination['list'],
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ]
- ];
- }
- //删除商品业绩分红值
- public function del() {
- try {
- $ids = $this->ids;
- $ids = explode(',', $ids);
- foreach ($ids as $id) {
- $form = TeamGradesGoods::findOne($id);
- if (!$form) {
- throw new \Exception('数据不存在');
- }
- $form->is_delete = 1;
- if (!$form->save()) {
- throw new \Exception('删除失败');
- }
- }
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- }catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|