using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXDT.CallCenter.Model;
using ZXDT.CallCenter.MVCWeb.Models;

namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class LostGoodsController : BaseController
    {
        BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
        BLL.T_Good_Info bllGoods = new BLL.T_Good_Info();
        BLL.T_Good_TransInfo trgoodBLL = new BLL.T_Good_TransInfo();
        #region 图片上传


        /// <summary>
        /// 图片上传
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UploadFile()
        {
            try
            {
                if (Request.Files.Count > 0)
                {
                    string uppath = string.Empty;
                    string savepath = string.Empty;
                    HttpPostedFileBase imgFile = Request.Files[0];
                    if (imgFile != null)
                    {
                        //创建图片新的名称
                        string nameImg = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        //获得上传图片的路径
                        string strPath = imgFile.FileName;
                        //获得上传图片的类型(后缀名)
                        string type = strPath.Substring(strPath.LastIndexOf(".") + 1).ToLower();
                        //拼写数据库保存的相对路径字符串
                        savepath = "/Content/Upload/images/";
                        //拼写上传图片的路径
                        uppath = Server.MapPath("~/Content/Upload/images/");
                        if (!Directory.Exists(Path.GetDirectoryName(uppath)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(uppath));
                        }
                        uppath += nameImg + "." + type;
                        savepath += nameImg + "." + type;
                        //上传图片
                        imgFile.SaveAs(uppath); //原图片路径
                    }
                    return Json(new
                    {
                        code = 0,
                        src = savepath,
                        msg = "上传成功"
                    });
                }
                else
                {
                    return Json(new
                    {
                        code = 1,
                        src = "",
                        msg = "上传出错 请检查图片名称或图片内容"
                    });
                }
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    code = 1,
                    src = "",
                    msg = "上传出错: " + ex.Message
                });
            }


        }


        #endregion

        #region 查询所有失物信息
        public ActionResult LostGoodsAllList(int? rightFlag)
        {
            LostGoodsModel viewModel = new LostGoodsModel();
            if (rightFlag == null)
            {
                viewModel.RightFlag = 1;
            }
            else
            {
                viewModel.RightFlag = (int)rightFlag;
            }
            //物品种类
            viewModel.GoodsKindsModelList = GetCodeType("WPZL");
            //物品属性
            viewModel.GoodsPropertyModelList = GetCodeType("WPSX");
            return View(viewModel);
        }

        /// <summary>
        /// 获取失物记录信息
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("GetRegistALLData")]
        [HttpGet]
        public string GetRegistALLData(DateTime? NowDateTime, int page, int limit, string getDate, string goodsName, int? goodsProty, int? goodsKinds, string getPlace, string getDetail, int? goodsStatus)
        {
            //自动销毁
            bllGoods.DeleteGoodsBySys();

            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = "";
            string startDate = "";
            string endDate = "";
            if (!string.IsNullOrEmpty(getDate))
            {
                startDate = getDate.Substring(0, 10);
                endDate = getDate.Substring(12);
                sql += " and F_GetDate>='" + startDate + " 00:00:00'";

                sql += " and F_GetDate<='" + endDate + " 23:59:59'";

            }
            if (!string.IsNullOrEmpty(goodsName))
            {
                sql += " and F_GoodsName like '%" + goodsName.Trim() + "%'";
            }
            if (goodsProty != null && goodsProty > 0)
            {
                sql += " and F_PropertyId=" + goodsProty;
            }
            if (goodsKinds != null && goodsKinds > 0)
            {
                sql += " and F_KindsId=" + goodsKinds;
            }
            if (!string.IsNullOrEmpty(getPlace))
            {
                sql += " and F_PlaceName like '%" + getPlace.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(getDetail))
            {
                sql += " and F_Remark like '%" + getDetail.Trim() + "%'";
            }
            if (goodsStatus != null)
            {

                sql += " and F_Status=" + goodsStatus;

            }
            sql += " and F_Status<9 ";
            DataTable dt = new DataTable();
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Good_Info",
                "F_Id",
                "*,CONVERT(varchar, F_GetDate, 120) as F_GetDateNew,CONVERT(varchar, F_CreateDate, 120) as F_CreateDateNew",
                sql,
                "ORDER BY F_Id desc",
                limit,
                page,
                true,
                out recordCount);

            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;
            string json = JsonConvert.SerializeObject(dataModel);
            return json;
        }

        #endregion

        #region 获取站点信息

        /// <summary>
        /// 获取字典信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPathInfo()
        {
            SelectInfoTypeModel modelRes = new SelectInfoTypeModel();
            List<T_Sys_DictionaryValue> modelList = new List<T_Sys_DictionaryValue>();
            BLL.T_Sys_DictionaryValue dicValueBll = new BLL.T_Sys_DictionaryValue();
            modelList = dicValueBll.GetModelList("F_DictionaryFlag='ZDXX'");

            if (modelList != null && modelList.Count > 0)
            {
                foreach (T_Sys_DictionaryValue model in modelList)
                {
                    model.DictionaryValueModelList = dicValueBll.GetModelList("F_DictionaryFlag='" + model.F_ValueCode + "'");
                }
            }
            modelRes.modelList = modelList;
            return View(modelRes);
        }
        #endregion

        #region 失物丢失登记

        /// <summary>
        /// 失物丢失登记列表
        /// </summary>
        /// <returns></returns>
        public ActionResult LostGoodsRegistList()
        {
            LostGoodsModel viewModel = new LostGoodsModel();
            //物品种类
            viewModel.GoodsKindsModelList = GetCodeType("WPZL");
            //物品属性
            viewModel.GoodsPropertyModelList = GetCodeType("WPSX");
            return View(viewModel);
        }

        /// <summary>
        /// 失物录入
        /// </summary>
        /// <returns></returns>
        public ActionResult LostGoodsRegist()
        {
            LostGoodsModel viewModel = new LostGoodsModel();
            //物品种类
            viewModel.GoodsKindsModelList = GetCodeType("WPZL");
            //物品属性
            viewModel.GoodsPropertyModelList = GetCodeType("WPSX");
            return View(viewModel);
        }

        /// <summary>
        /// 归还编辑
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public ActionResult LostGoodsRegistEdit(int fid)
        {

            Model.T_Good_Info model = bllGoods.GetModel(fid);
            //物品种类
            model.GoodsKindsModelList = GetCodeType("WPZL");
            //物品属性
            model.GoodsPropertyModelList = GetCodeType("WPSX");
            return View(model);
        }

        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveRegistInfoEdit(Model.T_Good_Info model)
        {
           
            return bllGoods.Update(model);
        }

        /// <summary>
        /// 首次录入保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveRegistInfo(Model.T_Good_Info model)
        {
            BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
            BLL.T_Good_Info bllGoods = new BLL.T_Good_Info();
            model.F_SaveDepeId = model.F_UserId = F_UserID;
            model.F_PlaceName = model.F_SaveDepeName = model.F_UserName = userBLL.GetModel(F_UserID).F_UserName;
            model.F_CreateDate = DateTime.Now;
            model.F_Status = 0;
            model.F_StatusName = "未归还";
            return bllGoods.Add(model) > 0;
        }


        /// <summary>
        /// 获取失物记录信息
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("GetRegistData")]
        [HttpGet]
        public string GetRegistData(DateTime? NowDateTime, int page, int limit, string getDate, string goodsName, int? goodsProty, int? goodsKinds, string getPlace, string getDetail, int? goodsStatus)
        {

            //自动销毁
            bllGoods.DeleteGoodsBySys();
            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = " and F_SaveDepeId=" + F_UserID + " and F_Status<=1 ";
            string startDate = "";
            string endDate = "";
            if (!string.IsNullOrEmpty(getDate))
            {
                startDate = getDate.Substring(0, 10);
                endDate = getDate.Substring(12);
                sql += " and F_GetDate>='" + startDate + " 00:00:00'";

                sql += " and F_GetDate<='" + endDate + " 23:59:59'";

            }
            if (!string.IsNullOrEmpty(goodsName))
            {
                sql += " and F_GoodsName like '%" + goodsName.Trim() + "%'";
            }
            if (goodsProty != null && goodsProty > 0)
            {
                sql += " and F_PropertyId=" + goodsProty;
            }
            if (goodsKinds != null && goodsKinds > 0)
            {
                sql += " and F_KindsId=" + goodsKinds;
            }
            if (!string.IsNullOrEmpty(getPlace))
            {
                sql += " and F_PlaceName like '%" + getPlace.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(getDetail))
            {
                sql += " and F_Remark like '%" + getDetail.Trim() + "%'";
            }
            if (goodsStatus != null)
            {

                sql += " and F_Status=" + goodsStatus;

            }
            DataTable dt = new DataTable();
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Good_Info",
                "F_Id",
                "*,(select f_id from T_Good_TransInfo  WHERE F_TStatus=0  and F_GoodInfoId=T_Good_Info.F_id) as rowKey,CONVERT(varchar, F_GetDate, 120) as F_GetDateNew,CONVERT(varchar, F_CreateDate, 120) as F_CreateDateNew",
                sql,
                "ORDER BY F_Id desc",
                limit,
                page,
                true,
                out recordCount);

            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;
            string json = JsonConvert.SerializeObject(dataModel);
            return json;
        }

        #endregion

        #region 待接收物品

        /// <summary>
        /// 失物丢失登记列表
        /// </summary>
        /// <returns></returns>
        public ActionResult LostGoodsReceivedList()
        {
            LostGoodsModel viewModel = new LostGoodsModel();
            //物品种类
            viewModel.GoodsKindsModelList = GetCodeType("WPZL");
            //物品属性
            viewModel.GoodsPropertyModelList = GetCodeType("WPSX");
            return View(viewModel);
        }
        /// <summary>
        /// 获取待接收记录信息
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("GetRegistReceivedData")]
        [HttpGet]
        public string GetRegistReceivedData(DateTime? NowDateTime, int page, int limit, string getDate, string goodsName, int? goodsProty, int? goodsKinds, string getPlace)
        {
            //自动销毁
            bllGoods.DeleteGoodsBySys();

            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = " and F_Status=1 ";
            string startDate = "";
            string endDate = "";
            if (!string.IsNullOrEmpty(getDate))
            {
                startDate = getDate.Substring(0, 10);
                endDate = getDate.Substring(12);
                sql += " and F_GetDate>='" + startDate + " 00:00:00'";

                sql += " and F_GetDate<='" + endDate + " 23:59:59'";

            }
            if (!string.IsNullOrEmpty(goodsName))
            {
                sql += " and F_GoodsName like '%" + goodsName.Trim() + "%'";
            }
            if (goodsProty != null && goodsProty > 0)
            {
                sql += " and F_PropertyId=" + goodsProty;
            }
            if (goodsKinds != null && goodsKinds > 0)
            {
                sql += " and F_KindsId=" + goodsKinds;
            }
            if (!string.IsNullOrEmpty(getPlace))
            {
                sql += " and F_PlaceName like '%" + getPlace.Trim() + "%'";
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "(SELECT t.*,m.F_Id as rowKey FROM T_Good_TransInfo m LEFT JOIN  T_Good_Info t ON t.F_Id=m.F_GoodInfoId WHERE m.F_TStatus=0 and F_RecivedDepId=" + F_UserID + ") as t",
                "F_Id",
                "*,CONVERT(varchar, F_GetDate, 120) as F_GetDateNew,CONVERT(varchar, F_CreateDate, 120) as F_CreateDateNew",
                sql,
                "ORDER BY F_Id desc",
                limit,
                page,
                true,
                out recordCount);

            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;
            string json = JsonConvert.SerializeObject(dataModel);
            return json;
        }

        /// <summary>
        /// 接收物品保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveReceivedData(string keyList, int status)
        {
            Model.T_Good_TransInfo model = new T_Good_TransInfo();

            model.KeyList = keyList;

            model.F_RecivedDepId = F_UserID;
            model.F_RecivedDepName = userBLL.GetModel(F_UserID).F_UserName;

            return bllGoods.TransferGoodReceivedList(model, status);
        }
        #endregion

        #region 失物归还

        /// <summary>
        /// 归还失物
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public ActionResult RebackLost(int fid)
        {

            Model.T_Good_Info model = bllGoods.GetModel(fid);
            return View(model);
        }
        /// <summary>
        /// 失物详情
        /// </summary>
        /// <returns></returns>
        public ActionResult LostGoodsView(int fid)
        {
            Model.T_Good_Info model = bllGoods.GetModel(fid);
            if (model != null && model.F_Id > 0)
            {
                model.TransInfoModelList = trgoodBLL.GetModelList(" F_GoodInfoId=" + fid);
            }


            return View(model);
        }

        /// <summary>
        /// 归还保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveRebackData(Model.T_Good_Info model)
        {
            BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
            BLL.T_Good_Info bllGoods = new BLL.T_Good_Info();
            model.F_Status = 2;
            model.F_StatusName = "已归还";
            return bllGoods.RebackGoods(model);
        }


        #endregion

        #region 失物转移

        /// <summary>
        /// 失物转移
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public ActionResult LostTransfer(string keyList)
        {
            BLL.T_Sys_Department deptBll = new BLL.T_Sys_Department();
            //实例化基础信息表
            LostGoodsModel lostGoodsModel = new LostGoodsModel();
            //获取部门列表
            lostGoodsModel.DepartmentList = deptBll.GetModelList("F_ParentId=0");
            lostGoodsModel.KeyList = keyList;
            return View(lostGoodsModel);
        }


        /// <summary>
        /// 转移保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveLostTransferData(Model.T_Good_TransInfo model)
        {

            return bllGoods.TransferGoodList(model);
        }


        #endregion


        #region 销毁
        /// <summary>
        /// 失物销毁
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public bool LostDelete(string keyList)
        {
            return bllGoods.DeleteGoods(keyList);
        }
        /// <summary>
        /// 失物删除
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public bool AdminDeleteGoods(string keyList)
        {
            return bllGoods.AdminDeleteGoods(keyList);
        }


        #endregion

    }
}
