using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Comm.Cookie;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.Areas.RptMng.Comm;
using HcUtility.Comm;
using HcUtility.Core;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.EntityDA;
using DSWeb.Areas.CommMng.Models;
using DSWeb.MvcShipping.DAL.MsRptSaleJieSuanTiChengDAL;
using DSWeb.MvcShipping.Models.MsOpBillProfit;

namespace DSWeb.MvcShipping.Controllers
{
    /// <summary>
    /// 鼎视结算提成报表//需求编号:SR2017072100012
    /// </summary>
    [JsonRequestBehavior]
    public class MsRptSaleJieSuanTiChengController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult DsIndex()
        {
            return View();
        }

        #region 查询
        public ContentResult GetDataList(int start, int limit, string condition, string stlcondition, string dcondition, string INTEREST_FEE, string sort, string printstr, string sortstr, string dcondition2 = "")
 
        {
            var dataList = MsRptSaleJieSuanTiChengDAL.GetDataList(condition, stlcondition, dcondition, dcondition2, INTEREST_FEE, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]), sort);
            var list = dataList.Skip(start).Take(limit);
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
            return new ContentResult() { Content = json };
        }
        public ContentResult GetDataList2(int start, int limit, string condition, string stlcondition, string dcondition, string INTEREST_FEE, string sort, string printstr, string sortstr, string dcondition2 = "")
        {
            var dataList = MsRptSaleJieSuanTiChengDAL.GetDataList2(condition, stlcondition, dcondition, dcondition2, INTEREST_FEE, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]), sort);
            var list = dataList.Skip(start).Take(limit);
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
            return new ContentResult() { Content = json };
        }

        public ContentResult GetDsDataList(int start, int limit, string condition, string sort, string printstr, string sortstr)
        {

            var strSql = new StringBuilder();

                strSql.Append("SELECT  B.BSNO,B.MBLNO,S.BILLNO,S.SETTLETIME,B.CUSTOMERNAME,B.BSSOURCE,B.BSSOURCEDETAIL,B.SALE,B.CUSTSERVICE,B.OPDATE,G.TTLDR TTLDR,G.TTLCR TTLCR");
                strSql.Append(",G.TTLDR-G.TTLCR AS TTLPROFIT,SUM(S.ORIGAMOUNT) STLAMOUNT,G.TTLDR-G.STLTTLDR TTLNODR,datediff(day,B.OPDATE,S.SETTLETIME)+1  STLDATE ");
                strSql.Append(" FROM V_OP_BILL B ");
                strSql.Append(" LEFT JOIN v_op_gain_sum G  ON (G.BSNO=B.BSNO)  ");
                strSql.Append(" LEFT JOIN v_fee_do_settlement s ON (s.BSNO=B.BSNO)  ");
                strSql.Append(" WHERE 1=1 ");

                if (!string.IsNullOrEmpty(condition))
                {
                    strSql.Append(" and " + condition);
                }
                strSql.Append(" GROUP BY B.BSNO,B.MBLNO,S.BILLNO,S.SETTLETIME,B.CUSTOMERNAME,B.BSSOURCE,B.BSSOURCEDETAIL,B.SALE,B.CUSTSERVICE,B.OPDATE,G.TTLDR,G.TTLCR ");
               strSql.Append(" ,G.TTLDR-G.TTLCR,G.TTLDR-G.STLTTLDR ");


            if ((!string.IsNullOrEmpty(printstr)) && (printstr == "true"))
            {
                if (!string.IsNullOrEmpty(sortstr))
                {
                    strSql.Append(" order by " + sortstr);
                }
                else
                {

                    strSql.Append(" order by B.BSNO DESC");

                }

                var jsonRespose = new JsonResponse
                {
                    Success = true,
                    Message = condition,
                    Data = strSql.ToString()
                };

                return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
            }
            else
            {

                var sortstring = DatasetSort.Getsortstring(sort);
                if (!string.IsNullOrEmpty(sortstring))
                {
                    strSql.Append(" order by " + sortstring);
                }
                else
                {

                    strSql.Append(" order by B.BSNO DESC");

                }
                var dbparams = new List<CustomDbParamter>();

                var paramps_sSQL = new CustomDbParamter();
                paramps_sSQL.ParameterName = "@sSQL";
                paramps_sSQL.DbType = DbType.String;
                paramps_sSQL.Direction = ParameterDirection.Input;
                paramps_sSQL.Value = strSql.ToString();
                dbparams.Add(paramps_sSQL);



                var dbRptResult = PubSysDAL.GetMsSqlPrcDataSet("sMsExesqlQry", dbparams, "Result_Set");
                var json = RptHelper.GetRptJsonResult(start, limit, dbRptResult, "Result_Set", true);
                return new ContentResult() { Content = json };
            }
        }


        #endregion

        #region 查询_合计
        public ContentResult GetDataListSum(int start, int limit, string condition, string stlcondition, string dcondition, string INTEREST_FEE, string sort, string printstr, string sortstr, string dcondition2 = "")
        {
            var dataList = MsRptSaleJieSuanTiChengDAL.GetDataListSum(condition, stlcondition, dcondition, dcondition2, INTEREST_FEE, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]), sort);
            var list = dataList.Skip(start).Take(limit);
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
            return new ContentResult() { Content = json };
        }
        public ContentResult GetDataListSum2(int start, int limit, string condition, string stlcondition, string dcondition, string INTEREST_FEE, string sort, string printstr, string sortstr, string dcondition2 = "")
        {
            var dataList = MsRptSaleJieSuanTiChengDAL.GetDataListSum2(condition, stlcondition, dcondition, dcondition2, INTEREST_FEE, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]), sort);
            var list = dataList.Skip(start).Take(limit);
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
            return new ContentResult() { Content = json };
        }
        #endregion

        #region 打印
        public ContentResult GetDataListStr(int start, int limit, string condition, string stlcondition, string dcondition, string INTEREST_FEE, string sort, string printstr, string sortstr, string dcondition2 = "")
        {
            var dataListStr = MsRptSaleJieSuanTiChengDAL.GetDataListStr(condition, stlcondition, dcondition, dcondition2, INTEREST_FEE, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]), sort);
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", data = dataListStr });
            return new ContentResult() { Content = json };
        }
        #endregion

        #region 发放提成
        public ContentResult SetFee(string data)
        {

            var bodyList = JsonConvert.Deserialize<List<MsOpBillProfit>>(data);

            DBResult result = MsRptSaleJieSuanTiChengDAL.SetFee(bodyList, Convert.ToString(Session["USERID"]));

            var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
        }
        #endregion

        #region 取消发放
        public ContentResult CancelSetFee(string data)
        {

            var bodyList = JsonConvert.Deserialize<List<MsOpBillProfit>>(data);

            DBResult result = MsRptSaleJieSuanTiChengDAL.CancelSetFee(bodyList, Convert.ToString(Session["USERID"]));

            var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
        }
        #endregion
    }
}