| | |
| | | |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using Microsoft.Data.SqlClient; |
| | | using NewPdaSqlServer.DB; |
| | | using NewPdaSqlServer.Dto.service; |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | public string ApproveUser(OaToMesUser user) |
| | | { |
| | | var _strMsg = ""; |
| | | if (user == null || user.dataJson == null || user.dataJson.Count == 0) |
| | | throw new Exception("dataJson不能为空且必须包含至少一条记录"); |
| | | |
| | | int insertCount = 0; |
| | | int updateCount = 0; |
| | | using (var conn = new SqlConnection(DbHelperSQL.strConn)) |
| | | { |
| | | conn.Open(); |
| | | using (var tran = conn.BeginTransaction()) |
| | | { |
| | | try |
| | | { |
| | | foreach (var item in user.dataJson) |
| | | { |
| | | // 参数校验增强 |
| | | if (string.IsNullOrEmpty(item.Id)) throw new Exception("用户ID不能为空"); |
| | | if (string.IsNullOrEmpty(item.Name)) throw new Exception("用户姓名不能为空"); |
| | | |
| | | // 检查是否存在 |
| | | var checkSql = @"SELECT COUNT(1) FROM OA_USER WHERE Id = @Id"; |
| | | using (var checkCmd = new SqlCommand(checkSql, conn, tran)) |
| | | { |
| | | checkCmd.Parameters.AddWithValue("@Id", item.Id); |
| | | var exists = (int)checkCmd.ExecuteScalar() > 0; |
| | | |
| | | if (exists) |
| | | { |
| | | // 更新 |
| | | var updateSql = @"UPDATE OA_USER SET NAME = @Name, JOBNUMBER = @Jobnumber, DEPARTMENT = @Department WHERE Id = @Id"; |
| | | using (var updateCmd = new SqlCommand(updateSql, conn, tran)) |
| | | { |
| | | updateCmd.Parameters.AddWithValue("@Id", item.Id); |
| | | updateCmd.Parameters.AddWithValue("@Name", item.Name ?? (object)DBNull.Value); |
| | | updateCmd.Parameters.AddWithValue("@Jobnumber", item.Jobnumber ?? (object)DBNull.Value); |
| | | updateCmd.Parameters.AddWithValue("@Department", item.Department ?? (object)DBNull.Value); |
| | | int rows = updateCmd.ExecuteNonQuery(); |
| | | if (rows > 0) updateCount++; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | // 插入 |
| | | var insertSql = @"INSERT INTO OA_USER (Id, NAME, JOBNUMBER, DEPARTMENT) VALUES (@Id, @Name, @Jobnumber, @Department)"; |
| | | using (var insertCmd = new SqlCommand(insertSql, conn, tran)) |
| | | { |
| | | insertCmd.Parameters.AddWithValue("@Id", item.Id); |
| | | insertCmd.Parameters.AddWithValue("@Name", item.Name ?? (object)DBNull.Value); |
| | | insertCmd.Parameters.AddWithValue("@Jobnumber", item.Jobnumber ?? (object)DBNull.Value); |
| | | insertCmd.Parameters.AddWithValue("@Department", item.Department ?? (object)DBNull.Value); |
| | | int rows = insertCmd.ExecuteNonQuery(); |
| | | if (rows > 0) insertCount++; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | tran.Commit(); |
| | | return $"用户数据同步成功!插入{insertCount}条,更新{updateCount}条记录。"; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | tran.Rollback(); |
| | | _strMsg = $"用户数据同步异常:{ex.Message}"; |
| | | throw new Exception(_strMsg); |
| | | } |
| | | finally |
| | | { |
| | | conn.Close(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | public string ApproveDepartment(OaToMesDepartment department) |
| | | { |
| | | var _strMsg = ""; |
| | | if (department == null || department.dataJson == null || department.dataJson.Count == 0) |
| | | throw new Exception("dataJson不能为空且必须包含至少一条记录"); |
| | | |
| | | int insertCount = 0; |
| | | int updateCount = 0; |
| | | using (var conn = new SqlConnection(DbHelperSQL.strConn)) |
| | | { |
| | | conn.Open(); |
| | | using (var tran = conn.BeginTransaction()) |
| | | { |
| | | try |
| | | { |
| | | foreach (var item in department.dataJson) |
| | | { |
| | | // 参数校验增强 |
| | | if (string.IsNullOrEmpty(item.Id)) throw new Exception("部门ID不能为空"); |
| | | if (string.IsNullOrEmpty(item.Name)) throw new Exception("部门名称不能为空"); |
| | | |
| | | // 检查是否存在 |
| | | var checkSql = @"SELECT COUNT(1) FROM OA_DEPARTMENT WHERE id = @Id"; |
| | | using (var checkCmd = new SqlCommand(checkSql, conn, tran)) |
| | | { |
| | | checkCmd.Parameters.AddWithValue("@Id", item.Id); |
| | | var exists = (int)checkCmd.ExecuteScalar() > 0; |
| | | |
| | | if (exists) |
| | | { |
| | | // 更新 |
| | | var updateSql = @"UPDATE OA_DEPARTMENT SET name = @Name, Parentid = @Parentid WHERE Id = @Id"; |
| | | using (var updateCmd = new SqlCommand(updateSql, conn, tran)) |
| | | { |
| | | updateCmd.Parameters.AddWithValue("@Id", item.Id); |
| | | updateCmd.Parameters.AddWithValue("@Name", item.Name ?? (object)DBNull.Value); |
| | | updateCmd.Parameters.AddWithValue("@Parentid", item.Parentid ?? (object)DBNull.Value); |
| | | int rows = updateCmd.ExecuteNonQuery(); |
| | | if (rows > 0) updateCount++; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | // 插入 |
| | | var insertSql = @"INSERT INTO OA_DEPARTMENT (id, name, Parentid) VALUES (@Id, @Name, @Parentid)"; |
| | | using (var insertCmd = new SqlCommand(insertSql, conn, tran)) |
| | | { |
| | | insertCmd.Parameters.AddWithValue("@Id", item.Id); |
| | | insertCmd.Parameters.AddWithValue("@Name", item.Name ?? (object)DBNull.Value); |
| | | insertCmd.Parameters.AddWithValue("@Parentid", item.Parentid ?? (object)DBNull.Value); |
| | | int rows = insertCmd.ExecuteNonQuery(); |
| | | if (rows > 0) insertCount++; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | tran.Commit(); |
| | | return $"部门数据同步成功!插入{insertCount}条,更新{updateCount}条记录。"; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | tran.Rollback(); |
| | | _strMsg = $"部门数据同步异常:{ex.Message}"; |
| | | throw new Exception(_strMsg); |
| | | } |
| | | finally |
| | | { |
| | | conn.Close(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |