Member_set.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. $w['clear'] = true;
  67. if ($v['del'] == 2) {
  68. $info = Dever::db('agent/member_goods')->one($w);
  69. if ($info) {
  70. Dever::db('agent/member_goods')->update(array('where_id' => $info['id'], 'state' => 2, 'total_num' => 0));
  71. $name = $info['name'] ? $info['name'] : Dever::load("agent/lib/manage.getGoods#name", $info['type'], $info['type_id'], $info['sku_id']);
  72. 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 . '[删除]');
  73. }
  74. } else {
  75. $total = $v['total'];
  76. $cash = $price = 0;
  77. if ($type == 1) {
  78. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($w['type_id'], $w['sku_id']);
  79. $price = $goods_info['price'];
  80. $cash = $goods_info['price'] * $total;
  81. if (isset($goods_info['sku'])) {
  82. $sku = '-' . $goods_info['sku']['string'];
  83. } else {
  84. $sku = '';
  85. }
  86. $name = $goods_info['name'] . $sku;
  87. } elseif ($type == 2) {
  88. # 体验卡
  89. $card = Dever::db('card/info')->find($w['type_id']);
  90. if (!$card) {
  91. continue;
  92. }
  93. $name = $card['name'];
  94. if ($sku_id == -1) {
  95. if ($card['create_type'] == 2) {
  96. $cash = $price = $card['value'] * $total;
  97. $total = 1;
  98. $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
  99. if (!$w['sku_id']) {
  100. continue;
  101. }
  102. } else {
  103. if ($card['create_type'] == 3 && isset($v['price']) && $v['price'] > 0) {
  104. $card['value'] = $v['price'];
  105. }
  106. $cash = $price = $card['value'];
  107. for($i=0; $i<$total;$i++) {
  108. $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
  109. if ($w['sku_id']) {
  110. $w['total_num'] = 1;
  111. $w['cash'] = $cash;
  112. $w['price'] = $price;
  113. $w['name'] = $name;
  114. $state = Dever::db('agent/member_goods')->insert($w);
  115. if ($state) {
  116. Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], 1, 0, 1, $name, $desc);
  117. }
  118. }
  119. }
  120. continue;
  121. }
  122. } else {
  123. $sku = explode('i', $sku_id);
  124. if ($sku) {
  125. foreach ($sku as $k1 => $v1) {
  126. $w['sku_id'] = $v1;
  127. $info = Dever::db('agent/member_goods')->one($w);
  128. if ($info) {
  129. $cur = ($info['total_num']-$info['sell_num']);
  130. if ($cur <= 0) {
  131. unset($sku[$k1]);
  132. }
  133. $u = array();
  134. $u['where_id'] = $info['id'];
  135. $u['state'] = 1;
  136. $state = Dever::db('agent/member_goods')->update($u);
  137. }
  138. }
  139. }
  140. $total = $v['total'] - count($sku);
  141. if ($total > 0) {
  142. if ($card['create_type'] == 2) {
  143. $cash = $price = $card['value'] * $total;
  144. $total = 1;
  145. $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
  146. if (!$w['sku_id']) {
  147. continue;
  148. }
  149. } else {
  150. if ($card['create_type'] == 3 && isset($v['price']) && $v['price'] > 0) {
  151. $card['value'] = $v['price'];
  152. }
  153. $cash = $price = $card['value'];
  154. for($i=0; $i<$total;$i++) {
  155. $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
  156. if ($w['sku_id']) {
  157. $w['total_num'] = 1;
  158. $w['cash'] = $cash;
  159. $w['price'] = $price;
  160. $w['name'] = $name;
  161. $state = Dever::db('agent/member_goods')->insert($w);
  162. if ($state) {
  163. Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], 1, 0, 1, $name, $desc);
  164. }
  165. }
  166. }
  167. continue;
  168. }
  169. } else {
  170. continue;
  171. }
  172. }
  173. } elseif ($type == 3) {
  174. $prize = Dever::db('setting/prize')->find($w['type_id']);
  175. $price = $prize['price'];
  176. $cash = $prize['price'] * $total;
  177. $name = $prize['name'];
  178. }
  179. $info = Dever::db('agent/member_goods')->one($w);
  180. $w['total_num'] = $total;
  181. $w['cash'] = $cash;
  182. $w['price'] = $price;
  183. $w['name'] = $name;
  184. if (!$info) {
  185. $state = Dever::db('agent/member_goods')->insert($w);
  186. if ($state) {
  187. 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);
  188. }
  189. } else {
  190. $w['total_num'] += $info['sell_num'];
  191. $w['where_id'] = $info['id'];
  192. $w['state'] = 1;
  193. $state = Dever::db('agent/member_goods')->update($w);
  194. $num = $w['total_num']-$info['total_num'];
  195. if ($state) {
  196. 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);
  197. }
  198. }
  199. }
  200. }
  201. $link = Dever::input('link');
  202. if ($link) {
  203. return $link;
  204. }
  205. if ($search_api_mid) {
  206. return Dever::url('project/database/list&table=member_goods&project=agent&search_option_state=1&search_api_mid='.$search_api_mid, 'manage');
  207. } else {
  208. return Dever::url('project/database/list&table=member_goods&project=agent&search_option_state=1&search_option_mid='.$mid, 'manage');
  209. }
  210. }
  211. # 记录日志
  212. public function log($mid, $parent_order_num, $type, $type_id, $sku_id, $num, $pnum, $fnum, $name = '', $desc = '')
  213. {
  214. $data['mid'] = $mid;
  215. $data['parent_order_num'] = $parent_order_num;
  216. $data['type'] = $type;
  217. $data['type_id'] = $type_id;
  218. $data['sku_id'] = $sku_id;
  219. $data['num'] = $num;
  220. $data['pnum'] = $pnum;
  221. $data['fnum'] = $fnum;
  222. $data['desc'] = $desc;
  223. $data['name'] = $name;
  224. $data['order_num'] = $this->getOrderId();
  225. $admin = Dever::load('manage/auth.info');
  226. if ($admin) {
  227. $data['admin'] = $admin['id'];
  228. }
  229. $id = Dever::db('agent/member_goods_log')->insert($data);
  230. return $id;
  231. }
  232. /**
  233. * 生成日志订单号
  234. *
  235. * @return mixed
  236. */
  237. public function getOrderId()
  238. {
  239. $where['order_num'] = Dever::order('QY');
  240. $state = Dever::db('agent/member_goods_log')->one($where);
  241. if (!$state) {
  242. return $where['order_num'];
  243. } else {
  244. return $this->getOrderId();
  245. }
  246. }
  247. }