Property.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. namespace app\adm\model;
  14. use think\model;
  15. use think\facade\Db;
  16. class Property extends Model
  17. {
  18. //资产来源
  19. public static $property_source = ['','采购','赠与','自产','其他'];
  20. //资产状态
  21. public static $property_status = ['闲置','在用','维修','报废','丢失'];
  22. /**
  23. * 获取分页列表
  24. * @param $where
  25. * @param $param
  26. */
  27. public function datalist($where, $param)
  28. {
  29. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  30. $order = empty($param['order']) ? 'p.id desc' : $param['order'];
  31. try {
  32. $list = self::where($where)
  33. ->field('p.*,pc.title as cate,pb.title as brand,pu.title as unit,u.name as create_name')
  34. ->alias('p')
  35. ->join('PropertyCate pc', 'pc.id = p.cate_id', 'left')
  36. ->join('PropertyBrand pb', 'pb.id = p.brand_id', 'left')
  37. ->join('PropertyUnit pu', 'pu.id = p.unit_id', 'left')
  38. ->join('Admin u', 'u.id = p.admin_id', 'left')
  39. ->order($order)
  40. ->paginate(['list_rows'=> $rows])
  41. ->each(function ($item, $key){
  42. $item->update_time_str = '-';
  43. $item->update_name = '-';
  44. if(!empty($item->update_time)){
  45. $item->update_time_str = $item->update_time;
  46. $item->update_name = Db::name('Admin')->where('id',$item->update_id)->value('name');
  47. }
  48. $item->status_str = self::$property_status[$item->status];
  49. $item->source_str = self::$property_source[$item->source];
  50. $item->create_time = to_date($item->create_time);
  51. $item->update_time_str = to_date($item->update_time);
  52. });
  53. return $list;
  54. } catch(\Exception $e) {
  55. return ['code' => 1, 'data' => [], 'msg' => $e->getMessage()];
  56. }
  57. }
  58. /**
  59. * 添加数据
  60. * @param $param
  61. */
  62. public function add($param)
  63. {
  64. $insertId = 0;
  65. try {
  66. $param['create_time'] = time();
  67. $insertId = self::strict(false)->field(true)->insertGetId($param);
  68. add_log('add', $insertId, $param);
  69. } catch(\Exception $e) {
  70. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  71. }
  72. return to_assign(0,'操作成功',['aid'=>$insertId]);
  73. }
  74. /**
  75. * 编辑信息
  76. * @param $param
  77. */
  78. public function edit($param)
  79. {
  80. try {
  81. $param['update_time'] = time();
  82. self::where('id', $param['id'])->strict(false)->field(true)->update($param);
  83. add_log('edit', $param['id'], $param);
  84. } catch(\Exception $e) {
  85. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  86. }
  87. return to_assign();
  88. }
  89. /**
  90. * 根据id获取信息
  91. * @param $id
  92. */
  93. public function getById($id)
  94. {
  95. $info = self::find($id);
  96. $info['status_str'] = self::$property_status[$info['status']];
  97. $info['source_str'] = self::$property_source[$info['source']];
  98. return $info;
  99. }
  100. /**
  101. * 删除信息
  102. * @param $id
  103. * @param $type
  104. * @return array
  105. */
  106. public function delById($id,$type=0)
  107. {
  108. if($type==0){
  109. //逻辑删除
  110. try {
  111. $param['delete_time'] = time();
  112. self::where('id', $id)->update(['delete_time'=>time()]);
  113. add_log('delete', $id);
  114. } catch(\Exception $e) {
  115. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  116. }
  117. }
  118. else{
  119. //物理删除
  120. try {
  121. self::destroy($id);
  122. add_log('delete', $id);
  123. } catch(\Exception $e) {
  124. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  125. }
  126. }
  127. return to_assign();
  128. }
  129. }