using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using NLog;
using Sugar.Enties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using WebAPIBase.Utils;

namespace WebAPIBase.API.Controllers
{
    /// <summary>
    /// 供应商申请相关
    /// </summary>
    [Produces("application/json;charset=UTF-8")]
    [Route("api/SupplierApply")]
    [ServiceFilter(typeof(Filter.TokenAuthorize))]
    public class SupplierApplyController : Controller
    {
        private static Logger logger = NLog.LogManager.GetCurrentClassLogger();
        #region 供应商扩展信息
        /// <summary>
        /// 根据供应商编码获取供应商扩展信息
        /// </summary>
        /// <param name="supplierCode">供应商编号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetzzSupplierExList")]
        public JsonResult GetzzSupplierExList(string supplierCode)
        {

            if (supplierCode.IsNullOrEmpty())
            {

                return Json("-1");
            }
            try
            {
                var service = new zzSupplierExManager();
                var list = service.GetzzSupplierExDTOList(supplierCode);

                return Json(list);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json("-2");
            }
        }

        /// <summary>
        /// 根据项目获取供应商申请的信息
        /// </summary>
        /// <param name="projectCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetzzSupplierExListByProject")]
        public JsonResult GetzzSupplierExListByProject(string projectCode)
        {

            if (projectCode.IsNullOrEmpty())
            {
                return Json("参数错误");
            }
            try
            {
                var service = new zzSupplierExManager();
                var list = service.GetzzSupplierExListByProject(projectCode);

                return Json(list);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(ex.Message + "\r\n" + ex.StackTrace);
            }
        }

        /// <summary>
        /// 获取供应商扩展实例信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetzzSupplierEx")]
        public JsonResult GetzzSupplierEx(int id)
        {

            if (id.IsNullOrEmpty())
            {
                return Json("参数错误");
            }
            try
            {
                var bll = new RequisitionManager();
                var service = new zzSupplierExManager();
                var entity = service.GetzzSupplierExDTO(id);
                var attachments = bll.GetAttachMents(id.ToString(), "zzSupplierExAttach");
                var data = new
                {
                    entity = entity,
                    attachments = attachments
                };

                return Json(data);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(ex.Message + "\r\n" + ex.StackTrace);
            }
        }

        /// <summary>
        /// 供应商扩展信息添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertzzSupplierEx")]
        public JsonResult InsertzzSupplierEx([FromBody] zzSupplierEx dto)
        {
            logger.Info($"【InsertzzSupplierEx】dto:{JsonConvert.SerializeObject(dto)}");

            if (dto.Title.IsNullOrEmpty())
            {

                return Json("标题不能为空");
            }
            if (dto.Title.Length > 200)
            {
                return Json("标题不能超过200字");
            }
            if (dto.Reason.IsNullOrEmpty())
            {

                return Json("原因不能为空");
            }
            if (dto.Reason.Length > 500)
            {

                return Json("原因不能超过500字");
            }
            try
            {
                dto.CheckDate = null;
                dto.CheckPerson = "";
                var service = new zzSupplierExManager();
                var id = service.Db.Insertable(dto).ExecuteReturnIdentity();
                if (id > 0)
                {

                    return Json(id);
                }
                else
                {

                    return Json("插入失败");
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);

                return Json(ex.Message);
            }

        }
        /// <summary>
        /// 更新供应商申请
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdatezzSupplierEx")]
        public JsonResult UpdatezzSupplierEx([FromBody] zzSupplierEx dto)
        {
            logger.Info($"【UpdatezzSupplierEx】dto:{JsonConvert.SerializeObject(dto)}");
            if (dto.ID.IsNullOrEmpty())
            {
                return Json("参数错误，不能审核");
            }
            if (dto.Title.IsNullOrEmpty())
            {

                return Json("标题不能为空");
            }
            if (dto.Title.Length > 200)
            {
                return Json("标题不能超过200字");
            }
            if (dto.Reason.IsNullOrEmpty())
            {

                return Json("原因不能为空");
            }
            if (dto.Reason.Length > 500)
            {

                return Json("原因不能超过500字");
            }
            try
            {
                var service = new zzSupplierExManager();
                dto.CheckDate = DateTime.Now;
                var row = service.Db.Updateable(dto).ExecuteCommand();
                if (row > 0)
                {
                    return Json("success");
                }
                else
                {

                    return Json("更新失败");
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(ex.Message + "\r\n" + ex.StackTrace);
            }

        }
        /// <summary>
        /// 管理员审核供应商提交的信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AuditzzSupplierEx")]
        public JsonResult AuditzzSupplierEx([FromBody] zzSupplierEx dto)
        {
            logger.Info($"【AuditzzSupplierEx】dto:{JsonConvert.SerializeObject(dto)}");
            if (dto.ID.IsNullOrEmpty()||dto.ProjectCode.IsNullOrEmpty())
            {
                return Json("参数错误或无权限，不能审核");
            }

            try
            {
                var service = new zzSupplierExManager();
                var model = service.GetById(dto.ID);
                if(model.ProjectCode!=dto.ProjectCode)
                {
                    return Json("无权限操作");
                }
                if(model.State!=1)
                {
                    return Json("非审核中状态，不能操作");
                }
                var success = service.AuditzzSupplierEx(dto);
                if (success)
                {
                    return Json("success");
                }
                else
                {

                    return Json("审核失败");
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(ex.Message + "\r\n" + ex.StackTrace);
            }

        }

        /// <summary>
        /// 提交供应商申请信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SubmitzzSupplierEx")]
        public JsonResult SubmitzzSupplierEx([FromBody] zzSupplierEx dto)
        {
            logger.Info($"【SubmitzzSupplierEx】dto:{JsonConvert.SerializeObject(dto)}");
            if (dto.ID.IsNullOrEmpty() || dto.SupplierCode.IsNullOrEmpty())
            {
                return Json("参数错误或无权限，不能审核");
            }

            try
            {
                var service = new zzSupplierExManager();
                var model = service.GetById(dto.ID);
                if (model.SupplierCode != dto.SupplierCode)
                {
                    return Json("无权限操作");
                }
                if (model.State != 0)
                {
                    return Json("非待审核状态，不能操作");
                }
                var success = service.SubmitzzSupplierEx(dto);
                if (success)
                {
                    return Json("success");
                }
                else
                {

                    return Json("提交失败");
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(ex.Message + "\r\n" + ex.StackTrace);
            }

        }

        /// <summary>
        /// 删除供应商扩展信息
        /// </summary>
        /// <param name="suppliercode"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("DeletezzSupplierEx")]
        public JsonResult DeletezzSupplierEx(string suppliercode, int id)
        {

            if (suppliercode.IsNullOrEmpty())
            {
                return Json("参数错误，不能删除");
            }
            if (id <= 0)
            {
                return Json("参数错误，不能删除");
            }

            try
            {
                var service = new zzSupplierExManager();
                var success = service.Delete(m => m.SupplierCode == suppliercode && m.ID == id);
                if (success)
                {

                    return Json("success");

                }
                else
                {

                    return Json("删除失败");
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return Json(ex.Message + "\r\n" + ex.StackTrace);
            }

        }
        /// <summary>
        /// 根据供应商编号获取其对应所有项目
        /// </summary>
        /// <param name="supplierCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetProjectBySupplierCode")]
        public JsonResult GetProjectBySupplierCode(string supplierCode)
        {

            if (supplierCode.IsNullOrEmpty())
            {
                return Json("请输入供应商编号");
            }
            try
            {
                var service = new zzSupplierExManager();
                var list = service.GetProjectBySupplierCode(supplierCode);
                logger.Info($"【GetProjectBySupplierCode】list:" + JsonConvert.SerializeObject(list));
                return Json(list);
            }
            catch (Exception ex)
            {
                return Json(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        #endregion
    }
}
