using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
namespace WebAPI.Controllers.DataPenetrate
{
///
/// 单位控制器
///
public class ProjectUnitPenetrateController : ApiController
{
#region 查询
///
/// 根据项目id查询单位信息
///
///
///
[HttpGet]
public Model.ResponeData getInfo(string projectIds="")
{
string projectId = projectIds;
var responeData = new Model.ResponeData();
if (string.IsNullOrEmpty(projectId))
{
responeData.code = 0;
responeData.message = "项目id不能为空";
return responeData;
}
try
{
using (Model.SUBQHSEDB db = new Model.SUBQHSEDB(Funs.ConnString))
{
var units = (from x in db.Base_Unit
orderby x.UnitCode
select x).ToList();
var units2 = (from x in units
join y in db.Project_ProjectUnit on x.UnitId equals y.UnitId
where y.ProjectId == projectId
select x).OrderBy(x => x.UnitCode).Select(s=> new Base_Unit_New() {
UnitId=s.UnitId,
UnitName=s.UnitName
}).ToList();
responeData.data = new { units2 };
if (units2.Count == 0)
{
responeData.code = 1;
responeData.message = "根据条件未查询到数据。";
return responeData;
}
else {
responeData.message = "查询成功。";
}
}
}
catch (Exception ex)
{
responeData.code = 0;
responeData.message = ex.Message;
}
return responeData;
}
private class Base_Unit_New {
public string UnitId { get; set; }
public string UnitName { get; set; }
}
#endregion
#region 保存
///
/// 保存单位
///
///
///
public ResponeData saveUnit(Base_Unit model)
{
var responeData = new Model.ResponeData();
try
{
#region 判断
if (string.IsNullOrEmpty(model.UnitCode))
{
responeData.code = 0;
responeData.message = "单位代码(UnitCode)不能为空。";
return responeData;
}
if (string.IsNullOrEmpty(model.UnitName))
{
responeData.code = 0;
responeData.message = "单位名称(UnitName)不能为空。";
return responeData;
}
if (string.IsNullOrEmpty(model.DataSources))
{
responeData.code = 0;
responeData.message = "项目id(DataSources)不能为空。";
return responeData;
}
else {
//判断projectid是否有数据
var pmodel = Funs.DB.Base_Project.FirstOrDefault(x => x.ProjectId == model.DataSources);
if (pmodel == null)
{
responeData.code = 0;
responeData.message = "项目id(DataSources)未查询到数据,请检查是否正确。";
return responeData;
}
}
#endregion
if (string.IsNullOrEmpty(model.UnitId))
{
model.UnitId = SQLHelper.GetNewID(typeof(Model.Base_Unit));
BLL.UnitService.AddUnit(model);
Model.Project_ProjectUnit newProjectUnit = new Model.Project_ProjectUnit
{
ProjectId = model.DataSources,
UnitId = model.UnitId,
InTime = System.DateTime.Now
};
BLL.ProjectUnitService.AddProjectUnit(newProjectUnit);
responeData.message = "保存成功!";
}
else {
//判断userid是否有数据
var umodel = Funs.DB.Base_Unit.FirstOrDefault(x => x.UnitId == model.UnitId);
if (umodel == null)
{
responeData.code = 0;
responeData.message = "单位id(UnitId)未查询到数据,无法修改,请检查是否正确。";
return responeData;
}
Model.Base_Unit oldUnit = BLL.UnitService.GetUnitByUnitId(model.UnitId);
if (oldUnit != null)
{
model.IsThisUnit = oldUnit.IsThisUnit;
}
BLL.UnitService.UpdateUnit(model);
responeData.message = "修改成功!";
}
}
catch (Exception ex)
{
responeData.code = 0;
responeData.message = ex.Message;
}
return responeData;
}
#endregion
}
}