GoodsgiftcardtypeController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\GoodsGiftcardType;
  5. use app\model\GoodsGiftcardCommission;
  6. use app\model\Category;
  7. use app\model\Goods;
  8. class GoodsgiftcardtypeController extends Base
  9. {
  10. function index()
  11. {
  12. $weid = weid();
  13. $page = input('post.page', 1, 'intval');
  14. $keyword = input('post.keyword', '', 'serach_in');
  15. $status = input('post.status', '', 'serach_in');
  16. $query = GoodsGiftcardType::where(['weid' => $weid]);
  17. if (!empty($keyword)) {
  18. $query->where('name', 'like', '%' . $keyword . '%');
  19. }
  20. if (!empty($status) || $status === "0") {
  21. $query->where(['status' => $status]);
  22. }
  23. if (!empty($this->sid)) {
  24. $query->where('sid', $this->sid);
  25. }
  26. $res = $query->order('sort asc,id desc')
  27. ->paginate(getpage())
  28. ->toArray();
  29. foreach ($res['data'] as &$vo) {
  30. if ($vo['expire_type'] == 10) {
  31. if ($vo['expire_day'] > 0) {
  32. $vo['expire_day'] = $vo['expire_day'] . "天";
  33. } else {
  34. $vo['expire_day'] = "长期";
  35. }
  36. $vo['expire_type'] = '领取后:' . $vo['expire_day'] . '有效';;
  37. } elseif ($vo['expire_type'] == 20) {
  38. $vo['expire_type'] = '有效期:' . time_format($vo['start_time']) . '到' . time_format($vo['end_time']);
  39. }
  40. $vo['color'] = getColor($vo['color']);
  41. }
  42. $data['data'] = $res;
  43. return $this->json($data);
  44. }
  45. function listUpdate()
  46. {
  47. $data = only('id,status,sort');
  48. if (!$data['id']) throw new ValidateException('参数错误');
  49. GoodsGiftcardType::update($data);
  50. return $this->json(['msg' => '操作成功']);
  51. }
  52. public function update()
  53. {
  54. $id = $this->request->post('id');
  55. $data = input('post.');
  56. unset($data['create_time']);
  57. $data['start_time'] = strtotime($data['start_time']);
  58. $data['end_time'] = strtotime($data['end_time']);
  59. if (empty($id)) {
  60. $data['weid'] = weid();
  61. if (!empty($this->sid)) {
  62. $data['sid'] = $this->sid;
  63. }
  64. try {
  65. $res = GoodsGiftcardType::create($data);
  66. if ($res->id && empty($data['sort'])) {
  67. GoodsGiftcardType::update(['sort' => $res->id, 'id' => $res->id]);
  68. }
  69. $this->_synupdata($data);
  70. } catch (\Exception $e) {
  71. throw new ValidateException($e->getMessage());
  72. }
  73. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  74. } else {
  75. try {
  76. GoodsGiftcardType::update($data);
  77. $this->_synupdata($data);
  78. } catch (\Exception $e) {
  79. throw new ValidateException($e->getMessage());
  80. }
  81. return $this->json(['msg' => '修改成功']);
  82. }
  83. }
  84. function _synupdata($data)
  85. {
  86. GoodsGiftcardCommission::where('card_tid', $data['id'])->delete();
  87. if (!empty($data['commission'])) {
  88. foreach ($data['commission'] as $mcvo) {
  89. if ($mcvo['return_percent'] > 0) {
  90. GoodsGiftcardCommission::create([
  91. 'card_tid' => (int) $data['id'],
  92. 'commission_method' => $data['commission_method'],
  93. 'roletype' => $mcvo['roletype'],
  94. 'return_percent' => $mcvo['return_percent']
  95. ]);
  96. }
  97. }
  98. }
  99. }
  100. function getInfo()
  101. {
  102. $id = $this->request->post('id', '', 'serach_in');
  103. if (!$id) throw new ValidateException('参数错误');
  104. $data = GoodsGiftcardType::find($id)->toArray();
  105. if ($data['use_goods']==2 && !empty($data['goods_ids'])) {
  106. $goods = Goods::find($data['goods_ids']);
  107. if (!empty($goods)) {
  108. $data['goods'] = $goods->toArray();
  109. }
  110. }else{
  111. $data['goods'] = [];
  112. }
  113. if (empty($data['start_time'])) {
  114. $data['start_time'] = "";
  115. } else {
  116. $data['start_time'] = time_ymd($data['start_time']);
  117. }
  118. if (empty($data['end_time'])) {
  119. $data['end_time'] = "";
  120. } else {
  121. $data['end_time'] = time_ymd($data['end_time']);
  122. }
  123. $GiftcardCommission = GoodsGiftcardCommission::where('card_tid', $id)->select()->toArray();
  124. foreach ($GiftcardCommission as $key => $vo) {
  125. $mc[$vo['roletype']] = $vo['return_percent'];
  126. }
  127. $data['commission'] = getCommissionType();
  128. foreach ($data['commission'] as &$vo) {
  129. $vo['return_percent'] = $mc[$vo['roletype']];
  130. }
  131. return $this->json(['data' => $data]);
  132. }
  133. function delete()
  134. {
  135. return $this->del(new GoodsGiftcardType());
  136. }
  137. function getField()
  138. {
  139. $data['colorarray'] = getColor();
  140. $data['expire_typearray'] = getExpireType();
  141. $data['cat_idsarray'] = _generateSelectTree(Category::getpcarray());
  142. return $this->json(['data' => $data]);
  143. }
  144. function getCommissionType()
  145. {
  146. $data['commission'] = getCommissionType();
  147. return $this->json(['data' => $data]);
  148. }
  149. }