Suppliercontact.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\supplier\controller;
  15. use app\base\BaseController;
  16. use app\supplier\model\SupplierContact;
  17. use app\supplier\validate\SupplierContactCheck;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\facade\View;
  21. class Contact extends BaseController
  22. {
  23. public function index()
  24. {
  25. if (request()->isAjax()) {
  26. $param = get_params();
  27. $where = array();
  28. if (!empty($param['keywords'])) {
  29. $where[] = ['a.id|a.name|a.mobile|s.title', 'like', '%' . $param['keywords'] . '%'];
  30. }
  31. $where[] = ['a.delete_time', '=', 0];
  32. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  33. $content = SupplierContact::where($where)
  34. ->field('a.*,s.title as supplier')
  35. ->alias('a')
  36. ->join('Supplier s', 'a.sid = s.id')
  37. ->order('a.create_time desc')
  38. ->paginate($rows, false, ['query' => $param])
  39. ->each(function ($item, $key) {
  40. $item->create_time = date('Y-m-d H:i:s', (int) $item->create_time);
  41. });
  42. return table_assign(0, '', $content);
  43. } else {
  44. return view();
  45. }
  46. }
  47. //添加
  48. public function add()
  49. {
  50. $param = get_params();
  51. if (request()->isAjax()) {
  52. if (!empty($param['id']) && $param['id'] > 0) {
  53. try {
  54. validate(SupplierContactCheck::class)->scene('edit')->check($param);
  55. } catch (ValidateException $e) {
  56. // 验证失败 输出错误信息
  57. return to_assign(1, $e->getError());
  58. }
  59. $param['update_time'] = time();
  60. $res = Db::name('SupplierContact')->strict(false)->field(true)->update($param);
  61. if ($res) {
  62. add_log('edit', $param['id'], $param);
  63. }
  64. return to_assign();
  65. } else {
  66. try {
  67. validate(SupplierContactCheck::class)->scene('add')->check($param);
  68. } catch (ValidateException $e) {
  69. // 验证失败 输出错误信息
  70. return to_assign(1, $e->getError());
  71. }
  72. $count= Db::name('SupplierContact')->where(['sid' => $param['sid'],'delete_time' => 0])->count();
  73. if($count == 0){
  74. $param['is_default'] = 1;
  75. }
  76. $param['admin_id'] = $this->uid;
  77. $param['create_time'] = time();
  78. $insertId = Db::name('SupplierContact')->strict(false)->field(true)->insertGetId($param);
  79. if ($insertId) {
  80. add_log('add', $insertId, $param);
  81. }
  82. return to_assign();
  83. }
  84. } else {
  85. $supplier_id = isset($param['sid']) ? $param['sid'] : 0;
  86. $id = isset($param['id']) ? $param['id'] : 0;
  87. if ($id > 0) {
  88. View::assign('detail', (new SupplierContact())->detail($id));
  89. return view('edit');
  90. }
  91. $supplier_name = Db::name('Supplier')->where('id',$supplier_id)->value('title');
  92. View::assign('supplier_id', $supplier_id);
  93. View::assign('supplier_name', $supplier_name);
  94. return view();
  95. }
  96. }
  97. //设置
  98. public function del()
  99. {
  100. if (request()->isDelete()) {
  101. $param = get_params();
  102. $contact = Db::name('SupplierContact')->where(['id' => $param['id']])->find();
  103. if($contact['is_default'] == 1){
  104. return to_assign(1, '供应商的首要联系人不能删除');
  105. }
  106. if($contact['admin_id'] != $this->uid){
  107. return to_assign(1, '你不是该联系人的创建人,无权限删除');
  108. }
  109. $param['delete_time'] = time();
  110. $res = SupplierContact::strict(false)->field(true)->update($param);
  111. if ($res) {
  112. add_log('edit', $param['id'], $param);
  113. return to_assign();
  114. } else {
  115. return to_assign(1, '操作失败');
  116. }
  117. } else {
  118. return to_assign(1, '参数错误');
  119. }
  120. }
  121. }