using GCAS.Model; using Dapper; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; namespace GCAS.Code { public class DeviceRepository { public async Task Insert(DeviceModel entity) { string exitSql = "Select count(1) from device where name=@Name"; string insertSql = "INSERT INTO device (ip,name,port,time,code,tareweight)VALUES(@ip,@name,@port,NOW(),@Code,@TareWeight)"; using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { var count = await conn.QueryFirstAsync(exitSql, new { entity.Name }); if (count > 0) { return -1; } return await conn.ExecuteAsync(insertSql, entity); } } public List GetList() { string selectSql = "SELECT device.id,device.ip,device.name,device.port,device.time,device.code,device.tareweight FROM device order by id asc"; using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { return conn.Query(selectSql).ToList(); } } public async Task Delete(int id) { string deleteSql = "Delete from device where id=@Id"; using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { return await Task.Run(() => conn.ExecuteAsync(deleteSql, new { Id = id })); } } public async Task Update(DeviceModel entity) { string exitSql = "Select count(1) from device where name=@Name and id<>@Id"; string updateSql = "UPDATE device SET device.ip=@Ip,device.name=@Name,device.port=@Port,device.code=@Code,device.tareweight=@TareWeight WHERE id=@Id "; using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { var count = await conn.QueryFirstAsync(exitSql, entity); if (count > 0) { return -1; } return await conn.ExecuteAsync(updateSql, entity); } } } }