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;

// 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 ScoreController : BaseController
    {
        private readonly ICus_ScoreSetRepository _cus_score_setRepository;
        

        public ScoreController(ICus_ScoreSetRepository cus_score_setRepository)
        {
            _cus_score_setRepository = cus_score_setRepository;
            
        }

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

        /// <summary>
        /// 获取积分规则
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet("getrule")]
        public async Task<IActionResult> GetRuleAsync()
        {
            var model = await _cus_score_setRepository.GetSingle(x => x.F_State  == 1);
            if (model == null)
            {
                return Error("获取失败");
            }
            return Success("获取成功！", model);
        }

        /// <summary>
        /// 添加积分规则
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> AddAsync(int start,int getpercent,int getday,int startdebit,int debitpercent)
        {
            
            var model = new T_Cus_ScoreSet();
            model.F_Start = start ;
            model.F_GetPercent = getpercent;
            model.F_GetDay = getday;
            model.F_StartDebit = startdebit;
            model.F_DebitPercent = debitpercent;
            model.F_State = 1;
           
            model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            model.F_CreateOn = DateTime.Now;

            //if (await _cus_score_setRepository.GetCount(x => x.F_Name == input.name) > 0)
            //{
            //    return Error("添加失败，存在相同标签信息");
            //}
            List<T_Cus_ScoreSet> listmodel = await _cus_score_setRepository.GetListALL(a => a.F_State ==1);


            //此处如果不是批量删除，最好事务
            for (int i = 0; i < listmodel.Count; i++)
            {
                listmodel[i].F_State = 0;
            }

            await _cus_score_setRepository.UpdateList(listmodel);//自动禁用之前的信息            
            var res = await _cus_score_setRepository.Add(model);
            if (res > 0)
            {
                return Success("添加成功");
            }
            else
            {
                return Error("添加失败");
            }
            
        }
    }
}
