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.Mvc;
using SqlSugar;

namespace TVShoppingCallCenter_ZLJ.Controllers.MediaCente
{
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class MediaManagementController : BaseController
    {
        private readonly ISys_MediaManagementRepository _sys_mediaManagement_Repository;
        private readonly ISys_AreaRepository _sys_areaRepository;
        public MediaManagementController(ISys_MediaManagementRepository sys_mediaManagement_Repository, ISys_AreaRepository sys_areaRepository)
        {
            _sys_mediaManagement_Repository = sys_mediaManagement_Repository;
            _sys_areaRepository = sys_areaRepository;
        }
        /// <summary>
        /// 添加媒体
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> AddAsync(T_Sys_MediaManagement input)
        {
            if (string.IsNullOrEmpty(input.F_Name))
                return Error("请输入媒体名称");
            if (string.IsNullOrEmpty(input.F_Telephone))
                return Error("请输入媒体电话");
            if(input.F_Name.Length>30)
                return Error("媒体名称不能过长");
            //  input.F_CreateUser = "8000";
            input.F_CreateTime = DateTime.Now;
            input.F_IsDelete = 0;
            input.F_CreateUser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            var res = await _sys_mediaManagement_Repository.Add(input);
            if (res > 0)
            {
                return Success("添加成功");
            }
            else
            {
                return Error("添加失败");
            }
        }
        /// <summary>
        /// 修改媒体
        /// </summary>
        [HttpPost("update")]
        public async Task<IActionResult> UpdateAsync(T_Sys_MediaManagement input)
        {
            if (input.F_ID <= 0)
                return Error("请选择正确的媒体");
            if (string.IsNullOrEmpty(input.F_Telephone))
                return Error("请输入媒体电话");
            if (input.F_Name.Length > 30)
                return Error("媒体名称不能过长");
            var model = await _sys_mediaManagement_Repository.GetSingle(x => x.F_ID == input.F_ID);
            if (model == null)
                return Error("操作失败");
            input.F_IsDelete = 0;
            input.F_CreateUser = model.F_CreateUser ;
            input.F_CreateTime = model.F_CreateTime;
            model = input;
            model.F_UpdateTime = DateTime.Now;
           // model.F_UpdateUser = "8000";
            model.F_UpdateUser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            var b = await _sys_mediaManagement_Repository.Update(model);
            if (b)
                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_mediaManagement_Repository.GetSingle(x => x.F_ID == item);
                    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_mediaManagement_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="keyword"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet("getlist")]
        public async Task<IActionResult> GetLisAsync(string name,string province, string city, string county, string region, 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(province))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_Province", ConditionalType = ConditionalType.Like, FieldValue = province });
            }
            if (!string.IsNullOrEmpty(city))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_City", ConditionalType = ConditionalType.Like, FieldValue = city });
            }
            if (!string.IsNullOrEmpty(county))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_County", ConditionalType = ConditionalType.Like, FieldValue = county });
            }
            if (!string.IsNullOrEmpty(region))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_Region", ConditionalType = ConditionalType.Equal, FieldValue = region });
            }
            #endregion
            int recordCount = 0;
            var list = await _sys_mediaManagement_Repository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount });

          
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = list,
                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_mediaManagement_Repository.GetSingle(x => x.F_ID == id);
            if (model == null)
            {
                return Error("获取失败");
            }
           
            return Success("获取成功！", model);
        }

    }
}