Goods.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. namespace app\seller\controller;
  3. use app\common\controller\SellerController;
  4. use app\http\IResponse;
  5. use think\Db;
  6. use think\facade\Log;
  7. class Goods extends SellerController
  8. {
  9. /**
  10. * 新增规格
  11. *
  12. * @author 许祖兴 < zuxing.xu@lettered.cn>
  13. * @date 2020/6/19 17:08
  14. *
  15. * @return mixed
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\ModelNotFoundException
  18. * @throws \think\exception\DbException
  19. */
  20. public function createSpec()
  21. {
  22. // 接收数据
  23. $params = $this->request->param();
  24. // 数据校验
  25. $valid = $this->validate($params,[
  26. 'name|规格名称' => 'require',
  27. ]);
  28. (true !== $valid) && IResponse::failure($valid);
  29. // 1. 存在性
  30. $model = model('common/GoodsSpec');
  31. if ($spec = $model->where(['spec_name' => $params['name']])->find())
  32. {
  33. //直接返回id
  34. return IResponse::success($spec);
  35. }
  36. // 不存在则新增
  37. return IResponse::success($model->create([
  38. 'spec_name' => $params['name']
  39. ],true));
  40. }
  41. /**
  42. * 新增规格属性
  43. *
  44. * @author 许祖兴 < zuxing.xu@lettered.cn>
  45. * @date 2020/6/19 17:10
  46. *
  47. * @return mixed
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. public function createSpecValue()
  53. {
  54. // 接收数据
  55. $params = $this->request->param();
  56. // 数据校验
  57. $valid = $this->validate($params,[
  58. 'spec_id|规格ID' => 'require',
  59. 'name|规格属性名称' => 'require',
  60. ]);
  61. (true !== $valid) && IResponse::failure($valid);
  62. // 1. 存在性
  63. $model = model('common/GoodsSpecValue');
  64. if ($value = $model->where(['spec_id' => $params['spec_id'],'value_name' => $params['name']])->find())
  65. {
  66. //直接返回id
  67. return IResponse::success($value);
  68. }
  69. // 不存在则新增
  70. return IResponse::success($model->create([
  71. 'spec_id' => $params['spec_id'],
  72. 'value_name' => $params['name']
  73. ],true));
  74. }
  75. /**
  76. * 取得商品规格
  77. *
  78. * @author 许祖兴 < zuxing.xu@lettered.cn>
  79. * @date 2020/6/19 17:00
  80. *
  81. * @param $id
  82. * @return mixed
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. * @throws \think\exception\DbException
  86. */
  87. public function getSkuById($id)
  88. {
  89. // 获取规格
  90. $skus = model('common/GoodsSku')
  91. ->where(['goods_id' => $id])
  92. ->select();
  93. $spec = [];
  94. $values = [];
  95. $ret = [];
  96. // 取出这个产品的SPEC SPEC_VALUE
  97. foreach ($skus as $sku) {
  98. $ret['sku'][] = [
  99. 'ids' => dejson($sku['spec_param']),
  100. 'price' => $sku['price'],
  101. 'instock_price' => $sku['instock_price'],
  102. 'stock' => $sku['stock'],
  103. ];
  104. foreach (dejson($sku['spec_param']) as $sp) {
  105. foreach ($sp as $sid => $vid) {
  106. $spec[] = model('GoodsSpec')->where(['id' => $sid])->find();
  107. $values[] = model('GoodsSpecValue')->where(['spec_id' => $sid, 'id' => $vid])->find();
  108. }
  109. }
  110. }
  111. // 构造
  112. foreach (array_unique($spec) as $s => $k) {
  113. $sub = [];
  114. foreach (array_unique($values) as $i => $v) {
  115. if ($v['spec_id'] == $k['id']) {
  116. $sub[] = [
  117. 'id' => $v['id'],
  118. 'name' => $v['value_name']
  119. ];
  120. }
  121. }
  122. $ret['spec'][] = [
  123. 'id' => $k['id'],
  124. 'name' => $k['spec_name'],
  125. 'sub' => $sub
  126. ];
  127. }
  128. return IResponse::success($ret);
  129. }
  130. /**
  131. * 获取商品信息
  132. *
  133. * @author 许祖兴 < zuxing.xu@lettered.cn>
  134. * @date 2020/7/10 12:15
  135. *
  136. * @return mixed
  137. * @throws \Lettered\Support\Exceptions\FailedException
  138. * @throws \think\exception\DbException
  139. */
  140. public function index()
  141. {
  142. $where = [];
  143. //组合搜索
  144. !empty(input('name')) && $where[]
  145. = ['name', 'like', '%' . input('name') . '%'];
  146. !empty(input('seller_id')) && $where[]
  147. = ['seller_id', 'eq', input('seller_id')];
  148. !empty(input('category_id')) && $where[]
  149. = ['category_id', 'eq', input('category_id')];
  150. !empty(input('area')) && $where[]
  151. = ['area_id', 'eq', input('area')];
  152. $product = model('common/Goods');
  153. return IResponse::paginate($product->where($where)
  154. ->where(['seller_id' => $this->seller->id])
  155. ->with(['category'])->order(['created_at' => 'desc'])
  156. ->paginate(input('limit'),false));
  157. }
  158. /**
  159. * 新增产品
  160. *
  161. * @author 许祖兴 < zuxing.xu@lettered.cn>
  162. * @date 2020/7/10 12:32
  163. *
  164. * @return mixed
  165. */
  166. public function save()
  167. {
  168. // 接收数据
  169. $params = $this->request->param();
  170. // 归属商家
  171. $params['seller_id'] = $this->seller->id;
  172. // 数据校验
  173. $valid = $this->validate($params,[
  174. 'seller_id|归属商家' => 'require',
  175. 'cover_img|产品主图' => 'require',
  176. 'name|产品名称' => 'require|unique:Goods',
  177. 'sku|产品规格' => 'require',
  178. 'content|产品详情' => 'require',
  179. 'sku| 商品规格参数' => 'require'
  180. ],[
  181. 'email.unique' => '重复产品名称!'
  182. ]);
  183. (true !== $valid) && IResponse::failure($valid);
  184. // 保存数据
  185. $goodsId = model('common/Goods')->storeBy($params);
  186. Db::startTrans();
  187. try {
  188. // sku处理
  189. $insku = dejson($params['sku']);
  190. //库存
  191. $stock = 0;
  192. foreach ($insku as &$sku){
  193. $sku['goods_id'] = $goodsId;
  194. $sku['spec_param'] = enjson($sku['ids']);
  195. $sku['param'] = enjson($sku['param']);
  196. $stock += $sku['stock'];
  197. model('common/GoodsSku')::create($sku, true);
  198. }
  199. // 进货价
  200. $iprice = array_column($insku, 'instock_price');
  201. sort($iprice);
  202. // 价格
  203. $price = array_column($insku, 'price');
  204. sort($price);
  205. // 更新信息
  206. model('common/Goods')->updateBy($goodsId, [
  207. 'stock' => $stock,
  208. 'instock_price' => $iprice[0],
  209. 'price' => $price[0]
  210. ]);
  211. Db::commit();
  212. }catch (\Exception $e){
  213. Log::debug($e->getMessage());
  214. Db::rollback();
  215. }
  216. return $goodsId ? IResponse::success([],'新增产品成功'):
  217. IResponse::failure('新增产品异常');
  218. }
  219. /**
  220. * 更新产品信息
  221. *
  222. * @author 许祖兴 < zuxing.xu@lettered.cn>
  223. * @date 2020/7/10 12:32
  224. *
  225. * @param $id
  226. * @return mixed
  227. * @throws \think\Exception
  228. * @throws \think\exception\PDOException
  229. */
  230. public function update($id)
  231. {
  232. // 接收数据
  233. $params = $this->request->param();
  234. // 归属商家
  235. $params['seller_id'] = $this->seller->id;
  236. // 查询
  237. $goods = model('common/Goods')->findBy($id);
  238. // 是否更改状态操作
  239. if (isset($params['status']) && $params['status'] != '') {
  240. $valid = $this->validate($params, [
  241. 'status|配置状态' => 'require|integer'
  242. ]);
  243. }else {
  244. // 数据校验
  245. $valid = $this->validate($params, [
  246. 'seller_id|归属商家' => 'require',
  247. 'cover_img|产品主图' => 'require',
  248. 'name|产品名称' => 'require|unique:Goods',
  249. 'sku|产品规格' => 'require',
  250. 'content|产品详情' => 'require',
  251. 'sku| 商品规格参数' => 'require'
  252. ]);
  253. }
  254. // 错误返回
  255. (true !== $valid) && IResponse::failure($valid);
  256. // 删除旧数据
  257. //TODO 模型方法有bug,等待优化
  258. $this->app->db()->name('goods_sku')->where(['goods_id' => $goods['id']])->delete();
  259. // sku处理
  260. $insku = dejson($params['sku']);
  261. $stock = 0;
  262. foreach ($insku as &$sku){
  263. $sku['goods_id'] = $goods['id'];
  264. $sku['spec_param'] = enjson($sku['ids']);
  265. $sku['param'] = enjson($sku['param']);
  266. $stock += $sku['stock'];
  267. model('common/GoodsSku')::create($sku,true);
  268. }
  269. // 库存
  270. $params['stock'] = $stock;
  271. // 进货价
  272. $iprice = array_column($insku, 'instock_price');
  273. sort($iprice);
  274. $params['instock_price'] = $iprice[0];
  275. // 价格
  276. $price = array_column($insku, 'price');
  277. sort($price);
  278. $params['price'] = $price[0];
  279. // 更新信息
  280. $goods->updateBy($id, $params);
  281. return IResponse::success('更新产品信息成功');
  282. }
  283. /**
  284. * 删除产品
  285. *
  286. * @param $id
  287. * @return \think\response\Json
  288. */
  289. public function delete($id)
  290. {
  291. model('common/Goods')->deleteBy($id);
  292. return IResponse::success([],'删除产品成功');
  293. }
  294. /************ 分类 **********************/
  295. public function categories()
  296. {
  297. $where = [];
  298. //组合搜索
  299. $user = model('common/GoodsCategory');
  300. return IResponse::paginate($user->where($where)
  301. ->paginate(input('limit'),false));
  302. }
  303. /**
  304. *
  305. * @author 许祖兴 < zuxing.xu@lettered.cn>
  306. * @date 2020/7/16 18:02
  307. *
  308. * @return mixed
  309. */
  310. public function plectron(){
  311. // 收参数
  312. $params = $this->request->param();
  313. foreach (str2arr($params['ids']) as $id){
  314. $user = model('common/GoodsOrder')->getBy($id);
  315. if ($this->request->isDelete()){
  316. $user->deleteBy($id);
  317. }
  318. $user->allowField(true)->updateBy($id, $params);
  319. }
  320. return IResponse::success([],'操作成功');
  321. }
  322. /**
  323. * 订单操作
  324. *
  325. * @author 许祖兴 < zuxing.xu@lettered.cn>
  326. * @date 2020/7/16 18:02
  327. *
  328. * @param $id
  329. * @return mixed
  330. */
  331. public function actionOrder($id)
  332. {
  333. $param = $this->request->param();
  334. $valid = $this->validate($param, [
  335. 'action|操作状态' => 'require'
  336. ]);
  337. // 错误返回
  338. (true !== $valid) && IResponse::failure($valid);
  339. $model = model('common/GoodsOrder');
  340. switch ($param['action']){
  341. case "ship":
  342. $msg = "发货";
  343. // 状态以及时间
  344. $model->updateBy($id,[
  345. 'shipping_at' => time(),
  346. 'status' => 3
  347. ]);
  348. break;
  349. case "confirm":
  350. $msg = "确认";
  351. // 系统帮助用户自主确认订单
  352. $model->updateBy($id,[
  353. 'received_at' => time(),
  354. 'status' => 4
  355. ]);
  356. break;
  357. case "delete":
  358. $msg = "删除";
  359. $model->deleteBy($id);
  360. break;
  361. }
  362. return IResponse::success([],$msg . '成功');
  363. }
  364. }