using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using XYY.Common.Standard;
using XYY.Core.Standard.Mvc;
using XYY.Data.Standard.Order;
using XYY.Service.Standard.Finance;

namespace XYY.Api.Order.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CustomerMaintenanceController : ApiControllerBase
    {
        ICustomerMaintenanceMsgRepository customerMaintenanceMsgRepository;
        IDingDingBaseService dingDingBaseService;

        public CustomerMaintenanceController(IDingDingBaseService dingDingBaseService, ICustomerMaintenanceMsgRepository customerMaintenanceMsgRepository)
        {
            this.dingDingBaseService = dingDingBaseService;
            this.customerMaintenanceMsgRepository = customerMaintenanceMsgRepository;
        }

        public class Msg
        {
            public string Contact
            {
                get; set;
            }

            public string Phone
            {
                get; set;
            }

            public int Id { get; set; }
        }

        public async Task<IActionResult> PostMsg(Msg msg)
        {
            await customerMaintenanceMsgRepository.InsertAsync(new XYY.Model.Standard.Order.Customer_Maintenance_Msg
            {
                Contact = msg.Contact,
                IsHandle = false,
                Phone = msg.Phone
            });
            var ddModel = new
            {
                msgtype = "text",
                text =
                  new
                  {
                      content = $"有新的客户咨询发货:客户联系人：{msg.Contact}，客户联系方式：{msg.Phone}"
                  }
            };
            var ddUserInfo = await dingDingBaseService.GetDingDingUserInfo(58);
            if (ddUserInfo != null)
            {
                await dingDingBaseService.SendMessage(ddUserInfo.UserId, ddModel);
            }
            return Ok();
        }

        public async Task<IActionResult> GetPageResult(QueryModel queryModel)
        {
            var pageresult = await customerMaintenanceMsgRepository.GetPageResult(queryModel);
            return Ok(pageresult);
        }

        public async Task<IActionResult> SetHandel(Msg msg)
        {
            await customerMaintenanceMsgRepository.SetHandle(new XYY.Model.Standard.Order.Customer_Maintenance_Msg { Id = msg.Id });
            return Ok();
        }
    }
}
