12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- 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<int> 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<int>(exitSql, new { entity.Name });
- if (count > 0)
- {
- return -1;
- }
- return await conn.ExecuteAsync(insertSql, entity);
- }
- }
- public List<DeviceModel> 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<DeviceModel>(selectSql).ToList();
- }
- }
- public async Task<int> 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<int> 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<int>(exitSql, entity);
- if (count > 0)
- {
- return -1;
- }
- return await conn.ExecuteAsync(updateSql, entity);
- }
- }
- }
- }
|