ServiceSide类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using db_models;
namespace ServiceSide
{
public class ServiceSide
{
const int jhh_foot_len= 5;
/// <summary>
/// 登录
/// </summary>
/// <param name="yhm"></param>
/// <param name="password"></param>
/// <returns></returns>
public ResultFormat Login(string yhm,string password)
{
SD.Data.IRepositoryBase _rs = new SD.Data.RepositoryBase("SDDbContext");
try
{
var user = _rs.IQueryable<Yh>(o => o.yhm == yhm && o.password == password).SingleOrDefault();
if (user==null)
{
return setResult.Fail("账号或密码错误");
}
else
{
return setResult.Success(user,"登陆成功");
}
}
catch (Exception ex)
{
return setResult.Fail("登陆失败");
}
}
/// <summary>
/// 获取磅房
/// </summary>
/// <returns></returns>
public ResultFormat GetBF()
{
SD.Data.IRepositoryBase _rs = new SD.Data.RepositoryBase("SDDbContext");
try
{
var bf = _rs.IQueryable<Bf>().ToList();
return setResult.Success(bf);
}
catch (Exception ex)
{
return setResult.Fail("获取磅房失败");
}
}
/// <summary>
/// 获取品名
/// </summary>
/// <returns></returns>
public ResultFormat GetPm()
{
SD.Data.IRepositoryBase _rs = new SD.Data.RepositoryBase("SDDbContext");
try
{
var pm = _rs.IQueryable<Pm>().ToList();
return setResult.Success(pm);
}
catch (Exception ex)
{
return setResult.Fail("获取品名失败");
}
}
/// <summary>
/// 获取一次计量数据
/// </summary>
/// <returns></returns>
public ResultFormat Get_FirstData(string ch)
{
SD.Data.IRepositoryBase _rs = new SD.Data.RepositoryBase("SDDbContext");
try
{
string sql = string.Format("select czxh,ch,pmm,ly,qx,pzdd,pzscy,pzsj,pz,kz,state from V_gb_data " +
"where ch='{0}' and getdate()<dateadd(hh,24,pzsj) and state=1 and mz is null",ch);
var first = _rs.IQueryable<GB_First_Data>(sql).FirstOrDefault();
return setResult.Success(first);
}
catch (Exception ex)
{
return setResult.Fail("获取一次计量数据失败");
}
}
/// <summary>
/// 设置过磅数据
/// </summary>
/// <param name="jhh"></param>
/// <returns></returns>
public ResultFormat Set_Gb_Data(string jhh,string ch,string pmm,string ly,string qx,string zl,string ycjl_data,string ycjl_time,string kz,string ddbm,string scybm,string jz)
{
SD.Data.IRepositoryBase _rs = new SD.Data.RepositoryBase("SDDbContext");
try
{
//一次计量
if (String.IsNullOrEmpty(jhh))
{
GB_Data data = new GB_Data();
data.czxh = Get_Czch();
data.pmm = pmm;
data.ch = ch;
data.ly = ly;
data.qx = qx;
data.pzddbm = ddbm;
data.pzscybm = scybm;
data.pzsj = DateTime.Now;
data.pz = double.Parse(zl);
data.kz = double.Parse(kz);
data.state = true;
_rs.Insert(data);
}
else
{
//二次计量
if (DateTime.Now>=DateTime.Parse(ycjl_time).AddHours(24))
{
//一次计量数据无效
GB_Data data = new GB_Data();
data.czxh = Get_Czch();
data.pmm = pmm;
data.ch = ch;
data.ly = ly;
data.qx = qx;
data.pzddbm = ddbm;
data.pzscybm = scybm;
data.pzsj = DateTime.Now;
data.pz = double.Parse(zl);
data.kz = double.Parse(kz);
_rs.Insert(data);
}
else
{
var gb_data = Get_Gb_Data(jhh);
if (gb_data==null)
{
return setResult.Fail("操作失败");
}
else
{
gb_data.pmm = pmm;
gb_data.ch = ch;
gb_data.ly = ly;
gb_data.qx = qx;
gb_data.mz = double.Parse(zl);
gb_data.kz = double.Parse(kz);
gb_data.mzddbm = ddbm;
gb_data.mzscybm = scybm;
gb_data.mzsj = DateTime.Now;
gb_data.jz = double.Parse(jz);
_rs.Update(gb_data);
}
}
}
return setResult.Success("操作成功");
}
catch (Exception ex)
{
return setResult.Fail("操作失败");
}
}
/// <summary>
/// 一次计量无效
/// </summary>
/// <param name="jhh"></param>
/// <returns></returns>
public ResultFormat Set_Gb_State(string yhm,string password,string jhh)
{
SD.Data.IRepositoryBase _rs = new SD.Data.RepositoryBase("SDDbContext");
try
{
var user = _rs.IQueryable<Yh>(o => o.yhm == yhm && o.password == password).SingleOrDefault();
if (user!=null)
{
var data = _rs.IQueryable<GB_Data>(o => o.czxh == jhh).FirstOrDefault();
if (data != null)
{
data.state = false;
data.sx_scybm = user.ID;
data.sx_time = DateTime.Now;
_rs.Update(data);
return setResult.Success("操作成功");
}
else
{
return setResult.Fail("操作失败");
}
}
else
{
return setResult.Fail("用户不存在");
}
}
catch (Exception ex)
{
return setResult.Fail("操作失败");
}
}
/// <summary>
/// 查询过磅数据
/// </summary>
/// <param name="ch"></param>
/// <param name="pm"></param>
/// <param name="ly"></param>
/// <param name="qx"></param>
/// <param name="ycgb_time"></param>
/// <param name="ecgb_time"></param>
/// <returns></returns>
public ResultFormat Get_Gb_Data(string ch,string pmm,string ly,string qx,string ycgb_time,string ecgb_time)
{
SD.Data.IRepositoryBase _rs = new SD.Data.RepositoryBase("SDDbContext");
try
{
string sql = string.Format("select czxh,ch,pmms,ly,qx,pzdd,pzscy,pzsj,pz,kz," +
"mzdd,mzscy,mzsj,jz,(case when state=1 then '失效' end) as state,sx_scy,sx_time from V_gb_data " +
"where 1=1");
if (ch!="")
{
sql += string.Format(" and ch='{0}'", ch);
}
if (pmm!="-1")
{
sql += string.Format(" and pmm='{0}'", pmm);
}
if (ly!="")
{
sql += string.Format(" and ly like '%{0}%'", ly);
}
if (qx!="")
{
sql += string.Format(" and qx like '%{0}%'", qx);
}
if (ycgb_time!="")
{
sql += string.Format(" and CONVERT(varchar(10),pzsj,120)='{0}'", ycgb_time);
}
if (ecgb_time!="")
{
sql += string.Format(" and (CONVERT(varchar(10),mzsj,120)='{0}' or mzsj is null)", ecgb_time);
}
var ds = _rs.GetDataSet(sql);
return setResult.Success(ds, "获取数据成功");
}
catch (Exception ex)
{
return setResult.Fail("获取数据失败");
}
}
/// <summary>
/// 获取称重车号
/// </summary>
/// <returns></returns>
private string Get_Czch()
{
SD.Data.IRepositoryBase _rs = new SD.Data.RepositoryBase("SDDbContext");
string jhh = DateTime.Now.ToString("yyyyMMdd");
string sql = string.Format("select * from [gb_data] where SUBSTRING(czxh,0,9)='{0}'", jhh);
try
{
int count = _rs.IQueryable<GB_Data>(sql).Count()+1;
var len = count.ToString().Length;
for (int i = 0; i < jhh_foot_len-len; i++)
{
jhh += "0";
}
jhh += count;
return jhh;
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 获取称重数据
/// </summary>
/// <param name="jhh"></param>
/// <returns></returns>
private GB_Data Get_Gb_Data(string jhh)
{
SD.Data.IRepositoryBase _rs = new SD.Data.RepositoryBase("SDDbContext");
try
{
var data = _rs.FindEntity<GB_Data>(o => o.czxh == jhh);
return data;
}
catch (Exception ex)
{
return null;
}
}
}
}