123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2017/12/18
- */
- namespace app\models\store;
- use app\models\store\StoreBargainUser;
- use app\models\store\StoreBargainUserHelp;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- /**
- * TODO 砍价商品Model
- * Class StoreBargain
- * @package app\models\store
- */
- class StoreBargain extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'store_bargain';
- use ModelTrait;
- public static function merSet($mer_id)
- {
- return $mer_id ? self::where('mer_id', $mer_id) : new self;
- }
- protected function getAddTimeAttr($value)
- {
- if ($value) return date('Y-m-d H:i:s', $value);
- return '';
- }
- /**
- * 正在开启的砍价活动
- * @param int $status
- * @param bool $mer_id
- * @return StoreBargain
- */
- public static function validWhere($status = 1, $mer_id = false)
- {
- return self::merSet($mer_id)->where('is_del', 0)->where('status', $status)->where('start_time', '<', time())->where('stop_time', '>', time());
- }
- /**
- * 判断砍价商品是否开启
- * @param int $bargainId
- * @param bool $mer_id
- * @return int|string
- */
- public static function validBargain($bargainId = 0, $mer_id = false)
- {
- $model = self::validWhere(1, $mer_id);
- return $bargainId ? $model->where('id', $bargainId)->count('id') : $model->count('id');
- }
- /**
- * TODO 获取正在开启的砍价商品编号
- * @return array
- */
- public static function validBargainNumber()
- {
- return self::validWhere()->column('id');
- }
- /**
- * 获取正在进行中的砍价商品
- * @param int $page
- * @param int $limit
- * @param bool $mer_id
- * @param string $field
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function getList($page = 0, $limit = 20, $mer_id = false, $field = 'id,product_id,title,price,min_price,image')
- {
- $model = self::validWhere(1, $mer_id)->field($field);
- if ($page) $model = $model->page($page, $limit);
- $list = $model->select()->each(function ($item) {
- $item['people'] = count(StoreBargainUser::getUserIdList($item['id']));
- $item['price'] = floatval($item['price']);
- });
- return $list ? $list->toArray() : [];
- }
- /**
- * TODO 获取一条正在进行中的砍价商品
- * @param int $bargainId $bargainId 砍价商品编号
- * @param bool $mer_id
- * @param string $field
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function getBargainTerm($bargainId = 0, $mer_id = false, $field = 'id,product_id,bargain_num,num,unit_name,image,title,price,min_price,image,start_time,stop_time,rule,info')
- {
- if (!$bargainId) return [];
- $model = self::validWhere(1, $mer_id);
- $bargain = $model->field($field)->where('id', $bargainId)->find();
- if ($bargain) return $bargain->toArray();
- else return [];
- }
- /**
- * 获取一条砍价商品
- * @param int $bargainId
- * @param string $field
- * @return array
- */
- public static function getBargain($bargainId = 0, $field = 'id,product_id,title,price,min_price,image')
- {
- if (!$bargainId) return [];
- $model = new self();
- $bargain = $model->field($field)->where('id', $bargainId)->find();
- if ($bargain) return $bargain->toArray();
- else return [];
- }
- /**
- * 获取最高价和最低价
- * @param int $bargainId
- * @return array
- */
- public static function getBargainMaxMinPrice($bargainId = 0)
- {
- if (!$bargainId) return [];
- return self::where('id', $bargainId)->field('bargain_min_price,bargain_max_price')->find()->toArray();
- }
- /**
- * 获取砍价次数
- * @param int $bargainId
- * @return mixed
- */
- public static function getBargainNum($bargainId = 0)
- {
- return self::where('id', $bargainId)->value('bargain_num');
- }
- /**
- * 判断当前砍价是否活动进行中
- * @param int $bargainId
- * @return bool
- */
- public static function setBargainStatus($bargainId = 0)
- {
- $model = self::validWhere();
- $count = $model->where('id', $bargainId)->count();
- if ($count) return true;
- else return false;
- }
- /**
- * 获取库存
- * @param int $bargainId
- * @return mixed
- */
- public static function getBargainStock($bargainId = 0)
- {
- return self::where('id', $bargainId)->value('stock');
- }
- /**
- * 获取字段值
- * @param $bargainId
- * @param string $field
- * @return mixed
- */
- public static function getBargainField($bargainId, $field = 'title')
- {
- return self::where('id', $bargainId)->value($field);
- }
- /**
- * 修改销量和库存
- * @param $num
- * @param $CombinationId
- * @return bool
- */
- public static function decBargainStock($num, $bargainId, $unique)
- {
- $product_id = self::where('id', $bargainId)->value('product_id');
- if ($unique) {
- $res = false !== StoreProductAttrValue::decProductAttrStock($bargainId, $unique, $num, 2);
- $res = $res && self::where('id', $bargainId)->dec('stock', $num)->inc('sales', $num)->update();
- $sku = StoreProductAttrValue::where('product_id', $bargainId)->where('unique', $unique)->where('type', 2)->value('suk');
- $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
- } else {
- $res = false !== self::where('id', $bargainId)->dec('stock', $num)->inc('sales', $num)->update();
- }
- $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
- return $res;
- }
- /**
- * TODO 增加库存减销量
- * @param $num
- * @param $bargainId
- * @return bool
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function IncBargainStock($num, $bargainId)
- {
- $bargain = self::where('id', $bargainId)->field(['stock', 'sales'])->find();
- if (!$bargain) return true;
- if ($bargain->sales > 0) $bargain->sales = bcsub($bargain->sales, $num, 0);
- if ($bargain->sales < 0) $bargain->sales = 0;
- $bargain->stock = bcadd($bargain->stock, $num, 0);
- return $bargain->save();
- }
- /**
- * TODO 获取所有砍价商品的浏览量
- * @return mixed
- */
- public static function getBargainLook()
- {
- return self::sum('look');
- }
- /**
- * TODO 获取正在开启的砍价活动
- * @return int|string
- */
- public static function getListCount()
- {
- return self::validWhere()->count();
- }
- /**
- * TODO 获取所有砍价商品的分享量
- * @return mixed
- */
- public static function getBargainShare()
- {
- return self::sum('share');
- }
- /**
- * TODO 添加砍价商品分享次数
- * @param int $id
- * @return StoreBargain|bool
- */
- public static function addBargainShare($id = 0)
- {
- if (!$id) return false;
- return self::where('id', $id)->inc('share', 1)->update();
- }
- /**
- * TODO 添加砍价商品浏览次数
- * @param int $id $id 砍价商品编号
- * @return StoreBargain|bool
- */
- public static function addBargainLook($id = 0)
- {
- if (!$id) return false;
- return self::where('id', $id)->inc('look', 1)->update();
- }
- /**
- * @param $where
- * @return array
- */
- public static function systemPage($where)
- {
- $model = new self;
- $model = self::isWhere($where, $model);
- $model = $model->order('id desc');
- $model = $model->where('is_del', 0);
- $model = $model->where('mer_id', $where['mer_id']);
- $model = self::getModelTime($where, $model, "add_time");
- $count = $model->count();
- $list = $model->page((int)$where['page'], (int)$where['limit'])
- ->select()
- ->each(function ($item) {
- if ($item['status']) {
- if ($item['start_time'] > time())
- $item['start_name'] = '活动未开始';
- else if ($item['stop_time'] < time())
- $item['start_name'] = '活动已结束';
- else if ($item['stop_time'] > time() && $item['start_time'] < time())
- $item['start_name'] = '正在进行中';
- }
- $item['count_people_all'] = StoreBargainUser::getCountPeopleAll($item['id']);//参与人数
- $item['count_people_help'] = StoreBargainUserHelp::getCountPeopleHelp($item['id']);//帮忙砍价人数
- $item['count_people_success'] = StoreBargainUser::getCountPeopleAll($item['id'], 3);//砍价成功人数
- });
- return compact('count', 'list');
- }
- /**
- * 查出导出数据
- * @param $where
- * @return array
- */
- public static function exportData($where)
- {
- $model = new self;
- $model = self::isWhere($where, $model);
- $model = $model->order('id desc');
- $model = $model->where('is_del', 0);
- $model = self::getModelTime($where, $model, "add_time");
- $list = $model->select()->toArray();
- return $list;
- }
- public static function isWhere($where = array(), $model = self::class)
- {
- if ($where['status'] != '') $model = $model->where('status', $where['status']);
- if ($where['store_name'] != '') $model = $model->where('id|title', 'LIKE', "%$where[store_name]%");
- if ($where['mer_id'] != '') $model = $model->where('mer_id', $where['mer_id']);
- // if($where['data'] != '') $model = $model->whereTime('add_time', 'between', explode('-',$where['data']));
- return $model;
- }
- /**
- * 详情
- */
- public static function getOne($id)
- {
- $info = self::get($id);
- if ($info) {
- if ($info['start_time'])
- $start_time = date('Y-m-d H:i:s', $info['start_time']);
- if ($info['stop_time'])
- $stop_time = date('Y-m-d H:i:s', $info['stop_time']);
- if (isset($start_time) && isset($stop_time))
- $info['section_time'] = [$start_time, $stop_time];
- else
- $info['section_time'] = [];
- unset($info['start_time'], $info['stop_time']);
- }
- if ($info['images'])
- $info['images'] = json_decode($info['images'], true);
- else
- $info['images'] = [];
- $info['give_integral'] = intval($info['give_integral']);
- $info['price'] = floatval($info['price']);
- $info['postage'] = floatval($info['postage']);
- $info['cost'] = floatval($info['cost']);
- $info['bargain_max_price'] = floatval($info['bargain_max_price']);
- $info['bargain_min_price'] = floatval($info['bargain_min_price']);
- $info['min_price'] = floatval($info['min_price']);
- $info['weight'] = floatval($info['weight']);
- $info['volume'] = floatval($info['volume']);
- $info['description'] = StoreDescription::getDescription($id, 2);
- $info['attrs'] = self::attr_list($id);
- return $info;
- }
- public static function attr_list($id)
- {
- $bargainInfo = self::where('id', $id)->find();
- $bargainResult = StoreProductAttrResult::where('product_id', $id)->where('type', 2)->value('result');
- $items = json_decode($bargainResult, true)['attr'];
- $productAttr = self::get_attr($items, $bargainInfo['product_id'], 0);
- $seckillAttr = self::get_attr($items, $id, 2);
- foreach ($productAttr as $pk => $pv) {
- foreach ($seckillAttr as &$sv) {
- if ($pv['detail'] == $sv['detail']) {
- $productAttr[$pk] = $sv;
- }
- }
- $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
- }
- $attrs['items'] = $items;
- $attrs['value'] = $productAttr;
- foreach ($items as $key => $item) {
- $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
- }
- $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
- $header[] = ['title' => '砍价起始金额', 'slot' => 'price', 'align' => 'center', 'minWidth' => 80];
- $header[] = ['title' => '砍价最低价', 'slot' => 'min_price', 'align' => 'center', 'minWidth' => 80];
- $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
- $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
- $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
- $header[] = ['title' => '限量', 'slot' => 'quota', 'align' => 'center', 'minWidth' => 80];
- $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
- $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
- $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
- $attrs['header'] = $header;
- return $attrs;
- }
- public static function get_attr($attr, $id, $type)
- {
- $value = attr_format($attr)[1];
- $valueNew = [];
- $count = 0;
- if ($type == 2) {
- $min_price = self::where('id', $id)->value('min_price');
- } else {
- $min_price = 0;
- }
- foreach ($value as $key => $item) {
- $detail = $item['detail'];
- sort($item['detail'], SORT_STRING);
- $suk = implode(',', $item['detail']);
- $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', $type)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
- if (count($sukValue)) {
- foreach (array_values($detail) as $k => $v) {
- $valueNew[$count]['value' . ($k + 1)] = $v;
- }
- $valueNew[$count]['detail'] = json_encode($detail);
- $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
- $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
- $valueNew[$count]['min_price'] = $min_price ? floatval($min_price) : 0;
- $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
- $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
- $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
- $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
- $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
- $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
- $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
- $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
- $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
- $valueNew[$count]['opt'] = $type != 0 ? true : false;
- $count++;
- }
- }
- return $valueNew;
- }
- }
|