TuangoodsController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\TuanGoods;
  5. use app\model\Goods;
  6. use app\model\GoodsSku;
  7. use app\model\TuanGoodsSkuValue;
  8. use app\model\Coupon;
  9. class TuangoodsController extends Base
  10. {
  11. function index()
  12. {
  13. $weid = weid();
  14. $keyword = input('post.keyword', '', 'serach_in');
  15. $query = TuanGoods::where(['weid' => $weid]);
  16. if (!empty($keyword)) {
  17. $query->where('title', 'like', '%' . $keyword . '%');
  18. }
  19. if (!empty($this->sid)) {
  20. $query->where('sid', $this->sid);
  21. }
  22. $res = $query->order('sort asc,id desc')
  23. ->paginate(getpage())
  24. ->toArray();
  25. foreach ($res['data'] as &$vo) {
  26. $vo['begin_date'] = time_format($vo['begin_date']);
  27. $vo['end_date'] = time_format($vo['end_date']);
  28. $vo['is_luckydraw'] = yesno($vo['is_luckydraw']);
  29. if (!empty($vo['goods_id'])) {
  30. $goods = Goods::find($vo['goods_id']);
  31. if (!empty($goods)) {
  32. $vo['goods'] = $goods->toArray();
  33. $vo['image'] = toimg($vo['goods']['image']);
  34. }
  35. }
  36. }
  37. $data['data'] = $res;
  38. return $this->json($data);
  39. }
  40. function listUpdate()
  41. {
  42. $data = only('id,status,sort');
  43. if (!$data['id']) throw new ValidateException('参数错误');
  44. TuanGoods::update($data);
  45. return $this->json(['msg' => '操作成功']);
  46. }
  47. public function update()
  48. {
  49. $id = $this->request->post('id');
  50. $data = input('post.');
  51. unset($data['create_time']);
  52. $data['begin_date'] = strtotime($data['begin_date']);
  53. $data['end_date'] = strtotime($data['end_date']);
  54. if (empty($id)) {
  55. $data['weid'] = weid();
  56. if (!empty($this->sid)) {
  57. $data['sid'] = $this->sid;
  58. }
  59. try {
  60. $res = TuanGoods::create($data);
  61. if ($res->id && empty($data['sort'])) {
  62. TuanGoods::update(['sort' => $res->id, 'id' => $res->id]);
  63. }
  64. $data['id'] = $res->id;
  65. $this->_synupdata($data);
  66. } catch (\Exception $e) {
  67. throw new ValidateException($e->getMessage());
  68. }
  69. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  70. } else {
  71. try {
  72. TuanGoods::update($data);
  73. $this->_synupdata($data);
  74. } catch (\Exception $e) {
  75. throw new ValidateException($e->getMessage());
  76. }
  77. return $this->json(['msg' => '修改成功']);
  78. }
  79. }
  80. function _synupdata($data)
  81. {
  82. TuanGoodsSkuValue::where(['tuan_id' => $data['id'], 'goods_id' => $data['goods_id']])->delete();
  83. if (isset($data['sku'])) {
  84. foreach ($data['sku'] as $skuarr) {
  85. TuanGoodsSkuValue::create([
  86. 'tuan_id' => (int) $data['id'],
  87. 'goods_id' => (int) $data['goods_id'],
  88. 'sku' => $skuarr['sku'],
  89. 'image' => $skuarr['image'],
  90. 'quantity' => $skuarr['quantity'],
  91. 'price' => $skuarr['price']
  92. ]);
  93. }
  94. }
  95. }
  96. function getInfo()
  97. {
  98. $id = $this->request->post('id', '', 'serach_in');
  99. if (!$id) throw new ValidateException('参数错误');
  100. $data = TuanGoods::field('*')->find($id)->toArray();
  101. if (!empty($data['goods_id'])) {
  102. $goods = Goods::find($data['goods_id']);
  103. if (!empty($goods)) {
  104. $data['goods'] = $goods->toArray();
  105. }
  106. }
  107. $data['attribute'] = GoodsSku::get_goods_sku($data['goods_id']);
  108. $data['sourceAttribute'] = $data['attribute'];
  109. $data['sku'] = TuanGoodsSkuValue::field('sku,image,quantity,price')->where(['tuan_id' => $id, 'goods_id' => $data['goods_id']])
  110. ->order('id asc')
  111. ->select()->toArray();
  112. $data['begin_date'] = time_format($data['begin_date']);
  113. $data['end_date'] = time_format($data['end_date']);
  114. return $this->json(['data' => $data]);
  115. }
  116. function getField()
  117. {
  118. $data['notwinningptypearray'] = getNotWinningPtype();
  119. $data['couponarray'] = Coupon::getpcarray();
  120. return $this->json(['data' => $data]);
  121. }
  122. function delete()
  123. {
  124. return $this->del(new TuanGoods());
  125. }
  126. }