using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;
using System.Web;
using MySystem.Models.Main1;
using MySystem.Service.Main1;
using LitJson;
using Library;
using MySystem.Service.KxsMain;
using MySystem.Service.Bs;

namespace MySystem.Areas.Api.Controllers.v1.Main1
{
    [Area("Api")]
    [Route("/v1/QrCodePlateMain/[controller]/[action]")]
    public class UserSwapWhiteController : BaseController
    {
        public UserSwapWhiteController(IHttpContextAccessor accessor) : base(accessor)
        {
        }


        #region 风险管理-风险创客列表
        [Authorize]
        public JsonResult RiskMakerList(string value)
        {
            value = PublicFunction.DesDecrypt(value); ;
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = RiskMakerListDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        private List<Dictionary<string, object>> RiskMakerListDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string MakerCode = data["MakerCode"].ToString(); //风控创客编号
            int pageSize = int.Parse(function.CheckInt(data["page_size"].ToString()));
            int pageNum = int.Parse(function.CheckInt(data["page_num"].ToString()));
            string condition = "";
            if (!string.IsNullOrEmpty(data["MakerCode"].ToString()))
            {
                var user = UsersService.QueryByMakerCode(MakerCode);
                condition += " and UserId=" + user.Id + "";
            }
            List<RelationData> relationData = new List<RelationData>();
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            List<Dictionary<string, object>> source = UserSwapWhiteService.List(relationData, condition, pageNum, pageSize);
            foreach (Dictionary<string, object> subdata in source)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("Id", int.Parse(subdata["Id"].ToString())); //记录Id
                var userInfo = UsersService.Query(int.Parse(subdata["UserId"].ToString()));
                curData.Add("MakerCode", userInfo.MakerCode); //风控创客编号
                curData.Add("RealName", userInfo.RealName); //风控创客姓名
                curData.Add("Remark", subdata["Remark"].ToString()); //备注
                curData.Add("CreateDate", subdata["CreateDate"].ToString() == "" ? "" : DateTime.Parse(subdata["CreateDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss")); //操作时间
                curData.Add("Operator", subdata["CreateMan"].ToString()); //操作人
                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

        #region 风险管理-添加风险创客
        [Authorize]
        public JsonResult AddRiskMaker(string value)
        {
            value = PublicFunction.DesDecrypt(value); ;
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = AddRiskMakerDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson AddRiskMakerDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string MakerCode = data["MakerCode"].ToString(); //风控创客编号
            string RealName = data["RealName"].ToString(); //风控创客姓名
            string Remark = data["Remark"].ToString(); //备注
            if (string.IsNullOrEmpty(data["MakerCode"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请输入风控创客编号" };
            }
            if (string.IsNullOrEmpty(data["RealName"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请输入风控创客姓名" };
            }
            var user = UsersService.QueryByMakerCode(MakerCode);
            if (user.Id == 0)
            {
                return new AppResultJson() { Status = "-1", Info = "未找到相关创客信息" };
            }
            if (user.MakerCode != MakerCode || user.RealName != RealName)
            {
                return new AppResultJson() { Status = "-1", Info = "输入的创客信息不匹配" };
            }
            Dictionary<string, object> fields = new Dictionary<string, object>();
            var sys = SysAdminService.Query(AppConfig.LoginSession.sysId);
            fields.Add("UserId", user.Id); //创客Id
            fields.Add("CreateMan", sys.AdminName + "_" + sys.RealName); //操作人
            fields.Add("Remark", Remark); //备注
            var Id = int.Parse(UserSwapWhiteService.Add(fields).Data.ToString());
            if (Id > 0)
            {
                return new AppResultJson() { Status = "1", Info = "成功", Data = Id };
            }
            else
            {
                return new AppResultJson() { Status = "-1", Info = "失败", Data = Id };
            }
        }
        #endregion


        #region 风险管理-删除风险创客
        [Authorize]
        public JsonResult DeleteRiskMaker(string value)
        {
            value = PublicFunction.DesDecrypt(value); ;
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = DeleteRiskMakerDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson DeleteRiskMakerDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int RiskId = int.Parse(data["RiskId"].ToString()); //风控记录Id
            UserSwapWhiteService.Delete(RiskId);
            return new AppResultJson() { Status = "1", Info = "删除成功" };
        }
        #endregion

    }
}
