using OpenAuth.Repository.Domain;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using static Dm.parser.LVal;

namespace OpenAuth.App.BasicQueryService
{
    public class DroneCaseInfoManager
    {
        ISqlSugarClient client;
        public DroneCaseInfoManager(ISqlSugarClient client)
        {
            this.client = client;
        }

        #region DroneCaseInfo 初始化相关
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="user"></param>
        /// <param name="db"></param>
        public DroneCaseInfo Init(DroneCaseInfo droneCaseInfo)
        {
            //主键
            droneCaseInfo.Id = Guid.NewGuid().ToString();

            //创建人和创建时间
            droneCaseInfo.createtime = DateTime.Now;
            //droneCaseInfo.createuser = user.Id;
            //droneCaseInfo.createusername = user.Name;

            //案件是否关闭，默认0 未关闭
            droneCaseInfo.is_closed = 0;

            //是否删除，默认0 未删除
            droneCaseInfo.is_delete = 0;

            //是否判读
            droneCaseInfo.is_intact = 0;

            //是否退回
            droneCaseInfo.is_improve = 0;
            droneCaseInfo.drawbackcount = 0;

            //处理状态
            droneCaseInfo.handle_status_id = 0;
            droneCaseInfo.handle_status_name = "未办理";

            // 案件是否已查看
            droneCaseInfo.is_checked = 0;

            //时间戳标识案件编号
            droneCaseInfo.case_no = DateTime.Now.ToString("yyyyMMddHHmmssffff");

            if (string.IsNullOrEmpty(droneCaseInfo.case_description)) droneCaseInfo.case_description = "";
            if (string.IsNullOrEmpty(droneCaseInfo.address)) droneCaseInfo.address = "";
            if (string.IsNullOrEmpty(droneCaseInfo.typeid)) droneCaseInfo.typeid = "";
            if (string.IsNullOrEmpty(droneCaseInfo.typename)) droneCaseInfo.typename = "";

            if (string.IsNullOrEmpty(droneCaseInfo.countyid)) droneCaseInfo.countyid = "";
            if (string.IsNullOrEmpty(droneCaseInfo.countyname)) droneCaseInfo.countyname = "";
            if (string.IsNullOrEmpty(droneCaseInfo.streetid)) droneCaseInfo.streetid = "";
            if (string.IsNullOrEmpty(droneCaseInfo.streetname)) droneCaseInfo.streetname = "";
            if (string.IsNullOrEmpty(droneCaseInfo.communityid)) droneCaseInfo.communityid = "";
            if (string.IsNullOrEmpty(droneCaseInfo.communityname)) droneCaseInfo.communityname = "";

            return InitOther(droneCaseInfo);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="user"></param>
        /// <param name="db"></param>
        public DroneCaseInfo Update(DroneCaseInfo droneCaseInfo)
        {
            return InitOther(droneCaseInfo);
        }

        /// <summary>
        /// 初始化 类型、状态、县镇村等名称
        /// </summary>
        /// <param name="db"></param>
        private DroneCaseInfo InitOther(DroneCaseInfo droneCaseInfo)
        {
            //类型
            if (!string.IsNullOrEmpty(droneCaseInfo.typeid))
            {
                var typeModel = client.Queryable<SysDataItemDetail>().Where(c => c.ItemDetailId == droneCaseInfo.typeid).First();
                if (typeModel != null)
                {
                    droneCaseInfo.typename = typeModel.ItemName;
                }
            }

            //案件状态
            if (!string.IsNullOrEmpty(droneCaseInfo.case_status_id))
            {
                var statusModel = client.Queryable<SysDataItemDetail>().Where(c => c.ItemDetailId == droneCaseInfo.case_status_id).First();
                if (statusModel != null)
                {
                    droneCaseInfo.case_status_name = statusModel.ItemName;
                }
            }

            //县
            if (!string.IsNullOrEmpty(droneCaseInfo.countyid))
            {
                var model = client.Queryable<SysOrg>().Where(c => c.Id.ToString() == droneCaseInfo.countyid).First();
                if (model != null)
                {
                    droneCaseInfo.countyname = model.Name;
                }
            }

            //镇
            if (!string.IsNullOrEmpty(droneCaseInfo.streetid))
            {
                var model = client.Queryable<SysOrg>().Where(c => c.Id.ToString() == droneCaseInfo.streetid).First();
                if (model != null)
                {
                    droneCaseInfo.streetname = model.Name;
                }
            }

            //村
            if (!string.IsNullOrEmpty(droneCaseInfo.communityid))
            {
                var model = client.Queryable<SysOrg>().Where(c => c.Id.ToString() == droneCaseInfo.communityid).First();
                if (model != null)
                {
                    droneCaseInfo.communityname = model.Name;
                }
            }

            return droneCaseInfo;
        }
        #endregion

        #region DroneCaseDeal 初始化相关
        //public DroneCaseDeal Init(SysUser user, string caseid)
        //{
        //    this.Id = caseid;
        //    this.createtime = DateTime.Now;
        //    this.createuser = user.Id;
        //    this.createusername = user.Name;
        //    this.is_delete = 0;
        //}

        #endregion


    }
}
