/*
 * 后台角色
 */

using System;
using System.Web;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using MySystem.Models.Bs;
using Library;
using LitJson;
using Microsoft.AspNetCore.Authorization;
using MySystem.Service.Bs;
using System.Linq;

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

        #region 系统管理-角色管理-角色列表
        [Authorize]
        public JsonResult SysAdminRoleList(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = SysAdminRoleListDo(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        private List<Dictionary<string, object>> SysAdminRoleListDo(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            string Name = data["Name"].ToString(); //角色名称
            int pageSize = int.Parse(function.CheckInt(data["page_size"].ToString()));
            int pageNum = int.Parse(function.CheckInt(data["page_num"].ToString()));
            List<RelationData> relationData = new List<RelationData>();
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            string condition = "";
            if (!string.IsNullOrEmpty(data["Name"].ToString()))
            {
                condition += " and Name like '%" + Name + "%'";
            }
            Other = new Dictionary<string, object>();
            int count = 0;
            List<Dictionary<string, object>> source = SysAdminRoleService.List(relationData, condition, out count, 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
                curData.Add("Name", subdata["Name"].ToString()); //角色名称
                curData.Add("Details", subdata["Details"].ToString()); //描述
                curData.Add("RightInfo", subdata["RightInfo"].ToString()); //权限字符串
                dataList.Add(curData);
            }
            Other.Add("Count", count); //总数
            return dataList;
        }
        #endregion


        #region 系统管理-角色管理-添加系统角色信息
        [Authorize]
        public JsonResult AddSysAdminRoleInfo(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = AddSysAdminRoleInfoDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson AddSysAdminRoleInfoDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string Name = data["Name"].ToString(); //角色名称
            string Details = data["Details"].ToString(); //详情
            string RightInfo = data["RightInfo"].ToString(); //权限字符串
            Dictionary<string, object> fields = new Dictionary<string, object>();
            fields.Add("Name", Name); //角色名称
            fields.Add("Details", Details); //详情
            fields.Add("RightInfo", RightInfo); //权限字符串
            var Id = int.Parse(SysAdminRoleService.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 EditSysAdminRoleInfo(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = EditSysAdminRoleInfoDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson EditSysAdminRoleInfoDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int SysAdminRoleId = int.Parse(data["SysAdminRoleId"].ToString()); //角色Id
            string Name = data["Name"].ToString(); //角色名称
            string Details = data["Details"].ToString(); //详情
            string RightInfo = data["RightInfo"].ToString(); //权限字符串
            var sysAdminRole = bsdb.SysAdminRole.FirstOrDefault(m => m.Id == SysAdminRoleId) ?? new SysAdminRole();
            if (sysAdminRole.Id > 0)
            {
                if (!string.IsNullOrEmpty(Name))
                {
                    sysAdminRole.Name = Name;
                }
                if (!string.IsNullOrEmpty(Details))
                {
                    sysAdminRole.Details = Details;
                }
                if (!string.IsNullOrEmpty(RightInfo))
                {
                    sysAdminRole.RightInfo = RightInfo;
                }
            }
            bsdb.SaveChanges();
            return new AppResultJson() { Status = "1", Info = "成功" };
        }
        #endregion


        #region 系统管理-角色管理-删除系统角色信息
        [Authorize]
        public JsonResult DeleteSysAdminRoleInfo(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = DeleteSysAdminRoleInfoDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson DeleteSysAdminRoleInfoDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int SysAdminRoleId = int.Parse(data["SysAdminRoleId"].ToString()); //角色Id
            SysAdminRoleService.Delete(SysAdminRoleId);
            return new AppResultJson() { Status = "1", Info = "成功" };
        }
        #endregion

    }
}