using WmsMobileServe.Annotation;
using WmsMobileServe.ApiServe.Mobile.Vo;
using WmsMobileServe.DataBase.Base.Po;

namespace WmsMobileServe.DataBase.Base.Dao;

[Component]
public class CuxWmsPoLinesItfDao(DataBaseClient client)
{

    /// <summary>
    /// 根据状态查询
    /// </summary>
    /// <param name="status"></param>
    /// <returns></returns>
    public List<CuxWmsPoLinesItf>? SelectWithStatus(int? status)
    {
        if (status == default) return default;
        try
        {
            var sqlFuc = client.Instance().Queryable<CuxWmsPoLinesItf>()
                .Where(x => x.Status == status);
            return sqlFuc.ToList();
        }
        catch (Exception ex)
        {
            _ = ex;
            return default;
        }
    }


    public List<CuxWmsPoLinesItf>? Select(CuxWmsPoLinesItf cuxWmsPoLinesItf)
    {
        try
        {
            var sqlFuc = client.Instance().Queryable<CuxWmsPoLinesItf>()
                .WhereIF(cuxWmsPoLinesItf.PoHeaderId == default, w => w.PoHeaderId == cuxWmsPoLinesItf.PoHeaderId)
                .WhereIF(cuxWmsPoLinesItf.PoLineId == default, w => w.PoLineId == cuxWmsPoLinesItf.PoLineId)
                .WhereIF(cuxWmsPoLinesItf.LineLocationId == default, w => w.LineLocationId == cuxWmsPoLinesItf.LineLocationId)
                .WhereIF(cuxWmsPoLinesItf.ShipToOrganization == default, w => w.ShipToOrganization == cuxWmsPoLinesItf.ShipToOrganization)
                .WhereIF(cuxWmsPoLinesItf.LineNum == default, w => w.LineNum == cuxWmsPoLinesItf.LineNum)
                .WhereIF(cuxWmsPoLinesItf.ItemId == default, w => w.ItemId == cuxWmsPoLinesItf.ItemId)
                .WhereIF(cuxWmsPoLinesItf.Segment1 == default, w => w.Segment1 == cuxWmsPoLinesItf.Segment1)
                .WhereIF(cuxWmsPoLinesItf.ItemDesc == default, w => w.ItemDesc == cuxWmsPoLinesItf.ItemDesc)
                .WhereIF(cuxWmsPoLinesItf.PurUomCode == default, w => w.PurUomCode == cuxWmsPoLinesItf.PurUomCode)
                .WhereIF(cuxWmsPoLinesItf.Quantity == default, w => w.Quantity == cuxWmsPoLinesItf.Quantity)
                .WhereIF(cuxWmsPoLinesItf.QuantityReceives == default, w => w.QuantityReceives == cuxWmsPoLinesItf.QuantityReceives)
                .WhereIF(cuxWmsPoLinesItf.Status == default, w => w.Status == cuxWmsPoLinesItf.Status);
            return sqlFuc.ToList();
        }
        catch (Exception ex)
        {
            _ = ex;
            return default;
        }
    }

    /// <summary>
    /// 查询可用的物料
    /// </summary>
    /// <param name="orderId"></param>
    /// <param name="goodId"></param>
    /// <returns></returns>
    public List<CuxWmsPoLinesItfView>? SelectCanUse(string? orderId, long? goodId)
    {
        try
        {
            var sqlFuc = client.Instance().SqlQueryable<CuxWmsPoLinesItf>($@"
                SELECT a.*, b.SEGMENT1 AS SEGMENT FROM CUX_WMS_PO_LINES_ITF_ZH a 
RIGHT JOIN CUX_WMS_PO_HEADES_ITF_ZH b ON a.PO_HEADER_ID = b.PO_HEADER_ID WHERE a.SEGMENT1 = '{orderId}' AND a.ITEM_ID = '{goodId}' AND (a.CLOSED_CODE = 'OPEN' OR a.CLOSED_CODE = 'CLOSED_FOR_INVOICE')
                ").Select<CuxWmsPoLinesItfView>();
            return sqlFuc.ToList();
        }
        catch (Exception ex)
        {
            _ = ex;
            return null;
        }

    }


}
