using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Sugar.Enties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Utils;

namespace PMS.NetCore.Controllers
{
    /// <summary>
    /// 客户信息
    /// </summary>
    [Produces("application/json;charset=UTF-8")]
    [Route("api/Customer")]
    [ServiceFilter(typeof(Filter.TokenAuthorize))]
    public class CustomerController : BaseController
    {
        /// <summary>
        /// 客户关系
        /// </summary>
        public CustomerController()
        {
            logger = NLog.LogManager.GetCurrentClassLogger();
        }
        #region 房主信息
        /// <summary>
        /// 获取房主资料列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("GetFdc_pm_ownerList")]
        public JsonResult GetFdc_pm_ownerList()
        {
            var manager = new fdc_pm_ownerManager();
            var list = manager.GetList();
            logger.Info($"【GetFdc_pm_ownerList】list:{JsonConvert.SerializeObject(list)}");
            return Json(list);
        }
        /// <summary>
        /// 获取房产资料实例
        /// </summary>
        /// <param name="dto">房号</param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetFdc_pm_owner")]
        public JsonResult GetFdc_pm_owner([FromBody] SingleParamDTO dto)
        {
            logger.Info($"【GetFdc_pm_owner】dto:{JsonConvert.SerializeObject(dto)}");
            if (dto == null || dto.value.IsNullOrEmpty())
            {
                return Json("参数错误");
            }
            var manager = new fdc_pm_ownerManager();
            var model = manager.GetById(dto.value);
            logger.Info($"【GetFdc_pm_owner】model:{JsonConvert.SerializeObject(model)}");
            return Json(model);
        }

        /// <summary>
        /// 业主资料添加
        /// </summary>
        /// <param name="list">业主资料列表</param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        [Route("Fdc_pm_ownerAdd")]
        public JsonResult Fdc_pm_ownerAdd([FromBody] List<fdc_pm_owner> list)
        {
            logger.Info($"【Fdc_pm_ownerAdd】dto:{JsonConvert.SerializeObject(list)}");
            try
            {
                var errmsg = "";

                var manager = new fdc_pm_ownerManager();
                foreach (var dto in list)
                {
                    var index = list.IndexOf(dto);
                    if (dto.ownerid.IsNullOrEmpty())
                    {
                        dto.ownerid = new maxbillcodeManager().GetMaxBillCode("", "", true);
                    }
                    if (dto.ownerid.IsNullOrEmpty())
                    {
                        errmsg += $"第{index + 1}条数据业主编码不能为空;";
                    }
                    if (dto.ownername.IsNullOrEmpty())
                    {
                        errmsg += $"第{index + 1}条数据业主名称不能为空;";
                    }
                    if (dto.firsttel.IsNullOrEmpty())
                    {
                        errmsg += $"第{index + 1}条数据业主电话不能为空;";
                    }
                    dto.flag = "1";
                }
                if (errmsg != "")
                {
                    return Json(errmsg);
                }

                //去除重复的业主主键
                var list1 = list.Where((x, i) => list.FindIndex(z => z.ownerid == x.ownerid) == i).ToList();
                //var list2 = new List<string>();
                //var owneridMsg = "";
                //foreach (var dto in list1)
                //{
                //    var index = list.IndexOf(dto);
                //    var entity = manager.GetById(dto.ownerid);
                //    if (entity != null)
                //    {
                //        owneridMsg += $"第{index + 1}条数据{dto.ownerid }业主编码已存在;";
                //        list2.Add(dto.ownerid);
                //    }

                //}
                //if(owneridMsg.IsNotNullAndEmpty())
                //{
                //    return Json(owneridMsg);
                //}
                for (int i = list1.Count - 1; i >= 0; i--)
                {
                    var entity = manager.GetById(list1[i].ownerid);
                    if (entity != null)
                    {
                        list1.Remove(list1[i]);
                    }
                }

                int row = 0;
                if (list1.Count > 0)
                {
                    //批量插入
                    row = manager.Db.Insertable(list1).ExecuteCommand();
                }
                else
                {
                    row = 1;
                }



                logger.Info($"【Fdc_pm_ownerAdd】row:{row}");
                return Json(row > 0 ? true : false);

            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(ex.Message);
            }
        }

        #endregion

        #region  房间客户关系信息
        /// <summary>
        /// 获取房间客户关系列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("GetFdc_pm_ownerhouseList")]
        public JsonResult GetFdc_pm_ownerhouseList()
        {
            try
            {
                var manager = new fdc_pm_ownerhouseManager();
                var list = manager.GetList();
                logger.Info($"【GetFdc_pm_ownerhouseList】list:{JsonConvert.SerializeObject(list)}");
                return Json(list);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(ex.Message);
            }
        }
        /// <summary>
        /// 获取房间客户关系实例
        /// </summary>
        /// <param name="dto">房号</param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetFdc_pm_ownerhouse")]
        public JsonResult GetFdc_pm_ownerhouse([FromBody] SingleParamDTO dto)
        {
            logger.Info($"【GetFdc_pm_ownerhouse】dto:{JsonConvert.SerializeObject(dto)}");
            if (dto == null || dto.value.IsNullOrEmpty())
            {
                return Json("参数错误");
            }
            var manager = new fdc_pm_ownerhouseManager();
            var model = manager.CurrentDb.GetSingle(m => m.autoid == dto.value.ToInteger());
            logger.Info($"【GetFdc_pm_ownerhouse】model:{JsonConvert.SerializeObject(model)}");
            return Json(model);
        }

        /// <summary>
        /// 房间客户关系添加
        /// </summary>
        /// <param name="list">房间客户关系实例</param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        [Route("Fdc_pm_ownerhouseAdd")]
        public JsonResult Fdc_pm_ownerhouseAdd([FromBody] List<fdc_pm_ownerhouse> list)
        {
            logger.Info($"【Fdc_pm_ownerhouseAdd】list:{JsonConvert.SerializeObject(list)}");
            try
            {
                var manager = new fdc_pm_ownerhouseManager();
                var errmsg = "";
                foreach (var dto in list)
                {
                    var index = list.IndexOf(dto);
                    if (dto.ownerid.IsNullOrEmpty())
                    {

                        errmsg += $"第{index + 1}条数据{dto.ownerid }业主编码不能为空;";
                    }
                    if (dto.hcode.IsNullOrEmpty())
                    {
                        errmsg += $"第{index + 1}条数据{dto.ownerid }房产编号不能为空;";

                    }
                    if (dto.hname.IsNullOrEmpty())
                    {
                        errmsg += $"第{index + 1}条数据{dto.ownerid }房产名称不能为空;";

                    }

                    dto.flag = "1";
                }
                if (errmsg.IsNotNullAndEmpty())
                {
                    return Json(errmsg);
                }
                var row = manager.Db.Insertable(list).ExecuteCommand();

                return Json(row > 0 ? true : false);

            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(ex.Message);
            }

        }
        #endregion

    }

}
