ScratchLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace addons\Scratch\common\logic;
  3. use addons\Coupon\common\models\AddonsCoupon;
  4. use addons\Scratch\common\enums\ScratchEnum;
  5. use addons\Scratch\common\models\ScratchCouponModel;
  6. use addons\Scratch\common\models\ScratchLogModel;
  7. use addons\Scratch\common\models\ScratchModel;
  8. use common\models\base\User;
  9. use common\models\goods\Goods;
  10. use common\models\goods\GoodsAttr;
  11. use common\models\mall\MallAddons;
  12. /**
  13. * Class ScratchLogic
  14. * @package addons\Scratch\common\logic
  15. */
  16. class ScratchLogic
  17. {
  18. /**
  19. * 列表
  20. * @param int $mallId
  21. * @param int $userId
  22. * @param array $postData
  23. * @return mixed
  24. * @throws \ErrorException
  25. */
  26. public static function getList($mallId = 0, $userId = 0, $postData = [])
  27. {
  28. try {
  29. $params = [
  30. 'with' => [
  31. 'goods' => function($q){
  32. $q->select('id,goods_name');
  33. }
  34. ],
  35. 'where' => [
  36. ['=', 'mall_id', $mallId],
  37. ['in', 'status', [ScratchEnum::ENABLED, ScratchEnum::DISABLED]],
  38. ],
  39. 'page' => $postData['page'] ?? 1,
  40. 'limit' => $postData['limit'] ?? 15,
  41. 'order' => $postData['order'] ?? 'id desc',
  42. ];
  43. if(!empty($postData['type'])){
  44. $params['where'][] = ['=', 'type', $postData['type']];
  45. }
  46. if(!empty($postData['keyword'])){
  47. $params['where'][] = ['like', 'name', $postData['keyword']];
  48. }
  49. $pageData = ScratchModel::listPage($params, false);
  50. if ($pageData === false) throw new \ErrorException(ScratchModel::getStaticError());
  51. foreach ($pageData['list'] as &$vo) {
  52. if($vo['type'] == ScratchModel::TYPE_COUPON){
  53. $vo['coupon_data'] = ScratchCouponModel::getData($vo['coupon_id'], 'id,name');
  54. }
  55. }
  56. $pageData['types'] = ScratchModel::getTypeList($mallId);
  57. return $pageData;
  58. } catch (\ErrorException $errorException) {
  59. throw new \ErrorException($errorException->getMessage());
  60. } catch (\Exception $exception) {
  61. throw new \Exception($exception->getMessage());
  62. }
  63. }
  64. /**
  65. * 列表
  66. * @param int $mallId
  67. * @param int $userId
  68. * @param array $postData
  69. * @return mixed
  70. * @throws \ErrorException
  71. */
  72. public static function getLogList($mallId = 0, $userId = 0, $postData = [])
  73. {
  74. try {
  75. $params = [
  76. 'with' => [
  77. 'scratch',
  78. 'user' => function($q){
  79. $q->select('id,nickname,avatar_url,mobile');
  80. },
  81. 'goods' => function($q){
  82. $q->select('id,goods_name');
  83. },
  84. ],
  85. 'where' => [
  86. ['=', 'mall_id', $mallId],
  87. ],
  88. 'page' => $postData['page'] ?? 1,
  89. 'limit' => $postData['limit'] ?? 20,
  90. 'order' => $postData['order'] ?? 'id desc',
  91. ];
  92. if(!empty($postData['type'])){
  93. $params['where'][] = ['=', 'type', $postData['type']];
  94. }
  95. if(!empty($userId)){
  96. $params['where'][] = ['=', 'user_id', $userId];
  97. }
  98. $is_search = 0;
  99. $user_id_arr = [];
  100. if (!empty($postData['keyword'])) {
  101. $is_search = 1;
  102. $user_list = User::lists([
  103. 'where' => [
  104. ['mall_id' => $mallId],
  105. [
  106. 'or',
  107. // ['like', 'username', $postData['keyword']],
  108. ['like', 'nickname', $postData['keyword']],
  109. ['like', 'mobile', $postData['keyword']]
  110. ]
  111. ],
  112. 'select' => 'id']);
  113. $user_ids = array_column($user_list, 'id');
  114. if (!empty($user_id_arr)) {
  115. $user_id_arr = array_intersect($user_id_arr, $user_ids);
  116. } else {
  117. $user_id_arr = $user_ids;
  118. }
  119. }
  120. if ($is_search) $params['where'][] = ['in', 'user_id', $user_id_arr];
  121. $pageData = ScratchLogModel::listPage($params, false);
  122. if ($pageData === false) throw new \ErrorException(ScratchModel::getStaticError());
  123. foreach ($pageData['list'] as &$vo) {
  124. if($vo['type'] == ScratchModel::TYPE_COUPON){
  125. $vo['coupon_data'] = ScratchCouponModel::getData($vo['coupon_id'], 'id,name');
  126. }
  127. }
  128. $pageData['types'] = ScratchModel::getTypeList($mallId);
  129. return $pageData;
  130. } catch (\ErrorException $errorException) {
  131. throw new \ErrorException($errorException->getMessage());
  132. } catch (\Exception $exception) {
  133. throw new \Exception($exception->getMessage());
  134. }
  135. }
  136. /**
  137. * @param $id
  138. * @return array|false|mixed|void
  139. * @throws \ErrorException
  140. */
  141. public static function getEdit($id, $mallId)
  142. {
  143. try {
  144. //所有可用的优惠券
  145. $coupon = AddonsCoupon::getMallCoupon($mallId);
  146. $list = ScratchModel::find()
  147. ->with([
  148. 'attr.goods',
  149. 'coupon',
  150. ])
  151. ->where([
  152. 'mall_id' => $mallId,
  153. 'id' => $id
  154. ])
  155. ->asArray()
  156. ->one();
  157. if ($list && $list['type'] == ScratchModel::TYPE_GOODS && $list['attr']) {
  158. $goodsAttr = GoodsAttr::find()->where(['is_delete' => 0, 'goods_id' => $list['attr']['goods_id']])->asArray()->all();
  159. foreach ($goodsAttr as $key => $item) {
  160. $attr_list = (new Goods())->signToAttr($item['sign_id'], $list['attr']['goods']['attr_groups']);
  161. $attr_str = '';
  162. foreach ($attr_list as $item1) {
  163. $attr_str .= $item1['attr_group_name'] . ':' . $item1['attr_name'] . ';';
  164. }
  165. $goodsAttr[$key]['attr_str'] = $attr_str;
  166. }
  167. $list['attr_list'] = $goodsAttr;
  168. $list['goods_name'] = $list['attr']['goods']['name'];
  169. unset($list['attr']);
  170. }
  171. return [
  172. 'list' => $list,
  173. 'types' => ScratchModel::getTypeList($mallId),
  174. 'is_coupon' => MallAddons::isInstall($mallId, 'Coupon'),
  175. 'coupon' => $coupon,
  176. ];
  177. } catch (\ErrorException $errorException) {
  178. throw new \ErrorException($errorException->getMessage());
  179. } catch (\Exception $exception) {
  180. throw new \Exception($exception->getMessage());
  181. }
  182. }
  183. /**
  184. * @param $id
  185. * @return array|false|mixed|void
  186. * @throws \ErrorException
  187. */
  188. public static function searchGoods($mallId, $postData)
  189. {
  190. try {
  191. $params = [
  192. 'with' => [
  193. 'attr' => function($q){
  194. $q->select('id,goods_id,name,stock');
  195. }
  196. ],
  197. 'where' => [
  198. ['=', 'mall_id', $mallId],
  199. ['in', 'status', [ScratchEnum::ENABLED, ScratchEnum::DISABLED]],
  200. ],
  201. 'select' => 'id,mall_id,goods_name',
  202. 'page' => $postData['page'] ?? 1,
  203. 'limit' => $postData['limit'] ?? 20,
  204. 'order' => $postData['order'] ?? 'id desc',
  205. ];
  206. if(!empty($postData['keyword'])){
  207. $params['where'][] = ['like', 'goods_name', $postData['keyword']];
  208. }
  209. $list = Goods::getGoodsList($params, true);
  210. return $list;
  211. } catch (\ErrorException $errorException) {
  212. throw new \ErrorException($errorException->getMessage());
  213. } catch (\Exception $exception) {
  214. throw new \Exception($exception->getMessage());
  215. }
  216. }
  217. /**
  218. * @param $mallId
  219. * @param $params
  220. * @return array
  221. * @throws \ErrorException
  222. */
  223. public static function saveEdit($mallId, $params)
  224. {
  225. try {
  226. $model = ScratchModel::setData($mallId, $params);
  227. //商品
  228. if ($params['type'] == ScratchModel::TYPE_GOODS) {
  229. $model->goods_id = $params['goods_id'] ?? 0;
  230. $model->attr_id = $params['attr_id'] ?? 0;
  231. $attrData = GoodsAttr::findOne([
  232. 'id' => $model->attr_id,
  233. 'goods_id' => $model->goods_id,
  234. 'status' => ScratchEnum::ENABLED,
  235. ]);
  236. if(empty($attrData)){
  237. throw new \ErrorException('商品不存在或已删除');
  238. }
  239. }
  240. // 优惠券
  241. if($params['type'] == ScratchModel::TYPE_COUPON ){
  242. self::setCouponCount($model);
  243. }
  244. if (!$model->save()) {
  245. throw new \ErrorException($model->getErrorMessage());
  246. }
  247. return true;
  248. } catch (\ErrorException $errorException) {
  249. throw new \ErrorException($errorException->getMessage());
  250. } catch (\Exception $exception) {
  251. throw new \Exception($exception->getMessage());
  252. }
  253. }
  254. /**
  255. * @param $mallId
  256. * @param $params
  257. * @return array
  258. * @throws \ErrorException
  259. */
  260. public static function saveEditStatus($mallId, $params)
  261. {
  262. try {
  263. $id = $params['id'] ?? 0;
  264. $model = ScratchModel::findOne($id);
  265. if(empty($model)){
  266. throw new \ErrorException('信息不存在');
  267. }
  268. $model->status = $params['status'] ?? 0;
  269. if (!$model->save()) {
  270. throw new \ErrorException($model->getErrorMessage());
  271. }
  272. return true;
  273. } catch (\ErrorException $errorException) {
  274. throw new \ErrorException($errorException->getMessage());
  275. } catch (\Exception $exception) {
  276. throw new \Exception($exception->getMessage());
  277. }
  278. }
  279. /**
  280. * @param ScratchModel $model
  281. * @return bool
  282. */
  283. private static function setCouponCount(ScratchModel $model){
  284. $coupon = new ScratchCouponModel();
  285. $old_stock = $model->getOldAttribute('stock');
  286. $new_stock = $old_stock - $model->stock;
  287. if($model->isNewRecord){
  288. // 新增,减优惠券数据量
  289. $coupon->updateCount($model->stock, 'sub', $model->coupon_id);
  290. return true;
  291. }
  292. // 修改
  293. if($new_stock > 0){
  294. // 减少了优惠券数据量,要把它加回去
  295. $res = $coupon->updateCount(abs($new_stock), 'add', $model->coupon_id);
  296. }else{
  297. // 增加了优惠券数量,要减出来
  298. $res = $coupon->updateCount(abs($new_stock), 'sub', $model->coupon_id);
  299. }
  300. return true;
  301. }
  302. }