Manage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. namespace Goods\Lib;
  3. use Dever;
  4. class Manage
  5. {
  6. public function search_api()
  7. {
  8. $table = 'goods/info';
  9. $keyword = Dever::input('keyword');
  10. $yes = Dever::input('yes');
  11. $where = array();
  12. $cate = Dever::input('cate');
  13. if ($cate) {
  14. $where['cate_id'] = $cate;
  15. }
  16. if ($yes) {
  17. $yes = Dever::db($table)->search(array('ids' => $yes));
  18. }
  19. if (!$keyword) {
  20. $where['limit'] = '0,50';
  21. $data = Dever::db($table)->search($where);
  22. } else {
  23. $where['name'] = $keyword;
  24. $data = Dever::db($table)->search($where);
  25. }
  26. $result = array();
  27. if ($yes) {
  28. foreach ($yes as $k => $v) {
  29. if (isset($data[$k])) {
  30. unset($data[$k]);
  31. }
  32. $yes[$k]['selected'] = 'selected';
  33. }
  34. $data = $yes + $data;
  35. $data = array_merge($data, array());
  36. } else {
  37. $data = array_merge($data, array());
  38. }
  39. if (!$data) {
  40. Dever::alert('暂无数据');
  41. }
  42. return $data;
  43. }
  44. /**
  45. * 更新信息
  46. *
  47. * @return mixed
  48. */
  49. public function infoUpdate($id, $name, $data)
  50. {
  51. # 更新分类id
  52. $category = Dever::param('category', $data);
  53. if ($category) {
  54. if (is_array($category)) {
  55. $category_id = end($category);
  56. $top_category_id = $category[0];
  57. if (isset($category[1])) {
  58. $second_category_id = $category[1];
  59. } else {
  60. $second_category_id = $category[0];
  61. }
  62. } else {
  63. $category_id = $category;
  64. $top_category_id = $category;
  65. $second_category_id = $category;
  66. }
  67. $update['top_category_id'] = $top_category_id;
  68. $update['second_category_id'] = $second_category_id;
  69. $update['category_id'] = $category_id;
  70. }
  71. $pic = Dever::param('pic', $data);
  72. $pic_cover = Dever::param('pic_cover', $data);
  73. if ($pic && !$pic_cover) {
  74. if (is_string($pic)) {
  75. $pic = explode(',', $pic);
  76. }
  77. $update['pic_cover'] = $pic[0];
  78. }
  79. if (isset($update)) {
  80. $update['where_id'] = $id;
  81. Dever::db('goods/info')->update($update);
  82. }
  83. }
  84. /**
  85. * 更新需求属性信息
  86. *
  87. * @return mixed
  88. */
  89. public function attrUpdate($id, $name, $data)
  90. {
  91. print_r($data);die;
  92. foreach ($data as $k => $v) {
  93. }
  94. }
  95. /**
  96. * 显示信息
  97. *
  98. * @return mixed
  99. */
  100. public function info($id)
  101. {
  102. $info = Dever::db('goods/info')->one($id);
  103. if ($info) {
  104. $table['编号'] = $info['id'];
  105. $table['标题'] = $info['name'];
  106. $table['分类'] = Dever::load("category/api.string", $info['category']);
  107. if ($info['pic']) {
  108. $table['商品图'] = '<img src="'.$info['pic'].'" width="150" />';
  109. }
  110. //$table['属性'] = $this->attrInfo($info);
  111. if ($info['cdate']) {
  112. $table['发布时间'] = date("Y-m-d H:i:s", $info['cdate']);
  113. }
  114. if ($info['udate']) {
  115. $table['更新时间'] = date("Y-m-d H:i:s", $info['udate']);
  116. }
  117. }
  118. return Dever::table($table);
  119. }
  120. private function attrInfo($info)
  121. {
  122. $table = array();
  123. $info = Info::init($info['top_category_id']);
  124. if ($info) {
  125. $info = $info->info($info, 'id');
  126. if ($info && isset($info['attr'])) {
  127. foreach ($info['attr'] as $k => $v) {
  128. $table[$v['name']] = $v['value_string'];
  129. }
  130. }
  131. }
  132. return Dever::table($table);
  133. }
  134. public function area($id, $name, $data)
  135. {
  136. # 不再执行syncone等后续操作
  137. Dever::config('base')->after = 1;
  138. $type = Dever::input('type');
  139. $area = Dever::param('area', $data);
  140. if ($area) {
  141. if (isset($area[2])) {
  142. $update['district_id'] = $area[2];
  143. }
  144. if (isset($area[1])) {
  145. $update['city_id'] = $area[1];
  146. }
  147. if (isset($area[0])) {
  148. $update['province_id'] = $area[0];
  149. }
  150. if (isset($update)) {
  151. $update['where_id'] = $id;
  152. Dever::db($type . '/info')->update($update);
  153. }
  154. }
  155. }
  156. public function skuConfig()
  157. {
  158. $data['action'] = Dever::url('goods/lib/manage.skuUp');
  159. $data['info_id'] = Dever::input('search_option_info_id');
  160. return $data;
  161. }
  162. public function skuUp_api()
  163. {
  164. $info_id = Dever::input('info_id');
  165. $attr = Dever::input('attr');
  166. $key = Dever::input('key');
  167. $price = Dever::input('price');
  168. $s_price = Dever::input('s_price');
  169. $f_price = Dever::input('f_price');
  170. $num = Dever::input('num');
  171. if ($key) {
  172. foreach ($key as $k => $v) {
  173. if (isset($price[$k]) && $price[$k]) {
  174. $info = Dever::db('goods/info_sku')->one(array('key' => $v));
  175. $data['info_id'] = $info_id;
  176. $data['attr'] = $attr[$k];
  177. $data['key'] = $v;
  178. $data['price'] = $price[$k];
  179. $data['s_price'] = $s_price[$k];
  180. $data['f_price'] = $f_price[$k];
  181. $data['num'] = $num[$k];
  182. if (!$info) {
  183. Dever::db('goods/info_sku')->insert($data);
  184. } else {
  185. $data['where_id'] = $info['id'];
  186. Dever::db('goods/info_sku')->update($data);
  187. }
  188. }
  189. }
  190. }
  191. Dever::out('yes');
  192. }
  193. public function skuInput()
  194. {
  195. $id = Dever::input('search_option_info_id');
  196. $sku = Dever::db('goods/info_sku')->getData(array('info_id' => $id));
  197. $info = Dever::db('goods/info')->one($id);
  198. $info = Info::init(-1)->info($info, 'list_reorder');
  199. $html = '请先选择属性';
  200. if ($info['sell_attr']) {
  201. $head = '<thead><tr>';
  202. $body = '<tbody>';
  203. $option = array();
  204. foreach ($info['sell_attr'] as $k => $v) {
  205. $head .= '<th>'.$v['name'].'</th>';
  206. if (isset($v['option_sku'])) {
  207. $option[$k] = $v['option_sku'];
  208. }
  209. }
  210. $head .= '<th>销售价</th>';
  211. $head .= '<th>市场价</th>';
  212. $head .= '<th>进货价</th>';
  213. //$head .= '<th>库存</th>';
  214. $head .= '</tr></thead>';
  215. if ($option) {
  216. $option = Dever::cartesian($option);
  217. foreach ($option as $k => $v) {
  218. $body .= '<tr data-row="' .($k+1). '">';
  219. $key = $id = array();
  220. foreach ($v['name'] as $k1 => $v1) {
  221. $rows = 1;
  222. $body .= '<td width="50" rowspan="'.$rows.'">'.$v1.'</td>';
  223. $id[] = array
  224. (
  225. 'id' => $v['id'][$k1],
  226. 'attr_id' => $v['info_id'][$k1],
  227. );
  228. $key[] = $v['info_id'][$k1] . '-' . $v['id'][$k1];
  229. }
  230. $key = implode('_', $key);
  231. $f_price = $s_price = $price = $num = '';
  232. if (isset($sku[$key])) {
  233. $f_price = $sku[$key]['f_price'];
  234. $s_price = $sku[$key]['s_price'];
  235. $price = $sku[$key]['price'];
  236. //$num = $sku[$key]['num'];
  237. }
  238. $body .= '<td width="30"><input type="text" class="layui-input" name="price['.$k.']" value="'.$price.'"/></td>';
  239. $body .= '<td width="30"><input type="text" class="layui-input" name="s_price['.$k.']" value="'.$s_price.'"/></td>';
  240. $body .= '<td width="30"><input type="text" class="layui-input" name="f_price['.$k.']" value="'.$f_price.'"/></td>';
  241. //$body .= '<td width="30"><input type="text" class="layui-input" name="num['.$k.']" value="'.$num.'"/></td>';
  242. $body .= '<input type="hidden" name="key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="attr['.$k.']"/>'.json_encode($id).'</textarea>';
  243. $body .= '</tr>';
  244. }
  245. }
  246. $body .= '</tbody>';
  247. $html = $head . $body;
  248. }
  249. return $html;
  250. }
  251. /**
  252. * 显示用户信息
  253. *
  254. * @return mixed
  255. */
  256. public function showUserInfo($id)
  257. {
  258. $info = Dever::db('goods/card_code')->find($id);
  259. $table = array();
  260. if ($info && $info['status'] > 1 && $info['uid'] && $info['uid'] > 0) {
  261. $user = Dever::load('passport/user-one', $info['uid']);
  262. $shop = Dever::load('shop/info-one', $info['shop_id']);
  263. if ($user) {
  264. $table['领取门店'] = $shop['name'] . '('.$shop['mobile'].')';
  265. $table['领取人'] = $user['username'] . '('.$info['uid'].')';
  266. $table['手机号'] = $user['mobile'];
  267. $table['兑换时间'] = date('Y-m-d H:i:s', $info['ddate']);
  268. }
  269. }
  270. return Dever::table($table);
  271. }
  272. /**
  273. * 创建兑换码
  274. *
  275. * @return mixed
  276. */
  277. public function createCard($id, $name, $param)
  278. {
  279. $num = Dever::param('num', $param);
  280. $card_id = Dever::param('card_id', $param);
  281. if ($num > 0) {
  282. for ($i = 0; $i< $num;$i++) {
  283. $this->createCode($card_id);
  284. }
  285. }
  286. }
  287. private function createCode($card_id)
  288. {
  289. # 生成卡号和密码
  290. $card = 'CR' . Dever::rand(14);
  291. $pwd = Dever::rand(8);
  292. $where['card'] = $card;
  293. $where['pwd'] = $pwd;
  294. $info = Dever::db('goods/card_code')->find($where);
  295. if (!$info) {
  296. $where['card_id'] = $card_id;
  297. Dever::db('goods/card_code')->insert($where);
  298. } else {
  299. $this->createCode($card_id);
  300. }
  301. }
  302. /**
  303. * 作废
  304. *
  305. * @return mixed
  306. */
  307. public function drop_api($id)
  308. {
  309. $update['where_id'] = $id;
  310. $update['type'] = 4;
  311. Dever::db('goods/card_code')->update($update);
  312. return 'ok';
  313. }
  314. public function recovery_api($id)
  315. {
  316. $update['where_id'] = $id;
  317. $update['type'] = 1;
  318. Dever::db('goods/card_code')->update($update);
  319. return 'ok';
  320. }
  321. }