Member_set.php 11 KB

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