| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- using System;
- using System.Collections.Generic;
- using System.Common;
- using System.IRepositories;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Model;
- using System.Security.Claims;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using SqlSugar;
- namespace TVShoppingCallCenter_ZLJ.Controllers.System
- {
- [Authorize]
- [Produces("application/json")]
- [Route("api/[controller]")]
- public class PositionController : BaseController
- {
- private readonly ISys_PositionRepository _sys_positionRepository;
- public PositionController(ISys_PositionRepository sys_positionRepository)
- {
- _sys_positionRepository = sys_positionRepository;
- }
- /// <summary>
- /// 获取列表
- /// </summary>
- /// <param name="filter"></param>
- /// <returns></returns>
- [Authorize]
- [HttpGet("getlistbypage")]
- public async Task<IActionResult> GetListsByPage(string key, int pageindex = 1, int pagesize = 20)
- {
- List<IConditionalModel> conModels = new List<IConditionalModel>();
- #region 条件筛选
- conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
- if (!string.IsNullOrEmpty(key))
- {
- conModels.Add(new ConditionalCollections()
- {
- ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
- {
- new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = key }),
- new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Des", ConditionalType = ConditionalType.Like, FieldValue = key })
- }
- });
- }
- #endregion
- int recordCount = 0;
- var list = await _sys_positionRepository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount }, "F_CreateOn desc");
- return Success("成功", list);
- }
- [Authorize]
- [HttpGet("getlistdrop")]
- public async Task<IActionResult> GetListDrop(string key)
- {
- List<IConditionalModel> conModels = new List<IConditionalModel>();
- #region 条件筛选
- conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
- if (!string.IsNullOrEmpty(key))
- {
- conModels.Add(new ConditionalCollections()
- {
- ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
- {
- new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = key }),
- new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Des", ConditionalType = ConditionalType.Like, FieldValue = key })
- }
- });
- }
- #endregion
- var list = await _sys_positionRepository.GetListALL(conModels, "F_CreateOn desc");
- var result = list.Select(x => new {
- x.F_Id,
- x.F_Name,
- x.F_Des
- });
- return Success("获取全部数据成功", result);
- }
- /// <summary>
- /// 获取实体
- /// </summary>
- [Authorize]
- [HttpGet("getdetails")]
- public async Task<IActionResult> GetDetails(int id)
- {
- if (id > 0)
- {
- var dModel = await _sys_positionRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
- if (dModel != null)
- {
- return Success("获取成功", dModel);
- }
- else
- {
- return Error("获取失败");
- }
- }
- else
- {
- return Error("获取参数失败");
- }
- }
- /// <summary>
- /// 添加
- /// </summary>
- [Authorize]
- [HttpPost("add")]
- public async Task<IActionResult> Add(string name,string des)
- {
- #region 验证
- if (string.IsNullOrWhiteSpace(name))
- return Error("职位名称不能为空");
- if (await GetExistByNameAsync(name, 0))
- return Error("职位名称不能重复,请重新输入!");
- #endregion
- T_Sys_Position clmodel = new T_Sys_Position();
- clmodel.F_Name = name;
- clmodel.F_Des = des;
- clmodel.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
- clmodel.F_CreateOn = DateTime.Now;
- clmodel.F_State = (int)EnumDelState.Enabled;
- var res = await _sys_positionRepository.Add(clmodel);
- if (res > 0)
- return Success("保存成功");
- else
- {
- return Error("保存失败");
- }
- }
- /// <summary>
- /// 修改
- /// </summary>
- [Authorize]
- [HttpPost("update")]
- public async Task<IActionResult> Edit(string name, string des,int id)
- {
- #region
- if (id <= 0)
- return Error("请选择要编辑的");
- if (string.IsNullOrWhiteSpace(name))
- return Error("职位名称不能为空");
- if (await GetExistByNameAsync(name, id))
- return Error("职位名称不能重复,请重新输入!");
- #endregion
- var clmodel = await _sys_positionRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
- if (clmodel == null)
- return Error("信息获取失败");
- clmodel.F_Name = name;
- clmodel.F_Des = des;
- clmodel.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- clmodel.F_LastModifyOn = DateTime.Now;
- var res = await _sys_positionRepository.Update(clmodel);
- if (res)
- return Success("保存成功");
- else
- {
- return Error("保存失败");
- }
- }
- [Authorize]
- [HttpPost("delete")]
- public async Task<IActionResult> Remove(int[] ids)
- {
- //使用逻辑删除
- //物理删除的数据无法恢复
- var res = 0;
- if (ids != null && ids.Length > 0)
- {
- foreach (var item in ids)
- {
- var ml = await _sys_positionRepository.GetSingle(x => x.F_Id == item);
- ml.F_State = (int)EnumDelState.Delete;
- if (await _sys_positionRepository.Update(ml))
- res += 1;
- }
- if (res == ids.Length)
- return Success("删除成功");
- else if (res > 0 && res < ids.Length)
- return Error("部分删除失败,请查看后重新操作");
- else
- return Error("删除失败,请查看后重新操作");
- }
- else
- return Error("请选择要删除的记录");
- }
- #region 私有方法
- /// <summary>
- /// 根据名称查询是否存在此职位
- /// </summary>
- /// <returns></returns>
- private async Task<bool> GetExistByNameAsync(string name, int id)
- {
- long c = 0;
- Expression<Func<T_Sys_Position, bool>> eq = a => a.F_State == (int)EnumDelState.Enabled;
- eq = eq.And(b => b.F_Name.Equals(name));
- if (id > 0)
- eq = eq.And(b => b.F_Id != id);
- c = await _sys_positionRepository.GetCount(eq);
- return c > 0;
- }
- #endregion
- }
- }
|