123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace app\oa\logic;
  3. use app\admin\model\user;
  4. use think\Db;
  5. class UserLogic
  6. {
  7. public function exp()
  8. {
  9. $contract_types = "1,2,3,4,5,6,7,8,9";
  10. $exp = new \think\Db\Expression('field(user.realname,' . $contract_types . '),convert(user.realname using gb2312) asc');
  11. return $exp;
  12. }
  13. /**
  14. * 通讯录列表
  15. * @param $param
  16. * @return array
  17. */
  18. public function getDataList($param)
  19. {
  20. $user_id = $param['user_id'];
  21. $search = $param['search'];
  22. $where = [];
  23. $initials_type = ($param['initials'] == 1) ? 1 : 2;
  24. $where['user.status'] = 1;
  25. if ($search) {
  26. $whereMap = function ($query) use ($search) {
  27. $query->where('user.realname', array('like', '%' . $search . '%'))
  28. ->whereOr('user.mobile', array('like', '%' . $search . '%'));
  29. };
  30. }
  31. if($param['structure_id']){
  32. $where['user.structure_id']= $param['structure_id'];
  33. }
  34. if (isset($param['star_type'])) {
  35. if ($param['star_type'] == 1) {
  36. $item = Db::name('crm_star')->where(['user_id'=>$user_id,'type'=>'admin_user'])->column('target_id');
  37. if(!empty($item)) {
  38. $where['user.id'] = ['in', $item];
  39. $list = Db::name('admin_user')
  40. ->alias('user')
  41. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  42. ->where($where)
  43. ->where($whereMap)
  44. ->field('user.id,user.thumb_img,user.mobile,user.realname,user.post,structure.name as structure_name,user.mobile')
  45. ->page($param['page'], $param['limit'])
  46. ->select();
  47. foreach ($list as $k => $v) {
  48. $starWhere = ['user_id' => $user_id, 'target_id' => $v['id'], 'type' => 'admin_user'];
  49. $star = Db::name('crm_star')->where($starWhere)->value('star_id');
  50. $list[$k]['thumb_img'] = $v['thumb_img'] ? getFullPath($v['thumb_img']) : '';
  51. $list[$k]['star'] = !empty($star) ? 1 : 0;
  52. }
  53. $dataCount = Db::name('admin_user')
  54. ->alias('user')
  55. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  56. ->where($where)
  57. ->count();
  58. $newarray = $this->groupByInitials($list, 'realname', $initials_type);
  59. }else{
  60. return false;
  61. }
  62. }
  63. } else {
  64. $list = Db::name('admin_user')
  65. ->alias('user')
  66. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  67. ->where($where)
  68. ->where($whereMap)
  69. ->field('user.id,user.thumb_img,user.mobile,user.realname,user.post,structure.name as structure_name')
  70. ->page($param['page'], $param['limit'])
  71. ->select();
  72. $dataCount = Db::name('admin_user')
  73. ->alias('user')
  74. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  75. ->where($where)
  76. ->count();
  77. foreach ($list as $k => $v) {
  78. $starWhere = ['user_id' => $user_id, 'target_id' => $v['id'], 'type' => 'admin_user'];
  79. $star = Db::name('crm_star')->where($starWhere)->value('star_id');
  80. $list[$k]['thumb_img'] = $v['thumb_img'] ? getFullPath($v['thumb_img']) : '';
  81. $list[$k]['star'] = !empty($star) ? 1 : 0;
  82. }
  83. $newarray = $this->groupByInitials($list, 'realname', $initials_type);
  84. }
  85. $data = [];
  86. $data['list'] = $newarray;
  87. $data['totalRow'] = $dataCount;
  88. return $data;
  89. }
  90. /**
  91. * 我关注的数据
  92. * @param $param
  93. * @return array
  94. */
  95. public function queryList($param)
  96. {
  97. $user_id = $param['user_id'];
  98. $search = $param['search'];
  99. $where = [];
  100. $where['user.status'] = 1;
  101. if ($search) {
  102. $where = function ($query) use ($search) {
  103. $query->where('user.realname', array('like', '%' . $search . '%'))
  104. ->whereOr('user.mobile', array('like', '%' . $search . '%'));
  105. };
  106. }
  107. $item = Db::name('crm_star')->where('user_id', $user_id)->column('target_id');
  108. if ($param['structure_id'] == '') {
  109. $list = Db::name('admin_user')
  110. ->alias('user')
  111. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  112. ->whereIn('user.id', $item)
  113. ->where($where)
  114. ->field('user.id,user.thumb_img,user.realname,user.post,structure.name as structure_name')
  115. ->orderRaw($this->exp())
  116. ->page($param['page'], $param['limit'])
  117. ->select();
  118. foreach ($list as $k => $v) {
  119. $starWhere = ['user_id' => $user_id, 'target_id' => $v['id'], 'type' => 'admin_user'];
  120. $star = Db::name('crm_star')->where($starWhere)->value('star_id');
  121. $list[$k]['star'] = !empty($star) ? 1 : 0;
  122. }
  123. } else {
  124. $list = Db::name('admin_user')
  125. ->alias('user')
  126. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  127. ->whereIn('user.id', $item)
  128. ->where($where)
  129. ->where('structure.id', $param['structure_id'])
  130. ->field('user.id,user.thumb_img,user.realname,user.post,structure.name as structure_name')
  131. ->orderRaw($this->exp())
  132. ->page($param['page'], $param['limit'])
  133. ->select();
  134. foreach ($list as $k => $v) {
  135. $starWhere = ['user_id' => $user_id, 'target_id' => $v['id'], 'type' => 'admin_user'];
  136. $star = Db::name('crm_star')->where($starWhere)->value('star_id');
  137. $list[$k]['star'] = !empty($star) ? 1 : 0;
  138. }
  139. }
  140. $data = [];
  141. $data['list'] = $list;
  142. return $data;
  143. }
  144. /**
  145. * 二维数组根据首字母分组排序
  146. * @param array $data 二维数组
  147. * @param string $targetKey 首字母的键名
  148. * @return array 根据首字母关联的二维数组
  149. */
  150. public function groupByInitials(array $data, $targetKey = 'name', $initials_type)
  151. {
  152. $data = array_map(function ($item) use ($targetKey) {
  153. return array_merge($item, [
  154. 'initials' => $this->getInitials($item[$targetKey]),
  155. ]);
  156. }, $data);
  157. $data = $this->sortInitials($data, $initials_type);
  158. $sortData = [];
  159. foreach ($data as $key => $value) {
  160. foreach ($value as $v) {
  161. $sortData[] = $v;
  162. }
  163. }
  164. return $sortData;
  165. }
  166. /**
  167. * 按字母排序
  168. * @param array $data
  169. * @return array
  170. */
  171. public function sortInitials(array $data, $initials_type)
  172. {
  173. $sortData = [];
  174. foreach ($data as $key => $value) {
  175. $sortData[$value['initials']][] = $value;
  176. }
  177. if ($initials_type == 1) {
  178. ksort($sortData, SORT_STRING);
  179. } else {
  180. krsort($sortData, SORT_STRING);
  181. }
  182. return $sortData;
  183. }
  184. /**
  185. * 获取首字母
  186. * @param string $str 汉字字符串
  187. * @return string 首字母
  188. */
  189. public function getInitials($str)
  190. {
  191. if (empty($str)) {
  192. return '';
  193. }
  194. $fchar = ord($str{0});
  195. if ($fchar >= ord('A') && $fchar <= ord('z')) {
  196. return strtoupper($str{0});
  197. }
  198. if (is_numeric($str)) {
  199. return $str{0};
  200. }
  201. $s1 = iconv('UTF-8', 'gb2312', $str);
  202. $s2 = iconv('gb2312', 'UTF-8', $s1);
  203. $s = $s2 == $str ? $s1 : $str;
  204. $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  205. if ($asc == -9300) {
  206. return 'G';
  207. }
  208. if ($asc >= -20319 && $asc <= -20284) {
  209. return 'A';
  210. }
  211. if ($asc >= -20283 && $asc <= -19776) {
  212. return 'B';
  213. }
  214. if ($asc >= -19775 && $asc <= -19219) {
  215. return 'C';
  216. }
  217. if ($asc >= -19218 && $asc <= -18711) {
  218. return 'D';
  219. }
  220. if ($asc >= -18710 && $asc <= -18527) {
  221. return 'E';
  222. }
  223. if ($asc >= -18526 && $asc <= -18240) {
  224. return 'F';
  225. }
  226. if ($asc >= -18239 && $asc <= -17923) {
  227. return 'G';
  228. }
  229. if ($asc >= -17922 && $asc <= -17418) {
  230. return 'H';
  231. }
  232. if ($asc >= -17417 && $asc <= -16475) {
  233. return 'J';
  234. }
  235. if ($asc >= -16474 && $asc <= -16213) {
  236. return 'K';
  237. }
  238. if ($asc >= -16212 && $asc <= -15641) {
  239. return 'L';
  240. }
  241. if ($asc >= -15640 && $asc <= -15166) {
  242. return 'M';
  243. }
  244. if ($asc >= -15165 && $asc <= -14923) {
  245. return 'N';
  246. }
  247. if ($asc >= -14922 && $asc <= -14915) {
  248. return 'O';
  249. }
  250. if ($asc >= -14914 && $asc <= -14631) {
  251. return 'P';
  252. }
  253. if ($asc >= -14630 && $asc <= -14150) {
  254. return 'Q';
  255. }
  256. if ($asc >= -14149 && $asc <= -14091) {
  257. return 'R';
  258. }
  259. if ($asc >= -14090 && $asc <= -13319) {
  260. return 'S';
  261. }
  262. if ($asc >= -13318 && $asc <= -12839) {
  263. return 'T';
  264. }
  265. if ($asc >= -12838 && $asc <= -12557) {
  266. return 'W';
  267. }
  268. if ($asc >= -12556 && $asc <= -11848) {
  269. return 'X';
  270. }
  271. if ($asc >= -11847 && $asc <= -11056) {
  272. return 'Y';
  273. }
  274. if ($asc >= -11055 && $asc <= -10247) {
  275. return 'Z';
  276. }
  277. if ($asc >= -10247 && $asc <= -10247) {
  278. return 'Z';
  279. }
  280. }
  281. }