using System; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Script.Services; using WebLib; namespace 報到系統 { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [ScriptService] public class MessageAccountManage : System.Web.Services.WebService { /// /// 查詢簡訊帳號密碼 /// [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public BaseResponse Search(int pageNo, int pageSize, string projectID, string platformCode) { try { // 計算起始索引 (pageNo 從 1 開始) int startRowIndex = (pageNo - 1) * pageSize; using (var conn = DAC.NewConnection()) { DAC_ProjectMMSAccount dac = new DAC_ProjectMMSAccount(conn); DAC_BPAA dacBpaa = new DAC_BPAA(conn); // 查詢資料 ProjectMMSAccountList result = dac.SelectPage( noInputReturnAll: true, startRowIndex: startRowIndex, maximumRows: pageSize, ProjectID: projectID, orderBy: "ProjectID ASC", PlatformCode: platformCode ); // 取得總筆數 int totalCount = dac.SelectCount( noInputReturnAll: true, ProjectID: projectID, orderBy: "", PlatformCode: platformCode ); // 補充計畫名稱 foreach (var item in result) { item.ProjectName = dacBpaa.SelectProjectName(item.ProjectID); } return new BaseResponse { Success = true, Message = "查詢成功", Data = result ?? new ProjectMMSAccountList(), TotalCount = totalCount }; } } catch (Exception ex) { return new BaseResponse { Success = false, Message = "查詢簡訊帳號發生錯誤:" + ex.Message, Data = new ProjectMMSAccountList(), TotalCount = 0 }; } } /// /// 儲存簡訊帳號密碼 (新增或修改) /// [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public BaseResponse Save(bool isNew, ProjectMMSAccountItem item) { try { if (item == null) { return new BaseResponse { Success = false, Message = "參數不完整" }; } // 驗證必填欄位 if (string.IsNullOrEmpty(item.ProjectID)) { return new BaseResponse { Success = false, Message = "計畫代號為必填欄位" }; } if (string.IsNullOrEmpty(item.MMSAccount)) { return new BaseResponse { Success = false, Message = "簡訊帳號為必填欄位" }; } if (string.IsNullOrEmpty(item.MMSPassword)) { return new BaseResponse { Success = false, Message = "簡訊密碼為必填欄位" }; } // PlatformCode 長度驗證(可為空,表示使用系統預設) if (!string.IsNullOrEmpty(item.PlatformCode) && item.PlatformCode.Length > 20) { return new BaseResponse { Success = false, Message = "平台代碼最長 20 個字元" }; } // 驗證欄位長度 if (item.ProjectID.Length > 20) { return new BaseResponse { Success = false, Message = "計畫代號最長 20 個字元" }; } if (item.MMSAccount.Length > 50) { return new BaseResponse { Success = false, Message = "簡訊帳號最長 50 個字元" }; } if (item.MMSPassword.Length > 50) { return new BaseResponse { Success = false, Message = "簡訊密碼最長 50 個字元" }; } using (var conn = DAC.NewConnection()) { DAC_ProjectMMSAccount dac = new DAC_ProjectMMSAccount(conn); if (isNew) { // 檢查計畫代號是否已存在 ProjectMMSAccountList existingList = dac.Select(noInputReturnAll: false, ProjectID: item.ProjectID); if (existingList != null && existingList.Count > 0) { return new BaseResponse { Success = false, Message = "該計畫已存在簡訊帳號設定,請使用修改功能" }; } // 新增 ProjectMMSAccountItem newItem = dac.InsertOne(item); return new BaseResponse { Success = true, Message = "新增成功", Data = newItem }; } else { // 修改:檢查計畫代號是否重複(新的計畫代號) if (item.ID > 0) { ProjectMMSAccountList existingList = dac.Select(noInputReturnAll: false, ProjectID: item.ProjectID); if (existingList != null && existingList.Count > 0 && existingList[0].ID != item.ID) { return new BaseResponse { Success = false, Message = "該計畫已被其他記錄使用,無法修改" }; } } // 更新 ProjectMMSAccountItem updatedItem = dac.UpdateOne(item); return new BaseResponse { Success = true, Message = "修改成功", Data = updatedItem }; } } } catch (Exception ex) { return new BaseResponse { Success = false, Message = "儲存簡訊帳號發生錯誤:" + ex.Message }; } } /// /// 刪除簡訊帳號密碼 /// [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public BaseResponse Delete(int id, int dbAppNo) { try { if (id <= 0) { return new BaseResponse { Success = false, Message = "參數不完整" }; } using (var conn = DAC.NewConnection()) { DAC_ProjectMMSAccount dac = new DAC_ProjectMMSAccount(conn); int rowsAffected = dac.DeleteOne(id, dbAppNo); if (rowsAffected > 0) { return new BaseResponse { Success = true, Message = "刪除成功" }; } else { return new BaseResponse { Success = false, Message = "找不到要刪除的資料或資料已被修改" }; } } } catch (Exception ex) { return new BaseResponse { Success = false, Message = "刪除簡訊帳號發生錯誤:" + ex.Message }; } } /// /// 搜尋計畫 - 用於自動完成 /// [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public BaseResponse SearchProjects(string keyword) { try { DAC_BPAA dac = new DAC_BPAA(); var result = dac.Select(keyword); return new BaseResponse { Success = true, Message = "搜尋成功", Data = result ?? new NameValueList() }; } catch (Exception ex) { return new BaseResponse { Success = false, Message = "搜尋計畫發生錯誤:" + ex.Message, Data = new NameValueList() }; } } /// /// 取得所有簡訊平台清單 /// [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public BaseResponse GetPlatforms() { try { using (var conn = DAC.NewConnection()) { var dac = new DAC_MMSPlatform(conn); MMSPlatformList list = dac.SelectAll(); return new BaseResponse { Success = true, Message = "查詢成功", Data = list }; } } catch (Exception ex) { return new BaseResponse { Success = false, Message = "查詢平台發生錯誤:" + ex.Message }; } } /// /// 取得目前系統預設平台 /// [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public BaseResponse GetCurrentPlatform() { try { using (var conn = DAC.NewConnection()) { var dac = new DAC_MMSPlatform(conn); MMSPlatformItem item = dac.SelectByCode(PublicVariable.CurrentMMSPlatform); return new BaseResponse { Success = true, Message = "查詢成功", Data = item }; } } catch (Exception ex) { return new BaseResponse { Success = false, Message = "查詢目前平台發生錯誤:" + ex.Message }; } } /// /// 儲存平台設定(帳號 / 密碼) /// [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public BaseResponse SavePlatform(MMSPlatformItem item) { try { if (item == null || string.IsNullOrEmpty(item.PlatformCode)) return new BaseResponse { Success = false, Message = "參數不完整" }; using (var conn = DAC.NewConnection()) { var dac = new DAC_MMSPlatform(conn); MMSPlatformItem existing = dac.SelectByCode(item.PlatformCode); if (existing == null) return new BaseResponse { Success = false, Message = "找不到指定平台:" + item.PlatformCode }; existing.Account = item.Account; existing.Password = item.Password; existing.Remark = item.Remark; dac.UpdateOne(existing); return new BaseResponse { Success = true, Message = "儲存成功" }; } } catch (Exception ex) { return new BaseResponse { Success = false, Message = "儲存平台設定發生錯誤:" + ex.Message }; } } /// /// 設定目前使用的簡訊平台(更新 BWEX 並同步 PublicVariable) /// [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public BaseResponse SetCurrentPlatform(string platformCode) { try { if (string.IsNullOrEmpty(platformCode)) return new BaseResponse { Success = false, Message = "平台代碼不能為空" }; using (var conn = DAC.NewConnection()) { // 確認平台代碼存在 var dacPlatform = new DAC_MMSPlatform(conn); if (dacPlatform.SelectByCode(platformCode) == null) return new BaseResponse { Success = false, Message = "找不到指定平台:" + platformCode }; // 更新 BWEX var dacBwex = new DAC_BWEX(conn); dacBwex.SetKeyValue("CurrentMMSPlatform", platformCode); } // 同步更新記憶體中的變數 PublicVariable.CurrentMMSPlatform = platformCode; return new BaseResponse { Success = true, Message = "已切換至平台:" + platformCode }; } catch (Exception ex) { return new BaseResponse { Success = false, Message = "設定平台發生錯誤:" + ex.Message }; } } } }