Activity.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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"])->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. $promocode= \app\youzan\model\YzActivityPromoCode::where(["is_del"=>0,"id"=>$info->promocode_id])->findOrEmpty();
  174. if($promocode->isEmpty()){
  175. $info->promocode_id=0;
  176. $info->status=1;
  177. }else {
  178. $promocode->save(['status'=>0]);
  179. $info->remark="重试生成券码";
  180. }
  181. }else{
  182. $info->promocode_id=0;
  183. $info->status=1;
  184. }
  185. $save= $info->save();
  186. if(!$save)throw new \Exception('重置失败');
  187. }catch (\Exception $e){
  188. $this->error($e->getMessage());
  189. }
  190. $this->success("重置成功");
  191. }
  192. public function delete(){
  193. $param=$this->request->param(["id"=>""],"post","trim");
  194. $info= $this->model->findOrEmpty($param['id']);
  195. if($info->isEmpty()){
  196. $this->error("信息有误");
  197. }
  198. $info->is_del=1;
  199. try{
  200. $save= $info->save();
  201. if(!$save)throw new \Exception('删除失败');
  202. if($info->promocode_id!==0){
  203. $promocode= \app\youzan\model\YzActivityPromoCode::where(['is_del'=>0,'id'=>$info->promocode_id])->findOrEmpty();
  204. if($promocode->isEmpty())throw new \Exception('优惠券信息有误');
  205. $promocode->save(['is_del'=>1]);
  206. }
  207. }catch (\Exception $e){
  208. $this->error($e->getMessage());
  209. }
  210. $this->success("删除成功");
  211. }
  212. public function promocodeList(){
  213. $param=$this->request->param(["title"=>"","status"=>"","start"=>"","end"=>"","page"=>1,"size"=>15],"post","trim");
  214. $where=[["is_del","=",0]];
  215. if($param['title']!==""){
  216. $where[]=["title","like","%".$param['title']."%"];
  217. }
  218. if($param['status']!==""){
  219. $where[]=["status","=",$param['status']];
  220. }
  221. if($param['start']!==""){
  222. $where[]=["createTime",">=",startTime($param['start'])];
  223. }
  224. if($param['end']!==""){
  225. $where[]=["createTime","<=",endTime($param['end'])];
  226. }
  227. $list= YzActivityPromoCode::with(["Good"])
  228. ->where($where)
  229. ->order("id desc")
  230. ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
  231. $list->each(function(&$item){
  232. $item['status_cn']=YzActivityPromoCode::$statusCn[$item['status']]??"";
  233. $item['codeType_cn']=YzActivityPromoCode::$codeTypeCn[$item['code_type']]??"";
  234. $item["rangeType_cn"]=YzActivityPromoCode::$rangeTypeCn[$item['range_type']]??"";
  235. });
  236. $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  237. }
  238. /**
  239. * 优惠券删除
  240. */
  241. public function promocodeDelete(){
  242. $param=$this->request->param(["id"=>""],"post","trim");
  243. $info= YzActivityPromoCode::where(['is_del'=>0,'id'=>$param['id']])->findOrEmpty();
  244. if($info->isEmpty()){
  245. $this->error("信息有误");
  246. }
  247. $info->is_del=1;
  248. try{
  249. $save= $info->save();
  250. if(!$save)throw new \Exception('删除失败');
  251. if($info->act_good_id!==0){
  252. $act= $this->model->with(["act"])->where(['is_del'=>0,'id'=>$info->act_good_id])->findOrEmpty();
  253. if(!$act->isEmpty()){
  254. if(time()<strtotime($act->startTime)){ //活动未开始
  255. $act->promocode_id=0;
  256. $act->status=1;
  257. $act->save();
  258. }
  259. }
  260. }
  261. }catch (\Exception $e){
  262. $this->error($e->getMessage());
  263. }
  264. $this->success("删除成功");
  265. }
  266. }