qlg.tsgz.moe/hyhproject/admin/model/GoodsClassify.php
2019-09-06 23:53:10 +08:00

877 lines
14 KiB
PHP
Executable File

<?php
namespace wstmart\admin\model;
use think\Validate;
/**
* ============================================================================
* 商品分类业务处理
*/
use think\Db;
class GoodsClassify extends Base{
/**
* 获取树形分类
*/
public function pageQuery(){
return Db::name('goods_classify')->order('orderby desc')->paginate(15)->toArray();
}
/**
* 获取指定对象
*/
public function getById($goodsclassifyId){
$obj = null;
if($goodsclassifyId>0){
$obj = Db::name('goods_classify')->where(['goodsclassifyId'=>$goodsclassifyId])->find();
}else{
$obj = self::getEModel("goods_classify");
}
return $obj;
}
/**
* 新增
*/
public function add(){
$data = input('post.');
$data['create_time'] = date('Y-m-d H:i:s');
$validate = Validate::make([
'goodsclassifyName' => 'require',
]);
$msg = [
'goodsclassifyName.require' => '商品总名称不能为空',
];
$info = [
'goodsclassifyName' => $data['goodsclassifyName'],
];
if (!$validate->check($info)) {
return WSTReturn ($validate->getError());
}
$find=$this->where('goodsclassifyName',$data['goodsclassifyName'])->find();
if($find) return WSTReturn('此总分类名称已存在');
$result = $this->allowField(true)->save($data);
if(false !== $result){
WSTClearAllCache();
return WSTReturn("新增成功", 1);
}else{
return WSTReturn($this->getError(),-1);
}
}
/**
* 编辑
*/
public function edit(){
$goodsclassifyId = input('post.goodsclassifyId/d');
$data = input('post.');
$result=$this->allowField(true)->save($data,['goodsclassifyId'=>$goodsclassifyId]);
if(false !== $result){
WSTClearAllCache();
return WSTReturn("新增成功", 1);
}else{
return WSTReturn($this->getError(),-1);
}
}
/**
* 删除
*/
public function del(){
$id = input('post.goodsclassifyId/d');
$see=db('cat_classify')->where('classifyId',$id)->find();
$recom=db('recom_classify')->where('classifyId',$id)->find();
if($see!==NUll) return WSTReturn ('商品有子分类,不能删除');
if($recom) return WSTReturn ('此商品分类有活动,不能删除');
$result=$this->where('goodsclassifyId',$id)->delete();
if($result!=false){
return WSTReturn("删除成功", 1);
}else{
return WSTReturn('删除失败',-1);
}
}
/**
* 获取指定对象
*/
public function getByIds($catId){
$obj = null;
if($catId>0){
$obj = Db::name('cat_classify')->where(['catId'=>$catId])->find();
}else{
$obj = self::getEModel("cat_classify");
}
return $obj;
}
// 添加商品分类
public function add_cat(){
$data=input('post.');
$result = db('cat_classify')->insert($data);
if(false !== $result){
WSTClearAllCache();
return WSTReturn("新增成功", 1);
}else{
return WSTReturn("新增失败",-1);
}
}
//获取商品分类
public function catdetail(){
$id=input('goodsclassifyId');
$result=db('cat_classify')->where('classifyId',$id)->field('catId')->select();
return $result;
}
//获取商品分类列表
public function catdetailPage(){
$id=input('classifyId');
$pagesize = input('limit/d');
$result=db('cat_classify')->alias('a')
->join('goods_cats g','g.catId=a.catId','left')
->where('classifyId',$id)->field('g.catName,createTime,a.catId')
->paginate($pagesize)->toArray();
return $result;
}
/**
* 删除商品分类
*/
public function catdel(){
$catId = input('catId');
$classifyId = input('classifyId');
$result=db('cat_classify')->where(['catId'=>$catId,'classifyId'=>$classifyId])->delete();
if($result!=false){
return WSTReturn("删除成功", 1);
}else{
return WSTReturn('删除失败',-1);
}
}
/*
详情
*/
public function detail(){
$id=input('catId');
$result=db('cat_classify')->where('catId',$id)->field('catId')->select();
return $result;
}
//详情分页
public function detailByPage(){
$catId=input('catId');
$pagesize = input('limit/d');
$get_child=$this->getChild('',$catId);
$goods_result=db('goods')->alias('a')->join('shops s','s.shopId=a.shopId','left')
->whereIn('goodsCatId',$get_child)->order('saleNum desc')
->where(['isSale'=>1,'a.dataFlag'=>1,'a.goodsStatus'=>1,'isHot'=>1])
->field('goodsId,goodsName,goodsImg,shopPrice,saleNum,goodsSn,s.shopName,a.shopId,goodsCatIdPath')
->paginate($pagesize)->toArray();
$keyCats = model('GoodsCats')->listKeyAll();
foreach ($goods_result['Rows'] as $key => $v){
$goods_result['Rows'][$key]['verfiycode'] = WSTShopEncrypt($v['shopId']);
$goods_result['Rows'][$key]['goodsCatName'] = self::getGoodsCatNames($v['goodsCatIdPath'],$keyCats);
}
return $goods_result;
}
public function getGoodsCatNames($goodsCatPath, $keyCats){
$catIds = explode("_",$goodsCatPath);
$catNames = array();
for($i=0,$k=count($catIds);$i<$k;$i++){
if($catIds[$i]=='')continue;
if(isset($keyCats[$catIds[$i]]))$catNames[] = $keyCats[$catIds[$i]];
}
return implode("",$catNames);
}
/**
* 迭代获取下级
* 获取一个分类下的所有子级分类id
*/
public function getChild($data,$pid){
$childId = db('goods_cats')->where("dataFlag=1")->where('parentId',$pid)->field('catId,parentId')->select();
//获取该分类id下的所有子级分类id
foreach($childId as $key=>$value){
$child[]=$value['catId'];
}
static $ids = array();
foreach($childId as $k=>$v){
//dump($childId);
$ids[] = $v['catId'];//将找到的下级分类id放入静态数组
//再找下当前id是否还存在下级id
$this->getChild($childId, $v['catId']);
}
return $ids;
}
//活动详情分页
public function setdetailPage(){
$recomId=input('recomId');
$goodsId=input('goodsId');
$where=[];
//dump($recomId);
if($recomId)$where['rg.recomId'] = $recomId;
if($goodsId)$where['rg.goodsId'] = ['like','%'.$goodsId.'%'];
$classifyId=input('classifyId');
$pagesize = input('limit/d');
$goods_result=db('recom_classify')->alias('rc')
->join('recom_goods rg','rg.recomId =rc.recomId','left')
->join('goods a','a.goodsId=rg.goodsId','left')
->where(['isSale'=>1,'a.dataFlag'=>1,'a.goodsStatus'=>1,'rc.classifyId'=>$classifyId])->where($where)
->field('recomGoodsId,a.goodsId,a.goodsName,goodsImg,rc.recomId,rc.recomName,rg.goodsOrder,from_unixtime(rg.createTime)createTime,a.shopPrice')
->order('goodsOrder desc')
->paginate($pagesize)->toArray();
return $goods_result;
}
//更改活动商品的排序号
public function changeSet(){
$id=(int)input('id');
$goodsOrder=(int)input('goodsOrder');
$result=db('recom_goods')->where('recomGoodsId',$id)->update(['goodsOrder'=>$goodsOrder]);
if($result!==false){
WSTClearAllCache();
return WSTReturn("新增成功", 1);
}else{
return WSTReturn("新增失败",-1);
}
}
//活动详情分页
public function recomPage(){
$classifyId=input('classifyId');
$goodsId=(int)input('goodsId');
$where=[];
if($goodsId)$where['ra.goodsId'] = ['like','%'.$goodsId.'%'];
$pagesize = input('limit/d');
$goods_result=db('recom_active')->alias('ra')
->join('goods a','a.goodsId=ra.goodsId','left')
->where(['isSale'=>1,'a.dataFlag'=>1,'a.goodsStatus'=>1,'ra.classifyId'=>$classifyId])->where($where)
->field('recomActiveId,a.goodsId,a.goodsName,goodsImg,ra.goodsOrder,from_unixtime(ra.createTime)createTime,a.shopPrice')
->order('goodsOrder desc')
->paginate($pagesize)->toArray();
return $goods_result;
}
//更改活动商品的排序号
public function changeRecom(){
$id=(int)input('id');
$goodsOrder=(int)input('goodsOrder');
$result=db('recom_active')->where('recomActiveId',$id)->update(['goodsOrder'=>$goodsOrder]);
if($result!==false){
WSTClearAllCache();
return WSTReturn("新增成功", 1);
}else{
return WSTReturn("新增失败",-1);
}
}
//删除推荐商品
public function recomActiveDel(){
$recomGoodsId=input('recomActiveId');
$result=db('recom_active')->where('recomActiveId',$recomGoodsId)->delete();
if($result!=false){
return WSTReturn("删除成功", 1);
}else{
return WSTReturn('删除失败',-1);
}
}
/**
* 获取指定对象
*/
public function getrecomActiveId($recomActiveId){
$obj = null;
if($recomActiveId>0){
$obj = Db::name('recom_active')->where(['recomActiveId'=>$recomActiveId])->find();
}else{
$obj = self::getEModel("recom_active");
}
return $obj;
}
/**
* 新增
*/
public function addrecomActive(){
Db::startTrans();
try{
$data=input('post.');
$data['createTime']=time();
$goodsId = trim(input('goodsId'));
$product_id = explode(',',$goodsId);
$arr=[];
foreach ($product_id as $k=>$value) {
$goods=db('goods')->where('goodsId',$value)->where('dataFlag=1 and isSale=1 and goodsStatus=1')->find();
if(!$goods) return WSTReturn('无效商品 '.$value);
$arr[$k]['goodsId'] = $value;
$arr[$k]['classifyId'] = $data['classifyId'];
$arr[$k]['createTime'] = $data['createTime'];
if($arr[$k]['goodsId']=="," || $arr[$k]['goodsId']==""){
unset($arr[$k]);
}
$find=db('recom_active')->where(['goodsId'=>$arr[$k]['goodsId']])->find();
if($find) return WSTReturn('此商品已存在 '.$arr[$k]['goodsId']);
}
$result=Db::name('recom_active')->insertAll($arr);
Db::commit();
WSTClearAllCache();
return WSTReturn("新增成功", 1);
}
catch(\Exception $e) {
Db::rollback();errLog($e);
}
return WSTReturn("添加失败",-1);
}
//查看活动页活动分类
public function catsPage(){
$classifyId=input('classifyId');
$goods_result=db('recom_classify')->alias('rc')
->join('goods_classify gc','gc.goodsclassifyId=rc.classifyId','left')
->where('classifyId',$classifyId)
->field('recomId,recomName,classifyId,goodsclassifyName,recomOrder,from_unixtime(startTime) startTime,from_unixtime(endTime)endTime')
->order('recomOrder desc')
->paginate()->toArray();
return $goods_result;
}
/**
* 获取指定对象
*/
public function getCatsId($recomId){
$obj = null;
if($recomId>0){
$obj = Db::name('recom_classify')->where(['recomId'=>$recomId])->find();
$obj['startTime']=date('Y-m-d H:i:s',$obj['startTime']);
$obj['endTime']=date('Y-m-d H:i:s',$obj['endTime']);
}else{
$obj = self::getEModel("recom_classify");
}
return $obj;
}
// 添加商品活动分类
public function addCats(){
$data=input('post.');
$data['startTime']=strtotime(input('startTime'));
$data['endTime']=strtotime(input('endTime'));
$find=db('recom_classify')->where('recomName',$data['recomName'])->find();
if($find) return WSTReturn('此分类名称已存在');
$result = db('recom_classify')->insert($data);
if(false !== $result){
WSTClearAllCache();
return WSTReturn("新增成功", 1);
}else{
return WSTReturn("新增失败",-1);
}
}
// 修改商品活动分类
public function editCats(){
$data=input('post.');
$data['startTime']=strtotime(input('startTime'));
$data['endTime']=strtotime(input('endTime'));
$result = db('recom_classify')->where('recomId',$data['recomId'])->update($data);
if(false !== $result){
WSTClearAllCache();
return WSTReturn("操作成功", 1);
}else{
return WSTReturn("操作失败",-1);
}
}
//删除分类活动
public function catsDel(){
$recomId=input('recomId');
$m=input('m');
Db::startTrans();
try{
if($m==2){
db('recom_goods')->where('recomId',$recomId)->delete();
db('recom_classify')->where('recomId',$recomId)->delete();
}else{
$find_goods=db('recom_goods')->where('recomId',$recomId)->find();
if($find_goods) return WSTReturn('此分类有活动商品',2);
db('recom_classify')->where('recomId',$recomId)->delete();
}
Db::commit();
return WSTReturn("操作成功", 1);
}catch (\Exception $e) {
Db::rollback();errLog($e);
return WSTReturn('删除失败',-1);
}
}
/**
* 获取指定对象
*/
public function getGoodsId($recomId){
$obj = null;
if($recomId>0){
$obj = Db::name('recom_goods')->where(['id'=>$recomId])->find();
}else{
$obj = self::getEModel("recom_goods");
}
return $obj;
}
// 添加活动商品
public function addGoods(){
Db::startTrans();
try{
$data=input('post.');
$data['createTime']=time();
$goodsId = trim(input('goodsId'));
$product_id = explode(',',$goodsId);
$arr=[];
foreach ($product_id as $k=>$value) {
$goods=db('goods')->where('goodsId',$value)->where('dataFlag=1 and isSale=1 and goodsStatus=1')->find();
if(!$goods) return WSTReturn('无效商品 '.$value);
$arr[$k]['goodsId'] = $value;
$arr[$k]['recomId'] = $data['recomId'];
$arr[$k]['createTime'] = $data['createTime'];
if($arr[$k]['goodsId']=="," || $arr[$k]['goodsId']==""){
unset($arr[$k]);
}
$find=db('recom_goods')->where(['goodsId'=>$arr[$k]['goodsId'],'recomId'=>$arr[$k]['recomId']])->find();
if($find) return WSTReturn('此商品已存在 '.$arr[$k]['goodsId']);
}
$result=Db::name('recom_goods')->insertAll($arr);
Db::commit();
WSTClearAllCache();
return WSTReturn("添加成功", 1);
}
catch(\Exception $e) {
Db::rollback();errLog($e);
}
return WSTReturn("添加失败",-1);
}
//删除分类活动商品
public function goodsDel(){
$recomGoodsId=input('recomGoodsId');
$result=db('recom_goods')->where('recomGoodsId',$recomGoodsId)->delete();
if($result!=false){
return WSTReturn("删除成功", 1);
}else{
return WSTReturn('删除失败',-1);
}
}
}