using System.Data;
using System.Data.SqlClient;
using Business.Supply.Entity;
using Common;

namespace Business.Supply.Vo
{
    public class StockVo
    {
        /// <summary>
        /// 参数赋值
        /// </summary>
        /// <param name="stock">实体类StockEty</param>
        /// <returns>参数数组</returns>
        private SqlParameter[] SetPara(StockEty stock)
        {
            return new[]
                {
                    new SqlParameter("@id", stock.Id),
                    new SqlParameter("@name", stock.Name),
                    new SqlParameter("@time", stock.Time),
                    new SqlParameter("@remark", stock.Remark),
                    new SqlParameter("@delflag", stock.DelFlag),
                    new SqlParameter("@addUserID", stock.AddUserId),
                    new SqlParameter("@addtime", stock.AddTime),
                    new SqlParameter("@updateUserID", stock.UpdateUserId),
                    new SqlParameter("@lastTime", stock.LastTime),
                    new SqlParameter("@quantity",stock.Quantity)
                };
        }

        /// <summary>
        /// 新增stock记录
        /// </summary>
        /// <param name="stock">实体类StockEty</param>
        /// <returns>成功返回空字符串,失败返回错误信息</returns>
        public string InsertStock(StockEty stock)
        {
            var sql =
                "insert into stock(id,name,time,remark,delflag,addUserID,addtime,updateUserID,lastTime,quantity) values(@id,@name,@time,@remark,@delflag,@addUserID,@addtime,@updateUserID,@lastTime,@quantity)";
            return SqlHelper.ExecSql(sql, SetPara(stock));
        }

        /// <summary>
        /// 获取全部库存信息
        /// </summary>
        /// <param name="searchText">查询字段</param>
        /// <returns>数据表</returns>
        public DataTable AllStock(string searchText)
        {
            var sql =
                "select p.id,p.name,p.quantity,p.remark,p.time,p.lastTime,s.username as addusername ,ss.username as updateusername from stock p left join SysUser s on s.userid=p.addUserID left join SysUser ss on ss.userid=p.updateUserID where p.delflag ='false' ";

            if (!string.IsNullOrEmpty(searchText))
            {
                //根据查询字段 进行查询
                sql +=
                    string.Format(
                        "and( p.name like '%{0}%' or p.remark like '%{0}%' or p.quantity like '%{0}%' or s.username like '%{0}%' or ss.username like '%{0}%')",
                        searchText);
            }
            sql += " order by p.lastTime DESC";

            return SqlHelper.ExecSqlDateTable(sql);
        }

        /// <summary>
        /// 更新库存信息
        /// </summary>
        /// <param name="stock">实体类StockEty</param>
        /// <returns>成功返回空字符串,失败返回错误信息</returns>
        public string UpdateStock(StockEty stock)
        {
            var sq1 =
                "update stock set name=@name,quantity=@quantity,remark=@remark,lastTime=@lastTime,updateUserID=@updateUserID,time=@time where id=@id";
            return SqlHelper.ExecSql(sq1, SetPara(stock));
        }

        /// <summary>
        /// 根据id删除库存信息
        /// </summary>
        /// <param name="id">库存id</param>
        /// <returns>成功返回空字符串,失败返回错误信息</returns>
        public string DelStock(string id)
        {
            var sql = "update stock set delflag='true' where id in (" + id + ")";
            return SqlHelper.ExecSql(sql);
        }
    }
}
