SystemController.cs 13 KB

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