using Competition.Common.Util;
using CompetitionAPI.api.unity;
using CompetitionAPI.Util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace CompetitionAPI.Controllers.unity.study
{
[Route("unity/[controller]")]
[ApiController]
public class StudyFileCompleteController : Controller
{
Competition.Mysql.BLL.pow_study_file file_bll = new Competition.Mysql.BLL.pow_study_file();
Competition.Mysql.BLL.pow_study_file_record file_record_bll = new Competition.Mysql.BLL.pow_study_file_record();
Competition.Mysql.BLL.pow_study_record record_bll = new Competition.Mysql.BLL.pow_study_record();
public StudyFileCompleteController()
{
}
///
/// 接收学习文件完成接口
///
/// 学习id
///
[Authorize]
[HttpPost]
[APIFilter]
public JsonResult Index([FromForm] StudyFileCompleteRequest req)
{
try
{
if (string.IsNullOrEmpty(req.StudyId))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "学习id不能为空"));
}
if (string.IsNullOrEmpty(req.UserId))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "用户id不能为空"));
}
if (string.IsNullOrEmpty(req.FileId))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "文件id不能为空"));
}
var count = file_record_bll.GetRecordCount(string.Format(" StudyId='{0}' and UserId='{1}' and FileId='{2}' ", req.StudyId, req.UserId, req.FileId));
if (count > 0)
{
return Json(Tool.GetJsonWithCode(APICode.Success, "已记录"));
}
var total = file_bll.GetRecordCount(string.Format(" StudyId='{0}' ", req.StudyId));
var now = DateTime.Now;
var record_model = new Competition.Mysql.Model.pow_study_file_record();
record_model.FileRecordId = Guid.NewGuid().ToString("N");
record_model.UserId = req.UserId;
record_model.FileId = req.FileId;
record_model.StudyId = req.StudyId;
record_model.CreateTime = now;
decimal file_count = file_record_bll.GetRecordCount(string.Format(" StudyId='{0}' and UserId='{1}' ", req.StudyId, req.UserId));
var com_count = (file_count + 1);
decimal value = (com_count / total);
var schedule = (int)(value * 100);
if (com_count == total)
{
schedule = 100;
}
var record_count = record_bll.GetRecordCount(string.Format(" StudyId='{0}' and UserId='{1}' ", req.StudyId, req.UserId));
if (record_count > 0)
{
if (record_bll.OperationEditOrAddData(req.StudyId, req.UserId, schedule, now, record_model) > 0)
{
return Json(Tool.GetJsonWithCode(APICode.Success, "成功"));
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "失败"));
}
}
else
{
var model = new Competition.Mysql.Model.pow_study_record();
model.RecordId = Guid.NewGuid().ToString("N");
model.StudyId = req.StudyId;
model.UserId = req.UserId;
model.RealName = req.RealName;
model.Schedule = schedule.ToString();
model.StudyTime = now;
model.CreateTime = now;
if (record_bll.OperationAddData(model, record_model) > 0)
{
return Json(Tool.GetJsonWithCode(APICode.Success, "成功"));
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "失败"));
}
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。"));
}
}
}
}