using Sugar.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;
public class MaterialOutManager : DbContext<MaterialOut>
{

    //当前类已经继承了 DbContext增、删、查、改的方法

    //这里面写的代码不会给覆盖,如果要重新生成请删除 MaterialOutManager.cs
    /// <summary>
    /// 根据项目代码获取领料单主表列表
    /// </summary>
    /// <param name="projectCode"></param>
    /// <param name="searchValue"></param>
    /// <returns></returns>
    public List<MaterialOutDTO> GetMaterialOuts(string projectCode, string searchValue)
    {

        var sql = $@"select m.MaterialOutCode,m.Status,sg.GroupName,su1.UserName outPersonName,su2.UserName inputPersonName,su3.UserName checkpersonName,m.OutDate,m.InputDate,m.CheckDate,co.ContractName from MaterialOut m INNER JOIN dbo.SystemGroup sg ON m.GroupCode=sg.GroupCode INNER JOIN systemuser su1 ON m.OutPerson=su1.UserCode left JOIN systemuser su2 ON m.InputPerson=su2.UserCode LEFT JOIN systemuser su3 ON m.CheckPerson=su3.UserCode INNER JOIN dbo.Contract co ON m.ContractCode=co.ContractCode  
          WHERE m.ProjectCode = '{projectCode}' and dbo.GetSystemGroupFullID(m.GroupCode) like '100036%' ";
        if (!string.IsNullOrEmpty(searchValue))
        {
            sql += $" and (m.MaterialOutCode='{searchValue}'  or sg.groupname like '%{searchValue}%')";
        }
        sql += "Order By m.MaterialOutCode DESC";
        var list = Db.Ado.SqlQuery<MaterialOutDTO>(sql);
        return list;
    }
    /// <summary>
    /// 获取领料单主表实例
    /// </summary>
    /// <param name="materialOutCode"></param>
    /// <returns></returns>
    public MaterialOutDTO GetMaterialOut(string materialOutCode)
    {

        var sql = $@"select m.MaterialOutCode,m.Status,sg.GroupName,su1.UserName outPersonName,su2.UserName inputPersonName,su3.UserName checkpersonName,m.OutDate,m.InputDate,m.CheckDate,co.ContractName,sp.SupplierName+' 类型：'+sg1.GroupName SupplierName from MaterialOut m INNER JOIN dbo.SystemGroup sg ON m.GroupCode=sg.GroupCode INNER JOIN systemuser su1 ON m.OutPerson=su1.UserCode left JOIN systemuser su2 ON m.InputPerson=su2.UserCode LEFT JOIN systemuser su3 ON m.CheckPerson=su3.UserCode INNER JOIN dbo.Contract co ON m.ContractCode=co.ContractCode   LEFT JOIN Supplier sp ON m.DoSupplierCode=sp.SupplierCode LEFT JOIN systemgroup sg1 ON m.SupplierTypeCode=sg1.GroupCode
          WHERE m.materialOutCode = '{materialOutCode}'  ";


        var entity = Db.Ado.SqlQuerySingle<MaterialOutDTO>(sql);
        return entity;
    }





}
