IndexController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. $type = $request->get('type','');
  55. $role = SysRole::find($user->join_user_role_id);
  56. $selfAndChildIds = SysRole::where('role_path', 'like', $role->role_path . "%")
  57. ->where('role_id', '<>', $role->role_id)
  58. ->pluck('role_id');
  59. $userIds = SysUser::whereIn('join_user_role_id', $selfAndChildIds)->pluck('user_id')->toArray();
  60. array_unshift($userIds, $userId);
  61. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  62. $order = $request->get('order', 'desc');
  63. $field = $field ?? 'created_at';
  64. $where['creator'] = ['in', $userIds];
  65. if(!empty($type)){
  66. $where['created_at'] = [
  67. strtotime(date('Y-m-d').' 00:00:00'),
  68. strtotime(date('Y-m-d').' 23:59:59')
  69. ];
  70. }
  71. $query = $this->doSelect($where, $field, $order);
  72. return $this->doFormat($query, $format, $limit);
  73. }
  74. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  75. {
  76. $model = $this->model->with('user');
  77. foreach ($where as $column => $value) {
  78. if (is_array($value)) {
  79. if ($value[0] === 'like' || $value[0] === 'not like') {
  80. $model = $model->where($column, $value[0], "%$value[1]%");
  81. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  82. $model = $model->where($column, $value[0], $value[1]);
  83. } elseif ($value[0] == 'in' && !empty($value[1])) {
  84. $valArr = $value[1];
  85. if (is_string($value[1])) {
  86. $valArr = explode(",", trim($value[1]));
  87. }
  88. $model = $model->whereIn($column, $valArr);
  89. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  90. $valArr = $value[1];
  91. if (is_string($value[1])) {
  92. $valArr = explode(",", trim($value[1]));
  93. }
  94. $model = $model->whereNotIn($column, $valArr);
  95. } elseif ($value[0] == 'null') {
  96. $model = $model->whereNull($column);
  97. } elseif ($value[0] == 'not null') {
  98. $model = $model->whereNotNull($column);
  99. } elseif ($value[0] !== '' || $value[1] !== '') {
  100. $model = $model->whereBetween($column, $value);
  101. }
  102. } else {
  103. $model = $model->where($column, $value);
  104. }
  105. }
  106. if ($field) {
  107. $model = $model->orderBy($field, $order);
  108. }
  109. return $model;
  110. }
  111. protected function afterQuery($items)
  112. {
  113. foreach ($items as &$item) {
  114. $item->city = explode(',', $item->city);
  115. }
  116. return $items;
  117. }
  118. protected function insertInput(Request $request): array
  119. {
  120. $data = $this->inputFilter($request->post());
  121. $data['creator'] = JwtToken::getCurrentId();
  122. if (!empty($data['birth'])) {
  123. $data['birth'] = date('Y-m-d', strtotime($data['birth']));
  124. }
  125. return $data;
  126. }
  127. protected function updateInput(Request $request): array
  128. {
  129. $primary_key = $this->model->getKeyName();
  130. $id = $request->post($primary_key);
  131. $data = $this->inputFilter($request->post());
  132. if (!empty($data['birth'])) {
  133. $data['birth'] = date('Y-m-d', strtotime($data['birth']));
  134. }
  135. $model = $this->model->find($id);
  136. if (!$model) {
  137. throw new BusinessException('记录不存在', 2);
  138. }
  139. unset($data[$primary_key]);
  140. return [$id, $data];
  141. }
  142. public function conversion(Request $request)
  143. {
  144. $id = $request->post('id');
  145. $customer = Customer::where('id', $id)->first();
  146. if (!$customer) {
  147. return json_fail('客户不存在');
  148. }
  149. Db::beginTransaction();
  150. try {
  151. $memberId = "ME" . date("ymdHi") . random_string(4, 'up');
  152. $member = [
  153. 'member_id' => $memberId,
  154. 'member_is_owner' => 'N',
  155. 'member_classify' => 'MEMBER',
  156. 'member_status' => 'ACTIVED',
  157. 'member_mobile' => $customer->mobile ?? '',
  158. // 'member_from' => 'CUSTOMER',
  159. 'member_addtimes' => time()
  160. ];
  161. $member['member_extend_json'] = json_encode(['come_from'=>'CUSTOMER']);
  162. // 归属门店
  163. $user = SysUser::where('user_id',$customer->creator)->first();
  164. $belong = [
  165. 'user_id'=>$customer->creator,
  166. 'premises' => $user->join_user_dept_id ?? '',
  167. ];
  168. $member['member_extend_json'] = json_encode(['belong'=>$belong]);
  169. Member::insert($member);
  170. // cert
  171. $cert = [
  172. 'join_cert_member_id' => $memberId,
  173. 'member_cert_name' => $customer->name,
  174. 'member_cert_nbr' => $customer->card_id ?? '',
  175. 'member_cert_birth' => $customer->birth ?? '',
  176. 'member_cert_gender' => $customer->gender ?? '',
  177. 'member_cert_province' => $customer->city ?? '',
  178. 'member_cert_addr' => $customer->address ?? ''
  179. ];
  180. MemberCert::insert($cert);
  181. // info
  182. $info = [
  183. 'join_info_member_id' => $memberId,
  184. 'member_info_nickname' => $customer->name
  185. ];
  186. MemberInfo::insert($info);
  187. $customer->status = '正式用户';
  188. $customer->save();
  189. // 账户表
  190. $accountData = [
  191. 'join_account_member_id' => $memberId,
  192. 'member_account_classify' => 'POINTS',
  193. 'member_account_status' => 'ACTIVED',
  194. 'member_account_category' => 'NORMAL',
  195. 'member_account_nbr' => $memberId . '-POINTS',
  196. 'member_account_name' => '积分账户',
  197. 'member_account_addtimes' => time()
  198. ];
  199. // 积分账户
  200. MemberAccount::insert($accountData);
  201. $accountData['member_account_classify'] = 'CASH';
  202. $accountData['member_account_nbr'] = $memberId . '-CASH';
  203. $accountData['member_account_name'] = '余额账户';
  204. // 现金/余额账户
  205. MemberAccount::insert($accountData);
  206. Db::commit();
  207. return json_success("转正式用户成功");
  208. } catch (\Exception $e) {
  209. dump($e->getMessage());
  210. Db::rollBack();
  211. return json_fail('转正式用户失败');
  212. }
  213. }
  214. }