using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ChuPiao.Common.Models;
using ChuPiao.Common.Core.Security;

namespace ChuPiao.Common.Communication.GtdJson.Packs
{
    public class A10001GetTicketRet : PackRetBase
    {
        private L_LotTicketEntity _ticket=null;
        public L_LotTicketEntity Ticket
        {
            get { return _ticket; }
            set { _ticket = value; }
        }

        #region Overrides of PackRetBase

        /// <summary>
        /// 包体解包
        /// </summary>
        /// <param name="body"></param>
        public override void UnPackBody(JToken body)
        {
           
            if (body["task"] != null && !string.IsNullOrEmpty(body["task"].ToString().Trim()))    //出票以外的其它任务
            {
                Ticket = new L_LotTicketEntity();
                string task = body["task"].ToString().Trim();
                Ticket.L_Code = task;
                if (task == "recharge")  //充值任务
                {   
                    Ticket.L_Money = Convert.ToDecimal(body["money"].ToString());
                    Ticket.L_TicketSn = body["ordernum"].ToString();
                }
                else if (task == "getbouns")  //兑奖任务
                {
                    if (body["ticketpw"] == null || string.IsNullOrEmpty(body["ticketpw"].ToString().Trim()))
                        throw new Exception("兑奖任务，密码为空");

                    Ticket.L_TicketSn = body["ticketsn"].ToString();
                    Ticket.L_TicketCode = body["ticketcode"].ToString();
                    Ticket.L_Password = DesUtil.Decrypt(body["ticketpw"].ToString());
                    Ticket.LotId = Convert.ToInt32(body["lotid"].ToString());   //因为用不到，传的是服务端lotid，回写时原样返回
                }
            }
            else  //出票任务
            {
                if (body["ticketsn"] != null && !string.IsNullOrEmpty(body["ticketsn"].ToString().Trim()))
                {
                    Ticket = new L_LotTicketEntity();
                    Ticket.L_TicketSn = body["ticketsn"].ToString();
                    Ticket.L_Mutiple = Convert.ToInt32(body["beishu"].ToString());
                    Ticket.L_PlayId = Convert.ToInt32(body["playtype"].ToString());
                    Ticket.LotId = Ticket.L_PlayId / 100;
                    Ticket.L_Code = body["code"].ToString();
                    Ticket.L_CodeType = Convert.ToInt32(body["codetype"].ToString());
                    Ticket.L_Money = Convert.ToDecimal(body["money"].ToString());
                    if (body["apiendtime"] != null && !string.IsNullOrEmpty(body["apiendtime"].ToString().Trim()))
                    {
                        Ticket.L_EndTime = Convert.ToDateTime(body["apiendtime"].ToString().Trim());
                    }
                    else
                    {
                        Ticket.L_EndTime = Convert.ToDateTime("2050-01-01 00:00:01");
                    }
                    if (body["expect"] != null && !string.IsNullOrEmpty(body["expect"].ToString().Trim()))
                    {
                        Ticket.L_Expect = body["expect"].ToString();
                    }
                    else
                    {
                        Ticket.L_Expect = "";
                    }

                    //当前期，CurrentPeriod
                    if (body["CurrentPeriod"] != null && !string.IsNullOrEmpty(body["CurrentPeriod"].ToString().Trim()))
                    {
                        Ticket.L_CurrentExpect = body["CurrentPeriod"].ToString();
                    }
                    else
                    {
                        Ticket.L_CurrentExpect = "";
                    }
                }
            }
        }

        #endregion
    }
}
