using Kivii.Domain.Contents.Entities;
using Kivii.Linq;
using Kivii.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kivii.Domain.Contents.Transforms
{
    public class ReportQuery : RestfulExecution<Report>
    {
        public string ReportId { get; set; }
        public string VerificationCode { get; set; }
        public override object OnExecution(IRequest req, IResponse res)
        {
            (ReportId.IsNullOrEmpty() || VerificationCode.IsNullOrEmpty()).ThrowIfTrue("未传入报告号和验证码信息!");
            var conn = KiviiContext.GetOpenedDbConnection<Report>();
            var queryReport = conn.From<Report>();
            queryReport.Where(o=>o.ReportId== ReportId&&o.TypeEx!="J" && o.Status == int.MaxValue);
            var report = conn.Select(queryReport).FirstOrDefault();
            (report == null).ThrowIfTrue("未查询到报告!请重新核对报告号信息.");
            var code = report.Kvid.ToString();
            code = code.Substring(0,4).ToLower();
            (code != VerificationCode.ToLower()).ThrowIfTrue("验证码匹配失败!请重新核对验证码信息.");
            var rtns = new RestfulQueryResponse<Report>();
            rtns.Results = new List<Report>();
            rtns.Results.Add(report);
            rtns.Total = rtns.Results.Count;
            return rtns;
        }
    }
}
