Manage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. namespace Goods\Lib;
  3. use Dever;
  4. class Manage
  5. {
  6. public function getByName_api()
  7. {
  8. $data = Dever::db('goods/info')->getAllByName(array
  9. (
  10. 'name' => Dever::input('term'))
  11. );
  12. if ($data) {
  13. return $data;
  14. }
  15. return array
  16. (
  17. 0 => array('id' => -1, 'value' => '没有找到您搜索的数据', 'label' => '没有找到您搜索的数据'),
  18. );
  19. }
  20. public function getByCate()
  21. {
  22. $cate = Dever::input('cate');
  23. $data = Dever::db('goods/info')->getAllByCate(array('top_category_id' => $cate));
  24. return Dever::load('manage/database')->getAjax($data);
  25. }
  26. public function search_api()
  27. {
  28. $where = array();
  29. $cate = Dever::input('cate');
  30. if ($cate) {
  31. $where['cate_id'] = $cate;
  32. }
  33. $price_type = Dever::input('price_type');
  34. if ($price_type) {
  35. $where['price_type'] = $price_type;
  36. }
  37. return Dever::search('goods/info', $where);
  38. }
  39. public function search_sku_api()
  40. {
  41. $where = array();
  42. $info_id = Dever::input('info_id');
  43. if ($info_id) {
  44. $where['info_id'] = $info_id;
  45. }
  46. $data = Dever::search('goods/info_sku', $where);
  47. if ($data) {
  48. foreach ($data as $k => $v) {
  49. $v['attr'] = Dever::json_decode($v['attr']);
  50. if ($v['attr']) {
  51. $v['attr'] = Dever::load('attr/api')->getInfoByJson($v['attr']);
  52. $v['name'] = $v['attr']['string'];
  53. }
  54. }
  55. }
  56. return $data;
  57. }
  58. /**
  59. * 验证是否可以上架
  60. *
  61. * @return mixed
  62. */
  63. public function statusUpdate($id, $name, $data)
  64. {
  65. $status = Dever::param('status', $data);
  66. if ($status == 1) {
  67. $id = $id['where_id'];
  68. $info = Dever::db('goods/info')->find($id);
  69. if (!$info) {
  70. Dever::alert('商品不存在');
  71. }
  72. if (!$info['tax']) {
  73. Dever::alert('商品税点未设置');
  74. }
  75. if (!$info['cover'] || !$info['pic']) {
  76. Dever::alert('商品图未设置');
  77. }
  78. $sku = Dever::db('goods/info_sku')->select(array('info_id' => $info['id']));
  79. if (!$sku) {
  80. Dever::alert('商品规格未设置');
  81. }
  82. foreach ($sku as $k => $v) {
  83. if (!$v['s_price']) {
  84. Dever::alert('市场价未设置');
  85. }
  86. if (!$v['f_price']) {
  87. Dever::alert('门店采购价未设置');
  88. }
  89. if (!$v['c_price']) {
  90. Dever::alert('成本价未设置');
  91. }
  92. if (!$v['price']) {
  93. Dever::alert('销售价未设置');
  94. }
  95. if ($v['min'] < 1) {
  96. Dever::alert('采购起订数未设置');
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * 更新信息
  103. *
  104. * @return mixed
  105. */
  106. public function infoUpdate($id, $name, $data)
  107. {
  108. $status = Dever::param('status', $data);
  109. if ($status > 0) {
  110. Dever::db('shop/goods')->updates(array('option_goods_id' => $id, 'set_status' => $status));
  111. Dever::db('store/goods')->updates(array('option_goods_id' => $id, 'set_status' => $status));
  112. Dever::db('factory/goods')->updates(array('option_goods_id' => $id, 'set_status' => $status));
  113. Dever::db('shop/goods_sku')->updates(array('option_goods_id' => $id, 'set_status' => $status));
  114. Dever::db('store/goods_sku')->updates(array('option_goods_id' => $id, 'set_status' => $status));
  115. }
  116. # 更新分类id
  117. $category = Dever::param('category', $data);
  118. if ($category) {
  119. if (is_array($category)) {
  120. $category_id = end($category);
  121. $top_category_id = $category[0];
  122. if (isset($category[1])) {
  123. $second_category_id = $category[1];
  124. } else {
  125. $second_category_id = $category[0];
  126. }
  127. } else {
  128. $category_id = $category;
  129. $top_category_id = $category;
  130. $second_category_id = $category;
  131. }
  132. $update['top_category_id'] = $top_category_id;
  133. $update['second_category_id'] = $second_category_id;
  134. $update['category_id'] = $category_id;
  135. }
  136. $pic = Dever::param('pic', $data);
  137. $pic_cover = Dever::param('pic_cover', $data);
  138. if ($pic && !$pic_cover) {
  139. if (is_string($pic)) {
  140. $pic = explode(',', $pic);
  141. }
  142. $update['pic_cover'] = $pic[0];
  143. }
  144. if (isset($update)) {
  145. $update['where_id'] = $id;
  146. Dever::db('goods/info')->update($update);
  147. }
  148. }
  149. /**
  150. * 更新需求属性信息
  151. *
  152. * @return mixed
  153. */
  154. public function attrUpdate($id, $name, $data)
  155. {
  156. print_r($data);die;
  157. foreach ($data as $k => $v) {
  158. }
  159. }
  160. public function column($cate, $im = ',')
  161. {
  162. if ($cate) {
  163. $cate = explode(',', $cate);
  164. $result = array();
  165. foreach ($cate as $k => $v) {
  166. $info = Dever::db('goods/column')->one($v);
  167. $result[$k] = $info['name'];
  168. }
  169. $result = implode($im, $result);
  170. return $result;
  171. }
  172. return '';
  173. }
  174. /**
  175. * 显示信息
  176. *
  177. * @return mixed
  178. */
  179. public function info($id)
  180. {
  181. $info = Dever::db('goods/info')->one($id);
  182. if ($info) {
  183. $table['名称'] = $info['name'];
  184. $table['编号'] = $info['id'];
  185. $table['税点'] = $info['tax'];
  186. $table['栏目'] = $this->column($info['column_id']);
  187. //$table['标题'] = $info['name'];
  188. $table['分类'] = Dever::load("category/api.string", $info['category']);
  189. if ($info['cover']) {
  190. $table['商品图'] = '<img src="'.$info['cover'].'" width="150" />';
  191. }
  192. if ($info['price_type'] == 3) {
  193. # 获取套餐的商品
  194. $goods = Dever::array_decode($info['goods']);
  195. foreach ($goods as $k => $v) {
  196. $goods_info = Dever::db('goods/info')->one($v['goods_id']);
  197. if ($goods_info) {
  198. $table['套餐商品'][] = $v['num'] . '个' . $goods_info['name'];
  199. }
  200. }
  201. $table['套餐商品'] = implode(',', $table['套餐商品']);
  202. } elseif ($info['price_type'] == 4) {
  203. # 获取套餐的商品
  204. $goods = Dever::array_decode($info['goods']);
  205. foreach ($goods as $k => $v) {
  206. $goods_info = Dever::db('goods/info')->one($v['goods_id']);
  207. if ($goods_info) {
  208. $table['组合商品'][] = $v['num'] . '个' . $goods_info['name'];
  209. }
  210. }
  211. $table['组合商品'] = implode(',', $table['组合商品']);
  212. }
  213. $attr = $this->attrInfo($info);
  214. if ($attr) {
  215. $table['属性'] = $attr;
  216. }
  217. $table['价格'] = Dever::load('goods/lib/sku')->table($info['id'], false, false, false, false);
  218. if ($info['udate']) {
  219. $table['更新时间'] = date("Y-m-d H:i:s", $info['udate']);
  220. }
  221. if ($info['cdate']) {
  222. $table['发布时间'] = date("Y-m-d H:i:s", $info['cdate']);
  223. }
  224. }
  225. return Dever::table($table);
  226. }
  227. private function attrInfo($info)
  228. {
  229. $table = array();
  230. $class = Info::init($info['top_category_id']);
  231. if ($info) {
  232. $info = $class->info($info);
  233. if ($info && isset($info['attr'])) {
  234. foreach ($info['attr'] as $k => $v) {
  235. if ($v['value_string']) {
  236. $table[$v['name']] = $v['value_string'];
  237. }
  238. }
  239. }
  240. }
  241. return $table ? Dever::table($table) : false;
  242. }
  243. public function area($id, $name, $data)
  244. {
  245. # 不再执行syncone等后续操作
  246. Dever::config('base')->after = 1;
  247. $type = Dever::input('type');
  248. $area = Dever::param('area', $data);
  249. if ($area) {
  250. if (isset($area[2])) {
  251. $update['district_id'] = $area[2];
  252. }
  253. if (isset($area[1])) {
  254. $update['city_id'] = $area[1];
  255. }
  256. if (isset($area[0])) {
  257. $update['province_id'] = $area[0];
  258. }
  259. if (isset($update)) {
  260. $update['where_id'] = $id;
  261. Dever::db($type . '/info')->update($update);
  262. }
  263. }
  264. }
  265. /**
  266. * 显示用户信息
  267. *
  268. * @return mixed
  269. */
  270. public function showUserInfo($id)
  271. {
  272. $info = Dever::db('goods/card_code')->find($id);
  273. $table = array();
  274. if ($info && $info['status'] > 1 && $info['uid'] && $info['uid'] > 0) {
  275. $user = Dever::load('passport/user-one', $info['uid']);
  276. $shop = Dever::load('shop/info-one', $info['shop_id']);
  277. if ($user) {
  278. $table['领取门店'] = $shop['name'] . '('.$shop['mobile'].')';
  279. $table['领取人'] = $user['username'] . '('.$info['uid'].')';
  280. $table['手机号'] = $user['mobile'];
  281. $table['兑换时间'] = date('Y-m-d H:i:s', $info['ddate']);
  282. }
  283. }
  284. return Dever::table($table);
  285. }
  286. /**
  287. * 创建兑换码
  288. *
  289. * @return mixed
  290. */
  291. public function createCard($id, $name, $param)
  292. {
  293. $num = Dever::param('num', $param);
  294. $card_id = Dever::param('card_id', $param);
  295. $info = Dever::db('goods/card')->find($card_id);
  296. if ($info && $num > 0) {
  297. for ($i = 0; $i< $num;$i++) {
  298. $this->createCode($info);
  299. }
  300. }
  301. }
  302. private function createCode($info)
  303. {
  304. # 生成卡号和密码
  305. $card = $info['card_prefix'] . Dever::rand($info['card_len'], $info['card_type'] - 1);
  306. $pwd = $info['pwd_prefix'] . Dever::rand($info['pwd_len'], $info['pwd_type'] - 1);
  307. $where['card'] = $card;
  308. $where['pwd'] = $pwd;
  309. $state = Dever::db('goods/card_code')->find($where);
  310. if (!$state) {
  311. $where['card_id'] = $info['id'];
  312. if ($info['type'] == 1) {
  313. $info['day'] = 3650;
  314. }
  315. $where['edate'] = time() + ($info['day']*86400);
  316. Dever::db('goods/card_code')->insert($where);
  317. } else {
  318. $this->createCode($info);
  319. }
  320. }
  321. /**
  322. * 作废
  323. *
  324. * @return mixed
  325. */
  326. public function drop_api($id)
  327. {
  328. $update['where_id'] = $id;
  329. $update['type'] = 4;
  330. Dever::db('goods/card_code')->update($update);
  331. return 'ok';
  332. }
  333. public function recovery_api($id)
  334. {
  335. $update['where_id'] = $id;
  336. $update['type'] = 1;
  337. Dever::db('goods/card_code')->update($update);
  338. return 'ok';
  339. }
  340. public function editor_api()
  341. {
  342. $data['name'] = '商品';
  343. $where = array();
  344. $name = Dever::input('search_name');
  345. $cate = Dever::input('search_cate');
  346. if ($name) {
  347. $where['name'] = $name;
  348. }
  349. if ($cate > 0) {
  350. $where['column_id'] = $cate;
  351. }
  352. $data['file'] = Dever::db('goods/info')->getPageAll($where);
  353. $data['cate'] = Dever::db('goods/column')->select();
  354. if ($data['file']) {
  355. foreach ($data['file'] as $k => $v) {
  356. $data['file'][$k]['source_name'] = $v['name'];
  357. $data['file'][$k]['file'] = $v['cover'];
  358. $data['file'][$k]['pic'] = $v['cover'];
  359. }
  360. }
  361. return $data;
  362. }
  363. }