123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace app\store\model\card;
- use app\common\model\card\RiceCard as RiceCardModel;
- /**
- * 米卡模型
- * @package app\common\model
- */
- class RiceCard extends RiceCardModel
- {
- protected $append = ['total_stock', 'status_text'];
-
- public function setDkCatIdsAttr($value)
- {
- return !empty($value) ? implode(',', $value) : '';
- }
- public function getDkCatIdsAttr($value, $data)
- {
- return !empty($value) ? explode(',', $value) : [];
- }
- public function getList(array $param = [])
- {
- // 检索查询条件
- $query = $this->setQueryFilter($param);
- // 查询列表数据
- return $query->with(['image'])
- ->where("is_delete", '=', 0)
- ->order(["id" => 'desc'])
- ->paginate(15);
- }
- public function getListAll(array $param = [])
- {
- // 检索查询条件
- $query = $this->setQueryFilter($param);
- // 查询列表数据
- return $query->where("is_delete", '=', 0)
- ->where('status',1)
- ->where('type',$param['type']??3)
- ->field('id,name,type,stock,sale_num,status')
- ->order(["id" => 'desc'])
- ->select();
- }
- /**
- * 米卡简易列表
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getSimpleList()
- {
- // 查询列表数据
- return self::where('type',2)->where("is_delete", '=', 0)
- ->order(["id" => 'desc'])
- ->field('id,name,stock,sale_num,status')->select()->toArray();
- }
- /**
- * 检索查询条件
- * @param array $param
- * @return \think\db\BaseQuery
- */
- private function setQueryFilter(array $param)
- {
- // 实例化查询对象
- $query = $this->getNewQuery();
- // 查询参数
- $params = $this->setQueryDefaultValue($param, [
- 'name' => '', // 米卡名称
- 'status' => -1, // 评价状态 -1全部 0-已下架 1-上架中
- ]);
-
- // 米卡名称
- !empty($params['name']) && $query->where('name', 'like', "%{$params['name']}%");
- // 米卡状态
- $params['status'] > -1 && $query->where("{$this->name}.status", '=', $params['status']);
-
- return $query;
- }
- /**
- * 获取详情
- * @param int $id
- * @return Comment|array|null
- */
- public function getDetail(int $id)
- {
- // 详情
- $detail = static::detail($id, ['image', 'riceCardGoods', 'riceCardGoodsExcept', 'riceCardPriceValues']);
-
- return $detail;
- }
- /**
- * 新增记录
- * @param array $data
- * @return bool
- */
- public function add(array $data)
- {
- $this->transaction(function () use ($data) {
-
- // 新增记录
- $this->save($data);
- // 新增关联数据
- if (!empty($data['rice_card_price_values'])) { // 米卡面额-现金卡
- (new RiceCardPriceValues())->add($this->id, $data['rice_card_price_values']);
- }
- // 米卡除外商品-现金卡
- if(isset($data['rice_card_goods_expect'])){
- $riceCardGoodsModel = new RiceCardGoods();
- $riceCardGoodsModel->add($this->id, RiceCardGoods::EXCEPT_YES, $data['rice_card_goods_expect']);
- }
-
- // 米卡兑换商品-兑换卡
- if(isset($data['rice_card_goods'])){
- $riceCardGoodsModel = new RiceCardGoods();
- $riceCardGoodsModel->add($this->id, RiceCardGoods::EXCEPT_NO, $data['rice_card_goods']);
- }
- });
- return true;
- }
- /**
- * 更新记录
- * @param array $data
- * @return bool
- */
- public function edit(array $data)
- {
- $this->transaction(function () use ($data) {
-
- // 更新记录
- $this->save($data);
- // 更新关联数据
- if (!empty($data['rice_card_price_values'])) { // 米卡面额-现金卡
- (new RiceCardPriceValues())->add($this->id, $data['rice_card_price_values']);
- }
- $riceCardGoodsModel = new RiceCardGoods();
- // 米卡除外商品-现金卡
- if(isset($data['rice_card_goods_expect'])){
- $riceCardGoodsModel->add($this->id, RiceCardGoods::EXCEPT_YES, $data['rice_card_goods_expect']);
- }
- if (isset($data['rice_card_goods'])) {
- //米卡可兑换商品-兑换卡 todo 下一期需求开发
- $riceCardGoodsModel->add($this->id, RiceCardGoods::EXCEPT_NO, $data['rice_card_goods']);
- }
- });
- return true;
- }
- /**
- * @param array $ids
- * @param bool $state
- * @return bool
- */
- public function setStatus(array $ids, bool $state)
- {
- // 批量更新记录
- return static::updateBase(['status' => $state ? 1 : 0], [['id', 'in', $ids]]);
- }
- }
|