BargaingoodsController.php 3.5 KB

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