using Ant.Data;
using Ant.ORM;
using Ant.Service.Common;
using Ant.Service.Common.Enums;
using Central.Control.Domain;
using MES.Production.Entity;
using MES.Production.Entity.Entity;
using MES.Production.Entity.Enum;
using MES.Production.Service.IService;
using MES.Production.Service.ServiceImp;
using Ant.Service.Utility;
using ChangFa.Machinery.WebPage.Controllers;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JsonHelper = Ant.Service.Common.JsonHelper;
using Ant.SuperSocket.Common;
namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
{
public class DistributorsController : BaseController
{
///
/// 字典编码
///
ICodeManage CodeManage = new CodeManage();
///
/// 部门
///
IDepartmentManage DepartmentManage = new DepartmentManage();
///
/// 用户部门
///
IUserDepartmentManage UserDepartmentManage = new UserDepartmentManage();
///
/// 岗位人员
///
IPostUserManage PostUserManage { get; set; }
///
/// 岗位部门
///
IPostDepartmentManage PostDepartmentManage = new PostDepartmentManage();
///
/// 加载列表
///
///
[UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "View")]
public ActionResult Index()
{
try
{
#region 处理查询参数
string posttype = Request.QueryString["posttype"];
string FK_DPTID = Request.QueryString["FK_DPTID"];
#endregion
#region 加载列表
var result = BindList(posttype, FK_DPTID, CurrentUser.USERTYPE);
ViewBag.Search = base.keywords;
// ViewData["PostType"] = this.CodeManage.GetCode("POSTTYPE");
//ViewData["post"] = posttype;
ViewData["Department"] = this.DepartmentManage.GetDepartmentByDetail();
ViewData["dptid"] = FK_DPTID;
#endregion
if (result.IsNull())
{
return View();
}
else
{
return View(result);
}
}
catch (Exception e)
{
WriteLog(enumOperator.Select, "加载岗位列表:", e);
throw e.InnerException;
}
}
///
/// 导出经销商excel
///
///
[HttpGet]
[UserAuthorizeAttribute(ModuleAlias = "SalesDealersList", OperaAction = "Export")]
public FileResult AllDistributorExcel()
{
//获取list数据
List list = new List();
//创建Excel文件的对象
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); string distributorsID = string.Empty; string CarSate = Request.QueryString["CarSate"];
List namelist = new List();
//添加一个sheet
NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
//给sheet1添加第一行的头部标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
row1.CreateCell(0).SetCellValue("经销商类型");
row1.CreateCell(1).SetCellValue("经销商编号");
row1.CreateCell(2).SetCellValue("经销商名称");
row1.CreateCell(3).SetCellValue("所在省");
row1.CreateCell(4).SetCellValue("所在市");
row1.CreateCell(5).SetCellValue("所在区");
row1.CreateCell(6).SetCellValue("经销商地址");
row1.CreateCell(7).SetCellValue("电子围栏范围");
row1.CreateCell(8).SetCellValue("经纬度");
row1.CreateCell(9).SetCellValue("经销商手机号");
row1.CreateCell(10).SetCellValue("经销商联系人");
row1.CreateCell(11).SetCellValue("销售产品");
row1.CreateCell(12).SetCellValue("轮拖轮式收条线联系人");
row1.CreateCell(13).SetCellValue("轮拖轮式收条线手机号");
row1.CreateCell(14).SetCellValue("履带插秧条线联系人");
row1.CreateCell(15).SetCellValue("履带插秧条线手机号");
row1.CreateCell(16).SetCellValue("玉米收花生机条线联系人");
row1.CreateCell(17).SetCellValue("玉米收花生机条线手机号");
//将数据逐步写入sheet1各个行
if (!Request.QueryString["distributorsid"].IsEmpty())
{
distributorsID = Request.QueryString["distributorsid"].ToString();
}
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
};
var q = orm.Queryable();
if (!Request.QueryString["hkprovid"].IsEmpty())
{
int provid = Request.QueryString["hkprovid"].ToInt32();
q = q.Where(p => p.Province == provid);
}
if (!Request.QueryString["hkcityid"].IsEmpty())
{
int provid = Request.QueryString["hkcityid"].ToInt32();
q = q.Where(p => p.City == provid);
}
q = q.Where(p => p.BUSINESSLEVEL > 1);
var reslutd = q.ToList(res);
if (reslutd.IsSuccess)
{
var modellist = reslutd.ResultModel; int i = 0;
foreach (EntSYS_DISTRIBUTORS md in modellist)
{
i++; int j = 0;
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i);
string saletype = (md.BUSINESSLEVEL == 2) ? "一级经销商" : "经销商仓库";
rowtemp.CreateCell(j).SetCellValue(saletype);
j++;
rowtemp.CreateCell(j).SetCellValue(md.DealerNumber);
j++;
rowtemp.CreateCell(j).SetCellValue(md.NAME);
j++;
string Province = ""; string pro = md.Province.ToString();
var resluts = orm.Queryable().Where(p => p.ID == pro).FirstOrDefault(res);
if (resluts.IsSuccess)
{
MES.Production.Entity.SYS_CODE_AREA mod = resluts.ResultModel;
Province = mod.NAME;
}
rowtemp.CreateCell(j).SetCellValue(Province);//省
j++;
string City = ""; pro = md.Province.ToString();
resluts = orm.Queryable().Where(p => p.ID == pro).FirstOrDefault(res);
if (resluts.IsSuccess)
{
MES.Production.Entity.SYS_CODE_AREA mod = resluts.ResultModel;
City = mod.NAME;
}
rowtemp.CreateCell(j).SetCellValue(City);//市
j++;
string Area = ""; pro = md.Province.ToString();
resluts = orm.Queryable().Where(p => p.ID == pro).FirstOrDefault(res);
if (resluts.IsSuccess)
{
MES.Production.Entity.SYS_CODE_AREA mod = resluts.ResultModel;
Area = mod.NAME;
}
rowtemp.CreateCell(j).SetCellValue(Area);//区
j++;
rowtemp.CreateCell(j).SetCellValue(md.Address);//经销商地址
j++;
rowtemp.CreateCell(j).SetCellValue(md.FenceRadius);//围栏半径
j++;
rowtemp.CreateCell(j).SetCellValue(md.Theodolite);//围栏经纬度
j++;
resluts = orm.Queryable().Where(p => p.DPTID == md.ID).FirstOrDefault(res);//经销商负责人和手机
if (resluts.IsSuccess)
{
En_SYS_USER usermod = resluts.ResultModel;
rowtemp.CreateCell(j).SetCellValue(usermod.NAME);
j++;
rowtemp.CreateCell(j).SetCellValue(usermod.PHONE);
j++;
}
else
{
rowtemp.CreateCell(j).SetCellValue("");
j++;
rowtemp.CreateCell(j).SetCellValue("");
j++;
}
rowtemp.CreateCell(j).SetCellValue("");
j++;
var reslut = orm.Queryable().Where(p => p.distributorId == md.ID).FirstOrDefault(res);//产品线负责人和手机
if (reslut.IsSuccess)
{
string strid = string.Empty; string strname = string.Empty;
EntYW_UserDistributor mod = reslut.ResultModel;
int userid = mod.userId.ToInt32();
var userreslut = orm.Queryable().Where(p => p.ID == userid).FirstOrDefault(res);
if (userreslut.IsSuccess)
{
En_SYS_USER usermod = userreslut.ResultModel;
if (mod.ProductLine == 601)
{
rowtemp.CreateCell(j).SetCellValue(usermod.NAME);
j++;
rowtemp.CreateCell(j).SetCellValue(usermod.PHONE);
j++;
}
else
{
rowtemp.CreateCell(j).SetCellValue("");
j++;
rowtemp.CreateCell(j).SetCellValue("");
j++;
}
if (mod.ProductLine2 == 612)
{
rowtemp.CreateCell(j).SetCellValue(usermod.NAME);
j++;
rowtemp.CreateCell(j).SetCellValue(usermod.PHONE);
j++;
}
else
{
rowtemp.CreateCell(j).SetCellValue("");
j++;
rowtemp.CreateCell(j).SetCellValue("");
j++;
}
if (mod.ProductLine4 == 622)
{
rowtemp.CreateCell(j).SetCellValue(usermod.NAME);
j++;
rowtemp.CreateCell(j).SetCellValue(usermod.PHONE);
j++;
}
else
{
rowtemp.CreateCell(j).SetCellValue("");
j++;
rowtemp.CreateCell(j).SetCellValue("");
j++;
}
}
#region 不用
//foreach (EntYW_UserDistributor mod in reslut.ResultModel)
//{
// int userid = mod.userId.ToInt32();
// var userreslut = orm.Queryable().Where(p => p.ID == userid).FirstOrDefault(res);
// if (userreslut.IsSuccess)
// {
// En_SYS_USER usermod = userreslut.ResultModel;
// if (mod.ProductLine == 601)
// {
// rowtemp.CreateCell(j).SetCellValue(usermod.NAME);
// j++;
// rowtemp.CreateCell(j).SetCellValue(usermod.PHONE);
// j++;
// }
// else
// {
// rowtemp.CreateCell(j).SetCellValue("");
// j++;
// rowtemp.CreateCell(j).SetCellValue("");
// j++;
// }
// if (mod.ProductLine2 == 612)
// {
// rowtemp.CreateCell(j).SetCellValue(usermod.NAME);
// j++;
// rowtemp.CreateCell(j).SetCellValue(usermod.PHONE);
// j++;
// }
// else
// {
// rowtemp.CreateCell(j).SetCellValue("");
// j++;
// rowtemp.CreateCell(j).SetCellValue("");
// j++;
// }
// if (mod.ProductLine4 == 622)
// {
// rowtemp.CreateCell(j).SetCellValue(usermod.NAME);
// j++;
// rowtemp.CreateCell(j).SetCellValue(usermod.PHONE);
// j++;
// }
// else
// {
// rowtemp.CreateCell(j).SetCellValue("");
// j++;
// rowtemp.CreateCell(j).SetCellValue("");
// j++;
// }
// }
//}
#endregion
}
else
{
rowtemp.CreateCell(j).SetCellValue("");
j++;
rowtemp.CreateCell(j).SetCellValue("");
j++;
rowtemp.CreateCell(j).SetCellValue("");
j++;
rowtemp.CreateCell(j).SetCellValue("");
j++;
rowtemp.CreateCell(j).SetCellValue("");
j++;
rowtemp.CreateCell(j).SetCellValue("");
j++;
}
}
}
}
if (namelist.Count > 0)
{
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
};
var q = orm.Queryable();
if (!CarSate.IsEmpty())
{
int state = CarSate.ToInt32();
q = q.Where(p => namelist.Contains(p.DistributorsID) && p.CarSate == state);
}
else
{
q = q.Where(p => namelist.Contains(p.DistributorsID));
}
var reslut = q.ToList(res);
if (reslut.IsSuccess)
{
List modlist = reslut.ResultModel;
for (int i = 0; i < modlist.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
rowtemp.CreateCell(0).SetCellValue(modlist[i].CarID);
rowtemp.CreateCell(1).SetCellValue(modlist[i].CarName);
rowtemp.CreateCell(2).SetCellValue(modlist[i].carBar);
rowtemp.CreateCell(3).SetCellValue(modlist[i].DistributorsName);
string carstatestr = "";
switch (modlist[i].CarSate)
{
case 0:
carstatestr = "未绑定"; break;
case 1:
carstatestr = "已绑定"; break;
case 2:
carstatestr = "已出库"; break;
case 3:
carstatestr = "在途中"; break;
case 4:
carstatestr = "已入库"; break;
case 5:
carstatestr = "已销售"; break;
case 6:
carstatestr = "退货中"; break;
case 7:
carstatestr = "调拨中"; break;
case 8:
carstatestr = "返厂中"; break;
}
rowtemp.CreateCell(4).SetCellValue(carstatestr);
rowtemp.CreateCell(5).SetCellValue(modlist[i].productionDate.ToString());
//rowtemp.CreateCell(5).SetCellValue(list[i].iName);
//rowtemp.CreateCell(6).SetCellValue(list[i].iPhone);
//rowtemp.CreateCell(7).SetCellValue(list[i].IntegralSum);
//rowtemp.CreateCell(8).SetCellValue(list[i].IntegralSy);
//rowtemp.CreateCell(9).SetCellValue(list[i].IntegralKy);
}
}
}
}
// 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application/vnd.ms-excel", "经销商库存信息.xls");
}
///
/// 获取经纬度地址
///
///
///
public ActionResult getLatLntAdderss(string latlntstr)
{
var json = new JsonHelper() { Status = "y", Msg = "Success" };
if (latlntstr.IndexOf(',') > 0)
{
try
{
var lng = latlntstr.Split(',')[0]; var lat = latlntstr.Split(',')[1];
var resaddress = ToolGPSamap.GPSToPositionString(lng.ToDou(), lat.ToDou());
json.Data = resaddress;
}
catch (Exception ex)
{
json.Status = "n";
json.Msg = "经纬度必须用英文的逗分隔开,经度在前,纬度在后!";
}
}
else
{
json.Status = "n";
json.Msg = "经纬度必须用英文的逗分隔开,经度在前,纬度在后!";
}
return Json(json);
}
///
/// 加载详情页
///
[HttpGet]
[UserAuthorizeAttribute(ModuleAlias = "SalesDealersList", OperaAction = "Detail")]
public ActionResult Detail(string id)
{
try
{
string filePathName = string.Empty;
DistributorUser distributorUser = new DistributorUser();
SYS_DISTRIBUTORS entity = new SYS_DISTRIBUTORS();
int uid = 0;
if (CurrentUser.USERTYPE == 2)
{
uid = CurrentUser.Id;
ViewData["usertype"] = CurrentUser.USERTYPE.ToString();
}
else
{
ViewData["usertype"] = "0";
}
ViewBag.moduleparent = this.DepartmentManage.GetDepartmentByDetailNew(uid);
//添加子部门
string parentId = Request.QueryString["parentId"];
if (!string.IsNullOrEmpty(parentId))
{
//entity.PARENTID = parentId;
distributorUser.distributorInfo.PARENTID = parentId;
return View(distributorUser);
}
if (!string.IsNullOrEmpty(id))
{
entity = this.DepartmentManage.Get(p => p.ID == id);
distributorUser.distributorInfo = entity;
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
};
if (CurrentUser.USERTYPE == 2)
{
int userid = CurrentUser.Id;
var reslut = orm.Queryable().Where(p => p.distributorId == id && p.userId == userid).FirstOrDefault(res);
if (reslut.IsSuccess)
{
EntYW_UserDistributor mod = reslut.ResultModel;
if (mod.ProductLine == 601)
{
ViewData["line1"] = true;
}
if (mod.ProductLine == 615)
{
ViewData["line2"] = true;
}
if (mod.ProductLine == 612)
{
ViewData["line3"] = true;
}
if (mod.ProductLine == 611)
{
ViewData["line4"] = true;
}
if (mod.ProductLine == 622)
{
ViewData["line5"] = true;
}
if (mod.ProductLine == 628)
{
ViewData["line6"] = true;
}
}
}
else
{
//IQuery q = orm.Queryable();
//IGroupingQuery group = q.Where(a => a.distributorId == id).GroupBy(a => a.userId);
//var re = group.Select(a => new { a.userId }).ToList(res);
//if (re.IsSuccess)
//{
// string strid = string.Empty; string strname = string.Empty;
// foreach (var mod in re.ResultModel)
// {
// int userid = mod.userId;
// var userreslut = orm.Queryable().Where(p => p.ID == userid).FirstOrDefault(res);
// if (userreslut.IsSuccess)
// {
// En_SYS_USER usermod = userreslut.ResultModel;
// if (strid.IsEmpty())
// {
// strid += usermod.ID;
// strname += usermod.NAME;
// }
// else
// {
// strid += ";" + usermod.ID;
// strname += ";" + usermod.NAME;
// }
// }
// }
// ViewData["userid"] = strid + "|" + strname;
//}
}
var usereslut = orm.Queryable().Where(p => p.DPTID == entity.ID).FirstOrDefault(res);
if (usereslut.IsSuccess)
{
distributorUser.userInfo = usereslut.ResultModel;
}
}
}
else
{
distributorUser.distributorInfo = new SYS_DISTRIBUTORS();
distributorUser.distributorInfo.PARENTID = "32f7a4bd-84de-4587-be29-734d65ad6f70";
distributorUser.userInfo = new En_SYS_USER() { ISCANLOGIN = false };
}
// ViewData["line1"] = true;
return View(distributorUser);
}
catch (Exception e)
{
WriteLog(enumOperator.Select, "经销商管理加载详情页:", e);
throw e.InnerException;
}
}
///
/// 保存部门
///
[ValidateInput(false)]
[UserAuthorizeAttribute(ModuleAlias = "SalesDealersList", OperaAction = "Add,Edit")]
public ActionResult Save(DistributorUser entity)
{
bool isEdit = false; bool bz = true;
var fileid = Request.Form["hidfileid"];
var fileurl = Request.Form["hidfilename"];
var paretid = Request.Form["PARENTID"];
int userid = 0;
var citystr = Request.Form["hkprov"] + "," + Request.Form["hkcity"] + "," + Request.Form["hkcountry"];
var json = new JsonHelper() { Msg = "保存成功", Status = "n" };
//先取消电子围栏
if (entity.distributorInfo.Theodolite.IsEmpty())
{
json.Msg = "经纬度不能为空";
return Json(json);
}
if (entity.distributorInfo.Theodolite.IndexOf(',') <= 0)
{
json.Msg = "经纬度必须用英文的逗分隔开,经度在前,纬度在后!";
return Json(json);
}
if (Request.Form["hkprov"].IsEmpty())
{
json.Msg = "所属省不能为空!";
return Json(json);
}
if (Request.Form["hkcity"].IsEmpty())
{
json.Msg = "所属市不能为空!";
return Json(json);
}
if (Request.Form["hkcountry"].IsEmpty())
{
json.Msg = "所属区不能为空!";
return Json(json);
}
var lng = entity.distributorInfo.Theodolite.Split(',')[0]; var lat = entity.distributorInfo.Theodolite.Split(',')[1];
var resaddress = ToolGPSamap.GPSToPositionString(lng.ToDou(), lat.ToDou());
if (resaddress.IsEmpty())
{
json.Msg = "经纬度不正确!";
return Json(json);
}
try
{
var parententity = new SYS_DISTRIBUTORS();
if (!string.IsNullOrEmpty(paretid))
{
parententity = this.DepartmentManage.Get(p => p.ID == paretid);
}
var _entity = new SYS_DISTRIBUTORS(); var _userenty = new En_SYS_USER(); //当前经销商和用户
string guid = Guid.NewGuid().ToString();
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
};
if (entity.distributorInfo != null)
{
if (!string.IsNullOrEmpty(entity.distributorInfo.ID))
{
guid = entity.distributorInfo.ID;
// var disreult = orm.DeleteByLambda(p => p.distributorId == entity.distributorInfo.ID, res);
}
else
{
guid = Guid.NewGuid().ToString();
}
}
else
{
guid = Guid.NewGuid().ToString();
}
}
if (entity.distributorInfo != null)
{
if (!string.IsNullOrEmpty(entity.distributorInfo.ID))
{
#region 修改
_entity = this.DepartmentManage.Get(p => p.ID == entity.distributorInfo.ID);
entity.distributorInfo.CREATEDATE = _entity.CREATEDATE;
entity.distributorInfo.CREATEPERID = _entity.CREATEPERID;
entity.distributorInfo.PARENTID = paretid;
entity.distributorInfo.UPDATEDATE = DateTime.Now;
entity.distributorInfo.UPDATEUSER = this.CurrentUser.Name;
entity.distributorInfo.Address = Request.Form["distributorInfo.Address"];
entity.distributorInfo.FenceRadius = Request.Form["example_length"].ToInt32();
entity.distributorInfo.LICENSEURL = fileurl;
entity.distributorInfo.LICENSEVALUE = fileid;
entity.distributorInfo.ProvinceCity = citystr;
entity.distributorInfo.Province = Request.Form["hkprov"].ToInt32();
entity.distributorInfo.City = Request.Form["hkcity"].ToInt32();
entity.distributorInfo.Area = Request.Form["hkcountry"].ToInt32();
if (entity.distributorInfo.PARENTID != _entity.PARENTID)
{
entity.distributorInfo.CODE = this.DepartmentManage.CreateCode(entity.distributorInfo.PARENTID);
}
else
{
entity.distributorInfo.CODE = _entity.CODE;
}
isEdit = true;
//获取父级记录
if (string.IsNullOrEmpty(paretid))
{
//业务等级
entity.distributorInfo.BUSINESSLEVEL = 1;
entity.distributorInfo.PARENTCODE = null;
}
else
{
entity.distributorInfo.BUSINESSLEVEL = parententity.BUSINESSLEVEL + 1;
entity.distributorInfo.PARENTCODE = parententity.CODE;
entity.distributorInfo.ParentName = parententity.NAME;
if (entity.distributorInfo.BUSINESSLEVEL == 2)
{
_userenty = entity.userInfo;
_userenty.ACCOUNT = _userenty.PHONE;
_userenty.USERTYPE = 3;
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
};
var resuser = orm.UpdateById(_userenty, res);
}
}
else
{
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
};
var disreult = orm.Queryable().Where(p => p.distributorId == paretid).ToList();
foreach (EntYW_UserDistributor usermod in disreult.ResultModel)
{
var count = orm.Queryable().Where(p => p.distributorId == usermod.distributorId && p.userId == usermod.userId).Count();
if (count == 0)
{
EntYW_UserDistributor entdis = new EntYW_UserDistributor();
entdis.userId = usermod.userId;
entdis.distributorId = guid;
entdis.ID = Guid.NewGuid();
var reslutdis = orm.Save(entdis, res);
}
}
}
}
}
#endregion
_entity = entity.distributorInfo;
}
else
{
if (entity.distributorInfo.PARENTID == "32f7a4bd-84de-4587-be29-734d65ad6f70")
{
if (entity.userInfo.PHONE.IsEmpty())
{
json.Msg = "经销商联系人和电话不能为空!";
return Json(json);
}
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
var disnum = orm.Queryable().Where(p => p.DealerNumber == entity.distributorInfo.DealerNumber && p.PARENTID == "32f7a4bd-84de-4587-be29-734d65ad6f70").Count();
if (disnum > 0)
{
json.Msg = "经销商一级点只能添加一个!";
return Json(json);
}
}
}
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
var disnum = orm.Queryable().Where(p => p.NAME == entity.distributorInfo.NAME).Count();
if (disnum > 0)
{
json.Msg = "经销商名称已经存不能添加";
return Json(json);
}
}
#region 添加
_entity = entity.distributorInfo;
_entity.ID = guid;
_entity.CREATEDATE = DateTime.Now;
_entity.CREATEPERID = this.CurrentUser.Name;
_entity.PARENTID = paretid;
_entity.UPDATEDATE = DateTime.Now;
_entity.UPDATEUSER = this.CurrentUser.Name;
_entity.Address = entity.distributorInfo.Address;
_entity.FenceRadius = Request.Form["example_length"].ToInt32();
_entity.LICENSEURL = fileurl;
_entity.LICENSEVALUE = fileid;
_entity.ProvinceCity = citystr;
_entity.Province = Request.Form["hkprov"].ToInt32();
_entity.City = Request.Form["hkcity"].ToInt32();
_entity.Area = Request.Form["hkcountry"].ToInt32();
//根据上级部门的ID确定当前部门的CODE
_entity.CODE = this.DepartmentManage.CreateCode(_entity.PARENTID);
//获取父级记录
if (string.IsNullOrEmpty(_entity.PARENTID))
{
//业务等级
entity.distributorInfo.BUSINESSLEVEL = 1;
entity.distributorInfo.PARENTCODE = null;
}
else
{
entity.distributorInfo.BUSINESSLEVEL = parententity.BUSINESSLEVEL + 1;
entity.distributorInfo.PARENTCODE = parententity.CODE;
entity.distributorInfo.ParentName = parententity.NAME;
if (entity.distributorInfo.BUSINESSLEVEL == 2)
{
if (!_userenty.PHONE.IsEmpty())
{
_userenty = entity.userInfo;
_userenty.DPTID = _entity.ID;
var pwd = PasswordUtils.MD5_Encrypt("111111", 32);
_userenty.USERTYPE = 3;
_userenty.ACCOUNT = _userenty.PHONE;
_userenty.PASSWORD = pwd;
_userenty.CREATEDATE = DateTime.Now;
_userenty.CREATEPER = this.CurrentUser.Name;
_userenty.UPDATEDATE = DateTime.Now;
_userenty.UPDATEUSER = this.CurrentUser.Name;
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
};
var resuser = orm.SaveHaveNull(_userenty, res);
}
}
}
}
#endregion
}
if (entity.userInfo.ID.IfNotNull())
{
EntSYS_USER_ROLE roleMod = new EntSYS_USER_ROLE();
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
};
int uid = _userenty.ID.ToInt32();
var reslutrolse = orm.Queryable().Where(p => p.FK_USERID == uid && p.FK_ROLEID == 3).FirstOrDefault(res);
if (reslutrolse.IsSuccess)
{
}
else
{
int ROLEID = 0; var usertype = CurrentUser.USERTYPE;
switch ((PostType)usertype)
{
case PostType.SalesClerk:
{
ROLEID = 2;
}
break;
case PostType.Distributors:
{
ROLEID = 3;
}
break;
case PostType.Warehouse:
{
ROLEID = 4;
}
break;
case PostType.ThreePacksStaff:
{
ROLEID = 6;
}
break;
case PostType.MarketingAdmin:
{
ROLEID = 7;
}
break;
}
//roleMod.FK_ROLEID = ROLEID;
roleMod.FK_ROLEID = 3;
roleMod.FK_USERID = uid;
orm.Save(roleMod, res);
}
}
}
}
//先取消电子围栏
if (entity.distributorInfo.Theodolite.IsEmpty())
{
var addressmod = AddressToGPS(entity.distributorInfo.Address);
if (addressmod.Geocodes.Count > 0)
{
foreach (Geocode mod in addressmod.Geocodes)
{
string lnglatstr = mod.Location;
_entity.Theodolite = lnglatstr;
}
}
}
else
{
_entity.Theodolite = entity.distributorInfo.Theodolite;
}
//判断同一个部门下,是否重名
var predicate = PredicateBuilder.True();
predicate = predicate.And(p => p.PARENTID == _entity.PARENTID);
predicate = predicate.And(p => p.NAME == _entity.NAME);
predicate = predicate.And(p => p.ID != _entity.ID);
if (!this.DepartmentManage.IsExist(predicate))
{
if (this.DepartmentManage.SaveOrUpdate(_entity, isEdit))
{
json.Status = "y";
try
{
if (CurrentUser.USERTYPE == 2)
{
var productline1 = Request.Form["ckb_per"];
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
};
var count = orm.Queryable().Where(p => p.distributorId == guid && p.userId == CurrentUser.Id).Count();
if (count == 0)
{
EntYW_UserDistributor userdismod = new EntYW_UserDistributor();
userdismod.ID = Guid.NewGuid();
userdismod.userId = CurrentUser.Id;
userdismod.distributorId = guid;
if (productline1.IndexOf("601") >= 0)
userdismod.ProductLine = 601;
if (productline1.IndexOf("615") >= 0)
userdismod.ProductLine1 = 615;
if (productline1.IndexOf("612") >= 0)
userdismod.ProductLine2 = 612;
if (productline1.IndexOf("611") >= 0)
userdismod.ProductLine3 = 611;
if (productline1.IndexOf("622") >= 0)
userdismod.ProductLine4 = 622;
if (productline1.IndexOf("628") >= 0)
userdismod.ProductLine5 = 628;
userdismod.add_name = "系统导入";
var reslut1 = orm.Save(userdismod, res);
}
}
}
//string imei = "356802032734930";
string imei = "333333333333333";
string discode = Ant.SuperSocket.Common.DataHelper.StringToHex(_entity.CODE);
var str = Ant.SuperSocket.Common.DataHelper.sendCarData(Convert.ToInt64(imei), "9922" + discode);
var body = Ant.SuperSocket.Common.DataHelper.HexToByte(str);
SocketClient client = new SocketClient();
string ip = System.Web.Configuration.WebConfigurationManager.AppSettings["ip"].ToString();
// string ip = "";
int port = System.Web.Configuration.WebConfigurationManager.AppSettings["port"].ToInt32();
client.Connect(ip, port);
var flag = client.SendData(body);
client.Disconnect();
}
catch (Exception ex) { }
}
else
{
json.Msg = "保存失败";
}
}
else
{
json.Msg = "经销商" + entity.distributorInfo.NAME + "已存在,不能重复添加";
}
if (isEdit)
{
WriteLog(enumOperator.Edit, "修改经销商信息,结果:" + json.Msg, enumLog4net.INFO);
}
else
{
WriteLog(enumOperator.Add, "添加经销商信息,结果:" + json.Msg, enumLog4net.INFO);
}
}
catch (Exception e)
{
json.Msg = "保存经销商信息发生内部错误!";
WriteLog(enumOperator.None, "保存经销商信息:", e);
}
return Json(json);
}
///
/// 将地址转GPS
///
///
///
private AddressGPS AddressToGPS(string address)
{
System.Net.WebClient server = new System.Net.WebClient();
string url = String.Format("http://restapi.amap.com/v3/geocode/geo?key={0}&address={1}&city={2}", "fa36ec6323075f37020083ceffe8eb24", address, "");
var replyBuffer = server.DownloadData(url);
var reply = System.Text.Encoding.UTF8.GetString(replyBuffer);
var mod = Newtonsoft.Json.JsonConvert.DeserializeObject(reply);
return mod;
}
///
/// 删除部门
///
[UserAuthorizeAttribute(ModuleAlias = "SalesDealersList", OperaAction = "Remove")]
public ActionResult Delete(string idList)
{
JsonHelper json = new JsonHelper() { Msg = "删除经销商成功", ReUrl = "/Distributors/SalesDealersList", Status = "n" };
try
{
if (!string.IsNullOrEmpty(idList))
{
idList = idList.TrimEnd(',');
//判断是否有下属部门
if (!this.DepartmentManage.DepartmentIsExists(idList))
{
//判断是否有用户归属该部门
if (!this.UserDepartmentManage.IsExist(p => idList.Contains(p.DEPARTMENT_ID)))
{
//判断该部门是否有岗位
if (!this.PostDepartmentManage.IsExist(p => idList.Contains(p.FK_DEPARTMENT_ID)))
{
var idList1 = idList.Split(',').ToList();
this.DepartmentManage.Delete(p => idList.Contains(p.ID));
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
foreach (string id in idList.Split(','))
{
RequestModel request = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询菜单的单个实体方法" },
Oid = id.ToString()
};
var disreult = orm.DeleteByLambda(p => p.distributorId == id, request);
var userreult = orm.Queryable().Where(p => p.DPTID == id).FirstOrDefault(request);
if (userreult.IsSuccess)
{
En_SYS_USER moduser = userreult.ResultModel;
var rolereult = orm.DeleteByLambda(p => p.FK_USERID == moduser.ID, request);
var usreult = orm.DeleteByLambda(p => p.DPTID == id, request);
}
}
}
json.Status = "y";
}
else
{
json.Msg = "该经销商有岗位信息不能删除,请先取消岗位分配";
}
}
else
{
json.Msg = "该经销商存在用户不能删除";
}
}
else
{
json.Msg = "该经销商有下属部门不能删除";
}
}
else
{
json.Msg = "未找到要删除的部门记录";
}
WriteLog(enumOperator.Remove, "删除部门:" + json.Msg, enumLog4net.WARN);
}
catch (Exception e)
{
json.Msg = "删除经销商发生内部错误!";
WriteLog(enumOperator.Remove, "删除部门:", e);
}
return Json(json);
}
///
///
///
///
public ActionResult FenceInfo(string id)
{
EntSYS_DISTRIBUTORS mod = new EntSYS_DISTRIBUTORS();
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel
{
newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
};
var q = orm.Queryable();
if (!id.IsEmpty())
{
var reslut = q.Where(p => p.ID == id).FirstOrDefault(res);
if (reslut.IsSuccess)
{
mod = reslut.ResultModel;
}
}
}
return View(mod);
}
///
/// 加载列表
///
///
[UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "View")]
public ActionResult InventoryList()
{
try
{
#region 处理查询参数
string posttype = Request.QueryString["posttype"];
string FK_DPTID = Request.QueryString["FK_DPTID"];
#endregion
#region 加载列表
var result = BindList(posttype, FK_DPTID, CurrentUser.USERTYPE);
ViewBag.Search = base.keywords;
// ViewData["PostType"] = this.CodeManage.GetCode("POSTTYPE");
//ViewData["post"] = posttype;
ViewData["Department"] = this.DepartmentManage.GetDepartmentByDetailNew();
ViewData["dptid"] = FK_DPTID;
#endregion
if (result.IsNull())
{
return View();
}
else
{
if (!Request.QueryString["hkprov"].IsEmpty())
{
ViewData["hkprov"] = Request.QueryString["hkprov"];
}
if (!Request.QueryString["hkcity"].IsEmpty())
{
ViewData["hkcity"] = Request.QueryString["hkcity"];
}
if (!Request.QueryString["hkcountry"].IsEmpty())
{
ViewData["hkcountry"] = Request.QueryString["hkcountry"];
}
return View(result);
}
}
catch (Exception e)
{
WriteLog(enumOperator.Select, "加载岗位列表:", e);
throw e.InnerException;
}
}
///
///
///
[UserAuthorizeAttribute(ModuleAlias = "SalesDealersList", OperaAction = "View")]
public ActionResult SalesDealersList()
{
try
{
#region 处理查询参数
string posttype = Request.QueryString["posttype"];
string FK_DPTID = Request.QueryString["FK_DPTID"];
#endregion
#region 加载列表
var result = new PageInfo(base.page, base.pagesize, 0, JsonConverter.JsonClass(new List()));
int uid = 0;
if (CurrentUser.USERTYPE == 0 || CurrentUser.USERTYPE == 8)
{
result = BindList(posttype, FK_DPTID, CurrentUser.USERTYPE);
}
else
{
uid = CurrentUser.Id;
result = SalesDistributionBindList(posttype, FK_DPTID);
}
ViewBag.Search = base.keywords;
// ViewData["PostType"] = this.CodeManage.GetCode("POSTTYPE");
//ViewData["post"] = posttype;
ViewData["Department"] = this.DepartmentManage.GetDepartmentByDetailNew(uid);
ViewData["dptid"] = FK_DPTID;
ViewData["saleId"] = CurrentUser.Id.ToString();
#endregion
if (!Request.QueryString["hkprov"].IsEmpty())
{
ViewData["hkprov"] = Request.QueryString["hkprov"];
}
if (!Request.QueryString["hkcity"].IsEmpty())
{
ViewData["hkcity"] = Request.QueryString["hkcity"];
}
if (!Request.QueryString["hkcountry"].IsEmpty())
{
ViewData["hkcountry"] = Request.QueryString["hkcountry"];
}
if (result.IsNull())
{
return View();
}
else
{
return View(result);
}
}
catch (Exception e)
{
WriteLog(enumOperator.Select, "加载岗位列表:", e);
throw e.InnerException;
}
}
///
/// 修改密码页面窗体
///
// [UserAuthorizeAttribute(ModuleAlias = "Department", OperaAction = "UserPassword")]
public ActionResult PasswordSet()
{
if (!string.IsNullOrEmpty(this.CurrentUser.Name))
{
var entity = this.UserManage.Get(p => p.ID == CurrentUser.Id);
return View(entity);
}
return View();
}
///
/// 修改密码功能实现函数
///
public ActionResult PasswordSave()
{
JsonHelper json = new JsonHelper() { Msg = "修改密码完成", Status = "n" };
var ID = Request.Form["ID"] ?? "";
if (string.IsNullOrEmpty(ID))
{
json.Msg = "请登录";
return Json(json);
}
string OldPassword = Request.Form["oldpass"];
string NewPassword = Request.Form["newpass"];
string confPassword = Request.Form["confpass"];
int newid = int.Parse(ID);
var entity = this.UserManage.Get(p => p.ID == newid);
var pwd = PasswordUtils.MD5_Encrypt(NewPassword, 32);// Ant.Service.Common.CryptHelper.DESCrypt.Encrypt("111111")
entity.PASSWORD = pwd;
//entity.PASSWORD = Ant.Service.Common.CryptHelper.DESCrypt.Encrypt(NewPassword);
try
{
this.UserManage.Update(entity);
json.Status = "y";
WriteLog(enumOperator.Edit, "修改密码:" + json.Msg, enumLog4net.INFO);
return Json(json);
}
catch (Exception e)
{
json.Msg = e.Message;
WriteLog(enumOperator.Edit, "修改密码错误:", e);
return Json(json);
}
}
///
/// 修改密码前检测旧密码是否正确
///
public ActionResult CheckoldPass(string oldPassword)
{
JsonHelper json = new JsonHelper() { Msg = "Success", Status = "n" };
string id = Request.Form["ID"];
string oldpass = Request.Form["oldpass"];
if (string.IsNullOrEmpty(id))
{
json.Msg = "未找到要处理的用户";
return Json(json);
}
var pass = PasswordUtils.MD5_Encrypt(oldpass, 32);
//var pass = Ant.Service.Common.CryptHelper.DESCrypt.Encrypt(oldpass);
int newid = int.Parse(id);
var result = this.UserManage.IsExist(p => p.ID == newid && p.PASSWORD.Equals(pass));
if (!result)
{
json.Msg = "原始密码错误";
return Json(json);
}
json.Status = "y";
return Json(json);
}
///
/// 绑定页面需要的属性
///
public PageInfo SalesDistributionBindList(string posttype, string FK_DPTID)
{
//var citystr = Request.QueryString["hkprov"] + "," + Request.QueryString["hkcity"] + "," + Request.QueryString["hkcountry"];
string saleid = CurrentUser.Id.ToString();
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel request = new RequestModel();
request.newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询菜单的单个实体方法" };
var dis = orm.Queryable();
var userdis = orm.Queryable();
IJoiningQuery view = dis.LeftJoin(userdis, (user, city) => user.ID == city.distributorId);
int uid = saleid.ToInt32();
var q = view.Select((user, city) => new { Users = user, Citys = city }).Where(a => a.Citys.userId == uid);
//var q = orm.Queryable();
if (!keywords.IsEmpty())
{
q = q.Where(p => p.Users.NAME.Contains(keywords) && p.Users.BUSINESSLEVEL > 1);
}
else
{
q = q.Where(p => p.Users.BUSINESSLEVEL > 1);
}
if (!Request.QueryString["hkprov"].IsEmpty())
{
int Province = Request.QueryString["hkprov"].ToInt32();
q = q.Where(p => p.Users.Province == Province);
}
if (!Request.QueryString["hkcity"].IsEmpty())
{
int City = Request.QueryString["hkcity"].ToInt32();
q = q.Where(p => p.Users.City == City);
}
if (!Request.QueryString["hkcountry"].IsEmpty())
{
int Area = Request.QueryString["hkcountry"].ToInt32();
q = q.Where(p => p.Users.Area == Area);
}
int pageno = (page - 1) * pagesize;
var disreslut = q.OrderBy(a => a.Users.CODE).Skip(pageno).Top(pagesize).ToList(request);
if (disreslut.IsSuccess)
{
disreslut.RecordNum = q.Count();
List modlist = new List();
foreach (var item in disreslut.ResultModel)
{
modlist.Add(item.Users);
}
return new PageInfo(base.page, base.pagesize, disreslut.RecordNum, JsonConverter.JsonClass(modlist));
}
else
{
return new PageInfo(base.page, base.pagesize, disreslut.RecordNum, JsonConverter.JsonClass(new List()));
}
}
//return new PageInfo(result.Index, result.PageSize, result.Count, JsonConverter.JsonClass(list));
}
///
/// 绑定页面需要的属性
///
public PageInfo BindList(string posttype, string FK_DPTID, int usertype)
{
//var citystr = Request.QueryString["hkprov"] + "," + Request.QueryString["hkcity"] + "," + Request.QueryString["hkcountry"];
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel request = new RequestModel();
request.newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询菜单的单个实体方法" };
var q = orm.Queryable();
if (!keywords.IsEmpty())
{
q = q.Where(p => p.NAME.Contains(keywords) && p.BUSINESSLEVEL > 1);
}
else
{
q = q.Where(p => p.BUSINESSLEVEL > 1);
}
if (!Request.QueryString["hkprov"].IsEmpty())
{
int Province = Request.QueryString["hkprov"].ToInt32();
q = q.Where(p => p.Province == Province);
}
if (!Request.QueryString["hkcity"].IsEmpty())
{
int City = Request.QueryString["hkcity"].ToInt32();
q = q.Where(p => p.City == City);
}
if (!Request.QueryString["hkcountry"].IsEmpty())
{
int Area = Request.QueryString["hkcountry"].ToInt32();
q = q.Where(p => p.Area == Area);
}
if (usertype == 8)//风控管理员
{
q = q.Where(p => p.FenceRadius > 0);
}
int pageno = (page - 1) * pagesize;
var reslut = q.OrderBy(a => a.CODE).Skip(pageno).Top(pagesize).ToList(request);
//foreach (EntSYS_DISTRIBUTORS mod in reslut.ResultModel)
//{
// //mod.NAME = GetModuleName(mod.NAME, mod.BUSINESSLEVEL - 1).ToString();
// mod.NAME = mod.NAME;
// modlist.Add(mod);
//}
//var reslut= orm.SqlQuery(string.Format("select top {0} * from YW_Bicycle where Id>@Id", 1), new { Id = "sfdfdfdf" }).ToList();
if (reslut.IsSuccess)
{
reslut.RecordNum = q.Count();
List modlist = new List();
modlist = reslut.ResultModel;
return new PageInfo(base.page, base.pagesize, reslut.RecordNum, JsonConverter.JsonClass(modlist));
}
else
{
return new PageInfo(base.page, base.pagesize, reslut.RecordNum, JsonConverter.JsonClass(new List()));
}
}
//return new PageInfo(result.Index, result.PageSize, result.Count, JsonConverter.JsonClass(list));
}
///
/// 显示错层方法
///
private object GetModuleName(string name, decimal? level)
{
if (level > 0)
{
string nbsp = " ";
for (int i = 0; i < level; i++)
{
nbsp += " ";
}
name = nbsp + " |--" + name;
}
return name;
}
}
}