using Sugar.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;
using WebAPIBase.Utils;
using NLog;
/// <summary>
/// 待审签证管理类
/// </summary>
public class ViseManager : DbContext<Vise>
{
    private static Logger logger = NLog.LogManager.GetCurrentClassLogger();
    //当前类已经继承了 DbContext增、删、查、改的方法

    //这里面写的代码不会给覆盖,如果要重新生成请删除 ViseManager.cs
    /// <summary>
    /// 获取待审签证列表
    /// </summary>
    /// <param name="projectCode"></param>
    /// <param name="searchValue"></param>
    /// <returns></returns>
    public List<ViseDTO> GetVises(string projectCode, string searchValue = "")
    {
        var sql = $"SELECT v.ViseCode,v.ViseId,v.ViseName,v.ViseType,CONVERT(VARCHAR(10),v.ViseDate,120) ViseDate,v.State,dbo.GetUnitFullName(v.UnitCode) unitFullName, c.ContractName,v.ContractCode from Vise v INNER JOIN dbo.Contract c ON v.ContractCode=c.ContractCode where (dbo.GetUnitFullCode(v.UnitCode) like ('100000%')) and v.ProjectCode = '{projectCode}' and v.State ='0'  ";
        if (searchValue.IsNotNullAndEmpty())
        {
            sql += $" and (v.ViseId='{searchValue}' or v.ViseName like '%{searchValue}%')";
        }
        sql += "  order by v.ViseCode desc";
        var list = Db.Ado.SqlQuery<ViseDTO>(sql);
        return list;
    }
    /// <summary>
    /// 根据签证编码获取待审签证实例
    /// </summary>
    /// <param name="viseCode"></param>
    /// <returns></returns>
    public ViseDTO GetVise(string viseCode)
    {
        var sql = $"SELECT v.*,dbo.GetUnitFullName(v.UnitCode) unitFullName, c.ContractName from Vise v INNER JOIN dbo.Contract c ON v.ContractCode=c.ContractCode where  v.viseCode = '{viseCode}'  ";

        var entity = Db.Ado.SqlQuerySingle<ViseDTO>(sql);
        return entity;
    }
    /// <summary>
    /// 待审核签证费用明细
    /// </summary>
    /// <param name="viseCode"></param>
    /// <returns></returns>
    public List<ViseCostDTO> GetViseCostDTOs(string viseCode)
    {
        var sql = $"SELECT *  FROM visecost  WHERE ViseCode='{viseCode}'";
        var list = Db.Ado.SqlQuery<ViseCostDTO>(sql);
        return list;
    }
    /// <summary>
    /// 获取合同明细
    /// </summary>
    /// <param name="contractCode"></param>
    /// <returns></returns>
    public List<ContractCostDTO> GetContractCostDTOs(string contractCode)
    {
        var sql = $"SELECT *  FROM ContractCost  WHERE contractCode='{contractCode}'";
        var list = Db.Ado.SqlQuery<ContractCostDTO>(sql);
        return list;
    }
    /// <summary>
    /// 插入待审核签证主表和明细表
    /// </summary>
    /// <param name="vise"></param>
    /// <param name="viseCost"></param>
    /// <returns></returns>
    public string InsertVise(Vise vise, List<ViseCost> viseCosts)
    {
        var unitManager = new UnitManager();
        string viseCode = "";
        try
        {
            Db.BeginTran();
            vise.State = "0";
            viseCode = unitManager.GetSysCodeValue("ViseCode");
            vise.ViseCode = viseCode;
            vise.TotalMoney = vise.TotalCash;
            vise.RateTotalMoney = vise.RateTotalCash;
            vise.NoRateTotalMoney = vise.NoRateTotalCash;
            Db.Insertable(vise).ExecuteCommand();
            foreach (var item in viseCosts)
            {
                item.State = "0";
                item.ViseCostCode = unitManager.GetSysCodeValue("ViseCostCode");
                item.ViseCode = viseCode;
                item.RateCheckCash = 0;
                item.RateCheckMoney = 0;
                item.NoRateCheckCash = 0;
                item.NoRateCheckMoney = 0;
                Db.Insertable(item).ExecuteCommand();
            }
            Db.CommitTran();
        }
        catch (Exception ex)
        {
            Db.RollbackTran();
            throw;
        }

        return viseCode;
    }
    /// <summary>
    /// 更新待审核签证主表和明细表
    /// </summary>
    /// <param name="vise"></param>
    /// <param name="viseCost"></param>
    /// <returns></returns>
    public string UpdateVise(Vise vise, List<ViseCost> viseCosts)
    {
        var unitManager = new UnitManager();
        var entity = ViseDb.GetById(vise.ViseCode);
        if (entity == null)
        {
            return "-1";
        }
        if (entity.State != "0")
        {
            return "-2";
        }
        string viseCode = "";
        try
        {
            Db.BeginTran();
            viseCode = vise.ViseCode;
            vise.TotalMoney = vise.TotalCash;
            vise.RateTotalMoney = vise.RateTotalCash;
            vise.NoRateTotalMoney = vise.NoRateTotalCash;
            Db.Updateable(vise).UpdateColumns(it => new { it.ViseId, it.ViseName, it.RequisitionCode, it.ContractCode, it.Rate, it.TotalCash, it.ViseType, it.Reason, it.Remark, it.EndDate, it.TotalMoney, it.RateTotalCash, it.RateTotalMoney, it.NoRateTotalCash, it.NoRateTotalMoney, it.ProjectCode, it.MoneyType, it.ExchangeRate, it.Person, it.UnitCode }).ExecuteCommand();
            Db.Deleteable<ViseCost>().Where(m => m.ViseCode == viseCode).ExecuteCommand();
            foreach (var item in viseCosts)
            {
                item.State = "0";
                item.ViseCostCode = unitManager.GetSysCodeValue("ViseCostCode");
                item.ViseCode = viseCode;
                item.RateCheckCash = 0;
                item.RateCheckMoney = 0;
                item.NoRateCheckCash = 0;
                item.NoRateCheckMoney = 0;
                Db.Insertable(item).ExecuteCommand();
            }
            Db.CommitTran();
        }
        catch (Exception ex)
        {
            Db.RollbackTran();
            throw;
        }

        return viseCode;
    }

}
