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 TVShoppingCallCenter_ZLJ.Models.Inputs;

namespace TVShoppingCallCenter_ZLJ.Controllers.MediaCente
{
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class MediaPlanController : BaseController
    {
        private readonly ISys_MediaCenteRepository _sys_mediacenter_Repository;
        private readonly ISys_PlanManagemenRepository _sys_planmanagemenRepository;
        public MediaPlanController(ISys_MediaCenteRepository sys_mediacenter_Repository, ISys_PlanManagemenRepository sys_planmanagemenRepository
           )
        {
            _sys_mediacenter_Repository = sys_mediacenter_Repository;
            _sys_planmanagemenRepository = sys_planmanagemenRepository;
        }
        /// <summary>
        /// 添加媒体计划
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> AddAsync(T_Sys_PlanManagemen input,int id)
        {
            if (input.F_Type < 0)
                return Error("请选择计划类型");
            if (id <= 0)
                return Error("参数错误");
            var model = await _sys_mediacenter_Repository.GetSingle(x => x.F_ID == id);
            if (model==null )
                return Error("媒体错误");
            //  string user = "8000";
            string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            input.F_CreateTime = DateTime.Now;
            input.F_CreateUser  = user;
            var res = await _sys_planmanagemenRepository.Add(input);
            if (res > 0)
            {
                if (input .F_Type ==0)
                {
                    if (!string.IsNullOrEmpty(model.F_PlayPlan))
                        model.F_PlayPlan += "," + res;// 
                    else
                        model.F_PlayPlan = res + "";
                }
                else
                {
                    if (!string.IsNullOrEmpty(model.F_MediaChange ))
                        model.F_MediaChange += "," + res;// 
                    else
                        model.F_MediaChange = res + "";
                }
               
                var mediacenter = await _sys_mediacenter_Repository.Update (model);
                if (mediacenter)
                return Success("添加成功");
                else
                    return Error("添加失败");
            }
            else
            {
                return Error("添加失败");
            }
        }
        /// <summary>
        /// 修改媒体计划
        /// </summary>
        [HttpPost("update")]
        public async Task<IActionResult> UpdateAsync(T_Sys_PlanManagemen input)
        {

            if (input.F_Id <= 0)
                return Error("请选择正确的媒体");
            if (input.F_Type < 0)
                return Error("请选择计划类型");
            var model = await _sys_planmanagemenRepository.GetSingle(x => x.F_Id == input.F_Id);
            if (model == null)
                return Error("操作失败");
            //  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";
            model.F_UpdateUser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            var b = await _sys_planmanagemenRepository.Update(input);
            if (b)
                return Success("修改成功");
            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_planmanagemenRepository.GetSingle(x => x.F_Id  == id);
            if (model == null)
            {
                return Error("获取失败");
            }
            model.F_State = state;
            var obj = await _sys_planmanagemenRepository.Update(model);
            if (obj)
                return Success("操作成功");
            else
                return Error("操作失败");
        }
        /// <summary>
        /// 删除媒体
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> Remove(int[] ids,int id)
        {
            if (id <= 0)
                return Error("参数错误");
            var mediacenter = await _sys_mediacenter_Repository.GetSingle(x => x.F_ID == id);
            if (mediacenter == null)
                return Error("媒体错误");
            var res = 0;
            if (ids != null && ids.Length > 0)
            {
                string[] plansprit = mediacenter.F_PlayPlan.Split(",");
                foreach (var item in ids)
                {
                    var model = await _sys_planmanagemenRepository.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_planmanagemenRepository.Update(model).Result)
                        res += 1;
                    plansprit.Remove(item.ToString ());
                }
                string planid = "";
                if (plansprit!=null )
                {
                    foreach (var it in plansprit)
                    {
                        if (planid == "")
                            planid = it;
                        else
                            planid += "," + it;
                    }
                }
                mediacenter.F_PlayPlan = planid;
                var media = _sys_mediacenter_Repository.Update(mediacenter);
                if (res == ids.Length)
                    return Success("删除成功");
                else if (res > 0 && res < ids.Length)
                    return Error("部分删除失败，请查看后重新操作");
                else
                    return Error("删除失败，请查看后重新操作");
            }
            else
                return Error("请选择要删除的记录");
        }
    }
}