TaskLog.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. /**
  5. * 模型
  6. */
  7. class TaskLog extends Model
  8. {
  9. // 开启自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. /**
  15. * 增加任务完成进度
  16. */
  17. public static function tofinish($user_id,$task_id,$number = 1) {
  18. if(!$user_id || !$task_id || !$number) {
  19. return false;
  20. }
  21. // 查询任务明细
  22. $taskModel = new \app\common\model\Task();
  23. $tasklogModel = new \app\common\model\TaskLog();
  24. $where = [];
  25. $where["id"] = $task_id;
  26. $taskInfo = $taskModel->where($where)->find();
  27. if(!$taskInfo) {
  28. return false;
  29. }
  30. if($taskInfo['is_show'] != 1){
  31. return true;
  32. }
  33. // 添加/修改任务日志
  34. $where = [];
  35. $where["user_id"] = $user_id;
  36. $where["task_id"] = $taskInfo["id"];
  37. if($taskInfo["type_id"] == 2){
  38. $where["finish_date"] = date("Ymd");
  39. };
  40. $tasklogInfo = $tasklogModel->where($where)->lock(true)->find();
  41. if($tasklogInfo) {
  42. if($tasklogInfo["finish_number"] >= $taskInfo["number"] || $tasklogInfo["is_finish"] == 1) {
  43. return true;
  44. }
  45. $finish_number = $tasklogInfo["finish_number"]+$number;
  46. $where = [];
  47. $where["id"] = $tasklogInfo["id"];
  48. $data = [];
  49. $data["finish_number"] = $finish_number;
  50. $data["pace"] = round($finish_number/$taskInfo["number"],2)*100;
  51. $data["finish_date"] = date("Ymd");
  52. if($finish_number >= $taskInfo["number"]) {
  53. $data["is_finish"] = 1;
  54. $data["finish_time"] = time();
  55. }
  56. $res = $tasklogModel->update($data,$where);
  57. } else {
  58. $data = [];
  59. $data["user_id"] = $user_id;
  60. $data["task_id"] = $taskInfo["id"];
  61. $data["finish_number"] = $number;
  62. $data["pace"] = round($number/$taskInfo["number"],2)*100;
  63. $data["finish_date"] = date("Ymd");
  64. if($number >= $taskInfo["number"]) {
  65. $data["is_finish"] = 1;
  66. $data["finish_time"] = time();
  67. }
  68. $data["createtime"] = time();
  69. $res = $tasklogModel->insertGetId($data);
  70. }
  71. return $res;
  72. }
  73. }