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

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

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

        /// <summary>
        /// 获取全部用料信息
        /// </summary>
        /// <param name="searchText">查询字段</param>
        /// <returns>数据表</returns>
        public DataTable AllIngredients(string searchText)
        {
            var sql =
                "select p.id,p.name,p.depid,sd.name as depname, p.quantity,p.remark,p.time,p.lastTime,s.username as addusername ,ss.username as updateusername from ingredients p left join SysDep sd on sd.depid=p.depid 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 sd.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="ingredients">实体类IngredientsEty</param>
        /// <returns>成功返回空字符串,失败返回错误信息</returns>
        public string UpdateIngredients(IngredientsEty ingredients)
        {
            var sq1 =
                "update ingredients set name=@name,depid=@depid,quantity=@quantity,remark=@remark,lastTime=@lastTime,updateUserID=@updateUserID,time=@time where id=@id";
            return SqlHelper.ExecSql(sq1, SetPara(ingredients));
        }

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


    }
}
