using AutoMapper;
|
using DTO;
|
using IServices;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using zhengcaioa.Models;
|
|
namespace Services
|
{
|
public class SimService :ISimService
|
{
|
private readonly zhengcaioaContext _context;
|
private readonly IMapper _mapper;
|
public SimService(zhengcaioaContext context, IMapper mapper)
|
{
|
_context = context;
|
_mapper = mapper;
|
}
|
|
#region SIM 分配
|
|
/// <summary>
|
/// 保存
|
/// </summary>
|
/// <param name="md"></param>
|
/// <returns></returns>
|
public ResultEntity Save(SimBind md)
|
{
|
ResultEntity resultEntity = new ResultEntity();
|
try
|
{
|
var temp = _context.SimBind.Where(e => e.sim_id == md.sim_id).SingleOrDefault();
|
if(temp!=null)
|
{
|
var users = _context.SimBind.Where(c => c.user_id == md.user_id).ToList();
|
if(users!=null)
|
{
|
foreach(var item in users)
|
{
|
item.user_id = "";
|
item.up_time = DateTime.Now;
|
item.up_user = md.up_user;
|
}
|
}
|
temp.cardpro = md.cardpro;
|
temp.user_id = md.user_id;
|
temp.up_user = md.up_user;
|
temp.up_time = DateTime.Now;
|
}
|
else
|
{
|
md.up_time = DateTime.Now;
|
_context.SimBind.Add(md);
|
}
|
_context.SaveChanges();
|
resultEntity.Result = true;
|
}
|
catch (Exception ex)
|
{
|
resultEntity.Result = false;
|
resultEntity.Message = "保存失败,请联系管理员";
|
ex.Message.ToString();
|
}
|
return resultEntity;
|
}
|
|
/// <summary>
|
/// 解绑
|
/// </summary>
|
/// <param name="ids"></param>
|
/// <returns></returns>
|
public ResultEntity UnBind(string ids,string user)
|
{
|
ResultEntity resultEntity = new ResultEntity();
|
try
|
{
|
var temp = _context.SimBind.Where(e =>ids.Contains(e.sim_id)).ToList();
|
if (temp != null)
|
{
|
foreach (var item in temp)
|
{
|
item.user_id = "";
|
item.up_time = DateTime.Now;
|
item.up_user = user;
|
}
|
}
|
_context.SaveChanges();
|
resultEntity.Result = true;
|
}
|
catch (Exception ex)
|
{
|
resultEntity.Result = false;
|
resultEntity.Message = "保存失败,请联系管理员";
|
ex.Message.ToString();
|
}
|
return resultEntity;
|
}
|
|
/// <summary>
|
/// 绑定信息查询
|
/// </summary>
|
/// <param name="id"></param>
|
/// <returns></returns>
|
public SimBindView Get(string id)
|
{
|
var entity = _context.SimBindView.Where(c=>c.Id==id).SingleOrDefault();
|
if (entity == null)
|
{
|
entity = new SimBindView();
|
}
|
return entity;
|
}
|
|
/// <summary>
|
/// 查询内容
|
/// </summary>
|
/// <param name="searchEntity"></param>
|
/// <returns></returns>
|
public ResultDataEntity<SimBindView> SearchByPaging(SimBindDTOSearch searchEntity)
|
{
|
ResultDataEntity<SimBindView> data = new ResultDataEntity<SimBindView>();
|
try
|
{
|
var codedata= (from e in _context.SysCodes
|
join c in _context.SysCodeDtls
|
on e.Id equals c.CodeId
|
where e.RecStatus == "A"
|
&& c.RecStatus == "A"
|
&& e.CodeTable == "t_sim_bind"
|
&& e.CodeField == "cardpro"
|
select c).OrderBy(x => x.Sort).ToList();
|
|
var dt = _context.SimBindView.OrderBy(c => c.simcard).ToList();
|
if(!string.IsNullOrEmpty(searchEntity.simcard))
|
{
|
dt = dt.Where(c => c.simcard.Contains(searchEntity.simcard)).ToList();
|
}
|
if (!string.IsNullOrEmpty(searchEntity.username))
|
{
|
dt = dt.Where(c => !string.IsNullOrEmpty(c.username) && c.username.Contains(searchEntity.username)).ToList();
|
}
|
|
//if (searchEntity.totalrows == 0)
|
searchEntity.totalrows = dt.Count();
|
|
var signinList = dt.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList();
|
|
signinList= signinList.Select(a=> new SimBindView()
|
{
|
cardproChs = ToChs(a.cardpro, codedata),
|
Id = a.Id,
|
simcard = a.simcard,
|
simstatus = a.simstatus,
|
up_time = a.up_time,
|
username = a.username
|
}).ToList();
|
|
data.LoadData(searchEntity, signinList);
|
}
|
catch (Exception ex)
|
{
|
throw;
|
}
|
return data;
|
}
|
|
private string ToChs(string key,List<SysCodeDtl> list)
|
{
|
string str = "";
|
var item = list.Find(c => c.CodeSn == key);
|
if(item!=null)
|
{
|
str = item.Comments;
|
}
|
return str;
|
}
|
|
#endregion
|
|
#region 费用录入
|
|
/// <summary>
|
/// 获取已绑定的电话卡
|
/// </summary>
|
/// <returns></returns>
|
public List<SimBindView> GetBindSim()
|
{
|
List<SimBindView> list = null;
|
try
|
{
|
list = _context.SimBindView.Where(c=>!string.IsNullOrEmpty(c.userid)).OrderBy(c => c.simcard).ToList();
|
}
|
catch (Exception er)
|
{
|
throw;
|
}
|
return list;
|
}
|
|
/// <summary>
|
/// 保存
|
/// </summary>
|
/// <param name="md"></param>
|
/// <returns></returns>
|
public ResultEntity SaveCost(SimCost md)
|
{
|
ResultEntity resultEntity = new ResultEntity();
|
try
|
{
|
var temp = _context.SimCost.Where(e => e.sim_id == md.sim_id && e.bill_year==md.bill_year && e.bill_month==md.bill_month).SingleOrDefault();
|
var tempBind = _context.SimBindView.Where(e => e.Id == md.sim_id).SingleOrDefault();
|
if (temp != null && temp.status=="0")
|
{
|
temp.bill_amount = md.bill_amount;
|
//temp.bill_month =md.m;
|
//temp.bill_year = md.accounted_time.Year;
|
temp.sub_userid = md.sub_userid;
|
temp.up_time = DateTime.Now;
|
if(tempBind!=null)
|
{
|
temp.allow_amount = decimal.Parse(tempBind.cardpro);
|
temp.user_id = tempBind.userid;
|
temp.amount = temp.allow_amount - temp.bill_amount > 0 ? 0 : temp.allow_amount - temp.bill_amount;
|
}
|
}
|
else
|
{
|
//md.bill_month = md.accounted_time.Month;
|
//md.bill_year = md.accounted_time.Year;
|
|
//当月重复判断
|
//var monthck = _context.SimCost.Where(c => c.sim_id == md.sim_id && c.bill_year == md.bill_year && c.bill_month == md.bill_month).Count();
|
//if(monthck>0)
|
//{
|
// resultEntity.Result = false;
|
// resultEntity.Message = string.Format("此电话卡在[{0}-{1}]月已存在记录,不能重复添加;",md.bill_year,md.bill_month);
|
// return resultEntity;
|
//}
|
//else
|
{
|
md.up_time = DateTime.Now;
|
md.status = "0";
|
md.accounted_time = DateTime.Now;
|
if (tempBind != null)
|
{
|
md.allow_amount = decimal.Parse(tempBind.cardpro);
|
md.amount = md.allow_amount - md.bill_amount > 0 ? 0 : md.allow_amount - md.bill_amount;
|
md.user_id = tempBind.userid;
|
}
|
_context.SimCost.Add(md);
|
}
|
}
|
_context.SaveChanges();
|
resultEntity.Result = true;
|
}
|
catch (Exception ex)
|
{
|
resultEntity.Result = false;
|
resultEntity.Message = "保存失败,请联系管理员";
|
ex.Message.ToString();
|
}
|
return resultEntity;
|
}
|
|
/// <summary>
|
/// 绑定信息查询
|
/// </summary>
|
/// <param name="id"></param>
|
/// <returns></returns>
|
public SimCost GetSimCost(int id)
|
{
|
var entity = _context.SimCost.Where(c => c.id == id).SingleOrDefault();
|
if (entity == null)
|
{
|
entity = new SimCost();
|
}
|
return entity;
|
}
|
|
/// <summary>
|
/// 查询内容
|
/// </summary>
|
/// <param name="searchEntity"></param>
|
/// <returns></returns>
|
public List<SimCostView> SearchSimCostByPage(string sim ,int year)
|
{
|
List<SimCostView> data = new List<SimCostView>();
|
try
|
{
|
//var codedata = (from e in _context.SysCodes
|
// join c in _context.SysCodeDtls
|
// on e.Id equals c.CodeId
|
// where e.RecStatus == "A"
|
// && c.RecStatus == "A"
|
// && e.CodeTable == "t_sim_bind"
|
// && e.CodeField == "cardpro"
|
// select c).OrderBy(x => x.Sort).ToList();
|
|
data = _context.SimCostView.Where(c => c.bill_year==year && c.sim_id==sim).OrderBy(c=>c.bill_month).ToList();
|
//if (!string.IsNullOrEmpty(searchEntity.searchtime))
|
//{
|
// var times = searchEntity.searchtime.Split('|');
|
// if(times.Length==2)
|
// {
|
// DateTime d1 = DateTime.Parse(times[0]);
|
// DateTime d2 = DateTime.Parse(times[1]);
|
// dt = dt.Where(c => c.accounted_time > d1 && c.accounted_time < d2).ToList();
|
// }
|
//}
|
//if (!string.IsNullOrEmpty(searchEntity.searchsim))
|
//{
|
// dt = dt.Where(c => !string.IsNullOrEmpty(c.simcard) && c.simcard.Contains(searchEntity.searchsim)).ToList();
|
//}
|
//SimCostDTOSearch searchEntity = new SimCostDTOSearch();
|
//searchEntity.rows = 20;
|
//searchEntity.page = 1;
|
|
//if (searchEntity.totalrows == 0)
|
// searchEntity.totalrows = dt.Count();
|
|
//var signinList = dt.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList();
|
|
//signinList = signinList.Select(a => new SimBindView()
|
//{
|
// cardproChs = ToChs(a.cardpro, codedata),
|
// Id = a.Id,
|
// simcard = a.simcard,
|
// simstatus = a.simstatus,
|
// up_time = a.up_time,
|
// username = a.username
|
//}).ToList();
|
|
//data.LoadData(searchEntity, signinList);
|
}
|
catch (Exception ex)
|
{
|
throw;
|
}
|
return data;
|
}
|
|
/// <summary>
|
/// 查询费用记录
|
/// </summary>
|
/// <param name="userid"></param>
|
/// <param name="bill_year"></param>
|
/// <param name="bill_month"></param>
|
/// <returns></returns>
|
public SimCostView GetSimCost(string userid, int bill_year, int bill_month)
|
{
|
|
var entity = _context.SimCostView.Where(x => x.user_id == userid && x.bill_year == bill_year && x.bill_month == bill_month).FirstOrDefault();
|
|
|
|
return entity;
|
}
|
|
/// <summary>
|
/// 查询内容
|
/// </summary>
|
/// <param name="searchEntity"></param>
|
/// <returns></returns>
|
public ResultDataEntity<SimCostYearView> SearchSimCostYearByPage(SimCostDTOSearch searchEntity)
|
{
|
ResultDataEntity<SimCostYearView> data = new ResultDataEntity<SimCostYearView>();
|
try
|
{
|
int year=DateTime.Now.Year;
|
if (!string.IsNullOrEmpty(searchEntity.searchtime))
|
{
|
year = int.Parse(searchEntity.searchtime);
|
}
|
|
var dt = (from c in _context.SimCostYearView where c.bill_year==year select c).ToList();
|
//添加未有记录的
|
var stringId = dt.Select(c => c.sim_id).ToList();
|
var nolog = (from c in _context.SimBindView where !stringId.Contains(c.Id) select c).ToList();
|
foreach(var item in nolog)
|
{
|
SimCostYearView ss = new SimCostYearView();
|
ss.bill_year = year;
|
ss.sim_id = item.Id;
|
ss.sim = item.simcard;
|
dt.Add(ss);
|
}
|
dt = dt.OrderBy(c => c.sim).ToList();
|
|
if (!string.IsNullOrEmpty(searchEntity.searchsim))
|
{
|
dt = dt.Where(c => c.sim==searchEntity.searchsim).ToList();
|
}
|
|
//if (searchEntity.totalrows == 0)
|
searchEntity.totalrows = dt.Count();
|
|
var signinList = dt.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList();
|
if(signinList.Count>0)
|
{
|
var month = DateTime.Now.Month-1;
|
var ck = (from c in _context.SimCost where c.status == "1" && c.bill_year == year orderby c.bill_month descending select c).FirstOrDefault();
|
if(ck!=null)
|
{
|
month= ck.bill_month;
|
}
|
foreach (var item in signinList)
|
{
|
item.month = month;
|
}
|
}
|
|
data.LoadData(searchEntity, signinList);
|
}
|
catch (Exception ex)
|
{
|
throw;
|
}
|
return data;
|
}
|
|
#endregion
|
}
|
}
|