using AUTS_Server.Models;
using AUTS_Server.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProjectIntegration;
using SqlSugar;
using System.Security.Cryptography;
using uts_bomei;
using uts_manage;
using XAct.Users;

namespace AUTS_Server.Controllers
{
    [Route("ITblShipping")]
    [ApiController]
    public class ITblShippingController : ControllerBase
    {
        private readonly ISqlSugarClient db;
        private readonly ILogHelperForService log;
        public ITblShippingController(ISqlSugarClient db, ILogHelperForService log)
        {
            this.db = db;
            this.log = log;
        }
        /// <summary>
        /// 添加或更新出货单
        /// </summary>
        /// <param name="shipping"></param>
        [HttpPost]
        [Route("AddOrUpdateShipping")]
        public Returninfo AddOrUpdateShipping([FromBody] JudgeTheDatabase shipping)
        {
            Returninfo rinfo = new Returninfo();
            try
            {
                switch (shipping.JudgeDatabase)
                {
                    case "uts_bomei":
                        if (shipping.ID == 0)
                        {
                            //添加
                            tbl_shipping tblshipping = shipping;
                            tblshipping.CreateDateTime = DateTime.Now;
                            int result = db.AsTenant().GetConnection(4).Insertable<tbl_shipping>(tblshipping).ExecuteCommand();
                            if (result > 0)
                            {
                                //添加成功
                                rinfo.status = 200;
                                rinfo.message = "添加成功";
                                rinfo.isok = true;
                            }
                            else
                            {
                                //添加失败
                                rinfo.status = 400;
                                rinfo.message = "添加失败";
                                rinfo.isok = false;
                            }
                        }
                        else
                        {
                            //修改
                            tbl_shipping list = db.AsTenant().GetConnection(4).Queryable<tbl_shipping>().Where(it => it.ID == shipping.ID).First();
                            if (list != null)
                            {
                                list.ShippingCode = shipping.ShippingCode;
                                list.Destination = shipping.Destination;
                                list.Voucher = shipping.Voucher;
                                list.Remark = shipping.Remark;
                                list.UpdateTime = DateTime.Now;
                                list.Status = shipping.Status;
                                list.ShippingType = shipping.ShippingType;
                                list.ProjectID = shipping.ProjectID;
                                list.QTY = shipping.QTY;
                                list.MO = shipping.MO;
                                list.Customer = shipping.Customer;
                                list.BindedColorBoxQTY = shipping.BindedColorBoxQTY;
                                list.Loader = shipping.Loader;
                                list.EndDateTime = shipping.EndDateTime;
                                list.StartDateTime = shipping.StartDateTime;
                                list.Creator = shipping.Creator;
                                list.ShippingCode = shipping.ShippingCode;
                                list.CreateDateTime = shipping.CreateDateTime;
                                int result = db.AsTenant().GetConnection(4).Updateable(list).Where(it => it.ID == shipping.ID).ExecuteCommand();
                                if (result > 0)
                                {
                                    //修改成功
                                    rinfo.status = 200;
                                    rinfo.message = "修改成功";
                                    rinfo.isok = true;
                                }
                                else
                                {
                                    //修改失败
                                    rinfo.status = 400;
                                    rinfo.message = "修改失败";
                                    rinfo.isok = false;
                                }
                            }
                            else
                            {
                                //修改失败
                                rinfo.status = 400;
                                rinfo.message = "没有找到该记录";
                                rinfo.isok = false;
                            }

                        }
                        break;
                    case "uts_dtl":
                        if (shipping.ID == 0)
                        {
                            //添加
                            tbl_shipping tblshipping = shipping;
                            tblshipping.CreateDateTime = DateTime.Now;
                            int result = db.AsTenant().GetConnection(3).Insertable<tbl_shipping>(tblshipping).ExecuteCommand();
                            if (result > 0)
                            {
                                //添加成功
                                rinfo.status = 200;
                                rinfo.message = "添加成功";
                                rinfo.isok = true;
                            }
                            else
                            {
                                //添加失败
                                rinfo.status = 400;
                                rinfo.message = "添加失败";
                                rinfo.isok = false;
                            }
                        }
                        else
                        {
                            //修改
                            tbl_shipping list = db.AsTenant().GetConnection(3).Queryable<tbl_shipping>().Where(it => it.ID == shipping.ID).First();
                            if (list != null)
                            {
                                list.ShippingCode = shipping.ShippingCode;
                                list.Destination = shipping.Destination;
                                list.Voucher = shipping.Voucher;
                                list.Remark = shipping.Remark;
                                list.UpdateTime = DateTime.Now;
                                list.Status = shipping.Status;
                                list.ShippingType = shipping.ShippingType;
                                list.ProjectID = shipping.ProjectID;
                                list.QTY = shipping.QTY;
                                list.MO = shipping.MO;
                                list.Customer = shipping.Customer;
                                list.BindedColorBoxQTY = shipping.BindedColorBoxQTY;
                                list.Loader = shipping.Loader;
                                list.EndDateTime = shipping.EndDateTime;
                                list.StartDateTime = shipping.StartDateTime;
                                list.Creator = shipping.Creator;
                                list.ShippingCode = shipping.ShippingCode;
                                list.CreateDateTime = shipping.CreateDateTime;
                                int result = db.AsTenant().GetConnection(3).Updateable(list).Where(it => it.ID == shipping.ID).ExecuteCommand();
                                if (result > 0)
                                {
                                    //修改成功
                                    rinfo.status = 200;
                                    rinfo.message = "修改成功";
                                    rinfo.isok = true;
                                }
                                else
                                {
                                    //修改失败
                                    rinfo.status = 400;
                                    rinfo.message = "修改失败";
                                    rinfo.isok = false;
                                }
                            }
                            else
                            {
                                //修改失败
                                rinfo.status = 400;
                                rinfo.message = "没有找到该数据";
                                rinfo.isok = false;
                            }
                        }
                        break;
                    case "uts_inhaos":
                        if (shipping.ID == 0)
                        {
                            //添加
                            tbl_shipping tblshipping = shipping;
                            tblshipping.CreateDateTime = DateTime.Now;
                            int result = db.AsTenant().GetConnection(2).Insertable<tbl_shipping>(tblshipping).ExecuteCommand();
                            if (result > 0)
                            {
                                //添加成功
                                rinfo.status = 200;
                                rinfo.message = "添加成功";
                                rinfo.isok = true;
                            }
                            else
                            {
                                //添加失败
                                rinfo.status = 400;
                                rinfo.message = "添加失败";
                                rinfo.isok = false;
                            }
                        }
                        else
                        {
                            //修改
                            tbl_shipping list = db.AsTenant().GetConnection(2).Queryable<tbl_shipping>().Where(it => it.ID == shipping.ID).First();
                            if (list != null)
                            {
                                list.ShippingCode = shipping.ShippingCode;
                                list.Destination = shipping.Destination;
                                list.Voucher = shipping.Voucher;
                                list.Remark = shipping.Remark;
                                list.UpdateTime = DateTime.Now;
                                list.Status = shipping.Status;
                                list.ShippingType = shipping.ShippingType;
                                list.ProjectID = shipping.ProjectID;
                                list.QTY = shipping.QTY;
                                list.MO = shipping.MO;
                                list.Customer = shipping.Customer;
                                list.BindedColorBoxQTY = shipping.BindedColorBoxQTY;
                                list.Loader = shipping.Loader;
                                list.EndDateTime = shipping.EndDateTime;
                                list.StartDateTime = shipping.StartDateTime;
                                list.Creator = shipping.Creator;
                                list.ShippingCode = shipping.ShippingCode;
                                list.CreateDateTime = shipping.CreateDateTime;
                                int result = db.AsTenant().GetConnection(2).Updateable(list).Where(it => it.ID == shipping.ID).ExecuteCommand();
                                if (result > 0)
                                {
                                    //修改成功
                                    rinfo.status = 200;
                                    rinfo.message = "修改成功";
                                    rinfo.isok = true;
                                }
                                else
                                {
                                    //修改失败
                                    rinfo.status = 400;
                                    rinfo.message = "修改失败";
                                    rinfo.isok = false;
                                }
                            }
                            else
                            {
                                //修改失败
                                rinfo.status = 400;
                                rinfo.message = "没有找到该数据";
                                rinfo.isok = false;
                            }
                        }
                        break;
                    case "uts_zongqing":
                        if (shipping.ID == 0)
                        {
                            //添加
                            tbl_shipping tblshipping = shipping;
                            tblshipping.CreateDateTime = DateTime.Now;
                            int result = db.AsTenant().GetConnection(1).Insertable<tbl_shipping>(tblshipping).ExecuteCommand();
                            if (result > 0)
                            {
                                //添加成功
                                rinfo.status = 200;
                                rinfo.message = "添加成功";
                                rinfo.isok = true;
                            }
                            else
                            {
                                //添加失败
                                rinfo.status = 400;
                                rinfo.message = "添加失败";
                                rinfo.isok = false;
                            }
                        }
                        else
                        {
                            //修改
                            tbl_shipping list = db.AsTenant().GetConnection(1).Queryable<tbl_shipping>().Where(it => it.ID == shipping.ID).First();
                            if (list != null)
                            {
                                list.ShippingCode = shipping.ShippingCode;
                                list.Destination = shipping.Destination;
                                list.Voucher = shipping.Voucher;
                                list.Remark = shipping.Remark;
                                list.UpdateTime = DateTime.Now;
                                list.Status = shipping.Status;
                                list.ShippingType = shipping.ShippingType;
                                list.ProjectID = shipping.ProjectID;
                                list.QTY = shipping.QTY;
                                list.MO = shipping.MO;
                                list.Customer = shipping.Customer;
                                list.BindedColorBoxQTY = shipping.BindedColorBoxQTY;
                                list.Loader = shipping.Loader;
                                list.EndDateTime = shipping.EndDateTime;
                                list.StartDateTime = shipping.StartDateTime;
                                list.Creator = shipping.Creator;
                                list.ShippingCode = shipping.ShippingCode;
                                list.CreateDateTime = shipping.CreateDateTime;
                                int result = db.AsTenant().GetConnection(1).Updateable(list).Where(it => it.ID == shipping.ID).ExecuteCommand();
                                if (result > 0)
                                {
                                    //修改成功
                                    rinfo.status = 200;
                                    rinfo.message = "修改成功";
                                    rinfo.isok = true;
                                }
                                else
                                {
                                    //修改失败
                                    rinfo.status = 400;
                                    rinfo.message = "修改失败";
                                    rinfo.isok = false;
                                }
                            }
                            else
                            {
                                //修改失败
                                rinfo.status = 400;
                                rinfo.message = "没有找到该数据";
                                rinfo.isok = false;
                            }
                        }
                        break;
                    default:
                        rinfo.status = 400;
                        rinfo.message = "没有找到该数据库";
                        rinfo.isok = false;
                        break;
                }
            }
            catch (Exception ex)
            {
                rinfo.status = 400;
                rinfo.message = "系统异常";
                rinfo.isok = false;
                log.WriteLine("添加修改"+shipping.JudgeDatabase + "出货单信息失败，错误信息为：" + ex.Message);
            }
            return rinfo;
        }
        /// <summary>
        /// 删除出货单接口
        /// </summary>
        /// <param name="determine"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("DeleteShipping")]
        public Returninfo DeleteShipping([FromBody] DetermineTheDatabase determine)
        {
            Returninfo rinfo = new Returninfo();
            try
            {
                switch (determine.JudgeDatabase)
                {
                    case "uts_bomei":
                        tbl_shipping blist = db.AsTenant().GetConnection(4).Queryable<tbl_shipping>().Where(x => x.ID == determine.id).First();
                        if (blist != null)
                        {
                            int countss = db.AsTenant().GetConnection(4).Deleteable(blist).Where(x => x.ID == blist.ID).ExecuteCommand();
                            if (countss > 0)
                            {
                                //修改成功
                                rinfo.status = 200;
                                rinfo.message = "删除成功";
                                rinfo.isok = true;
                            }
                            else
                            {
                                //修改失败
                                rinfo.status = 400;
                                rinfo.message = "删除失败";
                                rinfo.isok = false;
                            }
                        }
                        else
                        {
                            rinfo.status = 400;
                            rinfo.message = "没有找到该数据";
                            rinfo.isok = false;
                        }
                        break;
                    case "uts_dtl":

                        tbl_shipping dlist = db.AsTenant().GetConnection(3).Queryable<tbl_shipping>().Where(x => x.ID == determine.id).First();
                        if (dlist != null)
                        {
                            int countss = db.AsTenant().GetConnection(3).Deleteable(dlist).Where(x => x.ID == dlist.ID).ExecuteCommand();
                            if (countss > 0)
                            {
                                //修改成功
                                rinfo.status = 200;
                                rinfo.message = "删除成功";
                                rinfo.isok = true;
                            }
                            else
                            {
                                //修改失败
                                rinfo.status = 400;
                                rinfo.message = "删除失败";
                                rinfo.isok = false;
                            }
                        }
                        else
                        {
                            rinfo.status = 400;
                            rinfo.message = "没有找到该数据";
                            rinfo.isok = false;
                        }
                        break;
                    case "uts_inhaos":
                        tbl_shipping ilist = db.AsTenant().GetConnection(2).Queryable<tbl_shipping>().Where(x => x.ID == determine.id).First();
                        if (ilist != null)
                        {
                            int countss = db.AsTenant().GetConnection(2).Deleteable(ilist).Where(x => x.ID == ilist.ID).ExecuteCommand();
                            if (countss > 0)
                            {
                                //修改成功
                                rinfo.status = 200;
                                rinfo.message = "删除成功";
                                rinfo.isok = true;
                            }
                            else
                            {
                                //修改失败
                                rinfo.status = 400;
                                rinfo.message = "删除失败";
                                rinfo.isok = false;
                            }
                        }
                        else
                        {
                            rinfo.status = 400;
                            rinfo.message = "没有找到该数据";
                            rinfo.isok = false;
                        }
                        break;
                    case "uts_zongqing":
                        tbl_shipping zlist = db.AsTenant().GetConnection(1).Queryable<tbl_shipping>().Where(x => x.ID == determine.id).First();
                        if (zlist != null)
                        {
                            int countss = db.AsTenant().GetConnection(1).Deleteable(zlist).Where(x => x.ID == zlist.ID).ExecuteCommand();
                            if (countss > 0)
                            {
                                //修改成功
                                rinfo.status = 200;
                                rinfo.message = "删除成功";
                                rinfo.isok = true;
                            }
                            else
                            {
                                //修改失败
                                rinfo.status = 400;
                                rinfo.message = "删除失败";
                                rinfo.isok = false;
                            }
                        }
                        else
                        {
                            rinfo.status = 400;
                            rinfo.message = "没有找到该数据";
                            rinfo.isok = false;
                        }
                        break;
                    default:
                        rinfo.status = 400;
                        rinfo.message = "没有找到该数据库";
                        rinfo.isok = false;
                        break;
                }
            }
            catch (Exception ex)
            {

                rinfo.status = 500;
                rinfo.message = "系统异常";
                rinfo.isok = false;
                log.WriteLine("删除"+ determine.JudgeDatabase + "出货单信息失败，错误信息为：" + ex.Message);
            }
          
            return rinfo;
        }

        [HttpGet("SelectShippingAllinfo")]

        public SelectShippingInfo SelectShippingAllinfo([FromBody] SelectShippingAllinfo selectShipping)
        {
            SelectShippingInfo info = new SelectShippingInfo();
            int totalCount = 0;
            try
            {
                switch (selectShipping.JudgeDatabase)
                {
                    case "uts_bomei":
                        List<tbl_shipping> blist = db.AsTenant().GetConnection(4).Queryable<tbl_shipping>().ToPageList(selectShipping.page, 10, ref totalCount);
                        info.list = blist;
                        info.pagecount = (totalCount % 12) == 0 ? (totalCount / 12) : (totalCount / 12) + 1;
                        info.shippingcount = totalCount;
                        break;
                    case "uts_dtl":
                        List<tbl_shipping> zlist = db.AsTenant().GetConnection(3).Queryable<tbl_shipping>().ToPageList(selectShipping.page, 10, ref totalCount);
                        info.list = zlist;
                        info.pagecount = (totalCount % 12) == 0 ? (totalCount / 12) : (totalCount / 12) + 1;
                        info.shippingcount = totalCount;
                        break;
                    case "uts_inhaos":
                        List<tbl_shipping> ilist = db.AsTenant().GetConnection(2).Queryable<tbl_shipping>().ToPageList(selectShipping.page, 10, ref totalCount);
                        info.list = ilist;
                        info.pagecount = (totalCount % 12) == 0 ? (totalCount / 12) : (totalCount / 12) + 1;
                        info.shippingcount = totalCount;
                        break;
                    case "uts_zongqing":
                        List<tbl_shipping> zqlist = db.AsTenant().GetConnection(1).Queryable<tbl_shipping>().ToPageList(selectShipping.page, 10, ref totalCount);
                        info.list = zqlist;
                        info.pagecount = (totalCount % 12) == 0 ? (totalCount / 12) : (totalCount / 12) + 1;
                        info.shippingcount = totalCount;
                        break;
                    default:
                        info.list = null;
                        info.pagecount = 0;
                        info.shippingcount = 0;
                        break;
                }
            }
            catch (Exception ex)
            {
                info.list = null;
                info.pagecount = 0;
                info.shippingcount = 0;
                log.WriteLine("查询"+ selectShipping.JudgeDatabase + "出货单信息失败，错误信息为："+ex.Message);
                throw;
            }
           
            return info;
        }
    }
    public class JudgeTheDatabase : tbl_shipping
    {
        //数据库索引
        public string JudgeDatabase { get; set; }
    }
    public class DetermineTheDatabase
    {
        public int id { get; set; }
        //数据库索引
        public string JudgeDatabase { get; set; }
    }

    public class SelectShippingAllinfo
    {
        public int page { get; set; }
        //数据库索引
        public string JudgeDatabase { get; set; }
    }
    public class SelectShippingInfo
    {
        //原始数据
        public List<tbl_shipping> list { get; set; }
        //数据总数
        public int shippingcount { set; get; }
        //分页数
        public int pagecount { set; get; }
    }


}
