TaskLog.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. if ($finish_number == $taskInfo["number"]) {//完成任务赠送金币
  56. $giftvalue = $taskInfo['exp'];
  57. $walletRes = model('wallet')->lockChangeAccountRemain($user_id,0,'gold',$giftvalue,20,'完成任务:'.$taskInfo["name"],'task_log',$tasklogInfo['id']);
  58. }
  59. }
  60. $res = $tasklogModel->update($data,$where);
  61. } else {
  62. $data = [];
  63. $data["user_id"] = $user_id;
  64. $data["task_id"] = $taskInfo["id"];
  65. $data["finish_number"] = $number;
  66. $data["pace"] = round($number/$taskInfo["number"],2)*100;
  67. $data["finish_date"] = date("Ymd");
  68. if($number >= $taskInfo["number"]) {
  69. $data["is_finish"] = 1;
  70. $data["finish_time"] = time();
  71. }
  72. $data["createtime"] = time();
  73. $res = $tasklogModel->insertGetId($data);
  74. if ($number == $taskInfo["number"]) {//完成任务赠送金币
  75. $giftvalue = $taskInfo['exp'];
  76. $walletRes = model('wallet')->lockChangeAccountRemain($user_id,0,'gold',$giftvalue,20,'完成任务:'.$taskInfo["name"],'task_log',$res);
  77. }
  78. }
  79. return $res;
  80. }
  81. }