123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\product\brand;
- use app\dao\product\brand\StoreBrandDao;
- use app\jobs\product\ProductCategoryBrandJob;
- use app\services\BaseServices;
- use crmeb\exceptions\AdminException;
- /**
- * 商品品牌
- * Class StoreBrandServices
- * @package app\services\product\brand
- * @mixin StoreBrandDao
- */
- class StoreBrandServices extends BaseServices
- {
- public function __construct(StoreBrandDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 获取缓存
- * @param int $id
- * @return array|false|mixed|string|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2022/11/3
- */
- public function getCacheBrandInfo(int $id)
- {
- $storeBrandInfo = [];
- if ($id) {
- $storeBrandInfo = $this->dao->cacheRemember($id, function () use ($id) {
- $storeBrandInfo = $this->dao->get(['id' => $id, 'is_show' => 1, 'is_del' => 0]);
- if ($storeBrandInfo) {
- $storeBrandInfo = $storeBrandInfo->toArray();
- }
- return $storeBrandInfo ?: [];
- });
- }
- return $storeBrandInfo;
- }
- /**
- * 获取品牌列表
- * @param $where
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getTreeList($where)
- {
- $list = $this->dao->getList($where, ['product']);
- if (!empty($list) && $where['brand_name'] !== '') {
- $fid = [];
- foreach ($list as $item) {
- $fid = array_merge($fid, explode(',', $item['fid']));
- }
- $pids = array_unique(array_filter($fid));
- $parentList = $this->dao->getList(['id' => $pids], ['product']);
- $list = array_merge($list, $parentList);
- foreach ($list as $key => $item) {
- $arr = $list[$key];
- unset($list[$key]);
- if (!in_array($arr, $list)) {
- $list[] = $arr;
- }
- }
- }
- foreach ($list as &$item) {
- $item['brand_num'] = $item['product'][0]['brand_num'] ?? 0;
- $item['fid'] = $item['fid'] ? array_map('intval', explode(',', $item['fid'])) : [];
- $item['type'] = count($item['fid']) < 2 ? 1 : 0;
- //添加子品牌fid
- if ($item['type'] == 1) {
- $item['fid_son'] = $item['fid'];
- array_push($item['fid_son'], $item['id']);
- }
- unset($item['product']);
- }
- $list = get_tree_children($list);
- $count = $this->dao->count($where);
- return compact('list', 'count');
- }
- /**
- * 获取品牌列表
- * @param $where
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getList($where)
- {
- $list = $this->dao->getList($where, ['product', 'children']);
- $count = $this->dao->count($where);
- if ($list) {
- foreach ($list as &$item) {
- $item['brand_num'] = $item['product'][0]['brand_num'] ?? 0;
- $item['fid'] = $item['fid'] ? array_map('intval', explode(',', $item['fid'])) : [];
- $item['type'] = count($item['fid']) < 2 ? 1 : 0;
- //添加子品牌fid
- if ($item['type'] == 1) {
- $item['fid_son'] = $item['fid'];
- array_push($item['fid_son'], $item['id']);
- }
- if (isset($item['children']) && $item['children']) {
- $item['children'] = [];
- $item['loading'] = false;
- $item['_loading'] = false;
- } else {
- unset($item['children']);
- }
- unset($item['product']);
- }
- }
- return compact('list', 'count');
- }
- /**
- * 获取品牌cascader
- * @param string $show
- * @param int $type
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function cascaderList($type = 1)
- {
- $where = [];
- if ($type == 1) {
- $top = true;
- } else {
- $top = false;
- }
- $menus = [];
- $where['is_del'] = 0;
- $where['is_show'] = 1;
- $list = get_tree_children($this->dao->getList($where, [], ['id as value', 'brand_name as label', 'pid']), 'children', 'value');
- if ($top) {
- $menus = [['value' => 0, 'label' => '顶级品牌']];
- foreach ($list as &$item) {
- if (isset($item['children']) && $item['children']) {
- foreach ($item['children'] as &$val) {
- if (isset($val['children']) && $val['children']) {
- unset($val['children']);
- }
- }
- }
- }
- }
- $menus = array_merge($menus, $list);
- return $menus;
- }
- /**
- * 设置品牌状态
- * @param $id
- * @param $is_show
- */
- public function setShow(int $id, int $is_show)
- {
- $res = $this->dao->update($id, ['is_show' => $is_show]);
- // $res = $res && $this->dao->update($id, ['is_show' => $is_show], 'pid');
- if (!$res) {
- throw new AdminException('设置失败');
- }
- //设置缓存
- if (!$is_show) {
- $this->cacheDelById($id);
- return;
- }
- $branInfo = $this->dao->cacheInfoById($id);
- if ($branInfo) {
- $branInfo['is_show'] = 1;
- } else {
- $branInfo = $this->dao->get($id);
- if (!$branInfo) {
- return;
- }
- $branInfo = $branInfo->toArray();
- }
- $this->dao->cacheUpdate($branInfo);
- //修改关联
- ProductCategoryBrandJob::dispatchDo('setShow', [$id, 'brand_id', 'status', $is_show]);
- return true;
- }
- /**
- * 保存新增数据
- * @param $data
- */
- public function createData($data)
- {
- $data['pid'] = end($data['fid']);
- if ($this->dao->getOne(['brand_name' => $data['brand_name'], 'pid' => $data['pid']])) {
- throw new AdminException('该品牌已经存在');
- }
- $data['fid'] = implode(',', $data['fid']);
- $data['add_time'] = time();
- $res = $this->dao->save($data);
- if (!$res) throw new AdminException('添加失败');
- //更新缓存
- if ($data['is_show']) {
- $data['id'] = $res->id;
- $this->cacheUpdate($data);
- }
- }
- /**
- * 保存修改数据
- * @param $id
- * @param $data
- */
- public function editData($id, $data)
- {
- $cate = $this->dao->getOne(['id' => $id]);
- if (!$cate) {
- throw new AdminException('该品牌不存在');
- }
- $data['pid'] = end($data['fid']) ?? 0;
- if ($data['pid']) {
- $pcate = $this->dao->getOne(['id' => $data['pid']]);
- if (!$pcate) {
- throw new AdminException('上级品牌不存在');
- }
- if ($pcate['pid'] == $id) {
- throw new AdminException('上级品牌不能是当前品牌的下级');
- }
- }
- $data['fid'] = implode(',', $data['fid']);
- $cate = $this->dao->getOne(['pid' => $data['pid'], 'brand_name' => $data['brand_name']]);
- if ($cate && $cate['id'] != $id) {
- throw new AdminException('该品牌已经存在');
- }
- $res = $this->dao->update($id, $data);
- if (!$res) throw new AdminException('修改失败');
- //更新缓存
- if ($data['is_show']) {
- $data['id'] = $res->id;
- $this->cacheUpdate($data);
- }
- }
- /**
- * 删除数据
- * @param int $id
- */
- public function del(int $id)
- {
- if ($this->dao->count(['pid' => $id])) {
- throw new AdminException('请先删除子品牌!');
- }
- $res = $this->dao->delete($id);
- if (!$res) throw new AdminException('删除失败');
- //更新缓存
- $this->cacheDelById($id);
- //修改关联
- ProductCategoryBrandJob::dispatchDo('setShow', [$id, 'brand_id', 'is_del', 1]);
- return true;
- }
- }
|