using Sugar.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;

/// <summary>
/// 供应商扩展申请管理类
/// </summary>
public  class zzSupplierExManager : DbContext<zzSupplierEx>
{
    /// <summary>
    /// 根据供应商编号获取其对应所有项目
    /// </summary>
    /// <param name="supplierCode"></param>
    /// <returns></returns>
    public List<Project> GetProjectBySupplierCode(string supplierCode)
    {
        //通过供应商编号查询其签下的所有合同。根据合同查询其对应的项目
        var sql = $"SELECT * FROM project WHERE ProjectCode IN (SELECT ProjectCode  FROM dbo.Contract  WHERE SupplierCode='{supplierCode}' GROUP BY ProjectCode)";
        var list = Db.Ado.SqlQuery<Project>(sql);
        return list;
    }
    /// <summary>
    /// 获取zzSupplierExDTO列表
    /// </summary>
    /// <param name="supplierCode"></param>
    /// <returns></returns>
    public List<zzSupplierExDTO> GetzzSupplierExDTOList(string supplierCode)
    {
        var sql = $"SELECT a.*,b.ProjectName FROM dbo.zzSupplierEx a INNER JOIN dbo.Project b ON a.ProjectCode=b.ProjectCode where a.supplierCode='{supplierCode}' order by a.createdate desc  ";
        var list = Db.Ado.SqlQuery<zzSupplierExDTO>(sql);
        return list;
    }
    /// <summary>
    /// 根据项目编号获取供应商申请列表
    /// </summary>
    /// <param name="projectCode"></param>
    /// <returns></returns>
    public List<zzSupplierExDTO> GetzzSupplierExListByProject(string projectCode)
    {
        var sql = $"SELECT a.*,b.ProjectName FROM dbo.zzSupplierEx a INNER JOIN dbo.Project b ON a.ProjectCode=b.ProjectCode where a.projectcode='{projectCode}' and state>=1 order by a.createdate desc  ";
        var list = Db.Ado.SqlQuery<zzSupplierExDTO>(sql);
        return list;
    }

    /// <summary>
    /// 获取zzSupplierExDTO实体
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public zzSupplierExDTO GetzzSupplierExDTO(int id)
    {
        var sql = $"SELECT a.*,b.ProjectName FROM dbo.zzSupplierEx a INNER JOIN dbo.Project b ON a.ProjectCode=b.ProjectCode where a.id={id} ";
        var entity = Db.Ado.SqlQuerySingle<zzSupplierExDTO>(sql);
        return entity;
    }
    /// <summary>
    /// 审核提交的信息
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    public bool AuditzzSupplierEx(zzSupplierEx dto)
    {
        var sql = $"update zzSupplierEx set checkperson='{dto.CheckPerson}',checkdate=getdate(),state={dto.State} where id={dto.ID} and projectcode='{dto.ProjectCode}'";
        var row = Db.Ado.ExecuteCommand(sql);
        if(row>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /// <summary>
    /// 提交
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    public bool SubmitzzSupplierEx(zzSupplierEx dto)
    {
        var sql = $"update zzSupplierEx set state=1 where id={dto.ID} and suppliercode='{dto.SupplierCode}'";
        var row = Db.Ado.ExecuteCommand(sql);
        if (row > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}
 
