GoodsController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\Goods;
  5. use app\model\Category;
  6. use app\model\GoodsDescription;
  7. use app\model\GoodsImage;
  8. use app\model\GoodsSku;
  9. use app\model\GoodsSkuValue;
  10. use app\model\Store;
  11. use app\model\MemberAuthGroup;
  12. use app\model\GoodsMemberDiscount;
  13. use app\model\GoodsTimeDiscount;
  14. use app\model\Operatingcity;
  15. use app\model\Tuanzhang;
  16. class GoodsController extends Base
  17. {
  18. function index()
  19. {
  20. $weid = weid();
  21. $page = input('post.page', 1, 'intval');
  22. $path = input('post.path', '', 'serach_in');
  23. $ptypeistimes = input('post.ptypeistimes', '', 'serach_in');
  24. $ptype = input('post.ptype', '', 'serach_in');
  25. $is_times = input('post.is_times', '', 'serach_in');
  26. $is_all = input('post.is_all', '', 'serach_in');
  27. $is_mg = input('post.is_mg', '', 'serach_in');
  28. $status = input('post.status', '', 'serach_in');
  29. $keyword = input('post.keyword', '', 'serach_in');
  30. $cat_id = input('post.cat_id', '', 'serach_in');
  31. $is_recommended = input('post.is_recommended', '', 'serach_in');
  32. if ($ptypeistimes == 1) {
  33. $ptype = 1;
  34. } elseif ($ptypeistimes == 2) {
  35. $ptype = 2;
  36. } elseif ($ptypeistimes == 3) {
  37. $is_times = 1;
  38. }
  39. if ($path == '/order/service' || $path == '/order/storeservice') {
  40. $ptype = 2;
  41. }
  42. $query = Goods::where(['weid' => $weid, 'is_mg' => (int)$is_mg]);
  43. if (!empty($ptype)) {
  44. $query->where('ptype', $ptype);
  45. } else {
  46. $query->where('ptype', '>', 0);
  47. }
  48. if (empty($is_all)) {
  49. if (!empty($is_times)) {
  50. $query->where('is_times', $is_times);
  51. } else {
  52. $query->where('is_times', 0);
  53. }
  54. }
  55. if (!empty($this->sid)) {
  56. $query->where('sid', $this->sid);
  57. }
  58. if (!empty($this->tzid)) {
  59. $query->where('sid', Store::getidbytzid($this->tzid));
  60. }
  61. if (!empty($this->ocid)) {
  62. $Operatingcitydata = Operatingcity::find($this->ocid);
  63. if ($Operatingcitydata) {
  64. $Operatingcitydata = $Operatingcitydata->toArray();
  65. if (empty($Operatingcitydata['areatype'])) {
  66. $Operatingcitydata['areatype'] = 3;
  67. }
  68. if ($Operatingcitydata['areatype'] == 3) {
  69. $query->where('district_name', $Operatingcitydata['district_name']);
  70. } elseif ($Operatingcitydata['areatype'] == 2) {
  71. $query->where('city_name', $Operatingcitydata['city_name']);
  72. } elseif ($Operatingcitydata['areatype'] == 1) {
  73. $query->where('province_name', $Operatingcitydata['province_name']);
  74. }
  75. }
  76. }
  77. if (!empty($keyword)) {
  78. $query->where('name', 'like', '%' . $keyword . '%');
  79. }
  80. if (!empty($cat_id)) {
  81. $query->where('cat_id', $cat_id);
  82. }
  83. if (!empty($is_recommended) || $is_recommended === "0") {
  84. $query->where('is_recommended', $is_recommended);
  85. }
  86. if (!empty($status) || $status === "0") {
  87. $query->where(['status' => $status]);
  88. }
  89. $res = $query->order('sort asc,id desc')
  90. ->paginate(getpage())
  91. ->toArray();
  92. foreach ($res['data'] as &$vo) {
  93. $vo['image'] = toimg($vo['image']);
  94. if ($vo['time_amount'] == 0) {
  95. $vo['time_amount'] = '无';
  96. }
  97. if ($vo['sid'] == 0) {
  98. $vo['name'] = '[自营]' . $vo['name'];
  99. } else {
  100. $StoreTitle = Store::getTitle($vo['sid']);
  101. if (empty($StoreTitle)) {
  102. $vo['name'] = '[商户已被删除]' . $vo['name'];
  103. } else {
  104. $vo['name'] = '[' . $StoreTitle . ']' . $vo['name'];
  105. }
  106. }
  107. if ($vo['ptype'] == 2) {
  108. $vo['quantity'] = $vo['timesmum'];
  109. }
  110. if (empty($vo['quantity'])) {
  111. $vo['quantity'] = 1;
  112. }
  113. $vo['cat_id'] = Category::getTitle($vo['cat_id']);
  114. }
  115. $data['data'] = $res;
  116. if ($page == 1) {
  117. $data['field_data']['cidarray'] = _generateSelectTree(Category::getpcarray());
  118. }
  119. return $this->json($data);
  120. }
  121. function listUpdate()
  122. {
  123. $data = only('id,is_recommended,is_additional,status,sort');
  124. if (!$data['id']) throw new ValidateException('参数错误');
  125. Goods::update($data);
  126. return $this->json(['msg' => '操作成功']);
  127. }
  128. public function update()
  129. {
  130. $id = request()->post('id');
  131. $data = input('post.');
  132. unset($data['create_time']);
  133. $data['image'] = $data['images'][0]['url'];
  134. if (!empty($data['keyword'])) {
  135. $data['keyword'] = implode(',', $data['keyword']);
  136. }
  137. $data['time_amount'] = intval($data['time_amount']);
  138. //判断is_timer是否为1
  139. if ($data['is_timer'] == 1) {
  140. if (empty($data['time_amount'])) {
  141. throw new ValidateException('服务时长不能为空');
  142. }
  143. $data['quantity_unit'] = '分钟';
  144. }
  145. if ($data['cat_id']) {
  146. $cat = Category::find($data['cat_id']);
  147. if (!empty($cat)) {
  148. $cat = $cat->toArray();
  149. }
  150. $data['ptype'] = $cat['ptype'];
  151. }
  152. if (!empty($data['videourl'])) {
  153. $data['videotype'] = 1;
  154. } elseif (!empty($data['videoid'])) {
  155. $data['videotype'] = 2;
  156. } else {
  157. $data['videotype'] = 0;
  158. }
  159. if (empty($id)) {
  160. if (empty($data['cat_id'])) {
  161. throw new ValidateException('请选择分类');
  162. }
  163. if ($data['is_mg'] == 1) {
  164. if (empty($data['mgid'])) {
  165. throw new ValidateException('请选择会员等级');
  166. }
  167. }
  168. $data['weid'] = weid();
  169. if (!empty($this->sid)) {
  170. $data['sid'] = $this->sid;
  171. $storemod = Store::find($this->sid);
  172. if ($storemod) {
  173. $data['province_name'] = $storemod->province_name;
  174. $data['city_name'] = $storemod->city_name;
  175. $data['district_name'] = $storemod->district_name;
  176. }
  177. }
  178. if (!empty($this->tzid)) {
  179. $data['sid'] = Store::getidbytzid($this->tzid);
  180. $Tuanzhangmod = Tuanzhang::find($this->tzid);
  181. if ($Tuanzhangmod) {
  182. $data['province_name'] = $Tuanzhangmod->province_name;
  183. $data['city_name'] = $Tuanzhangmod->city_name;
  184. $data['district_name'] = $Tuanzhangmod->district_name;
  185. }
  186. }
  187. if (!empty($this->ocid)) {
  188. $data['ocid'] = $this->ocid;
  189. if (!empty($this->ocid)) {
  190. $ocmod = Operatingcity::find($this->ocid);
  191. if ($ocmod) {
  192. $data['province_name'] = $ocmod->province_name;
  193. $data['city_name'] = $ocmod->city_name;
  194. $data['district_name'] = $ocmod->district_name;
  195. }
  196. }
  197. }
  198. if (empty($data['tel'])) {
  199. $data['tel'] = '';
  200. }
  201. try {
  202. $res = Goods::create($data);
  203. if ($res->id && empty($data['sort'])) {
  204. Goods::update(['sort' => $res->id, 'id' => $res->id]);
  205. }
  206. $data['id'] = $res->id;
  207. $this->_synupdata($data);
  208. } catch (\Exception $e) {
  209. throw new ValidateException($e->getMessage());
  210. }
  211. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  212. } else {
  213. try {
  214. Goods::update($data);
  215. $this->_synupdata($data);
  216. } catch (\Exception $e) {
  217. throw new ValidateException($e->getMessage());
  218. }
  219. return $this->json(['msg' => '修改成功']);
  220. }
  221. }
  222. function _synupdata($data)
  223. {
  224. //详情
  225. if (empty(GoodsDescription::where('goods_id', $data['id'])->find())) {
  226. GoodsDescription::create([
  227. 'goods_id' => (int) $data['id'],
  228. 'description' => $data['description']
  229. ]);
  230. } else {
  231. GoodsDescription::where('goods_id', $data['id'])->update(['description' => $data['description']]);
  232. }
  233. //sku
  234. GoodsSku::where('goods_id', $data['id'])->delete();
  235. if (isset($data['attribute'])) {
  236. foreach ($data['attribute'] as $attr) {
  237. GoodsSku::create([
  238. 'goods_id' => (int) $data['id'],
  239. 'name' => $attr['name'],
  240. 'ptype' => 'radio',
  241. 'item' => $attr['item'] ? implode(',', $attr['item']) : ''
  242. ]);
  243. }
  244. }
  245. GoodsSkuValue::where('goods_id', $data['id'])->delete();
  246. if (isset($data['sku'])) {
  247. foreach ($data['sku'] as $skuarr) {
  248. GoodsSkuValue::create([
  249. 'goods_id' => (int) $data['id'],
  250. 'sku' => $skuarr['sku'],
  251. 'image' => $skuarr['image'],
  252. 'quantity' => $skuarr['quantity'],
  253. 'price' => $skuarr['price']
  254. ]);
  255. }
  256. }
  257. GoodsMemberDiscount::where('goods_id', $data['id'])->delete();
  258. if (!empty($data['is_member_discount']) && !empty($data['MemberGroup'])) {
  259. foreach ($data['MemberGroup'] as $mgvo) {
  260. if ($mgvo['price'] > 0) {
  261. GoodsMemberDiscount::create([
  262. 'goods_id' => (int) $data['id'],
  263. 'mgid' => (int) $mgvo['id'],
  264. 'price' => (float) $mgvo['price'],
  265. 'is_free' => (int) $mgvo['is_free']
  266. ]);
  267. }
  268. }
  269. }
  270. GoodsTimeDiscount::where('goods_id', $data['id'])->delete();
  271. if (!empty($data['timediscount'])) {
  272. foreach ($data['timediscount'] as $tdvo) {
  273. if ($tdvo['price'] > 0) {
  274. GoodsTimeDiscount::create([
  275. 'discount_method' => 1,
  276. 'goods_id' => (int) $data['id'],
  277. 'begin_time' => $tdvo['begin_time'],
  278. 'end_time' => $tdvo['end_time'],
  279. 'addsubtract' => (int) $tdvo['addsubtract'],
  280. 'price' => (float) $tdvo['price']
  281. ]);
  282. }
  283. }
  284. }
  285. //图片
  286. GoodsImage::where('goods_id', $data['id'])->delete();
  287. if (isset($data['images'])) {
  288. foreach ($data['images'] as $image) {
  289. GoodsImage::create([
  290. 'goods_id' => (int) $data['id'],
  291. 'weid' => weid(),
  292. 'image' => $image['url']
  293. ]);
  294. }
  295. }
  296. }
  297. function getgoodssku()
  298. {
  299. $id = $this->request->post('id', '', 'serach_in');
  300. $data['attribute'] = GoodsSku::get_goods_sku($id);
  301. $data['sourceAttribute'] = $data['attribute'];
  302. return $this->json(['data' => $data]);
  303. }
  304. function getInfo()
  305. {
  306. $id = $this->request->post('id', '', 'serach_in');
  307. //if (!$id) throw new ValidateException('参数错误');
  308. if ($id) {
  309. $data = Goods::field('*')->find($id)->toArray();
  310. } else {
  311. $data = [
  312. 'is_times' => 0,
  313. 'sort' => 100,
  314. 'status' => 1,
  315. ];
  316. }
  317. if (!empty($data['keyword'])) {
  318. $data['keyword'] = explode(',', $data['keyword']);
  319. } else {
  320. $data['keyword'] = [];
  321. }
  322. $GD = GoodsDescription::where(['goods_id' => $id])->find();
  323. if (!empty($GD)) {
  324. $data['description'] = $GD->description;
  325. }
  326. $goods_image = GoodsImage::where(['goods_id' => $id])
  327. ->field('image')
  328. ->order('id asc')
  329. ->select()->toArray();
  330. if (!empty($goods_image)) {
  331. foreach ($goods_image as $key => $vo) {
  332. $data['images'][$key]['url'] = toimg($vo['image']);
  333. }
  334. }
  335. $data['attribute'] = GoodsSku::get_goods_sku($id);
  336. $data['sourceAttribute'] = $data['attribute'];
  337. $MGDiscountarray = GoodsMemberDiscount::where(['goods_id' => $id])->select()->toArray();
  338. if (!empty($MGDiscountarray)) {
  339. $MGDiscount = [];
  340. foreach ($MGDiscountarray as $vo) {
  341. $MGDiscount[$vo['mgid']] = $vo;
  342. }
  343. }
  344. $data['MemberGroup'] = MemberAuthGroup::getGroup();
  345. foreach ($data['MemberGroup'] as &$mvo) {
  346. if (!empty($MGDiscount[$mvo['id']])) {
  347. $mvo['price'] = $MGDiscount[$mvo['id']]['price'];
  348. $mvo['addsubtract'] = $MGDiscount[$mvo['id']]['addsubtract'];
  349. $mvo['is_free'] = $MGDiscount[$mvo['id']]['is_free'];
  350. }
  351. }
  352. $timediscount = GoodsTimeDiscount::where(['goods_id' => $id])->select()->toArray();
  353. if (!empty($timediscount)) {
  354. $data['timediscount'] = $timediscount;
  355. } else {
  356. $data['timediscount'] = [['begin_time' => '', 'end_time' => '', 'addsubtract' => '', 'price' => '']];
  357. }
  358. $data['sku'] = GoodsSkuValue::field('sku,image,quantity,price')->where(['goods_id' => $id])
  359. ->order('id asc')
  360. ->select()->toArray();
  361. return $this->json(['data' => $data]);
  362. }
  363. function delete()
  364. {
  365. return $this->del(new Goods());
  366. }
  367. function getField()
  368. {
  369. $ptype = input('post.ptype', '', 'serach_in');
  370. $data['cidarray'] = _generateSelectTree(Category::getpcarray($ptype));
  371. $data['mgidarray'] = \app\model\MemberAuthGroup::getpcarray(1);
  372. $data['unitarray'] = \app\model\GoodsQuantityUnit::getpcarray($ptype);
  373. return $this->json(['data' => $data]);
  374. }
  375. }