Member_set.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace Agent\Lib;
  3. use Dever;
  4. # 批量设置商品
  5. class Member_set
  6. {
  7. public function __construct()
  8. {
  9. //Dever::load('manage/auth.init');
  10. }
  11. # 获取商品列表
  12. public function goods_api()
  13. {
  14. $id = Dever::input('mid');
  15. return Dever::load('agent/lib/set')->getData($id);
  16. }
  17. # 设置商品
  18. public function home_api()
  19. {
  20. $data = array();
  21. $data['link'] = Dever::decode(Dever::input('refer'));
  22. $data['mid'] = Dever::input('mid');
  23. $data['search_api_mid'] = Dever::input('search_api_mid');
  24. $data['host'] = Dever::url('lib/member_set.home?mid=' . $data['mid'], 'agent');
  25. $data['url'] = Dever::url('lib/member_set.goods?mid=' . $data['mid'], 'agent');
  26. $data['submit'] = Dever::url('lib/member_set.action_commit?json=1', 'agent');
  27. return Dever::render('setMemberGoods', $data);
  28. }
  29. # 设置
  30. public function action_commit_api()
  31. {
  32. $search_api_mid = Dever::input('search_api_mid');
  33. $goods = Dever::input('goods');
  34. if (!$goods) {
  35. Dever::alert('请传入商品');
  36. }
  37. $goods = Dever::json_decode($goods);
  38. $mid = Dever::input('mid');
  39. $desc = Dever::input('desc');
  40. $where['option_mid'] = $mid;
  41. $where['set_state'] = 2;
  42. Dever::db('agent/member_goods')->updates($where);
  43. foreach ($goods as $k => $v) {
  44. $type = 1;
  45. $type_id = $k;
  46. $sku_id = -1;
  47. if (strstr($k, '_')) {
  48. $temp = explode('_', $k);
  49. $k = $temp[0];
  50. $type_id = $temp[0];
  51. $type = $temp[1];
  52. }
  53. if (strstr($k, '-')) {
  54. $temp = explode('-', $k);
  55. $type_id = $temp[0];
  56. $sku_id = $temp[1];
  57. }
  58. if ($v['total'] < 0) {
  59. continue;
  60. }
  61. $w = array();
  62. $w['type'] = $type;
  63. $w['type_id'] = $type_id;
  64. $w['sku_id'] = $sku_id;
  65. $w['mid'] = $mid;
  66. if ($v['del'] == 2) {
  67. $info = Dever::db('agent/member_goods')->one($w);
  68. if ($info) {
  69. Dever::db('agent/member_goods')->update(array('where_id' => $info['id'], 'state' => 2));
  70. $name = $info['name'] ? $info['name'] : Dever::load("agent/lib/manage.getGoods#name", $info['type'], $info['type_id'], $info['sku_id']);
  71. Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], -1*$info['total_num'], $info['total_num'], 0, $name, $desc . '[删除]');
  72. }
  73. } else {
  74. $total = $v['total'];
  75. $cash = $price = 0;
  76. if ($type == 1) {
  77. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($w['type_id'], $w['sku_id']);
  78. $price = $goods_info['price'];
  79. $cash = $goods_info['price'] * $total;
  80. if (isset($goods_info['sku'])) {
  81. $sku = '-' . $goods_info['sku']['string'];
  82. } else {
  83. $sku = '';
  84. }
  85. $name = $goods_info['name'] . $sku;
  86. } elseif ($type == 2) {
  87. # 体验卡
  88. $card = Dever::db('card/info')->find($w['type_id']);
  89. if (!$card) {
  90. continue;
  91. }
  92. $name = $card['name'];
  93. if ($sku_id == -1) {
  94. if ($card['create_type'] == 2) {
  95. $cash = $price = $card['value'] * $total;
  96. $total = 1;
  97. $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
  98. if (!$w['sku_id']) {
  99. continue;
  100. }
  101. } else {
  102. if ($card['create_type'] == 3 && isset($v['price']) && $v['price'] > 0) {
  103. $card['value'] = $v['price'];
  104. }
  105. $cash = $price = $card['value'];
  106. for($i=0; $i<$total;$i++) {
  107. $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
  108. if ($w['sku_id']) {
  109. $w['total_num'] = 1;
  110. $w['cash'] = $cash;
  111. $w['price'] = $price;
  112. $w['name'] = $name;
  113. $state = Dever::db('agent/member_goods')->insert($w);
  114. if ($state) {
  115. Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], 1, 0, 1, $name, $desc);
  116. }
  117. }
  118. }
  119. continue;
  120. }
  121. } else {
  122. $sku = explode('i', $sku_id);
  123. if ($sku) {
  124. foreach ($sku as $v1) {
  125. $w['sku_id'] = $v1;
  126. $info = Dever::db('agent/member_goods')->one($w);
  127. if ($info) {
  128. $u = array();
  129. $u['where_id'] = $info['id'];
  130. $u['state'] = 1;
  131. $state = Dever::db('agent/member_goods')->update($u);
  132. }
  133. }
  134. }
  135. $total = $v['total'] - count($sku);
  136. if ($total > 0) {
  137. if ($card['create_type'] == 2) {
  138. $cash = $price = $card['value'] * $total;
  139. $total = 1;
  140. $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
  141. if (!$w['sku_id']) {
  142. continue;
  143. }
  144. } else {
  145. if ($card['create_type'] == 3 && isset($v['price']) && $v['price'] > 0) {
  146. $card['value'] = $v['price'];
  147. }
  148. $cash = $price = $card['value'];
  149. for($i=0; $i<$total;$i++) {
  150. $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
  151. if ($w['sku_id']) {
  152. $w['total_num'] = 1;
  153. $w['cash'] = $cash;
  154. $w['price'] = $price;
  155. $w['name'] = $name;
  156. $state = Dever::db('agent/member_goods')->insert($w);
  157. if ($state) {
  158. Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], 1, 0, 1, $name, $desc);
  159. }
  160. }
  161. }
  162. continue;
  163. }
  164. } else {
  165. continue;
  166. }
  167. }
  168. } elseif ($type == 3) {
  169. $prize = Dever::db('setting/prize')->find($w['type_id']);
  170. $price = $prize['price'];
  171. $cash = $prize['price'] * $total;
  172. $name = $prize['name'];
  173. }
  174. $info = Dever::db('agent/member_goods')->one($w);
  175. $w['total_num'] = $total;
  176. $w['cash'] = $cash;
  177. $w['price'] = $price;
  178. $w['name'] = $name;
  179. if (!$info) {
  180. $state = Dever::db('agent/member_goods')->insert($w);
  181. if ($state) {
  182. Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], $v['total'], 0, $w['total_num'], $name, $desc);
  183. }
  184. } else {
  185. $w['total_num'] += $info['sell_num'];
  186. $w['where_id'] = $info['id'];
  187. $w['state'] = 1;
  188. $state = Dever::db('agent/member_goods')->update($w);
  189. $num = $w['total_num']-$info['total_num'];
  190. if ($state && $num != 0) {
  191. Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], $num, $info['total_num'], $w['total_num'], $name, $desc);
  192. }
  193. }
  194. }
  195. }
  196. $link = Dever::input('link');
  197. if ($link) {
  198. return $link;
  199. }
  200. if ($search_api_mid) {
  201. return Dever::url('project/database/list&table=member_goods&project=agent&search_option_state=1&search_api_mid='.$search_api_mid, 'manage');
  202. } else {
  203. return Dever::url('project/database/list&table=member_goods&project=agent&search_option_state=1&search_option_mid='.$mid, 'manage');
  204. }
  205. }
  206. # 记录日志
  207. public function log($mid, $parent_order_num, $type, $type_id, $sku_id, $num, $pnum, $fnum, $name = '', $desc = '')
  208. {
  209. $data['mid'] = $mid;
  210. $data['parent_order_num'] = $parent_order_num;
  211. $data['type'] = $type;
  212. $data['type_id'] = $type_id;
  213. $data['sku_id'] = $sku_id;
  214. $data['num'] = $num;
  215. $data['pnum'] = $pnum;
  216. $data['fnum'] = $fnum;
  217. $data['desc'] = $desc;
  218. $data['name'] = $name;
  219. $data['order_num'] = $this->getOrderId();
  220. $admin = Dever::load('manage/auth.info');
  221. if ($admin) {
  222. $data['admin'] = $admin['id'];
  223. }
  224. $id = Dever::db('agent/member_goods_log')->insert($data);
  225. return $id;
  226. }
  227. /**
  228. * 生成日志订单号
  229. *
  230. * @return mixed
  231. */
  232. public function getOrderId()
  233. {
  234. $where['order_num'] = Dever::order('QY');
  235. $state = Dever::db('agent/member_goods_log')->one($where);
  236. if (!$state) {
  237. return $where['order_num'];
  238. } else {
  239. return $this->getOrderId();
  240. }
  241. }
  242. }