Activity.php 8.8 KB

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