SystemController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. 
  2. namespace OASystem.API.Controllers
  3. {
  4. /// <summary>
  5. /// 系统设置
  6. /// </summary>
  7. //[Authorize]
  8. [Route("api/[controller]/[action]")]
  9. public class SystemController : ControllerBase
  10. {
  11. private readonly CompanyRepository _syscomRep;
  12. private readonly DepartmentRepository _sysDepRep;
  13. private readonly UsersRepository _userRep;
  14. private readonly IMapper _mapper;
  15. public SystemController( CompanyRepository syscom,DepartmentRepository sysDepRep, UsersRepository userRep,
  16. IMapper mapper)
  17. {
  18. _syscomRep = syscom;
  19. _sysDepRep = sysDepRep;
  20. _userRep = userRep;
  21. _mapper = mapper;
  22. }
  23. #region user 操作
  24. /// <summary>
  25. /// 查询所有员工(web)
  26. /// </summary>
  27. /// <param name="dto"></param>
  28. /// <returns></returns>
  29. [HttpPost]
  30. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  31. public async Task<IActionResult> GetUserList(DtoBase dto)
  32. {
  33. try
  34. {
  35. var result = _userRep.GetUserList(dto.PortType,string.Empty);
  36. if (result.Result.Code != 0)
  37. {
  38. return Ok(JsonView(false, "暂无数据!"));
  39. }
  40. return Ok(JsonView(true,"查询成功!",result.Result.Data));
  41. }
  42. catch (Exception)
  43. {
  44. return Ok(JsonView(false, "程序错误!"));
  45. throw;
  46. }
  47. }
  48. #endregion
  49. #region 企业操作
  50. /// <summary>
  51. /// 查询企业数据
  52. /// </summary>
  53. /// <param name="dto"></param>
  54. /// <returns></returns>
  55. [HttpPost]
  56. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  57. public async Task<IActionResult> getCompanyList(DtoBase dto)
  58. {
  59. try
  60. {
  61. if (dto.PortType == 1)
  62. {
  63. return Ok(JsonView(false, "暂无数据!"));
  64. }
  65. else if (dto.PortType == 2)
  66. {
  67. var companyList = _sysDepRep.QueryDto<Sys_Company, CompanyView>(a=>a.IsDel!=null).ToList();
  68. if (companyList.Count == 0)
  69. {
  70. return Ok(JsonView(false, "暂无数据!"));
  71. }
  72. return Ok(JsonView(true,"查询成功!",companyList));
  73. }
  74. else if (dto.PortType == 3)
  75. {
  76. return Ok(JsonView(false, "暂无数据!"));
  77. }
  78. else
  79. {
  80. return Ok(JsonView(false, "暂无数据!"));
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. return Ok(JsonView(false, "程序错误!"));
  86. throw;
  87. }
  88. }
  89. /// <summary>
  90. /// 添加企业数据
  91. /// </summary>
  92. /// <param name="dto"></param>
  93. /// <returns></returns>
  94. [HttpPost]
  95. [ProducesResponseType(typeof(JsonView),StatusCodes.Status200OK)]
  96. public async Task<IActionResult> AddCompany(AddCompanyDto dto)
  97. {
  98. try
  99. {
  100. if (string.IsNullOrWhiteSpace(dto.CompanyName) || dto.CreateUserId == 0 || string.IsNullOrWhiteSpace(dto.CompanyCode))
  101. {
  102. return Ok(JsonView(false, "请检查信息是否输入完整!"));
  103. }
  104. else if (string.IsNullOrWhiteSpace(dto.Tel))
  105. {
  106. return Ok(JsonView(false, "请检查联系方式是否输入正确!"));
  107. }
  108. else
  109. {
  110. Sys_Company _Company = _mapper.Map<Sys_Company>(dto);
  111. int id = await _syscomRep.AddAsyncReturnId(_Company);
  112. if (id == 0)
  113. {
  114. return Ok(JsonView(false, "添加失败!"));
  115. }
  116. return Ok(JsonView(true,"添加成功", new { Id = id }));
  117. }
  118. }
  119. catch (Exception)
  120. {
  121. return Ok(JsonView(false, "程序错误!"));
  122. throw;
  123. }
  124. }
  125. /// <summary>
  126. /// 企业修改
  127. /// </summary>
  128. /// <param name="dto"></param>
  129. /// <returns></returns>
  130. [HttpPost]
  131. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  132. public async Task<IActionResult>EditCompany(EditCompanyDto dto)
  133. {
  134. try
  135. {
  136. if (string.IsNullOrWhiteSpace(dto.CompanyName) || string.IsNullOrWhiteSpace(dto.CompanyCode) || string.IsNullOrWhiteSpace(dto.Address) || dto.ParentCompanyId == 0 || dto.ContactUserId == 0)
  137. {
  138. return Ok(JsonView(false, "请检查信息是否输入完整!"));
  139. }
  140. else if (string.IsNullOrWhiteSpace(dto.Tel))
  141. {
  142. return Ok(JsonView(false, "请检查联系方式是否输入正确!"));
  143. }
  144. else
  145. {
  146. bool res = await _syscomRep.UpdateAsync(a => a.Id == dto.Id, a => new Sys_Company
  147. {
  148. CompanyName = dto.CompanyName,
  149. CompanyCode = dto.CompanyCode,
  150. Address = dto.Address,
  151. ParentCompanyId = dto.ParentCompanyId,
  152. Tel = dto.Tel,
  153. ContactUserId = dto.ContactUserId,
  154. });
  155. if (!res) { return Ok(JsonView(false, "修改失败")); }
  156. return Ok(JsonView(true,"修改成功!"));
  157. }
  158. }
  159. catch (Exception)
  160. {
  161. return Ok(JsonView(false, "程序错误!"));
  162. throw;
  163. }
  164. }
  165. /// <summary>
  166. /// 企业删除
  167. /// </summary>
  168. /// <param name="dto"></param>
  169. /// <returns></returns>
  170. [HttpPost]
  171. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  172. public async Task<IActionResult> DelCompany(DelCompanyDto dto)
  173. {
  174. try
  175. {
  176. bool res = await _syscomRep.SoftDeleteAsync<Sys_Company>(dto.Id.ToString());
  177. if (!res) { return Ok(JsonView(false, "删除失败")); }
  178. return Ok(JsonView(true, "删除成功"));
  179. }
  180. catch (Exception)
  181. {
  182. return Ok(JsonView(false, "程序错误!"));
  183. throw;
  184. }
  185. }
  186. #endregion
  187. #region 部门操作
  188. /// <summary>
  189. /// 查询部门数据
  190. /// </summary>
  191. /// <param name="dto"></param>
  192. /// <returns></returns>
  193. //[Authorize]
  194. [HttpPost]
  195. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  196. public async Task<IActionResult> QueryDepartmentList(DepartmentDto dto)
  197. {
  198. try
  199. {
  200. if (dto.PortType==1)
  201. {
  202. return Ok(JsonView(false, "暂无数据!"));
  203. }
  204. else if (dto.PortType==2)
  205. {
  206. var result = _sysDepRep.QueryDto<Sys_Department, DepartmentIView>(s => s.CompanyId == dto.CompanyId).ToList();
  207. if (result.Count == 0)
  208. {
  209. return Ok(JsonView(false, "暂无数据!"));
  210. }
  211. return Ok(JsonView(true,"查询成功!",result));
  212. }
  213. else if (dto.PortType == 3)
  214. {
  215. return Ok(JsonView(false, "暂无数据!"));
  216. }
  217. else
  218. {
  219. return Ok(JsonView(false, "暂无数据!"));
  220. }
  221. }
  222. catch (Exception ex)
  223. {
  224. return Ok(JsonView(false, "程序错误!"));
  225. throw;
  226. }
  227. }
  228. /// <summary>
  229. /// 部门添加
  230. /// </summary>
  231. /// <param name="dto"></param>
  232. /// <returns></returns>
  233. [HttpPost]
  234. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  235. public async Task<IActionResult> AddDepartment(AddDepartmentDto dto)
  236. {
  237. try
  238. {
  239. if (dto.CreateUserId == 0 || string.IsNullOrWhiteSpace(dto.DepName) || dto.CompanyId == 0 || string.IsNullOrWhiteSpace(dto.DepCode))
  240. {
  241. return Ok(JsonView(false, "请检查信息是否输入完整!"));
  242. }
  243. else
  244. {
  245. Sys_Department _Department = _mapper.Map<Sys_Department>(dto);
  246. int id = await _sysDepRep.AddAsyncReturnId(_Department);
  247. if (id == 0)
  248. {
  249. return Ok(JsonView(false, "添加失败!"));
  250. }
  251. return Ok(JsonView(true, "添加成功!", new { Id = id }));
  252. }
  253. }
  254. catch (Exception)
  255. {
  256. return Ok(JsonView(false, "程序错误!"));
  257. throw;
  258. }
  259. }
  260. /// <summary>
  261. /// 部门修改
  262. /// </summary>
  263. /// <param name="dto"></param>
  264. /// <returns></returns>
  265. [HttpPost]
  266. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  267. public async Task<IActionResult> EditDepartment(EditDepartmentDto dto)
  268. {
  269. try
  270. {
  271. if (dto.Id==0 || string.IsNullOrWhiteSpace(dto.DepName) || dto.CompanyId == 0 || string.IsNullOrWhiteSpace(dto.DepCode))
  272. {
  273. return Ok(JsonView(false, "请检查信息是否输入完整!"));
  274. }
  275. else
  276. {
  277. bool res = await _sysDepRep.UpdateAsync<Sys_Department>(a => a.Id == dto.Id, a => new Sys_Department
  278. {
  279. CompanyId=dto.CompanyId,
  280. DepCode=dto.DepCode,
  281. DepName=dto.DepName,
  282. ParentDepId=dto.ParentDepId,
  283. Remark=dto.Remark,
  284. });
  285. if (!res)
  286. {
  287. return Ok(JsonView(false, "修改失败!"));
  288. }
  289. return Ok(JsonView(true, "修改成功!"));
  290. }
  291. }
  292. catch (Exception)
  293. {
  294. return Ok(JsonView(false, "程序错误!"));
  295. throw;
  296. }
  297. }
  298. /// <summary>
  299. /// 部门删除
  300. /// </summary>
  301. /// <param name="dto"></param>
  302. /// <returns></returns>
  303. [HttpPost]
  304. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  305. public async Task<IActionResult> DelDepartment(DelDepartmentDto dto)
  306. {
  307. try
  308. {
  309. if (dto.Id == 0)
  310. {
  311. return Ok(JsonView(-1, "请检查信息是否输入完整!", null));
  312. }
  313. else
  314. {
  315. bool res =await _sysDepRep.SoftDeleteAsync<Sys_Department>(dto.Id.ToString());
  316. if (!res)
  317. {
  318. return Ok(JsonView(false, "删除失败!"));
  319. }
  320. return Ok(JsonView(true, "删除成功!"));
  321. }
  322. }
  323. catch (Exception)
  324. {
  325. return Ok(JsonView(false, "程序错误!"));
  326. throw;
  327. }
  328. }
  329. #endregion
  330. #region 用户操作
  331. /// <summary>
  332. /// 查询用户数据
  333. /// </summary>
  334. /// <param name="dto"></param>
  335. /// <returns></returns>
  336. [HttpPost]
  337. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  338. public async Task<IActionResult> QueryUserList(UserDto dto)
  339. {
  340. try
  341. {
  342. string sqlWhere = string.Empty;
  343. if (dto.CompanyId!=0)
  344. {
  345. sqlWhere += string.Format(@" And su.CompanyId={0}", dto.CompanyId);
  346. }
  347. if (dto.DepId != 0)
  348. {
  349. sqlWhere += string.Format(@" And su.DepId={0}", dto.DepId);
  350. }
  351. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  352. {
  353. Regex r = new Regex("And");
  354. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  355. }
  356. var result=_userRep.GetUserList(dto.PortType,sqlWhere);
  357. if (result.Result.Code!=0)
  358. {
  359. return Ok(JsonView(false, "暂无数据!"));
  360. }
  361. return Ok(JsonView(true,"查询成功!",result.Result.Data));
  362. }
  363. catch (Exception)
  364. {
  365. return Ok(JsonView(false, "程序错误!"));
  366. throw;
  367. }
  368. }
  369. [HttpPost]
  370. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  371. public async Task<IActionResult> AddUser(AddUserDto dto)
  372. {
  373. int id = 0;
  374. return Ok(JsonView(true, "添加成功!", new { Id = id }));
  375. }
  376. #endregion
  377. }
  378. }