MiaoshagoodsController.php 3.4 KB

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