Member_set.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 $k1 => $v1) {
  125. $w['sku_id'] = $v1;
  126. $info = Dever::db('agent/member_goods')->one($w);
  127. if ($info) {
  128. $cur = ($info['total_num']-$info['sell_num']);
  129. if ($cur <= 0) {
  130. unset($sku[$k1]);
  131. }
  132. $u = array();
  133. $u['where_id'] = $info['id'];
  134. $u['state'] = 1;
  135. $state = Dever::db('agent/member_goods')->update($u);
  136. }
  137. }
  138. }
  139. $total = $v['total'] - count($sku);
  140. if ($total > 0) {
  141. if ($card['create_type'] == 2) {
  142. $cash = $price = $card['value'] * $total;
  143. $total = 1;
  144. $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
  145. if (!$w['sku_id']) {
  146. continue;
  147. }
  148. } else {
  149. if ($card['create_type'] == 3 && isset($v['price']) && $v['price'] > 0) {
  150. $card['value'] = $v['price'];
  151. }
  152. $cash = $price = $card['value'];
  153. for($i=0; $i<$total;$i++) {
  154. $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
  155. if ($w['sku_id']) {
  156. $w['total_num'] = 1;
  157. $w['cash'] = $cash;
  158. $w['price'] = $price;
  159. $w['name'] = $name;
  160. $state = Dever::db('agent/member_goods')->insert($w);
  161. if ($state) {
  162. Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], 1, 0, 1, $name, $desc);
  163. }
  164. }
  165. }
  166. continue;
  167. }
  168. } else {
  169. continue;
  170. }
  171. }
  172. } elseif ($type == 3) {
  173. $prize = Dever::db('setting/prize')->find($w['type_id']);
  174. $price = $prize['price'];
  175. $cash = $prize['price'] * $total;
  176. $name = $prize['name'];
  177. }
  178. $info = Dever::db('agent/member_goods')->one($w);
  179. $w['total_num'] = $total;
  180. $w['cash'] = $cash;
  181. $w['price'] = $price;
  182. $w['name'] = $name;
  183. if (!$info) {
  184. $state = Dever::db('agent/member_goods')->insert($w);
  185. if ($state) {
  186. 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);
  187. }
  188. } else {
  189. $w['total_num'] += $info['sell_num'];
  190. $w['where_id'] = $info['id'];
  191. $w['state'] = 1;
  192. $state = Dever::db('agent/member_goods')->update($w);
  193. $num = $w['total_num']-$info['total_num'];
  194. if ($state) {
  195. 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);
  196. }
  197. }
  198. }
  199. }
  200. $link = Dever::input('link');
  201. if ($link) {
  202. return $link;
  203. }
  204. if ($search_api_mid) {
  205. return Dever::url('project/database/list&table=member_goods&project=agent&search_option_state=1&search_api_mid='.$search_api_mid, 'manage');
  206. } else {
  207. return Dever::url('project/database/list&table=member_goods&project=agent&search_option_state=1&search_option_mid='.$mid, 'manage');
  208. }
  209. }
  210. # 记录日志
  211. public function log($mid, $parent_order_num, $type, $type_id, $sku_id, $num, $pnum, $fnum, $name = '', $desc = '')
  212. {
  213. $data['mid'] = $mid;
  214. $data['parent_order_num'] = $parent_order_num;
  215. $data['type'] = $type;
  216. $data['type_id'] = $type_id;
  217. $data['sku_id'] = $sku_id;
  218. $data['num'] = $num;
  219. $data['pnum'] = $pnum;
  220. $data['fnum'] = $fnum;
  221. $data['desc'] = $desc;
  222. $data['name'] = $name;
  223. $data['order_num'] = $this->getOrderId();
  224. $admin = Dever::load('manage/auth.info');
  225. if ($admin) {
  226. $data['admin'] = $admin['id'];
  227. }
  228. $id = Dever::db('agent/member_goods_log')->insert($data);
  229. return $id;
  230. }
  231. /**
  232. * 生成日志订单号
  233. *
  234. * @return mixed
  235. */
  236. public function getOrderId()
  237. {
  238. $where['order_num'] = Dever::order('QY');
  239. $state = Dever::db('agent/member_goods_log')->one($where);
  240. if (!$state) {
  241. return $where['order_num'];
  242. } else {
  243. return $this->getOrderId();
  244. }
  245. }
  246. }