using Entity.DbModel.Station;
using Entity.Dto.Req;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
using Mapster;
using Repository.Station;
using SqlSugar;

namespace Service.Station;

[Scope("SingleInstance")]
public class EquipNetInfoService : BaseServices<EquipNetInfo>
{
    EquipNetInfoRepository _equipNetInfoRepository;

    public EquipNetInfoService(EquipNetInfoRepository service)
    {
        _equipNetInfoRepository = service;
    }

    /// <summary>
    /// 获取设备通信信息分页 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PageResult<EquipNetInfo>> Page(PageEquipNetInfoReq input)
    {
        RefAsync<int> total = 0;
        var items = await _equipNetInfoRepository.EquipNetInfoQueryPageAsync(
            !string.IsNullOrEmpty(input.Code), u => u.Code.Contains(input.Code),
            !string.IsNullOrEmpty(input.NetAddr), u => u.NetAddr.Contains(input.NetAddr),
            !string.IsNullOrEmpty(input.NetPort), u => u.NetPort.Equals(input.NetPort),
            input.PageNum, input.PageSize, total, input);
        return new PageResult<EquipNetInfo>()
        {
            PageNum = input.PageNum,
            PageSize = input.PageSize,
            ToTal = total,
            Rows = items,
        };
    }

    /// <summary>
    /// 增加设备通信信息🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<Result<string>> AddEquipNetInfo(AddEquipNetInfoReq input)
    {
        var isExist =
            await _equipNetInfoRepository.QueryByClauseAsync(u => u.Code == input.Code);
        if (isExist != null)
        {
            return Result<string>.Fail("编码已存在");
        }

        EquipNetInfo insertAsync = await _equipNetInfoRepository.InsertAsync(input.Adapt<EquipNetInfo>());
        if (insertAsync.Id > 0)
        {
            return Result<string>.Success("新增id:" + insertAsync.Id);
        }

        return Result<string>.Fail("新增失败");
    }

    /// <summary>
    /// 更新设备通信信息🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<Result<string>> UpdateEquipNetInfo(UpdateEquipNetInfoReq input)
    {
        var isExist = await _equipNetInfoRepository.QueryByClauseAsync(u =>
            (u.Code == input.Code) && u.Id != input.Id);
        if (isExist != null)
        {
            return Result<string>.Fail("已存在同名或同编码设备信息");
        }

        var config = input.Adapt<EquipNetInfo>();
        int updateResult = await _equipNetInfoRepository.UpdateAsync(config, true);
        if (updateResult > 0)
        {
            return Result<string>.Success("修改成功");
        }

        return Result<string>.Fail("修改失败");
    }

    /// <summary>
    /// 删除设备通信信息 🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<Result<string>> DeleteEquipNetInfo(DeleteEquipNetInfoReq input)
    {
        var equipNetInfo = await _equipNetInfoRepository.QueryByClauseAsync(u => u.Id == input.Id);
        if (equipNetInfo==null)
        {
            return Result<string>.Fail("id不存在");

        }
        bool deleteResult = await _equipNetInfoRepository.DeleteAsync(equipNetInfo);
        if (deleteResult)
        {
            return Result<string>.Success("删除设备信息成功");
        }

        return Result<string>.Fail("删除失败");
    }
}