Activity.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace app\txx\controller;
  3. use app\admin\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"=>'',"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","yzGood"])
  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();
  51. if(count($goodArr->toArray())!==count($param['snArr'])){
  52. $this->error("商品信息有误");
  53. }
  54. $yzGood = array_column($goodArr->toArray(),"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!=="0"){
  60. $this->error("活动状态有误");
  61. }
  62. if(strtotime($Act->endTime)<time()){
  63. $this->error("活动已结束");
  64. }
  65. if(strtotime($Act->startTime)<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']])) $this->error('商品信息有误');
  80. if($yzGood[$v['skuCode']]['status']!=="6") $this->error($v['skuCode'].'有赞商品未上线');
  81. $temp=[
  82. 'actCode'=>$param['actCode'],
  83. 'yz_good_code'=>$v['skuCode'],
  84. 'goodName'=>$yzGood[$v['skuCode']]['title']??'',
  85. 'stock_num'=>$v['stock_num'],
  86. "awards_type"=>$v['awards_type'],
  87. 'used_num'=>0,
  88. "version"=>"2.0",
  89. 'balance_num'=>$v['stock_num'],
  90. 'roundId'=>$v['roundId']??'',
  91. 'good_url'=>$yzGood[$v['skuCode']]['detail_url']??'',
  92. 'origin_price'=>$yzGood[$v['skuCode']]['origin']??'',
  93. 'status'=>1,
  94. ];
  95. $actArr[]=$temp;
  96. }
  97. $this->model->startTrans();
  98. try{
  99. $add= $this->model->saveAll($actArr);
  100. if($add->isEmpty()) throw new \Exception('添加失败');
  101. $Act->save(["status"=>1]);
  102. $this->model->commit();
  103. }catch (\Exception $e){
  104. $this->model->rollback();
  105. $this->error($e->getMessage());
  106. }
  107. $this->success('添加成功');
  108. }
  109. public function list(){
  110. $param=$this->request->param([
  111. "actCode"=>"",
  112. "status"=>"",
  113. "skuCode"=>"",
  114. "version"=>"",
  115. "roundId"=>"",
  116. 'company_name'=>'',
  117. 'contactor'=>'',
  118. 'goodName'=>'',
  119. "page"=>1,"size"=>15],"post","trim");
  120. $where=[["act_good.is_del","=",0]];
  121. if($param['actCode']!==""){
  122. $where[]=["act_good.actCode","like","%".$param['actCode']."%"];
  123. }
  124. if($param['status']!==""){
  125. $where[]=["act_good.status","=",$param['status']];
  126. }
  127. if($param['skuCode']!==""){
  128. $where[]=["yz_good_code","like","%".$param['skuCode']."%"];
  129. }
  130. if($param['roundId']!==""){
  131. $where[]=["roundId","=",$param['roundId']];
  132. }
  133. if($param['version']!==''){
  134. $where[]=['act_good.version','=',$param['version']];
  135. }
  136. if($param['company_name']!==""){
  137. $where[]=["act.company_name","like","%".$param['company_name']."%"];
  138. }
  139. if($param['contactor']!==""){
  140. $where[]=["act.contactor","like","%".$param['contactor']."%"];
  141. }
  142. if($param['goodName']!==""){
  143. $where[]=["goodName","like","%".$param['goodName']."%"];
  144. }
  145. $list= $this->model->with(["act","platformYz"=>["platform","good"=>['unit','brand','category']],"promocode"])
  146. ->withJoin(["act"],"left")
  147. ->where($where)->order("act_good.id desc")->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
  148. $list->each(function(&$item){
  149. $item['status_cn']=ActGood::$statusCn[$item['status']]??"";
  150. $item['ActStatus_cn']=\app\txx\model\Act::$statusCn[$item['act_status']];
  151. });
  152. $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  153. }
  154. public function info(){
  155. $param=$this->request->param(["id"=>""],"post","trim");
  156. $info= $this->model->with(["act","platformYz"=>["platform","good"=>['unit','brand','category']],"promocode"])->where(['is_del' => 0])->findOrEmpty($param['id']);
  157. if($info->isEmpty()){
  158. $this->error("信息有误");
  159. }
  160. $info['status_cn']=ActGood::$statusCn[$info['status']]??"";
  161. $info['act_status_cn']=\app\txx\model\Act::$statusCn[$info['act_status']];
  162. $this->success("获取成功",$info);
  163. }
  164. public function resetPromocode(){
  165. $param=$this->request->param(["id"=>""],"post","trim");
  166. $info= $this->model->findOrEmpty($param['id']);
  167. if($info->isEmpty()){
  168. $this->error("信息有误");
  169. }
  170. if(!in_array($info->status,[4,5]))throw new \Exception("状态有误");
  171. try{
  172. if($info->promocode_id!=0){
  173. $info->status=2;
  174. $promocode= \app\youzan\model\YzActivityPromoCode::where(["is_del"=>0,"id"=>$info->promocode_id])->findOrEmpty();
  175. if(!$promocode->isEmpty()){
  176. $promocode->save(['status'=>0,"remark"=>"重试生成券码"]);
  177. $info->remark="重试生成券码";
  178. }else{
  179. $info->promocode_id=0;
  180. $info->status=1;
  181. }
  182. }else{
  183. $info->promocode_id=0;
  184. $info->status=1;
  185. }
  186. $save= $info->save();
  187. if(!$save)throw new \Exception('重置失败');
  188. }catch (\Exception $e){
  189. $this->error($e->getMessage());
  190. }
  191. $this->success("重置成功");
  192. }
  193. public function delete(){
  194. $param=$this->request->param(["id"=>""],"post","trim");
  195. $info= $this->model->findOrEmpty($param['id']);
  196. if($info->isEmpty()){
  197. $this->error("信息有误");
  198. }
  199. $info->is_del=1;
  200. try{
  201. $save= $info->save();
  202. if(!$save)throw new \Exception('删除失败');
  203. if($info->promocode_id!==0){
  204. $promocode= \app\youzan\model\YzActivityPromoCode::where(['is_del'=>0,'id'=>$info->promocode_id])->findOrEmpty();
  205. if($promocode->isEmpty())throw new \Exception('优惠券信息有误');
  206. $promocode->save(['is_del'=>1]);
  207. }
  208. }catch (\Exception $e){
  209. $this->error($e->getMessage());
  210. }
  211. $this->success("删除成功");
  212. }
  213. public function promocodeList(){
  214. $param=$this->request->param(["title"=>"","status"=>"","start"=>"","end"=>"","page"=>1,"size"=>15],"post","trim");
  215. $where=[["is_del","=",0]];
  216. if($param['title']!==""){
  217. $where[]=["title","like","%".$param['title']."%"];
  218. }
  219. if($param['status']!==""){
  220. $where[]=["status","=",$param['status']];
  221. }
  222. if($param['start']!==""){
  223. $where[]=["createTime",">=",startTime($param['start'])];
  224. }
  225. if($param['end']!==""){
  226. $where[]=["createTime","<=",endTime($param['end'])];
  227. }
  228. $list= YzActivityPromoCode::with(["Good"])
  229. ->where($where)
  230. ->order("id desc")
  231. ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
  232. $list->each(function(&$item){
  233. $item['status_cn']=YzActivityPromoCode::$statusCn[$item['status']]??"";
  234. $item['codeType_cn']=YzActivityPromoCode::$codeTypeCn[$item['code_type']]??"";
  235. $item["rangeType_cn"]=YzActivityPromoCode::$rangeTypeCn[$item['range_type']]??"";
  236. });
  237. $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  238. }
  239. /**
  240. * 优惠券删除
  241. */
  242. public function promocodeDelete(){
  243. $param=$this->request->param(["id"=>""],"post","trim");
  244. $info= YzActivityPromoCode::where(['is_del'=>0,'id'=>$param['id']])->findOrEmpty();
  245. if($info->isEmpty()){
  246. $this->error("信息有误");
  247. }
  248. if($info->act_good_id!=0){
  249. $act = $this->model->with(['act'])->where(['is_del' => 0, 'id' => $info->act_good_id])->findOrEmpty();
  250. if (time()<=strtotime($act->endTime) && time()>=strtotime($act->startTime)) $this->error('活动正在进行中,不能删除');
  251. }
  252. $info->is_del=1;
  253. try{
  254. $save= $info->save();
  255. if(!$save)throw new \Exception('删除失败');
  256. if(isset($act) && !$act->isEmpty()){
  257. if(time()<strtotime($act->startTime)){ //活动未开始
  258. $act->promocode_id=0;
  259. $act->status=1;
  260. $act->save();
  261. }
  262. }
  263. }catch (\Exception $e){
  264. $this->error($e->getMessage());
  265. }
  266. $this->success("删除成功");
  267. }
  268. }