Field.php 74KB

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