using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Ropin.Inspection.Api.Common; using Ropin.Inspection.Model; using Ropin.Inspection.Model.ViewModel; using Ropin.Inspection.Service; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Ropin.Inspection.Api.Controllers { public class TpntStoreController : BaseController { private readonly ILogger _logger; private readonly ITpntStoreService _TpntStoreService; private readonly ITpntStoreOrgService _TpntStoreOrgService; /// /// 构造函数 /// /// /// /// public TpntStoreController(ITpntStoreService TpntStoreService, ITpntStoreOrgService TpntStoreOrgService, ILogger logger) { _TpntStoreService = TpntStoreService; _TpntStoreOrgService = TpntStoreOrgService; _logger = logger; } /// /// 通过ID获取网点信息 /// /// /// [HttpGet("GetStoreAsync/{id}")] public async Task GetStoreAsync(Guid id) { if (Guid.Empty == id) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TpntStoreService.GetByIdAsync(id); return new ApiResult(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 获取所有网点 /// /// [HttpGet("GetStoresAsync")] public async Task GetStoresAsync() { try { var contentList = await _TpntStoreService.GetAllAsync(); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过网点名称条件查询 /// /// /// [HttpPost("GetStoresByAsync")] public async Task GetStoresByAsync(TpntStoreSearchModel searchModel) { if (searchModel == null) { return new ApiResult(ReturnCode.ArgsError); } searchModel.IsPagination = false; try { var contentList = await _TpntStoreService.GetConditionAsync(searchModel); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 创建网点 /// /// /// [HttpPost("CreateStoreAsync")] public async Task CreateStoreAsync(TpntStoreViewModel content) { if (content == null) { return new ApiResult(ReturnCode.ArgsError); } try { await _TpntStoreService.CreateOneAsync(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 创建组织架构下网点 /// /// /// /// [HttpPost("CreateStoreByOrgAsync/{orgCode}")] public async Task CreateStoreByOrgAsync(Guid orgCode, TpntStoreCreateModel content) { if (content == null ||Guid.Empty == orgCode) { return new ApiResult(ReturnCode.ArgsError); } try { await _TpntStoreService.CreateStoreByOrgAsync(orgCode,content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 分配组织架构下网点 /// /// /// /// [HttpPost("AllotStoreByOrgAsync/{orgCode}/{storeCode}")] public async Task AllotStoreByOrgAsync(Guid orgCode, Guid storeCode) { if (Guid.Empty == storeCode || Guid.Empty == orgCode) { return new ApiResult(ReturnCode.ArgsError); } try { await _TpntStoreService.AllotStoreByOrgAsync(orgCode, storeCode); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 删除网点 /// /// /// [HttpDelete("DeleteStoreAsync/{id}")] public async Task DeleteStoreAsync(Guid id) { if (Guid.Empty == id) { return new ApiResult(ReturnCode.GeneralError); } try { await _TpntStoreService.DeleteAsync(id); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 更新网点 /// /// /// /// [HttpPut("UpdateStoreAsync/{id}")] public async Task UpdateStoreAsync(Guid id, TpntStoreUpdateModel updateModel) { if (Guid.Empty == id) { return new ApiResult(ReturnCode.GeneralError); } try { await _TpntStoreService.UpdateAsync(id, updateModel); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 为组织架构批量分配网点 /// /// /// /// [HttpPost("CreateOrgStoresAsync/{orgCode}")] public async Task CreateOrgStoresAsync(Guid orgCode, IEnumerable storeCodes) { if (Guid.Empty.Equals(orgCode) || storeCodes == null || !storeCodes.Any()) { return new ApiResult(ReturnCode.ArgsError); } try { await _TpntStoreOrgService.CreateOrgStoresAsync(orgCode, storeCodes); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 创建组织架构下网点 /// /// /// [HttpPost("CreateStoreOrgAsync")] public async Task CreateStoreOrgAsync(TpntStoreOrgViewModel mode) { if (mode == null || Guid.Empty.Equals(mode.C_OrgCode) || string.Empty.Equals(mode.C_StoreCode)) { return new ApiResult(ReturnCode.ArgsError); } try { TpntStoreSearchModel searchModel = new TpntStoreSearchModel(); searchModel.orgCode = mode.C_OrgCode; IEnumerable stores = await _TpntStoreOrgService.GetStoresByOrgCodeAsync(searchModel); List listStores = stores.ToList(); if (listStores[0] != null && listStores.FirstOrDefault(i => i.C_Code == mode.C_StoreCode) != null) return new ApiResult(ReturnCode.GeneralError, "此组织架构下已有此网点"); await _TpntStoreOrgService.CreateOneAsync(mode); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 删除组织架构下网点 /// /// /// /// [HttpDelete("DeleteByOrgStoreCodeAsync/{orgCode}/{storeCode}")] public async Task DeleteByOrgStoreCodeAsync(Guid orgCode,string storeCode) { if (Guid.Empty.Equals(orgCode)|| string.IsNullOrEmpty(storeCode)) { return new ApiResult(ReturnCode.GeneralError); } try { await _TpntStoreOrgService.DeleteByOrgStoreCodeAsync(orgCode, storeCode); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 通过网点Code获取关联的组织架构 /// /// /// [HttpGet("GetOrgsByStoreCodeAsync/{storeCode}")] public async Task GetOrgsByStoreCodeAsync(string storeCode) { if (string.IsNullOrEmpty(storeCode)) { return new ApiResult(ReturnCode.GeneralError); } try { var contentList = await _TpntStoreOrgService.GetOrgsByStoreCodeAsync(storeCode); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过点击组织机构树查看下面的网点 /// /// /// [HttpGet("GetStoresByOrgCodeAsync/{orgCode}")] public async Task GetStoresByOrgCodeAsync(Guid orgCode) { if (Guid.Empty == orgCode) { return new ApiResult(ReturnCode.GeneralError); } try { TpntStoreSearchModel searchModel = new TpntStoreSearchModel(); searchModel.orgCode = orgCode; var contentList = await _TpntStoreOrgService.GetStoresByOrgCodeAsync(searchModel); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过点击组织机构树查看下面的网点 /// /// [HttpPost("GetStoresByOrgCodeNameAsync")] public async Task GetStoresByOrgCodeNameAsync(TpntStoreSearchModel searchModel) { if (searchModel==null||Guid.Empty == searchModel.orgCode) { return new ApiResult(ReturnCode.GeneralError); } try { var contentList = await _TpntStoreOrgService.GetStoresByOrgCodeAsync(searchModel); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过组织架构Code获取关联的网点,管理公司可以查看所有 /// /// /// [HttpGet("GetStoresOnlyByOrgCodeAsync/{orgCode}")] public async Task GetStoresOnlyByOrgCodeAsync(Guid orgCode) { if (Guid.Empty == orgCode) { return new ApiResult(ReturnCode.GeneralError); } try { var contentList = await _TpntStoreOrgService.GetStoresOnlyByOrgCodeAsync(orgCode); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 获取所有的PNT类型 /// /// [HttpGet("GetAllPntType")] public async Task GetAllPntType() { try { var contentList = await _TpntStoreService.GetAllPntType(); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过网点code获取网点信息 /// /// /// [HttpGet("GetStoreByCodeAsync/{code}")] public async Task GetStoreByCodeAsync(string code) { if (string.IsNullOrEmpty(code)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TpntStoreService.GetStoreByCodeAsync(code); return new ApiResult(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } } }