SGGL_JT/SUBQHSE/WebAPI/Controllers/DataPenetrate/ProjectUnitPenetrateControl...

160 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 = "项目idDataSources不能为空。";
return responeData;
}
else {
//判断projectid是否有数据
var pmodel = Funs.DB.Base_Project.FirstOrDefault(x => x.ProjectId == model.DataSources);
if (pmodel == null)
{
responeData.code = 0;
responeData.message = "项目idDataSources未查询到数据请检查是否正确。";
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
}
}