username@email.com
2021-12-09 d14b82fec13361486c49165371b5dee1b7089c09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using DTO;
using IServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using zhengcaioa.IService;
using zhengcaioa.Models;
 
namespace zhengcaioa.Controllers
{
    public class PltUserRoleController : Controller
    {
        private readonly ILogger<PltUserRoleController> _logger;
        private readonly IPltRoleService _pltRoleService;
        private readonly IUserService _userService;
        private readonly ISysCodeService _sysCodeService;
        
 
 
        public PltUserRoleController(ILogger<PltUserRoleController> logger, IPltRoleService pltRoleService, IUserService userService, ISysCodeService sysCodeService)
        {
            _logger = logger;
            _pltRoleService = pltRoleService;
            _userService = userService;
            _sysCodeService = sysCodeService;
        }
 
        [CheckLogin]
        public IActionResult Index()
        {
            #region 获取所有角色
            var listRole = _pltRoleService.listRole();
            ViewBag.listRole = listRole;
            #endregion
            #region 获取所有人员
            var listUser = _userService.GetList();
            var listType = _sysCodeService.GetCodeDataAll().Where(a=>a.CodeTable == "plt_user" && a.CodeField == "user_type").ToList();
            var dicTypeUser = new Dictionary<string, List<PltUserDTO>>();
            if (listType.Count > 0)
            {
                listType.ForEach(t => {
                    dicTypeUser[t.CodeSn] = listUser.Where(u => u.UserType == t.CodeSn).ToList();
                });
 
            }
            ViewBag.listType = listType;
            ViewBag.dicTypeUser = dicTypeUser;
            #endregion
            #region 获取所有人员角色配置
            var listUserRole = _pltRoleService.GetList();
            var dicUserRole = new Dictionary<string, List<string>>();
            if (listRole.Count > 0)
            {
                listRole.ForEach(r => {
                    dicUserRole[r.Id] = listUserRole.Where(u => u.RoleId == r.Id).ToList().Select(u => u.UserId).ToList();
                });
            }
            ViewBag.dicUserRole = dicUserRole;
            #endregion
            return View();
        }
 
        [CheckLogin]
        public string Save(string roleid = "", string usersid = "")
        {
            var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;
            var result = new ResultEntity();
            #region 数据验证
            if (string.IsNullOrWhiteSpace(roleid))
            {
                result.Result = false;
                result.Message = "请选择角色";
                return JsonConvert.SerializeObject(result);
            }
            if (string.IsNullOrWhiteSpace(usersid))
            {
                result.Result = false;
                result.Message = "请选择人员";
                return JsonConvert.SerializeObject(result);
            }
            #endregion
            return JsonConvert.SerializeObject(_pltRoleService.SaveMoreEntity(roleid, usersid, curentuser.Id));
        }
    }
}