using System; using System.Collections.Generic; using System.Linq; using System.Text; using AutoMapper; using DTO; using IServices; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using zhengcaioa.Models; namespace Services { public class AdmGoodsManageService: IAdmGoodsManageService { private readonly zhengcaioaContext _context; private readonly IMapper _mapper; public AdmGoodsManageService(zhengcaioaContext context, IMapper mapper) { _context = context; _mapper = mapper; } public ResultEntity save(AdmGoodsManageDTO dto) { ResultEntity resultEntity = new ResultEntity(); try { var checkUserSn = _context.AdmGoodsManages.Where(x => x.GoodsName == dto.GoodsName && x.RecStatus == "A" && x.Id != dto.Id).FirstOrDefault(); if (checkUserSn != null && (string.IsNullOrWhiteSpace(dto.Id) || (!string.IsNullOrWhiteSpace(dto.Id) && checkUserSn.Id != dto.Id))) { resultEntity.Result = false; resultEntity.Message = "物品名称重复"; return resultEntity; } var entity = _mapper.Map(dto); if (String.IsNullOrEmpty(entity.Id)) { entity.Id = Guid.NewGuid().ToString(); entity.GoodsLeft = entity.GoodsNum; _context.AdmGoodsManages.Add(entity); } else { var updateproject = _context.AdmGoodsManages.Find(entity.Id); updateproject.ClassifyId = entity.ClassifyId; updateproject.GoodsName = entity.GoodsName; updateproject.ISBN = entity.ISBN; if (entity.GoodsPrice.HasValue) { updateproject.GoodsPrice = entity.GoodsPrice; } if (entity.GoodsNum.HasValue) { updateproject.GoodsNum = entity.GoodsNum; } if (entity.GoodsLeft.HasValue) { updateproject.GoodsLeft = entity.GoodsLeft; } updateproject.GoodsStatus = entity.GoodsStatus; updateproject.HaocaiStatus = entity.HaocaiStatus; updateproject.RecStatus = entity.RecStatus; updateproject.Modifier = entity.Modifier; updateproject.Modifytime = entity.Modifytime; } _context.SaveChanges(); resultEntity.ReturnID = entity.Id; resultEntity.Result = true; } catch (Exception ex) { resultEntity.Result = false; resultEntity.Message = "保存失败,请联系管理员"; } return resultEntity; } public AdmGoodsManageDTO Get(string id) { var entity = _context.AdmGoodsManages.Find(id); if (entity.RecStatus != "A") { entity = new AdmGoodsManage(); } var result = _mapper.Map(entity); return result; } public ResultDataEntity SearchByPaging(AdmGoodsManageDTOSearch searchEntity) { ResultDataEntity data = new ResultDataEntity(); List list = new List(); var listCode = (from a in _context.SysCodeDtls join b in _context.SysCodes on a.CodeId equals b.Id where a.RecStatus == "A" && b.RecStatus == "A" select new CodeDataEntity() { CodeId = b.Id, CodeTable = b.CodeTable, CodeField = b.CodeField, CodeSn = a.CodeSn, Comments = a.Comments, Contents = a.Contents, RecStatus = a.RecStatus, Sort = a.Sort } ); ///AdmGoodsManages var query = (from a in _context.AdmGoodsManages join e in listCode.Where(x => x.CodeTable == "adm_goods_manage" && x.CodeField == "goods_status") on a.GoodsStatus equals e.CodeSn into esssss from eee in esssss.DefaultIfEmpty() join g in _context.AdmGoodsClassifies.Where(x => x.RecStatus == "A") on a.ClassifyId equals g.Id into gsss from ggg in gsss.DefaultIfEmpty() where a.RecStatus == "A" && (string.IsNullOrWhiteSpace(searchEntity.ClassifyId) || a.ClassifyId == searchEntity.ClassifyId.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.GoodsStatus) || a.GoodsStatus == searchEntity.GoodsStatus.Trim()) && (string.IsNullOrWhiteSpace(searchEntity.GoodsName) || a.Id == searchEntity.GoodsName.Trim()) select new AdmGoodsManageDTO { Id = a.Id, ClassifyId = a.ClassifyId, GoodsStatusName = eee.Comments, ClassifyName = ggg.ClassifyName, GoodsPrice = a.GoodsPrice ?? 0, GoodsNum = a.GoodsNum ?? 0, // DocContent = a.DocContent, GoodsLeft = a.GoodsLeft, GoodsStatus = a.GoodsStatus, HaocaiStatus = a.HaocaiStatus, GoodsName = a.GoodsName, ISBN = a.ISBN, Creater = a.Creater, Createtime = a.Createtime, RecStatus = a.RecStatus, Modifier = a.Modifier, Modifytime = a.Modifytime, } ).OrderByDescending(x => x.Modifytime).ToList(); //if (searchEntity.totalrows == 0) searchEntity.totalrows = query.Count(); var lianlist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList(); data.LoadData(searchEntity, lianlist); return data; } /// /// 修改主表状态 /// /// 主id /// 用户 /// public ResultEntity ModifyStatus(string id, string userid) { ResultEntity result = new ResultEntity(); result.Result = true; var model = _context.AdmGoodsManages.Find(id); if (model != null) { model.RecStatus = "D"; model.Modifier = userid; model.Modifytime = DateTime.Now; _context.SaveChanges(); } return result; } /// /// 获取所有有效角色 /// /// public List GetList(string ClassifyId = "") { var listRole = _context.AdmGoodsManages.Where(r => r.RecStatus == "A").OrderBy(x => x.GoodsName).ToList(); if (!string.IsNullOrEmpty(ClassifyId)) { listRole = listRole.Where(x => x.ClassifyId == ClassifyId).ToList(); } var list = _mapper.Map>(listRole); return list; } } }