Field.php 75KB

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