206 lines
7.7 KiB
C#
206 lines
7.7 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// 单位控制器
|
||
/// </summary>
|
||
public class ProjectUnitPenetrateController : ApiController
|
||
{
|
||
#region 查询
|
||
/// <summary>
|
||
/// 根据项目id查询单位信息
|
||
/// </summary>
|
||
/// <param name="projectIds"></param>
|
||
/// <returns></returns>
|
||
[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 保存
|
||
/// <summary>
|
||
/// 保存单位
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
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
|
||
|
||
#region 根据ProjectUnitId获取信息
|
||
/// <summary>
|
||
/// 根据ProjectUnitId获取信息
|
||
/// </summary>
|
||
/// <param name="ProjectUnitId"></param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
public Model.ResponeData getDataByProjectUnitId(string ProjectUnitId)
|
||
{
|
||
var responeData = new Model.ResponeData();
|
||
try
|
||
{
|
||
var query = (from projectUnit in Funs.DB.Project_ProjectUnit
|
||
join project in Funs.DB.Base_Project
|
||
on projectUnit.ProjectId equals project.ProjectId into projectGroup
|
||
from project in projectGroup.DefaultIfEmpty()
|
||
join unit in Funs.DB.Base_Unit
|
||
on projectUnit.UnitId equals unit.UnitId into unitGroup
|
||
from unit in unitGroup.DefaultIfEmpty()
|
||
join sysConst in Funs.DB.Sys_Const
|
||
on new { GroupId = BLL.ConstValue.Group_ProjectUnitType, ConstValue = projectUnit.UnitType }
|
||
equals new { GroupId = sysConst.GroupId, ConstValue = sysConst.ConstValue } into sysConstGroup
|
||
from sysConst in sysConstGroup.DefaultIfEmpty()
|
||
where projectUnit.ProjectUnitId == ProjectUnitId
|
||
select new
|
||
{
|
||
ProjectUnitId = projectUnit.ProjectUnitId,
|
||
ProjectId = projectUnit.ProjectId,
|
||
ProjectName = project.ProjectName,
|
||
UnitId = projectUnit.UnitId,
|
||
UnitName = unit.UnitName,
|
||
}).FirstOrDefault();
|
||
responeData.data = new { query };
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
responeData.code = 0;
|
||
responeData.message = ex.Message;
|
||
}
|
||
|
||
return responeData;
|
||
}
|
||
|
||
|
||
#endregion
|
||
}
|
||
} |