ChainGoods.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /*
  3. * 门店管理中心
  4. */
  5. namespace app\api\controller;
  6. use think\facade\Lang;
  7. use think\facade\Db;
  8. class ChainGoods extends MobileChain {
  9. public function initialize() {
  10. parent::initialize();
  11. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/chain.lang.php');
  12. }
  13. /**
  14. * @api {POST} api/ChainGoods/goods_list 出售中的商品列表
  15. * @apiVersion 1.0.0
  16. * @apiGroup ChainGoods
  17. *
  18. * @apiHeader {String} X-DS-KEY 门店授权token
  19. *
  20. * @apiParam {String} keyword 关键词
  21. * @apiParam {String} goods_type 商品类型 lockup违规的商品 offline仓库的商品 waitverify等待审核的商品
  22. * @apiParam {Int} search_type 0商品名 1货号 2商品公共ID
  23. * @apiParam {String} page 页码
  24. * @apiParam {String} pagesize 每页显示数量
  25. *
  26. * @apiSuccess {String} code 返回码,10000为成功
  27. * @apiSuccess {String} message 返回消息
  28. * @apiSuccess {Object} result 返回数据
  29. * @apiSuccess {Object[]} result.goods_list 商品列表
  30. * @apiSuccess {String} result.goods_list.goods_addtime 添加时间
  31. * @apiSuccess {String} result.goods_list.goods_commonid 商品公共ID
  32. * @apiSuccess {String} result.goods_list.goods_image 商品图片
  33. * @apiSuccess {String} result.goods_list.goods_lock 商品锁定 0未锁,1已锁
  34. * @apiSuccess {String} result.goods_list.goods_name 商品名称
  35. * @apiSuccess {String} result.goods_list.goods_price 商品价格
  36. * @apiSuccess {String} result.goods_list.goods_state 商品状态 0:下架 1:正常 10:违规(禁售)
  37. * @apiSuccess {String} result.goods_list.goods_storage_sum 商品库存
  38. * @apiSuccess {Int} result.page_total 总页数
  39. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  40. */
  41. public function goods_list() {
  42. $keyword = input('post.keyword');
  43. $goods_type = input('post.goods_type');
  44. $search_type = input('post.search_type');
  45. $goods_model = model('goods');
  46. $condition = array();
  47. $condition[] = array('store_id', '=', $this->chain_info['store_id']);
  48. if (trim($keyword) != '') {
  49. switch ($search_type) {
  50. case 0:
  51. $condition[] = array('goods_name', 'like', '%' . trim($keyword) . '%');
  52. break;
  53. case 1:
  54. $condition[] = array('goods_serial', 'like', '%' . trim($keyword) . '%');
  55. break;
  56. case 2:
  57. $condition[] = array('goods_commonid', '=', intval($keyword));
  58. break;
  59. }
  60. }
  61. $fields = 'gc_id,goods_commonid,goods_name,goods_price,goods_addtime,goods_image,goods_state,goods_lock,store_id';
  62. switch ($goods_type) {
  63. // 违规的商品
  64. case 'lockup':
  65. $goods_list = $goods_model->getGoodsCommonLockUpList($condition, $fields, $this->pagesize);
  66. break;
  67. //仓库的商品
  68. case 'offline':
  69. $goods_list = $goods_model->getGoodsCommonOfflineList($condition, $fields, $this->pagesize);
  70. break;
  71. //等待审核的商品
  72. case 'waitverify':
  73. $goods_list = $goods_model->getGoodsCommonWaitVerifyList($condition);
  74. break;
  75. default:
  76. $goods_list = $goods_model->getGoodsCommonOnlineList($condition, $fields, $this->pagesize);
  77. break;
  78. }
  79. // 整理输出的数据格式
  80. foreach ($goods_list as $key => $value) {
  81. $condition = array();
  82. $condition[] = array('store_id', '=', $value['store_id']);
  83. $condition[] = array('goods_commonid', '=', $value['goods_commonid']);
  84. $condition[] = array('chain_id', '=', $this->chain_info['chain_id']);
  85. $storage_count = Db::name('chain_goods')->where($condition)->sum('goods_storage');
  86. $goods_list[$key]['goods_storage_sum'] = $storage_count;
  87. $condition = array();
  88. $condition[] = array('store_id', '=', $value['store_id']);
  89. $condition[] = array('goods_commonid', '=', $value['goods_commonid']);
  90. $goods_info = Db::name('goods')->where($condition)->fieldRaw('count(*) as count,goods_id')->find();
  91. if ($goods_info) {
  92. $goods_list[$key]['goods_count'] = $goods_info['count'];
  93. $goods_list[$key]['goods_id'] = $goods_info['goods_id'];
  94. } else {
  95. unset($goods_list[$key]);
  96. continue;
  97. }
  98. $goods_list[$key]['goods_addtime'] = date('Y-m-d', $goods_list[$key]['goods_addtime']);
  99. $goods_list[$key]['goods_image'] = goods_cthumb($goods_list[$key]['goods_image']);
  100. }
  101. $result = array_merge(array('goods_list' => $goods_list), mobile_page($goods_model->page_info));
  102. ds_json_encode(10000, '', $result);
  103. }
  104. /**
  105. * @api {POST} api/ChainGoods/get_common_data 获取新增/编辑商品的公共数据
  106. * @apiVersion 1.0.0
  107. * @apiGroup ChainGoods
  108. *
  109. * @apiHeader {String} X-DS-KEY 门店授权token
  110. *
  111. * @apiParam {Int} class_id 商品分类ID
  112. *
  113. * @apiSuccess {String} code 返回码,10000为成功
  114. * @apiSuccess {String} message 返回消息
  115. * @apiSuccess {Object} result 返回数据
  116. * @apiSuccess {Object} result.attr_list 属性列表,键为属性名id
  117. * @apiSuccess {String} result.attr_list.attr_name 属性名
  118. * @apiSuccess {Object[]} result.attr_list.value 属性值列表
  119. * @apiSuccess {Int} result.attr_list.value.attrvalue_id 属性值id
  120. * @apiSuccess {String} result.attr_list.value.attrvalue_name 属性值名称
  121. * @apiSuccess {Object[]} result.brand_list 品牌列表(返回字段参考brand表)
  122. * @apiSuccess {Object} result.goods_class 商品分类
  123. * @apiSuccess {Int} result.goods_class.gc_id 商品分类ID
  124. * @apiSuccess {Int} result.goods_class.gc_id_1 一级商品分类ID
  125. * @apiSuccess {Int} result.goods_class.gc_id_2 二级商品分类ID
  126. * @apiSuccess {Int} result.goods_class.gc_id_3 三级商品分类ID
  127. * @apiSuccess {Int} result.goods_class.gc_virtual 是否允许发布虚拟商品(0否1是)
  128. * @apiSuccess {String} result.goods_class.gctag_name 商品分类名称
  129. * @apiSuccess {String} result.goods_class.gctag_value 商品分类标签
  130. * @apiSuccess {Int} result.goods_class.type_id 类型ID
  131. * @apiSuccess {Object} result.sign_i 规格数量
  132. * @apiSuccess {Object} result.spec_json 规格值列表(键为规格名id,值中的键为规格值id)
  133. * @apiSuccess {String} result.spec_json.spvalue_color 规格值颜色
  134. * @apiSuccess {String} result.spec_json.spvalue_name 规格值
  135. * @apiSuccess {Object} result.spec_list 按规格名分类的规格列表(键为规格名id)
  136. * @apiSuccess {String} result.spec_list.sp_name 规格名
  137. * @apiSuccess {Object[]} result.spec_list.value 规格值列表
  138. * @apiSuccess {String} result.spec_list.value .spvalue_color 规格值颜色
  139. * @apiSuccess {String} result.spec_list.value .spvalue_name 规格值
  140. * @apiSuccess {Int} result.spec_list.value .spvalue_name 规格值id
  141. */
  142. public function get_common_data() {
  143. $result = array();
  144. $class_id = intval(input('param.class_id'));
  145. $goods_class = model('goodsclass')->getGoodsclassLineForTag($class_id);
  146. $type_model = model('type');
  147. // 获取类型相关数据
  148. $typeinfo = $type_model->getAttribute($goods_class['type_id'], $this->chain_info['store_id'], $class_id);
  149. list($spec_json, $spec_list, $attr_list, $brand_list) = $typeinfo;
  150. $result['spec_json'] = $spec_json;
  151. $result['sign_i'] = count($spec_list);
  152. $result['spec_list'] = $spec_list;
  153. ds_json_encode(10000, '', $result);
  154. }
  155. /**
  156. * @api {POST} api/ChainGoods/edit_goods 获取商品信息
  157. * @apiVersion 1.0.0
  158. * @apiGroup ChainGoods
  159. *
  160. * @apiHeader {String} X-DS-KEY 门店授权token
  161. *
  162. * @apiParam {Int} class_id 商品分类ID
  163. *
  164. * @apiSuccess {String} code 返回码,10000为成功
  165. * @apiSuccess {String} message 返回消息
  166. * @apiSuccess {Object} result 返回数据
  167. * @apiSuccess {Object} result.area 商品地区信息(返回字段参考area表)
  168. * @apiSuccess {Int[]} result.attr_checked 选择的属性值id
  169. * @apiSuccess {Object} result.goods 商品信息(返回字段参考goodscommon表)
  170. * @apiSuccess {Int} result.goods.goods_storage 商品总库存
  171. * @apiSuccess {Object[]} result.goods.mb_body 商品详情
  172. * @apiSuccess {String} result.goods.mb_body.type 商品详情类型(image图片text文字)
  173. * @apiSuccess {String} result.goods.mb_body.value 商品详情值
  174. * @apiSuccess {String} result.goods.mb_body.value_url 商品详情图片链接
  175. * @apiSuccess {String} result.goods.goods_image_url 商品图片
  176. * @apiSuccess {Object} result.sp_value 规格值列表(键为规格值id升序排列的字符串)
  177. * @apiSuccess {Int} result.sp_value.alarm 库存预警值
  178. * @apiSuccess {Int} result.sp_value.color 颜色规格id
  179. * @apiSuccess {Int} result.sp_value.id 商品id
  180. * @apiSuccess {String} result.sp_value.label 规格名(用|分隔)
  181. * @apiSuccess {Float} result.sp_value.marketprice 市场价
  182. * @apiSuccess {Float} result.sp_value.price 价格
  183. * @apiSuccess {String} result.sp_value.sku 货号
  184. * @apiSuccess {Object} result.sp_value.sp_value 规格值列表(键为规格值id,值为规格值名称)
  185. * @apiSuccess {Int} result.sp_value.stock 库存
  186. */
  187. public function edit_goods() {
  188. $common_id = intval(input('param.commonid'));
  189. if ($common_id <= 0) {
  190. ds_json_encode(10001, lang('param_error'));
  191. }
  192. $goods_model = model('goods');
  193. $goodscommon_info = $goods_model->getGoodsCommonInfoByID($common_id);
  194. if (empty($goodscommon_info) || $goodscommon_info['store_id'] != $this->chain_info['store_id']) {
  195. ds_json_encode(10001, lang('goods_not_exist_or_lock'));
  196. }
  197. $result = array();
  198. $where = array('goods_commonid' => $common_id, 'store_id' => $this->chain_info['store_id']);
  199. $goodscommon_info['spec_name'] = unserialize($goodscommon_info['spec_name']);
  200. $goodscommon_info['goods_storage'] = 0;
  201. // 取得商品规格的输入值
  202. $goods_array = $goods_model->getGoodsList($where, 'goods_id,goods_marketprice,goods_price,goods_weight,goods_storage,goods_serial,goods_storage_alarm,goods_spec,color_id');
  203. $sp_value = array();
  204. if (is_array($goods_array) && !empty($goods_array)) {
  205. $spec_checked = array();
  206. foreach ($goods_array as $k => $v) {
  207. $condition = array();
  208. $condition[] = array('goods_id', '=', $v['goods_id']);
  209. $condition[] = array('chain_id', '=', $this->chain_info['chain_id']);
  210. $storage_count = Db::name('chain_goods')->where($condition)->sum('goods_storage');
  211. $goodscommon_info['goods_storage'] += $storage_count;
  212. $a = unserialize($v['goods_spec']);
  213. if (!empty($a)) {
  214. foreach ($a as $key => $val) {
  215. $spec_checked[$key]['id'] = $key;
  216. $spec_checked[$key]['name'] = addslashes($val);
  217. }
  218. $matchs = array_keys($a);
  219. sort($matchs);
  220. $id = str_replace(',', '', implode(',', $matchs));
  221. $sp_value[$id] = array(
  222. 'sp_value' => $a,
  223. 'label' => implode('|', array_values($a)),
  224. 'color' => $v['color_id'],
  225. 'marketprice' => $v['goods_marketprice'],
  226. 'price' => $v['goods_price'],
  227. 'id' => $v['goods_id'],
  228. 'goods_weight' => $v['goods_weight'],
  229. 'stock' => $storage_count,
  230. 'alarm' => $v['goods_storage_alarm'],
  231. 'sku' => $v['goods_serial'],
  232. );
  233. }
  234. }
  235. $result['spec_checked'] = $spec_checked;
  236. }
  237. if($goodscommon_info['spec_value']){
  238. $goodscommon_info['spec_value']= unserialize($goodscommon_info['spec_value']);
  239. }
  240. $result['goods'] = $goodscommon_info;
  241. $result['sp_value'] = $sp_value;
  242. ds_json_encode(10000, '', $result);
  243. }
  244. /**
  245. * @api {POST} api/ChainGoods/save_goods 保存商品信息
  246. * @apiVersion 1.0.0
  247. * @apiGroup Sellergoods
  248. *
  249. * @apiHeader {String} X-DS-KEY 门店授权token
  250. *
  251. * @apiParam {Object} spec 商品规格(键为规格值id升序排列的字符串)
  252. * @apiParam {Int} spec.goods_id 商品ID
  253. * @apiParam {Int} spec.goods_storage 商品库存
  254. *
  255. * @apiSuccess {String} code 返回码,10000为成功
  256. * @apiSuccess {String} message 返回消息
  257. * @apiSuccess {Object} result 返回数据
  258. */
  259. public function save_goods() {
  260. $spec_array = input('post.spec/a'); #获取数组
  261. if (empty($spec_array)) {
  262. ds_json_encode(10001, lang('param_error'));
  263. }
  264. $chain_goods_model = model('chain_goods');
  265. $chain_goods_validate = ds_validate('chain_goods');
  266. $goods_model = model('goods');
  267. Db::startTrans();
  268. try {
  269. foreach ($spec_array as $value) {
  270. if (!$chain_goods_validate->scene('chain_goods_add')->check($value)) {
  271. throw new \think\Exception($chain_goods_validate->getError(), 10006);
  272. }
  273. $condition = array();
  274. $condition[] = array('store_id', '=', $this->chain_info['store_id']);
  275. $condition[] = array('goods_id', '=', $value['goods_id']);
  276. $goods_info = $goods_model->getGoodsInfo($condition, 'goods_id,goods_commonid,store_id');
  277. if (!$goods_info) {
  278. throw new \think\Exception(lang('goods_not_exist'), 10006);
  279. }
  280. $condition = array();
  281. $condition[] = array('store_id', '=', $this->chain_info['store_id']);
  282. $condition[] = array('goods_id', '=', $value['goods_id']);
  283. $condition[] = array('chain_id', '=', $this->chain_info['chain_id']);
  284. $chain_goods_info = $chain_goods_model->getChainGoodsInfo($condition);
  285. if (!$chain_goods_info) {
  286. $chain_goods_model->addChainGoods(array(
  287. 'store_id' => $goods_info['store_id'],
  288. 'goods_id' => $goods_info['goods_id'],
  289. 'chain_id' => $this->chain_info['chain_id'],
  290. 'goods_commonid' => $goods_info['goods_commonid'],
  291. 'goods_storage' => $value['goods_storage'],
  292. ));
  293. } else {
  294. $chain_goods_model->editChainGoods(array('goods_storage' => $value['goods_storage']), array(array('chain_goods_id', '=', $chain_goods_info['chain_goods_id'])));
  295. }
  296. }
  297. } catch (\Exception $e) {
  298. Db::rollback();
  299. ds_json_encode(10001, $e->getMessage());
  300. }
  301. Db::commit();
  302. ds_json_encode(10000, lang('ds_common_op_succ'));
  303. }
  304. }