Leads.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. /**
  8. *线索
  9. */
  10. class Leads Extends Model {
  11. use SoftDelete;
  12. // 表名,不含前缀
  13. protected $name = 'qingdongams_leads';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = 'deletetime';
  20. public function getCreatetimeAttr($value) {
  21. return date('Y-m-d H:i:s', $value);
  22. }
  23. public function getUpdatetimeAttr($value) {
  24. return date('Y-m-d H:i:s', $value);
  25. }
  26. //创建人
  27. public function createStaff() {
  28. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name');
  29. }
  30. //负责人
  31. public function ownerStaff() {
  32. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
  33. }
  34. //转派人
  35. public function tranferStaff() {
  36. return $this->hasOne(Staff::class, 'id', 'tranfer_stff_id')->field('id,name,img');
  37. }
  38. //获取线索相关信息
  39. public function leadsOther() {
  40. return $this->belongsTo(LeadsOther::class,'id','id');
  41. }
  42. //创建线索
  43. public static function createLeads($params=array(),$pool=null) {
  44. $other = [];
  45. foreach ($params as $name => $val) {
  46. if (strstr( $name,'other_') !== false) {
  47. if(is_array($val)){
  48. $other[$name] = implode(',',$val);
  49. }else{
  50. $other[$name] = $val;
  51. }
  52. unset($params[$name]);
  53. }else{
  54. if(empty($params[$name])){
  55. $params[$name]=NULL;
  56. }
  57. }
  58. }
  59. $staff = Staff::info();
  60. if(!empty($staff)){
  61. $params['create_staff_id'] = $staff->id;
  62. $params['owner_staff_id'] = $staff->id;
  63. }
  64. //后台添加线索
  65. if(isset($params['staff_id']) && $params['staff_id']){
  66. $params['create_staff_id'] = $params['staff_id'];
  67. $params['owner_staff_id'] = $params['staff_id'];
  68. }
  69. //线索池
  70. if($pool){
  71. $params['owner_staff_id'] = '';
  72. }
  73. $Model = new self;
  74. $result = $Model->allowField(true)->save($params);
  75. $lastId = $Model->getLastInsID();
  76. if (false === $result) {
  77. // 验证失败 输出错误信息
  78. throw new Exception($Model->getError());
  79. }
  80. //其他信息
  81. $otherModel = new LeadsOther();
  82. if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
  83. // 验证失败 输出错误信息
  84. throw new Exception($otherModel->getError());
  85. }
  86. return $lastId;
  87. }
  88. /**
  89. *修改线索
  90. */
  91. public static function updateLeads($params) {
  92. $other = [];
  93. foreach ($params as $name => $val) {
  94. if (strstr($name,'other_') !== false) {
  95. if(is_array($val)){
  96. $other[$name] = implode(',',$val);
  97. }else{
  98. $other[$name] = $val;
  99. }
  100. unset($params[$name]);
  101. }else{
  102. if(empty($params[$name])){
  103. $params[$name]=NULL;
  104. }
  105. }
  106. }
  107. $customer = new self;
  108. // 调用当前模型对应的User验证器类进行数据验证
  109. $result = $customer->save($params, ['id' => $params['id']]);
  110. if (false === $result) {
  111. // 验证失败 输出错误信息
  112. throw new Exception($customer->getError());
  113. }
  114. //其他信息
  115. $otherModel = new LeadsOther();
  116. if ($otherModel->save([ 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)],['id' => $params['id']]) === false) {
  117. // 验证失败 输出错误信息
  118. throw new Exception($otherModel->getError());
  119. }
  120. return true;
  121. }
  122. //客户
  123. public function customer() {
  124. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow');
  125. }
  126. //导入线索
  127. public static function importleads($data) {
  128. $addleads = [];
  129. $addOther = [];
  130. $addLog=[];
  131. foreach ($data as $params) {
  132. //自定义字段
  133. $other = [];
  134. foreach ($params as $name => $val) {
  135. if (strstr($name, 'other_') !== false) {
  136. if(is_array($val)){
  137. $other[$name] = implode(',',$val);
  138. }else{
  139. $other[$name] = $val;
  140. }
  141. unset($params[$name]);
  142. }else{
  143. if(empty($params[$name])){
  144. $params[$name]=NULL;
  145. }
  146. }
  147. }
  148. $other['id'] = $params['id'];
  149. $params['next_time'] = date('Y-m-d H:i:s');
  150. $params['createtime'] = time();
  151. $params['updatetime'] = time();
  152. $addOther[] = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];
  153. $addLog[] = [
  154. 'content' => '导入线索',
  155. 'operation_type' => 4,
  156. 'operation_id' => isset($params['owner_staff_id'])?$params['owner_staff_id']:$params['create_staff_id'],
  157. 'relation_type' => OperationLog::LEADS_TYPE,
  158. 'relation_id' => $params['id'],
  159. 'createtime' => time()
  160. ];
  161. $addleads[] = $params;
  162. }
  163. $leads = new self;
  164. // 调用当前模型对应的User验证器类进行数据验证
  165. $result = $leads->allowField(true)->insertAll($addleads);
  166. $otherModel = new LeadsOther();
  167. if($other){
  168. $otherModel->allowField(true)->insertAll($addOther);
  169. }
  170. $logModel = new OperationLog();
  171. $logModel->allowField(true)->insertAll($addLog);
  172. return true;
  173. }
  174. /**
  175. * 转移线索
  176. */
  177. public static function transfer($id, $staff_id) {
  178. Db::startTrans();
  179. try {
  180. $staff = Staff::info();
  181. //批量转移
  182. if(is_array($id)){
  183. foreach($id as $k=>$v){
  184. if (self::where(['id' => $v])->update([
  185. 'owner_staff_id' => $staff_id,
  186. 'tranfer_stff_id' => $staff->id,
  187. 'receive_time'=>time(),
  188. 'updatetime' => time()
  189. ]) == false) {
  190. throw new Exception('修改失败');
  191. }
  192. $staff = Staff::get($staff_id);
  193. OperationLog::createLog(OperationLog::LEADS_TYPE, $v,
  194. '将线索转移给:' . $staff['name']);
  195. }
  196. }else{
  197. if (self::where(['id' => $id])->update([
  198. 'owner_staff_id' => $staff_id,
  199. 'tranfer_stff_id' => $staff->id,
  200. 'receive_time'=>time(),
  201. 'updatetime' => time()
  202. ]) == false) {
  203. throw new Exception('修改失败');
  204. }
  205. $staff = Staff::get($staff_id);
  206. OperationLog::createLog(OperationLog::LEADS_TYPE, $id,
  207. '将线索转移给:' . $staff['name']);
  208. }
  209. Db::commit();
  210. } catch (Exception $e) {
  211. Db::rollback();
  212. throw new Exception($e->getMessage());
  213. }
  214. return true;
  215. }
  216. /**
  217. *移入公海
  218. */
  219. public static function movePool($id) {
  220. if (self::where(['id' => $id])->update(['owner_staff_id' => 0,'updatetime'=>time()]) == false) {
  221. throw new Exception('修改失败');
  222. }
  223. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $id, '将线索放入线索池');
  224. return true;
  225. }
  226. }