using System;
using System.Collections.Generic;
using System.Text;
using Bailun.DC.Models;
using Bailun.DC.DB;
using Dapper;
using MySql.Data.MySqlClient;

namespace Bailun.DC.Services
{
    public class PurchaseServices
    {
        /// <summary>
        /// 采购单列表
        /// </summary>
        /// <param name="parameter">分页信息</param>
        /// <param name="warehouse">仓库编码</param>
        /// <param name="sku">sku编码</param>
        /// <param name="no">采购单号</param>
        /// <param name="status">采购单状态</param>
        /// <param name="total">符合条件的记录数</param>
        /// <returns></returns>
        public List<Models.Purchase.mPurchase> ListPurchase(BtTableParameter parameter, string warehouse, string sku, string no, int? status, ref int total)
        {
            var sqlparam = new DynamicParameters();
            var sql = @"select t1.purchase_id,t1.`buy_status` ,t1.bailun_sku,t2.sku_image,t2.category_name,t2.sku_title_en,t2.sku_title_cn,t1.price,t1.count,(t1.price*t1.count) amount,t2.currency,t1.has_transfer,t1.supplier_name,t2.buyer_name,t1.warehouse_name as warehouse_name_from,t1.warehouse_name warehouse_name_to,t1.create_time,t1.update_time,t1.gmt_modified 
                                from dc_base_purchase t1
                                left  join dc_base_sku t2 on t1.bailun_sku = t2.bailun_sku
                                where t1.has_delete = 0";

            if (!string.IsNullOrEmpty(warehouse))
            {
                sql += " and t1.warehouse_code=@warehouse_code";
                sqlparam.Add("warehouse_code", warehouse);
            }

            if (!string.IsNullOrWhiteSpace(sku))
            {
                sql += " and t1.bailun_sku=@bailun_sku";
                sqlparam.Add("bailun_sku", sku);
            }

            if (!string.IsNullOrWhiteSpace(no))
            {
                sql += " and t1.purchase_id=@purchase_id";
                sqlparam.Add("purchase_id", no);
            }

            if (status.HasValue)
            {
                sql += " and t1.status=" + status.Value;
            }

            if (!string.IsNullOrWhiteSpace(parameter.sort))
            {
                sql += " order by " + parameter.sort;

                if (!string.IsNullOrWhiteSpace(parameter.order))
                {
                    sql += (" " + parameter.order);
                }
            }
            else
            {
                sql += " order by create_time desc";
            }

            using (var cn = new MySqlConnection(Common.GlobalConfig.ConnectionString))
            {
                if (cn.State == System.Data.ConnectionState.Closed)
                {
                    cn.Open();
                }

                var obj = cn.Page<Models.Purchase.mPurchase>(parameter.pageIndex, parameter.limit, sql, ref total,sqlparam);

                return obj.AsList();
            }

        }

        public Models.Purchase.mPurchase ListPurchaseCount(string warehouse, string sku, string no, int? status)
        {
            var sqlparam = new DynamicParameters();
            var sql = @"select sum(t1.count) count,sum(t1.price*t1.count) amount 
                                from dc_base_purchase t1
                                left  join dc_base_sku t2 on t1.bailun_sku = t2.bailun_sku
                                where t1.has_delete = 0";

            if (!string.IsNullOrEmpty(warehouse))
            {
                sql += " and t1.warehouse_code=@warehouse_code";
                sqlparam.Add("warehouse_code", warehouse);
            }

            if (!string.IsNullOrWhiteSpace(sku))
            {
                sql += " and t1.bailun_sku=@bailun_sku";
                sqlparam.Add("bailun_sku", sku);
            }

            if (!string.IsNullOrWhiteSpace(no))
            {
                sql += " and t1.purchase_id=@purchase_id";
                sqlparam.Add("purchase_id", no);
            }

            if (status.HasValue)
            {
                sql += " and t1.status=" + status.Value;
            }
            
            using (var cn = new MySqlConnection(Common.GlobalConfig.ConnectionString))
            {
                if (cn.State == System.Data.ConnectionState.Closed)
                {
                    cn.Open();
                }

                var obj = cn.QueryFirst<Models.Purchase.mPurchase>(sql,sqlparam);

                return obj;
            }
        }

    }
}
