Collageproduct.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace app\mall\admin;
  3. use app\admin\controller\Admin;
  4. use think\Db;
  5. use think\Request;
  6. use think\Validate;
  7. use app\common\builder\ZBuilder;
  8. class Collageproduct extends Admin
  9. {
  10. public function index(){
  11. $order = $this->getOrder();
  12. if($order===''){
  13. $order='id desc';
  14. }
  15. $map = $this->getMap();
  16. $data_list = Db::name('collage_products')->where($map)->order($order)->paginate();
  17. $page = $data_list->render();
  18. return ZBuilder::make('table')
  19. ->setPageTitle('') // 设置页面标题
  20. ->setPageTips('') // 设置页面提示信息
  21. ->hideCheckbox() //隐藏第一列多选框
  22. ->setTableName('collage_products') // 指定数据表名
  23. ->addOrder('id,end_at') // 添加排序
  24. ->addTopSelect('status', '全部拼单状态', ['0'=>'进行中','1'=>'拼单成功','2'=>'拼单失败']) //添加顶部下拉筛选
  25. ->setSearch(['id' => 'ID', 'title' => '产品名称'], '', '', '搜索') // 设置搜索参数
  26. ->addColumns([
  27. ['id', 'ID'],
  28. ['title', '产品名称'],
  29. ['collage_price', '拼单价格(元)'],
  30. ['collage_price', '购买价格(元)'],
  31. ['require_orders', '拼单要求'],
  32. ['collage_orders', '当前人数','callback',function($value,$data){
  33. return db('collage_orders')->where(['collage_product_id'=>$data['id']])->count('id');
  34. },'__data__'],
  35. ['end_at', '截止时间','datetime', '未知','Y-m-d H:i'],
  36. ['status','拼单状态','status', '', ['进行中','拼单成功','拼单失败:danger']],
  37. ['audit_status', '判定', 'callback',function($value,$data,$menu_id){
  38. if($data['status']==0){
  39. if(!if_menu_auth($menu_id)) return '';
  40. return "<a href='".url('audit',['ids'=>$data['id'],'status'=>'1'])."' class='btn btn-xs btn-default ajax-get confirm'>判定成功</a><a href='".url('audit',['ids'=>$data['id'],'status'=>'2',])."' class='btn btn-xs btn-default ajax-get confirm'>判定失败</a>";
  41. }else{
  42. return '已判定';
  43. }
  44. },'__data__','244'],
  45. ['right_button', '操作', 'btn'],
  46. ]) //添加多列数据
  47. //->addRightButton('custom',['title'=>'查看详情','href'=>url('look',['id'=>'__ID__'])],true)
  48. ->addRightButton('edit',['title'=>'编辑产品'])
  49. ->addRightButton('custom',['title'=>'查看订单','href'=>url('mall/collageorder/index',['collage_product_id'=>'__ID__'])])
  50. ->addTopButton('add',['title'=>'新建产品'])
  51. //->addTopButton('custom',['title'=>'判定成功','href'=>url('audit',['status'=>'1']),'icon'=>'fa fa-fw fa-calendar-check-o','class'=>'btn btn-primary ajax-post confirm'])
  52. //->addTopButton('custom',['title'=>'判定失败','href'=>url('audit',['status'=>'2']),'icon'=>'fa fa-fw fa-calendar-times-o','class'=>'btn btn-primary ajax-post confirm'])
  53. ->setRowList($data_list) // 设置表格数据
  54. ->setPages($page) // 设置分页数据
  55. ->fetch();
  56. }
  57. /*public function look($id=''){
  58. $collage_product=Db::name("collage_products")->where('id',$id)->find();
  59. if(!$collage_product){
  60. return $this->error('请求错误');
  61. }
  62. // 使用ZBuilder快速创建表单
  63. }*/
  64. public function audit(){
  65. $status=input('status','0');
  66. if($status!='1' && $status!='2'){
  67. return $this->error('请求错误');
  68. }
  69. $now=time();
  70. $ids = (Request::instance()->isGet()) ? input('ids') : input('post.ids/a');
  71. $ids=(array)$ids;
  72. foreach ($ids as $key => $value) {
  73. if(Db::name('collage_products')->where('id',$value)->value('status')!='0'){
  74. unset($ids[$key]);
  75. }
  76. }
  77. $rt=Db::name('collage_products')->where('id','in',$ids)->update(['status'=>$status,'updated_at'=>$now]);
  78. if($rt!==false){
  79. if($status=='1'){
  80. //判定订单状态
  81. Db::name('collage_orders')->where('collage_product_id','in',$ids)->update(['state'=>'1','updated_at'=>$now]);
  82. //记录订单事件
  83. $collage_order_ids=Db::name('collage_orders')->field('id')->where('collage_product_id','in',$ids)->select();
  84. foreach ($collage_order_ids as $key => $value) {
  85. Db::name('collage_order_events')->insertGetId(['collage_order_id'=>$value['id'],'event'=>'拼单成功','created_at'=>$now,'updated_at'=>$now]);
  86. }
  87. $tips='成功';
  88. }else{
  89. //判定订单状态
  90. Db::name('collage_orders')->where('collage_product_id','in',$ids)->update(['state'=>'2','updated_at'=>$now]);
  91. //记录订单事件
  92. $collage_order_ids=Db::name('collage_orders')->field('id')->where('collage_product_id','in',$ids)->select();
  93. foreach ($collage_order_ids as $key => $value) {
  94. Db::name('collage_order_events')->insertGetId(['collage_order_id'=>$value['id'],'event'=>'拼单失败','created_at'=>$now,'updated_at'=>$now]);
  95. }
  96. $tips='失败';
  97. }
  98. return $this->success('已判定为拼单'.$tips);
  99. } else {
  100. return $this->error('数据库写入错误');
  101. }
  102. }
  103. public function add(){
  104. //判断是否为post请求
  105. if (Request::instance()->isPost()) {
  106. $now=time();
  107. //获取请求的post数据
  108. $data=input('post.');
  109. //数据输入验证
  110. $validate = new Validate([
  111. 'title|标题'=> 'require|length:1,20',
  112. 'weight|重量' => 'require|gt:0|regex:^\d{0,5}(\.\d{1,3})?$',
  113. 'bland|品牌' => 'require',
  114. 'require_orders|拼单要求' => 'require|gt:0|regex:^\d{0,10}$',
  115. 'collage_price|拼单价格' => 'require|gt:0|regex:^\d{0,10}(\.\d{1,2})?$',
  116. 'price|购买价格' => 'require|gt:0|regex:^\d{0,10}(\.\d{1,2})?$',
  117. 'end_at|截止时间' => 'require',
  118. 'admin_attachment_ids|图片' => 'require',
  119. 'detail|产品详情' => 'require'
  120. ]);
  121. if (!$validate->check($data)) {
  122. return $this->error($validate->getError());
  123. }
  124. if ($data['collage_price']>=$data['price']) {
  125. return $this->error('拼单价格必须小于购买价格');
  126. }
  127. $data['end_at']=strtotime($data['end_at']);
  128. if ($data['end_at']<=$now) {
  129. return $this->error('截止时间必须在当前时间之后');
  130. }
  131. if (count(explode(',',$data['admin_attachment_ids']))>5) {
  132. return $this->error('图片最多只能上传五张');
  133. }
  134. //数据处理
  135. $insert=array();
  136. $insert['title']=$data['title'];
  137. $insert['spec']=$data['spec'];
  138. $insert['size']=$data['size'];
  139. $insert['weight']=$data['weight'];
  140. $insert['bland']=$data['bland'];
  141. $insert['require_orders']=$data['require_orders'];
  142. $insert['collage_price']=$data['collage_price'];
  143. $insert['price']=$data['price'];
  144. $insert['end_at']=$data['end_at'];
  145. $insert['admin_attachment_ids']=$data['admin_attachment_ids'];
  146. $insert['detail']=$data['detail'];
  147. $insert['created_at']=$now;
  148. $insert['updated_at']=$now;
  149. //数据更新
  150. $insert_id=Db::name("collage_products")->insertGetId($insert);
  151. //跳转
  152. if($insert_id>0){
  153. return $this->success('发布拼单产品成功',url('index'));
  154. } else {
  155. return $this->error('发布拼单产品失败');
  156. }
  157. }
  158. // 使用ZBuilder快速创建表单
  159. return ZBuilder::make('form')
  160. ->setPageTitle('新建拼单产品') // 设置页面标题
  161. ->setPageTips('请认真编辑相关信息') // 设置页面提示信息
  162. ->setBtnTitle('submit', '发布') //修改默认按钮标题
  163. ->addBtn('<button type="reset" class="btn btn-default">重置</button>') //添加额外按钮
  164. ->addText('title', '标题','必填,限制在20个字以内')
  165. ->addText('spec', '规格描述','')
  166. ->addText('size', '尺寸','')
  167. ->addText('weight', '重量','必填,规则范围0.001~99999.999', '', ['', 'KG'])
  168. ->addText('bland', '品牌','必填')
  169. ->addText('require_orders', '拼单要求','必填,即最低订单笔数,规则范围1~9999999999')
  170. ->addText('collage_price', '拼单价格','必填,若有人参与拼单后则不可修改,需小于购买价格,规则范围0.01~9999999999.99', '', ['<i class="fa fa-fw fa-yen"></i>', ''])
  171. ->addText('price', '购买价格','必填,若有人直接购买或补差价购买后则不可修改,规则范围0.00~9999999999.99', '', ['<i class="fa fa-fw fa-yen"></i>', ''])
  172. ->addDatetime('end_at', '截止时间', '必选,需为确定提交之后的时间')
  173. ->addImages('admin_attachment_ids', '图片','必传,最多上传五张图片,第一张为主图')
  174. ->addUeditor('detail', '产品详情','')
  175. //->isAjax(false) //默认为ajax的post提交
  176. ->fetch();
  177. }
  178. public function edit($id=''){
  179. //判断是否为post请求
  180. if (Request::instance()->isPost()) {
  181. $now=time();
  182. //获取请求的post数据
  183. $data=input('post.');
  184. //数据输入验证
  185. $validate = new Validate([
  186. 'title|标题'=> 'require|length:1,20',
  187. 'weight|重量' => 'require|gt:0|regex:^\d{0,5}(\.\d{1,3})?$',
  188. 'bland|品牌' => 'require',
  189. 'require_orders|拼单要求' => 'require|gt:0|regex:^\d{0,10}$',
  190. 'collage_price|拼单价格' => 'require|gt:0|regex:^\d{0,10}(\.\d{1,2})?$',
  191. 'price|购买价格' => 'require|gt:0|regex:^\d{0,10}(\.\d{1,2})?$',
  192. 'end_at|截止时间' => 'require',
  193. 'admin_attachment_ids|图片' => 'require',
  194. 'detail|产品详情' => 'require'
  195. ]);
  196. if (!$validate->check($data)) {
  197. return $this->error($validate->getError());
  198. }
  199. if ($data['collage_price']>=$data['price']) {
  200. return $this->error('拼单价格必须小于购买价格');
  201. }
  202. $data['end_at']=strtotime($data['end_at']);
  203. if ($data['end_at']<db('collage_products')->where('id',$data['id'])->value('end_at')) {
  204. return $this->error('截止时间不能小于原来的截止时间');
  205. }
  206. if (count(explode(',',$data['admin_attachment_ids']))>5) {
  207. return $this->error('图片最多只能上传五张');
  208. }
  209. //数据处理
  210. $update=array();
  211. $update['id']=$data['id'];
  212. $update['title']=$data['title'];
  213. $update['spec']=$data['spec'];
  214. $update['size']=$data['size'];
  215. $update['weight']=$data['weight'];
  216. $update['bland']=$data['bland'];
  217. $update['require_orders']=$data['require_orders'];
  218. $update['collage_price']=$data['collage_price'];
  219. $update['price']=$data['price'];
  220. $update['end_at']=$data['end_at'];
  221. $update['admin_attachment_ids']=$data['admin_attachment_ids'];
  222. $update['detail']=$data['detail'];
  223. $update['updated_at']=$now;
  224. //数据更新
  225. $rt=Db::name("collage_products")->update($update);
  226. //跳转
  227. if($rt!==false){
  228. return $this->success('编辑拼单产品成功',url('index'));
  229. } else {
  230. return $this->error('编辑拼单产品失败');
  231. }
  232. }
  233. // 接收id
  234. if ($id>0) {
  235. // 查处数据
  236. $collage_product=Db::name("collage_products")->where('id',$id)->find();
  237. if(!$collage_product){
  238. return $this->error('请求错误');
  239. }
  240. $col_order=db('collage_orders')->where('collage_product_id',$id)->where('state','in','1,2,3')->find();
  241. if($col_order){
  242. $col_attr='readonly';
  243. $col_tips='已有人参与拼单,不能修改';
  244. }else{
  245. $col_attr='';
  246. $col_tips='必填,需小于购买价格,规则范围0.01~9999999999.99';
  247. }
  248. $buy_order=db('collage_orders')->where('collage_product_id',$id)->where('state','in','4')->find();
  249. if($buy_order){
  250. $buy_attr='readonly';
  251. $buy_tips='已有人购买,不能修改';
  252. }else{
  253. $buy_attr='';
  254. $buy_tips='必填,规则范围0.00~9999999999.99';
  255. }
  256. // 使用ZBuilder快速创建表单
  257. return ZBuilder::make('form')
  258. ->setPageTitle('编辑拼单产品') // 设置页面标题
  259. ->setPageTips('请认真编辑相关信息') // 设置页面提示信息
  260. ->setBtnTitle('submit', '发布') //修改默认按钮标题
  261. ->addBtn('<button type="reset" class="btn btn-default">重置</button>') //添加额外按钮
  262. ->addText('title', '标题','必填,限制在20个字以内',$collage_product['title'])
  263. ->addText('spec', '规格描述','',$collage_product['spec'])
  264. ->addText('size', '尺寸','',$collage_product['size'])
  265. ->addText('weight', '重量','必填,规则范围0.001~99999.999',$collage_product['weight'], ['', 'KG'])
  266. ->addText('bland', '品牌','必填',$collage_product['bland'])
  267. ->addText('require_orders', '拼单要求','必填,即最低订单笔数,规则范围1~9999999999',$collage_product['require_orders'])
  268. ->addText('collage_price','拼单价格', $col_tips,$collage_product['collage_price'], ['<i class="fa fa-fw fa-yen"></i>', ''], $col_attr)
  269. ->addText('price', '购买价格',$buy_tips,$collage_product['price'], ['<i class="fa fa-fw fa-yen"></i>', ''], $buy_attr)
  270. ->addDatetime('end_at', '截止时间', '必选,需为确定提交之后的时间',date('Y-m-d H:i',$collage_product['end_at']))
  271. ->addImages('admin_attachment_ids', '图片','必传,最多上传五张图片,第一张为主图',$collage_product['admin_attachment_ids'])
  272. ->addUeditor('detail', '产品详情','',$collage_product['detail'])
  273. ->addHidden('id',$collage_product['id'])
  274. //->isAjax(false) //默认为ajax的post提交
  275. ->fetch();
  276. }
  277. }
  278. }