using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service.@base;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SlnMesnac.Repository.service.Impl
{
    public class T_RP_StationMaterialInventoryServiceImpl : BaseServiceImpl<T_RP_StationMaterialInventory>, IT_RP_StationMaterialInventoryService
    {
        public T_RP_StationMaterialInventoryServiceImpl(Repository<T_RP_StationMaterialInventory> repository):base(repository)
        {

        }

        public T_RP_StationMaterialInventory GetBatchNoByMaterialTypeID(string material_type_id, int stationid)
        {
            T_RP_StationMaterialInventory t_RP_StationMaterialInventory = null;
            string sql = "";
            try
            {
                sql = "select objid,stationid,material_type_id, batchno ,inputcount,create_time from T_RP_StationMaterialInventory where material_type_id = '" + material_type_id + "' and  inputcount > 0  and  stationid = " + stationid+"  order by create_time asc";
                t_RP_StationMaterialInventory = base._rep.AsTenant().GetConnection("mes").Ado.SqlQuerySingle<T_RP_StationMaterialInventory>(sql);
                return t_RP_StationMaterialInventory;
            }
            catch (Exception)
            {
                return t_RP_StationMaterialInventory;
            }

        }

        public List<T_RP_StationMaterialInventory> GetMaterialShip(int stationid)
        {
            List<T_RP_StationMaterialInventory> MaterialShipInfos = null;
            try
            {
                String sql = "select t2.material_type_id,t3.working_procedure_id as remark from (select material_type_id from( select material_type_id from T_RP_StationMaterialInventory where stationid = " + stationid +") t1 group by material_type_id) t2 left join base_materialtype_info t3 on t2.material_type_id = t3.material_type_id ";
                MaterialShipInfos = base._rep.AsTenant().GetConnection("mes").Ado.SqlQuery<T_RP_StationMaterialInventory>(sql);
            }
            catch (Exception ex)
            {
                //_logger.LogError($"获取AGV任务信息异常:{ex.Message}");
            }
            return MaterialShipInfos;
        }

        public async Task<bool> UpdateAsync(T_RP_StationMaterialInventory record)
        {
            bool result = await _rep.UpdateAsync(record);
            return result;
        }
    }
}