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