Field.php 105KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  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. $default_value = date('Y-m-d', time());
  1060. }
  1061. //地图类型
  1062. if ($v['form_type'] == 'map_address') {
  1063. $value = [
  1064. 'address' => $dataInfo['address'] ? explode(chr(10), $dataInfo['address']) : [],
  1065. 'location' => $dataInfo['location'],
  1066. 'detail_address' => $dataInfo['detail_address'],
  1067. 'lng' => $dataInfo['lng'],
  1068. 'lat' => $dataInfo['lat']
  1069. ];
  1070. } elseif ($v['form_type'] == 'product') {
  1071. //相关产品类型
  1072. switch ($param['types']) {
  1073. case 'crm_business' :
  1074. $rProduct = db('crm_business_product');
  1075. $r_id = 'business_id';
  1076. break;
  1077. case 'crm_contract' :
  1078. $rProduct = db('crm_contract_product');
  1079. $r_id = 'contract_id';
  1080. break;
  1081. default :
  1082. break;
  1083. }
  1084. $newProductList = [];
  1085. $productList = $rProduct->where([$r_id => $param['action_id']])->select();
  1086. foreach ($productList as $key => $product) {
  1087. $product_info = [];
  1088. $category_name = '';
  1089. $product_info = db('crm_product')->where(['product_id' => $product['product_id']])->field('product_id,name,category_id')->find();
  1090. $category_name = db('crm_product_category')->where(['category_id' => $product_info['category_id']])->value('name');
  1091. $productList[$key]['name'] = $product_info['name'] ?: '';
  1092. $productList[$key]['category_id_info'] = $category_name ?: '';
  1093. }
  1094. $value = [
  1095. 'product' => $productList,
  1096. 'total_price' => $dataInfo['total_price'],
  1097. 'discount_rate' => $dataInfo['discount_rate']
  1098. ];
  1099. } elseif ($v['form_type'] == 'user') {
  1100. $value = $userModel->getListByStr($dataInfo[$v['field']]) ?: [];
  1101. if (empty($value)) $default_value = $userModel->getListByStr($param['user_id']) ?: [];
  1102. } elseif ($v['form_type'] == 'single_user') {
  1103. # 单用户
  1104. $userInfo = $userModel->getListByStr($dataInfo[$v['field']]);
  1105. $value = !empty($userInfo[0]) ? $userInfo[0] : [];
  1106. if (empty($value)) {
  1107. $userInfo = $userModel->getListByStr($param['user_id']);
  1108. $default_value = !empty($userInfo[0]) ? $userInfo[0] : [];
  1109. }
  1110. } elseif ($v['form_type'] == 'structure') {
  1111. $value = $structureModel->getListByStr($dataInfo[$v['field']]) ?: [];
  1112. } elseif ($v['form_type'] == 'file') {
  1113. $fileIds = [];
  1114. $fileIds = stringToArray($dataInfo[$v['field']]);
  1115. $whereFile = [];
  1116. $whereFile['module'] = 'other';
  1117. $whereFile['module_id'] = 1;
  1118. $whereFile['file_id'] = ['in', $fileIds];
  1119. $fileList = $fileModel->getDataList($whereFile, 'all');
  1120. $value = $fileList['list'] ?: [];
  1121. } elseif ($v['form_type'] == 'customer') {
  1122. $value = $dataInfo[$v['field']] ? db('crm_customer')->where(['customer_id' => $dataInfo[$v['field']]])->field('customer_id,name')->select() : [];
  1123. } elseif ($v['form_type'] == 'business') {
  1124. $value = $dataInfo[$v['field']] ? db('crm_business')->where(['business_id' => $dataInfo[$v['field']]])->field('business_id,name')->select() : [];
  1125. } elseif ($v['form_type'] == 'contacts') {
  1126. $value = $dataInfo[$v['field']] ? db('crm_contacts')->where(['contacts_id' => $dataInfo[$v['field']]])->field('contacts_id,name')->select() : [];
  1127. } elseif ($v['form_type'] == 'contract') {
  1128. $value = $dataInfo[$v['field']] ? db('crm_contract')->where(['contract_id' => $dataInfo[$v['field']]])->field('contract_id,num')->select() : [];
  1129. } elseif ($v['form_type'] == 'floatnumber' && $v['field'] == 'contract_money' && $types == 'crm_invoice') {
  1130. $contractMoney = db('crm_invoice')->alias('invoice')
  1131. ->join('__CRM_CONTRACT__ contract', 'invoice.contract_id = contract.contract_id', 'LEFT')
  1132. ->where('invoice.invoice_id', $param['action_id'])->value('contract.money');
  1133. $value = $contractMoney;
  1134. } elseif ($v['form_type'] == 'category') {
  1135. //产品类别
  1136. if ($param['action'] == 'read') {
  1137. $category_name = db('crm_product_category')->where(['category_id' => $dataInfo['category_id']])->value('name');
  1138. $value = $category_name ?: '';
  1139. } elseif ($param['action'] == 'update') {
  1140. $parentIds = [];
  1141. if (!empty($dataInfo['category_id'])) {
  1142. $parentIds = $this->getProductParentIds($dataInfo['category_id']);
  1143. $parentIds = array_reverse($parentIds);
  1144. array_push($parentIds, $dataInfo['category_id']);
  1145. }
  1146. $value = $parentIds;
  1147. } else {
  1148. $categoryModel = new \app\crm\model\ProductCategory();
  1149. $value = $categoryModel->getDataList('tree');
  1150. }
  1151. } elseif ($v['form_type'] == 'business_type') {
  1152. //商机状态组
  1153. $businessStatusModel = new \app\crm\model\BusinessStatus();
  1154. $userInfo = $userModel->getUserById($user_id);
  1155. $setting = db('crm_business_type')
  1156. ->where('status', 1)
  1157. ->where('is_display', 1)
  1158. ->where(function ($query) use ($userInfo) {
  1159. $query->where(['structure_id' => ['like', '%,' . $userInfo['structure_id'] . ',%']]);
  1160. $query->whereOr('structure_id', '');
  1161. })->select();
  1162. foreach ($setting as $key => $val) {
  1163. $setting[$key]['statusList'] = $businessStatusModel->getDataList($val['type_id'], 0);
  1164. }
  1165. $setting = $setting ?: [];
  1166. if ($param['action'] == 'read') {
  1167. $value = $dataInfo[$v['field']] ? db('crm_business_type')->where(['type_id' => $dataInfo[$v['field']]])->value('name') : '';
  1168. } else {
  1169. $value = (int)$dataInfo[$v['field']] ?: '';
  1170. }
  1171. } elseif ($v['form_type'] == 'business_status') {
  1172. //商机阶段
  1173. if ($param['action'] == 'read') {
  1174. $value = $dataInfo[$v['field']] ? db('crm_business_status')->where(['status_id' => $dataInfo[$v['field']]])->value('name') : '';
  1175. } else {
  1176. $businessStatusModel = new \app\crm\model\BusinessStatus();
  1177. $setting = $businessStatusModel->getDataList($dataInfo['type_id'], 1);
  1178. $value = (int)$dataInfo[$v['field']] ?: '';
  1179. }
  1180. } elseif ($v['form_type'] == 'receivables_plan') {
  1181. //回款计划期数
  1182. $value = $dataInfo[$v['field']] ? db('crm_receivables_plan')->where(['plan_id' => $dataInfo[$v['field']]])->value('num') : '';
  1183. } elseif ($v['form_type'] == 'business_cause' || $v['form_type'] == 'examine_cause') {
  1184. $whereTravel = [];
  1185. $whereTravel['examine_id'] = $dataInfo['examine_id'];
  1186. $travelList = db('oa_examine_travel')->where($whereTravel)->select() ?: [];
  1187. foreach ($travelList as $key => $val) {
  1188. $where = [];
  1189. $fileList = [];
  1190. $imgList = [];
  1191. $where['module'] = 'oa_examine_travel';
  1192. $where['module_id'] = $val['travel_id'];
  1193. $newFileList = [];
  1194. $newFileList = $fileModel->getDataList($where, 'all');
  1195. if ($newFileList['list']) {
  1196. foreach ($newFileList['list'] as $val1) {
  1197. if ($val1['types'] == 'file') {
  1198. $fileList[] = $val1;
  1199. } else {
  1200. $imgList[] = $val1;
  1201. }
  1202. }
  1203. }
  1204. $travelList[$key]['start_time'] = $val['start_time'] ? date('Y-m-d H:i:s', $val['start_time']) : null;
  1205. $travelList[$key]['end_time'] = $val['end_time'] ? date('Y-m-d H:i:s', $val['end_time']) : null;
  1206. $travelList[$key]['fileList'] = $fileList ?: [];
  1207. $travelList[$key]['imgList'] = $imgList ?: [];
  1208. }
  1209. $value = $travelList ?: [];
  1210. } elseif ($v['form_type'] == 'checkbox') {
  1211. $value = isset($dataInfo[$v['field']]) ? stringToArray($dataInfo[$v['field']]) : [];
  1212. } elseif ($v['form_type'] == 'date') {
  1213. $value = ($dataInfo[$v['field']] && $dataInfo[$v['field']] !== '0000-00-00') ? $dataInfo[$v['field']] : '';
  1214. } elseif ($v['form_type'] == 'boolean_value') {
  1215. // 布尔类型
  1216. $value = !empty($dataInfo[$v['field']]) ? (string)$dataInfo[$v['field']] : '0';
  1217. } elseif ($v['form_type'] == 'percent') {
  1218. // 百分数
  1219. $value = !empty($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
  1220. } elseif ($v['form_type'] == 'website') {
  1221. // 网址
  1222. $value = !empty($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
  1223. } elseif ($v['form_type'] == 'handwriting_sign') {
  1224. // 手写签名
  1225. $fileData = !empty($dataInfo[$v['field']]) ? db('admin_file')->where('file_id', $dataInfo[$v['field']])->find() : '';
  1226. if (!empty($fileData['file_path'])) $fileData['file_path'] = getFullPath($fileData['file_path']);
  1227. if (!empty($fileData['file_path_thumb'])) $fileData['file_path_thumb'] = getFullPath($fileData['file_path_thumb']);
  1228. $value = !empty($fileData) ? ['file_id' => $fileData['file_id'], 'url' => $fileData['file_path']] : "";
  1229. } elseif ($v['form_type'] == 'desc_text') {
  1230. // 描述文字
  1231. $value = !empty($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : $v['default_value'];
  1232. } elseif (in_array($v['form_type'], ['position', 'location', 'date_interval', 'detail_table'])) {
  1233. // 地址、定位、日期区间、明细表格
  1234. $primaryKey = getPrimaryKeyName($param['types']);
  1235. $positionJson = !empty($dataInfo[$primaryKey]) ? db($param['types'] . '_data')->where([$primaryKey => $dataInfo[$primaryKey], 'field' => $v['field']])->value($param['types'] == 'oa_examine' ? 'value' : 'content') : '';
  1236. $positionData = !empty($positionJson) ? json_decode($positionJson, true) : '';
  1237. $value = $positionData;
  1238. if ($v['form_type'] == 'detail_table') {
  1239. $content = db('admin_field_extend')->where(['types' => $types, 'field' => $v['field']])->value('content');
  1240. $field_list[$k]['fieldExtendList'] = json_decode($content, true);
  1241. }
  1242. } else {
  1243. $value = isset($dataInfo[$v['field']]) ? $dataInfo[$v['field']] : '';
  1244. }
  1245. $field_list[$k]['setting'] = $setting;
  1246. $field_list[$k]['default_value'] = $default_value;
  1247. $field_list[$k]['value'] = $value;
  1248. $field_list[$k]['options'] = !empty($field_list[$k]['options']) ? $field_list[$k]['options'] : '';
  1249. $field_list[$k]['optionsData'] = !empty($field_list[$k]['options']) ? json_decode($field_list[$k]['options'], true) : '';
  1250. }
  1251. }
  1252. return array_values($field_list) ?: [];
  1253. }
  1254. private function getFormPositionXValue($types, $typesId)
  1255. {
  1256. $positionArray = db('admin_field')->where(['types' => $types, 'types_id' => $typesId, 'form_position' => [['neq', ''], ['not null'], 'AND']])->column('form_position');
  1257. if (!empty($positionArray)) {
  1258. $positionString = implode('-', $positionArray);
  1259. $positionString = str_replace(',0', '', $positionString);
  1260. $positionString = str_replace(',1', '', $positionString);
  1261. $positionString = str_replace(',2', '', $positionString);
  1262. $positionString = str_replace(',3', '', $positionString);
  1263. $positionArray = explode('-', $positionString);
  1264. return max($positionArray) + 1;
  1265. }
  1266. return 0;
  1267. }
  1268. /**
  1269. * [fieldSearch 获取自定义字段高级筛选信息]
  1270. * @param $types 分类
  1271. * @param $map 查询条件
  1272. * @param form_type 字段类型 (’text’,’textarea’,’mobile’,’email’等)
  1273. * @param setting 设置 (单选、下拉、多选的选项值,使用回车分隔)
  1274. * @author Michael_xu
  1275. */
  1276. public function fieldSearch($param)
  1277. {
  1278. $types = $param['types'];
  1279. if (!in_array($types, $this->types_arr)) {
  1280. $this->error = '参数错误';
  1281. return false;
  1282. }
  1283. $userModel = new \app\admin\model\User();
  1284. $user_id = $param['user_id'];
  1285. $map['types'] = ['in', ['', $types]];
  1286. $map['form_type'] = ['not in', ['file', 'form', 'business_status', 'detail_table', 'desc_text', 'handwriting_sign', 'date_interval']];
  1287. $map['is_hidden'] = 0;
  1288. $field_list = db('admin_field')
  1289. ->where($map)
  1290. ->whereOr(['types' => ''])
  1291. ->field('field,name,form_type,setting')
  1292. ->order('order_id asc, field_id asc, update_time desc')
  1293. ->select();
  1294. if (in_array($types, ['crm_contract', 'crm_receivables'])) {
  1295. $field_arr = [
  1296. '0' => [
  1297. 'field' => 'check_status',
  1298. 'name' => '审核状态',
  1299. 'form_type' => 'select',
  1300. 'setting' => '待审核' . chr(10) . '审核中' . chr(10) . '审核通过' . chr(10) . '审核失败' . chr(10) . '已撤回' . chr(10) . '未提交' . chr(10) . '已作废'
  1301. ]
  1302. ];
  1303. }
  1304. if (in_array($param['types'], ['crm_customer'])) {
  1305. $field_arr = [
  1306. '0' => [
  1307. 'field' => 'address',
  1308. 'name' => '地区定位',
  1309. 'form_type' => 'address',
  1310. 'setting' => []
  1311. ]
  1312. ];
  1313. }
  1314. if ($param['types'] == 'crm_customer') {
  1315. $field_arr[] = [
  1316. 'field' => 'detail_address',
  1317. 'name' => '详细地址',
  1318. 'form_type' => 'text',
  1319. 'setting' => []
  1320. ];
  1321. }
  1322. if (in_array($param['types'], ['crm_customer', 'crm_leads', 'crm_contacts', 'crm_business', 'crm_contract'])) {
  1323. $field_arr[] = [
  1324. 'field' => 'last_time',
  1325. 'name' => '最后跟进时间',
  1326. 'form_type' => 'datetime',
  1327. 'setting' => []
  1328. ];
  1329. }
  1330. if ($field_arr) $field_list = array_merge($field_list, $field_arr);
  1331. foreach ($field_list as $k => $v) {
  1332. //处理setting内容
  1333. $setting = [];
  1334. if (in_array($v['form_type'], ['radio', 'select', 'checkbox'])) {
  1335. $setting = explode(chr(10), $v['setting']);
  1336. }
  1337. $field_list[$k]['setting'] = $setting;
  1338. if ($v['field'] == 'customer_id') {
  1339. $field_list[$k]['form_type'] = 'module';
  1340. $field_list[$k]['field'] = 'customer_name';
  1341. }
  1342. if ($v['field'] == 'business_id') {
  1343. $field_list[$k]['form_type'] = 'module';
  1344. $field_list[$k]['field'] = 'business_name';
  1345. }
  1346. if ($v['field'] == 'contract_id') {
  1347. $field_list[$k]['form_type'] = 'module';
  1348. $field_list[$k]['field'] = 'contract_name';
  1349. }
  1350. if ($v['field'] == 'contacts_id') {
  1351. $field_list[$k]['form_type'] = 'module';
  1352. $field_list[$k]['field'] = 'contacts_name';
  1353. }
  1354. if ($v['form_type'] == 'category') {
  1355. } elseif ($v['form_type'] == 'business_type') {
  1356. //商机状态组
  1357. $businessStatusModel = new \app\crm\model\BusinessStatus();
  1358. $userInfo = $userModel->getUserById($user_id);
  1359. $setting = db('crm_business_type')
  1360. ->where(['structure_id' => ['like', ',%' . $userInfo['structure_id'] . '%,'], 'status' => 1])
  1361. ->whereOr('structure_id', '')
  1362. ->select();
  1363. foreach ($setting as $key => $val) {
  1364. $setting[$key]['statusList'] = $businessStatusModel->getDataList($val['type_id'], 1);
  1365. }
  1366. $setting = $setting ?: [];
  1367. }
  1368. $field_list[$k]['setting'] = $setting;
  1369. }
  1370. return $field_list ?: [];
  1371. }
  1372. /**
  1373. * 自定义字段验证规则
  1374. * @param string $types 类型:crm_customer crm_business ...
  1375. * @param int $types_id 自定义表types_id
  1376. * @param string $action 操作:save update
  1377. * @return array
  1378. */
  1379. public function validateField($types, $types_id = 0, $action = 'save')
  1380. {
  1381. $apiCommon = new ApiCommon();
  1382. $userId = $apiCommon->userInfo['id'];
  1383. $grantData = getFieldGrantData($userId);
  1384. $userLevel = isSuperAdministrators($userId);
  1385. $unField = ['update_time', 'create_time', 'create_user_id', 'owner_user_id'];
  1386. $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();
  1387. $validateArr = [];
  1388. $rule = [];
  1389. $message = [];
  1390. foreach ($fieldList as $field) {
  1391. # 字段授权
  1392. if (!$userLevel && !empty($grantData[$types])) {
  1393. $status = getFieldGrantStatus($field['field'], $grantData[$types]);
  1394. # 没有字段查看权限或者编辑时没有字段修改权限就跳过验证
  1395. if (empty($status['read']) || ($action != 'save' && empty($status['write']))) continue;
  1396. }
  1397. $rule_value = '';
  1398. $scene_value = '';
  1399. $max_length = $field['max_length'] ?: '';
  1400. if ($field['is_null']) {
  1401. $rule_value .= 'require';
  1402. $message[$field['field'] . '.require'] = $field['name'] . '不能为空';
  1403. }
  1404. if ($field['form_type'] == 'number') {
  1405. if ($rule_value) $rule_value .= '|';
  1406. $rule_value .= 'number';
  1407. $message[$field['field'] . '.number'] = $field['name'] . '必须是数字';
  1408. } elseif ($field['form_type'] == 'email') {
  1409. if ($rule_value) $rule_value .= '|';
  1410. $rule_value .= 'email';
  1411. $message[$field['field'] . '.email'] = $field['name'] . '格式错误';
  1412. } elseif ($field['form_type'] == 'mobile ') {
  1413. if ($rule_value) $rule_value .= '|';
  1414. $rule_value .= 'regex:^1[3456789][0-9]{9}?$';
  1415. $message[$field['field'] . '.regex'] = $field['name'] . '格式错误';
  1416. }
  1417. if ($field['is_unique']) {
  1418. if ($rule_value) $rule_value .= '|';
  1419. $rule_value .= 'unique:' . $types;
  1420. $message[$field['field'] . '.unique'] = $field['name'] . '已经存在,不能重复添加';
  1421. }
  1422. if ($max_length) {
  1423. if ($rule_value) $rule_value .= '|';
  1424. $rule_value .= 'max:' . $max_length;
  1425. $message[$field['field'] . '.max'] = $field['name'] . '不能超过' . $max_length . '个字符';
  1426. }
  1427. // if ($field['form_type'] == 'datetime') {
  1428. // $rule_value .= 'date';
  1429. // $message[$field['field'].'.date'] = $field['name'].'格式错误';
  1430. // }
  1431. if ($rule_value == 'require|') $rule_value = 'require';
  1432. if (!empty($rule_value)) $rule[$field['field']] = $rule_value;
  1433. }
  1434. $validateArr['rule'] = $rule ?: [];
  1435. $validateArr['message'] = $message ?: [];
  1436. return $validateArr;
  1437. }
  1438. /**
  1439. * [getIndexField 列表展示字段]
  1440. * @param types 分类
  1441. * @param excel 导出使用
  1442. * @author Michael_xu
  1443. */
  1444. public function getIndexFieldConfig($types, $user_id, $types_id = '',$excel='')
  1445. {
  1446. $userFieldModel = new \app\admin\model\UserField();
  1447. $userFieldData = $userFieldModel->getConfig($types, $user_id);
  1448. $userFieldData = $userFieldData ? json_decode($userFieldData, true) : [];
  1449. $grantData = getFieldGrantData($user_id);
  1450. $userLevel = isSuperAdministrators($user_id);
  1451. $fieldList = $this->getFieldList($types, $types_id,$excel);
  1452. $where = [];
  1453. if ($userFieldData) {
  1454. $fieldArr = [];
  1455. $i = 0;
  1456. foreach ($userFieldData as $k => $v) {
  1457. if (empty($fieldList[$k])) {
  1458. unset($userFieldData[$k]);
  1459. continue;
  1460. }
  1461. if (empty($v['is_hide'])) {
  1462. $fieldArr[$i]['field'] = $k;
  1463. $fieldArr[$i]['name'] = $fieldList[$k]['name'];
  1464. $fieldArr[$i]['form_type'] = $fieldList[$k]['form_type'];
  1465. $fieldArr[$i]['width'] = $v['width'] ?: '';
  1466. $i++;
  1467. }
  1468. }
  1469. $dataList = $fieldArr;
  1470. } else {
  1471. $dataList = $fieldList;
  1472. }
  1473. # 处理字段授权
  1474. foreach ($dataList as $k => $v) {
  1475. if (!$userLevel && !empty($grantData[$types])) {
  1476. $status = getFieldGrantStatus($v['field'], $grantData[$types]);
  1477. # 查看权限
  1478. if ($status['read'] == 0) unset($dataList[(int)$k]);
  1479. }
  1480. }
  1481. return array_values($dataList) ?: [];
  1482. }
  1483. /**
  1484. * 获取列表展示字段
  1485. * @return $types_id 默认为空多自定义字段条件使用
  1486. * @return void
  1487. * @author Ymob
  1488. * @datetime 2019-10-23 17:32:57
  1489. */
  1490. public function getFieldList($types, $types_id = '', $excel = '')
  1491. {
  1492. $newTypes = $types;
  1493. $unField = ['-1'];
  1494. if ($types == 'crm_customer_pool') {
  1495. $newTypes = 'crm_customer';
  1496. $unField = ['owner_user_id'];
  1497. }
  1498. if($excel=='excel'){
  1499. $where=[
  1500. 'types' => ['IN', ['', $newTypes]],
  1501. 'form_type' => ['not in', ['file', 'form', 'deal_status','position','location','handwriting_sign','date_interval','detail_table','desc_text','boolean_value']],
  1502. 'field' => ['not in', $unField],
  1503. 'types_id' => ['eq', $types_id],
  1504. 'is_hidden' => 0
  1505. ];
  1506. }else{
  1507. $where=[
  1508. 'types' => ['IN', ['', $newTypes]],
  1509. 'form_type' => ['not in', ['file', 'form', 'desc_text', 'detail_table']],
  1510. 'field' => ['not in', $unField],
  1511. 'types_id' => ['eq', $types_id],
  1512. 'is_hidden' => 0
  1513. ];
  1514. }
  1515. $fieldArr = $this
  1516. ->where($where)
  1517. ->field(['field', 'name', 'form_type', 'is_hidden'])
  1518. ->order('field_id', 'asc')
  1519. ->select();
  1520. $res = [];
  1521. foreach ($fieldArr as $val) {
  1522. $res[] = $val->toArray();
  1523. }
  1524. if ($types == 'oa_examine') {
  1525. }
  1526. if (isset($this->orther_field_list[$newTypes])) {
  1527. foreach ($this->orther_field_list[$newTypes] as $val) {
  1528. $res[] = $val;
  1529. }
  1530. }
  1531. if ($types == 'crm_customer') {
  1532. $res[] = [
  1533. 'field' => 'pool_day',
  1534. 'name' => '距进入公海天数',
  1535. 'form_type' => 'text',
  1536. 'is_hidden' => 0
  1537. ];
  1538. $res[] = [
  1539. 'field' => 'is_lock',
  1540. 'name' => '锁定状态',
  1541. 'form_type' => 'text',
  1542. 'is_hidden' => 0
  1543. ];
  1544. }
  1545. return array_column($res, null, 'field');
  1546. }
  1547. /**
  1548. * [getIndexField 列表展示字段]
  1549. * @param types 分类
  1550. * @param is_data 1 取数据时
  1551. * @author Michael_xu
  1552. */
  1553. public function getIndexField($types, $user_id, $is_data = '')
  1554. {
  1555. $apiCommon = new ApiCommon();
  1556. $userFieldModel = new \app\admin\model\UserField();
  1557. $userFieldData = $userFieldModel->getConfig($types, $user_id);
  1558. $userFieldData = $userFieldData ? json_decode($userFieldData, true) : [];
  1559. $othor_un_field = array_column($this->orther_field_list[$types], 'field');
  1560. $unField = array_merge(['pool_day', 'business-check', 'call'], $othor_un_field);
  1561. $user_id = !empty($user_id) ? $user_id : $apiCommon->userInfo['id'];
  1562. $grantData = getFieldGrantData($user_id);
  1563. $userLevel = isSuperAdministrators($user_id);
  1564. $where = [];
  1565. if ($userFieldData) {
  1566. $dataList = [];
  1567. foreach ($userFieldData as $k => $v) {
  1568. if (empty($v['is_hide']) && !in_array($k, $unField)) {
  1569. $dataList[] = $k;
  1570. }
  1571. }
  1572. } else {
  1573. $where['types'] = ['in', ['', $types]];
  1574. $dataList = $this->where($where)->column('field');
  1575. }
  1576. $newList = $dataList;
  1577. if ($is_data == 1) {
  1578. switch ($types) {
  1579. case 'crm_leads' :
  1580. $sysField = ['leads_id', 'create_time', 'update_time', 'create_user_id', 'owner_user_id', 'last_time', 'last_record'];
  1581. break;
  1582. case 'crm_business' :
  1583. $newList = [];
  1584. foreach ($dataList as $v) {
  1585. $newList[] = 'business.' . $v;
  1586. }
  1587. $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'];
  1588. break;
  1589. case 'crm_customer' :
  1590. $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'];
  1591. break;
  1592. case 'crm_customer_pool' :
  1593. $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'];
  1594. break;
  1595. case 'crm_contacts' :
  1596. $newList = [];
  1597. foreach ($dataList as $v) {
  1598. $newList[] = 'contacts.' . $v;
  1599. }
  1600. $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'];
  1601. break;
  1602. case 'crm_contract' :
  1603. $newList = [];
  1604. foreach ($dataList as $v) {
  1605. $newList[] = 'contract.' . $v;
  1606. }
  1607. $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'];
  1608. break;
  1609. case 'crm_receivables' :
  1610. $newList = [];
  1611. foreach ($dataList as $v) {
  1612. $newList[] = 'receivables.' . $v;
  1613. }
  1614. $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'];
  1615. break;
  1616. case 'crm_product' :
  1617. $newList = [];
  1618. foreach ($dataList as $v) {
  1619. $newList[] = 'product.' . $v;
  1620. }
  1621. $sysField = ['product.product_id', 'product.category_id', 'product.create_time', 'product.update_time', 'product.create_user_id', 'product.owner_user_id'];
  1622. break;
  1623. case 'crm_visit' :
  1624. $newList = [];
  1625. foreach ($dataList as $v) {
  1626. $newList[] = 'visit.' . $v;
  1627. }
  1628. $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'];
  1629. break;
  1630. case 'crm_invoice' :
  1631. $newList = [];
  1632. foreach ($dataList as $k => $v) {
  1633. $newList[] = 'invoice.' . $v;
  1634. if ($v == 'contract_money') {
  1635. unset($newList[$k]);
  1636. }
  1637. }
  1638. $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'];
  1639. break;
  1640. }
  1641. $listArr = $sysField ? array_unique(array_merge($newList, $sysField)) : $dataList;
  1642. } else {
  1643. $listArr = $dataList;
  1644. }
  1645. $typesArray = explode('_', $types);
  1646. $type = array_pop($typesArray);
  1647. if (isset($this->orther_field_list[$types])) {
  1648. foreach ($this->orther_field_list[$types] as $val) {
  1649. if (in_array($type . '.' . $val['field'], $listArr) && !in_array($types, ['crm_contract', 'crm_business', 'crm_receivables'])) {
  1650. unset($listArr[array_search($type . '.' . $val['field'], $listArr)]);
  1651. }
  1652. }
  1653. }
  1654. # 处理字段授权
  1655. foreach ($listArr as $k => $v) {
  1656. if (!$userLevel && !empty($grantData[$types])) {
  1657. $status = getFieldGrantStatus($v, $grantData[$types]);
  1658. # 查看权限
  1659. if ($status['read'] == 0) unset($listArr[(int)$k]);
  1660. }
  1661. }
  1662. return $listArr ?: [];
  1663. }
  1664. /**
  1665. * [checkFieldPer 判断权限]
  1666. * @param types 分类
  1667. * @author Michael_xu
  1668. */
  1669. public function checkFieldPer($module, $controller, $action, $user_id = '')
  1670. {
  1671. $userModel = new \app\admin\model\User();
  1672. if (!checkPerByAction($module, $controller, $action)) return false;
  1673. if ($user_id && !in_array($user_id, $userModel->getUserByPer($module, $controller, $action))) return false;
  1674. return true;
  1675. }
  1676. /**
  1677. * [getField 获取字段属性]
  1678. * @param types 分类
  1679. * @author Michael_xu
  1680. */
  1681. public function getField($param)
  1682. {
  1683. $types = $param['types'];
  1684. $unFormType = $param['unFormType'];
  1685. if (!in_array($types, $this->types_arr)) {
  1686. return resultArray(['error' => '参数错误']);
  1687. }
  1688. $field_arr = [];
  1689. //模拟自定义字段返回
  1690. switch ($types) {
  1691. case 'admin_user' :
  1692. $field_arr = \app\hrm\model\Userdet::getField();
  1693. break;
  1694. default :
  1695. $data = [];
  1696. $data['types'] = $types;
  1697. $data['user_id'] = $param['user_id'];
  1698. if ($unFormType) $data['form_type'] = array('not in', $unFormType);
  1699. $field_arr = $this->fieldSearch($data);
  1700. }
  1701. if ($types == 'crm_visit') {
  1702. foreach ($field_arr as $key => $value) {
  1703. if ($value['name'] == '负责人') unset($field_arr[(int)$key]);
  1704. }
  1705. }
  1706. if ($types == 'crm_invoice') {
  1707. $field_arr = array_merge($field_arr, $this->getInvoiceSearch());
  1708. }
  1709. return $field_arr;
  1710. }
  1711. /**
  1712. * 自定义字段验重
  1713. *
  1714. * @param $field string 字段名称
  1715. * @param $val array|string 字段值
  1716. * @param $id int 数据id
  1717. * @param $types string 模块类型
  1718. * @return bool
  1719. */
  1720. public function getValidate($field, $val, $id, $types)
  1721. {
  1722. $val = is_array($val) ? $val : trim($val);
  1723. if (!$val) {
  1724. $this->error = '验证内容不能为空';
  1725. return false;
  1726. }
  1727. if (!$field) {
  1728. $this->error = '数据验证错误,请联系管理员!';
  1729. return false;
  1730. }
  1731. if (!in_array($types, $this->types_arr)) {
  1732. $this->error = '参数错误!';
  1733. return false;
  1734. }
  1735. $field_info = db('admin_field')->where(['types' => $types, 'field' => $field])->find();
  1736. if (!$field_info) {
  1737. $this->error = '数据验证错误,请联系管理员!';
  1738. return false;
  1739. }
  1740. // 人员、部门、文件、手写签名、描述文字、多选、明细表格跳过验证唯一性
  1741. if (in_array($field_info['form_type'], ['user', 'structure', 'file', 'handwriting_sign', 'desc_text', 'checkbox', 'detail_table'])) return true;
  1742. // 前端传来的定位数据有问题,在提交数据时做验证
  1743. if ($field_info['form_type'] == 'location') return true;
  1744. // 处理日期区间类型数据
  1745. if ($field_info['form_type'] == 'date_interval') {
  1746. $val = implode('_', $val);
  1747. }
  1748. // 处理地址类型数据
  1749. if ($field_info['form_type'] == 'position') {
  1750. $positionNames = array_column($val, 'name');
  1751. $val = implode(',', $positionNames);
  1752. }
  1753. $dataModel = '';
  1754. switch ($types) {
  1755. case 'crm_leads' :
  1756. $dataModel = new \app\crm\model\Leads();
  1757. break;
  1758. case 'crm_customer' :
  1759. $dataModel = new \app\crm\model\Customer();
  1760. break;
  1761. case 'crm_contacts' :
  1762. $dataModel = new \app\crm\model\Contacts();
  1763. break;
  1764. case 'crm_business' :
  1765. $dataModel = new \app\crm\model\Business();
  1766. break;
  1767. case 'crm_product' :
  1768. $dataModel = new \app\crm\model\Product();
  1769. break;
  1770. case 'crm_contract' :
  1771. $dataModel = new \app\crm\model\Contract();
  1772. break;
  1773. case 'crm_receivables' :
  1774. $dataModel = new \app\crm\model\Receivables();
  1775. break;
  1776. case 'crm_invoice' :
  1777. $dataModel = new \app\crm\model\Invoice();
  1778. break;
  1779. case 'oa_examine' :
  1780. $dataModel = db('oa_examine');
  1781. break;
  1782. }
  1783. $where = [];
  1784. $where[$field] = ['eq', $val];
  1785. // 编辑时的验重
  1786. if ($id) $where[$dataModel->getpk()] = ['neq', $id];
  1787. if ($types == 'crm_product') {
  1788. $where['delete_user_id'] = 0;
  1789. }
  1790. if ($res = $dataModel->where($where)->find()) {
  1791. $this->error = '该数据已存在,请修改后提交!';
  1792. return false;
  1793. }
  1794. return true;
  1795. }
  1796. /**
  1797. * [getFieldByFormType 根据字段类型获取字段数组]
  1798. * @param types 分类
  1799. * @author Michael_xu
  1800. */
  1801. public function getFieldByFormType($types, $form_type)
  1802. {
  1803. $fieldArr = $this->where(['types' => $types, 'form_type' => $form_type])->column('field');
  1804. return $fieldArr ?: [];
  1805. }
  1806. /**
  1807. * [getFormValueByField 格式化表格字段类型值]
  1808. * @param $field 字段名
  1809. * @param $value 字段值
  1810. * @author Michael_xu
  1811. */
  1812. public function getFormValueByField($field, $value)
  1813. {
  1814. $formValue = db('admin_field')->where(['field' => $field])->value('form_value');
  1815. $formValue = sort_select($formValue, 'order_id', 1); //二维数组排序
  1816. $field = [];
  1817. foreach ($formValue as $k => $v) {
  1818. $field[] = $v['field'];
  1819. }
  1820. $data = [];
  1821. foreach ($value as $k => $v) {
  1822. foreach ($field as $key => $val) {
  1823. $data[$k][$val] = $v['value'];
  1824. }
  1825. }
  1826. return $data;
  1827. }
  1828. /**
  1829. * 根据form_type处理数据
  1830. * @author lee
  1831. */
  1832. public function getValueByFormtype($val, $form_type)
  1833. {
  1834. $userModel = new \app\admin\model\User();
  1835. $structureModel = new \app\admin\model\Structure();
  1836. switch ($form_type) {
  1837. // case 'datetime' :
  1838. // $val = $val > 0 ? date('Y-m-d H:i:s', $val) : '';
  1839. // break;
  1840. case 'user' :
  1841. $val = count($userModel->getUserNameByArr($val)) > 1 ? ArrayToString($userModel->getUserNameByArr($val)) : implode(',', $userModel->getUserNameByArr(stringToArray($val)));
  1842. break;
  1843. case 'userStr' :
  1844. $val = explode(',', $val);
  1845. $val = count($userModel->getUserNameByArr($val)) > 1 ? ArrayToString($userModel->getUserNameByArr($val)) : implode(',', $userModel->getUserNameByArr($val));
  1846. break;
  1847. case 'structure' :
  1848. $val = implode(',', $structureModel->getStructureNameByArr(stringToArray($val)));
  1849. break;
  1850. case 'customer' :
  1851. $val = db('crm_customer')->where(['customer_id' => $val])->value('name');
  1852. break;
  1853. case 'contract' :
  1854. $val = db('crm_contract')->where(['contract_id' => $val])->value('num');
  1855. break;
  1856. case 'business' :
  1857. $val = db('crm_business')->where(['business_id' => $val])->value('name');
  1858. break;
  1859. case 'category' :
  1860. $val = db('crm_product_category')->where(['category_id' => $val])->value('name');
  1861. break;
  1862. case 'business_type' :
  1863. $val = db('crm_business_type')->where(['type_id' => $val])->value('name');
  1864. break;
  1865. case 'business_status' :
  1866. $val = db('crm_business_status')->where(['status_id' => $val])->value('name');
  1867. break;
  1868. }
  1869. return $val;
  1870. }
  1871. /**
  1872. * [getIndexFieldList 列表展示字段]
  1873. * @param types 分类
  1874. * @author Michael_xu
  1875. */
  1876. public function getIndexFieldList($types, $user_id)
  1877. {
  1878. $fieldArr = $this->getIndexField($types, $user_id);
  1879. $types = $types == 'crm_customer_pool' ? 'crm_customer' : $types;
  1880. $fieldList = db('admin_field')->where(['field' => array('in', $fieldArr)])->where('types', ['eq', $types], ['eq', ''], 'or')->order('order_id asc')->select();
  1881. return $fieldList ?: [];
  1882. }
  1883. /**
  1884. * [getArrayField 数组类型字段]
  1885. * @param types 分类
  1886. * @author Michael_xu
  1887. */
  1888. public function getArrayField($types)
  1889. {
  1890. $arrayFormType = ['structure', 'user', 'checkbox', 'file'];
  1891. $arrFieldAtt = db('admin_field')->where(['types' => $types, 'form_type' => ['in', $arrayFormType]])->column('field');
  1892. return $arrFieldAtt ?: [];
  1893. }
  1894. /**
  1895. * 字段对照关系处理
  1896. * @param $types 分类
  1897. * @param $data 数据
  1898. * @return
  1899. * @author Michael_xu
  1900. */
  1901. public function getRelevantData($types, $data = [])
  1902. {
  1903. $types_arr = ['crm_leads'];
  1904. if (!in_array($types, $types_arr)) {
  1905. $this->error = '参数错误';
  1906. return false;
  1907. }
  1908. if (!$data) return $data;
  1909. $list = $this->where(['types' => $types, 'relevant' => ['neq', '']])->field('field,relevant')->select();
  1910. if (!$list) return $data;
  1911. $newData = $data;
  1912. foreach ($list as $k => $v) {
  1913. $newData[$v['relevant']] = $data[$v['field']];
  1914. }
  1915. return $newData ?: [];
  1916. }
  1917. /**
  1918. * 字段排序
  1919. * @param types 自定义字段分类
  1920. * @param prefix 自定义字段前缀
  1921. * @param field 自定义字段
  1922. * @param order 排序规则
  1923. * @return
  1924. * @author Michael_xu
  1925. */
  1926. public function getOrderByFormtype($types, $prefix, $field, $order_type)
  1927. {
  1928. $form_type = $this->where(['types' => $types, 'field' => $field])->value('form_type');
  1929. // if (!$form_type) {
  1930. // $this->error = '参数错误';
  1931. // return false;
  1932. // }
  1933. $temp_field = $field;
  1934. $field = $prefix ? $prefix . '.' . $field : $field;
  1935. switch ($form_type) {
  1936. case 'textarea' :
  1937. case 'radio' :
  1938. case 'select' :
  1939. case 'checkbox' :
  1940. case 'address' :
  1941. $order = 'convert(' . $field . ' using gbk) ' . trim($order_type);
  1942. break;
  1943. default :
  1944. $order = $field . ' ' . $order_type;
  1945. break;
  1946. }
  1947. if (isset($this->orther_field_list[$types])) {
  1948. foreach ($this->orther_field_list[$types] as $val) {
  1949. // $res[] = $val;
  1950. // $order
  1951. $temp = trim($prefix . '.' . $val['field'], '.');
  1952. if ($temp == $field) {
  1953. $order = str_replace($temp, $val['field'], $order);
  1954. }
  1955. }
  1956. }
  1957. return $order;
  1958. }
  1959. /**
  1960. * 处理字段别名、权限
  1961. *
  1962. * @param int $userId 用户ID
  1963. * @param string $types 模块类型
  1964. * @param string $action 行为
  1965. * @param array $data 字段数据
  1966. * @return array
  1967. * @since 2021-04-06
  1968. * @author fanqi
  1969. */
  1970. public function resetField($userId, $types, $action, $data)
  1971. {
  1972. $grantData = getFieldGrantData($userId);
  1973. $userLevel = isSuperAdministrators($userId);
  1974. foreach ($data as $key => $value) {
  1975. # 处理字段授权
  1976. if (!$userLevel && !empty($grantData[$types])) {
  1977. $status = getFieldGrantStatus($value['field'], $grantData[$types]);
  1978. # 查看权限
  1979. if ($status['read'] == 0) {
  1980. unset($data[(int)$key]);
  1981. continue;
  1982. }
  1983. }
  1984. switch ($value['field']) {
  1985. case 'create_user_id' :
  1986. $data[$key]['fieldName'] = 'create_user_name';
  1987. break;
  1988. case 'owner_user_id' :
  1989. $data[$key]['fieldName'] = 'owner_user_name';
  1990. break;
  1991. case 'customer_id' :
  1992. $data[$key]['fieldName'] = 'customer_name';
  1993. break;
  1994. case 'type_id' :
  1995. $data[$key]['fieldName'] = 'type_id_info';
  1996. break;
  1997. case 'status_id' :
  1998. $data[$key]['fieldName'] = 'status_id_info';
  1999. break;
  2000. case 'business_id' :
  2001. $data[$key]['fieldName'] = 'business_name';
  2002. break;
  2003. case 'contacts_id' :
  2004. $data[$key]['fieldName'] = 'contacts_name';
  2005. break;
  2006. case 'order_user_id' :
  2007. $data[$key]['fieldName'] = 'order_user_name';
  2008. break;
  2009. case 'contract_id' :
  2010. $data[$key]['fieldName'] = 'contract_num';
  2011. break;
  2012. case 'plan_id' :
  2013. $data[$key]['fieldName'] = 'plan_id_info';
  2014. break;
  2015. case 'category_id' :
  2016. $data[$key]['fieldName'] = 'category_name';
  2017. break;
  2018. default :
  2019. $data[$key]['fieldName'] = $value['field'];
  2020. }
  2021. # 详情中不显示产品类别、回款期数
  2022. if ($action == 'read' && in_array($value['field'], ['category_id'])) {
  2023. unset($data[(int)$key]);
  2024. }
  2025. }
  2026. return $data;
  2027. }
  2028. /**
  2029. * 获取公海自定义字段数据
  2030. *
  2031. * @param $poolId
  2032. * @param $dataInfo
  2033. * @return bool|\PDOStatement|string|\think\Collection
  2034. */
  2035. public function getPoolFieldData($poolId, $dataInfo)
  2036. {
  2037. $poolFields = db('crm_customer_pool_field_setting')->field(['field_name AS field', 'form_type', 'name'])->where(['pool_id' => $poolId, 'is_hidden' => 0])->select();
  2038. foreach ($poolFields as $key => $value) {
  2039. # 字段值
  2040. $poolFields[$key]['value'] = !empty($dataInfo[$value['field']]) ? $dataInfo[$value['field']] : '';
  2041. # 处理别名
  2042. switch ($value['field']) {
  2043. case 'create_user_id' :
  2044. $poolFields[$key]['fieldName'] = 'create_user_name';
  2045. $poolFields[$key]['value'] = !empty($dataInfo['create_user_id_info']) ? [$dataInfo['create_user_id_info']] : '';
  2046. break;
  2047. case 'before_owner_user_id' :
  2048. $poolFields[$key]['fieldName'] = 'before_owner_user_name';
  2049. $poolFields[$key]['value'] = !empty($dataInfo['before_owner_user_id_info']) ? [$dataInfo['before_owner_user_id_info']] : '';
  2050. break;
  2051. default :
  2052. $poolFields[$key]['fieldName'] = $value['field'];
  2053. }
  2054. if (in_array($value['form_type'], ['user', 'structure']) && !in_array($value['field'], ['create_user_id', 'owner_user_id', 'before_owner_user_id'])) {
  2055. $poolFields[$key]['fieldName'] = $value['field_name'] . '_name';
  2056. }
  2057. # 系统字段
  2058. if (in_array($value['field'], ['last_record', 'create_user_id', 'create_time', 'update_time', 'last_time', 'before_owner_user_id'])) {
  2059. $poolFields[$key]['system'] = 1;
  2060. } else {
  2061. $poolFields[$key]['system'] = 0;
  2062. }
  2063. }
  2064. return $poolFields;
  2065. }
  2066. /**
  2067. * 获取发票高级搜索字段
  2068. *
  2069. * @return array[]
  2070. */
  2071. private function getInvoiceSearch()
  2072. {
  2073. return [
  2074. // ['field' => 'invoice_number', 'form_type' => 'text', 'setting' => [], 'name' => '发票号码'],
  2075. ['field' => 'real_invoice_date', 'form_type' => 'datetime', 'setting' => [], 'name' => '实际开票日期'],
  2076. ['field' => 'logistics_number', 'form_type' => 'text', 'setting' => [], 'name' => '物流单号'],
  2077. ['field' => 'invoice_status', 'form_type' => 'select', 'setting' => ['未开票', '已开票'], 'name' => '开票状态'],
  2078. ['field' => 'check_status', 'form_type' => 'select', 'setting' => ['待审核', '审核中', '审核通过', '审核未通过', '撤回'], 'name' => '审核状态'],
  2079. ];
  2080. }
  2081. /**
  2082. * 获取产品父类层级(不包含自身)
  2083. *
  2084. * @param $productId
  2085. * @param array $parentIds
  2086. * @return array|mixed
  2087. * @throws \think\db\exception\DataNotFoundException
  2088. * @throws \think\db\exception\ModelNotFoundException
  2089. * @throws \think\exception\DbException
  2090. */
  2091. private function getProductParentIds($productId, &$parentIds = [])
  2092. {
  2093. $category = db('crm_product_category')->select();
  2094. foreach ($category as $key => $value) {
  2095. if ($value['category_id'] == $productId) {
  2096. if (!empty($value['pid'])) {
  2097. $parentIds[] = $value['pid'];
  2098. $this->getProductParentIds($value['pid'], $parentIds);
  2099. }
  2100. }
  2101. }
  2102. return $parentIds;
  2103. }
  2104. /**
  2105. * 设置明细表格数据
  2106. *
  2107. * @param string $types 模块类型:crm_leads crm_customer ...
  2108. * @param string $field 字段名称
  2109. * @param array $data 明细表单中的字段数据
  2110. * @return bool
  2111. * @since 2021-04-27
  2112. * @author fanqi
  2113. */
  2114. private function setDetailTableData($types, $field, $data)
  2115. {
  2116. $id = db('admin_field_extend')->where(['field' => $field, 'types' => $types])->value('id');
  2117. foreach ($data as $k1 => $v1) {
  2118. if (empty($v1['field'])) $data[$k1]['field'] = $this->createField($types, $types == 'oa_examine' ? 'oa_' : 'crm_');
  2119. $data[$k1]['default_value'] = !empty($v1['default_value']) ? $v1['default_value'] : '';
  2120. }
  2121. if (!empty($id)) {
  2122. return db('admin_field_extend')->where('id', $id)->update(['content' => json_encode($data, JSON_NUMERIC_CHECK)]);
  2123. }
  2124. if (empty($id)) {
  2125. return db('admin_field_extend')->insert(['types' => $types, 'field' => $field, 'content' => json_encode($data, JSON_NUMERIC_CHECK), 'create_time' => time()]);
  2126. }
  2127. return false;
  2128. }
  2129. }