FCleanPlanRecord.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. class FCleanPlanRecord extends Base {
  7. protected $createTime = 'create_time';
  8. protected $updateTime = 'update_time';
  9. public $table = 'f_clean_plan_record';
  10. protected $validateName = 'FCleanPlanRecord';
  11. public function addSave() {
  12. $data = request()->post();
  13. $data['forms'] = !empty($data['forms'])?implode(',',$data['forms']):'';
  14. $data['addrs'] = !empty($data['addrs'])?implode(',',$data['addrs']):'';
  15. $data['org_id'] = cur_org_id();
  16. $result = validate($this->validateName)->check($data, [], 'add');
  17. if (true !== $result) {
  18. $this->error = validate($this->validateName)->getError();
  19. return false;
  20. }
  21. $user = $data['user'];
  22. unset($data['user']);
  23. $this->startTrans();
  24. try {
  25. $forms = explode(',',$data['forms']);
  26. $addrs = explode(',',$data['addrs']);
  27. unset($data['forms']);
  28. unset($data['addrs']);
  29. foreach ($forms as $k=>$v){
  30. //foreach ($addrs as $kk=>$vv){
  31. $rdata = [
  32. 'org_id' => $data['org_id'],
  33. 'form_id' => $v,
  34. 'address_id' => implode(',',$addrs),
  35. 'plan_id' => $data['plan_id'],
  36. 'start' => $data['start'],
  37. 'end' => $data['end'],
  38. 'remark' => $data['remark'],
  39. 'create_time' => getTime()
  40. ];
  41. $rid = Db::name('f_clean_plan_record')->insertGetId($rdata);
  42. if(!$rid){
  43. \exception('任务保存失败');
  44. }
  45. //添加任务人员
  46. $users = explode(',', $user);
  47. $nArr = [];
  48. foreach ($users as $v) {
  49. if($v){
  50. $nArr[] = [
  51. 'record_id' => $rid,
  52. 'user_id' => $v
  53. ];
  54. }
  55. }
  56. if($nArr){
  57. $res = Db::name('f_clean_plan_user')->insertAll($nArr);
  58. if (!$res) {
  59. \exception('保存失败');
  60. }
  61. }
  62. // }
  63. }
  64. $this->commit();
  65. return true;
  66. } catch (Exception $e) {
  67. $this->rollback();
  68. $this->error = $e->getMessage();
  69. return false;
  70. }
  71. }
  72. public function editSave() {
  73. $data = request()->post();
  74. $data['org_id'] = cur_org_id();
  75. $data['address_id'] = !empty($data['address_id'])?implode(',',$data['address_id']):'';
  76. $result = validate($this->validateName)->check($data, [], 'edit');
  77. if (true !== $result) {
  78. $this->error = validate($this->validateName)->getError();
  79. return false;
  80. }
  81. $user = $data['user'];
  82. unset($data['user']);
  83. $this->startTrans();
  84. try {
  85. $data['update_time'] = getTime();
  86. $res = Db::name('f_clean_plan_record')->where('id',$data['id'])->update($data);
  87. if(!$res){
  88. \exception('操作失败');
  89. }
  90. Db::name('f_clean_plan_user')->where('record_id',$data['id'])->delete();
  91. //添加任务人员
  92. $users = explode(',', $user);
  93. $nArr = [];
  94. foreach ($users as $v) {
  95. if($v){
  96. $nArr[] = [
  97. 'record_id' => $data['id'],
  98. 'user_id' => $v
  99. ];
  100. }
  101. }
  102. if($nArr){
  103. $res = Db::name('f_clean_plan_user')->insertAll($nArr);
  104. if (!$res) {
  105. \exception('保存失败');
  106. }
  107. }
  108. $this->commit();
  109. return true;
  110. } catch (Exception $e) {
  111. $this->rollback();
  112. $this->error = $e->getMessage();
  113. return false;
  114. }
  115. }
  116. public function get_list_by_time($orgId,$start,$end){
  117. $map[] = ['org_id','=',$orgId];
  118. $map[] = ['del','=',0];
  119. $map[] = ['start','<=',$end];
  120. $map[] = ['end','>=',$start];
  121. $list = $this->field('id,form_id,address_id,start,end,enable')
  122. ->where($map)
  123. ->select();
  124. $list = $list?$list->toArray():[];
  125. foreach ($list as $k=>$v){
  126. $list[$k]['ftitle'] = Db::name('f_clean_type')->where('id',$v['form_id'])->value('title');
  127. $list[$k]['atitle'] = Db::name('address')->where('id',$v['address_id'])->value('title');
  128. }
  129. return $list?$list:array();
  130. }
  131. public function detail($id,$orgId){
  132. $info = Db::name('f_clean_plan_record')->where('org_id',$orgId)->where('id',$id)->where('del',0)->find();
  133. if(!$info){
  134. $this->error = '记录不存在';
  135. return false;
  136. }
  137. $users = Db::name('f_clean_plan_user')
  138. ->alias('a')
  139. ->join('user b','a.user_id = b.id')
  140. ->where('a.record_id',$info['id'])
  141. ->column('b.real_name');
  142. $info['users'] = $users?implode(',',$users):'';
  143. $address = Db::name('address')
  144. ->where('id','in',explode(',',$info['address_id']))->select();
  145. $info['address'] = $address?implode(',',array_column($address,'title')):'';
  146. $forms = Db::name('f_clean_type')->where('id',$info['form_id'])->find();
  147. $info['form'] = $forms?$forms['title']:'';
  148. $info['finish_user'] = '';
  149. if($info['finish_user_id'] > 0){
  150. $info['finish_user'] = Db::name('user')->where('id',$info['finish_user_id'])->value('real_name');
  151. }
  152. $info['imgs'] = $info['imgs']?$info['imgs']:'';
  153. return $info;
  154. }
  155. public function lists($page,$size,$status,$day,$userId,$orgId) {
  156. $map[] = ['b.del','=',0];
  157. $map[] = ['b.enable','=',$status];
  158. $map[] = ['b.start','<=',$day];
  159. $map[] = ['b.end','>=',$day];
  160. $map[] = ['b.org_id','=',$orgId];
  161. $map[] = ['a.user_id','=',$userId];
  162. $lists = Db::name('f_clean_plan_user')
  163. ->alias('a')
  164. ->join('f_clean_plan_record b','a.record_id = b.id')
  165. ->where($map)
  166. ->order('b.id desc')
  167. ->field('b.id,b.address_id,b.form_id,b.enable,b.start,b.end')
  168. ->page($page, $size)
  169. ->select();
  170. $lists = $lists?$lists:[];
  171. foreach ($lists as $k=>$v){
  172. $users = Db::name('f_clean_plan_user')
  173. ->alias('a')
  174. ->join('user b','a.user_id = b.id')
  175. ->where('a.record_id',$v['id'])
  176. ->column('b.real_name');
  177. $lists[$k]['users'] = $users?implode(',',$users):'';
  178. $address = Db::name('address')
  179. ->where('id','in',explode(',',$v['address_id']))
  180. ->select();
  181. $lists[$k]['address'] = $address?implode(',',array_column($address,'title')):'';
  182. $forms = Db::name('f_clean_type')->where('id',$v['form_id'])->find();
  183. $lists[$k]['form'] = $forms?$forms['title']:'';
  184. }
  185. return $lists;
  186. }
  187. public function finish($id,$content,$imgs,$userId,$orgId){
  188. $info = Db::name('f_clean_plan_record')->where('org_id',$orgId)->where('id',$id)->where('del',0)->find();
  189. if(!$info){
  190. $this->error = '记录不存在';
  191. return false;
  192. }
  193. if($info['enable'] == 1){
  194. $this->error = '已完成,勿重复操作';
  195. return false;
  196. }
  197. $users = Db::name('f_clean_plan_user')
  198. ->alias('a')
  199. ->join('user b','a.user_id = b.id')
  200. ->where('a.record_id',$info['id'])
  201. ->where('a.user_id',$userId)
  202. ->column('b.id');
  203. if(!$users){
  204. $this->error = '无权限操作';
  205. return false;
  206. }
  207. $ret = Db::name('f_clean_plan_record')->where('id',$id)->update([
  208. 'enable' => 1,
  209. 'finish_user_id' => $userId,
  210. 'finish_time' => getTime(),
  211. 'content' => $content,
  212. 'imgs' => $imgs
  213. ]);
  214. if(!$ret){
  215. $this->error = '操作失败';
  216. return false;
  217. }
  218. return true;
  219. }
  220. }