123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace app\txx\controller;
- use app\admin\model\GoodSpec;
- use app\txx\model\ActGood;
- use app\txx\model\PlatformYouzan;
- use app\txx\model\YzActivityPromoCode;use think\App;
- use think\facade\Validate;
- class Activity extends Base{
- private $platform_id=[37,79];
- private $model;
- private $origin_img_host ='http://stock.api.caixiao365.com';
- private $ssl_img_host ='https://image.caixiao365.com';
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->model = new ActGood();
- }
- public function goodList(){
- $param = $this->request->param(["good_name"=>"","skuCode"=>'',"plat_code"=>'',"exam_status"=>"","page"=>1,"size"=>15],"post","trim");
- $where=[["is_del","=",0],["platform_id",'in',$this->platform_id]];
- if($param['good_name']!=="")$where[]=["good_name","like","%".$param['good_name']."%"];
- if($param['skuCode']!=="")$where[]=["skuCode","like","%".$param['skuCode']."%"];
- if($param['plat_code']!=="")$where[]=["plat_code","like","%".$param['plat_code']."%"];
- if($param['exam_status']!==''){
- $where[]=["exam_status","=",$param['exam_status']];
- }
- $list=PlatformYouzan::with(["good"=>["unit","category","brand"],"platform","yzGood"])
- ->where($where)->order("id desc")
- ->field("plat_code,spuCode,skuCode,sale_price,final_price,exam_status,online_time,creater")
- ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
- $list->each(function (&$item) {
- $item->good_img=str_replace($this->origin_img_host,$this->ssl_img_host,$item->good_img);
- $item->good_info_img=str_replace($this->origin_img_host,$this->ssl_img_host,$item->good_info_img);
- $item->good_thumb_img=str_replace($this->origin_img_host,$this->ssl_img_host,$item->good_thumb_img);
- $item['spec_info'] =GoodSpec::where(['spuCode' => $item['spuCode'], 'is_del' => 0])->with(['spec',
- 'Spec_info'])->field('spec_id,spec_value_id')->select();
- });
- $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
- }
- public function create(){
- $param = $this->request->param(["snArr"=>"","actCode"=>""],"post","trim");
- $valid = Validate::rule([
- "snArr|选中的商品集合"=>"require|array",
- "actCode|活动编码"=>"require|max:20"
- ]);
- if(!$valid->check($param)){
- $this->error($valid->getError());
- }
- $skuCodeArr= array_column($param['snArr'],"skuCode");
- $goodArr= PlatformYouzan::with(["yzGood"])->where(["skuCode"=>$skuCodeArr,"is_del"=>0])->select();
- if(count($goodArr->toArray())!==count($param['snArr'])){
- $this->error("商品信息有误");
- }
- $yzGood = array_column($goodArr->toArray(),"yzGood","skuCode");
- $Act = \app\txx\model\Act::where(["actCode"=>$param['actCode'],"is_del"=>0])->findOrEmpty();
- if($Act->isEmpty()){
- $this->error("活动信息有误");
- }
- if($Act->status!=="0"){
- $this->error("活动状态有误");
- }
- if(strtotime($Act->endTime)<time()){
- $this->error("活动已结束");
- }
- if(strtotime($Act->startTime)<time()){
- $this->error('活动已开始');
- }
- $valids= Validate::rule([
- "skuCode|商品编码"=>"require|max:20",
- "stock_num|库存数量"=>"require|integer",
- "awards_type|商品类型"=>"require|max:255",
- "roundId|轮次"=>"integer",
- ]);
- $actArr=[];
- foreach ($param['snArr'] as $k=>$v){
- if(!$valids->check($v)){
- $this->error($valids->getError());
- }
- if(!isset($yzGood[$v['skuCode']])) $this->error('商品信息有误');
- if($yzGood[$v['skuCode']]['status']!=="6") $this->error($v['skuCode'].'有赞商品未上线');
- $temp=[
- 'actCode'=>$param['actCode'],
- 'yz_good_code'=>$v['skuCode'],
- 'goodName'=>$yzGood[$v['skuCode']]['title']??'',
- 'stock_num'=>$v['stock_num'],
- "awards_type"=>$v['awards_type'],
- 'used_num'=>0,
- "version"=>"2.0",
- 'balance_num'=>$v['stock_num'],
- 'roundId'=>$v['roundId']??'',
- 'good_url'=>$yzGood[$v['skuCode']]['detail_url']??'',
- 'origin_price'=>$yzGood[$v['skuCode']]['origin']??'',
- 'status'=>1,
- ];
- $actArr[]=$temp;
- }
- $this->model->startTrans();
- try{
- $add= $this->model->saveAll($actArr);
- if($add->isEmpty()) throw new \Exception('添加失败');
- $Act->save(["status"=>1]);
- $this->model->commit();
- }catch (\Exception $e){
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- $this->success('添加成功');
- }
- public function list(){
- $param=$this->request->param([
- "actCode"=>"",
- "status"=>"",
- "skuCode"=>"",
- "version"=>"",
- "roundId"=>"",
- 'company_name'=>'',
- 'contactor'=>'',
- 'goodName'=>'',
- "page"=>1,"size"=>15],"post","trim");
- $where=[["act_good.is_del","=",0]];
- if($param['actCode']!==""){
- $where[]=["act_good.actCode","like","%".$param['actCode']."%"];
- }
- if($param['status']!==""){
- $where[]=["act_good.status","=",$param['status']];
- }
- if($param['skuCode']!==""){
- $where[]=["yz_good_code","like","%".$param['skuCode']."%"];
- }
- if($param['roundId']!==""){
- $where[]=["roundId","=",$param['roundId']];
- }
- if($param['version']!==''){
- $where[]=['act_good.version','=',$param['version']];
- }
- if($param['company_name']!==""){
- $where[]=["act.company_name","like","%".$param['company_name']."%"];
- }
- if($param['contactor']!==""){
- $where[]=["act.contactor","like","%".$param['contactor']."%"];
- }
- if($param['goodName']!==""){
- $where[]=["goodName","like","%".$param['goodName']."%"];
- }
- $list= $this->model->with(["act","platformYz"=>["platform","good"=>['unit','brand','category']],"promocode"])
- ->withJoin(["act"],"left")
- ->where($where)->order("act_good.id desc")->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
- $list->each(function(&$item){
- $item['status_cn']=ActGood::$statusCn[$item['status']]??"";
- $item['ActStatus_cn']=\app\txx\model\Act::$statusCn[$item['act_status']];
- });
- $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
- }
- public function info(){
- $param=$this->request->param(["id"=>""],"post","trim");
- $info= $this->model->with(["act","platformYz"=>["platform","good"=>['unit','brand','category']],"promocode"])->findOrEmpty($param['id']);
- if($info->isEmpty()){
- $this->error("信息有误");
- }
- $info['status_cn']=ActGood::$statusCn[$info['status']]??"";
- $info['act_status_cn']=\app\txx\model\Act::$statusCn[$info['act_status']];
- $this->success("获取成功",$info);
- }
- public function resetPromocode(){
- $param=$this->request->param(["id"=>""],"post","trim");
- $info= $this->model->findOrEmpty($param['id']);
- if($info->isEmpty()){
- $this->error("信息有误");
- }
- if(!in_array($info->status,[4,5]))throw new \Exception("状态有误");
- try{
- if($info->promocode_id!==0){
- $promocode= \app\youzan\model\YzActivityPromoCode::where(["is_del"=>0,"id"=>$info->promocode_id])->findOrEmpty();
- if($promocode->isEmpty()){
- $info->promocode_id=0;
- $info->status=1;
- }else {
- $promocode->save(['status'=>0]);
- $info->remark="重试生成券码";
- }
- }else{
- $info->promocode_id=0;
- $info->status=1;
- }
- $save= $info->save();
- if(!$save)throw new \Exception('重置失败');
- }catch (\Exception $e){
- $this->error($e->getMessage());
- }
- $this->success("重置成功");
- }
- public function delete(){
- $param=$this->request->param(["id"=>""],"post","trim");
- $info= $this->model->findOrEmpty($param['id']);
- if($info->isEmpty()){
- $this->error("信息有误");
- }
- $info->is_del=1;
- try{
- $save= $info->save();
- if(!$save)throw new \Exception('删除失败');
- if($info->promocode_id!==0){
- $promocode= \app\youzan\model\YzActivityPromoCode::where(['is_del'=>0,'id'=>$info->promocode_id])->findOrEmpty();
- if($promocode->isEmpty())throw new \Exception('优惠券信息有误');
- $promocode->save(['is_del'=>1]);
- }
- }catch (\Exception $e){
- $this->error($e->getMessage());
- }
- $this->success("删除成功");
- }
- public function promocodeList(){
- $param=$this->request->param(["title"=>"","status"=>"","start"=>"","end"=>"","page"=>1,"size"=>15],"post","trim");
- $where=[["is_del","=",0]];
- if($param['title']!==""){
- $where[]=["title","like","%".$param['title']."%"];
- }
- if($param['status']!==""){
- $where[]=["status","=",$param['status']];
- }
- if($param['start']!==""){
- $where[]=["createTime",">=",startTime($param['start'])];
- }
- if($param['end']!==""){
- $where[]=["createTime","<=",endTime($param['end'])];
- }
- $list= YzActivityPromoCode::with(["Good"])
- ->where($where)
- ->order("id desc")
- ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
- $list->each(function(&$item){
- $item['status_cn']=YzActivityPromoCode::$statusCn[$item['status']]??"";
- $item['codeType_cn']=YzActivityPromoCode::$codeTypeCn[$item['code_type']]??"";
- $item["rangeType_cn"]=YzActivityPromoCode::$rangeTypeCn[$item['range_type']]??"";
- });
- $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
- }
- /**
- * 优惠券删除
- */
- public function promocodeDelete(){
- $param=$this->request->param(["id"=>""],"post","trim");
- $info= YzActivityPromoCode::where(['is_del'=>0,'id'=>$param['id']])->findOrEmpty();
- if($info->isEmpty()){
- $this->error("信息有误");
- }
- $info->is_del=1;
- try{
- $save= $info->save();
- if(!$save)throw new \Exception('删除失败');
- if($info->act_good_id!==0){
- $act= $this->model->with(["act"])->where(['is_del'=>0,'id'=>$info->act_good_id])->findOrEmpty();
- if(!$act->isEmpty()){
- if(time()<strtotime($act->startTime)){ //活动未开始
- $act->promocode_id=0;
- $act->status=1;
- $act->save();
- }
- }
- }
- }catch (\Exception $e){
- $this->error($e->getMessage());
- }
- $this->success("删除成功");
- }
- }
|