12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  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. # 产品基本信息增加负责人信息
  656. if ($param['action'] == 'read' && $param['types'] == 'crm_product') {
  657. $new_field_list[] = db('admin_field')->where(['types_id' => 0, 'field' => 'owner_user_id'])->find();
  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. # (联系人,商机,合同,回款,回访)关联其他模块的字段在详情页面不允许修改;创建人、负责人不允许修改
  674. if ($param['action'] == 'read' && in_array($v['field'], ['customer_id', 'business_id', 'contacts_id', 'contract_id', 'create_user_id', 'owner_user_id'])) {
  675. $field_list[$k]['writeStatus'] = 0;
  676. }
  677. //处理setting内容
  678. $setting = [];
  679. $default_value = $v['default_value'];
  680. $value = [];
  681. if (in_array($v['form_type'], ['radio', 'select', 'checkbox'])) {
  682. $setting = explode(chr(10), $v['setting']);
  683. if ($v['form_type'] == 'checkbox') $default_value = $v['default_value'] ? explode(',', $v['default_value']) : [];
  684. }
  685. if ($v['field'] == 'order_date') {
  686. $default_value = date('Y-m-d', time());
  687. }
  688. //地图类型
  689. if ($v['form_type'] == 'map_address') {
  690. $value = [
  691. 'address' => $dataInfo['address'] ? explode(chr(10), $dataInfo['address']) : [],
  692. 'location' => $dataInfo['location'],
  693. 'detail_address' => $dataInfo['detail_address'],
  694. 'lng' => $dataInfo['lng'],
  695. 'lat' => $dataInfo['lat']
  696. ];
  697. } elseif ($v['form_type'] == 'product') {
  698. //相关产品类型
  699. switch ($param['types']) {
  700. case 'crm_business' :
  701. $rProduct = db('crm_business_product');
  702. $r_id = 'business_id';
  703. break;
  704. case 'crm_contract' :
  705. $rProduct = db('crm_contract_product');
  706. $r_id = 'contract_id';
  707. break;
  708. default :
  709. break;
  710. }
  711. $newProductList = [];
  712. $productList = $rProduct->where([$r_id => $param['action_id']])->select();
  713. foreach ($productList as $key => $product) {
  714. $product_info = [];
  715. $category_name = '';
  716. $product_info = db('crm_product')->where(['product_id' => $product['product_id']])->field('product_id,name,category_id')->find();
  717. $category_name = db('crm_product_category')->where(['category_id' => $product_info['category_id']])->value('name');
  718. $productList[$key]['name'] = $product_info['name'] ?: '';
  719. $productList[$key]['category_id_info'] = $category_name ?: '';
  720. }
  721. $value = [
  722. 'product' => $productList,
  723. 'total_price' => $dataInfo['total_price'],
  724. 'discount_rate' => $dataInfo['discount_rate']
  725. ];
  726. } elseif ($v['form_type'] == 'user') {
  727. $value = $userModel->getListByStr($dataInfo[$v['field']]) ?: [];
  728. if (empty($value)) $default_value = $userModel->getListByStr($param['user_id']) ? : [];
  729. } elseif ($v['form_type'] == 'single_user') {
  730. # 单用户
  731. $userInfo = $userModel->getListByStr($dataInfo[$v['field']]);
  732. $value = !empty($userInfo[0]) ? $userInfo[0] : [];
  733. if (empty($value)) {
  734. $userInfo = $userModel->getListByStr($param['user_id']);
  735. $default_value = !empty($userInfo[0]) ? $userInfo[0] : [];
  736. }
  737. }elseif ($v['form_type'] == 'structure') {
  738. $value = $structureModel->getListByStr($dataInfo[$v['field']]) ?: [];
  739. } elseif ($v['form_type'] == 'file') {
  740. $fileIds = [];
  741. $fileIds = stringToArray($dataInfo[$v['field']]);
  742. $whereFile = [];
  743. $whereFile['module'] = 'other';
  744. $whereFile['module_id'] = 1;
  745. $whereFile['file_id'] = ['in', $fileIds];
  746. $fileList = $fileModel->getDataList($whereFile, 'all');
  747. $value = $fileList['list'] ?: [];
  748. } elseif ($v['form_type'] == 'customer') {
  749. $value = $dataInfo[$v['field']] ? db('crm_customer')->where(['customer_id' => $dataInfo[$v['field']]])->field('customer_id,name')->select() : [];
  750. } elseif ($v['form_type'] == 'business') {
  751. $value = $dataInfo[$v['field']] ? db('crm_business')->where(['business_id' => $dataInfo[$v['field']]])->field('business_id,name')->select() : [];
  752. } elseif ($v['form_type'] == 'contacts') {
  753. $value = $dataInfo[$v['field']] ? db('crm_contacts')->where(['contacts_id' => $dataInfo[$v['field']]])->field('contacts_id,name')->select() : [];
  754. } elseif ($v['form_type'] == 'contract') {
  755. $value = $dataInfo[$v['field']] ? db('crm_contract')->where(['contract_id' => $dataInfo[$v['field']]])->field('contract_id,num')->select() : [];
  756. } elseif ($v['form_type'] == 'category') {
  757. //产品类别
  758. if ($param['action'] == 'read') {
  759. $category_name = db('crm_product_category')->where(['category_id' => $dataInfo['category_id']])->value('name');
  760. $value = $category_name ?: '';
  761. } elseif ($param['action'] == 'update') {
  762. $parentIds = [];
  763. if (!empty($dataInfo['category_id'])) {
  764. $parentIds = $this->getProductParentIds($dataInfo['category_id']);
  765. $parentIds = array_reverse($parentIds);
  766. array_push($parentIds, $dataInfo['category_id']);
  767. }
  768. $value = $parentIds;
  769. } else {
  770. $categoryModel = new \app\crm\model\ProductCategory();
  771. $value = $categoryModel->getDataList('tree');
  772. }
  773. } elseif ($v['form_type'] == 'business_type') {
  774. //商机状态组
  775. $businessStatusModel = new \app\crm\model\BusinessStatus();
  776. $userInfo = $userModel->getUserById($user_id);
  777. $setting = db('crm_business_type')
  778. ->where('status', 1)
  779. ->where('is_display', 1)
  780. ->where(function ($query) use ($userInfo) {
  781. $query->where(['structure_id' => ['like', '%,' . $userInfo['structure_id'] . ',%']]);
  782. $query->whereOr('structure_id', '');
  783. })->select();
  784. foreach ($setting as $key => $val) {
  785. $setting[$key]['statusList'] = $businessStatusModel->getDataList($val['type_id'], 0);
  786. }
  787. $setting = $setting ?: [];
  788. if ($param['action'] == 'read') {
  789. $value = $dataInfo[$v['field']] ? db('crm_business_type')->where(['type_id' => $dataInfo[$v['field']]])->value('name') : '';
  790. } else {
  791. $value = (int)$dataInfo[$v['field']] ?: '';
  792. }
  793. } elseif ($v['form_type'] == 'business_status') {
  794. //商机阶段
  795. if ($param['action'] == 'read') {
  796. $value = $dataInfo[$v['field']] ? db('crm_business_status')->where(['status_id' => $dataInfo[$v['field']]])->value('name') : '';
  797. } else {
  798. $businessStatusModel = new \app\crm\model\BusinessStatus();
  799. $setting = $businessStatusModel->getDataList($dataInfo['type_id'], 1);
  800. $value = (int)$dataInfo[$v['field']] ?: '';
  801. }
  802. } elseif ($v['form_type'] == 'receivables_plan') {
  803. //回款计划期数
  804. $value = $dataInfo[$v['field']] ? db('crm_receivables_plan')->where(['plan_id' => $dataInfo[$v['field']]])->value('num') : '';
  805. } elseif ($v['form_type'] == 'business_cause' || $v['form_type'] == 'examine_cause') {
  806. $whereTravel = [];
  807. $whereTravel['examine_id'] = $dataInfo['examine_id'];
  808. $travelList = db('oa_examine_travel')->where($whereTravel)->select() ?: [];
  809. foreach ($travelList as $key => $val) {
  810. $where = [];
  811. $fileList = [];
  812. $imgList = [];
  813. $where['module'] = 'oa_examine_travel';
  814. $where['module_id'] = $val['travel_id'];
  815. $newFileList = [];
  816. $newFileList = $fileModel->getDataList($where, 'all');
  817. if ($newFileList['list']) {
  818. foreach ($newFileList['list'] as $val1) {
  819. if ($val1['types'] == 'file') {
  820. $fileList[] = $val1;
  821. } else {
  822. $imgList[] = $val1;
  823. }
  824. }
  825. }
  826. $travelList[$key]['start_time'] = $val['start_time'] ? date('Y-m-d H:i:s', $val['start_time']) : null;
  827. $travelList[$key]['end_time'] = $val['end_time'] ? date('Y-m-d H:i:s', $val['end_time']) : null;
  828. $travelList[$key]['fileList'] = $fileList ?: [];
  829. $travelList[$key]['imgList'] = $imgList ?: [];
  830. }
  831. $value = $travelList ?: [];
  832. } elseif ($v['form_type'] == 'checkbox') {
  833. $value = isset($dataInfo[$v['field']]) ? stringToArray($dataInfo[$v['field']]) : [];
  834. } elseif ($v['form_type'] == 'date') {
  835. $value = ($dataInfo[$v['field']] && $dataInfo[$v['field']] !== '0000-00-00') ? $dataInfo[$v['field']] : '';
  836. } else {
  837. $value = isset($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
  838. }
  839. $field_list[$k]['setting'] = $setting;
  840. $field_list[$k]['default_value'] = $default_value;
  841. $field_list[$k]['value'] = $value;
  842. }
  843. }
  844. return array_values($field_list) ?: [];
  845. }
  846. /**
  847. * [fieldSearch 获取自定义字段高级筛选信息]
  848. * @param $types 分类
  849. * @param $map 查询条件
  850. * @param form_type 字段类型 (’text’,’textarea’,’mobile’,’email’等)
  851. * @param setting 设置 (单选、下拉、多选的选项值,使用回车分隔)
  852. * @author Michael_xu
  853. */
  854. public function fieldSearch($param)
  855. {
  856. $types = $param['types'];
  857. if (!in_array($types, $this->types_arr)) {
  858. $this->error = '参数错误';
  859. return false;
  860. }
  861. $userModel = new \app\admin\model\User();
  862. $user_id = $param['user_id'];
  863. $map['types'] = ['in', ['', $types]];
  864. $map['form_type'] = ['not in', ['file', 'form', 'business_status']];
  865. $map['is_hidden'] = 0;
  866. $field_list = db('admin_field')
  867. ->where($map)
  868. ->whereOr(['types' => ''])
  869. ->field('field,name,form_type,setting')
  870. ->order('order_id asc, field_id asc, update_time desc')
  871. ->select();
  872. if (in_array($types, ['crm_contract', 'crm_receivables'])) {
  873. $field_arr = [
  874. '0' => [
  875. 'field' => 'check_status',
  876. 'name' => '审核状态',
  877. 'form_type' => 'select',
  878. 'setting' => '待审核' . chr(10) . '审核中' . chr(10) . '审核通过' . chr(10) . '审核失败' . chr(10) . '已撤回' . chr(10) . '未提交' . chr(10) . '已作废'
  879. ]
  880. ];
  881. }
  882. if (in_array($param['types'], ['crm_customer'])) {
  883. $field_arr = [
  884. '0' => [
  885. 'field' => 'address',
  886. 'name' => '地区定位',
  887. 'form_type' => 'address',
  888. 'setting' => []
  889. ]
  890. ];
  891. }
  892. if ($param['types'] == 'crm_customer') {
  893. $field_arr[] = [
  894. 'field' => 'detail_address',
  895. 'name' => '详细地址',
  896. 'form_type' => 'text',
  897. 'setting' => []
  898. ];
  899. }
  900. if ($field_arr) $field_list = array_merge($field_list, $field_arr);
  901. foreach ($field_list as $k => $v) {
  902. //处理setting内容
  903. $setting = [];
  904. if (in_array($v['form_type'], ['radio', 'select', 'checkbox'])) {
  905. $setting = explode(chr(10), $v['setting']);
  906. }
  907. $field_list[$k]['setting'] = $setting;
  908. if ($v['field'] == 'customer_id') {
  909. $field_list[$k]['form_type'] = 'module';
  910. $field_list[$k]['field'] = 'customer_name';
  911. }
  912. if ($v['field'] == 'business_id') {
  913. $field_list[$k]['form_type'] = 'module';
  914. $field_list[$k]['field'] = 'business_name';
  915. }
  916. if ($v['field'] == 'contract_id') {
  917. $field_list[$k]['form_type'] = 'module';
  918. $field_list[$k]['field'] = 'contract_name';
  919. }
  920. if ($v['field'] == 'contacts_id') {
  921. $field_list[$k]['form_type'] = 'module';
  922. $field_list[$k]['field'] = 'contacts_name';
  923. }
  924. if ($v['form_type'] == 'category') {
  925. } elseif ($v['form_type'] == 'business_type') {
  926. //商机状态组
  927. $businessStatusModel = new \app\crm\model\BusinessStatus();
  928. $userInfo = $userModel->getUserById($user_id);
  929. $setting = db('crm_business_type')
  930. ->where(['structure_id' => ['like', ',%' . $userInfo['structure_id'] . '%,'], 'status' => 1])
  931. ->whereOr('structure_id', '')
  932. ->select();
  933. foreach ($setting as $key => $val) {
  934. $setting[$key]['statusList'] = $businessStatusModel->getDataList($val['type_id'], 1);
  935. }
  936. $setting = $setting ?: [];
  937. }
  938. $field_list[$k]['setting'] = $setting;
  939. }
  940. return $field_list ?: [];
  941. }
  942. /**
  943. * [validateField 自定义字段验证规则]
  944. * @param
  945. * @author Michael_xu
  946. */
  947. public function validateField($types, $types_id = 0)
  948. {
  949. $unField = ['update_time', 'create_time', 'create_user_id', 'owner_user_id'];
  950. $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();
  951. $validateArr = [];
  952. $rule = [];
  953. $message = [];
  954. foreach ($fieldList as $field) {
  955. $rule_value = '';
  956. $scene_value = '';
  957. $max_length = $field['max_length'] ?: '';
  958. if ($field['is_null']) {
  959. $rule_value .= 'require';
  960. $message[$field['field'] . '.require'] = $field['name'] . '不能为空';
  961. }
  962. if ($field['form_type'] == 'number') {
  963. if ($rule_value) $rule_value .= '|';
  964. $rule_value .= 'number';
  965. $message[$field['field'] . '.number'] = $field['name'] . '必须是数字';
  966. } elseif ($field['form_type'] == 'email') {
  967. if ($rule_value) $rule_value .= '|';
  968. $rule_value .= 'email';
  969. $message[$field['field'] . '.email'] = $field['name'] . '格式错误';
  970. } elseif ($field['form_type'] == 'mobile ') {
  971. if ($rule_value) $rule_value .= '|';
  972. $rule_value .= 'regex:^1[3456789][0-9]{9}?$';
  973. $message[$field['field'] . '.regex'] = $field['name'] . '格式错误';
  974. }
  975. if ($field['is_unique']) {
  976. if ($rule_value) $rule_value .= '|';
  977. $rule_value .= 'unique:' . $types;
  978. $message[$field['field'] . '.unique'] = $field['name'] . '已经存在,不能重复添加';
  979. }
  980. if ($max_length) {
  981. if ($rule_value) $rule_value .= '|';
  982. $rule_value .= 'max:' . $max_length;
  983. $message[$field['field'] . '.max'] = $field['name'] . '不能超过' . $max_length . '个字符';
  984. }
  985. // if ($field['form_type'] == 'datetime') {
  986. // $rule_value .= 'date';
  987. // $message[$field['field'].'.date'] = $field['name'].'格式错误';
  988. // }
  989. if ($rule_value == 'require|') $rule_value = 'require';
  990. if (!empty($rule_value)) $rule[$field['field']] = $rule_value;
  991. }
  992. $validateArr['rule'] = $rule ?: [];
  993. $validateArr['message'] = $message ?: [];
  994. return $validateArr;
  995. }
  996. /**
  997. * [getIndexField 列表展示字段]
  998. * @param types 分类
  999. * @author Michael_xu
  1000. */
  1001. public function getIndexFieldConfig($types, $user_id, $types_id = '')
  1002. {
  1003. $userFieldModel = new \app\admin\model\UserField();
  1004. $userFieldData = $userFieldModel->getConfig($types, $user_id);
  1005. $userFieldData = $userFieldData ? json_decode($userFieldData, true) : [];
  1006. $grantData = getFieldGrantData($user_id);
  1007. $userLevel = isSuperAdministrators($user_id);
  1008. $fieldList = $this->getFieldList($types, $types_id);
  1009. $where = [];
  1010. if ($userFieldData) {
  1011. $fieldArr = [];
  1012. $i = 0;
  1013. foreach ($userFieldData as $k => $v) {
  1014. if (empty($fieldList[$k])) {
  1015. unset($userFieldData[$k]);
  1016. continue;
  1017. }
  1018. if (empty($v['is_hide'])) {
  1019. $fieldArr[$i]['field'] = $k;
  1020. $fieldArr[$i]['name'] = $fieldList[$k]['name'];
  1021. $fieldArr[$i]['form_type'] = $fieldList[$k]['form_type'];
  1022. $fieldArr[$i]['width'] = $v['width'] ?: '';
  1023. $i++;
  1024. }
  1025. }
  1026. $dataList = $fieldArr;
  1027. } else {
  1028. $dataList = $fieldList;
  1029. }
  1030. # 处理字段授权
  1031. foreach ($dataList as $k => $v) {
  1032. if (!$userLevel && !empty($grantData[$types])) {
  1033. $status = getFieldGrantStatus($v['field'], $grantData[$types]);
  1034. # 查看权限
  1035. if ($status['read'] == 0) unset($dataList[(int)$k]);
  1036. }
  1037. }
  1038. return array_values($dataList) ?: [];
  1039. }
  1040. /**
  1041. * 获取列表展示字段
  1042. * @return $types_id 默认为空多自定义字段条件使用
  1043. * @return void
  1044. * @author Ymob
  1045. * @datetime 2019-10-23 17:32:57
  1046. */
  1047. public function getFieldList($types, $types_id = '')
  1048. {
  1049. $newTypes = $types;
  1050. $unField = ['-1'];
  1051. if ($types == 'crm_customer_pool') {
  1052. $newTypes = 'crm_customer';
  1053. $unField = ['owner_user_id'];
  1054. }
  1055. $fieldArr = $this
  1056. ->where([
  1057. 'types' => ['IN', ['', $newTypes]],
  1058. 'form_type' => ['not in', ['file', 'form']],
  1059. 'field' => ['not in', $unField],
  1060. 'types_id' => ['eq', $types_id],
  1061. 'is_hidden'=>0
  1062. ])
  1063. ->field(['field', 'name', 'form_type,is_hidden'])
  1064. ->order('order_id asc')
  1065. ->select();
  1066. $res = [];
  1067. foreach ($fieldArr as $val) {
  1068. $res[] = $val->toArray();
  1069. }
  1070. if ($types == 'oa_examine') {
  1071. }
  1072. if (isset($this->orther_field_list[$newTypes])) {
  1073. foreach ($this->orther_field_list[$newTypes] as $val) {
  1074. $res[] = $val;
  1075. }
  1076. }
  1077. return array_column($res, null, 'field');
  1078. }
  1079. /**
  1080. * [getIndexField 列表展示字段]
  1081. * @param types 分类
  1082. * @param is_data 1 取数据时
  1083. * @author Michael_xu
  1084. */
  1085. public function getIndexField($types, $user_id, $is_data = '')
  1086. {
  1087. $apiCommon = new ApiCommon();
  1088. $userFieldModel = new \app\admin\model\UserField();
  1089. $userFieldData = $userFieldModel->getConfig($types, $user_id);
  1090. $userFieldData = $userFieldData ? json_decode($userFieldData, true) : [];
  1091. $othor_un_field = array_column($this->orther_field_list[$types], 'field');
  1092. $unField = array_merge(['pool_day', 'business-check', 'call'], $othor_un_field);
  1093. $user_id = !empty($user_id) ? $user_id : $apiCommon->userInfo['id'];
  1094. $grantData = getFieldGrantData($user_id);
  1095. $userLevel = isSuperAdministrators($user_id);
  1096. $where = [];
  1097. if ($userFieldData) {
  1098. $dataList = [];
  1099. foreach ($userFieldData as $k => $v) {
  1100. if (empty($v['is_hide']) && !in_array($k, $unField)) {
  1101. $dataList[] = $k;
  1102. }
  1103. }
  1104. } else {
  1105. $where['types'] = ['in', ['', $types]];
  1106. $dataList = $this->where($where)->column('field');
  1107. }
  1108. $newList = $dataList;
  1109. if ($is_data == 1) {
  1110. switch ($types) {
  1111. case 'crm_leads' :
  1112. $sysField = ['leads_id', 'create_time', 'update_time', 'create_user_id', 'owner_user_id', 'last_time', 'last_record'];
  1113. break;
  1114. case 'crm_business' :
  1115. $newList = [];
  1116. foreach ($dataList as $v) {
  1117. $newList[] = 'business.' . $v;
  1118. }
  1119. $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'];
  1120. break;
  1121. case 'crm_customer' :
  1122. $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'];
  1123. break;
  1124. case 'crm_customer_pool' :
  1125. $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'];
  1126. break;
  1127. case 'crm_contacts' :
  1128. $newList = [];
  1129. foreach ($dataList as $v) {
  1130. $newList[] = 'contacts.' . $v;
  1131. }
  1132. $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'];
  1133. break;
  1134. case 'crm_contract' :
  1135. $newList = [];
  1136. foreach ($dataList as $v) {
  1137. $newList[] = 'contract.' . $v;
  1138. }
  1139. $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'];
  1140. break;
  1141. case 'crm_receivables' :
  1142. $newList = [];
  1143. foreach ($dataList as $v) {
  1144. $newList[] = 'receivables.' . $v;
  1145. }
  1146. $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'];
  1147. break;
  1148. case 'crm_product' :
  1149. $newList = [];
  1150. foreach ($dataList as $v) {
  1151. $newList[] = 'product.' . $v;
  1152. }
  1153. $sysField = ['product.product_id', 'product.category_id', 'product.create_time', 'product.update_time', 'product.create_user_id', 'product.owner_user_id'];
  1154. break;
  1155. case 'crm_visit' :
  1156. $newList = [];
  1157. foreach ($dataList as $v) {
  1158. $newList[] = 'visit.' . $v;
  1159. }
  1160. $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'];
  1161. break;
  1162. }
  1163. $listArr = $sysField ? array_unique(array_merge($newList, $sysField)) : $dataList;
  1164. } else {
  1165. $listArr = $dataList;
  1166. }
  1167. $typesArray = explode('_', $types);
  1168. $type = array_pop($typesArray);
  1169. if (isset($this->orther_field_list[$types])) {
  1170. foreach ($this->orther_field_list[$types] as $val) {
  1171. if (in_array($type . '.' . $val['field'], $listArr) && !in_array($types, ['crm_contract', 'crm_business', 'crm_receivables'])) {
  1172. unset($listArr[array_search($type . '.' . $val['field'], $listArr)]);
  1173. }
  1174. }
  1175. }
  1176. # 处理字段授权
  1177. foreach ($listArr as $k => $v) {
  1178. if (!$userLevel && !empty($grantData[$types])) {
  1179. $status = getFieldGrantStatus($v, $grantData[$types]);
  1180. # 查看权限
  1181. if ($status['read'] == 0) unset($listArr[(int)$k]);
  1182. }
  1183. }
  1184. return $listArr ?: [];
  1185. }
  1186. /**
  1187. * [checkFieldPer 判断权限]
  1188. * @param types 分类
  1189. * @author Michael_xu
  1190. */
  1191. public function checkFieldPer($module, $controller, $action, $user_id = '')
  1192. {
  1193. $userModel = new \app\admin\model\User();
  1194. if (!checkPerByAction($module, $controller, $action)) return false;
  1195. if ($user_id && !in_array($user_id, $userModel->getUserByPer($module, $controller, $action))) return false;
  1196. return true;
  1197. }
  1198. /**
  1199. * [getField 获取字段属性]
  1200. * @param types 分类
  1201. * @author Michael_xu
  1202. */
  1203. public function getField($param)
  1204. {
  1205. $types = $param['types'];
  1206. $unFormType = $param['unFormType'];
  1207. if (!in_array($types, $this->types_arr)) {
  1208. return resultArray(['error' => '参数错误']);
  1209. }
  1210. $field_arr = [];
  1211. //模拟自定义字段返回
  1212. switch ($types) {
  1213. case 'admin_user' :
  1214. $field_arr = \app\hrm\model\Userdet::getField();
  1215. break;
  1216. case 'crm_invoice':
  1217. $field_arr = $this->getInvoiceSearch();
  1218. break;
  1219. default :
  1220. $data = [];
  1221. $data['types'] = $types;
  1222. $data['user_id'] = $param['user_id'];
  1223. if ($unFormType) $data['form_type'] = array('not in', $unFormType);
  1224. $field_arr = $this->fieldSearch($data);
  1225. }
  1226. if ($types == 'crm_visit') {
  1227. foreach ($field_arr AS $key => $value) {
  1228. if ($value['name'] == '负责人') unset($field_arr[(int)$key]);
  1229. }
  1230. }
  1231. return $field_arr;
  1232. }
  1233. /**
  1234. * 自定义字段验重
  1235. * @param $field 字段名, $val 值, $id 排除当前数据验重, $types 需要查询的模块名
  1236. * @return
  1237. * @author
  1238. */
  1239. public function getValidate($field, $val, $id, $types)
  1240. {
  1241. $val = trim($val);
  1242. if (!$val) {
  1243. $this->error = '验证内容不能为空';
  1244. return false;
  1245. }
  1246. if (!$field) {
  1247. $this->error = '数据验证错误,请联系管理员!';
  1248. return false;
  1249. }
  1250. if (!in_array($types, $this->types_arr)) {
  1251. $this->error = '参数错误!';
  1252. return false;
  1253. }
  1254. $field_info = db('admin_field')->where(['types' => $types, 'field' => $field])->find();
  1255. if (!$field_info) {
  1256. $this->error = '数据验证错误,请联系管理员!';
  1257. return false;
  1258. }
  1259. $dataModel = '';
  1260. switch ($types) {
  1261. case 'crm_leads' :
  1262. $dataModel = new \app\crm\model\Leads();
  1263. break;
  1264. case 'crm_customer' :
  1265. $dataModel = new \app\crm\model\Customer();
  1266. break;
  1267. case 'crm_contacts' :
  1268. $dataModel = new \app\crm\model\Contacts();
  1269. break;
  1270. case 'crm_business' :
  1271. $dataModel = new \app\crm\model\Business();
  1272. break;
  1273. case 'crm_product' :
  1274. $dataModel = new \app\crm\model\Product();
  1275. break;
  1276. case 'crm_contract' :
  1277. $dataModel = new \app\crm\model\Contract();
  1278. break;
  1279. case 'crm_receivables' :
  1280. $dataModel = new \app\crm\model\Receivables();
  1281. break;
  1282. }
  1283. $where = [];
  1284. $where[$field] = ['eq', $val];
  1285. if ($id) {
  1286. //为编辑时的验重
  1287. $where[$dataModel->getpk()] = ['neq', $id];
  1288. }
  1289. if($types=='crm_product'){
  1290. $where['delete_user_id'] = 0;
  1291. }
  1292. if ($res = $dataModel->where($where)->find()) {
  1293. $this->error = '该数据已存在,请修改后提交!';
  1294. return false;
  1295. }
  1296. return true;
  1297. }
  1298. /**
  1299. * [getFieldByFormType 根据字段类型获取字段数组]
  1300. * @param types 分类
  1301. * @author Michael_xu
  1302. */
  1303. public function getFieldByFormType($types, $form_type)
  1304. {
  1305. $fieldArr = $this->where(['types' => $types, 'form_type' => $form_type])->column('field');
  1306. return $fieldArr ?: [];
  1307. }
  1308. /**
  1309. * [getFormValueByField 格式化表格字段类型值]
  1310. * @param $field 字段名
  1311. * @param $value 字段值
  1312. * @author Michael_xu
  1313. */
  1314. public function getFormValueByField($field, $value)
  1315. {
  1316. $formValue = db('admin_field')->where(['field' => $field])->value('form_value');
  1317. $formValue = sort_select($formValue, 'order_id', 1); //二维数组排序
  1318. $field = [];
  1319. foreach ($formValue as $k => $v) {
  1320. $field[] = $v['field'];
  1321. }
  1322. $data = [];
  1323. foreach ($value as $k => $v) {
  1324. foreach ($field as $key => $val) {
  1325. $data[$k][$val] = $v['value'];
  1326. }
  1327. }
  1328. return $data;
  1329. }
  1330. /**
  1331. * 根据form_type处理数据
  1332. * @author lee
  1333. */
  1334. public function getValueByFormtype($val, $form_type)
  1335. {
  1336. $userModel = new \app\admin\model\User();
  1337. $structureModel = new \app\admin\model\Structure();
  1338. switch ($form_type) {
  1339. // case 'datetime' :
  1340. // $val = $val > 0 ? date('Y-m-d H:i:s', $val) : '';
  1341. // break;
  1342. case 'user' :
  1343. $val = count($userModel->getUserNameByArr($val)) > 1 ? ArrayToString($userModel->getUserNameByArr($val)) : implode(',', $userModel->getUserNameByArr(stringToArray($val)));
  1344. break;
  1345. case 'userStr' :
  1346. $val = explode(',', $val);
  1347. $val = count($userModel->getUserNameByArr($val)) > 1 ? ArrayToString($userModel->getUserNameByArr($val)) : implode(',', $userModel->getUserNameByArr($val));
  1348. break;
  1349. case 'structure' :
  1350. $val = implode(',', $structureModel->getStructureNameByArr(stringToArray($val)));
  1351. break;
  1352. case 'customer' :
  1353. $val = db('crm_customer')->where(['customer_id' => $val])->value('name');
  1354. break;
  1355. case 'contract' :
  1356. $val = db('crm_contract')->where(['customer_id' => $val])->value('num');
  1357. break;
  1358. case 'business' :
  1359. $val = db('crm_business')->where(['business_id' => $val])->value('name');
  1360. break;
  1361. case 'category' :
  1362. $val = db('crm_product_category')->where(['category_id' => $val])->value('name');
  1363. break;
  1364. case 'business_type' :
  1365. $val = db('crm_business_type')->where(['type_id' => $val])->value('name');
  1366. break;
  1367. case 'business_status' :
  1368. $val = db('crm_business_status')->where(['status_id' => $val])->value('name');
  1369. break;
  1370. }
  1371. return $val;
  1372. }
  1373. /**
  1374. * [getIndexFieldList 列表展示字段]
  1375. * @param types 分类
  1376. * @author Michael_xu
  1377. */
  1378. public function getIndexFieldList($types, $user_id)
  1379. {
  1380. $fieldArr = $this->getIndexField($types, $user_id);
  1381. $types = $types == 'crm_customer_pool' ? 'crm_customer' : $types;
  1382. $fieldList = db('admin_field')->where(['field' => array('in', $fieldArr)])->where('types', ['eq', $types], ['eq', ''], 'or')->order('order_id asc')->select();
  1383. return $fieldList ?: [];
  1384. }
  1385. /**
  1386. * [getArrayField 数组类型字段]
  1387. * @param types 分类
  1388. * @author Michael_xu
  1389. */
  1390. public function getArrayField($types)
  1391. {
  1392. $arrayFormType = ['structure', 'user', 'checkbox', 'file'];
  1393. $arrFieldAtt = db('admin_field')->where(['types' => $types, 'form_type' => ['in', $arrayFormType]])->column('field');
  1394. return $arrFieldAtt ?: [];
  1395. }
  1396. /**
  1397. * 字段对照关系处理
  1398. * @param $types 分类
  1399. * @param $data 数据
  1400. * @return
  1401. * @author Michael_xu
  1402. */
  1403. public function getRelevantData($types, $data = [])
  1404. {
  1405. $types_arr = ['crm_leads'];
  1406. if (!in_array($types, $types_arr)) {
  1407. $this->error = '参数错误';
  1408. return false;
  1409. }
  1410. if (!$data) return $data;
  1411. $list = $this->where(['types' => $types, 'relevant' => ['neq', '']])->field('field,relevant')->select();
  1412. if (!$list) return $data;
  1413. $newData = $data;
  1414. foreach ($list as $k => $v) {
  1415. $newData[$v['relevant']] = $data[$v['field']];
  1416. }
  1417. return $newData ?: [];
  1418. }
  1419. /**
  1420. * 字段排序
  1421. * @param types 自定义字段分类
  1422. * @param prefix 自定义字段前缀
  1423. * @param field 自定义字段
  1424. * @param order 排序规则
  1425. * @return
  1426. * @author Michael_xu
  1427. */
  1428. public function getOrderByFormtype($types, $prefix, $field, $order_type)
  1429. {
  1430. $form_type = $this->where(['types' => $types, 'field' => $field])->value('form_type');
  1431. // if (!$form_type) {
  1432. // $this->error = '参数错误';
  1433. // return false;
  1434. // }
  1435. $temp_field = $field;
  1436. $field = $prefix ? $prefix . '.' . $field : $field;
  1437. switch ($form_type) {
  1438. case 'textarea' :
  1439. case 'radio' :
  1440. case 'select' :
  1441. case 'checkbox' :
  1442. case 'address' :
  1443. $order = 'convert(' . $field . ' using gbk) ' . trim($order_type);
  1444. break;
  1445. default :
  1446. $order = $field . ' ' . $order_type;
  1447. break;
  1448. }
  1449. if (isset($this->orther_field_list[$types])) {
  1450. foreach ($this->orther_field_list[$types] as $val) {
  1451. // $res[] = $val;
  1452. // $order
  1453. $temp = trim($prefix . '.' . $val['field'], '.');
  1454. if ($temp == $field) {
  1455. $order = str_replace($temp, $val['field'], $order);
  1456. }
  1457. }
  1458. }
  1459. return $order;
  1460. }
  1461. /**
  1462. * 获取发票高级搜索字段
  1463. *
  1464. * @return array[]
  1465. */
  1466. private function getInvoiceSearch()
  1467. {
  1468. return [
  1469. ['field' => 'invoice_number', 'form_type' => 'text', 'setting' => [], 'name' => '发票号码'],
  1470. ['field' => 'real_invoice_date', 'form_type' => 'datetime', 'setting' => [], 'name' => '实际开票日期'],
  1471. ['field' => 'logistics_number', 'form_type' => 'text', 'setting' => [], 'name' => '物流单号'],
  1472. ['field' => 'invoice_status', 'form_type' => 'select', 'setting' => ['未开票', '已开票'], 'name' => '开票状态'],
  1473. ['field' => 'check_status', 'form_type' => 'select', 'setting' => ['待审核', '审核中', '审核通过', '审核未通过', '撤回'], 'name' => '审核状态'],
  1474. ['field' => 'owner_user_id', 'form_type' => 'user', 'setting' => [], 'name' => '负责人']
  1475. ];
  1476. }
  1477. public function resetField($types, $data)
  1478. {
  1479. foreach ($data AS $key => $value) {
  1480. switch ($value['field']) {
  1481. case 'create_user_id' :
  1482. $data[$key]['fieldName'] = 'create_user_name';
  1483. break;
  1484. case 'owner_user_id' :
  1485. $data[$key]['fieldName'] = 'owner_user_name';
  1486. break;
  1487. case 'customer_id' :
  1488. $data[$key]['fieldName'] = 'customer_name';
  1489. break;
  1490. case 'type_id' :
  1491. $data[$key]['fieldName'] = 'type_id_info';
  1492. break;
  1493. case 'status_id' :
  1494. $data[$key]['fieldName'] = 'status_id_info';
  1495. break;
  1496. case 'business_id' :
  1497. $data[$key]['fieldName'] = 'business_name';
  1498. break;
  1499. case 'contacts_id' :
  1500. $data[$key]['fieldName'] = 'contacts_name';
  1501. break;
  1502. case 'order_user_id' :
  1503. $data[$key]['fieldName'] = 'order_user_name';
  1504. break;
  1505. case 'contract_id' :
  1506. $data[$key]['fieldName'] = 'contract_num';
  1507. break;
  1508. case 'plan_id' :
  1509. $data[$key]['fieldName'] = 'plan_id_info';
  1510. break;
  1511. case 'category_id' :
  1512. $data[$key]['fieldName'] = 'category_name';
  1513. break;
  1514. default :
  1515. $data[$key]['fieldName'] = $value['field'];
  1516. }
  1517. if (in_array($value['form_type'], ['user', 'structure']) && !in_array($value['field'], ['create_user_id', 'owner_user_id'])) {
  1518. $data[$key]['fieldName'] = $value['field'] . '_name';
  1519. }
  1520. }
  1521. # 线索
  1522. // if ($types == 'crm_leads') {
  1523. // foreach ($data as $key => $value) {
  1524. // switch ($value['field']) {
  1525. // case 'create_user_id' :
  1526. // $data[$key]['fieldName'] = 'create_user_name';
  1527. // break;
  1528. // case 'owner_user_id' :
  1529. // $data[$key]['fieldName'] = 'owner_user_name';
  1530. // break;
  1531. // default :
  1532. // $data[$key]['fieldName'] = $value['field'];
  1533. // }
  1534. // }
  1535. // }
  1536. # 客户
  1537. // if ($types == 'crm_customer') {
  1538. // foreach ($data as $key => $value) {
  1539. // switch ($value['field']) {
  1540. // case 'create_user_id' :
  1541. // $data[$key]['fieldName'] = 'create_user_name';
  1542. // break;
  1543. // case 'owner_user_id' :
  1544. // $data[$key]['fieldName'] = 'owner_user_name';
  1545. // break;
  1546. // default :
  1547. // $data[$key]['fieldName'] = $value['field'];
  1548. // }
  1549. // }
  1550. // }
  1551. # 联系人
  1552. // if ($types == 'crm_contacts') {
  1553. // foreach ($data as $key => $value) {
  1554. // switch ($value['field']) {
  1555. // case 'customer_id' :
  1556. // $data[$key]['fieldName'] = 'customer_name';
  1557. // break;
  1558. // case 'create_user_id' :
  1559. // $data[$key]['fieldName'] = 'create_user_name';
  1560. // break;
  1561. // case 'owner_user_id' :
  1562. // $data[$key]['fieldName'] = 'owner_user_name';
  1563. // break;
  1564. // default :
  1565. // $data[$key]['fieldName'] = $value['field'];
  1566. // }
  1567. // }
  1568. // }
  1569. # 商机
  1570. // if ($types == 'crm_business') {
  1571. // foreach ($data as $key => $value) {
  1572. // switch ($value['field']) {
  1573. // case 'customer_id' :
  1574. // $data[$key]['fieldName'] = 'customer_name';
  1575. // break;
  1576. // case 'type_id' :
  1577. // $data[$key]['fieldName'] = 'type_id_info';
  1578. // break;
  1579. // case 'status_id' :
  1580. // $data[$key]['fieldName'] = 'status_id_info';
  1581. // break;
  1582. // case 'create_user_id' :
  1583. // $data[$key]['fieldName'] = 'create_user_name';
  1584. // break;
  1585. // case 'owner_user_id' :
  1586. // $data[$key]['fieldName'] = 'owner_user_name';
  1587. // break;
  1588. // default :
  1589. // $data[$key]['fieldName'] = $value['field'];
  1590. // }
  1591. // }
  1592. // }
  1593. # 合同
  1594. // if ($types == 'crm_contract') {
  1595. // foreach ($data as $key => $value) {
  1596. // switch ($value['field']) {
  1597. // case 'customer_id' :
  1598. // $data[$key]['fieldName'] = 'customer_name';
  1599. // break;
  1600. // case 'business_id' :
  1601. // $data[$key]['fieldName'] = 'business_name';
  1602. // break;
  1603. // case 'contacts_id' :
  1604. // $data[$key]['fieldName'] = 'contacts_name';
  1605. // break;
  1606. // case 'order_user_id' :
  1607. // $data[$key]['fieldName'] = 'order_user_name';
  1608. // break;
  1609. // case 'create_user_id' :
  1610. // $data[$key]['fieldName'] = 'create_user_name';
  1611. // break;
  1612. // case 'owner_user_id' :
  1613. // $data[$key]['fieldName'] = 'owner_user_name';
  1614. // break;
  1615. // default :
  1616. // $data[$key]['fieldName'] = $value['field'];
  1617. // }
  1618. // }
  1619. // }
  1620. # 回款
  1621. // if ($types == 'crm_receivables') {
  1622. // foreach ($data as $key => $value) {
  1623. // switch ($value['field']) {
  1624. // case 'customer_id' :
  1625. // $data[$key]['fieldName'] = 'customer_name';
  1626. // break;
  1627. // case 'contract_id' :
  1628. // $data[$key]['fieldName'] = 'contract_num';
  1629. // break;
  1630. // case 'create_user_id' :
  1631. // $data[$key]['fieldName'] = 'create_user_name';
  1632. // break;
  1633. // case 'owner_user_id' :
  1634. // $data[$key]['fieldName'] = 'owner_user_name';
  1635. // break;
  1636. // case 'plan_id' :
  1637. // $data[$key]['fieldName'] = 'plan_id_info';
  1638. // break;
  1639. // default :
  1640. // $data[$key]['fieldName'] = $value['field'];
  1641. // }
  1642. // }
  1643. // }
  1644. # 回访
  1645. // if ($types == 'crm_visit') {
  1646. // foreach ($data as $key => $value) {
  1647. // switch ($value['field']) {
  1648. // case 'customer_id' :
  1649. // $data[$key]['fieldName'] = 'customer_name';
  1650. // break;
  1651. // case 'owner_user_id' :
  1652. // $data[$key]['fieldName'] = 'owner_user_name';
  1653. // $data[$key]['name'] = '回访人';
  1654. // break;
  1655. // case 'contacts_id' :
  1656. // $data[$key]['fieldName'] = 'contacts_name';
  1657. // break;
  1658. // case 'contract_id' :
  1659. // $data[$key]['fieldName'] = 'contract_number';
  1660. // break;
  1661. // case 'create_user_id' :
  1662. // $data[$key]['fieldName'] = 'create_user_name';
  1663. // break;
  1664. // default :
  1665. // $data[$key]['fieldName'] = $value['field'];
  1666. // }
  1667. // }
  1668. // }
  1669. # 产品
  1670. // if ($types == 'crm_product') {
  1671. // foreach ($data as $key => $value) {
  1672. // switch ($value['field']) {
  1673. // case 'category_id' :
  1674. // $data[$key]['fieldName'] = 'category_name';
  1675. // break;
  1676. // case 'create_user_id' :
  1677. // $data[$key]['fieldName'] = 'create_user_name';
  1678. // break;
  1679. // case 'owner_user_id' :
  1680. // $data[$key]['fieldName'] = 'owner_user_name';
  1681. // break;
  1682. // default :
  1683. // $data[$key]['fieldName'] = $value['field'];
  1684. // }
  1685. // }
  1686. // }
  1687. return $data;
  1688. }
  1689. /**
  1690. * 获取产品父类层级(不包含自身)
  1691. *
  1692. * @param $productId
  1693. * @param array $parentIds
  1694. * @return array|mixed
  1695. * @throws \think\db\exception\DataNotFoundException
  1696. * @throws \think\db\exception\ModelNotFoundException
  1697. * @throws \think\exception\DbException
  1698. */
  1699. private function getProductParentIds($productId, &$parentIds = [])
  1700. {
  1701. $category = db('crm_product_category')->select();
  1702. foreach ($category as $key => $value) {
  1703. if ($value['category_id'] == $productId) {
  1704. if (!empty($value['pid'])) {
  1705. $parentIds[] = $value['pid'];
  1706. $this->getProductParentIds($value['pid'], $parentIds);
  1707. }
  1708. }
  1709. }
  1710. return $parentIds;
  1711. }
  1712. }