using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace Learun.Application.CRM
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创 建:超级管理员
/// 日 期:2017-07-11 11:30
/// 描 述:商机管理
///
public class CrmChanceService : RepositoryFactory
{
private CrmTrailRecordService crmTrailRecordService = new CrmTrailRecordService();
#region 获取数据
///
/// 获取列表
///
/// 分页
/// 查询参数
/// 返回分页列表
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
try
{
var expression = LinqExtensions.True();
var queryParam = queryJson.ToJObject();
//查询条件
if (!queryParam["keyword"].IsEmpty())
{
string keyword = queryParam["keyword"].ToString();
expression = expression.And(t => t.F_FullName.Contains(keyword)
|| t.F_Contacts.Contains(keyword)
|| t.F_Mobile.Contains(keyword)
|| t.F_Tel.Contains(keyword)
|| t.F_QQ.Contains(keyword)
|| t.F_Wechat.Contains(keyword)
|| t.F_CompanyName.Contains(keyword)
|| t.F_Contacts.Contains(keyword)
|| t.F_City.Contains(keyword)
);
}
return this.BaseRepository().FindList(expression, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取实体
///
/// 主键值
///
public CrmChanceEntity GetEntity(string keyValue)
{
try
{
return this.BaseRepository().FindEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 验证数据
///
/// 商机名称不能重复
///
/// 名称
/// 主键
///
public bool ExistFullName(string fullName, string keyValue)
{
try
{
var expression = LinqExtensions.True();
expression = expression.And(t => t.F_FullName == fullName);
if (!string.IsNullOrEmpty(keyValue))
{
expression = expression.And(t => t.F_ChanceId != keyValue);
}
return this.BaseRepository().IQueryable(expression).Count() == 0 ? true : false;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 删除数据
///
/// 主键
public void DeleteEntity(string keyValue)
{
IRepository db = new RepositoryFactory().BaseRepository().BeginTrans();
try
{
db.Delete(t => t.F_ChanceId == keyValue);
db.Delete(t => t.F_ObjectId.Equals(keyValue));
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存表单(新增、修改)
///
/// 主键值
/// 实体对象
///
public void SaveEntity(string keyValue, CrmChanceEntity entity)
{
try
{
if (!string.IsNullOrEmpty(keyValue))
{
entity.Modify(keyValue);
this.BaseRepository().Update(entity);
}
else
{
IRepository db = new RepositoryFactory().BaseRepository().BeginTrans();
try
{
entity.Create();
db.Insert(entity);
db.Commit();
}
catch (Exception)
{
db.Rollback();
throw;
}
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 商机作废
///
/// 主键值
public void Invalid(string keyValue)
{
try
{
CrmChanceEntity entity = new CrmChanceEntity();
entity.Modify(keyValue);
entity.F_ChanceState = 0;
this.BaseRepository().Update(entity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 商机转换客户
///
/// 主键
/// 客户编号
public void ToCustomer(string keyValue,string customerCode)
{
CrmChanceEntity chanceEntity = this.GetEntity(keyValue);
IEnumerable trailRecordList = crmTrailRecordService.GetList(keyValue);
IRepository db = new RepositoryFactory().BaseRepository().BeginTrans();
try
{
chanceEntity.Modify(keyValue);
chanceEntity.F_IsToCustom = 1;
db.Update(chanceEntity);
CrmCustomerEntity customerEntity = new CrmCustomerEntity();
customerEntity.Create();
customerEntity.F_EnCode = customerCode;
customerEntity.F_FullName = chanceEntity.F_CompanyName;
customerEntity.F_TraceUserId = chanceEntity.F_TraceUserId;
customerEntity.F_TraceUserName = chanceEntity.F_TraceUserName;
customerEntity.F_CustIndustryId = chanceEntity.F_CompanyNatureId;
customerEntity.F_CompanySite = chanceEntity.F_CompanySite;
customerEntity.F_CompanyDesc = chanceEntity.F_CompanyDesc;
customerEntity.F_CompanyAddress = chanceEntity.F_CompanyAddress;
customerEntity.F_Province = chanceEntity.F_Province;
customerEntity.F_City = chanceEntity.F_City;
customerEntity.F_Contact = chanceEntity.F_Contacts;
customerEntity.F_Mobile = chanceEntity.F_Mobile;
customerEntity.F_Tel = chanceEntity.F_Tel;
customerEntity.F_Fax = chanceEntity.F_Fax;
customerEntity.F_QQ = chanceEntity.F_QQ;
customerEntity.F_Email = chanceEntity.F_Email;
customerEntity.F_Wechat = chanceEntity.F_Wechat;
customerEntity.F_Hobby = chanceEntity.F_Hobby;
customerEntity.F_Description = chanceEntity.F_Description;
customerEntity.F_CustLevelId = "C";
customerEntity.F_CustDegreeId = "往来客户";
db.Insert(customerEntity);
foreach (CrmTrailRecordEntity item in trailRecordList)
{
item.F_TrailId = Guid.NewGuid().ToString();
item.F_ObjectId = customerEntity.F_CustomerId;
item.F_ObjectSort = 2;
db.Insert(item);
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
}
}