ClerkService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace addons\Happiness\common\service;
  3. use addons\Happiness\common\models\HappinessClerk;
  4. use addons\Happiness\common\models\HappinessClerkLog;
  5. use addons\Happiness\common\models\HappinessStore;
  6. use addons\Happiness\common\models\HappinessStoreGoods;
  7. use common\enums\StatusEnum;
  8. use common\helpers\ApiCode;
  9. use common\enums\AddonsEnum;
  10. use common\models\user\User;
  11. use yii\helpers\Json;
  12. use Exception;
  13. use Money\Exchange;
  14. use Swoole\Http\Status;
  15. use Yii;
  16. class ClerkService extends BaseService
  17. {
  18. //获取核销员列表
  19. public function getClerkMemberList($params)
  20. {
  21. try {
  22. $where = [
  23. ['mall_id' => $this->mall_id],
  24. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  25. ];
  26. if (!empty($params['user_id'])) $where[] = ['user_id' => $params['user_id']];
  27. if (!empty($params['keyword'])) {
  28. $uids = User::find()->where(['mall_id' => $this->mall_id])
  29. ->andWhere(['status' => StatusEnum::ENABLED])
  30. ->andWhere(['or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']]])
  31. ->select('id')->column();
  32. if (!empty($uids)) $where[] = ['user_id' => $uids];
  33. }
  34. if (!empty($params['store_id'])) $where[] = ['store_id' => $params['store_id']];
  35. $clerk_params = [
  36. 'where' => $where,
  37. 'page' => $params['page'],
  38. 'limig' => $params['limit'],
  39. 'order' => 'id desc',
  40. 'with' => ['user', 'store'],
  41. ];
  42. $list = HappinessClerk::listPage($clerk_params);
  43. return $list;
  44. } catch (\Exception $e) {
  45. throw new Exception($e->getMessage());
  46. }
  47. }
  48. //获取用户列表
  49. public function searchUser($params)
  50. {
  51. try {
  52. $where = [
  53. ['mall_id' => $this->mall_id],
  54. ['status' => StatusEnum::ENABLED],
  55. ];
  56. if (!empty($params['keyword'])) {
  57. $uids = User::find()->where(['mall_id' => $this->mall_id])
  58. ->andWhere(['status' => StatusEnum::ENABLED])
  59. ->andWhere(['or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']], ['id' => $params['keyword']]])
  60. ->select('id')->column();
  61. if (!empty($uids)) $where[] = ['id' => $uids];
  62. }
  63. //获取已被添加的核销员user_id
  64. $clerk_user = HappinessClerk::lists([
  65. 'where' => [
  66. ['mall_id' => $this->mall_id],
  67. ['status' => StatusEnum::ENABLED]
  68. ],
  69. ]);
  70. if (!empty($clerk_user)) {
  71. $user_id = array_column($clerk_user, 'user_id', 'user_id');
  72. $where[] = ['not in', 'id', $user_id];
  73. }
  74. $list = User::listPage([
  75. 'where' => $where,
  76. 'page' => 1,
  77. 'limit' => 10,
  78. ]);
  79. return $list;
  80. } catch (\Exception $e) {
  81. throw new Exception($e->getMessage());
  82. }
  83. }
  84. //查询门店数据
  85. public function searchStore($params)
  86. {
  87. try {
  88. $where = [
  89. ['mall_id' => $this->mall_id],
  90. ['status' => StatusEnum::ENABLED],
  91. ];
  92. if (!empty($params['keyword'])) {
  93. $where[] = ['or', ['like', 'store_name', $params['keyword']], ['id' => $params['keyword']]];
  94. }
  95. $store_params = [
  96. 'where' => $where,
  97. 'page' => $params['page'],
  98. 'limit' => $params['limit'],
  99. 'order' => 'id desc'
  100. ];
  101. $list = HappinessStore::listPage($store_params);
  102. return $list;
  103. } catch (\Exception $e) {
  104. throw new Exception($e->getMessage());
  105. }
  106. }
  107. //添加、编辑核销员
  108. public function addClerk($params)
  109. {
  110. try {
  111. $params['mall_id'] = $this->mall_id;
  112. $params['status'] = StatusEnum::ENABLED;
  113. $res = HappinessClerk::setData($params);
  114. if (!$res) throw new Exception(HappinessClerk::$static_error);
  115. return true;
  116. } catch (\Exception $e) {
  117. throw new Exception($e->getMessage());
  118. }
  119. }
  120. //删除核销员
  121. public function deleteClerk($params)
  122. {
  123. try {
  124. //查询是否存在该核销员
  125. $info = HappinessClerk::getOne([
  126. ['mall_id' => $this->mall_id],
  127. ['id' => $params['id']],
  128. ]);
  129. if (empty($info)) throw new Exception('不存在该核销员');
  130. $info->status = StatusEnum::DELETE;
  131. if (!$info->save()) throw new Exception('删除失败');
  132. return true;
  133. } catch (\Exception $e) {
  134. throw new Exception($e->getMessage());
  135. }
  136. }
  137. public function clerkLogList($params)
  138. {
  139. try {
  140. $where = [
  141. ['mall_id' => $this->mall_id],
  142. ['status' => StatusEnum::ENABLED],
  143. ];
  144. if (!empty($params['store_id'])) $where[] = ['store_id' => $params['store_id']];
  145. if (!empty($params['order_no'])) $where[] = ['order_no' => $params['order_no']];
  146. if (!empty($params['date_start'])) $where[] = ['>=', 'created_at', strtotime($params['date_start'])];
  147. if (!empty($params['date_end'])) $where[] = ['<=', 'created_at', strtotime($params['date_end'])];
  148. if (!empty($params['user_keyword'])) {
  149. //获取用户数据
  150. $uids = User::find()->where(['mall_id' => $this->mall_id])
  151. ->andWhere(['status' => StatusEnum::ENABLED])
  152. ->andWhere(['or', ['like', 'nickname', $params['user_keyword']], ['like', 'mobile', $params['user_keyword']], ['like', 'id', $params['user_keyword']]])
  153. ->select('id')->column();
  154. if (empty($uids)) {
  155. $where[] = ['user_id' => 0];
  156. } else {
  157. $where[] = ['user_id' => $uids];
  158. }
  159. }
  160. if (!empty($params['clerk_keyword'])) {
  161. //获取用户数据
  162. $uids = User::find()->where(['mall_id' => $this->mall_id])
  163. ->andWhere(['status' => StatusEnum::ENABLED])
  164. ->andWhere(['or', ['like', 'nickname', $params['clerk_keyword']], ['like', 'mobile', $params['clerk_keyword']], ['like', 'id', $params['clerk_keyword']]])
  165. ->select('id')->column();
  166. if (empty($uids)) {
  167. $where[] = ['user_id' => 0];
  168. } else {
  169. $clerk_ids = HappinessClerk::find()->where(['mall_id' => $this->mall_id])
  170. ->andWhere(['status' => StatusEnum::ENABLED])
  171. ->andWhere(['user_id' => $uids])
  172. ->select('id')->column();
  173. if (empty($clerk_ids)) {
  174. $where[] = ['clerk_id' => 0];
  175. } else {
  176. $where[] = ['clerk_id' => $clerk_ids];
  177. }
  178. }
  179. }
  180. $clerk_params = [
  181. 'where' => $where,
  182. 'page' => $params['page'],
  183. 'limit' => $params['limit'],
  184. 'order' => 'id desc',
  185. 'with' => ['store', 'clerkUser', 'user'],
  186. ];
  187. $list = HappinessClerkLog::listPage($clerk_params);
  188. return $list;
  189. } catch (\Exception $e) {
  190. throw new Exception($e->getMessage());
  191. }
  192. }
  193. //获取门店核销记录数据列表
  194. public function storeClerkLog($params)
  195. {
  196. try {
  197. $where = [
  198. ['mall_id' => $this->mall_id],
  199. ['status' => StatusEnum::ENABLED],
  200. ['store_id' => $this->store_id],
  201. ];
  202. if (!empty($params['order_no'])) $where[] = ['order_no' => $params['order_no']];
  203. if (!empty($params['user_id'])) $where[] = ['user_id' => $params['user_id']];
  204. if (!empty($params['clerk_id'])) {
  205. //获取用户数据
  206. $clerk_ids = HappinessClerk::find()->where(['mall_id' => $this->mall_id])
  207. ->andWhere(['status' => StatusEnum::ENABLED])
  208. ->andWhere(['user_id' => $params['clerk_id']])
  209. ->select('id')->column();
  210. if (empty($clerk_ids)) {
  211. $where[] = ['clerk_ids' => 0];
  212. } else {
  213. $where[] = ['clerk_id' => $clerk_ids];
  214. }
  215. }
  216. $clerk_params = [
  217. 'where' => $where,
  218. 'order' => 'id desc',
  219. 'page' => $params['page'],
  220. 'limit' => $params['limit'],
  221. 'with' => ['clerkUser', 'user', 'detail']
  222. ];
  223. $list = HappinessClerkLog::listPage($clerk_params);
  224. if (!empty($list['list'])) {
  225. //获取门店商品
  226. $goods_list = HappinessStoreGoods::lists([
  227. 'where' => [
  228. ['mall_id' => $this->mall_id],
  229. ['store_id' => $this->store_id],
  230. ['status' => StatusEnum::ENABLED],
  231. ['is_on_sale' => 1],
  232. ],
  233. 'with' => ['good'],
  234. 'index' => 'goods_id'
  235. ]);
  236. foreach ($list['list'] as &$item) {
  237. foreach ($item['detail'] as $key => $value) {
  238. $item['detail'][$key]['cover_pic'] = $goods_list[$value['goods_id']]['good']['cover_pic'];
  239. }
  240. }
  241. }
  242. return $list;
  243. } catch (\Exception $e) {
  244. throw new Exception($e->getMessage());
  245. }
  246. }
  247. //获取核销记录详情
  248. public function clerkLogDetail($params)
  249. {
  250. try {
  251. $where = [
  252. 'mall_id' => $params['mall_id'],
  253. 'store_id' => $params['store_id'],
  254. 'id' => $params['id'],
  255. ];
  256. $info = HappinessClerkLog::find()->where($where)->with(['clerkUser', 'user', 'detail'])->asArray()->one();
  257. if (!empty($info)) {
  258. $goods_list = HappinessStoreGoods::lists([
  259. 'where' => [
  260. ['mall_id' => $params['mall_id']],
  261. ['store_id' => $params['store_id']],
  262. ['status' => StatusEnum::ENABLED],
  263. ['is_on_sale' => 1],
  264. ],
  265. 'with' => ['good'],
  266. 'index' => 'goods_id'
  267. ]);
  268. foreach ($info['detail'] as &$item) {
  269. $item['cover_pic'] = $goods_list[$item['goods_id']]['good']['cover_pic'];
  270. }
  271. }
  272. return $info;
  273. } catch (\Exception $e) {
  274. throw new Exception($e->getMessage());
  275. }
  276. }
  277. public function changeClerkStatus($params)
  278. {
  279. try {
  280. //查询核销员
  281. $clerk_info = HappinessClerk::getOne([
  282. ['mall_id' => $this->mall_id],
  283. ['store_id' => $this->store_id],
  284. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  285. ['id' => $params['id']],
  286. ]);
  287. if (empty($clerk_info)) throw new Exception('查无此核销员');
  288. $clerk_info->status = $params['status'];
  289. if (!$clerk_info->save()) throw new Exception('操作失败');
  290. return true;
  291. } catch (\Exception $e) {
  292. throw new Exception($e->getMessage());
  293. }
  294. }
  295. }