1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 自定义字段
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\model;
  8. use app\admin\controller\ApiCommon;
  9. use think\Config;
  10. use think\Db;
  11. use think\Model;
  12. use think\Request;
  13. use think\Validate;
  14. class Field extends Model
  15. {
  16. /**
  17. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  18. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  19. */
  20. protected $name = 'admin_field';
  21. protected $createTime = 'create_time';
  22. protected $updateTime = 'update_time';
  23. protected $autoWriteTimestamp = true;
  24. private $tableName = ''; //表名
  25. private $queryStr = ''; //sql语句
  26. private $__db_prefix; //数据库表前缀
  27. private $types_arr = ['crm_leads', 'crm_customer', 'crm_contacts', 'crm_product', 'crm_business', 'crm_contract', 'oa_examine', 'hrm_parroll', 'admin_user', 'crm_receivables', 'crm_receivables_plan', 'crm_invoice', 'crm_visit']; //支持自定义字段的表,不包含表前缀
  28. private $formtype_arr = ['text', 'textarea', 'mobile', 'email', 'number', 'floatnumber', 'radio', 'select', 'checkbox', 'date', 'datetime', 'address', 'user', 'file', 'structure'];
  29. protected $type = [
  30. 'form_value' => 'array',
  31. ];
  32. /**
  33. * 列表展示额外关联字段
  34. */
  35. public $orther_field_list = [
  36. 'crm_leads' => [
  37. [
  38. 'field' => 'last_record',
  39. 'name' => '最后跟进记录',
  40. 'form_type' => 'text',
  41. 'width' => '',
  42. 'is_hidden' => 0,
  43. ],
  44. [
  45. 'field' => 'last_time',
  46. 'name' => '最后跟进时间',
  47. 'form_type' => 'datetime',
  48. 'width' => '',
  49. 'is_hidden' => 0,
  50. ],
  51. ],
  52. 'crm_customer' => [
  53. [
  54. 'field' => 'last_record',
  55. 'name' => '跟进记录',
  56. 'form_type' => 'text',
  57. 'width' => '',
  58. 'is_hidden' => 0,
  59. ],
  60. [
  61. 'field' => 'last_time',
  62. 'name' => '最后跟进时间',
  63. 'form_type' => 'datetime',
  64. 'width' => '',
  65. 'is_hidden' => 0,
  66. ],
  67. [
  68. 'field' => 'address',
  69. 'name' => '省、市、区/县',
  70. 'form_type' => 'customer_address',
  71. 'width' => '',
  72. 'is_hidden' => 0,
  73. ],
  74. [
  75. 'field' => 'detail_address',
  76. 'name' => '详细地址',
  77. 'form_type' => 'text',
  78. 'width' => '',
  79. 'is_hidden' => 0,
  80. ]
  81. ],
  82. 'crm_contacts' => [
  83. [
  84. 'field' => 'last_record',
  85. 'name' => '跟进记录',
  86. 'form_type' => 'text',
  87. 'width' => '',
  88. 'is_hidden' => 0,
  89. ],
  90. [
  91. 'field' => 'last_time',
  92. 'name' => '最后跟进时间',
  93. 'form_type' => 'datetime',
  94. 'width' => '',
  95. 'is_hidden' => 0,
  96. ],
  97. ],
  98. 'crm_business' => [
  99. [
  100. 'field' => 'last_record',
  101. 'name' => '跟进记录',
  102. 'form_type' => 'text',
  103. 'width' => '',
  104. 'is_hidden' => 0,
  105. ],
  106. [
  107. 'field' => 'last_time',
  108. 'name' => '最后跟进时间',
  109. 'form_type' => 'datetime',
  110. 'width' => '',
  111. 'is_hidden' => 0,
  112. ],
  113. ],
  114. 'crm_contract' => [
  115. [
  116. 'field' => 'check_status',
  117. 'name' => '审核状态',
  118. 'form_type' => 'text',
  119. 'width' => '',
  120. 'is_hidden' => 0,
  121. ],
  122. [
  123. 'field' => 'last_record',
  124. 'name' => '跟进记录',
  125. 'form_type' => 'text',
  126. 'width' => '',
  127. 'is_hidden' => 0,
  128. ],
  129. [
  130. 'field' => 'last_time',
  131. 'name' => '最后跟进时间',
  132. 'form_type' => 'datetime',
  133. 'width' => '',
  134. 'is_hidden' => 0,
  135. ],
  136. [
  137. 'field' => 'done_money',
  138. 'name' => '已回款',
  139. 'form_type' => 'floatnumber',
  140. 'width' => '',
  141. 'is_hidden' => 0,
  142. ],
  143. [
  144. 'field' => 'un_money',
  145. 'name' => '未回款',
  146. 'form_type' => 'floatnumber',
  147. 'width' => '',
  148. 'is_hidden' => 0,
  149. ]
  150. ],
  151. 'crm_receivables' => [
  152. [
  153. 'field' => 'check_status',
  154. 'name' => '审核状态',
  155. 'form_type' => 'text',
  156. 'width' => '',
  157. 'is_hidden' => 0,
  158. ],
  159. [
  160. 'field' => 'contract_money',
  161. 'name' => '合同金额',
  162. 'form_type' => 'floatnumber',
  163. 'width' => '',
  164. 'is_hidden' => 0,
  165. ]
  166. ]
  167. ];
  168. protected function initialize()
  169. {
  170. $this->__db_prefix = Config::get('database.prefix');
  171. }
  172. /**
  173. * [getDataList 获取列表]
  174. * @param types 分类
  175. * @return [array]
  176. * @author Michael_xu
  177. */
  178. public function getDataList($param)
  179. {
  180. $types = trim($param['types']);
  181. if (!in_array($types, $this->types_arr)) {
  182. $this->error = '参数错误';
  183. return false;
  184. }
  185. $map = $param;
  186. if ($types == 'oa_examine') {
  187. $map['types_id'] = $param['types_id'];
  188. }
  189. if ($param['types'] == 'crm_customer') {
  190. $map['field'] = array('not in', ['deal_status']);
  191. }
  192. $list = Db::name('AdminField')->where($map)->order('order_id')->select();
  193. foreach ($list as $k => $v) {
  194. $list[$k]['setting'] = $v['setting'] ? explode(chr(10), $v['setting']) : [];
  195. if ($v['form_type'] == 'checkbox') {
  196. $list[$k]['default_value'] = $v['default_value'] ? explode(',', $v['default_value']) : array();
  197. }
  198. }
  199. return $list ?: [];
  200. }
  201. /**
  202. * [createData 创建自定义字段]
  203. * @param types 分类
  204. * @param field 字段名
  205. * @param name 字段标识名(字段注释)
  206. * @param form_type 字段类型
  207. * @param max_length 字段最大长度
  208. * @param default_value 默认值
  209. * @param setting 单选、下拉、多选类型的选项值
  210. * @return [array]
  211. * @author Michael_xu
  212. */
  213. public function createData($types, $param)
  214. {
  215. if (!$types || !in_array($types, $this->types_arr) || !is_array($param)) {
  216. $this->error = '参数错误';
  217. return false;
  218. }
  219. # 公海数据
  220. $poolList = [];
  221. $poolData = [];
  222. if ($types == 'crm_customer') {
  223. $poolList = db('crm_customer_pool')->column('pool_id');
  224. }
  225. # 用户自定义字段
  226. $userFields = db('admin_user_field')->field(['id', 'datas'])->where('types', $types)->select();
  227. $error_message = [];
  228. $i = 0;
  229. foreach ($param as $k => $data) {
  230. $i++;
  231. $data['types'] = $types;
  232. if ($types == 'oa_examine' && !$data['types_id']) {
  233. $error_message[] = $data['name'] . '参数错误';
  234. }
  235. $data['types_id'] = $data['types_id'] ?: 0;
  236. if (!in_array($data['form_type'], $this->formtype_arr)) {
  237. $error_message[] = $data['name'] . ',字段类型错误';
  238. }
  239. //生成字段名
  240. if (!$data['field']) $data['field'] = $this->createField($types);
  241. $rule = [
  242. 'field' => ['regex' => '/^[a-z]([a-z]|_)+[a-z]$/i'],
  243. 'name' => 'require',
  244. 'types' => 'require',
  245. 'form_type' => 'require',
  246. ];
  247. $msg = [
  248. 'field.regex' => '字段名称格式不正确!',
  249. 'name.require' => '字段标识必须填写',
  250. 'types.require' => '分类必须填写',
  251. 'form_type.require' => '字段类型必须填写',
  252. ];
  253. // 验证
  254. // $validate = validate($this->name);
  255. $validate = new Validate($rule, $msg);
  256. if (!$validate->check($data)) {
  257. $error_message[] = $validate->getError();
  258. } else {
  259. //单选、下拉、多选类型(使用回车符隔开)
  260. if (in_array($data['form_type'], ['radio', 'select', 'checkbox']) && $data['setting']) {
  261. $data = $this->settingValue($data);
  262. }
  263. //表格类型
  264. if ($data['form_type'] == 'form' && $data['form_value']) {
  265. $new_form_value = [];
  266. foreach ($data['form_value'] as $form => $fromVal) {
  267. $fromVal['field'] = 'form_' . $this->createField($types);
  268. if (in_array($fromVal['form_type'], ['radio', 'select', 'checkbox']) && $fromVal['setting']) {
  269. $fromVal = $this->settingValue($fromVal);
  270. }
  271. }
  272. $new_form_value = $fromVal;
  273. $data['form_value'] = $new_form_value;
  274. }
  275. unset($data['field_id']);
  276. if ($i > 1) {
  277. $resField = $this->data($data)->allowField(true)->isUpdate(false)->save();
  278. } else {
  279. $resField = $this->data($data)->allowField(true)->save();
  280. }
  281. # 处理公海字段数据
  282. if ($types == 'crm_customer') {
  283. foreach ($poolList AS $k1 => $poolId) {
  284. $poolData[] = [
  285. 'pool_id' => $poolId,
  286. 'name' => $data['name'],
  287. 'field_name' => $data['field'],
  288. 'form_type' => $data['form_type'],
  289. 'is_hidden' => 1
  290. ];
  291. }
  292. }
  293. if ($types !== 'oa_examine') {
  294. if ($resField) {
  295. $this->tableName = $types;
  296. $maxlength = '255';
  297. $defaultvalue = $data['default_value'] ? "DEFAULT '" . $data['default_value'] . "'" : "DEFAULT NULL";
  298. //根据字段类型,创建字段
  299. switch ($data['form_type']) {
  300. case 'address' :
  301. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '" . $data['name'] . "'";
  302. break;
  303. case 'radio' :
  304. case 'select' :
  305. case 'checkbox' :
  306. $defaultvalue = $data['default_value'] ? "DEFAULT '" . $data['default_value'] . "'" : '';
  307. $maxlength = 500;
  308. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR( " . $maxlength . " ) CHARACTER SET utf8 COLLATE utf8_general_ci " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  309. break;
  310. case 'textarea' :
  311. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` TEXT COMMENT '" . $data['name'] . "'";
  312. break;
  313. case 'number' :
  314. $defaultvalue = abs(intval($data['default_value'])) > 2147483647 ? 2147483647 : intval($data['default_value']);
  315. $maxlength = 11;
  316. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` int ( " . $maxlength . " ) DEFAULT '" . $defaultvalue . "' COMMENT '" . $data['name'] . "'";
  317. break;
  318. case 'floatnumber' :
  319. $defaultvalue = abs(intval($data['default_value'])) > 9999999999999999.99 ? 9999999999999999.99 : intval($data['default_value']);
  320. $maxlength = 18;
  321. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` decimal (" . $maxlength . ",2) DEFAULT '" . $defaultvalue . "' COMMENT '" . $data['name'] . "'";
  322. break;
  323. case 'date' :
  324. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` DATE " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  325. break;
  326. case 'datetime' :
  327. $defaultvalue = $data['default_value'] ? "DEFAULT '" . strtotime($data['default_value']) . "'" : '';
  328. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` int (11) " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  329. break;
  330. case 'file' :
  331. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR ( " . $maxlength . " ) CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT '" . $data['name'] . "'";
  332. break;
  333. case 'form' :
  334. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT '" . $data['name'] . "'";
  335. break;
  336. default :
  337. $maxlength = 255;
  338. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR( " . $maxlength . " ) CHARACTER SET utf8 COLLATE utf8_general_ci " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  339. break;
  340. }
  341. $resData = Db::execute($this->queryStr);
  342. if ($resData === false) {
  343. $this->where(['field_id' => $this->field_id])->delete();
  344. $error_message[] = $data['name'] . ',添加失败';
  345. }
  346. # 处理用户自定义字段数据
  347. if ($resData !== false && !empty($userFields)) {
  348. foreach ($userFields AS $key => $value) {
  349. $userFields[$key]['datas'] = json_decode($value['datas'], true);
  350. $userFields[$key]['datas'][$data['field']] = ['width' => '', 'is_hide' => 0];
  351. $userFields[$key]['datas'] = json_encode($userFields[$key]['datas']);
  352. }
  353. }
  354. } else {
  355. $error_message[] = $data['name'] . ',添加失败';
  356. }
  357. }
  358. }
  359. }
  360. # 更新用户自定义字段
  361. if (!empty($userFields)) {
  362. foreach ($userFields AS $key => $value) {
  363. db('admin_user_field')->where('id', $value['id'])->update(['datas' => $value['datas']]);
  364. }
  365. }
  366. # 更新公海字段
  367. if (!empty($poolData)) {
  368. db('crm_customer_pool_field_setting')->insertAll($poolData);
  369. }
  370. if ($error_message) {
  371. $this->error = implode(';', $error_message);
  372. return false;
  373. }
  374. return true;
  375. }
  376. /**
  377. * [settingValue 单选、下拉、多选值]
  378. * @return [array]
  379. * @author Michael_xu
  380. */
  381. public function settingValue($data)
  382. {
  383. //将英文逗号转换为中文逗号
  384. $new_setting = [];
  385. foreach ($data['setting'] as $k => $v) {
  386. $v = str_replace(')', ')', $v);
  387. $v = str_replace('(', '(', $v);
  388. $new_setting[] = str_replace(',', ',', $v);
  389. }
  390. $data['setting'] = implode(chr(10), $new_setting);
  391. //默认值
  392. $new_default_value = [];
  393. if ($data['default_value'] && $data['form_type'] == 'checkbox') {
  394. foreach ($data['default_value'] as $k => $v) {
  395. $new_default_value[] = str_replace(',', ',', $v);
  396. }
  397. $data['default_value'] = implode(',', $new_default_value);
  398. }
  399. return $data;
  400. }
  401. /**
  402. * [updateDataById 編輯自定义字段]
  403. * @param types 分类
  404. * @param field 字段名
  405. * @param name 字段标识名(字段注释)
  406. * @param form_type 字段类型
  407. * @param max_length 字段最大长度
  408. * @param default_value 默认值
  409. * @return [array]
  410. * @author Michael_xu
  411. */
  412. public function updateDataById($param, $types = '')
  413. {
  414. $error_message = [];
  415. if (!is_array($param)) {
  416. $this->error = '参数错误';
  417. return false;
  418. }
  419. # 查询老数据
  420. $oldData = [];
  421. if (!empty($types) && $types == 'crm_customer') {
  422. $oldList = db('admin_field')->field(['field', 'name'])->where('types', $types)->select();
  423. foreach ($oldList AS $key => $value) {
  424. $oldData[$value['field']] = $value['name'];
  425. }
  426. }
  427. $i = 0;
  428. foreach ($param as $data) {
  429. $i++;
  430. $field_id = intval($data['field_id']);
  431. if (!$field_id) {
  432. $error_message[] = $data['name'] . ',参数错误';
  433. }
  434. $dataInfo = $this->get($field_id);
  435. if (!$dataInfo) {
  436. $error_message[] = $data['name'] . '参数错误';
  437. }
  438. // $error_message[] = $data['name'].',该字段不能编辑';
  439. $data['types'] = $dataInfo['types'];
  440. //单选、下拉、多选类型(使用回车符隔开)
  441. if (in_array($data['form_type'], ['radio', 'select', 'checkbox']) && $data['setting']) {
  442. //将英文逗号转换为中文逗号
  443. $data = $this->settingValue($data);
  444. }
  445. // 验证
  446. $validate = validate($this->name);
  447. if (!$validate->check($data)) {
  448. $error_message[] = $validate->getError();
  449. } else {
  450. // unset($data['field']);
  451. $data['field'] = $dataInfo['field'];
  452. unset($data['operating']);
  453. $box_form_type = array('checkbox', 'select', 'radio');
  454. if ((in_array($dataInfo['form_type'], $box_form_type) && !in_array($data['form_type'], $box_form_type)) || !in_array($dataInfo['form_type'], $box_form_type)) {
  455. unset($data['form_type']);
  456. }
  457. // $resField = $this->allowField(true)->save($data, ['field_id' => $field_id]);
  458. unset($data['showSetting']);
  459. unset($data['componentName']);
  460. unset($data['is_deleted']);
  461. $data['update_time'] = time();
  462. $resField = db('admin_field')->where(['field_id' => $field_id])->update($data);
  463. if ($dataInfo['types'] !== 'oa_examine') {
  464. if ($resField) {
  465. # 更新公海字段
  466. if (!empty($oldData[$data['field']]) && $data['name'] != $oldData[$data['field']]['name']) {
  467. db('crm_customer_pool_field_setting')->where('field_name', $data['field'])->update(['name' => $data['name']]);
  468. }
  469. //actionLog($field_id); //操作日志
  470. $this->tableName = $dataInfo['types'];
  471. $maxlength = '255';
  472. $defaultvalue = $data['default_value'] ? "DEFAULT '" . $data['default_value'] . "'" : "DEFAULT NULL";
  473. //根据字段类型,创建字段
  474. switch ($dataInfo['form_type']) {
  475. case 'address' :
  476. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` VARCHAR( 500 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '" . $data['name'] . "'";
  477. break;
  478. case 'radio' :
  479. case 'select' :
  480. case 'checkbox' :
  481. $defaultvalue = $data['default_value'] ? "DEFAULT '" . $data['default_value'] . "'" : '';
  482. $maxlength = 500;
  483. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` VARCHAR( " . $maxlength . " ) CHARACTER SET utf8 COLLATE utf8_general_ci " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  484. break;
  485. case 'textarea' :
  486. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` TEXT COMMENT '" . $data['name'] . "'";
  487. break;
  488. case 'number' :
  489. $defaultvalue = abs(intval($data['default_value'])) > 2147483647 ? 2147483647 : intval($data['default_value']);
  490. $maxlength = 11;
  491. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` int ( " . $maxlength . " ) DEFAULT '" . $defaultvalue . "' COMMENT '" . $data['name'] . "'";
  492. break;
  493. case 'floatnumber' :
  494. $defaultvalue = abs(intval($data['default_value'])) > 9999999999999999.99 ? 9999999999999999.99 : intval($data['default_value']);
  495. $maxlength = 18;
  496. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` decimal (" . $maxlength . ",2) DEFAULT '" . $defaultvalue . "' COMMENT '" . $data['name'] . "'";
  497. break;
  498. case 'date' :
  499. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` DATE " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  500. break;
  501. case 'datetime' :
  502. $defaultvalue = $data['default_value'] ? "DEFAULT '" . strtotime($data['default_value']) . "'" : '';
  503. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` int (11) " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  504. break;
  505. case 'file' :
  506. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` VARCHAR ( " . $maxlength . " ) CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT '" . $data['name'] . "' ";
  507. break;
  508. default :
  509. $maxlength = 255;
  510. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` VARCHAR( " . $maxlength . " ) CHARACTER SET utf8 COLLATE utf8_general_ci " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  511. break;
  512. }
  513. $resData = Db::execute($this->queryStr);
  514. if ($resData === false) {
  515. $error_message[] = $data['name'] . ',修改失败';
  516. }
  517. } else {
  518. $error_message[] = $data['name'] . ',修改失败';
  519. }
  520. }
  521. }
  522. }
  523. if ($error_message) {
  524. $this->error = implode(';', $error_message);
  525. return false;
  526. }
  527. return true;
  528. }
  529. /**
  530. * [delDataById 删除自定义字段] 删除逻辑数据不可恢复,谨慎操作
  531. * @param $id [array] 字段ID
  532. * @param $types 分类
  533. * @author Michael_xu
  534. */
  535. public function delDataById($ids, $types = '')
  536. {
  537. if (!is_array($ids)) {
  538. $ids[] = $ids;
  539. }
  540. # 删除公海字段的条件
  541. $poolWhere = [];
  542. $delMessage = [];
  543. foreach ($ids as $id) {
  544. $dataInfo = [];
  545. $dataInfo = $this->get($id);
  546. if ($dataInfo) {
  547. //operating : 0改删,1改,2删,3无
  548. if (in_array($dataInfo['operating'], ['1', '3'])) {
  549. $delMessage[] = $dataInfo['name'] . ',系统字段,不能删除';
  550. } else {
  551. $resDel = $this->where(['field_id' => $id])->delete(); //删除自定义字段信息
  552. if ($resDel && $dataInfo['types'] !== 'oa_examine') {
  553. $this->tableName = $dataInfo['types'];
  554. if ($dataInfo['form_type'] == 'img') {
  555. //图片类型需删除两个字段
  556. // $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` DROP `".$dataInfo['field']."`,"." DROP `thumb_".$dataInfo['field']."`";
  557. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` DROP `" . $dataInfo['field'] . "`";
  558. } else {
  559. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` DROP `" . $dataInfo['field'] . "`";
  560. }
  561. $resData = Db::execute($this->queryStr); //删除表字段数据
  562. if (!$resData) {
  563. $delMessage[] = $dataInfo['name'] . ',删除失败';
  564. }
  565. //删除列表字段配置数据
  566. $userFieldList = db('admin_user_field')->where(['types' => $dataInfo['types']])->select();
  567. foreach ($userFieldList as $key => $val) {
  568. $datas = [];
  569. $datas = json_decode($val['datas'], true);
  570. if ($datas) {
  571. foreach ($datas as $k => $v) {
  572. $datas[$k]['field'] = $k;
  573. unset($datas[$dataInfo['field']]);
  574. }
  575. $dataUserField = [];
  576. $dataUserField['value'] = $datas;
  577. $dataUserField['hide_value'] = [];
  578. $resUserField = model('UserField')->updateConfig($dataInfo['types'], $dataUserField, $val['id']);
  579. }
  580. }
  581. //删除场景字段数据
  582. $sceneFieldList = db('admin_scene')->where(['types' => $dataInfo['types']])->select();
  583. foreach ($sceneFieldList as $key => $val) {
  584. $data = [];
  585. $data = json_decode($val['data'], true);
  586. if ($data) {
  587. foreach ($data as $k => $v) {
  588. unset($data[$dataInfo['field']]);
  589. }
  590. $data = $data ?: [];
  591. $sceneModel = new \app\admin\model\Scene();
  592. $resScene = $sceneModel->updateData($data, $val['scene_id']);
  593. }
  594. }
  595. # 处理删除公海字段的条件
  596. if (!empty($types) && $types == 'crm_customer' && !empty($dataInfo['field'])) $poolWhere[] = $dataInfo['field'];
  597. } else {
  598. $delMessage[] = $dataInfo['name'] . ',删除失败';
  599. }
  600. }
  601. }
  602. }
  603. # 删除公海字段
  604. if (!empty($poolWhere)) {
  605. db('crm_customer_pool_field_setting')->whereIn('field_name', $poolWhere)->delete();
  606. }
  607. return $delMessage ? implode(';', $delMessage) : '';
  608. }
  609. /**
  610. * [createField 随机生成自定义字段名]
  611. * @param $field_str 字段名前缀
  612. * @param $types 分类
  613. * @author Michael_xu
  614. */
  615. public function createField($types = '', $field_str = 'crm_')
  616. {
  617. for ($i = 1; $i <= 6; $i++) {
  618. $field_str .= chr(rand(97, 122));
  619. }
  620. //验证字段名是否已存在
  621. if ($this->where(['types' => $types, 'field' => $field_str])->find()) {
  622. $this->createField($types);
  623. }
  624. return $field_str;
  625. }
  626. /**
  627. * [field 获取自定义字段信息]
  628. * @param $types 分类
  629. * @param $dataInfo 数据展示
  630. * @param $map 查询条件
  631. * @param form_type 字段类型 (’text’,’textarea’,’mobile’,’email’等)
  632. * @param default_value 默认值
  633. * @param max_length 输入最大长度
  634. * @param is_unique 1时,唯一性验证
  635. * @param is_null 1时,必填
  636. * @param input_tips 输入框提示内容
  637. * @param setting 设置 (单选、下拉、多选的选项值,使用回车分隔)
  638. * @author Michael_xu
  639. */
  640. public function field($param, $dataInfo = [])
  641. {
  642. $apiCommon = new ApiCommon();
  643. $userModel = new \app\admin\model\User();
  644. $structureModel = new \app\admin\model\Structure();
  645. $fileModel = new \app\admin\model\File();
  646. $user_id = !empty($param['user_id']) ? $param['user_id'] : $apiCommon->userInfo['id'];
  647. $types = $param['types'];
  648. $types_id = $param['types_id'] ?: 0;
  649. $grantData = getFieldGrantData($user_id);
  650. $userLevel = isSuperAdministrators($user_id);
  651. if ($types == 'crm_customer_pool') $types = 'crm_customer';
  652. $map = $param['map'] ?: [];
  653. if (!in_array($types, $this->types_arr)) {
  654. $this->error = '参数错误';
  655. return false;
  656. }
  657. if ($types == 'oa_examine' && !$types_id) {
  658. $this->error = '参数错误';
  659. return false;
  660. } elseif ($types == 'admin_user') {
  661. return User::$import_field_list;
  662. }
  663. if (in_array($param['action'], array('index', 'view'))) {
  664. $map['types'] = array(array('eq', $types), array('eq', ''), 'or');
  665. } else {
  666. if ($param['types'] == 'crm_customer' && (in_array($param['action'], array('save', 'update', 'excel')))) {
  667. $map['field'] = array('not in', ['deal_status']);
  668. }
  669. $map['types'] = $types;
  670. }
  671. if ($param['controller'] == 'customer' && $param['action'] == 'pool') {
  672. $map['field'] = array('not in', ['owner_user_id']);
  673. $types = 'crm_customer_pool';
  674. }
  675. if ($param['action'] == 'excel') {
  676. $map['form_type'] = array('not in', ['file', 'form','deal_status']);//删除了过滤structure user字段类型数据 添加deal_status
  677. } elseif ($param['action'] == 'index') {
  678. $map['form_type'] = array('not in', ['file', 'form']);
  679. }
  680. $map['types_id'] = $types_id;
  681. $order = 'order_id asc, field_id asc';
  682. if ($param['action'] == 'index' || $param['action'] == 'pool') {
  683. $field_list = $this->getIndexFieldConfig($types, $param['user_id']);
  684. foreach ($field_list as $k => $v) {
  685. # 处理字段授权
  686. $field_list[$k]['writeStatus'] = 1;
  687. if (!$userLevel && $param['module'] == 'crm' && !empty($grantData[$param['types']])) {
  688. $status = getFieldGrantStatus($v['field'], $grantData[$param['types']]);
  689. # 查看权限
  690. if ($status['read'] == 0) {
  691. unset($field_list[(int)$k]);
  692. continue;
  693. }
  694. # 编辑权限
  695. $field_list[$k]['writeStatus'] = $status['write'];
  696. }
  697. }
  698. # 客户模块增加锁定状态、距进入公海天数字段
  699. if (!$userLevel && $param['types'] == 'crm_customer' && !empty($grantData[$param['types']])) {
  700. $poolDayStatus = getFieldGrantStatus('pool_day', $grantData[$param['types']]);
  701. if (!empty($poolDayStatus['read'])) {
  702. $field_list[] = [
  703. 'field' => 'pool_day',
  704. 'name' => '距进入公海天数',
  705. 'form_type' => 'text',
  706. 'writeStatus' => 0,
  707. 'fieldName' => 'pool_day'
  708. ];
  709. }
  710. $isLockStatus = getFieldGrantStatus('is_lock', $grantData[$param['types']]);
  711. if (!empty($isLockStatus['read'])) {
  712. $field_list[] = [
  713. 'field' => "is_lock",
  714. 'fieldName' => "is_lock",
  715. 'form_type' => "text",
  716. 'name' => "锁定状态",
  717. 'writeStatus' => 0
  718. ];
  719. }
  720. }
  721. if ($userLevel) {
  722. $field_list[] = [
  723. 'field' => 'pool_day',
  724. 'name' => '距进入公海天数',
  725. 'form_type' => 'text',
  726. 'writeStatus' => 0,
  727. 'fieldName' => 'pool_day'
  728. ];
  729. $field_list[] = [
  730. 'field' => "is_lock",
  731. 'fieldName' => "is_lock",
  732. 'form_type' => "text",
  733. 'name' => "锁定状态",
  734. 'writeStatus' => 0
  735. ];
  736. }
  737. } else {
  738. $field_list = db('admin_field')->where($map)->where( 'is_hidden',0)->field('field,types,name,form_type,default_value,is_unique,is_null,input_tips,setting,is_hidden')->order($order)->select();
  739. # 详情页面增加负责人字段
  740. if ($param['action'] == 'read' && !in_array($param['types'], ['crm_visit', 'crm_product','oa_examine'])) {
  741. $field_list[] = [
  742. 'field' => 'owner_user_id',
  743. 'name' => '负责人',
  744. 'form_type' => 'user',
  745. 'writeStatus' => 0,
  746. 'fieldName' => 'owner_user_name',
  747. 'value' => $dataInfo['owner_user_name'],
  748. ];
  749. }
  750. //客户
  751. if (in_array($param['types'], ['crm_customer'])) {
  752. $new_field_list[] = [
  753. 'field' => 'customer_address',
  754. 'name' => '地区定位',
  755. 'form_type' => 'map_address',
  756. 'default_value' => '',
  757. 'is_unique' => 0,
  758. 'is_null' => 0,
  759. 'input_tips' => '',
  760. 'setting' => [],
  761. 'value' => []
  762. ];
  763. }
  764. //商机、合同下产品
  765. if (in_array($param['types'], ['crm_business', 'crm_contract'])) {
  766. $new_field_list[] = [
  767. 'field' => 'product',
  768. 'name' => '产品',
  769. 'form_type' => 'product',
  770. 'default_value' => '',
  771. 'is_unique' => 0,
  772. 'is_null' => 0,
  773. 'input_tips' => '',
  774. 'setting' => [],
  775. 'value' => []
  776. ];
  777. }
  778. # 产品基本信息增加负责人信息
  779. if ($param['action'] == 'read' && $param['types'] == 'crm_product') {
  780. $new_field_list[] = db('admin_field')->where(['types_id' => 0, 'field' => 'owner_user_id'])->find();
  781. }
  782. if ($new_field_list) $field_list = array_merge(collection($field_list)->toArray(), $new_field_list);
  783. foreach ($field_list as $k => $v) {
  784. # 处理字段授权
  785. $field_list[$k]['writeStatus'] = 1;
  786. if (!$userLevel && $param['module'] == 'crm' && !empty($grantData[$param['types']])) {
  787. $status = getFieldGrantStatus($v['field'], $grantData[$param['types']]);
  788. # 查看权限
  789. if (empty($status['read'])) {
  790. unset($field_list[(int)$k]);
  791. continue;
  792. }
  793. # 编辑权限
  794. if ($param['action'] != 'save') $field_list[$k]['writeStatus'] = $status['write'];
  795. }
  796. # (联系人,商机,合同,回款,回访)关联其他模块的字段在详情页面不允许修改;创建人、负责人不允许修改
  797. if ($param['action'] == 'read' && in_array($v['field'], ['customer_id', 'business_id', 'contacts_id', 'contract_id', 'create_user_id', 'owner_user_id', 'plan_id'])) {
  798. $field_list[$k]['writeStatus'] = 0;
  799. }
  800. //处理setting内容
  801. $setting = [];
  802. $default_value = $v['default_value'];
  803. $value = [];
  804. if (in_array($v['form_type'], ['radio', 'select', 'checkbox'])) {
  805. $setting = explode(chr(10), $v['setting']);
  806. if ($v['form_type'] == 'checkbox') $default_value = $v['default_value'] ? explode(',', $v['default_value']) : [];
  807. }
  808. if ($v['field'] == 'order_date') {
  809. $default_value = date('Y-m-d', time());
  810. }
  811. //地图类型
  812. if ($v['form_type'] == 'map_address') {
  813. $value = [
  814. 'address' => $dataInfo['address'] ? explode(chr(10), $dataInfo['address']) : [],
  815. 'location' => $dataInfo['location'],
  816. 'detail_address' => $dataInfo['detail_address'],
  817. 'lng' => $dataInfo['lng'],
  818. 'lat' => $dataInfo['lat']
  819. ];
  820. } elseif ($v['form_type'] == 'product') {
  821. //相关产品类型
  822. switch ($param['types']) {
  823. case 'crm_business' :
  824. $rProduct = db('crm_business_product');
  825. $r_id = 'business_id';
  826. break;
  827. case 'crm_contract' :
  828. $rProduct = db('crm_contract_product');
  829. $r_id = 'contract_id';
  830. break;
  831. default :
  832. break;
  833. }
  834. $newProductList = [];
  835. $productList = $rProduct->where([$r_id => $param['action_id']])->select();
  836. foreach ($productList as $key => $product) {
  837. $product_info = [];
  838. $category_name = '';
  839. $product_info = db('crm_product')->where(['product_id' => $product['product_id']])->field('product_id,name,category_id')->find();
  840. $category_name = db('crm_product_category')->where(['category_id' => $product_info['category_id']])->value('name');
  841. $productList[$key]['name'] = $product_info['name'] ?: '';
  842. $productList[$key]['category_id_info'] = $category_name ?: '';
  843. }
  844. $value = [
  845. 'product' => $productList,
  846. 'total_price' => $dataInfo['total_price'],
  847. 'discount_rate' => $dataInfo['discount_rate']
  848. ];
  849. } elseif ($v['form_type'] == 'user') {
  850. $value = $userModel->getListByStr($dataInfo[$v['field']]) ?: [];
  851. if (empty($value)) $default_value = $userModel->getListByStr($param['user_id']) ? : [];
  852. } elseif ($v['form_type'] == 'single_user') {
  853. # 单用户
  854. $userInfo = $userModel->getListByStr($dataInfo[$v['field']]);
  855. $value = !empty($userInfo[0]) ? $userInfo[0] : [];
  856. if (empty($value)) {
  857. $userInfo = $userModel->getListByStr($param['user_id']);
  858. $default_value = !empty($userInfo[0]) ? $userInfo[0] : [];
  859. }
  860. }elseif ($v['form_type'] == 'structure') {
  861. $value = $structureModel->getListByStr($dataInfo[$v['field']]) ?: [];
  862. } elseif ($v['form_type'] == 'file') {
  863. $fileIds = [];
  864. $fileIds = stringToArray($dataInfo[$v['field']]);
  865. $whereFile = [];
  866. $whereFile['module'] = 'other';
  867. $whereFile['module_id'] = 1;
  868. $whereFile['file_id'] = ['in', $fileIds];
  869. $fileList = $fileModel->getDataList($whereFile, 'all');
  870. $value = $fileList['list'] ?: [];
  871. } elseif ($v['form_type'] == 'customer') {
  872. $value = $dataInfo[$v['field']] ? db('crm_customer')->where(['customer_id' => $dataInfo[$v['field']]])->field('customer_id,name')->select() : [];
  873. } elseif ($v['form_type'] == 'business') {
  874. $value = $dataInfo[$v['field']] ? db('crm_business')->where(['business_id' => $dataInfo[$v['field']]])->field('business_id,name')->select() : [];
  875. } elseif ($v['form_type'] == 'contacts') {
  876. $value = $dataInfo[$v['field']] ? db('crm_contacts')->where(['contacts_id' => $dataInfo[$v['field']]])->field('contacts_id,name')->select() : [];
  877. } elseif ($v['form_type'] == 'contract') {
  878. $value = $dataInfo[$v['field']] ? db('crm_contract')->where(['contract_id' => $dataInfo[$v['field']]])->field('contract_id,num')->select() : [];
  879. } elseif ($v['form_type'] == 'category') {
  880. //产品类别
  881. if ($param['action'] == 'read') {
  882. $category_name = db('crm_product_category')->where(['category_id' => $dataInfo['category_id']])->value('name');
  883. $value = $category_name ?: '';
  884. } elseif ($param['action'] == 'update') {
  885. $parentIds = [];
  886. if (!empty($dataInfo['category_id'])) {
  887. $parentIds = $this->getProductParentIds($dataInfo['category_id']);
  888. $parentIds = array_reverse($parentIds);
  889. array_push($parentIds, $dataInfo['category_id']);
  890. }
  891. $value = $parentIds;
  892. } else {
  893. $categoryModel = new \app\crm\model\ProductCategory();
  894. $value = $categoryModel->getDataList('tree');
  895. }
  896. } elseif ($v['form_type'] == 'business_type') {
  897. //商机状态组
  898. $businessStatusModel = new \app\crm\model\BusinessStatus();
  899. $userInfo = $userModel->getUserById($user_id);
  900. $setting = db('crm_business_type')
  901. ->where('status', 1)
  902. ->where('is_display', 1)
  903. ->where(function ($query) use ($userInfo) {
  904. $query->where(['structure_id' => ['like', '%,' . $userInfo['structure_id'] . ',%']]);
  905. $query->whereOr('structure_id', '');
  906. })->select();
  907. foreach ($setting as $key => $val) {
  908. $setting[$key]['statusList'] = $businessStatusModel->getDataList($val['type_id'], 0);
  909. }
  910. $setting = $setting ?: [];
  911. if ($param['action'] == 'read') {
  912. $value = $dataInfo[$v['field']] ? db('crm_business_type')->where(['type_id' => $dataInfo[$v['field']]])->value('name') : '';
  913. } else {
  914. $value = (int)$dataInfo[$v['field']] ?: '';
  915. }
  916. } elseif ($v['form_type'] == 'business_status') {
  917. //商机阶段
  918. if ($param['action'] == 'read') {
  919. $value = $dataInfo[$v['field']] ? db('crm_business_status')->where(['status_id' => $dataInfo[$v['field']]])->value('name') : '';
  920. } else {
  921. $businessStatusModel = new \app\crm\model\BusinessStatus();
  922. $setting = $businessStatusModel->getDataList($dataInfo['type_id'], 1);
  923. $value = (int)$dataInfo[$v['field']] ?: '';
  924. }
  925. } elseif ($v['form_type'] == 'receivables_plan') {
  926. //回款计划期数
  927. $value = $dataInfo[$v['field']] ? db('crm_receivables_plan')->where(['plan_id' => $dataInfo[$v['field']]])->value('num') : '';
  928. } elseif ($v['form_type'] == 'business_cause' || $v['form_type'] == 'examine_cause') {
  929. $whereTravel = [];
  930. $whereTravel['examine_id'] = $dataInfo['examine_id'];
  931. $travelList = db('oa_examine_travel')->where($whereTravel)->select() ?: [];
  932. foreach ($travelList as $key => $val) {
  933. $where = [];
  934. $fileList = [];
  935. $imgList = [];
  936. $where['module'] = 'oa_examine_travel';
  937. $where['module_id'] = $val['travel_id'];
  938. $newFileList = [];
  939. $newFileList = $fileModel->getDataList($where, 'all');
  940. if ($newFileList['list']) {
  941. foreach ($newFileList['list'] as $val1) {
  942. if ($val1['types'] == 'file') {
  943. $fileList[] = $val1;
  944. } else {
  945. $imgList[] = $val1;
  946. }
  947. }
  948. }
  949. $travelList[$key]['start_time'] = $val['start_time'] ? date('Y-m-d H:i:s', $val['start_time']) : null;
  950. $travelList[$key]['end_time'] = $val['end_time'] ? date('Y-m-d H:i:s', $val['end_time']) : null;
  951. $travelList[$key]['fileList'] = $fileList ?: [];
  952. $travelList[$key]['imgList'] = $imgList ?: [];
  953. }
  954. $value = $travelList ?: [];
  955. } elseif ($v['form_type'] == 'checkbox') {
  956. $value = isset($dataInfo[$v['field']]) ? stringToArray($dataInfo[$v['field']]) : [];
  957. } elseif ($v['form_type'] == 'date') {
  958. $value = ($dataInfo[$v['field']] && $dataInfo[$v['field']] !== '0000-00-00') ? $dataInfo[$v['field']] : '';
  959. } else {
  960. $value = isset($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
  961. }
  962. $field_list[$k]['setting'] = $setting;
  963. $field_list[$k]['default_value'] = $default_value;
  964. $field_list[$k]['value'] = $value;
  965. }
  966. }
  967. return array_values($field_list) ?: [];
  968. }
  969. /**
  970. * [fieldSearch 获取自定义字段高级筛选信息]
  971. * @param $types 分类
  972. * @param $map 查询条件
  973. * @param form_type 字段类型 (’text’,’textarea’,’mobile’,’email’等)
  974. * @param setting 设置 (单选、下拉、多选的选项值,使用回车分隔)
  975. * @author Michael_xu
  976. */
  977. public function fieldSearch($param)
  978. {
  979. $types = $param['types'];
  980. if (!in_array($types, $this->types_arr)) {
  981. $this->error = '参数错误';
  982. return false;
  983. }
  984. $userModel = new \app\admin\model\User();
  985. $user_id = $param['user_id'];
  986. $map['types'] = ['in', ['', $types]];
  987. $map['form_type'] = ['not in', ['file', 'form', 'business_status']];
  988. $map['is_hidden'] = 0;
  989. $field_list = db('admin_field')
  990. ->where($map)
  991. ->whereOr(['types' => ''])
  992. ->field('field,name,form_type,setting')
  993. ->order('order_id asc, field_id asc, update_time desc')
  994. ->select();
  995. if (in_array($types, ['crm_contract', 'crm_receivables'])) {
  996. $field_arr = [
  997. '0' => [
  998. 'field' => 'check_status',
  999. 'name' => '审核状态',
  1000. 'form_type' => 'select',
  1001. 'setting' => '待审核' . chr(10) . '审核中' . chr(10) . '审核通过' . chr(10) . '审核失败' . chr(10) . '已撤回' . chr(10) . '未提交' . chr(10) . '已作废'
  1002. ]
  1003. ];
  1004. }
  1005. if (in_array($param['types'], ['crm_customer'])) {
  1006. $field_arr = [
  1007. '0' => [
  1008. 'field' => 'address',
  1009. 'name' => '地区定位',
  1010. 'form_type' => 'address',
  1011. 'setting' => []
  1012. ]
  1013. ];
  1014. }
  1015. if ($param['types'] == 'crm_customer') {
  1016. $field_arr[] = [
  1017. 'field' => 'detail_address',
  1018. 'name' => '详细地址',
  1019. 'form_type' => 'text',
  1020. 'setting' => []
  1021. ];
  1022. }
  1023. if ($field_arr) $field_list = array_merge($field_list, $field_arr);
  1024. foreach ($field_list as $k => $v) {
  1025. //处理setting内容
  1026. $setting = [];
  1027. if (in_array($v['form_type'], ['radio', 'select', 'checkbox'])) {
  1028. $setting = explode(chr(10), $v['setting']);
  1029. }
  1030. $field_list[$k]['setting'] = $setting;
  1031. if ($v['field'] == 'customer_id') {
  1032. $field_list[$k]['form_type'] = 'module';
  1033. $field_list[$k]['field'] = 'customer_name';
  1034. }
  1035. if ($v['field'] == 'business_id') {
  1036. $field_list[$k]['form_type'] = 'module';
  1037. $field_list[$k]['field'] = 'business_name';
  1038. }
  1039. if ($v['field'] == 'contract_id') {
  1040. $field_list[$k]['form_type'] = 'module';
  1041. $field_list[$k]['field'] = 'contract_name';
  1042. }
  1043. if ($v['field'] == 'contacts_id') {
  1044. $field_list[$k]['form_type'] = 'module';
  1045. $field_list[$k]['field'] = 'contacts_name';
  1046. }
  1047. if ($v['form_type'] == 'category') {
  1048. } elseif ($v['form_type'] == 'business_type') {
  1049. //商机状态组
  1050. $businessStatusModel = new \app\crm\model\BusinessStatus();
  1051. $userInfo = $userModel->getUserById($user_id);
  1052. $setting = db('crm_business_type')
  1053. ->where(['structure_id' => ['like', ',%' . $userInfo['structure_id'] . '%,'], 'status' => 1])
  1054. ->whereOr('structure_id', '')
  1055. ->select();
  1056. foreach ($setting as $key => $val) {
  1057. $setting[$key]['statusList'] = $businessStatusModel->getDataList($val['type_id'], 1);
  1058. }
  1059. $setting = $setting ?: [];
  1060. }
  1061. $field_list[$k]['setting'] = $setting;
  1062. }
  1063. return $field_list ?: [];
  1064. }
  1065. /**
  1066. * 自定义字段验证规则
  1067. * @param string $types 类型:crm_customer crm_business ...
  1068. * @param int $types_id 自定义表types_id
  1069. * @param string $action 操作:save update
  1070. * @return array
  1071. */
  1072. public function validateField($types, $types_id = 0, $action = 'save')
  1073. {
  1074. $apiCommon = new ApiCommon();
  1075. $userId = $apiCommon->userInfo['id'];
  1076. $grantData = getFieldGrantData($userId);
  1077. $userLevel = isSuperAdministrators($userId);
  1078. $unField = ['update_time', 'create_time', 'create_user_id', 'owner_user_id'];
  1079. $fieldList = $this->where(['types' => ['in', ['', $types]], 'types_id' => $types_id, 'field' => ['not in', $unField], 'form_type' => ['not in', ['checkbox', 'user', 'structure', 'file']]])->field('field,name,form_type,is_unique,is_null,max_length')->select();
  1080. $validateArr = [];
  1081. $rule = [];
  1082. $message = [];
  1083. foreach ($fieldList as $field) {
  1084. # 字段授权
  1085. if (!$userLevel && !empty($grantData[$types])) {
  1086. $status = getFieldGrantStatus($field['field'], $grantData[$types]);
  1087. # 没有字段查看权限或者编辑时没有字段修改权限就跳过验证
  1088. if (empty($status['read']) || ($action != 'save' && empty($status['write']))) continue;
  1089. }
  1090. $rule_value = '';
  1091. $scene_value = '';
  1092. $max_length = $field['max_length'] ?: '';
  1093. if ($field['is_null']) {
  1094. $rule_value .= 'require';
  1095. $message[$field['field'] . '.require'] = $field['name'] . '不能为空';
  1096. }
  1097. if ($field['form_type'] == 'number') {
  1098. if ($rule_value) $rule_value .= '|';
  1099. $rule_value .= 'number';
  1100. $message[$field['field'] . '.number'] = $field['name'] . '必须是数字';
  1101. } elseif ($field['form_type'] == 'email') {
  1102. if ($rule_value) $rule_value .= '|';
  1103. $rule_value .= 'email';
  1104. $message[$field['field'] . '.email'] = $field['name'] . '格式错误';
  1105. } elseif ($field['form_type'] == 'mobile ') {
  1106. if ($rule_value) $rule_value .= '|';
  1107. $rule_value .= 'regex:^1[3456789][0-9]{9}?$';
  1108. $message[$field['field'] . '.regex'] = $field['name'] . '格式错误';
  1109. }
  1110. if ($field['is_unique']) {
  1111. if ($rule_value) $rule_value .= '|';
  1112. $rule_value .= 'unique:' . $types;
  1113. $message[$field['field'] . '.unique'] = $field['name'] . '已经存在,不能重复添加';
  1114. }
  1115. if ($max_length) {
  1116. if ($rule_value) $rule_value .= '|';
  1117. $rule_value .= 'max:' . $max_length;
  1118. $message[$field['field'] . '.max'] = $field['name'] . '不能超过' . $max_length . '个字符';
  1119. }
  1120. // if ($field['form_type'] == 'datetime') {
  1121. // $rule_value .= 'date';
  1122. // $message[$field['field'].'.date'] = $field['name'].'格式错误';
  1123. // }
  1124. if ($rule_value == 'require|') $rule_value = 'require';
  1125. if (!empty($rule_value)) $rule[$field['field']] = $rule_value;
  1126. }
  1127. $validateArr['rule'] = $rule ?: [];
  1128. $validateArr['message'] = $message ?: [];
  1129. return $validateArr;
  1130. }
  1131. /**
  1132. * [getIndexField 列表展示字段]
  1133. * @param types 分类
  1134. * @author Michael_xu
  1135. */
  1136. public function getIndexFieldConfig($types, $user_id, $types_id = '')
  1137. {
  1138. $userFieldModel = new \app\admin\model\UserField();
  1139. $userFieldData = $userFieldModel->getConfig($types, $user_id);
  1140. $userFieldData = $userFieldData ? json_decode($userFieldData, true) : [];
  1141. $grantData = getFieldGrantData($user_id);
  1142. $userLevel = isSuperAdministrators($user_id);
  1143. $fieldList = $this->getFieldList($types, $types_id);
  1144. $where = [];
  1145. if ($userFieldData) {
  1146. $fieldArr = [];
  1147. $i = 0;
  1148. foreach ($userFieldData as $k => $v) {
  1149. if (empty($fieldList[$k])) {
  1150. unset($userFieldData[$k]);
  1151. continue;
  1152. }
  1153. if (empty($v['is_hide'])) {
  1154. $fieldArr[$i]['field'] = $k;
  1155. $fieldArr[$i]['name'] = $fieldList[$k]['name'];
  1156. $fieldArr[$i]['form_type'] = $fieldList[$k]['form_type'];
  1157. $fieldArr[$i]['width'] = $v['width'] ?: '';
  1158. $i++;
  1159. }
  1160. }
  1161. $dataList = $fieldArr;
  1162. } else {
  1163. $dataList = $fieldList;
  1164. }
  1165. # 处理字段授权
  1166. foreach ($dataList as $k => $v) {
  1167. if (!$userLevel && !empty($grantData[$types])) {
  1168. $status = getFieldGrantStatus($v['field'], $grantData[$types]);
  1169. # 查看权限
  1170. if ($status['read'] == 0) unset($dataList[(int)$k]);
  1171. }
  1172. }
  1173. return array_values($dataList) ?: [];
  1174. }
  1175. /**
  1176. * 获取列表展示字段
  1177. * @return $types_id 默认为空多自定义字段条件使用
  1178. * @return void
  1179. * @author Ymob
  1180. * @datetime 2019-10-23 17:32:57
  1181. */
  1182. public function getFieldList($types, $types_id = '')
  1183. {
  1184. $newTypes = $types;
  1185. $unField = ['-1'];
  1186. if ($types == 'crm_customer_pool') {
  1187. $newTypes = 'crm_customer';
  1188. $unField = ['owner_user_id'];
  1189. }
  1190. $fieldArr = $this
  1191. ->where([
  1192. 'types' => ['IN', ['', $newTypes]],
  1193. 'form_type' => ['not in', ['file', 'form']],
  1194. 'field' => ['not in', $unField],
  1195. 'types_id' => ['eq', $types_id],
  1196. 'is_hidden'=>0
  1197. ])
  1198. ->field(['field', 'name', 'form_type,is_hidden'])
  1199. ->order('order_id asc')
  1200. ->select();
  1201. $res = [];
  1202. foreach ($fieldArr as $val) {
  1203. $res[] = $val->toArray();
  1204. }
  1205. if ($types == 'oa_examine') {
  1206. }
  1207. if (isset($this->orther_field_list[$newTypes])) {
  1208. foreach ($this->orther_field_list[$newTypes] as $val) {
  1209. $res[] = $val;
  1210. }
  1211. }
  1212. return array_column($res, null, 'field');
  1213. }
  1214. /**
  1215. * [getIndexField 列表展示字段]
  1216. * @param types 分类
  1217. * @param is_data 1 取数据时
  1218. * @author Michael_xu
  1219. */
  1220. public function getIndexField($types, $user_id, $is_data = '')
  1221. {
  1222. $apiCommon = new ApiCommon();
  1223. $userFieldModel = new \app\admin\model\UserField();
  1224. $userFieldData = $userFieldModel->getConfig($types, $user_id);
  1225. $userFieldData = $userFieldData ? json_decode($userFieldData, true) : [];
  1226. $othor_un_field = array_column($this->orther_field_list[$types], 'field');
  1227. $unField = array_merge(['pool_day', 'business-check', 'call'], $othor_un_field);
  1228. $user_id = !empty($user_id) ? $user_id : $apiCommon->userInfo['id'];
  1229. $grantData = getFieldGrantData($user_id);
  1230. $userLevel = isSuperAdministrators($user_id);
  1231. $where = [];
  1232. if ($userFieldData) {
  1233. $dataList = [];
  1234. foreach ($userFieldData as $k => $v) {
  1235. if (empty($v['is_hide']) && !in_array($k, $unField)) {
  1236. $dataList[] = $k;
  1237. }
  1238. }
  1239. } else {
  1240. $where['types'] = ['in', ['', $types]];
  1241. $dataList = $this->where($where)->column('field');
  1242. }
  1243. $newList = $dataList;
  1244. if ($is_data == 1) {
  1245. switch ($types) {
  1246. case 'crm_leads' :
  1247. $sysField = ['leads_id', 'create_time', 'update_time', 'create_user_id', 'owner_user_id', 'last_time', 'last_record'];
  1248. break;
  1249. case 'crm_business' :
  1250. $newList = [];
  1251. foreach ($dataList as $v) {
  1252. $newList[] = 'business.' . $v;
  1253. }
  1254. $sysField = ['business.business_id', 'business.customer_id', 'business.create_time', 'business.update_time', 'business.status_id', 'business.type_id', 'business.create_user_id', 'business.owner_user_id', 'business.ro_user_id', 'business.rw_user_id', 'business.last_time', 'business.last_record'];
  1255. break;
  1256. case 'crm_customer' :
  1257. $sysField = ['customer_id', 'deal_time', 'create_time', 'update_time', 'is_lock', 'deal_status', 'create_user_id', 'owner_user_id', 'ro_user_id', 'rw_user_id', 'address', 'detail_address', 'last_time', 'last_record'];
  1258. break;
  1259. case 'crm_customer_pool' :
  1260. $sysField = ['customer_id', 'deal_time', 'create_time', 'update_time', 'create_user_id', 'owner_user_id', 'ro_user_id', 'rw_user_id', 'address', 'detail_address', 'last_time', 'last_record'];
  1261. break;
  1262. case 'crm_contacts' :
  1263. $newList = [];
  1264. foreach ($dataList as $v) {
  1265. $newList[] = 'contacts.' . $v;
  1266. }
  1267. $sysField = ['contacts.contacts_id', 'contacts.customer_id', 'contacts.create_time', 'contacts.update_time', 'contacts.create_user_id', 'contacts.owner_user_id', 'contacts.last_time', 'contacts.last_record'];
  1268. break;
  1269. case 'crm_contract' :
  1270. $newList = [];
  1271. foreach ($dataList as $v) {
  1272. $newList[] = 'contract.' . $v;
  1273. }
  1274. $sysField = ['contract.contract_id', 'contract.create_time', 'contract.update_time', 'contract.create_user_id', 'contract.owner_user_id', 'contract.check_status', 'contract.last_time', 'contract.last_record'];
  1275. break;
  1276. case 'crm_receivables' :
  1277. $newList = [];
  1278. foreach ($dataList as $v) {
  1279. $newList[] = 'receivables.' . $v;
  1280. }
  1281. $sysField = ['receivables.receivables_id', 'receivables.customer_id', 'receivables.contract_id', 'receivables.plan_id', 'receivables.create_time', 'receivables.update_time', 'receivables.create_user_id', 'receivables.owner_user_id', 'receivables.check_status'];
  1282. break;
  1283. case 'crm_product' :
  1284. $newList = [];
  1285. foreach ($dataList as $v) {
  1286. $newList[] = 'product.' . $v;
  1287. }
  1288. $sysField = ['product.product_id', 'product.category_id', 'product.create_time', 'product.update_time', 'product.create_user_id', 'product.owner_user_id'];
  1289. break;
  1290. case 'crm_visit' :
  1291. $newList = [];
  1292. foreach ($dataList as $v) {
  1293. $newList[] = 'visit.' . $v;
  1294. }
  1295. $sysField = ['visit.visit_id', 'visit.owner_user_id', 'visit.status', 'visit.customer_id', 'visit.contract_id', 'visit.create_time', 'visit.update_time', 'visit.create_user_id', 'visit.visit_time', 'visit.contacts_id'];
  1296. break;
  1297. }
  1298. $listArr = $sysField ? array_unique(array_merge($newList, $sysField)) : $dataList;
  1299. } else {
  1300. $listArr = $dataList;
  1301. }
  1302. $typesArray = explode('_', $types);
  1303. $type = array_pop($typesArray);
  1304. if (isset($this->orther_field_list[$types])) {
  1305. foreach ($this->orther_field_list[$types] as $val) {
  1306. if (in_array($type . '.' . $val['field'], $listArr) && !in_array($types, ['crm_contract', 'crm_business', 'crm_receivables'])) {
  1307. unset($listArr[array_search($type . '.' . $val['field'], $listArr)]);
  1308. }
  1309. }
  1310. }
  1311. # 处理字段授权
  1312. foreach ($listArr as $k => $v) {
  1313. if (!$userLevel && !empty($grantData[$types])) {
  1314. $status = getFieldGrantStatus($v, $grantData[$types]);
  1315. # 查看权限
  1316. if ($status['read'] == 0) unset($listArr[(int)$k]);
  1317. }
  1318. }
  1319. return $listArr ?: [];
  1320. }
  1321. /**
  1322. * [checkFieldPer 判断权限]
  1323. * @param types 分类
  1324. * @author Michael_xu
  1325. */
  1326. public function checkFieldPer($module, $controller, $action, $user_id = '')
  1327. {
  1328. $userModel = new \app\admin\model\User();
  1329. if (!checkPerByAction($module, $controller, $action)) return false;
  1330. if ($user_id && !in_array($user_id, $userModel->getUserByPer($module, $controller, $action))) return false;
  1331. return true;
  1332. }
  1333. /**
  1334. * [getField 获取字段属性]
  1335. * @param types 分类
  1336. * @author Michael_xu
  1337. */
  1338. public function getField($param)
  1339. {
  1340. $types = $param['types'];
  1341. $unFormType = $param['unFormType'];
  1342. if (!in_array($types, $this->types_arr)) {
  1343. return resultArray(['error' => '参数错误']);
  1344. }
  1345. $field_arr = [];
  1346. //模拟自定义字段返回
  1347. switch ($types) {
  1348. case 'admin_user' :
  1349. $field_arr = \app\hrm\model\Userdet::getField();
  1350. break;
  1351. case 'crm_invoice':
  1352. $field_arr = $this->getInvoiceSearch();
  1353. break;
  1354. default :
  1355. $data = [];
  1356. $data['types'] = $types;
  1357. $data['user_id'] = $param['user_id'];
  1358. if ($unFormType) $data['form_type'] = array('not in', $unFormType);
  1359. $field_arr = $this->fieldSearch($data);
  1360. }
  1361. if ($types == 'crm_visit') {
  1362. foreach ($field_arr AS $key => $value) {
  1363. if ($value['name'] == '负责人') unset($field_arr[(int)$key]);
  1364. }
  1365. }
  1366. return $field_arr;
  1367. }
  1368. /**
  1369. * 自定义字段验重
  1370. * @param $field 字段名, $val 值, $id 排除当前数据验重, $types 需要查询的模块名
  1371. * @return
  1372. * @author
  1373. */
  1374. public function getValidate($field, $val, $id, $types)
  1375. {
  1376. $val = trim($val);
  1377. if (!$val) {
  1378. $this->error = '验证内容不能为空';
  1379. return false;
  1380. }
  1381. if (!$field) {
  1382. $this->error = '数据验证错误,请联系管理员!';
  1383. return false;
  1384. }
  1385. if (!in_array($types, $this->types_arr)) {
  1386. $this->error = '参数错误!';
  1387. return false;
  1388. }
  1389. $field_info = db('admin_field')->where(['types' => $types, 'field' => $field])->find();
  1390. if (!$field_info) {
  1391. $this->error = '数据验证错误,请联系管理员!';
  1392. return false;
  1393. }
  1394. $dataModel = '';
  1395. switch ($types) {
  1396. case 'crm_leads' :
  1397. $dataModel = new \app\crm\model\Leads();
  1398. break;
  1399. case 'crm_customer' :
  1400. $dataModel = new \app\crm\model\Customer();
  1401. break;
  1402. case 'crm_contacts' :
  1403. $dataModel = new \app\crm\model\Contacts();
  1404. break;
  1405. case 'crm_business' :
  1406. $dataModel = new \app\crm\model\Business();
  1407. break;
  1408. case 'crm_product' :
  1409. $dataModel = new \app\crm\model\Product();
  1410. break;
  1411. case 'crm_contract' :
  1412. $dataModel = new \app\crm\model\Contract();
  1413. break;
  1414. case 'crm_receivables' :
  1415. $dataModel = new \app\crm\model\Receivables();
  1416. break;
  1417. }
  1418. $where = [];
  1419. $where[$field] = ['eq', $val];
  1420. if ($id) {
  1421. //为编辑时的验重
  1422. $where[$dataModel->getpk()] = ['neq', $id];
  1423. }
  1424. if($types=='crm_product'){
  1425. $where['delete_user_id'] = 0;
  1426. }
  1427. if ($res = $dataModel->where($where)->find()) {
  1428. $this->error = '该数据已存在,请修改后提交!';
  1429. return false;
  1430. }
  1431. return true;
  1432. }
  1433. /**
  1434. * [getFieldByFormType 根据字段类型获取字段数组]
  1435. * @param types 分类
  1436. * @author Michael_xu
  1437. */
  1438. public function getFieldByFormType($types, $form_type)
  1439. {
  1440. $fieldArr = $this->where(['types' => $types, 'form_type' => $form_type])->column('field');
  1441. return $fieldArr ?: [];
  1442. }
  1443. /**
  1444. * [getFormValueByField 格式化表格字段类型值]
  1445. * @param $field 字段名
  1446. * @param $value 字段值
  1447. * @author Michael_xu
  1448. */
  1449. public function getFormValueByField($field, $value)
  1450. {
  1451. $formValue = db('admin_field')->where(['field' => $field])->value('form_value');
  1452. $formValue = sort_select($formValue, 'order_id', 1); //二维数组排序
  1453. $field = [];
  1454. foreach ($formValue as $k => $v) {
  1455. $field[] = $v['field'];
  1456. }
  1457. $data = [];
  1458. foreach ($value as $k => $v) {
  1459. foreach ($field as $key => $val) {
  1460. $data[$k][$val] = $v['value'];
  1461. }
  1462. }
  1463. return $data;
  1464. }
  1465. /**
  1466. * 根据form_type处理数据
  1467. * @author lee
  1468. */
  1469. public function getValueByFormtype($val, $form_type)
  1470. {
  1471. $userModel = new \app\admin\model\User();
  1472. $structureModel = new \app\admin\model\Structure();
  1473. switch ($form_type) {
  1474. // case 'datetime' :
  1475. // $val = $val > 0 ? date('Y-m-d H:i:s', $val) : '';
  1476. // break;
  1477. case 'user' :
  1478. $val = count($userModel->getUserNameByArr($val)) > 1 ? ArrayToString($userModel->getUserNameByArr($val)) : implode(',', $userModel->getUserNameByArr(stringToArray($val)));
  1479. break;
  1480. case 'userStr' :
  1481. $val = explode(',', $val);
  1482. $val = count($userModel->getUserNameByArr($val)) > 1 ? ArrayToString($userModel->getUserNameByArr($val)) : implode(',', $userModel->getUserNameByArr($val));
  1483. break;
  1484. case 'structure' :
  1485. $val = implode(',', $structureModel->getStructureNameByArr(stringToArray($val)));
  1486. break;
  1487. case 'customer' :
  1488. $val = db('crm_customer')->where(['customer_id' => $val])->value('name');
  1489. break;
  1490. case 'contract' :
  1491. $val = db('crm_contract')->where(['customer_id' => $val])->value('num');
  1492. break;
  1493. case 'business' :
  1494. $val = db('crm_business')->where(['business_id' => $val])->value('name');
  1495. break;
  1496. case 'category' :
  1497. $val = db('crm_product_category')->where(['category_id' => $val])->value('name');
  1498. break;
  1499. case 'business_type' :
  1500. $val = db('crm_business_type')->where(['type_id' => $val])->value('name');
  1501. break;
  1502. case 'business_status' :
  1503. $val = db('crm_business_status')->where(['status_id' => $val])->value('name');
  1504. break;
  1505. }
  1506. return $val;
  1507. }
  1508. /**
  1509. * [getIndexFieldList 列表展示字段]
  1510. * @param types 分类
  1511. * @author Michael_xu
  1512. */
  1513. public function getIndexFieldList($types, $user_id)
  1514. {
  1515. $fieldArr = $this->getIndexField($types, $user_id);
  1516. $types = $types == 'crm_customer_pool' ? 'crm_customer' : $types;
  1517. $fieldList = db('admin_field')->where(['field' => array('in', $fieldArr)])->where('types', ['eq', $types], ['eq', ''], 'or')->order('order_id asc')->select();
  1518. return $fieldList ?: [];
  1519. }
  1520. /**
  1521. * [getArrayField 数组类型字段]
  1522. * @param types 分类
  1523. * @author Michael_xu
  1524. */
  1525. public function getArrayField($types)
  1526. {
  1527. $arrayFormType = ['structure', 'user', 'checkbox', 'file'];
  1528. $arrFieldAtt = db('admin_field')->where(['types' => $types, 'form_type' => ['in', $arrayFormType]])->column('field');
  1529. return $arrFieldAtt ?: [];
  1530. }
  1531. /**
  1532. * 字段对照关系处理
  1533. * @param $types 分类
  1534. * @param $data 数据
  1535. * @return
  1536. * @author Michael_xu
  1537. */
  1538. public function getRelevantData($types, $data = [])
  1539. {
  1540. $types_arr = ['crm_leads'];
  1541. if (!in_array($types, $types_arr)) {
  1542. $this->error = '参数错误';
  1543. return false;
  1544. }
  1545. if (!$data) return $data;
  1546. $list = $this->where(['types' => $types, 'relevant' => ['neq', '']])->field('field,relevant')->select();
  1547. if (!$list) return $data;
  1548. $newData = $data;
  1549. foreach ($list as $k => $v) {
  1550. $newData[$v['relevant']] = $data[$v['field']];
  1551. }
  1552. return $newData ?: [];
  1553. }
  1554. /**
  1555. * 字段排序
  1556. * @param types 自定义字段分类
  1557. * @param prefix 自定义字段前缀
  1558. * @param field 自定义字段
  1559. * @param order 排序规则
  1560. * @return
  1561. * @author Michael_xu
  1562. */
  1563. public function getOrderByFormtype($types, $prefix, $field, $order_type)
  1564. {
  1565. $form_type = $this->where(['types' => $types, 'field' => $field])->value('form_type');
  1566. // if (!$form_type) {
  1567. // $this->error = '参数错误';
  1568. // return false;
  1569. // }
  1570. $temp_field = $field;
  1571. $field = $prefix ? $prefix . '.' . $field : $field;
  1572. switch ($form_type) {
  1573. case 'textarea' :
  1574. case 'radio' :
  1575. case 'select' :
  1576. case 'checkbox' :
  1577. case 'address' :
  1578. $order = 'convert(' . $field . ' using gbk) ' . trim($order_type);
  1579. break;
  1580. default :
  1581. $order = $field . ' ' . $order_type;
  1582. break;
  1583. }
  1584. if (isset($this->orther_field_list[$types])) {
  1585. foreach ($this->orther_field_list[$types] as $val) {
  1586. // $res[] = $val;
  1587. // $order
  1588. $temp = trim($prefix . '.' . $val['field'], '.');
  1589. if ($temp == $field) {
  1590. $order = str_replace($temp, $val['field'], $order);
  1591. }
  1592. }
  1593. }
  1594. return $order;
  1595. }
  1596. /**
  1597. * 处理字段别名、权限
  1598. *
  1599. * @param int $userId 用户ID
  1600. * @param string $types 模块类型
  1601. * @param string $action 行为
  1602. * @param array $data 字段数据
  1603. * @author fanqi
  1604. * @since 2021-04-06
  1605. * @return array
  1606. */
  1607. public function resetField($userId, $types, $action, $data)
  1608. {
  1609. $grantData = getFieldGrantData($userId);
  1610. $userLevel = isSuperAdministrators($userId);
  1611. foreach ($data AS $key => $value) {
  1612. # 处理字段授权
  1613. if (!$userLevel && !empty($grantData[$types])) {
  1614. $status = getFieldGrantStatus($value['field'], $grantData[$types]);
  1615. # 查看权限
  1616. if ($status['read'] == 0) {
  1617. unset($data[(int)$key]);
  1618. continue;
  1619. }
  1620. }
  1621. switch ($value['field']) {
  1622. case 'create_user_id' :
  1623. $data[$key]['fieldName'] = 'create_user_name';
  1624. break;
  1625. case 'owner_user_id' :
  1626. $data[$key]['fieldName'] = 'owner_user_name';
  1627. break;
  1628. case 'customer_id' :
  1629. $data[$key]['fieldName'] = 'customer_name';
  1630. break;
  1631. case 'type_id' :
  1632. $data[$key]['fieldName'] = 'type_id_info';
  1633. break;
  1634. case 'status_id' :
  1635. $data[$key]['fieldName'] = 'status_id_info';
  1636. break;
  1637. case 'business_id' :
  1638. $data[$key]['fieldName'] = 'business_name';
  1639. break;
  1640. case 'contacts_id' :
  1641. $data[$key]['fieldName'] = 'contacts_name';
  1642. break;
  1643. case 'order_user_id' :
  1644. $data[$key]['fieldName'] = 'order_user_name';
  1645. break;
  1646. case 'contract_id' :
  1647. $data[$key]['fieldName'] = 'contract_num';
  1648. break;
  1649. case 'plan_id' :
  1650. $data[$key]['fieldName'] = 'plan_id_info';
  1651. break;
  1652. case 'category_id' :
  1653. $data[$key]['fieldName'] = 'category_name';
  1654. break;
  1655. default :
  1656. $data[$key]['fieldName'] = $value['field'];
  1657. }
  1658. if (in_array($value['form_type'], ['user', 'structure']) && !in_array($value['field'], ['create_user_id', 'owner_user_id'])) {
  1659. $data[$key]['fieldName'] = $value['field'] . '_name';
  1660. }
  1661. # 详情中不显示产品类别、回款期数
  1662. if ($action == 'read' && in_array($value['field'], ['category_id'])) {
  1663. unset($data[(int)$key]);
  1664. }
  1665. }
  1666. return $data;
  1667. }
  1668. /**
  1669. * 获取公海自定义字段数据
  1670. *
  1671. * @param $poolId
  1672. * @param $dataInfo
  1673. * @return bool|\PDOStatement|string|\think\Collection
  1674. */
  1675. public function getPoolFieldData($poolId, $dataInfo)
  1676. {
  1677. $poolFields = db('crm_customer_pool_field_setting')->field(['field_name AS field', 'form_type', 'name'])->where(['pool_id' => $poolId, 'is_hidden' => 0])->select();
  1678. foreach ($poolFields AS $key => $value) {
  1679. # 字段值
  1680. $poolFields[$key]['value'] = !empty($dataInfo[$value['field']]) ? $dataInfo[$value['field']] : '';
  1681. # 处理别名
  1682. switch ($value['field']) {
  1683. case 'create_user_id' :
  1684. $poolFields[$key]['fieldName'] = 'create_user_name';
  1685. $poolFields[$key]['value'] = !empty($dataInfo['create_user_id_info']) ? [$dataInfo['create_user_id_info']] : '';
  1686. break;
  1687. case 'before_owner_user_id' :
  1688. $poolFields[$key]['fieldName'] = 'before_owner_user_name';
  1689. $poolFields[$key]['value'] = !empty($dataInfo['before_owner_user_id_info']) ? [$dataInfo['before_owner_user_id_info']] : '';
  1690. break;
  1691. default :
  1692. $poolFields[$key]['fieldName'] = $value['field'];
  1693. }
  1694. if (in_array($value['form_type'], ['user', 'structure']) && !in_array($value['field'], ['create_user_id', 'owner_user_id', 'before_owner_user_id'])) {
  1695. $poolFields[$key]['fieldName'] = $value['field_name'] . '_name';
  1696. }
  1697. # 系统字段
  1698. if (in_array($value['field'], ['last_record', 'create_user_id', 'create_time', 'update_time', 'last_time', 'before_owner_user_id'])) {
  1699. $poolFields[$key]['system'] = 1;
  1700. } else {
  1701. $poolFields[$key]['system'] = 0;
  1702. }
  1703. }
  1704. return $poolFields;
  1705. }
  1706. /**
  1707. * 获取发票高级搜索字段
  1708. *
  1709. * @return array[]
  1710. */
  1711. private function getInvoiceSearch()
  1712. {
  1713. return [
  1714. ['field' => 'invoice_number', 'form_type' => 'text', 'setting' => [], 'name' => '发票号码'],
  1715. ['field' => 'real_invoice_date', 'form_type' => 'datetime', 'setting' => [], 'name' => '实际开票日期'],
  1716. ['field' => 'logistics_number', 'form_type' => 'text', 'setting' => [], 'name' => '物流单号'],
  1717. ['field' => 'invoice_status', 'form_type' => 'select', 'setting' => ['未开票', '已开票'], 'name' => '开票状态'],
  1718. ['field' => 'check_status', 'form_type' => 'select', 'setting' => ['待审核', '审核中', '审核通过', '审核未通过', '撤回'], 'name' => '审核状态'],
  1719. ['field' => 'owner_user_id', 'form_type' => 'user', 'setting' => [], 'name' => '负责人']
  1720. ];
  1721. }
  1722. /**
  1723. * 获取产品父类层级(不包含自身)
  1724. *
  1725. * @param $productId
  1726. * @param array $parentIds
  1727. * @return array|mixed
  1728. * @throws \think\db\exception\DataNotFoundException
  1729. * @throws \think\db\exception\ModelNotFoundException
  1730. * @throws \think\exception\DbException
  1731. */
  1732. private function getProductParentIds($productId, &$parentIds = [])
  1733. {
  1734. $category = db('crm_product_category')->select();
  1735. foreach ($category as $key => $value) {
  1736. if ($value['category_id'] == $productId) {
  1737. if (!empty($value['pid'])) {
  1738. $parentIds[] = $value['pid'];
  1739. $this->getProductParentIds($value['pid'], $parentIds);
  1740. }
  1741. }
  1742. }
  1743. return $parentIds;
  1744. }
  1745. }