75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using WX.CRM.Common;
|
|
using ZXDService.Dao;
|
|
using ZXDService.Domain;
|
|
using ZXDService.Dto;
|
|
|
|
namespace ZXDService.Services
|
|
{
|
|
public class AssignDeptRulesServices
|
|
{
|
|
public bool Save(List<ResDeptRuleDto> dto)
|
|
{
|
|
var ch = dto.Select(p => p.ch).Distinct().FirstOrDefault();
|
|
|
|
using (var db = new AssignContext())
|
|
{
|
|
using (var trans = db.Database.BeginTransaction())
|
|
{
|
|
var deptList = db.AssignDeptRules.Where(p => p.ch == ch).ToList();
|
|
|
|
try
|
|
{
|
|
foreach (var item in dto)
|
|
{
|
|
if (item.crmdeptid == 0 && item.num == 0)
|
|
{
|
|
db.AssignDeptRules.RemoveRange(deptList);
|
|
}
|
|
else
|
|
{
|
|
var id = item.ch + "," + item.crmdeptid;
|
|
var info = deptList.FirstOrDefault(p => p.uniquekey == id);
|
|
if (info != null)
|
|
{
|
|
info.num = item.num;
|
|
info.crmdeptid = item.crmdeptid;
|
|
var entry = db.Entry(info);
|
|
db.Set<AssignDeptRules>().Attach(info);
|
|
entry.State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
info = new AssignDeptRules
|
|
{
|
|
uniquekey = item.ch + "," + item.crmdeptid,
|
|
ch = item.ch,
|
|
crmdeptid = item.crmdeptid,
|
|
num = item.num
|
|
};
|
|
db.AssignDeptRules.Add(info);
|
|
}
|
|
}
|
|
}
|
|
|
|
db.SaveChanges();
|
|
|
|
trans.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Error(ex);
|
|
trans.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
}
|
|
} |