/*
 * 机具申请订单
 */

using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Data;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MySystem.Models;
using Library;
using LitJson;
using MySystemLib;

namespace MySystem.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class MachineApplyController : BaseController
    {
        public MachineApplyController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
            OtherMySqlConn.connstr = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
        }

        #region 机具申请订单列表

        /// <summary>
        /// 根据条件查询机具申请订单列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(MachineApply data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            return View();
        }

        #endregion

        #region 根据条件查询机具申请订单列表

        /// <summary>
        /// 机具申请订单列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(MachineApply data, string ApplyNo, string MakerCode, string RealName, string StoreNo, string StoreName, string StoreMakerCode, string StatusSelect, string ApplyTimeData, string SendDateData, string SendStatusSelect, string StoreTypeSelect, int page = 1, int limit = 30)
        {

            Dictionary<string, string> Fields = new Dictionary<string, string>();

            Fields.Add("ApplyNo", "1"); //申请单号
            Fields.Add("BrandId", "1"); //品牌
            Fields.Add("ExpressNo", "1"); //快递单号
            Fields.Add("DeliveryType", "0"); //提货类型
            Fields.Add("StoreManager", "1"); //仓库联系人
            Fields.Add("TopUserId", "0"); //顶级创客
            Fields.Add("SwapSnExpand", "1"); //兑换SN
            Fields.Add("SendSn", "1"); //发货SN

            string condition = " and Status>=-1";
            //申请单号
            if (!string.IsNullOrEmpty(ApplyNo))
            {
                condition += " and ApplyNo like'" + ApplyNo + "' ";
            }
            //创客编号
            if (!string.IsNullOrEmpty(MakerCode))
            {
                condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + MakerCode + "')";
            }
            //创客名称
            if (!string.IsNullOrEmpty(RealName))
            {
                condition += " and UserId in (select UserId from UserForRealName where RealName='" + RealName + "')";
            }
            //仓库编号
            if (!string.IsNullOrEmpty(StoreNo))
            {
                condition += " and StoreId in (select StoreId from StoreForCode where Code='" + StoreNo + "')";
            }
            //仓库名称
            if (!string.IsNullOrEmpty(StoreName))
            {
                condition += " and StoreId in (select StoreId from StoreForName where Name='" + StoreName + "')";
            }
            //仓库归属创客编号
            if (!string.IsNullOrEmpty(StoreMakerCode))
            {
                condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + StoreMakerCode + "')";
            }
            //订单状态
            if (!string.IsNullOrEmpty(StatusSelect))
            {
                condition += " and Status=" + StatusSelect;
            }
            //发货状态
            if (!string.IsNullOrEmpty(SendStatusSelect))
            {
                condition += " and SendStatus=" + SendStatusSelect;
            }
            //仓库类型
            if (!string.IsNullOrEmpty(StoreTypeSelect))
            {
                condition += " and StoreType=" + StoreTypeSelect;
            }
            //申请时间
            if (!string.IsNullOrEmpty(ApplyTimeData))
            {
                string[] datelist = ApplyTimeData.Split(new string[] { " - " }, StringSplitOptions.None);
                string start = datelist[0];
                string end = datelist[1];
                condition += " and ApplyTime>='" + start + " 00:00:00' and ApplyTime<='" + end + " 23:59:59'";
            }

            if (!string.IsNullOrEmpty(SendDateData))
            {
                string[] datelist = SendDateData.Split(new string[] { " - " }, StringSplitOptions.None);
                string start = datelist[0];
                string end = datelist[1];
                condition += " and SendDate>='" + start + " 00:00:00' and SendDate<='" + end + " 23:59:59'";
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("MachineApply", Fields, "Id desc", "0", page, limit, condition);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                dic["BrandId"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));

                //发货仓库
                int StoreId = int.Parse(dic["StoreId"].ToString());
                StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
                dic["StoreInfo"] = store.StoreNo + "<br />" + store.StoreName;

                //仓库联系人信息
                dic["StoreContact"] = dic["StoreManagerMobile"].ToString() + "<br />" + dic["StoreManager"].ToString();

                //仓库归属信息
                int StoreUserId = int.Parse(dic["StoreUserId"].ToString());
                Users storeUser = db.Users.FirstOrDefault(m => m.Id == StoreUserId) ?? new Users();
                int StoreType = int.Parse(dic["StoreType"].ToString());
                string StoreTypeName = "";
                if (StoreType == 0) StoreTypeName = "总仓";
                if (StoreType == 1) StoreTypeName = "分仓";
                dic["StoreUser"] = storeUser.MakerCode + "<br />" + StoreTypeName;

                //创客信息
                int UserId = int.Parse(dic["UserId"].ToString());
                Users puser = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                dic["UserInfo"] = puser.MakerCode + "<br />" + puser.RealName;

                //顶级创客
                int TopUserId = int.Parse(dic["TopUserId"].ToString());
                Users tuser = db.Users.FirstOrDefault(m => m.Id == TopUserId) ?? new Users();
                dic["TopUserInfo"] = tuser.MakerCode + "<br />" + tuser.RealName;

                //收件人姓名-联系电话
                dic["StoreContact"] = dic["RealName"].ToString() + "<br />" + dic["Mobile"].ToString();

                //订单状态
                int Status = int.Parse(dic["Status"].ToString());
                if (Status == -1) dic["StatusName"] = "已驳回";
                if (Status == 0) dic["StatusName"] = "待审核";
                if (Status == 1) dic["StatusName"] = "申请成功";
                if (Status == 2) dic["StatusName"] = "申请失败";

                //提货方式
                int DeliveryType = int.Parse(dic["DeliveryType"].ToString());
                if (DeliveryType == 0) dic["DeliveryType"] = "";
                if (DeliveryType == 1) dic["DeliveryType"] = "邮寄到付";
                if (DeliveryType == 2) dic["DeliveryType"] = "上门自提";

                //发货状态
                int SendStatus = int.Parse(dic["Status"].ToString());
                if (SendStatus == -1) dic["SendStatus"] = "已驳回";
                if (SendStatus == 0) dic["SendStatus"] = "未发货";
                if (SendStatus == 1) dic["SendStatus"] = "已发货";

                //快递单号-快递名称
                dic["ExpressInfo"] = dic["ExpressName"].ToString() + "<br />" + dic["ExpressNo"].ToString();
            }
            Dictionary<string, object> other = new Dictionary<string, object>();
            int ApplyCount = 0;//申请机具数
            int SendCount = 0;//发货机具数
            DataTable dt = OtherMySqlConn.dtable("select sum(ApplyDeviceNum) from MachineApply where 1=1" + condition);
            if (dt.Rows.Count > 0)
            {
                ApplyCount = int.Parse(function.CheckNum(dt.Rows[0][0].ToString()));
            }
            dt = OtherMySqlConn.dtable("select sum(ApplyDeviceNum) from MachineApply where Status=1" + condition);
            if (dt.Rows.Count > 0)
            {
                SendCount = int.Parse(function.CheckNum(dt.Rows[0][0].ToString()));
            }
            other.Add("ApplyCount", ApplyCount);
            other.Add("SendCount", SendCount);
            obj.Add("other", other);
            return Json(obj);
        }

        #endregion

        #region 增加机具申请订单

        /// <summary>
        /// 增加或修改机具申请订单信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Add(string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            return View();
        }

        #endregion

        #region 增加机具申请订单

        /// <summary>
        /// 增加或修改机具申请订单信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Add(MachineApply data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();


            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Add("MachineApply", Fields, 0);
            AddSysLog(data.Id.ToString(), "MachineApply", "add");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 修改机具申请订单

        /// <summary>
        /// 增加或修改机具申请订单信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            MachineApply editData = db.MachineApply.FirstOrDefault(m => m.Id == Id) ?? new MachineApply();
            ViewBag.data = editData;

            var SnNos = "";
            var order = db.Orders.FirstOrDefault(m => m.Id == editData.QueryCount) ?? new Orders();
            if (!string.IsNullOrEmpty(order.SnNos))
            {
                var sendSn = order.SnNos.Replace("\r", "").Replace("\n", ",").Split(',');
                for (int a = 0; a < sendSn.Length; a++)
                {
                    SnNos += sendSn[a] + "\n";
                }
            }
            ViewBag.SnNos = SnNos;

            Users user = db.Users.FirstOrDefault(m => m.Id == editData.UserId) ?? new Users();
            ViewBag.MakerCode = user.MakerCode;
            ViewBag.RealName = user.RealName;

            int Status = editData.Status;
            if (Status == 0) ViewBag.Status = "待审核";
            if (Status == 1) ViewBag.Status = "申请成功";
            if (Status == 2) ViewBag.Status = "申请失败";

            int AuditResult = editData.AuditResult;
            if (AuditResult == 0) ViewBag.AuditResult = "未通过";
            if (AuditResult == 1) ViewBag.AuditResult = "通过";

            //邮寄方式
            int DeliveryType = editData.DeliveryType;
            if (DeliveryType == 1) ViewBag.DeliveryType = "邮寄到付";
            if (DeliveryType == 2) ViewBag.DeliveryType = "上门自提";

            //发货状态
            // int SendStatus = editData.SendStatus;
            // if (SendStatus == 0) ViewBag.SendStatus = "未发货";
            // if (SendStatus == 1) ViewBag.SendStatus = "已发货";

            int SendStatus = editData.Status;
            if (SendStatus == -1) ViewBag.SendStatus = "已驳回";
            if (SendStatus == 0) ViewBag.SendStatus = "未发货";
            if (SendStatus == 1) ViewBag.SendStatus = "已发货";
            return View();
        }

        #endregion

        #region 修改机具申请订单

        /// <summary>
        /// 增加或修改机具申请订单信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(MachineApply data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();


            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("MachineApply", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "MachineApply", "update");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 删除机具申请订单信息

        /// <summary>
        /// 删除机具申请订单信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "MachineApply", "del");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", -1);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("MachineApply", Fields, id);
            }
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 取消机具申请订单

        /// <summary>
        /// 取消机具申请订单
        /// </summary>
        /// <returns></returns>
        public string Cancel(int Id)
        {
            AddSysLog(Id, "MachineApply", "cancel");
            MachineApply apply = db.MachineApply.FirstOrDefault(m => m.Id == Id);
            if (apply != null)
            {
                Orders order = db.Orders.FirstOrDefault(m => m.Id == apply.QueryCount);
                if (order != null)
                {
                    string SwapSnExpand = apply.SwapSnExpand;
                    if (!string.IsNullOrEmpty(SwapSnExpand))
                    {
                        string key = function.MD5_16(apply.UserId + apply.OrderExpand);
                        string[] list = SwapSnExpand.TrimEnd('\n').Split('\n');
                        if (apply.Sort > 0)
                        {
                            int Kind = apply.Sort - 1;
                            string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
                            int CouponCount = 0;
                            foreach (string sub in list)
                            {
                                PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.ExchangeCode == sub && m.UserId == order.UserId && m.IsUse == 1);
                                if (coupon != null)
                                {
                                    coupon.IsUse = 0;
                                    coupon.IsLock = 0;
                                    coupon.UseDate = DateTime.Parse("1900-01-01");
                                    PosCouponRecord query = db.PosCouponRecord.Add(new PosCouponRecord()
                                    {
                                        QueryCount = Kind,
                                        CreateDate = DateTime.Now,
                                        ChangeKind = 1,
                                        OrderNo = ChangeRecordNo,
                                        ToUserId = order.UserId,
                                        FromUserId = order.UserId,
                                        PosCouponId = coupon.Id,
                                    }).Entity;
                                    CouponCount += 1;
                                }
                            }
                            if (CouponCount > 0)
                            {
                                PosCouponForUser forUser = db.PosCouponForUser.FirstOrDefault(m => m.Id == order.UserId);
                                if (forUser == null)
                                {
                                    forUser = db.PosCouponForUser.Add(new PosCouponForUser()
                                    {
                                        Id = order.UserId,
                                    }).Entity;
                                    db.SaveChanges();
                                }
                                int BeforeOut = forUser.OutNum;
                                int BeforeTotal = forUser.TotalNum;
                                int BeforeStock = forUser.StockNum;
                                forUser.StockNum += CouponCount;
                                int AfterOut = forUser.OutNum;
                                int AfterTotal = forUser.TotalNum;
                                int AfterStock = forUser.StockNum;
                                PosCouponOrders stat = db.PosCouponOrders.Add(new PosCouponOrders()
                                {
                                    QueryCount = Kind,
                                    CreateDate = DateTime.Now,
                                    ChangeKind = 1,
                                    ChangeCount = CouponCount,
                                    AfterOut = AfterOut,
                                    AfterTotal = AfterTotal,
                                    AfterStock = AfterStock,
                                    BeforeOut = BeforeOut,
                                    BeforeTotal = BeforeTotal,
                                    BeforeStock = BeforeStock,
                                    OrderNo = ChangeRecordNo,
                                    ToUserId = order.UserId,
                                    FromUserId = order.UserId,
                                }).Entity;
                            }
                            function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(apply) + "#cut#" + Newtonsoft.Json.JsonConvert.SerializeObject(order) + "#cut#" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "机具申请订单取消日志");
                            db.MachineApply.Remove(apply);
                            db.Orders.Remove(order);
                            db.SaveChanges();
                        }
                        else
                        {
                            foreach (string sub in list)
                            {
                                string SourcePosSn = sub.Split(':')[0];
                                MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SourcePosSn) ?? new MachineForSnNo();
                                PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId);
                                if (pos != null)
                                {
                                    function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(apply) + "#cut#" + Newtonsoft.Json.JsonConvert.SerializeObject(order) + "#cut#" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "机具申请订单取消日志");
                                    pos.IsPurchase = 0;
                                }
                            }
                            db.MachineApply.Remove(apply);
                            db.Orders.Remove(order);
                            db.SaveChanges();
                        }
                        RedisDbconn.Instance.Clear("ConfirmApply2:" + key);
                    }
                }
            }
            return "success";
        }

        #endregion

        #region 开启

        /// <summary>
        /// 开启
        /// </summary>
        /// <returns></returns>
        public string Open(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "MachineApply", "open");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 1);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("MachineApply", Fields, id);
            }
            db.SaveChanges();
            return "success";
        }

        #endregion

        #region 关闭

        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        public string Close(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "MachineApply", "close");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 0);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("MachineApply", Fields, id);
            }
            db.SaveChanges();
            return "success";
        }

        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="Id"></param>
        public string Sort(int Id, int Sort)
        {
            new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Sort("MachineApply", Sort, Id);

            AddSysLog(Id.ToString(), "MachineApply", "sort");
            return "success";
        }
        #endregion

        #region 导入数据
        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="ExcelData"></param>
        public string Import(string ExcelData)
        {
            ExcelData = HttpUtility.UrlDecode(ExcelData);
            JsonData list = JsonMapper.ToObject(ExcelData);
            for (int i = 1; i < list.Count; i++)
            {
                JsonData dr = list[i];

                db.MachineApply.Add(new MachineApply()
                {
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now,

                });
                db.SaveChanges();
            }
            AddSysLog("0", "MachineApply", "Import");
            return "success";
        }
        #endregion

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(MachineApply data, string MakerCode, string RealName, string StoreNo, string StoreName, string StoreMakerCode, string StatusSelect, string SendStatusSelect, string StoreTypeSelect)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();

            Fields.Add("ApplyNo", "1"); //申请单号
            Fields.Add("BrandId", "1"); //品牌
            Fields.Add("ApplyTime", "3"); //申请时间
            Fields.Add("ExpressNo", "1"); //快递单号
            Fields.Add("DeliveryType", "0"); //提货类型
            Fields.Add("StoreManager", "1"); //仓库联系人
            Fields.Add("SendDate", "3"); //发货时间
            Fields.Add("TopUserId", "0"); //顶级创客
            Fields.Add("SwapSnExpand", "1"); //兑换SN
            Fields.Add("SendSn", "1"); //发货SN

            string condition = " and Status>-1";
            //创客编号
            if (!string.IsNullOrEmpty(MakerCode))
            {
                condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + MakerCode + "')";
            }
            //创客名称
            if (!string.IsNullOrEmpty(RealName))
            {
                condition += " and UserId in (select UserId from UserForRealName where RealName='" + RealName + "')";
            }
            //仓库编号
            if (!string.IsNullOrEmpty(StoreNo))
            {
                condition += " and StoreId in (select StoreId from StoreForCode where Code='" + StoreNo + "')";
            }
            //仓库名称
            if (!string.IsNullOrEmpty(RealName))
            {
                condition += " and StoreId in (select StoreId from StoreForName where Name='" + RealName + "')";
            }
            //仓库归属创客编号
            if (!string.IsNullOrEmpty(StoreMakerCode))
            {
                condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + StoreMakerCode + "')";
            }
            //订单状态
            if (!string.IsNullOrEmpty(StatusSelect))
            {
                condition += " and Status=" + StatusSelect;
            }
            //发货状态
            if (!string.IsNullOrEmpty(SendStatusSelect))
            {
                condition += " and SendStatus=" + SendStatusSelect;
            }
            //仓库类型
            if (!string.IsNullOrEmpty(StoreTypeSelect))
            {
                condition += " and StoreType=" + StoreTypeSelect;
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("MachineApply", Fields, "Id desc", "0", 1, 20000, condition, "ApplyNo,UserId,TopUserId,BrandId,Areas,Address,RealName,Mobile,ApplyDeviceName,ApplyDeviceNum,ApplyTime,SendStatus,ExpressNo,ExpressName,DeliveryType,StoreId,StoreName,StoreManager,StoreManagerMobile,Remark,StoreUserId,SendDate,StoreType,Status", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                dic["BrandId"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));

                //发货仓库
                int StoreId = int.Parse(dic["StoreId"].ToString());
                StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
                dic["StoreNo"] = store.StoreNo;
                dic["StoreName"] = store.StoreName;

                //仓库归属信息
                int StoreUserId = int.Parse(dic["StoreUserId"].ToString());
                Users storeUser = db.Users.FirstOrDefault(m => m.Id == StoreUserId) ?? new Users();
                dic["StoreMakerCode"] = storeUser.MakerCode;
                int StoreType = int.Parse(dic["StoreType"].ToString());
                if (StoreType == 0) dic["StoreType"] = "总仓";
                if (StoreType == 1) dic["StoreType"] = "分仓";

                //创客信息
                int UserId = int.Parse(dic["UserId"].ToString());
                Users puser = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                dic["MakerCode"] = puser.MakerCode;
                dic["MakerName"] = puser.RealName;

                //顶级创客
                int TopUserId = int.Parse(dic["TopUserId"].ToString());
                Users tuser = db.Users.FirstOrDefault(m => m.Id == TopUserId) ?? new Users();
                dic["TopMakerCode"] = tuser.MakerCode;
                dic["TopRealName"] = tuser.RealName;

                //订单状态
                int Status = int.Parse(dic["Status"].ToString());
                if (Status == 0) dic["Status"] = "待审核";
                if (Status == 1) dic["Status"] = "申请成功";
                if (Status == 2) dic["Status"] = "申请失败";

                //提货方式
                int DeliveryType = int.Parse(dic["DeliveryType"].ToString());
                if (DeliveryType == 0) dic["DeliveryType"] = "";
                if (DeliveryType == 1) dic["DeliveryType"] = "邮寄到付";
                if (DeliveryType == 2) dic["DeliveryType"] = "上门自提";

                //发货状态
                int SendStatus = int.Parse(dic["SendStatus"].ToString());
                if (SendStatus == 0) dic["SendStatus"] = "未发货";
                if (SendStatus == 1) dic["SendStatus"] = "已发货";

                dic.Remove("UserId");
                dic.Remove("TopUserId");
                dic.Remove("StoreId");
                dic.Remove("StoreUserId");
            }

            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("Status", "1");
            result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
            result.Add("Obj", diclist);
            Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
            ReturnFields.Add("ApplyNo", "申请单号");
            ReturnFields.Add("StoreNo", "发货仓库编号");
            ReturnFields.Add("StoreName", "发货仓库名称");
            ReturnFields.Add("StoreMakerCode", "仓库归属创客编号");
            ReturnFields.Add("StoreRealName", "仓库归属创客名称");
            ReturnFields.Add("MakerCode", "创客编号");
            ReturnFields.Add("MakerName", "创客名称");
            ReturnFields.Add("StoreType", "仓库类型");
            ReturnFields.Add("StoreManager", "仓库联系人");
            ReturnFields.Add("StoreManagerMobile", "仓库联系人手机号");
            ReturnFields.Add("BrandId", "产品类型");
            ReturnFields.Add("Areas", "收货所在地区");
            ReturnFields.Add("Address", "收货详细地址");
            ReturnFields.Add("RealName", "收件人姓名");
            ReturnFields.Add("Mobile", "收件人手机号");
            ReturnFields.Add("ApplyDeviceName", "申请机具名称");
            ReturnFields.Add("ApplyDeviceNum", "申请机具数量");
            ReturnFields.Add("Status", "订单状态");
            ReturnFields.Add("ApplyTime", "申请时间");
            ReturnFields.Add("SendStatus", "发货状态");
            ReturnFields.Add("SendDate", "发货时间");
            ReturnFields.Add("ExpressNo", "快递单号");
            ReturnFields.Add("ExpressName", "快递名称");
            ReturnFields.Add("DeliveryType", "提货类型");
            ReturnFields.Add("Remark", "订单备注");

            result.Add("Fields", ReturnFields);
            AddSysLog("0", "MachineApply", "ExportExcel");
            return Json(result);
        }

        #endregion





        #region 审核发货

        public IActionResult AuditSend(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            MachineApply editData = db.MachineApply.FirstOrDefault(m => m.Id == Id) ?? new MachineApply();
            ViewBag.data = editData;
            return View();
        }

        #endregion

        #region 审核发货

        [HttpPost]
        public string AuditSend(MachineApply data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();
            Fields.Add("ExpressNo", data.ExpressNo);
            Fields.Add("ExpressName", data.ExpressName);
            Fields.Add("StoreId", data.StoreId);
            Fields.Add("AuditResult", data.AuditResult);
            Fields.Add("SendSn", data.SendSn);
            Fields.Add("SendStatus", 1);

            new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("MachineApply", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "MachineApply", "AuditSend");
            db.SaveChanges();

            return "success";
        }

        #endregion

    }
}
