using System;
using System.Collections.Generic;
using System.Common;
using System.Common.Helpers;
using System.Data;
using System.IRepositories;
using System.Linq;
using System.Model;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using TVShoppingCallCenter_ZLJ.Models.Inputs;

namespace TVShoppingCallCenter_ZLJ.Controllers.TaskManagement
{
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class TaskManagementController : BaseController
    {
        private readonly ISMS_InternalMessagesReposytory _sys_internalMessagesrepository;
        private readonly ISys_UserAccountRepository _sys_useraccountRepository;
        private readonly ISys_TaskManagementRepository _sys_taskmanagement_Repository;
        private readonly ISys_DepartmentRepository _sys_departmentRepository;
        public TaskManagementController(ISys_TaskManagementRepository sys_taskmanagement_Repository, ISys_DepartmentRepository sys_departmentRepository,
            ISys_UserAccountRepository sys_useraccountRepository, ISMS_InternalMessagesReposytory sys_internalMessagesrepository)
        {
            _sys_taskmanagement_Repository = sys_taskmanagement_Repository;
            _sys_departmentRepository = sys_departmentRepository;
            _sys_useraccountRepository = sys_useraccountRepository;
            _sys_internalMessagesrepository = sys_internalMessagesrepository;
        }
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> AddAsync(T_Sys_TaskManagement input)
        {
            if (string.IsNullOrEmpty(input.F_Name))
                return Error("请输入任务名称");
            if (string.IsNullOrEmpty(input.F_Content))
                return Error("请输入任务内容");
            if (input.F_StartTime == null)
                return Error("请选择任务开始时间");
            if (input.F_EndTime == null)
                return Error("请选择任务结束时间");
            if (input.F_Type < 0)
                return Error("请选择任务类型");
            if (string .IsNullOrEmpty (input .F_Deptid ))
                return Error("请选择任务部门");
            if (input.F_Money < 0)
                return Error("请输入目标金额");
          //  string user = "8000";
            if (input.F_Remindertime == null)
                input.F_Remindertime = input.F_EndTime.Value.AddDays(-3);
            if (input.F_Currentamount > 0)
                input.F_Taskprogress = string.Format("{0:f2}", input.F_Currentamount / input.F_Money * 100);
            else
                input.F_Taskprogress = "0";
             string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            input.F_CreateTime = DateTime.Now;
            input.F_CreateUser = user;
            input.F_IsDelete = 0;

           
            
            var res = await _sys_taskmanagement_Repository.Add(input);
            if (res > 0)
            {
                if (input .F_Parentid == res)
                {
                    input.F_Parentid = 0;
                   var  v=  _sys_taskmanagement_Repository.Update(input);
                }
                try
                {
                    DateTime time = DateTime.Parse(input.F_Remindertime.ToString());
                    if (input .F_Tasker >0)
                    {
                        string usercode = _sys_useraccountRepository.GetSingle(x => x.F_UserId == input.F_Tasker).Result.F_UserCode;
                        bool n = new InternalMessages.InternalMessagesController(_sys_internalMessagesrepository, _sys_useraccountRepository).AddInternalMessagesInfo(res, "任务提醒", input.F_Name + "任务提醒", user, time, 2, 1, 0, usercode, 1);
                    }
                    else
                    {
                        bool n = new InternalMessages.InternalMessagesController(_sys_internalMessagesrepository, _sys_useraccountRepository).AddInternalMessagesInfo(res, "任务提醒", input.F_Name + "任务提醒", user, time, 2, 1, int.Parse(input.F_Deptid.Split(',')[input.F_Deptid.Split(',').Length - 1]), "", 0);
                    }
                   
                }
                catch
                {

                }
                return Success("添加成功");
            }
            else
            {
                return Error("添加失败");
            }
        }
        /// <summary>
        /// 修改任务
        /// </summary>
        [HttpPost("update")]
        public async Task<IActionResult> UpdateAsync(T_Sys_TaskManagement input)
        {
            if (input.F_ID <= 0)
                return Error("参数错误");
            if (input.F_Parentid == input.F_ID)
                return Error("父级ID不能和子ID相同");
            if (string.IsNullOrEmpty(input.F_Name))
                return Error("请输入任务名称");
            if (string.IsNullOrEmpty(input.F_Content))
                return Error("请输入任务内容");
            if (input.F_StartTime == null)
                return Error("请选择任务开始时间");
            if (input.F_EndTime == null)
                return Error("请选择任务结束时间");
            if (input.F_Type < 0)
                return Error("请选择任务类型");
            if (string.IsNullOrEmpty(input.F_Deptid))
                return Error("请选择任务部门");
            if (input.F_Money < 0)
                return Error("请输入目标金额");
            var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == input.F_ID);
            if (model == null)
                return Error("操作失败");
            if (input.F_Remindertime == null)
                input.F_Remindertime = input.F_EndTime.Value.AddDays(-3);
            if (input.F_Currentamount > 0)
                input.F_Taskprogress = string.Format("{0:f2}", input.F_Currentamount / input.F_Money * 100);
            else
                input.F_Taskprogress = "0";
             // string user = "8000";
         string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            input.F_IsDelete = 0;
            input.F_CreateUser  = model.F_CreateUser;
            input.F_CreateTime = model.F_CreateTime ;
            input.F_UpdateTime = DateTime.Now;
            input.F_UpdateUser = user;
            // model.F_UpdateUser = "8000";
            var b = await _sys_taskmanagement_Repository.Update(input);
            if (b)
            {
                DateTime time = DateTime.Parse(input.F_Remindertime.ToString());
                var InternalMessages = _sys_internalMessagesrepository.GetListALL (x => x.F_ID == model.F_ID&&x.SMS_IsDelete ==0).Result.FirstOrDefault ();
                if (InternalMessages!=null )
                {
                    try
                    {
                        if (input.F_Tasker > 0)
                        {
                            string usercode = _sys_useraccountRepository.GetSingle(x => x.F_UserId == input.F_Tasker).Result.F_UserCode;
                            InternalMessages.SMS_Content = input.F_Name + "任务提醒";
                            InternalMessages.SMS_Remindertime = input.F_Remindertime;
                            InternalMessages.SMS_ReceiveUserCode = usercode;
                            bool n = await _sys_internalMessagesrepository.Update(InternalMessages);
                        }
                        else
                        {
                            InternalMessages.SMS_Content = input.F_Name + "任务提醒";
                            InternalMessages.SMS_Remindertime = input.F_Remindertime;
                            InternalMessages.SMS_Deptid = int.Parse(input.F_Deptid.Split(',')[input.F_Deptid.Split(',').Length - 1]);
                            bool n = await _sys_internalMessagesrepository.Update(InternalMessages);
                        }
                    }
                    catch
                    {

                    }
                 
                }
                else
                {
                    try
                    {
                        if (input.F_Tasker > 0)
                        {
                            string usercode = _sys_useraccountRepository.GetSingle(x => x.F_UserId == input.F_Tasker).Result.F_UserCode;
                            bool n = new InternalMessages.InternalMessagesController(_sys_internalMessagesrepository, _sys_useraccountRepository).AddInternalMessagesInfo(model.F_ID, "任务提醒", input.F_Name + "任务提醒", user, time, 2, 1, 0, usercode, 1);
                        }
                        else
                        {
                            bool n = new InternalMessages.InternalMessagesController(_sys_internalMessagesrepository, _sys_useraccountRepository).AddInternalMessagesInfo(model.F_ID, "任务提醒", input.F_Name + "任务提醒", user, time, 2, 1, int.Parse(input.F_Deptid.Split(',')[input.F_Deptid.Split(',').Length - 1]), "", 0);
                        }
                    }
                    catch
                    {

                    }
                  
                }
               
                
                return Success("修改成功");
            }
               
            return Error("修改失败");
        }
        
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> Remove(int[] ids)
        {
            var res = 0;
            if (ids != null && ids.Length > 0)
            {
                foreach (var item in ids)
                {
                    var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == item);
                    var message = await _sys_internalMessagesrepository.GetListALL(x => x.F_ID == item);
                    if (message != null)
                    {
                        foreach (var it in message)
                        {
                            it.SMS_IsDelete = (int)EnumUserCountState.Delete;
                            var n = _sys_internalMessagesrepository.Update(it);
                        }
                    }
                    model.F_IsDelete = (int)EnumUserCountState.Delete;
                    model.F_DeleteTime = DateTime.Now.ToLocalTime();
                    model.F_DeleteUser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
                    if (_sys_taskmanagement_Repository.Update(model).Result)
                        res += 1;
                }
                if (res == ids.Length)
                    return Success("删除成功");
                else if (res > 0 && res < ids.Length)
                    return Error("部分删除失败，请查看后重新操作");
                else
                    return Error("删除失败，请查看后重新操作");
            }
            else
                return Error("请选择要删除的记录");
        }
        /// <summary>
        /// 操作任务
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet("operation")]
        public async Task<IActionResult> GetOperationAsync(int id, int state)
        {
            if (id <= 0)
                return Error("参数错误");

            var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == id);
            if (model == null)
            {
                return Error("获取失败");
            }
            model.F_State = state;
            var obj = await _sys_taskmanagement_Repository.Update(model);
            if (obj)
                return Success("操作成功");
            else
                return Error("操作失败");
        }
        /// <summary>
        /// 获取任务列表
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet("getlist")]
        public async Task<IActionResult> GetListMark(string keyword, string name, string starttime, string endtime, string tasker,
      string  deptid , int parentid=0,int type = -1, int pageindex = 1, int pagesize = 20)
        {
            List<IConditionalModel> conModels = new List<IConditionalModel>();
            #region 条件筛选
            conModels.Add(new ConditionalModel() { FieldName = "F_IsDelete", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumUserCountState.Enabled).ToString() });
            if (!string.IsNullOrEmpty(name))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = name });
            }
            if (!string.IsNullOrEmpty(tasker))
            {
                if (_sys_useraccountRepository .GetListALL(x => x.F_UserName  == tasker&&x.F_DeleteFlag ==0).Result .FirstOrDefault ()!= null)
                {
                    conModels.Add(new ConditionalModel() { FieldName = "F_Tasker", ConditionalType = ConditionalType.Equal , FieldValue = _sys_useraccountRepository.GetListALL (x => x.F_UserName == tasker && x.F_DeleteFlag == 0).Result.FirstOrDefault ().F_UserId .ToString ()  });
                }
                else
                {
                    return Error("请输入正确的任务人");
                }
            }
            if (string .IsNullOrEmpty (tasker)&& string.IsNullOrEmpty(keyword))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_Parentid", ConditionalType = ConditionalType.Equal, FieldValue = parentid.ToString() });
            }
           // string user = "8000";
               string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            var dept = _sys_useraccountRepository.GetSingle(x => x.F_UserCode == user&&x.F_DeleteFlag ==0).Result != null ? _sys_useraccountRepository.GetSingle(x => x.F_UserCode == user&& x.F_DeleteFlag == 0).Result.F_DeptId : 0;
            if (!string.IsNullOrEmpty(deptid))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_Deptid", ConditionalType = ConditionalType.Equal, FieldValue = deptid });
            }
            else
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_CreateUser", ConditionalType = ConditionalType.Equal, FieldValue = user });
                new KeyValuePair<WhereType, ConditionalModel>(WhereType.Or, new ConditionalModel() { FieldName = "F_Deptid", ConditionalType = ConditionalType.Like, FieldValue = dept.ToString() });
                deptid = dept.ToString();
            }
          
            
            if (type > -1)
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_Type", ConditionalType = ConditionalType.Like, FieldValue = type.ToString() });
            }
           
            if (!string.IsNullOrEmpty(keyword))
            {
                conModels.Add(new ConditionalCollections()
                {
                    ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
                    {
                        new  KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = keyword }),
                        new  KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Content", ConditionalType = ConditionalType.Like, FieldValue = keyword })
                    }
                });
            }

            if (!string.IsNullOrEmpty(starttime))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_StartTime", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = starttime });
            }
            if (!string.IsNullOrEmpty(endtime))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_EndTime", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = endtime });
            }
          
            #endregion
            int recordCount = 0;
            var list = await _sys_taskmanagement_Repository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount });
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = ConvertAsync(list.Rows ),
                total = list.Totals,
            };

            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取任务详情
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet("getdetails")]
        public async Task<IActionResult> GetDetailsAsync(int id)
        {
            if (id <= 0)
                return Error("参数错误");

            var model = await _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == id);
            if (model == null)
            {
                return Error("获取失败");
            }
            return Success("获取成功！", Convert(model, 1));
        }
        /// <summary>
        /// 修改任务当前金额
        /// </summary>
        /// <param name="id"></param>
        /// <param name="money"></param>
        /// <returns></returns>
        public int  UpdateTaskAmount(T_Bus_Order  t_Bus_Order )
        {
            //0成功1任务暂停2任务终止3修改任务金额失败4任务不存在5父级任务不存在6父级任务修改失败7父级任务暂停8父级任务终止
            int n = 0;
          
            var model =  _sys_taskmanagement_Repository.GetListALL (x => x.F_Tasker  == t_Bus_Order.F_AddUser&& x.F_IsDelete ==0);
            if (model == null)
              return 4;
            foreach (var it  in model.Result )
            {
                if (it.F_State == 0)
                {
                    it.F_Currentamount +=(float  ) t_Bus_Order.F_RealPrice;
                    it.F_Taskprogress = string.Format("{0:f2}", it.F_Currentamount / it.F_Money * 100);
                    var res = _sys_taskmanagement_Repository.Update(it);
                    if (!res.Result)
                        return 3;
                    if (it.F_Parentid > 0)
                    {
                        n = TreeRecursion(it.F_Parentid, (float)t_Bus_Order.F_RealPrice);
                    }
                }
                else
                    n = (int)it .F_State;
            }
            
             return  n;
        }
        /// <summary>
        /// 递归算法
        /// </summary>
        /// <param name="data"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private int   TreeRecursion( int parentId = 0,float  money=0)
        {
            int  n = 0;
            var model = _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == parentId).Result;
            if (model == null)
                return 5;
            if (model.F_State == 0)
            {
                model.F_Currentamount += money;
                model.F_Taskprogress = string.Format("{0:f2}", model.F_Currentamount / model.F_Money * 100);
                var res = _sys_taskmanagement_Repository.Update(model);
                if (!res.Result)
                    return 6;
                if (model.F_Parentid > 0)
                {
                    n = TreeRecursion(model.F_Parentid, money);
                }
            }
            else
                n = (int)model.F_State+6;
            return n;
        }
        /// <summary>
        /// 上传文件并导入数据库
        /// </summary>
        /// <returns></returns>
        [HttpPost("importexcel")]
        public async Task<IActionResult> ImportExcel(int headrow = 0,int parentid=0)
        {
            Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
            if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
                return Error($"请正确上传Excel文件：file.ContentType={_upfile.ContentType}");
            NPOIHelper npoi = new NPOIHelper();
            var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
            int num = dtExcel.Rows.Count;
            var cols = dtExcel.Columns;
            int colnum = cols.Count;
            string errmsg = string.Empty;
            if (num > 0)
            {
                int index = 1;
                foreach (DataRow dr in dtExcel.Rows)
                {
                    
                    var model = new T_Sys_TaskManagement();
                    string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
                    try
                    {
                        model.F_Name = dr["任务名称"].ToString();
                        model.F_Content = dr["任务内容"].ToString();
                        if (string.IsNullOrEmpty(dr["任务开始时间"].ToString()))
                        {
                            errmsg = errmsg + "\r\n第" + index + "行导入失败！请输入任务开始时间";
                            continue;
                        }
                        if (string.IsNullOrEmpty(dr["任务结束时间"].ToString()))
                        {
                            errmsg = errmsg + "\r\n第" + index + "行导入失败！请输入任务结束时间";
                            continue;
                        }
                        if (string.IsNullOrEmpty(dr["目标金额"].ToString()))
                        {
                            errmsg = errmsg + "\r\n第" + index + "行导入失败！请输入目标金额";
                            continue;
                        }
                        try
                        {
                            model.F_StartTime = DateTime.Parse(dr["任务开始时间"].ToString());
                        }
                        catch
                        {
                            errmsg = errmsg + "\r\n第" + index + "行导入失败！任务开始时间格式错误";
                            continue;
                        }
                        try
                        {
                            model.F_EndTime = DateTime.Parse(dr["任务结束时间"].ToString());
                        }
                        catch
                        {
                            errmsg = errmsg + "\r\n第" + index + "行导入失败！任务结束时间格式错误";
                            continue;
                        }
                        if (dr["任务类型"].ToString() == "月")
                            model.F_Type = 2;
                        else if (dr["任务类型"].ToString() == "周")
                            model.F_Type = 1;
                        else
                            model.F_Type = 0;
                        if (_sys_departmentRepository.GetSingle(x => x.F_DeptName == dr["任务部门"].ToString()).Result != null)
                        {
                            model.F_Deptid = _sys_departmentRepository.GetSingle(x => x.F_DeptName == dr["任务部门"].ToString()).Result.F_DeptId.ToString();
                        }
                        else
                        {
                            errmsg = errmsg + "\r\n第" + index + "行导入失败！请检查任务部门";
                            continue;
                        }
                        if (_sys_useraccountRepository.GetSingle(x => x.F_UserName == dr["任务人"].ToString() && x.F_DeleteFlag == 0).Result != null)
                        {
                            model.F_Tasker = _sys_useraccountRepository.GetSingle(x => x.F_UserName == dr["任务人"].ToString() && x.F_DeleteFlag == 0).Result.F_UserId;

                        }
                        else
                        {
                            errmsg = errmsg + "\r\n第" + index + "行导入失败！请检查任务人";
                            continue;
                        }
                        try
                        {
                            model.F_Money = float.Parse(dr["目标金额"].ToString());
                        }
                        catch
                        {
                            errmsg = errmsg + "\r\n第" + index + "行导入失败！请输入正确的目标金额";
                            continue;
                        }
                        if (string.IsNullOrEmpty(dr["当前金额"].ToString()))
                            model.F_Currentamount = 0;
                        try
                        {
                            model.F_Currentamount = float.Parse(dr["当前金额"].ToString());
                        }
                        catch
                        {
                            errmsg = errmsg + "\r\n第" + index + "行导入失败！请输入正确的当前金额";
                            continue;
                        }
                        if (!string.IsNullOrEmpty(dr["任务提醒时间"].ToString()))
                        {
                            try
                            {
                                model.F_Remindertime = DateTime.Parse(dr["任务提醒时间"].ToString());
                            }
                            catch
                            {
                                errmsg = errmsg + "\r\n第" + index + "行导入失败！任务提醒时间格式错误";
                                continue;
                            }
                        }
                        else
                        {
                            model.F_Remindertime = model.F_EndTime.Value.AddDays(-3);
                        }
                        model.F_Taskprogress = string.Format("{0:f2}", model.F_Currentamount / model.F_Money * 100);
                        if (dr["任务类型"].ToString() == "终止")
                            model.F_State = 2;
                        else if (dr["任务类型"].ToString() == "暂停")
                            model.F_State = 1;
                        else
                            model.F_State = 0;
                    }
                   catch
                    {
                        return Error("模板错误，请选择正确的模板");
                    }
                    model.F_CreateTime = DateTime.Now;
                    model.F_CreateUser  = user;
                    model.F_IsDelete = 0;
                    model.F_Parentid = parentid;
                    int b = await _sys_taskmanagement_Repository.Add(model);
                    if (b <= 0)
                    {
                        if (!string.IsNullOrEmpty(errmsg))
                        {
                            errmsg = errmsg + "\r\n第" + index + "行导入失败！";
                        }
                        else
                        {
                            errmsg = "第" + index + "行导入失败！";
                        }
                    }
                    index++;

                }
            }
            else
            {
                return Error("文件中无数据");
            }

            if (!string.IsNullOrEmpty(errmsg))
            {
                return Error(errmsg);
            }

            return Success("导入成功");
        }
        #region 私有方法
        private TaskManagementInput Convert(T_Sys_TaskManagement it, int type = 0)
        {
            TaskManagementInput task  = new TaskManagementInput();
            task.F_ID = it.F_ID;
            task.F_Name = it.F_Name;
            task.F_Content = it.F_Content;
            task.F_StartTime = it.F_StartTime;
            task.F_EndTime = it.F_EndTime;
            task.F_Type = it.F_Type;
            task.F_Deptid = it.F_Deptid;
            string deptmsg = "";
            if (!string.IsNullOrEmpty(it.F_Deptid))
            {
                try
                {

                    string[] sprit = it.F_Deptid.Split(',');
                    foreach (var iv in sprit)
                    {

                        if (deptmsg == "")
                            deptmsg = _sys_departmentRepository.GetSingle(x => x.F_DeptId == int.Parse(iv)).Result != null ? _sys_departmentRepository.GetSingle(x => x.F_DeptId == int.Parse(iv)).Result.F_DeptName : "";
                        else
                            deptmsg = deptmsg + "," + _sys_departmentRepository.GetSingle(x => x.F_DeptId == int.Parse(iv)).Result != null ? _sys_departmentRepository.GetSingle(x => x.F_DeptId == int.Parse(iv)).Result.F_DeptName : "";
                    }
                }
                catch
                {
                }
            }
            task.F_DeptName = deptmsg;// 部门名称
            task.F_Tasker = it.F_Tasker;
            task.F_Money = it.F_Money;
            task.F_TaskerName = _sys_useraccountRepository.GetSingle(x => x.F_UserId == it.F_Tasker).Result != null ? _sys_useraccountRepository.GetSingle(x => x.F_UserId == it.F_Tasker).Result .F_UserName  : "";
            task.F_Currentamount = it.F_Currentamount;
            task.F_Taskprogress = it.F_Taskprogress;
            task.F_State = it.F_State;
            task.F_Remindertime = it.F_Remindertime;
            if (type ==0)
            {
                var  taskcount = _sys_taskmanagement_Repository.GetListALL(x => x.F_Parentid == it.F_ID&&x.F_IsDelete ==0).Result != null ? _sys_taskmanagement_Repository.GetListALL(x => x.F_Parentid == it.F_ID && x.F_IsDelete == 0).Result: null ;

               if (taskcount!=null )
                {
                    task.children = ConvertAsync(taskcount);
                }
            }
          
            task.F_Parentid = it.F_Parentid;
            task.F_ParentName = _sys_taskmanagement_Repository.GetSingle(x => x.F_ID  == it.F_Parentid).Result != null ? _sys_taskmanagement_Repository.GetSingle(x => x.F_ID == it.F_Parentid).Result.F_Name  : "";// 部门名称
            return task;
        }
        /// model转input
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private List<TaskManagementInput> ConvertAsync(List<T_Sys_TaskManagement> model)
        {
            List<TaskManagementInput> input = new List<TaskManagementInput>();
            if (model != null)
            {

                foreach (var it in model)
                {
                    TaskManagementInput marketing = new TaskManagementInput();
                    marketing = Convert(it);
                    input.Add(marketing);
                }
            }
            return input;
        }
        #endregion
    }
}