123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- /**
- * 素材Model
- * Created by PhpStorm.
- * User: 小威
- * Date: 2020/03/31
- * Time: 15:44
- */
- namespace JinDouYun\Model\Material;
- use Exception;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\Material\DMaterialCategory;
- use JinDouYun\Dao\Material\DMaterialContent;
- class MMaterial
- {
- private $objDMaterialCategory;
- private $objDMaterialContent;
- private $userCenterId;
- private $enterpriseId;
- public function __construct($enterpriseId, $userCenterId)
- {
- $this->enterpriseId = $enterpriseId;
- $this->userCenterId = $userCenterId;
- $this->objDMaterialCategory = new DMaterialCategory('default');
- $this->objDMaterialContent = new DMaterialContent('default');
- $this->objDMaterialContent->setTable($this->objDMaterialContent->get_Table().'_'.$enterpriseId);
- }
- /**
- * 素材内容添加
- * @param $params
- * @return ResultWrapper
- * @throws Exception
- */
- public function addMaterialContent($params)
- {
- $insertData['shopId'] = isset($params['shopId']) ? $params['shopId'] : 0;
- $insertData['categoryId'] = $params['categoryId'];
- $insertData['name'] = $params['name'];
- $insertData['content'] = $params['content'];
- $insertData['deleteStatus'] = StatusCode::$standard;
- $insertData['createTime'] = time();
- $insertData['updateTime'] = time();
- $dbResult = $this->objDMaterialContent->insert($insertData);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDMaterialContent->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 素材分类添加
- * @param $params
- * @return ResultWrapper
- * @throws Exception
- */
- public function addMaterialCategory($params)
- {
- $insertData['shopId'] = isset($params['shopId']) ? $params['shopId'] : 0;
- $insertData['enterpriseId'] = $this->enterpriseId;
- $insertData['pid'] = $params['pid'];
- $insertData['title'] = $params['title'];
- $insertData['deleteStatus'] = StatusCode::$standard;
- $insertData['createTime'] = time();
- $insertData['updateTime'] = time();
- $dbResult = $this->objDMaterialCategory->insert($insertData);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDMaterialCategory->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 素材内容修改
- * @param $params
- * @return ResultWrapper
- * @throws Exception
- */
- public function updateMaterialContent($params)
- {
- $dbResult = $this->objDMaterialContent->update(['categoryId' => $params['categoryId'], 'updateTime' => time()], ['id' => $params['id']]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDMaterialContent->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 素材分类修改
- * @param $params
- * @return ResultWrapper
- * @throws Exception
- */
- public function updateMaterialCategory($params)
- {
- $where = ['id' => $params['id']];
- unset($params['id']);
- $dbResult = $this->objDMaterialCategory->update($params, $where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDMaterialCategory->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 素材内容删除
- * @param $params
- * @return ResultWrapper
- */
- public function delMaterialContent($params)
- {
- $where = ['id' => $params['id']];
- $dbResult = $this->objDMaterialContent->update(['deleteStatus' => StatusCode::$delete,'updateTime' => time(), 'extend' => json_encode(['userCenterId' => $this->userCenterId])], $where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDMaterialContent->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 素材分类删除
- * @param $params
- * @return ResultWrapper
- */
- public function delMaterialCategory($params)
- {
- $where = ['id' => $params['id']];
- //判断是否有内容
- $dbResult = $this->objDMaterialContent->select(['categoryId' => $where['id'], 'deleteStatus' => StatusCode::$standard]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDMaterialContent->error(), ErrorCode::$dberror);
- }
- if($dbResult){
- return ResultWrapper::fail('请先删除分组里内容', ErrorCode::$paramError);
- }
- $dbResult = $this->objDMaterialCategory->update(['deleteStatus' => StatusCode::$delete], $where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDMaterialCategory->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 素材分类列表
- * @return ResultWrapper
- */
- public function getAllMaterialCategory($params = [])
- {
- $where = [
- 'pid' => 0,
- 'enterpriseId' => $this->enterpriseId,
- 'deleteStatus' => StatusCode::$standard,
- 'shopId' => isset($params['shopId']) ? $params['shopId'] : 0,
- ];
- $dbResult = $this->objDMaterialCategory->select($where, '*', 'id desc');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMaterialCategory->error(), ErrorCode::$dberror);
- }
- $defaultCategory = [
- 'id' => 0,
- 'enterpriseId' => $this->enterpriseId,
- 'pid' => 0,
- 'title' => '默认分组',
- 'deleteStatus' => StatusCode::$standard,
- 'createTime' => 0,
- 'updateTime' => 0,
- ];
- array_unshift($dbResult, $defaultCategory);
- return ResultWrapper::success($dbResult);
- }
- /**
- * 素材内容列表
- * @param $params
- * @return ResultWrapper
- */
- public function getAllMaterialContent($params)
- {
- $limit = $params['limit'];
- unset($params['limit']);
- $offset = $params['offset'];
- unset($params['offset']);
- $where = ' categoryId = '.$params['categoryId']. ' and deleteStatus = '.StatusCode::$standard;
- !empty($params['name']) && $where .= " and name like '%".$params['name']."%'";
- $where .= ' and shopId = '.(isset($params['shopId']) ? $params['shopId'] : 0);
- $dbResult = $this->objDMaterialContent->select($where, '*', 'id desc', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMaterialContent->error(), ErrorCode::$dberror);
- }
- $total = $this->objDMaterialContent->count($where);
- $return = [
- 'data' => $dbResult,
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
- }
|