| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace app\modules\admin\models\integralAppreciation;
- use app\models\Goods;
- use app\models\IntegralAppreciationGoods;
- use yii\base\Model;
- class GoodsForm extends Model
- {
- public $store_id;
- public $goods_id;
- public $reflux_amount;
- public $integral_amount;
- public $goods_name;
- public $start_time;
- public $end_time;
- public $id;
- public $ids;
- public function rules()
- {
- return [
- [['store_id', 'goods_id', 'id'], 'integer'],
- [['reflux_amount', 'integral_amount'], 'number'],
- [['ids', 'goods_name', 'start_time', 'end_time'], 'string']
- ];
- }
- public function attributeLabels()
- {
- return [
- 'store_id' => '商城ID',
- 'goods_id' => '商品ID',
- 'reflux_amount' => '回流奖金池金额',
- 'integral_amount' => '增值积分金额',
- 'id' => 'ID',
- 'ids' => 'ID',
- ];
- }
- /**
- * 保存
- */
- public function save() {
- try {
- $id = $this->id;
- $goods_id = $this->goods_id;
- $store_id = $this->store_id;
- $reflux_amount = $this->reflux_amount;
- $integral_amount = $this->integral_amount;
- $goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0, 'store_id' => $store_id]);
- if (!$goods) {
- throw new \Exception('商品不存在');
- }
- if ($reflux_amount <= 0) {
- throw new \Exception('回流奖金池金额必须大于0');
- }
- // if ($integral_amount <= 0) {
- // throw new \Exception('增值积分金额必须大于0');
- // }
- if ($id) {
- $model = IntegralAppreciationGoods::findOne(['id' => $id, 'is_delete' => 0]);
- } else {
- $model = IntegralAppreciationGoods::findOne(['goods_id' => $goods_id, 'is_delete' => 0]);
- if ($model) {
- throw new \Exception('该商品已存在');
- }
- $model = new IntegralAppreciationGoods();
- $model->store_id = $store_id;
- }
- $model->goods_id = $goods_id;
- $model->reflux_amount = $reflux_amount;
- $model->integral_amount = $integral_amount;
- if (!$model->save()) {
- throw new \Exception(implode(',', $model->getFirstErrors()));
- }
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 列表
- */
- public function getList() {
- $goods_name = $this->goods_name;
- $start_time = $this->start_time;
- $end_time = $this->end_time;
- $store_id = $this->store_id;
- $query = IntegralAppreciationGoods::find()->alias('ig')
- ->leftJoin(['g' => Goods::tableName()], 'ig.goods_id = g.id')
- ->where(['ig.is_delete' => 0, 'g.is_delete' => 0, 'ig.store_id' => $store_id]);
- if ($goods_name) {
- $query->andWhere(['like', 'g.name', $goods_name]);
- }
- if ($start_time) {
- $start_time = strtotime($start_time);
- $query->andWhere(['>=', 'ig.created_at', $start_time]);
- }
- if ($end_time) {
- $end_time = strtotime($end_time);
- $query->andWhere(['<=', 'ig.created_at', $end_time]);
- }
- $query->orderBy('ig.id DESC')
- ->select('ig.id, ig.goods_id, ig.reflux_amount, ig.integral_amount, ig.created_at, g.name, g.cover_pic, g.price');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => $list
- ];
- }
- /**
- * 删除 / 批量
- */
- public function del() {
- try {
- $ids = $this->ids;
- $ids = explode(',', $ids);
- if (!$ids) {
- throw new \Exception('请选择要删除的项');
- }
- foreach ($ids as $id) {
- $goods = IntegralAppreciationGoods::findOne($id);
- $goods->is_delete = 1;
- if (!$goods->save()) {
- throw new \Exception(implode(',', $goods->getFirstErrors()));
- }
- }
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|