using System;
using System.Collections.Generic;
using System.Common;
using System.IRepositories;
using System.Linq;
using System.Model;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace TVShoppingCallCenter_ZLJ.Controllers.Customer
{
  //  [Authorize]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class CusMsgController : BaseController
    {
        private readonly ICus_MsgRepository _cus_msgRepository;
        private readonly ICus_VipInfoRepository _cus_vip_infoRepository;
        public CusMsgController(ICus_MsgRepository cus_msgRepository, ICus_VipInfoRepository cus_vip_infoRepository)
        {
            _cus_msgRepository = cus_msgRepository;
            _cus_vip_infoRepository = cus_vip_infoRepository;
        }

        [HttpGet]
        public IActionResult Index()
        {
            return Success("成功");
        }


        /// <summary>
        /// 添加消息提醒
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> AddAsync(int type, int vipid, string  toperson, string notifyperson,string notice)
        {

            var model = new T_Cus_Msg();
            model.F_Type = 1;
            model.F_VipInfoID = vipid;
            model.F_ToPerson = toperson;
            model.F_State = 0;
            model.F_Notice = notice;
            model.F_NotifyPerson = notifyperson;
            model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            model.F_CreateOn = DateTime.Now;
            var res = await _cus_msgRepository.Add(model);
            if (res > 0)
            {
                return Success("添加成功");
            }
            else
            {
                return Error("添加失败");
            }

        }

        /// <summary>
        /// 获取列表分页
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet("getlistbypage")]
        public async Task<IActionResult> GetListsByPageAsync(int state,string tousercode, string fromusercode, int type,int pageindex = 1, int pagesize = 20)//state传0时获取全部，传1时获取未处理，传2时获取已处理
        {
            List<IConditionalModel> conModels = new List<IConditionalModel>();
            #region 条件筛选
            if (state == 1)
            {
                //conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = state.ToString() });
                conModels.Add(new ConditionalCollections()
                {
                    ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
                    {
                        new  KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = "0" }),
                        new  KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = "1" })

                    }
                });
            }
            else if (state == 2)
            {
                    conModels.Add(new ConditionalCollections()
                    {
                        ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
                    {
                        new  KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
                        new  KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = "3" })

                    }
                    });
            }
            if (!string.IsNullOrEmpty(tousercode))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_ToPerson", ConditionalType = ConditionalType.Like, FieldValue = tousercode });
            }
            if (!string.IsNullOrEmpty(fromusercode))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_NotifyPerson", ConditionalType = ConditionalType.Like, FieldValue = fromusercode });
            }
            //if (!string.IsNullOrEmpty(keyword))
            //{
            //    conModels.Add(new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = keyword });
            //}
            
            #endregion
            int recordCount = 0;
            var list = await _cus_msgRepository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount },"F_ID DESC");
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = list,
                total = recordCount,
            };
            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 _cus_msgRepository.GetSingle(x => x.F_ID == id);
            if (model == null)
            {
                return Error("获取失败");
            }
            var infomodel= await  _cus_vip_infoRepository.GetSingle(x => x.F_ID == model.F_VipInfoID && x.F_State == (int)EnumDelState.Enabled);
            if(infomodel==null)
                return Error("相关的会员信息已不存在，请查询后处理！");
            if (model.F_State==0)
            {
                model.F_State = 1;
                await _cus_msgRepository.Update(model);
            }
            var obj = new
            {
                model,
                infomodel
            };
            return Success("获取成功！", obj);
        }

        /// <summary>
        /// 更新消息状态
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("updatescore")]
        public async Task<IActionResult> UpdateScore(int id,int state)
        {
            if (id <= 0)
                return Error("参数错误");
            else
            {
                var ml = await _cus_msgRepository.GetSingle(x => x.F_ID == id);
                ml.F_State = state;

                if (await _cus_msgRepository.Update(ml))
                {
                    return Success("状态更新成功");
                }
                else
                    return Error("状态更新失败");
            }
        }
    }
}
