Company.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller\cpuser;
  4. use app\admin\BaseController;
  5. use app\admin\model\Department as DepartmentModel;
  6. use app\admin\validate\FinancialValidate;
  7. use think\exception\ValidateException;
  8. use think\facade\View;
  9. use think\facade\Db;
  10. class Company extends BaseController
  11. {
  12. /**
  13. * 构造函数
  14. */
  15. public function __construct()
  16. {
  17. $this->model = new DepartmentModel();
  18. $this->uid = get_login_admin('id');
  19. }
  20. /**
  21. * 数据列表
  22. */
  23. public function datalist()
  24. {
  25. if (request()->isAjax()) {
  26. $param = get_params();
  27. $where = [];
  28. $where[] = ["delete_time","=",0];
  29. $where = [
  30. ["delete_time","=",0],
  31. ["pid","=",0],
  32. ["type","=",2]
  33. ];
  34. $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
  35. $order = empty($param['order']) ? 'id desc' : $param['order'];
  36. $list = $this->model->where($where)->field('id,title,leader_id,phone,landline,address,company_type,authorization_module,status,create_time')
  37. ->order($order)->paginate($rows, false, ['query' => $param])
  38. ->each(function ($item){
  39. $data = Db::name("admin")->where("id",$item->leader_id)->find();
  40. if(!empty($data["nickname"])){
  41. $item->leader =$data["nickname"];
  42. }
  43. if(!empty($data["username"])){
  44. $item->username = $data["username"];
  45. }
  46. if(!empty($data["mobile"])){
  47. $item->phone = $data["mobile"];
  48. }
  49. });
  50. // halt($list);
  51. return table_assign(0, '', $list);
  52. }
  53. else{
  54. return view();
  55. }
  56. }
  57. /**
  58. * 添加
  59. */
  60. public function add()
  61. {
  62. if (request()->isAjax()) {
  63. $param = get_params();
  64. // 检验完整性
  65. // try {
  66. // validate(FinancialValidate::class)->check($param);
  67. // } catch (ValidateException $e) {
  68. // // 验证失败 输出错误信息
  69. // return to_assign(1, $e->getError());
  70. // }
  71. if (isset($param["start_time"])) {
  72. $param["start_time"] = $param["start_time"] ? strtotime($param["start_time"]) : 0;
  73. }
  74. if (isset($param["end_time"])) {
  75. $param["end_time"] = $param["end_time"] ? strtotime($param["end_time"]) : 0;
  76. }
  77. $param = array_merge($param,['type'=>2]);
  78. try {
  79. $did = Db::name('Department')->strict(false)->field(true)->insertGetId($param);
  80. $pid = $did;
  81. while($pid != 0){
  82. $value = Db::name('department')->where('id', $pid)->column('id,pid,title')[0];
  83. $unit_name = $value['id'];
  84. $pid = $value['pid'];
  85. $title = $value['title'];
  86. }
  87. $unit_name = [
  88. 'unit_name' => $unit_name,
  89. ];
  90. Db::name('Department')->where('id', $did)->update($unit_name);
  91. add_log('add', $did, $param);
  92. } catch(\Exception $e) {
  93. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  94. }
  95. return to_assign(0,'操作成功');
  96. // halt($param);
  97. }else{
  98. return view();
  99. }
  100. }
  101. /**
  102. * 编辑
  103. */
  104. public function edit()
  105. {
  106. $param = get_params();
  107. // halt($param);
  108. if (request()->isAjax()) {
  109. // 检验完整性
  110. // try {
  111. // validate(FinancialValidate::class)->check($param);
  112. // } catch (ValidateException $e) {
  113. // // 验证失败 输出错误信息
  114. // return to_assign(1, $e->getError());
  115. // }
  116. if (isset($param["start_time"])) {
  117. $param["start_time"] = $param["start_time"] ? strtotime($param["start_time"]) : 0;
  118. }
  119. if (isset($param["end_time"])) {
  120. $param["end_time"] = $param["end_time"] ? strtotime($param["end_time"]) : 0;
  121. }
  122. try {
  123. $param['update_time'] = time();
  124. $this->model->where('id', $param['id'])->strict(false)->field(true)->update($param);
  125. add_log('edit', $param['id'], $param);
  126. } catch(\Exception $e) {
  127. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  128. }
  129. return to_assign();
  130. }else{
  131. $id = isset($param['id']) ? $param['id'] : 0;
  132. $detail = $this->model->where('id', $id)->find();
  133. $users = Db::name('Admin')->where(['did' => $id, 'status' => 1])->select();
  134. if (!empty($detail)) {
  135. View::assign('detail', $detail);
  136. View::assign('users', $users);
  137. return view();
  138. }
  139. else{
  140. throw new \think\exception\HttpException(404, '找不到页面');
  141. }
  142. }
  143. }
  144. /**
  145. * 查看信息
  146. */
  147. public function read()
  148. {
  149. $param = get_params();
  150. $id = isset($param['id']) ? $param['id'] : 0;
  151. $detail = $this->model->where('id', $id)->find();
  152. if (!empty($detail)) {
  153. View::assign('detail', $detail);
  154. return view();
  155. }
  156. else{
  157. throw new \think\exception\HttpException(404, '找不到页面');
  158. }
  159. }
  160. /**
  161. * 删除
  162. * type=0,逻辑删除,默认
  163. * type=1,物理删除
  164. */
  165. public function del()
  166. {
  167. $param = get_params();
  168. $id = isset($param['id']) ? $param['id'] : 0;
  169. $type = isset($param['type']) ? $param['type'] : 0;
  170. if($type==0){
  171. //逻辑删除
  172. try {
  173. $param['delete_time'] = time();
  174. $this->model->where('id', $id)->update(['delete_time'=>time()]);
  175. add_log('delete', $id);
  176. } catch(\Exception $e) {
  177. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  178. }
  179. }
  180. else{
  181. //物理删除
  182. try {
  183. $this->model->where('id', $id)->delete();
  184. add_log('delete', $id);
  185. } catch(\Exception $e) {
  186. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  187. }
  188. }
  189. return to_assign();
  190. }
  191. }