Activity.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\txx\controller;
  3. use app\api\model\GoodSpec;
  4. use app\txx\model\ActGood;
  5. use app\txx\model\PlatformYouzan;
  6. use app\txx\model\YzActivityPromoCode;use think\App;
  7. use think\facade\Validate;
  8. class Activity extends Base{
  9. private $platform_id=[37,79];
  10. private $model;
  11. private $origin_img_host ='http://stock.api.caixiao365.com';
  12. private $ssl_img_host ='https://image.caixiao365.com';
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. $this->model = new ActGood();
  17. }
  18. public function goodList(){
  19. $param = $this->request->param(["good_name"=>"","skuCode"=>'',"plat_code"=>'',"page"=>1,"size"=>15],"post","trim");
  20. $where=[["is_del","=",0],["platform_id",'in',$this->platform_id],['exam_status','=',6]];
  21. if($param['good_name']!=="")$where[]=["good_name","like","%".$param['good_name']."%"];
  22. if($param['skuCode']!=="")$where[]=["skuCode","like","%".$param['skuCode']."%"];
  23. if($param['plat_code']!=="")$where[]=["plat_code","like","%".$param['plat_code']."%"];
  24. $list=PlatformYouzan::with(["good"=>["unit","category","brand"],"platform"])
  25. ->where($where)->order("id desc")
  26. ->field("id,plat_code,spuCode,skuCode,sale_price,final_price,exam_status,online_time,creater")
  27. ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
  28. $list->each(function (&$item) {
  29. $item->good_img=str_replace($this->origin_img_host,$this->ssl_img_host,$item->good_img);
  30. $item->good_info_img=str_replace($this->origin_img_host,$this->ssl_img_host,$item->good_info_img);
  31. $item->good_thumb_img=str_replace($this->origin_img_host,$this->ssl_img_host,$item->good_thumb_img);
  32. $item['spec_info'] =GoodSpec::where(['spuCode' => $item['spuCode'], 'is_del' => 0])->with(['spec',
  33. 'Spec_info'])->field('spec_id,spec_value_id')->select();
  34. });
  35. $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  36. }
  37. public function create(){
  38. $param = $this->request->param(["snArr"=>"","actCode"=>""],"post","trim");
  39. $valid = Validate::rule([
  40. "snArr|选中的商品集合"=>"require|array",
  41. "actCode|活动编码"=>"require|max:20"
  42. ]);
  43. if(!$valid->check($param)){
  44. $this->error($valid->getError());
  45. }
  46. $skuCodeArr= array_column($param['snArr'],"skuCode");
  47. $goodArr= PlatformYouzan::with(["yzGood"])->where(["skuCode"=>$skuCodeArr,"is_del"=>0])->select();
  48. if(count($goodArr->toArray())!==count($param['snArr'])){
  49. $this->error("商品信息有误");
  50. }
  51. $yzGood = array_column($goodArr->toArray(),"yzGood","skuCode");
  52. $Act = \app\txx\model\Act::where(["actCode"=>$param['actCode'],"is_del"=>0])->findOrEmpty();
  53. if($Act->isEmpty()){
  54. $this->error("活动信息有误");
  55. }
  56. if($Act->status!==0){
  57. $this->error("活动状态有误");
  58. }
  59. if(strtotime($Act->endTime)<time()){
  60. $this->error("活动已结束");
  61. }
  62. if(strtotime($Act->startTime)<time()){
  63. $this->error('活动已开始');
  64. }
  65. $valids= Validate::rule([
  66. "skuCode|商品编码"=>"require|max:20",
  67. "stock_num|库存数量"=>"require|integer",
  68. "awards_type|商品类型"=>"require|max:255",
  69. "roundId|轮次"=>"integer",
  70. ]);
  71. $actArr=[];
  72. foreach ($param['snArr'] as $k=>$v){
  73. if(!$valids->check($v)){
  74. $this->error($valids->getError());
  75. }
  76. if(!isset($yzGood[$v['skuCode']])) $this->error('商品信息有误');
  77. if($yzGood[$v['skuCode']]['status']!==6) $this->error($v['skuCode'].'有赞商品未上线');
  78. $temp=[
  79. 'actCode'=>$param['actCode'],
  80. 'yz_good_code'=>$v['skuCode'],
  81. 'goodName'=>$yzGood[$v['skuCode']]['title']??'',
  82. 'stock_num'=>$v['stock_num'],
  83. "awards_type"=>$v['awards_type'],
  84. 'used_num'=>0,
  85. "version"=>"2.0",
  86. 'balance_num'=>$v['stock_num'],
  87. 'roundId'=>$v['roundId']??'',
  88. 'good_url'=>$yzGood[$v['skuCode']]['detail_url']??'',
  89. 'origin_price'=>$yzGood[$v['skuCode']]['origin']??'',
  90. 'status'=>1,
  91. ];
  92. $actArr[]=$temp;
  93. }
  94. $this->model->startTrans();
  95. try{
  96. $add= $this->model->saveAll($actArr);
  97. if($add->isEmpty()) throw new \Exception('添加失败');
  98. $Act->save(["status"=>1]);
  99. $this->model->commit();
  100. }catch (\Exception $e){
  101. $this->model->rollback();
  102. $this->error($e->getMessage());
  103. }
  104. $this->success('添加成功');
  105. }
  106. public function list(){
  107. $param=$this->request->param(["actCode"=>"","status"=>"","skuCode"=>"","roundId"=>"","page"=>1,"size"=>15],"post","trim");
  108. $where=[["is_del","=",0],['version',"=","2.0"]];
  109. if($param['actCode']!==""){
  110. $where[]=["actCode","like","%".$param['actCode']."%"];
  111. }
  112. if($param['status']!==""){
  113. $where[]=["status","=",$param['status']];
  114. if($param['status']==3) $where[]=["used_num",">=",$param['stock_num']];
  115. }
  116. if($param['skuCode']!==""){
  117. $where[]=["yz_good_code","like","%".$param['skuCode']."%"];
  118. }
  119. if($param['roundId']!==""){
  120. $where[]=["roundId","=",$param['roundId']];
  121. }
  122. $list= $this->model->with(["act","platformYz"=>["platform","good"=>["unit","brand","category"]],"promocode"])
  123. ->where($where)->order("id desc")
  124. ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
  125. $list->visible(['id',"actCode","yz_good_code","stock_num","awards_type","status","addtime","updatetime","act_name",
  126. "company_name","contactor","mobile","startTime","endTime","act_status","sale_price","good_name","unit_name","cat_name",
  127. "brand_name","fetch_url","roundId","status_cn","ActStatus_cn","awards_order_num"]);
  128. $list->each(function (&$item){
  129. $item['status_cn']=ActGood::$statusCn[$item['status']]??"";
  130. $item['ActStatus_cn']=\app\txx\model\Act::$statusCn[$item['act_status']];
  131. if($item['act_status']!==4){
  132. $item['fetch_url']=null;
  133. }
  134. $item['awards_order_num']=YzActivityPromoCode::GetOrderNumById($item['id']);
  135. });
  136. $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  137. }
  138. public function info(){
  139. $param=$this->request->param(["id"=>""],"post","trim");
  140. $info= $this->model->with(["act","platformYz"=>["platform","good"=>['unit','brand','category']],"promocode"])->where(["is_del"=>0])->findOrEmpty($param['id']);
  141. if($info->isEmpty()){
  142. $this->error("信息有误");
  143. }
  144. $info->visible(['id','actCode','yz_good_code','awards_type','stock_num','status','addtime','updatetime','act_name',
  145. 'company_name','contactor','mobile','startTime','endTime','act_status','final_price','good_name','unit_name','cat_name',
  146. 'brand_name','fetch_url','roundId','status_cn','act_status_cn']);
  147. $info['status_cn']=ActGood::$statusCn[$info['status']]??"";
  148. $info['act_status_cn']=\app\txx\model\Act::$statusCn[$info['act_status']];
  149. $this->success("获取成功",$info);
  150. }
  151. public function resetPromocode(){
  152. $param=$this->request->param(["id"=>""],"post","trim");
  153. $info= $this->model->findOrEmpty($param['id']);
  154. if($info->isEmpty()){
  155. $this->error("信息有误");
  156. }
  157. if(!in_array($info->status,[4,5]))throw new \Exception("状态有误");
  158. try{
  159. if($info->promocode_id!==0){
  160. $promocode= \app\youzan\model\YzActivityPromoCode::where(["is_del"=>0,"id"=>$info->promocode_id])->findOrEmpty();
  161. if($promocode->isEmpty()){
  162. $info->promocode_id=0;
  163. $info->status=1;
  164. }else {
  165. $promocode->save(['status'=>0]);
  166. $info->remark="重试生成券码";
  167. }
  168. }else{
  169. $info->promocode_id=0;
  170. $info->status=1;
  171. }
  172. $save= $info->save();
  173. if(!$save)throw new \Exception('重置失败');
  174. }catch (\Exception $e){
  175. $this->error($e->getMessage());
  176. }
  177. $this->success("重置成功");
  178. }
  179. public function delete(){
  180. $param=$this->request->param(["id"=>""],"post","trim");
  181. $info= $this->model->findOrEmpty($param['id']);
  182. if($info->isEmpty()){
  183. $this->error("信息有误");
  184. }
  185. $info->is_del=1;
  186. try{
  187. $save= $info->save();
  188. if(!$save)throw new \Exception('删除失败');
  189. if($info->promocode_id!==0){
  190. $promocode= \app\youzan\model\YzActivityPromoCode::where(['is_del'=>0,'id'=>$info->promocode_id])->findOrEmpty();
  191. if($promocode->isEmpty())throw new \Exception('优惠券信息有误');
  192. $promocode->save(['is_del'=>1]);
  193. }
  194. }catch (\Exception $e){
  195. $this->error($e->getMessage());
  196. }
  197. $this->success("删除成功");
  198. }
  199. }