GoodsController.php 11 KB

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