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