IndexController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace app\admin\controller\customer;
  3. use app\admin\validate\customer\IndexValidate;
  4. use app\controller\Curd;
  5. use app\model\Customer;
  6. use app\model\Member;
  7. use app\model\MemberAccount;
  8. use app\model\MemberCert;
  9. use app\model\MemberInfo;
  10. use app\model\SysRole;
  11. use app\model\SysSerial;
  12. use app\model\SysUser;
  13. use support\Db;
  14. use support\exception\BusinessException;
  15. use support\Request;
  16. use support\Response;
  17. use Tinywan\Jwt\JwtToken;
  18. class IndexController extends Curd
  19. {
  20. public function __construct()
  21. {
  22. $this->model = new Customer();
  23. $this->validate = true;
  24. $this->validateClass = new IndexValidate();
  25. }
  26. public function selectList(Request $request)
  27. {
  28. $keywords = $request->get('keywords');
  29. if (!$keywords) {
  30. return json_fail("查询错误");
  31. }
  32. $res = Db::table('customer')
  33. ->select('id','name','mobile')
  34. ->where(function ($query) use ($keywords) {
  35. $query->where('name', 'like', '%' . $keywords . '%')
  36. ->orWhere("mobile", 'like', '%' . $keywords . '%');
  37. })->get();
  38. $data = [];
  39. foreach ($res as $item) {
  40. $data[] = [
  41. 'key' => $item->id,
  42. 'value' => $item->name,
  43. ];
  44. }
  45. return json_success('', $data);
  46. }
  47. public function select(Request $request): Response
  48. {
  49. $userId = JwtToken::getCurrentId();
  50. $user = SysUser::find($userId);
  51. if (empty($user->join_user_role_id)) {
  52. return json_fail("请先关联角色");
  53. }
  54. $role = SysRole::find($user->join_user_role_id);
  55. $selfAndChildIds = SysRole::where('role_path', 'like', $role->role_path . "%")
  56. ->where('role_id', '<>', $role->role_id)
  57. ->pluck('role_id');
  58. $userIds = SysUser::whereIn('join_user_role_id', $selfAndChildIds)->pluck('user_id')->toArray();
  59. array_unshift($userIds, $userId);
  60. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  61. $order = $request->get('order', 'desc');
  62. $field = $field ?? 'created_at';
  63. $where['creator'] = ['in', $userIds];
  64. $query = $this->doSelect($where, $field, $order);
  65. return $this->doFormat($query, $format, $limit);
  66. }
  67. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  68. {
  69. $model = $this->model->with('user');
  70. foreach ($where as $column => $value) {
  71. if (is_array($value)) {
  72. if ($value[0] === 'like' || $value[0] === 'not like') {
  73. $model = $model->where($column, $value[0], "%$value[1]%");
  74. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  75. $model = $model->where($column, $value[0], $value[1]);
  76. } elseif ($value[0] == 'in' && !empty($value[1])) {
  77. $valArr = $value[1];
  78. if (is_string($value[1])) {
  79. $valArr = explode(",", trim($value[1]));
  80. }
  81. $model = $model->whereIn($column, $valArr);
  82. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  83. $valArr = $value[1];
  84. if (is_string($value[1])) {
  85. $valArr = explode(",", trim($value[1]));
  86. }
  87. $model = $model->whereNotIn($column, $valArr);
  88. } elseif ($value[0] == 'null') {
  89. $model = $model->whereNull($column);
  90. } elseif ($value[0] == 'not null') {
  91. $model = $model->whereNotNull($column);
  92. } elseif ($value[0] !== '' || $value[1] !== '') {
  93. $model = $model->whereBetween($column, $value);
  94. }
  95. } else {
  96. $model = $model->where($column, $value);
  97. }
  98. }
  99. if ($field) {
  100. $model = $model->orderBy($field, $order);
  101. }
  102. return $model;
  103. }
  104. protected function afterQuery($items)
  105. {
  106. foreach ($items as &$item) {
  107. $item->city = explode(',', $item->city);
  108. }
  109. return $items;
  110. }
  111. protected function insertInput(Request $request): array
  112. {
  113. $data = $this->inputFilter($request->post());
  114. $data['creator'] = JwtToken::getCurrentId();
  115. if (!empty($data['birth'])) {
  116. $data['birth'] = date('Y-m-d', strtotime($data['birth']));
  117. }
  118. return $data;
  119. }
  120. protected function updateInput(Request $request): array
  121. {
  122. $primary_key = $this->model->getKeyName();
  123. $id = $request->post($primary_key);
  124. $data = $this->inputFilter($request->post());
  125. if (!empty($data['birth'])) {
  126. $data['birth'] = date('Y-m-d', strtotime($data['birth']));
  127. }
  128. $model = $this->model->find($id);
  129. if (!$model) {
  130. throw new BusinessException('记录不存在', 2);
  131. }
  132. unset($data[$primary_key]);
  133. return [$id, $data];
  134. }
  135. public function conversion(Request $request)
  136. {
  137. $id = $request->post('id');
  138. $customer = Customer::where('id', $id)->first();
  139. if (!$customer) {
  140. return json_fail('客户不存在');
  141. }
  142. Db::beginTransaction();
  143. try {
  144. $memberId = "ME" . str_pad(SysSerial::getSerial(), 16, "0", STR_PAD_LEFT) . random_string(6, 'up');
  145. $member = [
  146. 'member_id' => $memberId,
  147. 'member_is_owner' => 'N',
  148. 'member_classify' => 'MEMBER',
  149. 'member_status' => 'ACTIVED',
  150. 'member_mobile' => $customer->mobile ?? '',
  151. // 'member_from' => 'CUSTOMER',
  152. 'member_addtimes' => time()
  153. ];
  154. $member['member_extend_json'] = json_encode(['come_from'=>'CUSTOMER']);
  155. // 归属门店
  156. $user = SysUser::where('user_id',$customer->creator)->first();
  157. $belong = [
  158. 'user_id'=>$customer->creator,
  159. 'premises' => $user->join_user_dept_id ?? '',
  160. ];
  161. $member['member_extend_json'] = json_encode(['belong'=>$belong]);
  162. Member::insert($member);
  163. // cert
  164. $cert = [
  165. 'join_cert_member_id' => $memberId,
  166. 'member_cert_name' => $customer->name,
  167. 'member_cert_nbr' => $customer->card_id ?? '',
  168. 'member_cert_birth' => $customer->birth ?? '',
  169. 'member_cert_gender' => $customer->gender ?? '',
  170. 'member_cert_province' => $customer->city ?? '',
  171. 'member_cert_addr' => $customer->address ?? ''
  172. ];
  173. MemberCert::insert($cert);
  174. // info
  175. $info = [
  176. 'join_info_member_id' => $memberId,
  177. 'member_info_nickname' => $customer->name
  178. ];
  179. MemberInfo::insert($info);
  180. $customer->status = '正式用户';
  181. $customer->save();
  182. // 账户表
  183. $accountData = [
  184. 'join_account_member_id' => $memberId,
  185. 'member_account_classify' => 'POINTS',
  186. 'member_account_status' => 'ACTIVED',
  187. 'member_account_category' => 'NORMAL',
  188. 'member_account_nbr' => $memberId . '-POINTS',
  189. 'member_account_name' => '积分账户',
  190. 'member_account_addtimes' => time()
  191. ];
  192. // 积分账户
  193. MemberAccount::insert($accountData);
  194. $accountData['member_account_classify'] = 'CASH';
  195. $accountData['member_account_nbr'] = $memberId . '-CASH';
  196. $accountData['member_account_name'] = '余额账户';
  197. // 现金/余额账户
  198. MemberAccount::insert($accountData);
  199. Db::commit();
  200. return json_success("转正式用户成功");
  201. } catch (\Exception $e) {
  202. dump($e->getMessage());
  203. Db::rollBack();
  204. return json_fail('转正式用户失败');
  205. }
  206. }
  207. }