Field.php 122KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648
  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 = [
  28. 'crm_leads',
  29. 'crm_customer',
  30. 'crm_contacts',
  31. 'crm_product',
  32. 'crm_business',
  33. 'crm_contract',
  34. 'oa_examine',
  35. 'hrm_parroll',
  36. 'admin_user',
  37. 'crm_receivables',
  38. 'crm_receivables_plan',
  39. 'crm_invoice',
  40. 'crm_visit',
  41. 'jxc_product',
  42. 'jxc_supplier',
  43. 'jxc_purchase',
  44. 'jxc_retreat',
  45. 'jxc_sale',
  46. 'jxc_salereturn',
  47. 'jxc_receipt',
  48. 'jxc_outbound',
  49. 'jxc_payment',
  50. 'jxc_collection',
  51. 'jxc_inventory',
  52. 'jxc_allocation',
  53. ]; //支持自定义字段的表,不包含表前缀
  54. private $formtype_arr = [
  55. 'text',
  56. 'pic',
  57. 'textarea',
  58. 'mobile',
  59. 'email',
  60. 'number',
  61. 'floatnumber',
  62. 'radio',
  63. 'select',
  64. 'checkbox',
  65. 'date',
  66. 'datetime',
  67. 'address',
  68. 'user',
  69. 'file',
  70. 'structure',
  71. 'boolean_value', # 布尔值
  72. 'percent', # 百分数
  73. 'website', # 网址
  74. 'position', # 地址
  75. 'location', # 地址
  76. 'handwriting_sign', # 手写签名
  77. 'date_interval', # 日期区间
  78. 'desc_text', # 描述类型
  79. 'detail_table', # 明细表格
  80. ];
  81. protected $type = [
  82. 'form_value' => 'array',
  83. ];
  84. /**
  85. * 列表展示额外关联字段
  86. */
  87. public $orther_field_list = [
  88. 'crm_leads' => [
  89. [
  90. 'field' => 'last_record',
  91. 'name' => '最后跟进记录',
  92. 'form_type' => 'text',
  93. 'width' => '',
  94. 'is_hidden' => 0,
  95. ],
  96. [
  97. 'field' => 'last_time',
  98. 'name' => '最后跟进时间',
  99. 'form_type' => 'datetime',
  100. 'width' => '',
  101. 'is_hidden' => 0,
  102. ],
  103. ],
  104. 'crm_customer' => [
  105. [
  106. 'field' => 'last_record',
  107. 'name' => '跟进记录',
  108. 'form_type' => 'text',
  109. 'width' => '',
  110. 'is_hidden' => 0,
  111. ],
  112. [
  113. 'field' => 'last_time',
  114. 'name' => '最后跟进时间',
  115. 'form_type' => 'datetime',
  116. 'width' => '',
  117. 'is_hidden' => 0,
  118. ],
  119. [
  120. 'field' => 'address',
  121. 'name' => '省、市、区/县',
  122. 'form_type' => 'customer_address',
  123. 'width' => '',
  124. 'is_hidden' => 0,
  125. ],
  126. [
  127. 'field' => 'detail_address',
  128. 'name' => '详细地址',
  129. 'form_type' => 'text',
  130. 'width' => '',
  131. 'is_hidden' => 0,
  132. ]
  133. ],
  134. 'crm_contacts' => [
  135. [
  136. 'field' => 'last_record',
  137. 'name' => '跟进记录',
  138. 'form_type' => 'text',
  139. 'width' => '',
  140. 'is_hidden' => 0,
  141. ],
  142. [
  143. 'field' => 'last_time',
  144. 'name' => '最后跟进时间',
  145. 'form_type' => 'datetime',
  146. 'width' => '',
  147. 'is_hidden' => 0,
  148. ],
  149. ],
  150. 'crm_business' => [
  151. [
  152. 'field' => 'last_record',
  153. 'name' => '跟进记录',
  154. 'form_type' => 'text',
  155. 'width' => '',
  156. 'is_hidden' => 0,
  157. ],
  158. [
  159. 'field' => 'last_time',
  160. 'name' => '最后跟进时间',
  161. 'form_type' => 'datetime',
  162. 'width' => '',
  163. 'is_hidden' => 0,
  164. ],
  165. ],
  166. 'crm_contract' => [
  167. [
  168. 'field' => 'check_status',
  169. 'name' => '审核状态',
  170. 'form_type' => 'text',
  171. 'width' => '',
  172. 'is_hidden' => 0,
  173. ],
  174. [
  175. 'field' => 'last_record',
  176. 'name' => '跟进记录',
  177. 'form_type' => 'text',
  178. 'width' => '',
  179. 'is_hidden' => 0,
  180. ],
  181. [
  182. 'field' => 'last_time',
  183. 'name' => '最后跟进时间',
  184. 'form_type' => 'datetime',
  185. 'width' => '',
  186. 'is_hidden' => 0,
  187. ],
  188. [
  189. 'field' => 'done_money',
  190. 'name' => '已回款',
  191. 'form_type' => 'floatnumber',
  192. 'width' => '',
  193. 'is_hidden' => 0,
  194. ],
  195. [
  196. 'field' => 'un_money',
  197. 'name' => '未回款',
  198. 'form_type' => 'floatnumber',
  199. 'width' => '',
  200. 'is_hidden' => 0,
  201. ]
  202. ],
  203. 'crm_receivables' => [
  204. [
  205. 'field' => 'check_status',
  206. 'name' => '审核状态',
  207. 'form_type' => 'text',
  208. 'width' => '',
  209. 'is_hidden' => 0,
  210. ],
  211. [
  212. 'field' => 'contract_money',
  213. 'name' => '合同金额',
  214. 'form_type' => 'floatnumber',
  215. 'width' => '',
  216. 'is_hidden' => 0,
  217. ]
  218. ],
  219. 'jxc_supplier' => [
  220. [
  221. 'field' => 'detail_address',
  222. 'name' => '地址',
  223. 'form_type' => 'map_address',
  224. 'width' => '',
  225. 'is_hidden' => 0,
  226. ]
  227. ],
  228. 'jxc_purchase' => [
  229. [
  230. 'field' => 'check_status',
  231. 'name' => '审核状态',
  232. 'form_type' => 'text',
  233. 'width' => '',
  234. 'is_hidden' => 0,
  235. ]
  236. ],
  237. 'jxc_retreat' => [
  238. [
  239. 'field' => 'check_status',
  240. 'name' => '审核状态',
  241. 'form_type' => 'text',
  242. 'width' => '',
  243. 'is_hidden' => 0,
  244. ]
  245. ],
  246. 'jxc_product' => [
  247. [
  248. 'field' => 'product_code',
  249. 'name' => '产品编码',
  250. 'form_type' => 'text',
  251. 'width' => '',
  252. 'is_hidden' => 0,
  253. ],
  254. [
  255. 'field' => 'product_picture',
  256. 'name' => '产品图片',
  257. 'form_type' => 'text',
  258. 'width' => '',
  259. 'is_hidden' => 0,
  260. ],
  261. [
  262. 'field' => 'sp_data_value',
  263. 'name' => '产品规格',
  264. 'form_type' => 'text',
  265. 'width' => '',
  266. 'is_hidden' => 0,
  267. ]
  268. ],
  269. 'jxc_sale' => [
  270. [
  271. 'field' => 'check_status',
  272. 'name' => '审核状态',
  273. 'form_type' => 'text',
  274. 'width' => '',
  275. 'is_hidden' => 0,
  276. ]
  277. ],
  278. 'jxc_salereturn' => [
  279. [
  280. 'field' => 'check_status',
  281. 'name' => '审核状态',
  282. 'form_type' => 'text',
  283. 'width' => '',
  284. 'is_hidden' => 0,
  285. ]
  286. ],
  287. 'jxc_receipt' => [
  288. [
  289. 'field' => 'state',
  290. 'name' => '状态',
  291. 'form_type' => 'text',
  292. 'width' => '',
  293. 'is_hidden' => 0,
  294. ]
  295. ],
  296. 'jxc_outbound' => [
  297. [
  298. 'field' => 'state',
  299. 'name' => '状态',
  300. 'form_type' => 'text',
  301. 'width' => '',
  302. 'is_hidden' => 0,
  303. ]
  304. ],
  305. 'jxc_allocation' => [
  306. [
  307. 'field' => 'check_status',
  308. 'name' => '审核状态',
  309. 'form_type' => 'text',
  310. 'width' => '',
  311. 'is_hidden' => 0,
  312. ]
  313. ],
  314. 'jxc_inventory' => [
  315. [
  316. 'field' => 'check_status',
  317. 'name' => '审核状态',
  318. 'form_type' => 'text',
  319. 'width' => '',
  320. 'is_hidden' => 0,
  321. ]
  322. ],
  323. 'jxc_collection' => [
  324. [
  325. 'field' => 'check_status',
  326. 'name' => '审核状态',
  327. 'form_type' => 'text',
  328. 'width' => '',
  329. 'is_hidden' => 0,
  330. ]
  331. ],
  332. 'jxc_payment' => [
  333. [
  334. 'field' => 'check_status',
  335. 'name' => '审核状态',
  336. 'form_type' => 'text',
  337. 'width' => '',
  338. 'is_hidden' => 0,
  339. ]
  340. ],
  341. ];
  342. protected function initialize()
  343. {
  344. $this->__db_prefix = Config::get('database.prefix');
  345. }
  346. /**
  347. * [getDataList 获取列表]
  348. * @param types 分类
  349. * @return [array]
  350. * @author Michael_xu
  351. */
  352. public function getDataList($param)
  353. {
  354. $types = trim($param['types']);
  355. if (!in_array($types, $this->types_arr)) {
  356. $this->error = '参数错误';
  357. return false;
  358. }
  359. $map = $param;
  360. if ($types == 'oa_examine') {
  361. $map['types_id'] = $param['types_id'];
  362. }
  363. if ($param['types'] == 'crm_customer') {
  364. $map['field'] = array('not in', ['deal_status']);
  365. }
  366. if ($types == 'crm_receivables_plan') {
  367. $setting = Db::name('AdminField')->where('types', 'crm_receivables')->value('setting');
  368. }
  369. $list = Db::name('AdminField')->where($map)->order('form_position', 'asc')->select();
  370. $detailTableList = db('admin_field_extend')->field(['field', 'content'])->where('types', $types)->select();
  371. $detailTableData = [];
  372. foreach ($detailTableList as $key => $value) {
  373. $detailTableData[$value['field']] = !empty($value['content']) ? json_decode($value['content'], true) : [];
  374. }
  375. foreach ($list as $k => $v) {
  376. $list[$k]['setting'] = $v['setting'] ? explode(chr(10), $v['setting']) : [];
  377. if ($types == 'crm_receivables_plan') {
  378. $list[$k]['stting'] = $setting ? explode(chr(10), $v['setting']) : [];
  379. }
  380. if ($v['form_type'] == 'checkbox') {
  381. $list[$k]['default_value'] = $v['default_value'] ? explode(',', $v['default_value']) : array();
  382. }
  383. if ($v['form_type']=='date_interval') {
  384. $list[$k]['default_value'] = !empty($v['default_value']) ? explode(',',$v['default_value']) : [];
  385. }
  386. if($v['form_type']=='position'){
  387. $list[$k]['default_value'] = !empty($v['default_value']) ? json_decode($v['default_value'], true) : [];
  388. }
  389. if ($v['form_type'] == 'detail_table') {
  390. $list[$k]['fieldExtendList'] = !empty($detailTableData[$v['field']]) ? $detailTableData[$v['field']] : [];
  391. }
  392. if (!empty($v['form_position'])) {
  393. $coordinate = explode(',', $v['form_position']);
  394. $list[$k]['xaxis'] = (int)$coordinate[0];
  395. $list[$k]['yaxis'] = (int)$coordinate[1];
  396. }
  397. if (!empty($v['relevant'])) {
  398. $list[$k]['relevant'] = !empty($v['relevant']) ? (int)$v['relevant'] : [];
  399. }
  400. if (!empty($v['options'])) {
  401. $list[$k]['optionsData'] = json_decode($v['options'], true);
  402. } else {
  403. $list[$k]['options'] = $v['setting'];
  404. }
  405. // 处理数值范围字段
  406. $list[$k]['minNumRestrict'] = $v['min_num_restrict'];
  407. $list[$k]['maxNumRestrict'] = $v['max_num_restrict'];
  408. unset($list[$k]['min_num_restrict']);
  409. unset($list[$k]['max_num_restrict']);
  410. }
  411. return getFieldGroupOrderData((array)$list);
  412. }
  413. /**
  414. * [createData 创建自定义字段]
  415. * @param types 分类
  416. * @param field 字段名
  417. * @param name 字段标识名(字段注释)
  418. * @param form_type 字段类型
  419. * @param max_length 字段最大长度
  420. * @param default_value 默认值
  421. * @param setting 单选、下拉、多选类型的选项值
  422. * @return [array]
  423. * @author Michael_xu
  424. */
  425. public function createData($types, $param)
  426. {
  427. if (!$types || !in_array($types, $this->types_arr) || !is_array($param)) {
  428. $this->error = '参数错误';
  429. return false;
  430. }
  431. # 公海数据
  432. $poolList = [];
  433. $poolData = [];
  434. if ($types == 'crm_customer') {
  435. $poolList = db('crm_customer_pool')->column('pool_id');
  436. }
  437. # 用户自定义字段
  438. $userFields = db('admin_user_field')->field(['id', 'datas'])->where('types', $types)->select();
  439. # 获取最大formAssistId
  440. $formAssistId = db('admin_field')->where('types', $types)->order('formAssistId', 'desc')->value('formAssistId');
  441. $formAssistId = !empty($formAssistId) ? $formAssistId : 1000;
  442. $error_message = [];
  443. $i = 0;
  444. foreach ($param as $k => $data) {
  445. // 设置$formAssistId值
  446. $formAssistId += 1;
  447. $data['formAssistId'] = $formAssistId;
  448. // 数值范围
  449. if (!empty($data['minNumRestrict'])) $data['min_num_restrict'] = $data['minNumRestrict'];
  450. if (!empty($data['maxNumRestrict'])) $data['max_num_restrict'] = $data['maxNumRestrict'];
  451. // 清除坐标
  452. unset($data['xaxis']);
  453. unset($data['yaxis']);
  454. unset($data['maxNumRestrict']);
  455. unset($data['minNumRestrict']);
  456. // 设置明细表格类型的默认值为空,防止为null的报错。
  457. if ($data['form_type'] == 'detail_table') {
  458. $data['default_value'] = '';
  459. }
  460. $i++;
  461. $data['types'] = $types;
  462. if ($types == 'oa_examine' && !$data['types_id']) {
  463. $error_message[] = $data['name'] . '参数错误';
  464. }
  465. $data['types_id'] = $data['types_id'] ?: 0;
  466. if (!in_array($data['form_type'], $this->formtype_arr)) {
  467. $error_message[] = $data['name'] . ',字段类型错误';
  468. }
  469. //生成字段名
  470. if (!$data['field']) $data['field'] = $this->createField($types, $types == 'oa_examine' ? 'oa_' : 'crm_');
  471. $rule = [
  472. 'field' => ['regex' => '/^[a-z]([a-z]|_)+[a-z]$/i'],
  473. // 'name' => 'require',
  474. 'types' => 'require',
  475. 'form_type' => 'require',
  476. ];
  477. $msg = [
  478. 'field.regex' => '字段名称格式不正确!',
  479. // 'name.require' => '字段标识必须填写',
  480. 'types.require' => '分类必须填写',
  481. 'form_type.require' => '字段类型必须填写',
  482. ];
  483. // 验证
  484. // $validate = validate($this->name);
  485. $validate = new Validate($rule, $msg);
  486. if (!$validate->check($data)) {
  487. $error_message[] = $validate->getError();
  488. } else {
  489. //单选、下拉、多选类型(使用回车符隔开)
  490. if (in_array($data['form_type'], ['radio', 'select', 'checkbox']) && $data['setting']) {
  491. $data = $this->settingValue($data);
  492. }
  493. //表格类型
  494. if ($data['form_type'] == 'form' && $data['form_value']) {
  495. $new_form_value = [];
  496. foreach ($data['form_value'] as $form => $fromVal) {
  497. $fromVal['field'] = 'form_' . $this->createField($types);
  498. if (in_array($fromVal['form_type'], ['radio', 'select', 'checkbox']) && $fromVal['setting']) {
  499. $fromVal = $this->settingValue($fromVal);
  500. }
  501. }
  502. $new_form_value = $fromVal;
  503. $data['form_value'] = $new_form_value;
  504. }
  505. # 处理日期区间、地址类型的默认数据
  506. // if (in_array($data['form_type'], ['position', 'date_interval']) && !empty($data['default_value'])) {
  507. // $data['default_value'] = json_encode($data['default_value']);
  508. // }
  509. # 处理明细表格中的字段数据
  510. if ($data['form_type'] == 'detail_table' && !empty($data['fieldExtendList']) && $this->setDetailTableData($types, $data['field'], $data['fieldExtendList']) === false) {
  511. $error_message[] = '创建明细表单失败!';
  512. }
  513. # 处理选项中的逻辑表单数据
  514. // if (in_array($data['form_type'], ['select', 'checkbox'])) {
  515. // $data['options'] = !empty($data['options']) ? json_encode($data['options']) : '';
  516. // }
  517. # 设置描述文字类型的字段名称
  518. if (empty($data['name']) && $data['form_type'] == 'desc_text') {
  519. $data['name'] = '描述文字';
  520. }
  521. unset($data['field_id']);
  522. if ($i > 1) {
  523. $resField = $this->data($data)->allowField(true)->isUpdate(false)->save();
  524. } else {
  525. $resField = $this->data($data)->allowField(true)->save();
  526. }
  527. # 处理公海字段数据
  528. if ($types == 'crm_customer') {
  529. foreach ($poolList as $k1 => $poolId) {
  530. $poolData[] = [
  531. 'pool_id' => $poolId,
  532. 'name' => $data['name'],
  533. 'field_name' => $data['field'],
  534. 'form_type' => $data['form_type'],
  535. 'is_null' => $data['is_null'],
  536. 'is_unique' => $data['is_unique'],
  537. 'is_hidden' => 1
  538. ];
  539. }
  540. }
  541. if ($types !== 'oa_examine') {
  542. if ($resField) {
  543. $this->tableName = $types;
  544. $maxlength = '255';
  545. $defaultvalue = $data['default_value'] ? "DEFAULT '" . $data['default_value'] . "'" : "DEFAULT NULL";
  546. //根据字段类型,创建字段
  547. switch ($data['form_type']) {
  548. case 'address' :
  549. $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'] . "'";
  550. break;
  551. case 'radio' :
  552. case 'select' :
  553. case 'checkbox' :
  554. $defaultvalue = $data['default_value'] ? "DEFAULT '" . $data['default_value'] . "'" : '';
  555. $maxlength = 500;
  556. $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'] . "'";
  557. break;
  558. case 'textarea' :
  559. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` TEXT COMMENT '" . $data['name'] . "'";
  560. break;
  561. case 'number' :
  562. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  563. break;
  564. case 'floatnumber' :
  565. $defaultvalue = abs(intval($data['default_value'])) > 9999999999999999.99 ? 9999999999999999.99 : intval($data['default_value']);
  566. $maxlength = 18;
  567. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` decimal (" . $maxlength . ",2) DEFAULT '" . $defaultvalue . "' COMMENT '" . $data['name'] . "'";
  568. break;
  569. case 'date' :
  570. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` DATE " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  571. break;
  572. case 'datetime' :
  573. $defaultvalue = $data['default_value'] ? "DEFAULT '" . strtotime($data['default_value']) . "'" : '';
  574. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` int (11) " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  575. break;
  576. case 'file' :
  577. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR ( " . $maxlength . " ) CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT '" . $data['name'] . "'";
  578. break;
  579. case 'form' :
  580. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT '" . $data['name'] . "'";
  581. break;
  582. case 'boolean_value' :
  583. # 布尔值类型字段
  584. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` TINYINT(1) unsigned NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  585. break;
  586. case 'percent' :
  587. # 百分数类型字段
  588. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  589. break;
  590. case 'position' :
  591. # 地址类型字段,存放在相应的数据扩展表中,比如crm_customer_extra_data
  592. $addressValue = [];
  593. if (!empty($data['default_value'])) {
  594. $data['default_value'] = json_decode($data['default_value'], true);
  595. foreach ($data['default_value'] as $kk => $vv) {
  596. if (!empty($vv['name'])) $addressValue[] = $vv['name'];
  597. }
  598. }
  599. $defaultValue = !empty($addressValue) ? "DEFAULT '" . implode(',', $addressValue) . "'" : "DEFAULT NULL";
  600. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultValue . " COMMENT '" . $data['name'] . "'";
  601. break;
  602. case 'location' :
  603. # 定位类型字段,存放在相应的数据扩展表中,比如crm_customer_extra_data
  604. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  605. break;
  606. case 'handwriting_sign' :
  607. # 手写签名类型字段
  608. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` INT(10) unsigned NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  609. break;
  610. case 'date_interval' :
  611. # 日期区间类型字段,存放在相应的数据扩展表中,比如crm_customer_extra_data
  612. $defaultValue = !empty($data['default_value']) ? "DEFAULT '" . implode('_', json_decode($data['default_value'], true)) . "'" : "DEFAULT NULL";
  613. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultValue . " COMMENT '" . $data['name'] . "'";
  614. break;
  615. case 'desc_text' :
  616. # 描述文字类型字段
  617. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(1000) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  618. break;
  619. case 'detail_table' :
  620. # 明细表格类型字段,存放在相应的数据扩展表中,比如crm_customer_extra_data
  621. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` ADD `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  622. break;
  623. default :
  624. $maxlength = 255;
  625. $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'] . "'";
  626. }
  627. if (!empty($this->queryStr)) {
  628. $resData = Db::execute($this->queryStr);
  629. if ($resData === false) {
  630. $this->where(['field_id' => $this->field_id])->delete();
  631. $error_message[] = $data['name'] . ',添加失败';
  632. }
  633. }
  634. # 处理用户自定义字段数据
  635. if (!empty($userFields)) {
  636. foreach ($userFields as $key => $value) {
  637. if (in_array($data['form_type'], ['handwriting_sign', 'desc_text', 'detail_table'])) continue;
  638. $userFields[$key]['datas'] = json_decode($value['datas'], true);
  639. $userFields[$key]['datas'][$data['field']] = ['width' => '', 'is_hide' => 0];
  640. $userFields[$key]['datas'] = json_encode($userFields[$key]['datas']);
  641. }
  642. }
  643. } else {
  644. $error_message[] = $data['name'] . ',添加失败';
  645. }
  646. }
  647. }
  648. }
  649. # 更新用户自定义字段
  650. if (!empty($userFields)) {
  651. foreach ($userFields as $key => $value) {
  652. db('admin_user_field')->where('id', $value['id'])->update(['datas' => $value['datas']]);
  653. }
  654. }
  655. # 更新公海字段
  656. if (!empty($poolData)) {
  657. db('crm_customer_pool_field_setting')->insertAll($poolData);
  658. }
  659. if ($error_message) {
  660. $this->error = implode(';', $error_message);
  661. return false;
  662. }
  663. return true;
  664. }
  665. /**
  666. * [settingValue 单选、下拉、多选值]
  667. * @return [array]
  668. * @author Michael_xu
  669. */
  670. public function settingValue($data, $controller = '')
  671. {
  672. //将英文逗号转换为中文逗号
  673. $new_setting = [];
  674. foreach ($data['setting'] as $k => $v) {
  675. $v = str_replace(')', ')', $v);
  676. $v = str_replace('(', '(', $v);
  677. $new_setting[] = str_replace(',', ',', $v);
  678. }
  679. $data['setting'] = implode(chr(10), $new_setting);
  680. //默认值
  681. $new_default_value = [];
  682. if ($data['default_value'] && $data['form_type'] == 'checkbox' && !empty($data['default_value'])) {
  683. foreach ($data['default_value'] as $k => $v) {
  684. $new_default_value[] = str_replace(',', ',', $v);
  685. }
  686. $data['default_value'] = implode(',', $new_default_value);
  687. }elseif($data['default_value'] && $data['form_type'] == 'select' && !empty($data['default_value'])){
  688. $data['default_value'] = $data['default_value'];
  689. } else {
  690. $data['default_value'] = '';
  691. }
  692. return $data;
  693. }
  694. /**
  695. * [updateDataById 編輯自定义字段]
  696. * @param types 分类
  697. * @param field 字段名
  698. * @param name 字段标识名(字段注释)
  699. * @param form_type 字段类型
  700. * @param max_length 字段最大长度
  701. * @param default_value 默认值
  702. * @return [array]
  703. * @author Michael_xu
  704. */
  705. public function updateDataById($param, $types = '')
  706. {
  707. $error_message = [];
  708. if (!is_array($param)) {
  709. $this->error = '参数错误';
  710. return false;
  711. }
  712. // 查询老数据
  713. $oldData = [];
  714. if (!empty($types) && $types == 'crm_customer') {
  715. $oldList = db('admin_field')->field(['field', 'name'])->where('types', $types)->select();
  716. foreach ($oldList as $key => $value) {
  717. $oldData[$value['field']] = $value['name'];
  718. }
  719. }
  720. // 获取最大formAssistId
  721. $formAssistId = db('admin_field')->where('types', $types)->order('formAssistId', 'desc')->value('formAssistId');
  722. $formAssistId = !empty($formAssistId) ? $formAssistId : 1000;
  723. $i = 0;
  724. foreach ($param as $data) {
  725. // 设置formAssistId
  726. if (empty($data['formAssistId'])) {
  727. $formAssistId += 1;
  728. $data['formAssistId'] = $formAssistId;
  729. }
  730. // 数值范围
  731. if (!empty($data['minNumRestrict'])) {
  732. $data['min_num_restrict'] = $data['minNumRestrict'];
  733. }else{
  734. $data['min_num_restrict']='';
  735. }
  736. if (!empty($data['maxNumRestrict'])){
  737. $data['max_num_restrict'] = $data['maxNumRestrict'];
  738. }else{
  739. $data['max_num_restrict'] = '';
  740. }
  741. // 清除坐标
  742. unset($data['xaxis']);
  743. unset($data['yaxis']);
  744. unset($data['maxNumRestrict']);
  745. unset($data['minNumRestrict']);
  746. // 设置明细表格类型的默认值为空,防止为null的报错。
  747. if ($data['form_type'] == 'detail_table') {
  748. $data['default_value'] = '';
  749. }
  750. $i++;
  751. $field_id = intval($data['field_id']);
  752. if (!$field_id) {
  753. $error_message[] = $data['name'] . ',参数错误';
  754. }
  755. $dataInfo = $this->get($field_id);
  756. if (!$dataInfo) {
  757. $error_message[] = $data['name'] . '参数错误';
  758. }
  759. // $error_message[] = $data['name'].',该字段不能编辑';
  760. $data['types'] = $dataInfo['types'];
  761. //单选、下拉、多选类型(使用回车符隔开)
  762. if (in_array($data['form_type'], ['radio', 'select', 'checkbox']) && $data['setting']) {
  763. //将英文逗号转换为中文逗号
  764. $data = $this->settingValue($data, 'update');
  765. }
  766. // 验证
  767. $validate = validate($this->name);
  768. if (!$validate->check($data)) {
  769. $error_message[] = $validate->getError();
  770. } else {
  771. // unset($data['field']);
  772. $data['field'] = $dataInfo['field'];
  773. unset($data['operating']);
  774. // $box_form_type = array('checkbox', 'select', 'radio');
  775. // 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)) {
  776. // unset($data['form_type']);
  777. // }
  778. # 处理日期区间、地址类型的默认数据
  779. // if (in_array($data['form_type'], ['position']) && !empty($data['default_value'])) {
  780. // dump($data['default_value']);
  781. // p(json_encode($data['default_value']));
  782. // $data['default_value'] = json_encode($data['default_value']);
  783. // }
  784. # 处理明细表格中的字段数据
  785. if ($data['form_type'] == 'detail_table' && !empty($data['fieldExtendList']) && $this->setDetailTableData($data['types'], $data['field'], $data['fieldExtendList']) === false) {
  786. $error_message[] = '创建明细表单失败!';
  787. }
  788. unset($data['fieldExtendList']);
  789. # 处理选项中的逻辑表单数据
  790. // if (in_array($data['form_type'], ['select', 'checkbox'])) {
  791. // $data['options'] = json_encode($data['options'], JSON_NUMERIC_CHECK);
  792. // }
  793. // $resField = $this->allowField(true)->save($data, ['field_id' => $field_id]);
  794. unset($data['showSetting']);
  795. unset($data['componentName']);
  796. unset($data['is_deleted']);
  797. $data['update_time'] = time();
  798. $resField = db('admin_field')->where(['field_id' => $field_id])->update($data);
  799. if ($dataInfo['types'] !== 'oa_examine') {
  800. if ($resField) {
  801. # 更新公海字段
  802. if (!empty($oldData[$data['field']]) && $data['name'] != $oldData[$data['field']]['name']) {
  803. db('crm_customer_pool_field_setting')->where('field_name', $data['field'])->update(['name' => $data['name'], 'is_null' => $data['is_null'], 'is_unique' => $data['is_unique']]);
  804. }
  805. //actionLog($field_id); //操作日志
  806. $this->tableName = $dataInfo['types'];
  807. $maxlength = '255';
  808. $defaultvalue = $data['default_value'] ? "DEFAULT '" . $data['default_value'] . "'" : "DEFAULT NULL";
  809. //根据字段类型,创建字段
  810. switch ($dataInfo['form_type']) {
  811. case 'address' :
  812. $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'] . "'";
  813. break;
  814. case 'radio' :
  815. case 'select' :
  816. case 'checkbox' :
  817. $defaultvalue = $data['default_value'] ? "DEFAULT '" . $data['default_value'] . "'" : '';
  818. $maxlength = 500;
  819. $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'] . "'";
  820. break;
  821. case 'textarea' :
  822. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` TEXT COMMENT '" . $data['name'] . "'";
  823. break;
  824. case 'number' :
  825. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` VARCHAR(255) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  826. break;
  827. case 'floatnumber' :
  828. $defaultvalue = abs(intval($data['default_value'])) > 9999999999999999.99 ? 9999999999999999.99 : intval($data['default_value']);
  829. $maxlength = 18;
  830. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` decimal (" . $maxlength . ",2) DEFAULT '" . $defaultvalue . "' COMMENT '" . $data['name'] . "'";
  831. break;
  832. case 'date' :
  833. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` DATE " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  834. break;
  835. case 'datetime' :
  836. $defaultvalue = $data['default_value'] ? "DEFAULT '" . strtotime($data['default_value']) . "'" : '';
  837. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` CHANGE `" . $dataInfo['field'] . "` `" . $data['field'] . "` int (11) " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  838. break;
  839. case 'file' :
  840. $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'] . "' ";
  841. break;
  842. case 'boolean_value' :
  843. # 布尔值类型字段
  844. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` TINYINT(1) unsigned NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  845. break;
  846. case 'percent' :
  847. # 百分数类型字段
  848. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(255) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  849. break;
  850. case 'position' :
  851. # 地址类型字段,存放在相应的数据扩展表中,比如crm_customer_extra_data
  852. $addressValue = [];
  853. if (!empty($data['default_value'])) {
  854. $data['default_value'] = json_decode($data['default_value'], true);
  855. foreach ($data['default_value'] as $kk => $vv) {
  856. if (!empty($vv['name'])) $addressValue[] = $vv['name'];
  857. }
  858. }
  859. $defaultValue = !empty($addressValue) ? "DEFAULT '" . implode(',', $addressValue) . "'" : "DEFAULT NULL";
  860. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultValue . " COMMENT '" . $data['name'] . "'";
  861. break;
  862. case 'location' :
  863. # 定位类型字段,存放在相应的数据扩展表中,比如crm_customer_extra_data
  864. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  865. break;
  866. case 'handwriting_sign' :
  867. # 手写签名类型字段
  868. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` INT(10) unsigned NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  869. break;
  870. case 'date_interval' :
  871. # 日期区间类型字段,存放在相应的数据扩展表中,比如crm_customer_extra_data
  872. $defaultValue = !empty($data['default_value']) ? "DEFAULT '" . implode('_', json_decode($data['default_value'], true)) . "'" : "DEFAULT NULL";
  873. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultValue . " COMMENT '" . $data['name'] . "'";
  874. break;
  875. case 'desc_text' :
  876. # 描述文字类型字段
  877. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(1000) NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  878. break;
  879. case 'detail_table' :
  880. # 明细表格类型字段,存放在相应的数据扩展表中,比如crm_customer_extra_data
  881. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` MODIFY COLUMN `" . $data['field'] . "` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL " . $defaultvalue . " COMMENT '" . $data['name'] . "'";
  882. break;
  883. default :
  884. $maxlength = 255;
  885. $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'] . "'";
  886. break;
  887. }
  888. if (!empty($this->queryStr)) {
  889. $resData = Db::execute($this->queryStr);
  890. if ($resData === false) {
  891. $error_message[] = $data['name'] . ',修改失败';
  892. }
  893. }
  894. } else {
  895. $error_message[] = $data['name'] . ',修改失败';
  896. }
  897. }
  898. }
  899. }
  900. if ($error_message) {
  901. $this->error = implode(';', $error_message);
  902. return false;
  903. }
  904. return true;
  905. }
  906. /**
  907. * [delDataById 删除自定义字段] 删除逻辑数据不可恢复,谨慎操作
  908. * @param $id [array] 字段ID
  909. * @param $types 分类
  910. * @author Michael_xu
  911. */
  912. public function delDataById($ids, $types = '')
  913. {
  914. if (!is_array($ids)) {
  915. $ids[] = $ids;
  916. }
  917. # 删除公海字段的条件
  918. $poolWhere = [];
  919. $delMessage = [];
  920. foreach ($ids as $id) {
  921. $dataInfo = [];
  922. $dataInfo = $this->get($id);
  923. if ($dataInfo) {
  924. //operating : 0改删,1改,2删,3无
  925. if (in_array($dataInfo['operating'], ['1', '3'])) {
  926. $delMessage[] = $dataInfo['name'] . ',系统字段,不能删除';
  927. } else {
  928. $resDel = $this->where(['field_id' => $id])->delete(); //删除自定义字段信息
  929. // 客户模块下的栏目,成功删除自定义字段后做相应的处理
  930. if ($resDel && $dataInfo['types'] !== 'oa_examine') {
  931. $this->tableName = $dataInfo['types'];
  932. if ($dataInfo['form_type'] == 'img') {
  933. //图片类型需删除两个字段
  934. // $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` DROP `".$dataInfo['field']."`,"." DROP `thumb_".$dataInfo['field']."`";
  935. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` DROP `" . $dataInfo['field'] . "`";
  936. } else {
  937. $this->queryStr = "ALTER TABLE `" . $this->__db_prefix . $this->tableName . "` DROP `" . $dataInfo['field'] . "`";
  938. }
  939. $resData = Db::execute($this->queryStr); //删除表字段数据
  940. if (!$resData) {
  941. $delMessage[] = $dataInfo['name'] . ',删除失败';
  942. }
  943. //删除列表字段配置数据
  944. $userFieldList = db('admin_user_field')->where(['types' => $dataInfo['types']])->select();
  945. foreach ($userFieldList as $key => $val) {
  946. $datas = json_decode($val['datas'], true);
  947. if ($datas) {
  948. foreach ($datas as $k => $v) {
  949. $datas[$k]['field'] = $k;
  950. unset($datas[$dataInfo['field']]);
  951. }
  952. $dataUserField = [];
  953. $dataUserField['value'] = $datas;
  954. $dataUserField['hide_value'] = [];
  955. $userFieldModel = new \app\admin\model\UserField();
  956. $resUserField = $userFieldModel->updateConfig($dataInfo['types'], $dataUserField, $val['id']);
  957. // $resUserField = model('UserField')->updateConfig($dataInfo['types'], $dataUserField, $val['id']);
  958. }
  959. }
  960. //删除场景字段数据
  961. $sceneFieldList = db('admin_scene')->where(['types' => $dataInfo['types']])->select();
  962. foreach ($sceneFieldList as $key => $val) {
  963. $data = json_decode($val['data'], true);
  964. if ($data) {
  965. foreach ($data as $k => $v) {
  966. unset($data[$dataInfo['field']]);
  967. }
  968. $data = $data ?: [];
  969. $sceneModel = new \app\admin\model\Scene();
  970. $sceneModel->updateData($data, $val['scene_id']);
  971. }
  972. }
  973. // 处理删除公海字段的条件
  974. if (!empty($types) && $types == 'crm_customer' && !empty($dataInfo['field'])) $poolWhere[] = $dataInfo['field'];
  975. }
  976. // 删除字段成功后做相应处理
  977. if ($resDel) {
  978. // 删除明细表格字段数据
  979. if (!empty($dataInfo['form_type']) && $dataInfo['form_type'] == 'detail_table') {
  980. db('admin_field_extend')->where(['types' => $dataInfo['types'], 'field' => $dataInfo['field']])->delete();
  981. }
  982. // 删除相应模块扩展数据:crm_leads_data、crm_customer_data、oa_examine ...
  983. if (!empty($types)) db($types . '_data')->where('field', $dataInfo['field'])->delete();
  984. }
  985. // 删除失败
  986. if (!$resDel) {
  987. $delMessage[] = $dataInfo['name'] . ',删除失败';
  988. }
  989. }
  990. }
  991. }
  992. # 删除公海字段
  993. if (!empty($poolWhere)) {
  994. db('crm_customer_pool_field_setting')->whereIn('field_name', $poolWhere)->delete();
  995. }
  996. return $delMessage ? implode(';', $delMessage) : '';
  997. }
  998. /**
  999. * [createField 随机生成自定义字段名]
  1000. * @param $field_str 字段名前缀
  1001. * @param $types 分类
  1002. * @author Michael_xu
  1003. */
  1004. public function createField($types = '', $field_str = 'crm_')
  1005. {
  1006. for ($i = 1; $i <= 6; $i++) {
  1007. $field_str .= chr(rand(97, 122));
  1008. }
  1009. //验证字段名是否已存在
  1010. if ($this->where(['types' => $types, 'field' => $field_str])->find()) {
  1011. $this->createField($types);
  1012. }
  1013. return $field_str;
  1014. }
  1015. /**
  1016. * [field 获取自定义字段信息]
  1017. * @param $types 分类
  1018. * @param $dataInfo 数据展示
  1019. * @param $map 查询条件
  1020. * @param form_type 字段类型 (’text’,’textarea’,’mobile’,’email’等)
  1021. * @param default_value 默认值
  1022. * @param max_length 输入最大长度
  1023. * @param is_unique 1时,唯一性验证
  1024. * @param is_null 1时,必填
  1025. * @param input_tips 输入框提示内容
  1026. * @param setting 设置 (单选、下拉、多选的选项值,使用回车分隔)
  1027. * @author Michael_xu
  1028. */
  1029. public function field($param, $dataInfo = [])
  1030. {
  1031. $apiCommon = new ApiCommon();
  1032. $userModel = new \app\admin\model\User();
  1033. $structureModel = new \app\admin\model\Structure();
  1034. $fileModel = new \app\admin\model\File();
  1035. $user_id = !empty($param['user_id']) ? $param['user_id'] : $apiCommon->userInfo['id'];
  1036. $types = $param['types'];
  1037. $types_id = $param['types_id'] ?: 0;
  1038. $grantData = getFieldGrantData($user_id);
  1039. $userLevel = isSuperAdministrators($user_id);
  1040. if ($types == 'crm_customer_pool') $types = 'crm_customer';
  1041. $map = $param['map'] ?: [];
  1042. if (!in_array($types, $this->types_arr)) {
  1043. $this->error = '参数错误';
  1044. return false;
  1045. }
  1046. if ($types == 'oa_examine' && !$types_id) {
  1047. $this->error = '参数错误';
  1048. return false;
  1049. } elseif ($types == 'admin_user') {
  1050. return User::$import_field_list;
  1051. }
  1052. if (in_array($param['action'], array('index', 'view'))) {
  1053. $map['types'] = array(array('eq', $types), array('eq', ''), 'or');
  1054. } else {
  1055. if ($param['types'] == 'crm_customer' && (in_array($param['action'], array('save', 'update', 'excel')))) {
  1056. $map['field'] = array('not in', ['deal_status']);
  1057. }
  1058. $map['types'] = $types;
  1059. }
  1060. if ($param['controller'] == 'customer' && $param['action'] == 'pool') {
  1061. $map['field'] = array('not in', ['owner_user_id']);
  1062. $types = 'crm_customer_pool';
  1063. }
  1064. if ($param['action'] == 'excel') {
  1065. $map['form_type'] = array('not in', ['file', 'form', 'user', 'structure', 'checkbox', 'deal_status', 'position', 'location', 'handwriting_sign', 'date_interval', 'detail_table', 'desc_text', 'boolean_value']);//删除了过滤structure user字段类型数据 添加deal_status
  1066. } elseif ($param['action'] == 'index') {
  1067. $map['form_type'] = array('not in', ['file', 'form']);
  1068. }
  1069. $map['types_id'] = $types_id;
  1070. $order = 'order_id asc, field_id asc';
  1071. if ($param['action'] == 'index' || $param['action'] == 'pool') {
  1072. $field_list = $this->getIndexFieldConfig($types, $param['user_id']);
  1073. $field_list[] = [
  1074. 'field' => 'owner_user_structure_name',
  1075. 'name' => '所属部门',
  1076. 'form_type' => 'structure',
  1077. 'writeStatus' => 0,
  1078. 'is_hidden' => 1,
  1079. 'fieldName' => 'owner_user_structure_name'
  1080. ];
  1081. foreach ($field_list as $k => $v) {
  1082. # 处理字段授权
  1083. $field_list[$k]['writeStatus'] = 1;
  1084. if (!$userLevel && $param['module'] == 'crm' && !empty($grantData[$param['types']])) {
  1085. $status = getFieldGrantStatus($v['field'], $grantData[$param['types']]);
  1086. # 查看权限
  1087. if ($status['read'] == 0) {
  1088. unset($field_list[(int)$k]);
  1089. continue;
  1090. }
  1091. $field_list[$k]['maskType'] = $status['maskType'];
  1092. # 编辑权限
  1093. $field_list[$k]['writeStatus'] = $status['write'];
  1094. }
  1095. }
  1096. if ($param['types'] == 'crm_invoice') {
  1097. $field_list[] = [
  1098. 'field' => 'check_status',
  1099. 'name' => '审核状态',
  1100. 'form_type' => 'text',
  1101. 'writeStatus' => 0,
  1102. 'fieldName' => 'check_status'
  1103. ];
  1104. $field_list[] = [
  1105. 'field' => 'invoice_number',
  1106. 'name' => '发票号码',
  1107. 'form_type' => 'text',
  1108. 'writeStatus' => 0,
  1109. 'fieldName' => 'invoice_number'
  1110. ];
  1111. $field_list[] = [
  1112. 'field' => 'real_invoice_date',
  1113. 'name' => '实际开票日期',
  1114. 'form_type' => 'date',
  1115. 'writeStatus' => 0,
  1116. 'fieldName' => 'real_invoice_date'
  1117. ];
  1118. $field_list[] = [
  1119. 'field' => 'logistics_number',
  1120. 'name' => '物流单号',
  1121. 'form_type' => 'text',
  1122. 'writeStatus' => 0,
  1123. 'fieldName' => 'logistics_number'
  1124. ];
  1125. }
  1126. } else {
  1127. $fields = 'field_id,field,types,name,max_num_restrict as maxNumRestrict,min_num_restrict as minNumRestrict,form_type,default_value,is_unique,is_null,input_tips,setting,is_hidden,form_position,precisions,options,style_percent,formAssistId,remark';
  1128. $field_list = db('admin_field')->field($fields)->where($map)->where('is_hidden', 0)->order($order)->select();
  1129. // 获取X坐标值
  1130. $x = $this->getFormPositionXValue($types, $types_id);
  1131. # 详情页面增加负责人字段
  1132. // if ($param['action'] == 'read' && !in_array($param['types'], ['crm_visit', 'crm_product', 'oa_examine', 'crm_invoice'])) {
  1133. // $field_list[] = [
  1134. // 'field' => 'owner_user_id',
  1135. // 'name' => '负责人',
  1136. // 'form_type' => 'user',
  1137. // 'writeStatus' => 0,
  1138. // 'fieldName' => 'owner_user_name',
  1139. // 'value' => $dataInfo['owner_user_name'],
  1140. // ];
  1141. // }
  1142. //客户
  1143. if (in_array($param['types'], ['crm_customer'])) {
  1144. $new_field_list[] = [
  1145. 'field' => 'customer_address',
  1146. 'name' => '地区定位',
  1147. 'form_type' => 'map_address',
  1148. 'default_value' => '',
  1149. 'is_unique' => 0,
  1150. 'is_null' => 0,
  1151. 'input_tips' => '',
  1152. 'setting' => [],
  1153. 'value' => [],
  1154. 'style_percent' => 100,
  1155. 'xaxis' => $x,
  1156. 'yaxis' => 0,
  1157. 'form_position' => $x . ',0'
  1158. ];
  1159. }
  1160. // 商机下产品
  1161. if (in_array($param['types'], ['crm_business'])) {
  1162. $new_field_list[] = [
  1163. 'field' => 'product',
  1164. 'name' => '产品',
  1165. 'form_type' => 'product',
  1166. 'default_value' => '',
  1167. 'is_unique' => 0,
  1168. 'is_null' => 0,
  1169. 'input_tips' => '',
  1170. 'setting' => [],
  1171. 'value' => [],
  1172. 'style_percent' => 100,
  1173. 'xaxis' => $x,
  1174. 'yaxis' => 0,
  1175. 'form_position' => $x . ',0'
  1176. ];
  1177. }
  1178. // 合同下产品
  1179. if (in_array($param['types'], ['crm_contract'])) {
  1180. $new_field_list[] = [
  1181. 'field' => 'product',
  1182. 'name' => '产品',
  1183. 'form_type' => 'product',
  1184. 'default_value' => '',
  1185. 'is_unique' => 0,
  1186. 'is_null' => 0,
  1187. 'input_tips' => '',
  1188. 'setting' => [],
  1189. 'value' => [],
  1190. 'style_percent' => 100,
  1191. 'xaxis' => $x,
  1192. 'yaxis' => 0,
  1193. 'form_position' => $x . ',0'
  1194. ];
  1195. }
  1196. # 产品基本信息增加负责人信息
  1197. // if ($param['action'] == 'read' && $param['types'] == 'crm_product') {
  1198. // $new_field_list[] = db('admin_field')->where(['types_id' => 0, 'field' => 'owner_user_id'])->find();
  1199. // }
  1200. if ($new_field_list) $field_list = array_merge(collection($field_list)->toArray(), $new_field_list);
  1201. foreach ($field_list as $k => $v) {
  1202. # 处理字段授权
  1203. $field_list[$k]['writeStatus'] = 1;
  1204. if (!$userLevel && $param['module'] == 'crm' && !empty($grantData[$param['types']])) {
  1205. $status = getFieldGrantStatus($v['field'], $grantData[$param['types']]);
  1206. # 查看权限
  1207. if (empty($status['read'])) {
  1208. unset($field_list[(int)$k]);
  1209. continue;
  1210. }
  1211. # 编辑权限
  1212. if ($param['action'] != 'save') $field_list[$k]['writeStatus'] = $status['write'];
  1213. }
  1214. if ($param['action'] == 'read') {
  1215. $field_list[$k]['maskType'] = $status['maskType'];
  1216. if($status['maskType']!=0){
  1217. $field_list[$k]['writeStatus'] = 0;
  1218. }
  1219. }
  1220. # (联系人,商机,合同,回款,回访)关联其他模块的字段在详情页面不允许修改;创建人、负责人不允许修改
  1221. if ($param['action'] == 'read' && in_array($v['field'], ['customer_id', 'business_id', 'contacts_id', 'contract_id', 'create_user_id', 'owner_user_id', 'plan_id'])) {
  1222. $field_list[$k]['writeStatus'] = 0;
  1223. }
  1224. // 删除描述文字的name名称
  1225. if ($v['form_type'] == 'desc_text') {
  1226. $field_list[$k]['name'] = '';
  1227. }
  1228. //处理setting内容
  1229. $setting = [];
  1230. $default_value = $v['default_value'];
  1231. $value = [];
  1232. if (in_array($v['form_type'], ['radio', 'select', 'checkbox'])) {
  1233. $setting = explode(chr(10), $v['setting']);
  1234. if ($v['form_type'] == 'checkbox') $default_value = $v['default_value'] ? explode(',', $v['default_value']) : [];
  1235. }
  1236. if ($v['field'] == 'order_date') {
  1237. $default_value = date('Y-m-d', time());
  1238. }
  1239. //地图类型
  1240. if ($v['form_type'] == 'map_address') {
  1241. $value = [
  1242. 'address' => $dataInfo['address'] ? explode(chr(10), $dataInfo['address']) : [],
  1243. 'location' => $dataInfo['location'],
  1244. 'detail_address' => $dataInfo['detail_address'],
  1245. 'lng' => $dataInfo['lng'],
  1246. 'lat' => $dataInfo['lat']
  1247. ];
  1248. } elseif ($v['form_type'] == 'product') {
  1249. //相关产品类型
  1250. switch ($param['types']) {
  1251. case 'crm_business' :
  1252. $rProduct = db('crm_business_product');
  1253. $r_id = 'business_id';
  1254. break;
  1255. case 'crm_contract' :
  1256. $rProduct = db('crm_contract_product');
  1257. $r_id = 'contract_id';
  1258. break;
  1259. default :
  1260. break;
  1261. }
  1262. $newProductList = [];
  1263. $productList = $rProduct->where([$r_id => $param['action_id']])->select();
  1264. foreach ($productList as $key => $product) {
  1265. $product_info = [];
  1266. $category_name = '';
  1267. $product_info = db('crm_product')->where(['product_id' => $product['product_id']])->field('product_id,name,category_id')->find();
  1268. $category_name = db('crm_product_category')->where(['category_id' => $product_info['category_id']])->value('name');
  1269. $productList[$key]['name'] = $product_info['name'] ?: '';
  1270. $productList[$key]['category_id_info'] = $category_name ?: '';
  1271. }
  1272. $value = [
  1273. 'product' => $productList,
  1274. 'total_price' => $dataInfo['total_price'],
  1275. 'discount_rate' => $dataInfo['discount_rate']
  1276. ];
  1277. } elseif ($v['form_type'] == 'user') {
  1278. $value = $userModel->getListByStr($dataInfo[$v['field']]) ?: [];
  1279. // if (empty($value)) $default_value = $userModel->getListByStr($param['user_id']) ?: [];
  1280. } elseif ($v['form_type'] == 'single_user') {
  1281. # 单用户
  1282. $userInfo = $userModel->getListByStr($dataInfo[$v['field']]);
  1283. $value = !empty($userInfo[0]) ? $userInfo[0] : [];
  1284. if (empty($value)) {
  1285. $userInfo = $userModel->getListByStr($param['user_id']);
  1286. $default_value = !empty($userInfo[0]) ? $userInfo[0] : [];
  1287. }
  1288. } elseif ($v['form_type'] == 'structure') {
  1289. $value = $structureModel->getListByStr($dataInfo[$v['field']]) ?: [];
  1290. } elseif ($v['form_type'] == 'file') {
  1291. $fileIds = [];
  1292. $fileIds = stringToArray($dataInfo[$v['field']]);
  1293. $whereFile = [];
  1294. $whereFile['module'] = 'other';
  1295. $whereFile['module_id'] = 1;
  1296. $whereFile['file_id'] = ['in', $fileIds];
  1297. $fileList = $fileModel->getDataList($whereFile, 'all');
  1298. $value = $fileList['list'] ?: [];
  1299. } elseif ($v['form_type'] == 'customer') {
  1300. $value = $dataInfo[$v['field']] ? db('crm_customer')->where(['customer_id' => $dataInfo[$v['field']]])->field('customer_id,name')->select() : [];
  1301. } elseif ($v['form_type'] == 'business') {
  1302. $value = $dataInfo[$v['field']] ? db('crm_business')->where(['business_id' => $dataInfo[$v['field']]])->field('business_id,name')->select() : [];
  1303. } elseif ($v['form_type'] == 'contacts') {
  1304. $value = $dataInfo[$v['field']] ? db('crm_contacts')->where(['contacts_id' => $dataInfo[$v['field']]])->field('contacts_id,name')->select() : [];
  1305. } elseif ($v['form_type'] == 'contract') {
  1306. $value = $dataInfo[$v['field']] ? db('crm_contract')->where(['contract_id' => $dataInfo[$v['field']]])->field('contract_id,num')->select() : [];
  1307. } elseif ($v['form_type'] == 'floatnumber' && $v['field'] == 'contract_money' && $types == 'crm_invoice') {
  1308. $contractMoney = db('crm_invoice')->alias('invoice')
  1309. ->join('__CRM_CONTRACT__ contract', 'invoice.contract_id = contract.contract_id', 'LEFT')
  1310. ->where('invoice.invoice_id', $param['action_id'])->value('contract.money');
  1311. $value = $contractMoney;
  1312. } elseif ($v['form_type'] == 'category') {
  1313. //产品类别
  1314. if ($param['action'] == 'read') {
  1315. $category_name = db('crm_product_category')->where(['category_id' => $dataInfo['category_id']])->value('name');
  1316. $value = $category_name ?: '';
  1317. } elseif ($param['action'] == 'update') {
  1318. $parentIds = [];
  1319. if (!empty($dataInfo['category_id'])) {
  1320. $parentIds = $this->getProductParentIds($dataInfo['category_id']);
  1321. $parentIds = array_reverse($parentIds);
  1322. array_push($parentIds, $dataInfo['category_id']);
  1323. }
  1324. $value = $parentIds;
  1325. } else {
  1326. $categoryModel = new \app\crm\model\ProductCategory();
  1327. $value = $categoryModel->getDataList('tree');
  1328. }
  1329. } elseif ($v['form_type'] == 'business_type') {
  1330. //商机状态组
  1331. $businessStatusModel = new \app\crm\model\BusinessStatus();
  1332. $userInfo = $userModel->getUserById($user_id);
  1333. $setting = db('crm_business_type')
  1334. ->where('status', 1)
  1335. ->where('is_display', 1)
  1336. ->where(function ($query) use ($userInfo) {
  1337. $query->where(['structure_id' => ['like', '%,' . $userInfo['structure_id'] . ',%']]);
  1338. $query->whereOr('structure_id', '');
  1339. })->select();
  1340. foreach ($setting as $key => $val) {
  1341. $setting[$key]['statusList'] = $businessStatusModel->getDataList($val['type_id'], 0);
  1342. }
  1343. $setting = $setting ?: [];
  1344. if ($param['action'] == 'read') {
  1345. $value = $dataInfo[$v['field']] ? db('crm_business_type')->where(['type_id' => $dataInfo[$v['field']]])->value('name') : '';
  1346. } else {
  1347. $value = (int)$dataInfo[$v['field']] ?: '';
  1348. }
  1349. } elseif ($v['form_type'] == 'business_status') {
  1350. //商机阶段
  1351. if ($param['action'] == 'read') {
  1352. $value = $dataInfo[$v['field']] ? db('crm_business_status')->where(['status_id' => $dataInfo[$v['field']]])->value('name') : '';
  1353. } else {
  1354. $businessStatusModel = new \app\crm\model\BusinessStatus();
  1355. $setting = $businessStatusModel->getDataList($dataInfo['type_id'], 1);
  1356. $value = (int)$dataInfo[$v['field']] ?: '';
  1357. }
  1358. } elseif ($v['form_type'] == 'receivables_plan') {
  1359. //回款计划期数
  1360. $value = $dataInfo[$v['field']] ? db('crm_receivables_plan')->where(['plan_id' => $dataInfo[$v['field']]])->value('num') : '';
  1361. } elseif ($v['form_type'] == 'business_cause' || $v['form_type'] == 'examine_cause') {
  1362. $whereTravel = [];
  1363. $whereTravel['examine_id'] = $dataInfo['examine_id'];
  1364. $travelList = db('oa_examine_travel')->where($whereTravel)->select() ?: [];
  1365. foreach ($travelList as $key => $val) {
  1366. $where = [];
  1367. $fileList = [];
  1368. $imgList = [];
  1369. $where['module'] = 'oa_examine_travel';
  1370. $where['module_id'] = $val['travel_id'];
  1371. $newFileList = [];
  1372. $newFileList = $fileModel->getDataList($where, 'all');
  1373. if ($newFileList['list']) {
  1374. foreach ($newFileList['list'] as $val1) {
  1375. if ($val1['types'] == 'file') {
  1376. $fileList[] = $val1;
  1377. } else {
  1378. $imgList[] = $val1;
  1379. }
  1380. }
  1381. }
  1382. $travelList[$key]['start_time'] = $val['start_time'] ? date('Y-m-d H:i:s', $val['start_time']) : null;
  1383. $travelList[$key]['end_time'] = $val['end_time'] ? date('Y-m-d H:i:s', $val['end_time']) : null;
  1384. $travelList[$key]['fileList'] = $fileList ?: [];
  1385. $travelList[$key]['imgList'] = $imgList ?: [];
  1386. }
  1387. $value = $travelList ?: [];
  1388. } elseif ($v['form_type'] == 'checkbox') {
  1389. $value = isset($dataInfo[$v['field']]) ? stringToArray($dataInfo[$v['field']]) : [];
  1390. } elseif ($v['form_type'] == 'date') {
  1391. $value = ($dataInfo[$v['field']] && $dataInfo[$v['field']] !== '0000-00-00') ? $dataInfo[$v['field']] : '';
  1392. } elseif ($v['form_type'] == 'boolean_value') {
  1393. // 布尔类型
  1394. $value = !empty($dataInfo[$v['field']]) ? (string)$dataInfo[$v['field']] : '0';
  1395. } elseif ($v['form_type'] == 'percent') {
  1396. // 百分数
  1397. $value = !empty($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
  1398. } elseif ($v['form_type'] == 'website') {
  1399. // 网址
  1400. $value = !empty($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
  1401. } elseif ($v['form_type'] == 'handwriting_sign') {
  1402. // 手写签名
  1403. $fileData = !empty($dataInfo[$v['field']]) ? db('admin_file')->where('file_id', $dataInfo[$v['field']])->find() : '';
  1404. if (!empty($fileData['file_path'])) $fileData['file_path'] = getFullPath($fileData['file_path']);
  1405. if (!empty($fileData['file_path_thumb'])) $fileData['file_path_thumb'] = getFullPath($fileData['file_path_thumb']);
  1406. $value = !empty($fileData) ? ['file_id' => $fileData['file_id'], 'url' => $fileData['file_path']] : "";
  1407. } elseif ($v['form_type'] == 'desc_text') {
  1408. // 描述文字
  1409. $value = !empty($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : $v['default_value'];
  1410. } elseif ($v['form_type']=='location') {
  1411. // 地址、定位、日期区间、明细表格
  1412. $primaryKey = getPrimaryKeyName($param['types']);
  1413. $positionJson = !empty($dataInfo[$primaryKey]) ? db($param['types'] . '_data')->where([$primaryKey => $dataInfo[$primaryKey], 'field' => $v['field']])->value($param['types'] == 'oa_examine' ? 'value' : 'content') : '';
  1414. $positionData = !empty($positionJson) ? json_decode($positionJson, true) : '';
  1415. $value = $positionData;
  1416. } elseif($v['form_type']=='detail_table'){
  1417. // $fieldGrant = db('admin_field_mask')->where('types', 'contract')->select();
  1418. $primaryKey = getPrimaryKeyName($param['types']);
  1419. $positionJson = !empty($dataInfo[$primaryKey]) ? db($param['types'] . '_data')->where([$primaryKey => $dataInfo[$primaryKey], 'field' => $v['field']])->value($param['types'] == 'oa_examine' ? 'value' : 'content') : '';
  1420. $positionData = !empty($positionJson) ? json_decode($positionJson, true) : '';
  1421. foreach ($positionData as $kk => $val){
  1422. foreach ($val as $key => $values){
  1423. if($values['form_type']=='user'){
  1424. $positionData[$kk][$key]['value']= !empty($values['value'])?$userModel->getListByStr($values['value']) :[];
  1425. }
  1426. if($values['form_type']=='structure'){
  1427. $positionData[$kk][$key]['value']= !empty($values['value'])? $structureModel->getListByStr($values['value']) : [];
  1428. }
  1429. if($values['form_type']=='datetime' && is_numeric($values['value'])){
  1430. $positionData[$kk][$key]['value']= date('Y-m-d, H:i:s',$values['value']);
  1431. }
  1432. if($values['form_type']=='boolean_value'){
  1433. $positionData[$kk][$key]['value']= (string)$values['value'];
  1434. }
  1435. if($values['form_type']=='file'){
  1436. $fileIds = stringToArray($values['value']);
  1437. $whereFile = [];
  1438. $whereFile['module'] = 'other';
  1439. $whereFile['module_id'] = 1;
  1440. $whereFile['file_id'] = ['in', $fileIds];
  1441. $fileList = $fileModel->getDataList($whereFile, 'all');
  1442. $positionData[$kk][$key]['value'] = $fileList['list'] ?: [];
  1443. }
  1444. // foreach ($fieldGrant as $val) {
  1445. // if (in_array($val['statue_type'], [1, 3]) && $val['form_type'] == ['mobile']) {
  1446. // $positionData[$kk][$key]['value'] = !empty($values['value']) ? (string)substr_replace($values['value'], '*', 2, 4) : null;
  1447. // } elseif (in_array($val['statue_type'], [1, 3]) && $val['form_type'] == ['email']) {
  1448. // $email_array = explode("@", $values['value']);
  1449. // $str = substr_replace($email_array[0], '*', 1);
  1450. // $positionData[$kk][$key]['value'] = !empty($values['value']) ? (string)$str . $email_array[1] : null;
  1451. // } elseif (in_array($val['statue_type'], [1, 3]) && in_array($val['form_type'],['position','floatnumber'])) {
  1452. // $positionData[$kk][$key]['value'] = !empty($dataInfo[$val['fiele_id']]) ? (string)substr_replace($values['value'], '*',0,strlen($values['value'])) : null;
  1453. // }
  1454. // }
  1455. $positionData[$kk][$key]['optionsData']=!empty($field_list[$k]['options']) ? json_decode($field_list[$k]['options'], true) : '';
  1456. }
  1457. }
  1458. $value = $positionData;
  1459. if ($v['form_type'] == 'detail_table') {
  1460. $content = db('admin_field_extend')->where(['types' => $types, 'field' => $v['field']])->value('content');
  1461. $content=json_decode($content, true);
  1462. foreach ($content as &$vv){
  1463. $vv['optionsData']=!empty($field_list[$k]['options']) ? json_decode($field_list[$k]['options'], true) : '';
  1464. }
  1465. $field_list[$k]['fieldExtendList'] = $content;
  1466. }
  1467. } elseif($v['form_type']=='position'){
  1468. // 地址
  1469. $default_value = !empty($v['default_value']) ? json_decode($v['default_value'], true) : [];
  1470. if(!empty($dataInfo[$v['field']])){
  1471. $position= explode(',',$dataInfo[$v['field']]);
  1472. for ($i=0; $i<count($position); $i++) {
  1473. $b[]['name'] =trim(json_encode($position[$i],JSON_UNESCAPED_UNICODE),'"');
  1474. }
  1475. $value =$b;
  1476. }
  1477. } elseif($v['form_type']=='date_interval'){
  1478. if (!empty($dataInfo[$v['field']])) {
  1479. $position= explode('_',$dataInfo[$v['field']]);
  1480. $value =$position;
  1481. }
  1482. $default_value = !empty($v['default_value'])?explode(',',$v['default_value']):[];
  1483. } else {
  1484. $value = isset($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
  1485. }
  1486. // $fieldGrant = db('admin_field_mask')->where('types', 'contract')->select();
  1487. // foreach ($fieldGrant as $val) {
  1488. // if (in_array($val['statue_type'], [1, 3]) && $val['form_type'] == ['mobile']) {
  1489. // $value = !empty($dataInfo[$val['fiele_id']]) ? (string)substr_replace($dataInfo[$val['fiele_id']], '*', 2, 4) : null;
  1490. // } elseif (in_array($val['statue_type'], [1, 3]) && $val['form_type'] == ['email']) {
  1491. // $email_array = explode("@", $dataInfo[$val['fiele_id']]);
  1492. // $str = substr_replace($email_array[0], '*', 1);
  1493. // $value = !empty($dataInfo[$val['fiele_id']]) ? (string)$str . $email_array[1] : null;
  1494. // } elseif (in_array($val['statue_type'], [1, 3]) && in_array($val['form_type'],['position','floatnumber'])) {
  1495. // $value = !empty($dataInfo[$val['fiele_id']]) ? (string)substr_replace($dataInfo[$val['fiele_id']], '*',0,strlen($dataInfo[$val['fiele_id']])) : null;
  1496. // }
  1497. // }
  1498. $field_list[$k]['setting'] = $setting;
  1499. $field_list[$k]['default_value'] = $default_value;
  1500. $field_list[$k]['value'] = $value;
  1501. $field_list[$k]['options'] = !empty($field_list[$k]['options']) ? $field_list[$k]['options'] : '';
  1502. $field_list[$k]['optionsData'] = !empty($field_list[$k]['options']) ? json_decode($field_list[$k]['options'], true) : '';
  1503. }
  1504. }
  1505. return array_values($field_list) ?: [];
  1506. }
  1507. private function getFormPositionXValue($types, $typesId)
  1508. {
  1509. $positionArray = db('admin_field')->where(['types' => $types, 'types_id' => $typesId, 'form_position' => [['neq', ''], ['not null'], 'AND']])->column('form_position');
  1510. if (!empty($positionArray)) {
  1511. $positionString = implode('-', $positionArray);
  1512. $positionString = str_replace(',0', '', $positionString);
  1513. $positionString = str_replace(',1', '', $positionString);
  1514. $positionString = str_replace(',2', '', $positionString);
  1515. $positionString = str_replace(',3', '', $positionString);
  1516. $positionArray = explode('-', $positionString);
  1517. return max($positionArray) + 1;
  1518. }
  1519. return 0;
  1520. }
  1521. /**
  1522. * [fieldSearch 获取自定义字段高级筛选信息]
  1523. * @param $types 分类
  1524. * @param $map 查询条件
  1525. * @param form_type 字段类型 (’text’,’textarea’,’mobile’,’email’等)
  1526. * @param setting 设置 (单选、下拉、多选的选项值,使用回车分隔)
  1527. * @author Michael_xu
  1528. */
  1529. public function fieldSearch($param)
  1530. {
  1531. $types = $param['types'];
  1532. if (!in_array($types, $this->types_arr)) {
  1533. $this->error = '参数错误';
  1534. return false;
  1535. }
  1536. $userModel = new \app\admin\model\User();
  1537. $user_id = $param['user_id'];
  1538. $map['types'] = ['in', ['', $types]];
  1539. $map['form_type'] = ['not in', ['file', 'pic', 'structure', 'form', 'business_status', 'detail_table', 'desc_text', 'handwriting_sign', 'date_interval']];
  1540. $map['is_hidden'] = 0;
  1541. $field_list = db('admin_field')
  1542. ->where($map)
  1543. ->whereOr(['types' => ''])
  1544. ->field('field,name,form_type,setting')
  1545. ->order('order_id asc, field_id asc, update_time desc')
  1546. ->select();
  1547. if (in_array($types, ['crm_contract', 'crm_receivables'])) {
  1548. $field_arr = [
  1549. '0' => [
  1550. 'field' => 'check_status',
  1551. 'name' => '审核状态',
  1552. 'form_type' => 'select',
  1553. 'setting' => '待审核' . chr(10) . '审核中' . chr(10) . '审核通过' . chr(10) . '审核失败' . chr(10) . '已撤回' . chr(10) . '未提交' . chr(10) . '已作废'
  1554. ]
  1555. ];
  1556. }
  1557. if (in_array($param['types'], ['crm_customer'])) {
  1558. $field_arr = [
  1559. '0' => [
  1560. 'field' => 'address',
  1561. 'name' => '地区定位',
  1562. 'form_type' => 'address',
  1563. 'setting' => []
  1564. ]
  1565. ];
  1566. }
  1567. if ($param['types'] == 'crm_customer') {
  1568. $field_arr[] = [
  1569. 'field' => 'detail_address',
  1570. 'name' => '详细地址',
  1571. 'form_type' => 'text',
  1572. 'setting' => []
  1573. ];
  1574. }
  1575. if (in_array($param['types'], ['crm_customer', 'crm_leads', 'crm_contacts', 'crm_business', 'crm_contract'])) {
  1576. $field_arr[] = [
  1577. 'field' => 'last_time',
  1578. 'name' => '最后跟进时间',
  1579. 'form_type' => 'datetime',
  1580. 'setting' => []
  1581. ];
  1582. }
  1583. if ($field_arr) $field_list = array_merge($field_list, $field_arr);
  1584. foreach ($field_list as $k => $v) {
  1585. //处理setting内容
  1586. $setting = [];
  1587. if (in_array($v['form_type'], ['radio', 'select', 'checkbox'])) {
  1588. $setting = explode(chr(10), $v['setting']);
  1589. }
  1590. $field_list[$k]['setting'] = $setting;
  1591. if ($v['field'] == 'customer_id') {
  1592. $field_list[$k]['form_type'] = 'module';
  1593. $field_list[$k]['field'] = 'customer_name';
  1594. }
  1595. if ($v['field'] == 'business_id') {
  1596. $field_list[$k]['form_type'] = 'module';
  1597. $field_list[$k]['field'] = 'business_name';
  1598. }
  1599. if ($v['field'] == 'contract_id') {
  1600. $field_list[$k]['form_type'] = 'module';
  1601. $field_list[$k]['field'] = 'contract_name';
  1602. }
  1603. if ($v['field'] == 'contacts_id') {
  1604. $field_list[$k]['form_type'] = 'module';
  1605. $field_list[$k]['field'] = 'contacts_name';
  1606. }
  1607. if ($v['field'] == 'warehouse_id' && in_array($param['types'], ['jxc_receipt', 'jxc_outbound'])) {
  1608. $field_list[$k]['form_type'] = 'text';
  1609. }
  1610. if ($v['form_type'] == 'warehouse_cause') {
  1611. $field_list[$k]['form_type'] = 'text';
  1612. }
  1613. if ($v['form_type'] == 'category') {
  1614. } elseif ($v['form_type'] == 'business_type') {
  1615. //商机状态组
  1616. $businessStatusModel = new \app\crm\model\BusinessStatus();
  1617. $userInfo = $userModel->getUserById($user_id);
  1618. $setting = db('crm_business_type')
  1619. ->where(['structure_id' => ['like', ',%' . $userInfo['structure_id'] . '%,'], 'status' => 1])
  1620. ->whereOr('structure_id', '')
  1621. ->select();
  1622. foreach ($setting as $key => $val) {
  1623. $setting[$key]['statusList'] = $businessStatusModel->getDataList($val['type_id'], 1);
  1624. }
  1625. $setting = $setting ?: [];
  1626. }
  1627. $field_list[$k]['setting'] = $setting;
  1628. }
  1629. return $field_list ?: [];
  1630. }
  1631. /**
  1632. * 自定义字段验证规则
  1633. * @param string $types 类型:crm_customer crm_business ...
  1634. * @param int $types_id 自定义表types_id
  1635. * @param string $action 操作:save update
  1636. * @return array
  1637. */
  1638. public function validateField($types, $types_id = 0, $action = 'save')
  1639. {
  1640. $apiCommon = new ApiCommon();
  1641. $userId = $apiCommon->userInfo['id'];
  1642. $grantData = getFieldGrantData($userId);
  1643. $userLevel = isSuperAdministrators($userId);
  1644. $unField = ['update_time', 'create_time', 'create_user_id', 'owner_user_id'];
  1645. $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();
  1646. $validateArr = [];
  1647. $rule = [];
  1648. $message = [];
  1649. foreach ($fieldList as $field) {
  1650. # 字段授权
  1651. if (!$userLevel && !empty($grantData[$types])) {
  1652. $status = getFieldGrantStatus($field['field'], $grantData[$types]);
  1653. # 没有字段查看权限或者编辑时没有字段修改权限就跳过验证
  1654. if (empty($status['read']) || ($action != 'save' && empty($status['write']))) continue;
  1655. }
  1656. $rule_value = '';
  1657. $scene_value = '';
  1658. $max_length = $field['max_length'] ?: '';
  1659. if ($field['is_null']) {
  1660. $rule_value .= 'require';
  1661. $message[$field['field'] . '.require'] = $field['name'] . '不能为空';
  1662. }
  1663. if ($field['form_type'] == 'number') {
  1664. if ($rule_value) $rule_value .= '|';
  1665. $rule_value .= 'number';
  1666. $message[$field['field'] . '.number'] = $field['name'] . '必须是数字';
  1667. } elseif ($field['form_type'] == 'email') {
  1668. if ($rule_value) $rule_value .= '|';
  1669. $rule_value .= 'email';
  1670. $message[$field['field'] . '.email'] = $field['name'] . '格式错误';
  1671. } elseif ($field['form_type'] == 'mobile ') {
  1672. if ($rule_value) $rule_value .= '|';
  1673. $rule_value .= 'regex:^1[3456789][0-9]{9}?$';
  1674. $message[$field['field'] . '.regex'] = $field['name'] . '格式错误';
  1675. }
  1676. if ($field['is_unique']) {
  1677. if ($rule_value) $rule_value .= '|';
  1678. $rule_value .= 'unique:' . $types;
  1679. $message[$field['field'] . '.unique'] = $field['name'] . '已经存在,不能重复添加';
  1680. }
  1681. if ($max_length) {
  1682. if ($rule_value) $rule_value .= '|';
  1683. $rule_value .= 'max:' . $max_length;
  1684. $message[$field['field'] . '.max'] = $field['name'] . '不能超过' . $max_length . '个字符';
  1685. }
  1686. // if ($field['form_type'] == 'datetime') {
  1687. // $rule_value .= 'date';
  1688. // $message[$field['field'].'.date'] = $field['name'].'格式错误';
  1689. // }
  1690. if ($rule_value == 'require|') $rule_value = 'require';
  1691. if (!empty($rule_value)) $rule[$field['field']] = $rule_value;
  1692. }
  1693. $validateArr['rule'] = $rule ?: [];
  1694. $validateArr['message'] = $message ?: [];
  1695. return $validateArr;
  1696. }
  1697. /**
  1698. * [getIndexField 列表展示字段]
  1699. * @param types 分类
  1700. * @param excel 导出使用
  1701. * @author Michael_xu
  1702. */
  1703. public function getIndexFieldConfig($types, $user_id, $types_id = '', $excel = '')
  1704. {
  1705. $userFieldModel = new \app\admin\model\UserField();
  1706. $userFieldData = $userFieldModel->getConfig($types, $user_id);
  1707. $userFieldData = $userFieldData ? json_decode($userFieldData, true) : [];
  1708. $grantData = getFieldGrantData($user_id);
  1709. $userLevel = isSuperAdministrators($user_id);
  1710. $fieldList = $this->getFieldList($types, $types_id, $excel);
  1711. $where = [];
  1712. if ($userFieldData) {
  1713. $fieldArr = [];
  1714. $i = 0;
  1715. foreach ($userFieldData as $k => $v) {
  1716. if (empty($fieldList[$k])) {
  1717. unset($userFieldData[$k]);
  1718. continue;
  1719. }
  1720. if (empty($v['is_hide'])) {
  1721. $fieldArr[$i]['field'] = $k;
  1722. $fieldArr[$i]['name'] = $fieldList[$k]['name'];
  1723. $fieldArr[$i]['form_type'] = $fieldList[$k]['form_type'];
  1724. $fieldArr[$i]['width'] = $v['width'] ?: '';
  1725. $i++;
  1726. }
  1727. }
  1728. $dataList = $fieldArr;
  1729. } else {
  1730. $dataList = $fieldList;
  1731. }
  1732. # 处理字段授权
  1733. foreach ($dataList as $k => $v) {
  1734. if (!$userLevel && !empty($grantData[$types])) {
  1735. $status = getFieldGrantStatus($v['field'], $grantData[$types]);
  1736. $dataList[(int)$k]['maskType']=$status['maskType'];
  1737. # 查看权限
  1738. if ($status['read'] == 0) unset($dataList[(int)$k]);
  1739. }
  1740. }
  1741. return array_values($dataList) ?: [];
  1742. }
  1743. /**
  1744. * 获取列表展示字段
  1745. * @return $types_id 默认为空多自定义字段条件使用
  1746. * @return void
  1747. * @author Ymob
  1748. * @datetime 2019-10-23 17:32:57
  1749. */
  1750. public function getFieldList($types, $types_id = '', $excel = '')
  1751. {
  1752. $newTypes = $types;
  1753. $unField = ['-1'];
  1754. if ($types == 'crm_customer_pool') {
  1755. $newTypes = 'crm_customer';
  1756. $unField = ['owner_user_id'];
  1757. }
  1758. if ($excel == 'excel') {
  1759. if ($types == 'jxc_product') {
  1760. $unField = ['product_picture'];
  1761. }
  1762. $where = [
  1763. 'types' => ['IN', ['', $newTypes]],
  1764. 'form_type' => ['not in', ['file', 'form', 'pic', 'deal_status', 'handwriting_sign', 'detail_table', 'desc_text']],
  1765. 'field' => ['not in', $unField],
  1766. 'types_id' => ['eq', $types_id],
  1767. 'is_hidden' => 0
  1768. ];
  1769. } else {
  1770. $where = [
  1771. 'types' => ['IN', ['', $newTypes]],
  1772. 'form_type' => ['not in', ['file', 'form', 'desc_text', 'detail_table']],
  1773. 'field' => ['not in', $unField],
  1774. 'types_id' => ['eq', $types_id],
  1775. 'is_hidden' => 0
  1776. ];
  1777. }
  1778. $fieldArr = $this
  1779. ->where($where)
  1780. ->field(['field', 'name', 'form_type', 'is_hidden'])
  1781. ->order('field_id', 'asc')
  1782. ->select();
  1783. $res = [];
  1784. foreach ($fieldArr as $val) {
  1785. $res[] = $val->toArray();
  1786. }
  1787. if ($types == 'oa_examine') {
  1788. }
  1789. if (isset($this->orther_field_list[$newTypes])) {
  1790. foreach ($this->orther_field_list[$newTypes] as $val) {
  1791. if($val['field'] != 'product_picture'){
  1792. $res[] = $val;
  1793. }
  1794. }
  1795. }
  1796. if ($types == 'crm_customer') {
  1797. $res[] = [
  1798. 'field' => 'pool_day',
  1799. 'name' => '距进入公海天数',
  1800. 'form_type' => 'text',
  1801. 'is_hidden' => 0
  1802. ];
  1803. $res[] = [
  1804. 'field' => 'is_lock',
  1805. 'name' => '锁定状态',
  1806. 'form_type' => 'text',
  1807. 'is_hidden' => 0
  1808. ];
  1809. } elseif ($types == 'crm_receivables_plan') {
  1810. $res[] = [
  1811. 'field' => 'real_money',
  1812. 'name' => '实际回款金额',
  1813. 'form_type' => 'floatnumber',
  1814. 'is_hidden' => 0
  1815. ];
  1816. $res[] = [
  1817. 'field' => 'real_data',
  1818. 'name' => '实际回款日期',
  1819. 'form_type' => 'date',
  1820. 'is_hidden' => 0
  1821. ];
  1822. $res[] = [
  1823. 'field' => 'un_money',
  1824. 'name' => '未回金额',
  1825. 'form_type' => 'floatnumber',
  1826. 'is_hidden' => 0
  1827. ];
  1828. }
  1829. return array_column($res, null, 'field');
  1830. }
  1831. /**
  1832. * [getIndexField 列表展示字段]
  1833. * @param types 分类
  1834. * @param is_data 1 取数据时
  1835. * @author Michael_xu
  1836. */
  1837. public function getIndexField($types, $user_id, $is_data = '')
  1838. {
  1839. $apiCommon = new ApiCommon();
  1840. $userFieldModel = new \app\admin\model\UserField();
  1841. $userFieldData = $userFieldModel->getConfig($types, $user_id);
  1842. $userFieldData = $userFieldData ? json_decode($userFieldData, true) : [];
  1843. $othor_un_field = array_column($this->orther_field_list[$types], 'field');
  1844. $unField = array_merge(['pool_day', 'business-check', 'call'], $othor_un_field);
  1845. $user_id = !empty($user_id) ? $user_id : $apiCommon->userInfo['id'];
  1846. $grantData = getFieldGrantData($user_id);
  1847. $userLevel = isSuperAdministrators($user_id);
  1848. $where = [];
  1849. if ($userFieldData) {
  1850. $dataList = [];
  1851. foreach ($userFieldData as $k => $v) {
  1852. if (empty($v['is_hide']) && !in_array($k, $unField)) {
  1853. $dataList[] = $k;
  1854. }
  1855. }
  1856. } else {
  1857. $where['types'] = ['in', ['', $types]];
  1858. $dataList = $this->where($where)->column('field');
  1859. }
  1860. $newList = $dataList;
  1861. if ($is_data == 1) {
  1862. switch ($types) {
  1863. case 'crm_leads' :
  1864. $sysField = ['leads_id', 'create_time', 'update_time', 'create_user_id', 'owner_user_id', 'last_time', 'last_record'];
  1865. break;
  1866. case 'crm_business' :
  1867. $newList = [];
  1868. foreach ($dataList as $v) {
  1869. $newList[] = 'business.' . $v;
  1870. }
  1871. $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'];
  1872. break;
  1873. case 'crm_customer' :
  1874. $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'];
  1875. break;
  1876. case 'crm_customer_pool' :
  1877. $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'];
  1878. break;
  1879. case 'crm_contacts' :
  1880. $newList = [];
  1881. foreach ($dataList as $v) {
  1882. $newList[] = 'contacts.' . $v;
  1883. }
  1884. $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'];
  1885. break;
  1886. case 'crm_contract' :
  1887. $newList = [];
  1888. foreach ($dataList as $v) {
  1889. $newList[] = 'contract.' . $v;
  1890. }
  1891. $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'];
  1892. break;
  1893. case 'crm_receivables' :
  1894. $newList = [];
  1895. foreach ($dataList as $v) {
  1896. $newList[] = 'receivables.' . $v;
  1897. }
  1898. $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'];
  1899. break;
  1900. case 'crm_product' :
  1901. $newList = [];
  1902. foreach ($dataList as $v) {
  1903. $newList[] = 'product.' . $v;
  1904. }
  1905. $sysField = ['product.product_id', 'product.category_id', 'product.create_time', 'product.update_time', 'product.create_user_id', 'product.owner_user_id'];
  1906. break;
  1907. case 'crm_visit' :
  1908. $newList = [];
  1909. foreach ($dataList as $v) {
  1910. $newList[] = 'visit.' . $v;
  1911. }
  1912. $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'];
  1913. break;
  1914. case 'crm_invoice' :
  1915. $newList = [];
  1916. foreach ($dataList as $k => $v) {
  1917. $newList[] = 'invoice.' . $v;
  1918. if ($v == 'contract_money') {
  1919. unset($newList[$k]);
  1920. }
  1921. }
  1922. $sysField = ['invoice.invoice_id', 'invoice.owner_user_id', 'invoice.invoice_status', 'invoice.invoice_number', 'invoice.invoice_type', 'invoice.check_status', 'invoice.customer_id', 'invoice.contract_id', 'invoice.create_user_id', 'invoice.flow_id', 'invoice.real_invoice_date', 'invoice.logistics_number'];
  1923. break;
  1924. case 'crm_receivables_plan' :
  1925. $newList = [];
  1926. foreach ($dataList as $k => $v) {
  1927. $newList[] = 'receivables_plan.' . $v;
  1928. }
  1929. $sysField = ['receivables_plan.plan_id', 'receivables_plan.num', 'receivables_plan.receivables_id', 'receivables_plan.status', 'receivables_plan.contract_id', 'receivables_plan.customer_id', 'receivables_plan.money', 'receivables_plan.return_date', 'receivables_plan.return_type', 'receivables_plan.remind', 'receivables_plan.remind_date', 'receivables_plan.remark',
  1930. 'receivables_plan.create_user_id', 'receivables_plan.owner_user_id', 'receivables_plan.create_time', 'receivables_plan.update_time', 'receivables_plan.file', 'receivables_plan.is_dealt', 'receivables_plan.un_money', 'receivables_plan.real_data', 'receivables_plan.real_money'
  1931. ];
  1932. break;
  1933. }
  1934. $listArr = $sysField ? array_unique(array_merge($newList, $sysField)) : $dataList;
  1935. } else {
  1936. $listArr = $dataList;
  1937. }
  1938. $typesArray = explode('_', $types);
  1939. $type = array_pop($typesArray);
  1940. if (isset($this->orther_field_list[$types])) {
  1941. foreach ($this->orther_field_list[$types] as $val) {
  1942. if (in_array($type . '.' . $val['field'], $listArr) && !in_array($types, ['crm_contract', 'crm_business', 'crm_receivables'])) {
  1943. unset($listArr[array_search($type . '.' . $val['field'], $listArr)]);
  1944. }
  1945. }
  1946. }
  1947. # 处理字段授权
  1948. foreach ($listArr as $k => $v) {
  1949. if (!$userLevel && !empty($grantData[$types])) {
  1950. $status = getFieldGrantStatus($v, $grantData[$types]);
  1951. # 查看权限
  1952. if ($status['read'] == 0) unset($listArr[(int)$k]);
  1953. }
  1954. }
  1955. return $listArr ?: [];
  1956. }
  1957. /**
  1958. * [checkFieldPer 判断权限]
  1959. * @param types 分类
  1960. * @author Michael_xu
  1961. */
  1962. public function checkFieldPer($module, $controller, $action, $user_id = '')
  1963. {
  1964. $userModel = new \app\admin\model\User();
  1965. if (!checkPerByAction($module, $controller, $action)) return false;
  1966. if ($user_id && !in_array($user_id, $userModel->getUserByPer($module, $controller, $action))) return false;
  1967. return true;
  1968. }
  1969. /**
  1970. * [getField 获取字段属性]
  1971. * @param types 分类
  1972. * @author Michael_xu
  1973. */
  1974. public function getField($param)
  1975. {
  1976. $types = $param['types'];
  1977. $unFormType = $param['unFormType'];
  1978. if (!in_array($types, $this->types_arr)) {
  1979. return resultArray(['error' => '参数错误']);
  1980. }
  1981. $field_arr = [];
  1982. //模拟自定义字段返回
  1983. switch ($types) {
  1984. case 'admin_user' :
  1985. $field_arr = \app\hrm\model\Userdet::getField();
  1986. break;
  1987. default :
  1988. $data = [];
  1989. $data['types'] = $types;
  1990. $data['user_id'] = $param['user_id'];
  1991. if ($unFormType) $data['form_type'] = array('not in', $unFormType);
  1992. $field_arr = $this->fieldSearch($data);
  1993. }
  1994. if ($types == 'crm_visit') {
  1995. foreach ($field_arr as $key => $value) {
  1996. if ($value['name'] == '负责人') unset($field_arr[(int)$key]);
  1997. }
  1998. }
  1999. if ($types == 'crm_invoice') {
  2000. $field_arr = array_merge($field_arr, $this->getInvoiceSearch());
  2001. }
  2002. return $field_arr;
  2003. }
  2004. /**
  2005. * 自定义字段验重
  2006. *
  2007. * @param $field string 字段名称
  2008. * @param $val array|string 字段值
  2009. * @param $id int 数据id
  2010. * @param $types string 模块类型
  2011. * @return bool
  2012. */
  2013. public function getValidate($field, $val, $id, $types)
  2014. {
  2015. $val = is_array($val) ? $val : trim($val);
  2016. if (!$val) {
  2017. $this->error = '验证内容不能为空';
  2018. return false;
  2019. }
  2020. if (!$field) {
  2021. $this->error = '数据验证错误,请联系管理员!';
  2022. return false;
  2023. }
  2024. if (!in_array($types, $this->types_arr)) {
  2025. $this->error = '参数错误!';
  2026. return false;
  2027. }
  2028. $field_info = db('admin_field')->where(['types' => $types, 'field' => $field])->find();
  2029. if (!$field_info) {
  2030. $this->error = '数据验证错误,请联系管理员!';
  2031. return false;
  2032. }
  2033. // 人员、部门、文件、手写签名、描述文字、多选、明细表格跳过验证唯一性
  2034. if (in_array($field_info['form_type'], ['user', 'structure', 'file', 'handwriting_sign', 'desc_text', 'checkbox', 'detail_table'])) return true;
  2035. // 前端传来的定位数据有问题,在提交数据时做验证
  2036. if ($field_info['form_type'] == 'location') return true;
  2037. // 处理日期区间类型数据
  2038. if ($field_info['form_type'] == 'date_interval') {
  2039. $val = implode('_', $val);
  2040. }
  2041. // 处理地址类型数据
  2042. if ($field_info['form_type'] == 'position') {
  2043. $positionNames = array_column($val, 'name');
  2044. $val = implode(',', $positionNames);
  2045. }
  2046. $dataModel = '';
  2047. switch ($types) {
  2048. case 'crm_leads' :
  2049. $dataModel = new \app\crm\model\Leads();
  2050. break;
  2051. case 'crm_customer' :
  2052. $dataModel = new \app\crm\model\Customer();
  2053. break;
  2054. case 'crm_contacts' :
  2055. $dataModel = new \app\crm\model\Contacts();
  2056. break;
  2057. case 'crm_business' :
  2058. $dataModel = new \app\crm\model\Business();
  2059. break;
  2060. case 'crm_product' :
  2061. $dataModel = new \app\crm\model\Product();
  2062. break;
  2063. case 'crm_contract' :
  2064. $dataModel = new \app\crm\model\Contract();
  2065. break;
  2066. case 'crm_receivables' :
  2067. $dataModel = new \app\crm\model\Receivables();
  2068. break;
  2069. case 'crm_invoice' :
  2070. $dataModel = new \app\crm\model\Invoice();
  2071. break;
  2072. case 'oa_examine' :
  2073. $dataModel = db('oa_examine');
  2074. break;
  2075. case 'jxc_supplier' :
  2076. $dataModel = new \app\jxc\model\Supplier();//db('jxc_supplier');
  2077. break;
  2078. case 'jxc_product' :
  2079. $dataModel = db('jxc_product');
  2080. break;
  2081. case 'jxc_purchase' :
  2082. $dataModel = db('jxc_purchase');
  2083. break;
  2084. case 'jxc_retreat' :
  2085. $dataModel = db('jxc_retreat');
  2086. break;
  2087. case 'jxc_sale' :
  2088. $dataModel = db('jxc_sale');
  2089. break;
  2090. case 'jxc_salereturn' :
  2091. $dataModel = db('jxc_salereturn');
  2092. break;
  2093. case 'jxc_receipt' :
  2094. $dataModel = db('jxc_receipt');
  2095. break;
  2096. case 'jxc_outbound' :
  2097. $dataModel = db('jxc_outbound');
  2098. break;
  2099. case 'jxc_payment' :
  2100. $dataModel = db('jxc_payment');
  2101. break;
  2102. case 'jxc_collection' :
  2103. $dataModel = db('jxc_collection');
  2104. break;
  2105. case 'jxc_inventory' :
  2106. $dataModel = db('jxc_inventory');
  2107. break;
  2108. case 'jxc_allocation' :
  2109. $dataModel = db('jxc_allocation');
  2110. break;
  2111. }
  2112. $where = [];
  2113. $where[$field] = ['eq', $val];
  2114. // 编辑时的验重
  2115. if ($id) $where[$dataModel->getpk()] = ['neq', $id];
  2116. if ($types == 'crm_product') {
  2117. $where['delete_user_id'] = 0;
  2118. }
  2119. if ($types == 'jxc_product') {
  2120. $where['is_del'] = 0;
  2121. }
  2122. $res = $dataModel->where($where)->find();
  2123. if ($res) {
  2124. $this->error = '该数据已存在,请修改后提交!';
  2125. return false;
  2126. }
  2127. return true;
  2128. }
  2129. /**
  2130. * [getFieldByFormType 根据字段类型获取字段数组]
  2131. * @param types 分类
  2132. * @author Michael_xu
  2133. */
  2134. public function getFieldByFormType($types, $form_type)
  2135. {
  2136. $fieldArr = $this->where(['types' => $types, 'form_type' => $form_type])->column('field');
  2137. return $fieldArr ?: [];
  2138. }
  2139. /**
  2140. * [getFormValueByField 格式化表格字段类型值]
  2141. * @param $field 字段名
  2142. * @param $value 字段值
  2143. * @author Michael_xu
  2144. */
  2145. public function getFormValueByField($field, $value)
  2146. {
  2147. $formValue = db('admin_field')->where(['field' => $field])->value('form_value');
  2148. $formValue = sort_select($formValue, 'order_id', 1); //二维数组排序
  2149. $field = [];
  2150. foreach ($formValue as $k => $v) {
  2151. $field[] = $v['field'];
  2152. }
  2153. $data = [];
  2154. foreach ($value as $k => $v) {
  2155. foreach ($field as $key => $val) {
  2156. $data[$k][$val] = $v['value'];
  2157. }
  2158. }
  2159. return $data;
  2160. }
  2161. /**
  2162. * 根据form_type处理数据
  2163. * @author lee
  2164. */
  2165. public function getValueByFormtype($val, $form_type, $dataInfo)
  2166. {
  2167. $userModel = new \app\admin\model\User();
  2168. $structureModel = new \app\admin\model\Structure();
  2169. switch ($form_type) {
  2170. // case 'datetime' :
  2171. // $val = $val > 0 ? date('Y-m-d H:i:s', $val) : '';
  2172. // break;
  2173. case 'user' :
  2174. if (is_numeric($val)) {
  2175. $val = count($userModel->getUserNameByArr($val)) > 1 ? ArrayToString($userModel->getUserNameByArr($val)) : implode(',', $userModel->getUserNameByArr(stringToArray($val)));
  2176. } else {
  2177. $val = $val;
  2178. }
  2179. break;
  2180. case 'userStr' :
  2181. $val = explode(',', $val);
  2182. $val = count($userModel->getUserNameByArr($val)) > 1 ? ArrayToString($userModel->getUserNameByArr($val)) : implode(',', $userModel->getUserNameByArr($val));
  2183. break;
  2184. case 'structure' :
  2185. if (is_numeric($val)) {
  2186. $val = implode(',', $structureModel->getStructureNameByArr(stringToArray($val)));
  2187. } else {
  2188. $val = $val;
  2189. }
  2190. break;
  2191. case 'customer' :
  2192. $val = db('crm_customer')->where(['customer_id' => $val])->value('name');
  2193. break;
  2194. case 'contract' :
  2195. $val = db('crm_contract')->where(['contract_id' => $val])->value('num');
  2196. break;
  2197. case 'business' :
  2198. $val = db('crm_business')->where(['business_id' => $val])->value('name');
  2199. break;
  2200. case 'category' :
  2201. $val = db('crm_product_category')->where(['category_id' => $val])->value('name');
  2202. break;
  2203. case 'business_type' :
  2204. $val = db('crm_business_type')->where(['type_id' => $val])->value('name');
  2205. break;
  2206. case 'business_status' :
  2207. $val = db('crm_business_status')->where(['status_id' => $val])->value('name');
  2208. break;
  2209. case 'location' :
  2210. $val = $val['address'];
  2211. break;
  2212. case 'position' :
  2213. $val = trim(arrayToString(array_column($val, 'name')), ',');
  2214. break;
  2215. case 'warehouse_cause' :
  2216. $val = db('jxc_warehouse')->where(['warehouse_id' => $val])->value('warehouse_name');
  2217. break;
  2218. case 'sale_cause' :
  2219. $val = db('jxc_sale')->where(['sale_id' => $val])->value('order_number');
  2220. break;
  2221. case 'supplier_cause' :
  2222. $val = db('jxc_supplier')->where(['supplier_id' => $val])->value('supplier_name');
  2223. break;
  2224. case 'purchase_cause' :
  2225. $val = db('jxc_purchase')->where(['purchase_id' => $val])->value('order_number');
  2226. break;
  2227. case 'order_cause' :
  2228. if($dataInfo['receipt_type'] == '销售退货入库'){
  2229. $val = db('jxc_salereturn')->where(['salereturn_id' => $val])->value('order_number');
  2230. }elseif ($dataInfo['receipt_type'] == '采购入库') {
  2231. $val = db('jxc_purchase')->where(['purchase_id' => $val])->value('order_number');
  2232. }elseif ($dataInfo['outbound_type'] == '销售出库') {
  2233. $val = db('jxc_sale')->where(['sale_id' => $val])->value('order_number');
  2234. }elseif ($dataInfo['outbound_type'] == '采购退货出库') {
  2235. $val = db('jxc_retreat')->where(['retreat_id' => $val])->value('order_number');
  2236. }
  2237. break;
  2238. case 'category_cause' :
  2239. $val = db('jxc_product_category')->where(['category_id' => $val])->value('category_name');
  2240. break;
  2241. case 'product_cause' :
  2242. $val = db('jxc_product')->where(['product_id' => $val])->value('product_name');
  2243. break;
  2244. case 'collection_object' :
  2245. if($dataInfo['collection_type'] == '采购退货'){
  2246. // $val = db('jxc_supplier')->where(['supplier_id' => $val])->value('supplier_name');
  2247. }elseif ($dataInfo['collection_type'] == '销售出库') {
  2248. $val = db('jxc_sale')->where(['sale_id' => $val])->value('order_number');
  2249. }elseif ($dataInfo['payment_type'] == '采购') {
  2250. $val = db('crm_customer')->where(['customer_id' => $val])->value('name');
  2251. }elseif ($dataInfo['payment_type'] == '销售退货') {
  2252. $val = db('jxc_salereturn')->where(['salereturn_id' => $val])->value('order_number');
  2253. }
  2254. break;
  2255. }
  2256. return $val;
  2257. }
  2258. /**
  2259. * [getIndexFieldList 列表展示字段]
  2260. * @param types 分类
  2261. * @author Michael_xu
  2262. */
  2263. public function getIndexFieldList($types, $user_id)
  2264. {
  2265. $fieldArr = $this->getIndexField($types, $user_id);
  2266. $types = $types == 'crm_customer_pool' ? 'crm_customer' : $types;
  2267. $fieldList = db('admin_field')->where(['field' => array('in', $fieldArr)])->where('types', ['eq', $types], ['eq', ''], 'or')->order('order_id asc')->select();
  2268. return $fieldList ?: [];
  2269. }
  2270. /**
  2271. * [getArrayField 数组类型字段]
  2272. * @param types 分类
  2273. * @author Michael_xu
  2274. */
  2275. public function getArrayField($types)
  2276. {
  2277. $arrayFormType = ['structure', 'user', 'checkbox', 'file'];
  2278. $arrFieldAtt = db('admin_field')->where(['types' => $types, 'form_type' => ['in', $arrayFormType]])->column('field');
  2279. return $arrFieldAtt ?: [];
  2280. }
  2281. /**
  2282. * 字段对照关系处理
  2283. * @param $types 分类
  2284. * @param $data 数据
  2285. * @return
  2286. * @author Michael_xu
  2287. */
  2288. public function getRelevantData($types, $data = [])
  2289. {
  2290. $types_arr = ['crm_leads'];
  2291. if (!in_array($types, $types_arr)) {
  2292. $this->error = '参数错误';
  2293. return false;
  2294. }
  2295. if (!$data) return $data;
  2296. $list = db('admin_field')->where(['types' => $types, 'relevant' => ['neq', '']])->field('field,relevant')->select();
  2297. foreach ($list as &$val){
  2298. $val=!empty($val)?$val:'';
  2299. }
  2300. $newData = [];
  2301. //crm_hfsomz
  2302. foreach ($list as $k => $v) {
  2303. $customer_field=db('admin_field')->where(['types' => 'crm_customer', 'field_id' => $v['relevant']])->field('field')->find();
  2304. $newData[$customer_field['field']] = $data[$v['field']];
  2305. }
  2306. return $newData ?: [];
  2307. }
  2308. /**
  2309. * 字段排序
  2310. * @param types 自定义字段分类
  2311. * @param prefix 自定义字段前缀
  2312. * @param field 自定义字段
  2313. * @param order 排序规则
  2314. * @return
  2315. * @author Michael_xu
  2316. */
  2317. public function getOrderByFormtype($types, $prefix, $field, $order_type)
  2318. {
  2319. $form_type = $this->where(['types' => $types, 'field' => $field])->value('form_type');
  2320. // die('123');
  2321. // if (!$form_type) {
  2322. // $this->error = '参数错误';
  2323. // return false;
  2324. // }
  2325. $temp_field = $field;
  2326. $field = $prefix ? $prefix . '.' . $field : $field;
  2327. switch ($form_type) {
  2328. case 'textarea' :
  2329. case 'radio' :
  2330. case 'select' :
  2331. case 'checkbox' :
  2332. case 'address' :
  2333. $order = 'convert(' . $field . ' using gbk) ' . trim($order_type);
  2334. break;
  2335. default :
  2336. $order = $field . ' ' . $order_type;
  2337. break;
  2338. }
  2339. if (isset($this->orther_field_list[$types])) {
  2340. foreach ($this->orther_field_list[$types] as $val) {
  2341. // $res[] = $val;
  2342. // $order
  2343. $temp = trim($prefix . '.' . $val['field'], '.');
  2344. if ($temp == $field) {
  2345. $order = str_replace($temp, $val['field'], $order);
  2346. }
  2347. }
  2348. }
  2349. return $order;
  2350. }
  2351. /**
  2352. * 处理字段别名、权限
  2353. *
  2354. * @param int $userId 用户ID
  2355. * @param string $types 模块类型
  2356. * @param string $action 行为
  2357. * @param array $data 字段数据
  2358. * @return array
  2359. * @since 2021-04-06
  2360. * @author fanqi
  2361. */
  2362. public function resetField($userId, $types, $action, $data)
  2363. {
  2364. $grantData = getFieldGrantData($userId);
  2365. $userLevel = isSuperAdministrators($userId);
  2366. foreach ($data as $key => $value) {
  2367. # 处理字段授权
  2368. if (!$userLevel && !empty($grantData[$types])) {
  2369. $status = getFieldGrantStatus($value['field'], $grantData[$types]);
  2370. # 查看权限
  2371. if ($status['read'] == 0) {
  2372. unset($data[(int)$key]);
  2373. continue;
  2374. }
  2375. }
  2376. switch ($value['field']) {
  2377. case 'create_user_id' :
  2378. $data[$key]['fieldName'] = 'create_user_name';
  2379. break;
  2380. case 'owner_user_id' :
  2381. $data[$key]['fieldName'] = 'owner_user_name';
  2382. break;
  2383. case 'customer_id' :
  2384. $data[$key]['fieldName'] = 'customer_name';
  2385. break;
  2386. case 'type_id' :
  2387. $data[$key]['fieldName'] = 'type_id_info';
  2388. break;
  2389. case 'status_id' :
  2390. $data[$key]['fieldName'] = 'status_id_info';
  2391. break;
  2392. case 'business_id' :
  2393. $data[$key]['fieldName'] = 'business_name';
  2394. break;
  2395. case 'contacts_id' :
  2396. $data[$key]['fieldName'] = 'contacts_name';
  2397. break;
  2398. case 'order_user_id' :
  2399. $data[$key]['fieldName'] = 'order_user_name';
  2400. break;
  2401. case 'contract_id' :
  2402. $data[$key]['fieldName'] = 'contract_num';
  2403. break;
  2404. case 'plan_id' :
  2405. $data[$key]['fieldName'] = 'plan_id_info';
  2406. break;
  2407. case 'category_id' :
  2408. $data[$key]['fieldName'] = 'category_name';
  2409. break;
  2410. default :
  2411. $data[$key]['fieldName'] = $value['field'];
  2412. }
  2413. # 详情中不显示产品类别、回款期数
  2414. if ($action == 'read' && in_array($value['field'], ['category_id'])) {
  2415. unset($data[(int)$key]);
  2416. }
  2417. }
  2418. return $data;
  2419. }
  2420. /**
  2421. * 获取公海自定义字段数据
  2422. *
  2423. * @param $poolId
  2424. * @param $dataInfo
  2425. * @return bool|\PDOStatement|string|\think\Collection
  2426. */
  2427. public function getPoolFieldData($poolId, $dataInfo)
  2428. {
  2429. $poolFields = db('crm_customer_pool_field_setting')->field(['field_name AS field', 'form_type', 'name'])->where(['pool_id' => $poolId, 'is_hidden' => 0])->select();
  2430. foreach ($poolFields as $key => $value) {
  2431. # 字段值
  2432. $poolFields[$key]['value'] = !empty($dataInfo[$value['field']]) ? $dataInfo[$value['field']] : '';
  2433. # 处理别名
  2434. switch ($value['field']) {
  2435. case 'create_user_id' :
  2436. $poolFields[$key]['fieldName'] = 'create_user_name';
  2437. $poolFields[$key]['value'] = !empty($dataInfo['create_user_id_info']) ? [$dataInfo['create_user_id_info']] : '';
  2438. break;
  2439. case 'before_owner_user_id' :
  2440. $poolFields[$key]['fieldName'] = 'before_owner_user_name';
  2441. $poolFields[$key]['value'] = !empty($dataInfo['before_owner_user_id_info']) ? [$dataInfo['before_owner_user_id_info']] : '';
  2442. break;
  2443. default :
  2444. $poolFields[$key]['fieldName'] = $value['field'];
  2445. }
  2446. if (in_array($value['form_type'], ['user', 'structure']) && !in_array($value['field'], ['create_user_id', 'owner_user_id', 'before_owner_user_id'])) {
  2447. $poolFields[$key]['fieldName'] = $value['field_name'] . '_name';
  2448. }
  2449. # 系统字段
  2450. if (in_array($value['field'], ['last_record', 'create_user_id', 'create_time', 'update_time', 'last_time', 'before_owner_user_id'])) {
  2451. $poolFields[$key]['system'] = 1;
  2452. } else {
  2453. $poolFields[$key]['system'] = 0;
  2454. }
  2455. }
  2456. return $poolFields;
  2457. }
  2458. /**
  2459. * 获取发票高级搜索字段
  2460. *
  2461. * @return array[]
  2462. */
  2463. private function getInvoiceSearch()
  2464. {
  2465. return [
  2466. // ['field' => 'invoice_number', 'form_type' => 'text', 'setting' => [], 'name' => '发票号码'],
  2467. ['field' => 'real_invoice_date', 'form_type' => 'datetime', 'setting' => [], 'name' => '实际开票日期'],
  2468. ['field' => 'logistics_number', 'form_type' => 'text', 'setting' => [], 'name' => '物流单号'],
  2469. ['field' => 'invoice_status', 'form_type' => 'select', 'setting' => ['未开票', '已开票'], 'name' => '开票状态'],
  2470. ['field' => 'check_status', 'form_type' => 'select', 'setting' => ['待审核', '审核中', '审核通过', '审核未通过', '撤回'], 'name' => '审核状态'],
  2471. ];
  2472. }
  2473. /**
  2474. * 获取产品父类层级(不包含自身)
  2475. *
  2476. * @param $productId
  2477. * @param array $parentIds
  2478. * @return array|mixed
  2479. * @throws \think\db\exception\DataNotFoundException
  2480. * @throws \think\db\exception\ModelNotFoundException
  2481. * @throws \think\exception\DbException
  2482. */
  2483. private function getProductParentIds($productId, &$parentIds = [])
  2484. {
  2485. $category = db('crm_product_category')->select();
  2486. foreach ($category as $key => $value) {
  2487. if ($value['category_id'] == $productId) {
  2488. if (!empty($value['pid'])) {
  2489. $parentIds[] = $value['pid'];
  2490. $this->getProductParentIds($value['pid'], $parentIds);
  2491. }
  2492. }
  2493. }
  2494. return $parentIds;
  2495. }
  2496. /**
  2497. * 设置明细表格数据
  2498. *
  2499. * @param string $types 模块类型:crm_leads crm_customer ...
  2500. * @param string $field 字段名称
  2501. * @param array $data 明细表单中的字段数据
  2502. * @return bool
  2503. * @since 2021-04-27
  2504. * @author fanqi
  2505. */
  2506. private function setDetailTableData($types, $field, $data)
  2507. {
  2508. $id = db('admin_field_extend')->where(['field' => $field, 'types' => $types])->value('id');
  2509. foreach ($data as $k1 => $v1) {
  2510. if (empty($v1['field'])) $data[$k1]['field'] = $this->createField($types, $types == 'oa_examine' ? 'oa_' : 'crm_');
  2511. $data[$k1]['default_value'] = !empty($v1['default_value']) ? $v1['default_value'] : '';
  2512. }
  2513. if (!empty($id)) {
  2514. return db('admin_field_extend')->where('id', $id)->update(['content' => json_encode($data)]);
  2515. }
  2516. if (empty($id)) {
  2517. return db('admin_field_extend')->insert(['types' => $types, 'field' => $field, 'content' => json_encode($data), 'create_time' => time()]);
  2518. }
  2519. return false;
  2520. }
  2521. }