using MoYu.FriendlyException;
using MoYu.RemoteRequest;
using MoYu.RemoteRequest.Extensions;
using Newtonsoft.Json.Linq;
using SafeCampus.Application.Manager.DeepelephManager;
using SafeCampus.Web.Core.Controllers.Application.Business.Dto;

namespace SafeCampus.Web.Core.Controllers.Application.Business;
/// <summary>
/// 服装底库管理接口
/// </summary>
[Route("/business/[controller]")]
[ApiDescriptionSettings(ApiGroupConsts.SYSTEM_Business, Order = 97,Tag = "服装底库管理")]
public class ClothApi
{
    private readonly IDeepelephManager _deepelephManager;

    public ClothApi(IDeepelephManager deepelephManager)
    {
        _deepelephManager = deepelephManager;
    }

    /// <summary>
    /// 新增服装底库
    /// </summary>
    /// <param name="input">人员信息</param>
    /// <returns></returns>
    public async Task<dynamic> AddClothDataBaseA(ClothDataNameInfo input)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/cloth/set/create"
            .SetBody(new
            {
                token = _deepelephManager.GetToken(),
                tenantCode = appSettings.TenantCode,
                poiId = appSettings.PoiId,
                clothSetId = Guid.NewGuid().ToString("N"),
                clothSetName = input.ClothSetName,
            })
            .SetContentType("application/json")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if ((bool)model["success"])
        {
            return model["data"];
        }
        throw Oops.Oh(model["message"].ToString());
    }
    /// <summary>
    /// 删除服装底库
    /// </summary>
    /// <param name="clothSetId"></param>
    /// <returns></returns>
    public async Task<dynamic> DeleteClothDataBaseD(string clothSetId)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/cloth/set/delete"
            .SetBody(new
            {
                token = _deepelephManager.GetToken(),
                tenantCode = appSettings.TenantCode,
                poiId = appSettings.PoiId,
                clothSetId= clothSetId,
            })
            .SetContentType("application/json")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if ((bool)model["success"])
        {
            return model["data"];
        }
        throw Oops.Oh(model["message"].ToString());
    }
    /// <summary>
    /// 更新服装底库
    /// </summary>
    /// <returns></returns>
    public async Task<dynamic> UpdateClothU(ClothDataBaseInfo info)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/cloth/set/update"
            .SetBody(new
            {
                token = _deepelephManager.GetToken(),
                tenantCode = appSettings.TenantCode,
                poiId = appSettings.PoiId,
                clothSetId = info.ClothSetId,
                clothSetName = info.ClothSetName,
            })
            .SetContentType("application/json")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if ((bool)model["success"])
        {
            return model["data"];
        }
        throw Oops.Oh(model["message"].ToString());
    }
    /// <summary>
    /// 查询服装底库列表
    /// </summary>
    /// <returns></returns>
    public async Task<dynamic> GetList()
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/cloth/set/query-list"
            .SetBody(new
            {
                token = _deepelephManager.GetToken(),
                tenantCode = appSettings.TenantCode,
                poiId = appSettings.PoiId,
            })
            .SetContentType("application/json")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if ((bool)model["success"])
        {
            return model["data"];
        }
        throw Oops.Oh(model["message"].ToString());
    }
    /// <summary>
    /// 查询服装底库详情
    /// </summary>
    /// <param name="clothSetId">服装库Id</param>
    /// <returns></returns>
    public async Task<dynamic> GetInfo(string clothSetId)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/cloth/set/query-detail"
            .SetBody(new
            {
                token = _deepelephManager.GetToken(),
                tenantCode = appSettings.TenantCode,
                poiId = appSettings.PoiId,
                clothSetId= clothSetId
            })
            .SetContentType("application/json")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if ((bool)model["success"])
        {
            return model["data"];
        }
        throw Oops.Oh(model["message"].ToString());
    }
    /// <summary>
    /// 服装图片上传
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<dynamic> UploadFile([FromForm] BaseFileInput input)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var jsonparam = JsonConvert.SerializeObject(new
        {
            token = _deepelephManager.GetToken(),
            tenantCode = appSettings.TenantCode,
            poiId = appSettings.PoiId,
            usageScenario = "staff"
        });
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/common/upload-file"
            .SetFiles(HttpFile.Create("file", input.File.OpenReadStream(), input.File.FileName))
            .SetBody(new
            {
                param = jsonparam
            })
            .SetContentType("multipart/form-data")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if ((bool)model["success"])
        {
            return model["data"]["httpUrl"].ToString();
        }
        throw Oops.Oh(model["message"].ToString());
    }
    /// <summary>
    /// 新增服装
    /// </summary>
    /// <param name="info"></param>
    /// <returns></returns>
    public async Task<dynamic> AddClothA(ClothInfos info)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/cloth/add"
            .SetBody(new
            {
                token = _deepelephManager.GetToken(),
                tenantCode = appSettings.TenantCode,
                poiId = appSettings.PoiId,
                info.clothSetId,
                info.clothUrl
            })
            .SetContentType("application/json")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if ((bool)model["success"])
        {
            return model["data"];
        }
        throw Oops.Oh(model["message"].ToString());
    }
    /// <summary>
    /// 删除服装
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<dynamic> DeleteClothD(ClothInfos info)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/cloth/delete"
            .SetBody(new
            {
                token = _deepelephManager.GetToken(),
                tenantCode = appSettings.TenantCode,
                poiId = appSettings.PoiId,
                info.clothSetId,
                info.clothId
            })
            .SetContentType("application/json")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if ((bool)model["success"])
        {
            return model["data"];
        }
        throw Oops.Oh(model["message"].ToString());
    }
}