Goods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\shop\GoodsAttr;
  9. use app\admin\model\shop\Spec;
  10. use app\admin\model\shop\GoodsSkuSpec;
  11. /**
  12. * 商品管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Goods extends Backend
  17. {
  18. /**
  19. * 快速搜索时执行查找的字段
  20. */
  21. protected $searchFields = 'id,goods_sn,title,subtitle';
  22. /**
  23. * Goods模型对象
  24. * @var \app\admin\model\shop\Goods
  25. */
  26. protected $model = null;
  27. protected $sku_model = null;
  28. public function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->model = new \app\admin\model\shop\Goods;
  32. $this->sku_model = new \app\admin\model\shop\GoodsSku;
  33. $this->view->assign("flagList", $this->model->getFlagList());
  34. $this->view->assign("statusList", $this->model->getStatusList());
  35. }
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $list = $this->model
  50. ->with(['Freight', 'Brand', 'Category'])
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. $result = array("total" => $list->total(), "rows" => $list->items());
  55. return json($result);
  56. }
  57. return $this->view->fetch();
  58. }
  59. /**
  60. * 查看
  61. */
  62. public function select()
  63. {
  64. //设置过滤方法
  65. $this->request->filter(['strip_tags', 'trim']);
  66. if ($this->request->isAjax()) {
  67. //如果发送的来源是Selectpage,则转发到Selectpage
  68. if ($this->request->request('keyField')) {
  69. return $this->selectpage();
  70. }
  71. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  72. $list = $this->model
  73. ->with(['Freight', 'Brand', 'Category'])
  74. ->where($where)
  75. ->order($sort, $order)
  76. ->paginate($limit);
  77. $result = array("total" => $list->total(), "rows" => $list->items());
  78. return json($result);
  79. }
  80. return $this->view->fetch();
  81. }
  82. //检查属性skus 和 spec 是否对上
  83. protected function checkSku($skus, $spec)
  84. {
  85. foreach ($skus as $item) {
  86. if (!isset($item['skus']) || !is_array($item['skus']) || empty($item['skus'])) {
  87. throw new Exception('规格属性不能为空');
  88. }
  89. if (!isset($item['price']) || !isset($item['marketprice'])) {
  90. throw new Exception('请录入价格');
  91. }
  92. if (($item['marketprice'] > 0 || $item['price'] > 0 || $item['stocks'] > 0) && $item['marketprice'] <= $item['price']) {
  93. throw new Exception('市场价必须大于销售价');
  94. }
  95. foreach ($item['skus'] as $k => $v) {
  96. if (empty($v) && !is_numeric($v)) {
  97. throw new Exception('规格【' . $v . '】属性值不能为空');
  98. }
  99. if (!isset($spec[$k]['value']) || (empty($spec[$k]['name']) && !is_numeric($spec[$k]['name']))) {
  100. throw new Exception('规格【' . $v . '】名称不能为空');
  101. }
  102. foreach ($spec[$k]['value'] as $m => $n) {
  103. if (stripos($n, ',') !== false) {
  104. throw new Exception('规格【' . $v . '】属性值中不能包含,');
  105. }
  106. }
  107. if (count($spec[$k]['value']) != count(array_unique($spec[$k]['value']))) {
  108. throw new Exception('规格【' . $v . '】属性值中不能有重复值');
  109. }
  110. if (empty($spec[$k]['value']) || !in_array($v, $spec[$k]['value'])) {
  111. throw new Exception('规格【' . $v . '】属性不匹配');
  112. }
  113. }
  114. }
  115. }
  116. protected function getSkuId($skus, $newSpec, $spec)
  117. {
  118. $arr = [];
  119. foreach ($skus as $index => $item) {
  120. $specArr = $spec[$index];
  121. foreach ($newSpec as $subindex => $subitem) {
  122. if ($subitem['spec_name'] == $specArr['name'] && $subitem['spec_value_value'] == $item) {
  123. $arr[] = $subitem['id'];
  124. }
  125. }
  126. }
  127. sort($arr);
  128. return implode(',', $arr);
  129. }
  130. //添加商品属性
  131. protected function addGoodsSku($skus, $spec, $goods_id)
  132. {
  133. //属性入库
  134. $specList = Spec::push($spec);
  135. $newSpec = GoodsSkuSpec::push($specList, $goods_id);
  136. //匹配属性
  137. $list = $this->sku_model->where('goods_id', $goods_id)->select();
  138. $newData = [];
  139. $stocks = 0;
  140. foreach ($skus as $k => $sk) {
  141. $newSkuId = $this->getSkuId($sk['skus'], $newSpec, $spec);
  142. $newSkuData = [
  143. 'goods_id' => $goods_id,
  144. 'sku_id' => $newSkuId,
  145. 'goods_sn' => $sk['goods_sn'] ?? '',
  146. 'image' => $sk['image'] ?? '',
  147. 'price' => $sk['price'] ?? 0,
  148. 'marketprice' => $sk['marketprice'] ?? 0,
  149. 'stocks' => $sk['stocks'] ?? 0,
  150. ];
  151. if (isset($list[$k])) {
  152. $row = $list[$k];
  153. $oldSkuIdsArr = explode(',', $row['sku_id']);
  154. sort($oldSkuIdsArr);
  155. $oldSkuId = implode(',', $oldSkuIdsArr);
  156. if ($oldSkuId == $newSkuId) {
  157. //相等的更新
  158. $row->save($newSkuData);
  159. } else {
  160. //不等的
  161. $row->save(array_merge($newSkuData, ['sales' => 0]));
  162. }
  163. unset($list[$k]);
  164. } else { //多余的
  165. $newData[] = array_merge($newSkuData, ['sales' => 0]);
  166. }
  167. $stocks = bcadd($stocks, $sk['stocks'] ?? 0);
  168. }
  169. if (!empty($newData)) {
  170. $this->sku_model->saveAll($newData);
  171. }
  172. //更新库存
  173. if (!empty($skus)) {
  174. $this->model->where('id', $goods_id)->update(['stocks' => $stocks, 'spectype' => 1]);
  175. } else {
  176. $this->model->where('id', $goods_id)->update(['spectype' => 0]);
  177. }
  178. //原来多的删除
  179. foreach ($list as $it) {
  180. $it->delete();
  181. }
  182. }
  183. /**
  184. * 添加
  185. */
  186. public function add()
  187. {
  188. if ($this->request->isPost()) {
  189. $params = $this->request->post("row/a");
  190. if ($params) {
  191. $params = $this->preExcludeFields($params);
  192. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  193. $params[$this->dataLimitField] = $this->auth->id;
  194. }
  195. $result = false;
  196. Db::startTrans();
  197. try {
  198. //是否采用模型验证
  199. if ($this->modelValidate) {
  200. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  201. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  202. $this->model->validateFailException(true)->validate($validate);
  203. }
  204. $result = $this->model->allowField(true)->save($params);
  205. //商品规格
  206. if (isset($params['skus']) && isset($params['spec'])) {
  207. $params['skus'] = (array)json_decode($params['skus'], true);
  208. $params['spec'] = (array)json_decode($params['spec'], true);
  209. $this->checkSku($params['skus'], $params['spec']);
  210. $this->addGoodsSku($params['skus'], $params['spec'], $this->model->id);
  211. }
  212. //商品属性
  213. if (isset($params['attribute_ids'])) {
  214. GoodsAttr::addGoodsAttr($params['attribute_ids'], $this->model->id);
  215. }
  216. Db::commit();
  217. } catch (ValidateException $e) {
  218. Db::rollback();
  219. $this->error($e->getMessage());
  220. } catch (PDOException $e) {
  221. Db::rollback();
  222. $this->error($e->getMessage());
  223. } catch (Exception $e) {
  224. Db::rollback();
  225. $this->error($e->getMessage());
  226. }
  227. if ($result !== false) {
  228. $this->success();
  229. } else {
  230. $this->error(__('No rows were inserted'));
  231. }
  232. }
  233. $this->error(__('Parameter %s can not be empty', ''));
  234. }
  235. return $this->view->fetch();
  236. }
  237. /**
  238. * 编辑
  239. */
  240. public function edit($ids = null)
  241. {
  242. $row = $this->model->get($ids);
  243. if (!$row) {
  244. $this->error(__('No Results were found'));
  245. }
  246. $adminIds = $this->getDataLimitAdminIds();
  247. if (is_array($adminIds)) {
  248. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  249. $this->error(__('You have no permission'));
  250. }
  251. }
  252. if ($this->request->isPost()) {
  253. $params = $this->request->post("row/a");
  254. if ($params) {
  255. $params = $this->preExcludeFields($params);
  256. $result = false;
  257. Db::startTrans();
  258. try {
  259. //是否采用模型验证
  260. if ($this->modelValidate) {
  261. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  262. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  263. $row->validateFailException(true)->validate($validate);
  264. }
  265. $result = $row->allowField(true)->save($params);
  266. //商品规格
  267. if (isset($params['skus']) && isset($params['spec'])) {
  268. $params['skus'] = (array)json_decode($params['skus'], true);
  269. $params['spec'] = (array)json_decode($params['spec'], true);
  270. $this->checkSku($params['skus'], $params['spec']);
  271. $this->addGoodsSku($params['skus'], $params['spec'], $row->id);
  272. }
  273. //商品属性
  274. if (isset($params['attribute_ids'])) {
  275. GoodsAttr::addGoodsAttr($params['attribute_ids'], $row->id);
  276. }
  277. Db::commit();
  278. } catch (ValidateException $e) {
  279. Db::rollback();
  280. $this->error($e->getMessage());
  281. } catch (PDOException $e) {
  282. Db::rollback();
  283. $this->error($e->getMessage());
  284. } catch (Exception $e) {
  285. Db::rollback();
  286. $this->error($e->getMessage());
  287. }
  288. if ($result !== false) {
  289. $this->success();
  290. } else {
  291. $this->error(__('No rows were updated'));
  292. }
  293. }
  294. $this->error(__('Parameter %s can not be empty', ''));
  295. }
  296. //查询属性输出
  297. $list = $this->sku_model->field("sku.*,GROUP_CONCAT(sp.name,':',sv.value ORDER BY sp.id asc) sku_attr")
  298. ->alias('sku')
  299. ->where('sku.goods_id', $row->id)
  300. ->join('shop_goods_sku_spec p', "FIND_IN_SET(p.id,sku.sku_id)", 'LEFT')
  301. ->join('shop_spec sp', 'sp.id=p.spec_id', 'LEFT')
  302. ->join('shop_spec_value sv', 'sv.id=p.spec_value_id', 'LEFT')
  303. ->group('sku.id')
  304. ->select();
  305. $this->view->assign("row", $row);
  306. $this->assignconfig('goods', $row);
  307. $this->assignconfig('goods_skus', $list);
  308. return $this->view->fetch();
  309. }
  310. /**
  311. * 根据ID批量获取商品信息
  312. */
  313. public function get_goods_by_ids()
  314. {
  315. $ids = $this->request->request('ids');
  316. if (!$ids) {
  317. $this->error('请选择商品');
  318. }
  319. $ids = explode(',', $ids);
  320. $list = $this->model
  321. ->with(['Category'])
  322. ->where('id', 'in', $ids)
  323. ->select();
  324. // 处理图片URL
  325. foreach ($list as &$item) {
  326. if ($item['image']) {
  327. $item['image'] = cdnurl($item['image'], true);
  328. }
  329. }
  330. return json($list);
  331. }
  332. }