南骏 池
8 天以前 4910c0fa81d93635e19a57c073c3a62c76053320
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using NewPdaSqlServer.DB;
using NewPdaSqlServer.Dto.service;
using System.Data;
 
namespace NewPdaSqlServer.service.JJGZ
{
    public class OaToMesRecordManager : Repository<OaToMesRecord>
    {
        public string ApproveRecord(OaToMesRecord record)
        {
            var _strMsg = "";
            if (record == null || record.dataJson == null || record.dataJson.Count == 0)
                throw new Exception("dataJson不能为空且必须包含至少一条记录");
 
            int successCount = 0;
            using (var conn = new SqlConnection(DbHelperSQL.strConn))
            {
                conn.Open();
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        foreach (var item in record.dataJson)
                        {
                            // 参数校验增强
                            if (string.IsNullOrEmpty(item.StaffId)) throw new Exception("员工编号不能为空");
                            if (string.IsNullOrEmpty(item.LineNo)) throw new Exception("产线不能为空");
                            if (string.IsNullOrEmpty(item.Classes)) throw new Exception("班次不能为空");
                            if (item.CheckTime == null) throw new Exception("打卡日期不能为空");
 
                            var sql = @"INSERT INTO MES_JJGZ_RECORD (GUID, Staff_Id, Staff_No, Staff_Name, Line_No, Line_Name, CheckTime, ScheNo,RecordDate,OrgId) VALUES (NEWID(), @StaffId, @StaffNo, @StaffName, @LineNo, @LineName, @CheckTime, @Classes, getdate(),@OrgId)";
                            using (var cmd = new SqlCommand(sql, conn, tran))
                            {
                                cmd.Parameters.AddWithValue("@StaffId", item.StaffId ?? (object)DBNull.Value);
                                cmd.Parameters.AddWithValue("@StaffNo", item.StaffNo ?? (object)DBNull.Value);
                                cmd.Parameters.AddWithValue("@StaffName", item.StaffName ?? (object)DBNull.Value);
                                cmd.Parameters.AddWithValue("@LineNo", item.LineNo ?? (object)DBNull.Value);
                                cmd.Parameters.AddWithValue("@LineName", item.LineName ?? (object)DBNull.Value);
                                cmd.Parameters.AddWithValue("@CheckTime", item.CheckTime ?? (object)DBNull.Value);
                                cmd.Parameters.AddWithValue("@Classes", item.Classes ?? (object)DBNull.Value);
                                cmd.Parameters.AddWithValue("@OrgId", item.OrgId ?? (object)DBNull.Value);
                                int rows = cmd.ExecuteNonQuery();
                                if (rows > 0) successCount++;
                            }
                        }
                        tran.Commit();
                        return $"批量插入MES_JJGZ_RECORD成功!共插入{successCount}条记录。";
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        _strMsg = $"批量插入MES_JJGZ_RECORD异常:{ex.Message}";
                        throw new Exception(_strMsg);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }
    }
}