using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Sugar.Enties;
using Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Utils.Jwt;
using WxPayAPI;
using SqlSugar;

namespace PMS.NetCore.Controllers
{



    /// <summary>
    /// 仪表相关
    /// </summary>
    [Produces("application/json;charset=UTF-8")]
    [Route("api/Meter")]
    [ServiceFilter(typeof(Filter.TokenAuthorizeByOwner))]
    public class MeterController : BaseController
    {
        private IHttpContextAccessor _accessor;
        /// <summary>
        /// 令牌获取和验证类
        /// </summary>
        private readonly ITokenHelper tokenHelper = null;
        public MeterController(IHttpContextAccessor accessor, ITokenHelper _tokenHelper)
        {
            logger = NLog.LogManager.GetCurrentClassLogger();
            _accessor = accessor;
            tokenHelper = _tokenHelper;
        }
        /// <summary>
        /// 发送手机验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [Route("SendSms")]
        public JsonResult SendSms(string phone)
        {
            var res = new ApiResponse();
            try
            {
                if (phone.IsNullOrEmpty())
                {

                    res.ErrMsg = "请填写手机号";
                    return Json(res);

                }
                if (!phone.IsCellPhone())
                {
                    res.ErrMsg = "手机号格式不正确";
                    return Json(res);
                }
                logger.Info($"【SendSms】phone:{phone}");
                var service = new fdc_pm_meterrecordManager();
                var model = service.LoginOwner(phone);

                logger.Info($"【SendSms】model:{JsonConvert.SerializeObject(model)}");

                if (model == null)
                {
                    res.ErrMsg = "手机号找不到所对应的业主";
                    return Json(res);
                }

                var code = Sms.GetSMSCode();

                var signName = "唯友";
                var templateid = "SMS_230825153";
                var templateParam = "{\"code\":\"" + code + "\"}";
                logger.Info($"【SendSms】templateParam:{templateParam} 手机号：{phone}");

                //发送验证码到手机
                AlibabaSMSSdk.SendSms(phone, signName, templateid, templateParam);
                //用session存随机验证码
                SessionHelper sessionHelp = new SessionHelper(_accessor.HttpContext);
                sessionHelp.SetSession("verifiedCode", code);
                sessionHelp.SetSession("phone", phone);
                res.IsSuccess = true;
                res.Data = "success";
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }

        /// <summary>
        /// 业主登录
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        [Route("LoginOwner")]
        public JsonResult LoginOwner([FromBody] LoginOwnerDTO dto)
        {

            var res = new ApiResponse();
            try
            {
                logger.Info($"【LoginOwner 输入参数】dto:{JsonConvert.SerializeObject(dto)}");
                SessionHelper sessionHelp = new SessionHelper(_accessor.HttpContext);
                if (dto.Phone != sessionHelp.GetSession("phone"))
                {
                    res.ErrMsg = "手机号不正确";
                    return Json(res);
                }
                if (dto.VeriCode != sessionHelp.GetSession("verifiedCode"))
                {
                    res.ErrMsg = "手机验证码不正确";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var model = service.LoginOwner(dto.Phone);

                //获取令牌，返回客户端
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>
                    {
                        { "UserID", model.Ownerid }
                    };
                var tnToken = tokenHelper.CreateToken(keyValuePairs);

                var ownerManager = new fdc_pm_ownerManager();
                ownerManager.Upfdc_pm_ownerOpenId(dto.Phone, dto.OpenId);

                res.IsSuccess = true;
                res.Data = model;
                res.TnToken = tnToken;
                res.OpenId = dto.OpenId;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }

        /// <summary>
        /// 绑定房屋信息
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("BindHouse")]
        public JsonResult BindHouse(string phone)
        {

            var res = new ApiResponse();
            try
            {
                logger.Info($"【BindHouse 输入参数】dto:{phone}");
                var service = new fdc_pm_meterrecordManager();
                var list = service.BindHouse(phone);


                res.IsSuccess = true;
                res.Data = list;
                logger.Info($"【BindHouse 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }



        #region 抄表单
        /// <summary>
        /// 抄表单添加
        /// </summary>
        /// <param name="chaoBiaos"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Fdc_pm_meterAdd")]
        public JsonResult Fdc_pm_meterAdd([FromBody] List<ChaobiaoDTO> chaoBiaos)
        {
            var res = new ApiResponse();
            try
            {
                logger.Info($"【Fdc_pm_meterAdd输入参数】chaoBiaos:{JsonConvert.SerializeObject(chaoBiaos)}");
                var service = new fdc_pm_meterrecordManager();
                var str = service.Fdc_pm_meterAdd(chaoBiaos);
                logger.Info($"Fdc_pm_meterAdd输出参数】res:{res}");
                if (str == "success")
                {
                    res.IsSuccess = true;
                }
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }
        #endregion

        #region 缴费接口
        /// <summary>
        /// 获取欠费信息
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Getfdc_pm_unfare")]
        public JsonResult Getfdc_pm_unfare(string phone)
        {
            var res = new ApiResponse();
            try
            {
                logger.Info($"【Getfdc_pm_unfare 输入参数】phone:{phone}");
                if (phone.IsNullOrEmpty())
                {
                    res.ErrMsg = "手机号不能为空";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var list = service.Getfdc_pm_unfare(phone);
                logger.Info($"Getfdc_pm_unfare 输出参数】list:{JsonConvert.SerializeObject(list)}");
                res.IsSuccess = true;
                res.Data = list;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }
        /// <summary>
        /// 获取缴费记录
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Getfdc_pm_revfare")]
        public JsonResult Getfdc_pm_revfare(string phone)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【Getfdc_pm_revfare 输入参数】phone:{phone}");
                if (phone.IsNullOrEmpty())
                {
                    res.ErrMsg = "参数错误";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var list = service.Getfdc_pm_revfare(phone);
                logger.Info($"Getfdc_pm_revfare 输出参数】list:{JsonConvert.SerializeObject(list)}");
                res.IsSuccess = true;
                res.Data = list;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }

        /// <summary>
        /// 生成微信缴费单二维码
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        [Route("MakePayQrCode")]
        public JsonResult MakePayQrCode([FromBody] PayDTO dto)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【MakePayUrl 输入参数】dto:{JsonConvert.SerializeObject(dto)}");
                if (dto.farwarid.IsNullOrEmpty())
                {
                    res.ErrMsg = "缴费编号错误";
                    return Json(res);
                }
                if (!dto.money.IsDecimal())
                {
                    res.ErrMsg = "缴费金额不正确";
                    return Json(res);
                }
                if (dto.ownername.IsNullOrEmpty())
                {
                    res.ErrMsg = "业主名称错误";
                    return Json(res);
                }
                if (dto.hname.IsNullOrEmpty())
                {
                    res.ErrMsg = "房屋名称错误";
                    return Json(res);
                }
                var order = new Order();
                order.OrderNo = Utility.GetRandomNumber();
                order.Farerowid = dto.farwarid;
                order.Money = dto.money.ToDecimal();
                order.Ownername = dto.ownername;
                order.Hname = dto.hname;
                order.Revfarecode = "";

                //logger.Info("SqlFunc.ToDate", SqlFunc.ToDateShort(DateTime.Now));

                var service = new fdc_pm_meterrecordManager();
                var orderNew = service.Db.Queryable<Order>().First(m => m.Farerowid == order.Farerowid && m.Money == order.Money && SqlFunc.ToDateShort(m.AddTime) == SqlFunc.ToDateShort(service.Db.GetDate()));
                if (orderNew == null)
                {
                    orderNew = service.Db.Insertable(order).ExecuteReturnEntity();
                }
                if (orderNew != null)   //表示生成订单成功
                {
                    var pay = new NativePay();
                    var money = (int)(dto.money.ToDecimal() * 100);
                    //logger.Info("MakePayUrl 输出参数】dto.money:"+ dto.money);
                    //logger.Info("MakePayUrl 输出参数】money.ToDecimal:"+dto.money.ToDecimal() * 100);
                    //logger.Info("MakePayUrl 输出参数】money:"+ money);
                    var payurl = pay.GetPayUrl(dto.farwarid, money, orderNew.OrderNo);
                    res.IsSuccess = true;
                    res.Data = payurl;
                    logger.Info($"MakePayUrl 输出参数】res:{JsonConvert.SerializeObject(res)}");
                    return Json(res);
                }
                else
                {
                    res.IsSuccess = false;
                    res.ErrMsg = "生成订单失败，重新操作";
                    logger.Info($"MakePayUrl 输出参数】res:{JsonConvert.SerializeObject(res)}");
                    return Json(res);
                }

            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }


        /// <summary>
        /// 获取JSAPI支付数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        [Route("GetJSAPIWxPayData")]
        public JsonResult GetJSAPIWxPayData([FromBody] PayDTO dto)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【MakePayUrl 输入参数】dto:{JsonConvert.SerializeObject(dto)}");
                if (dto.farwarid.IsNullOrEmpty())
                {
                    res.ErrMsg = "缴费编号错误";
                    return Json(res);
                }
                if (!dto.money.IsDecimal())
                {
                    res.ErrMsg = "缴费金额不正确";
                    return Json(res);
                }
                if (dto.ownername.IsNullOrEmpty())
                {
                    res.ErrMsg = "业主名称错误";
                    return Json(res);
                }
                if (dto.hname.IsNullOrEmpty())
                {
                    res.ErrMsg = "房屋名称错误";
                    return Json(res);
                }
                var order = new Order();
                order.OrderNo = Utility.GetRandomNumber();
                order.Farerowid = dto.farwarid;
                order.Money = dto.money.ToDecimal();
                order.Ownername = dto.ownername;
                order.Hname = dto.hname;
                order.Revfarecode = "";

                //logger.Info("SqlFunc.ToDate", SqlFunc.ToDateShort(DateTime.Now));

                var service = new fdc_pm_meterrecordManager();
                var orderNew = service.Db.Queryable<Order>().First(m => m.Farerowid == order.Farerowid && m.Money == order.Money && SqlFunc.ToDateShort(m.AddTime) == SqlFunc.ToDateShort(service.Db.GetDate()));
                if (orderNew == null)
                {
                    orderNew = service.Db.Insertable(order).ExecuteReturnEntity();
                }
                if (orderNew != null)   //表示生成订单成功
                {
                    var pay = new JSAPIPay();
                    var money = (int)(dto.money.ToDecimal() * 100);
                    //logger.Info("MakePayUrl 输出参数】dto.money:"+ dto.money);
                    //logger.Info("MakePayUrl 输出参数】money.ToDecimal:"+dto.money.ToDecimal() * 100);
                    //logger.Info("MakePayUrl 输出参数】money:"+ money);
                    var wxPayData = pay.GetJSAPIWxPayData(money, dto.OpenId, orderNew.OrderNo);
                    //logger.Info($"MakePayUrl 输出参数】xm;:{xml}");
                    res.IsSuccess = true;
                    res.Data = wxPayData;
                    logger.Info($"MakePayUrl 输出参数】res:{JsonConvert.SerializeObject(res)}");
                    return Json(res);
                }
                else
                {
                    res.IsSuccess = false;
                    res.ErrMsg = "生成订单失败，重新操作";
                    logger.Info($"MakePayUrl 输出参数】res:{JsonConvert.SerializeObject(res)}");
                    return Json(res);
                }

            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }



        /// <summary>
        /// 关闭微信预订单
        /// </summary>
        /// <param name="out_trade_no"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [Route("CloseOrder")]
        public JsonResult CloseOrder(string out_trade_no)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【CloseOrder 输入参数】dto:{JsonConvert.SerializeObject(out_trade_no)}");

                //logger.Info("SqlFunc.ToDate", SqlFunc.ToDateShort(DateTime.Now));

                var service = new fdc_pm_meterrecordManager();
                var order = service.Db.Queryable<Order>().First(m => m.OrderNo == out_trade_no);
                if (order == null)
                {
                    res.IsSuccess = false;
                    res.ErrMsg = "订单不存在";
                }
                else
                {
                    //if (orderNew != null)   //表示生成订单成功
                    //{
                    var pay = new JSAPIPay();
                    //    var money = (int)(dto.money.ToDecimal() * 100);
                    //    //logger.Info("MakePayUrl 输出参数】dto.money:"+ dto.money);
                    //    //logger.Info("MakePayUrl 输出参数】money.ToDecimal:"+dto.money.ToDecimal() * 100);
                    //    //logger.Info("MakePayUrl 输出参数】money:"+ money);
                    var wxPayData = pay.CloseOrder(out_trade_no);

                    service.Db.Deleteable(order).ExecuteCommand();
                    res.IsSuccess = true;
                    res.Data = wxPayData;
                    logger.Info($"CloseOrder 输出参数】res:{JsonConvert.SerializeObject(res)}");

                }
                return Json(res);
                //}
                //else
                //{
                //    res.IsSuccess = false;
                //    res.ErrMsg = "生成订单失败，重新操作";
                //    logger.Info($"MakePayUrl 输出参数】res:{JsonConvert.SerializeObject(res)}");
                //    return Json(res);
                //}

            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }

        /// <summary>
        /// 获取通过查询条件欠费信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Getfdc_pm_unfarebysearch")]
        public JsonResult Getfdc_pm_unfarebysearch([FromBody] FareSearchDTO dto)
        {
            var res = new ApiResponse();
            try
            {
                logger.Info($"【Getfdc_pm_unfarebysearch 输入参数】search:{JsonConvert.SerializeObject(dto)}");
                if (dto == null)
                {
                    res.ErrMsg = "查询条件不能为空";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var list = service.Getfdc_pm_unfarebysearch(dto.ownername, dto.hname, dto.staffcode);
                logger.Info($"Getfdc_pm_unfarebysearch 输出参数】list:{JsonConvert.SerializeObject(list)}");
                res.IsSuccess = true;
                res.Data = list;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }

        /// <summary>
        /// 获取缴费记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Getfdc_pm_revfarebysearch")]
        public JsonResult Getfdc_pm_revfarebysearch([FromBody] FareSearchDTO dto)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【Getfdc_pm_revfarebysearch 输入参数】phone:{JsonConvert.SerializeObject(dto)}");
                if (dto == null)
                {
                    res.ErrMsg = "参数错误";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var list = service.Getfdc_pm_revfarebysearch(dto.ownername, dto.hname, dto.staffcode);
                logger.Info($"Getfdc_pm_revfarebysearch 输出参数】list:{JsonConvert.SerializeObject(list)}");
                res.IsSuccess = true;
                res.Data = list;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }
        #endregion

        #region 报修和投诉
        /// <summary>
        /// 获取报修记录
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetRepaireList")]
        public JsonResult GetRepaireList(string phone)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【GetRepaireList 输入参数】phone:{phone}");
                if (phone.IsNullOrEmpty())
                {
                    res.ErrMsg = "参数错误";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var list = service.GetRepaireList(phone);
                logger.Info($"GetRepaireList 输出参数】list:{JsonConvert.SerializeObject(list)}");
                res.IsSuccess = true;
                res.Data = list;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }

        /// <summary>
        /// 获取投诉记录
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetComplaintList")]
        public JsonResult GetComplaintList(string phone)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【GetComplaintList 输入参数】phone:{phone}");
                if (phone.IsNullOrEmpty())
                {
                    res.ErrMsg = "参数错误";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var list = service.GetComplaintList(phone);
                logger.Info($"GetComplaintList 输出参数】list:{JsonConvert.SerializeObject(list)}");
                res.IsSuccess = true;
                res.Data = list;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }


        /// <summary>
        /// 获取报修详情
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetRepairDetail")]
        public JsonResult GetRepairDetail(string code)
        {
            var res = new ApiResponse();
            logger.Info($"【GetRepairDetail 输入参数】code:{code}");
            try
            {
                var service = new fdc_pm_meterrecordManager();
                var model = service.GetRepairDetail(code);

                res.IsSuccess = true;
                res.Data = model;
                logger.Info($"GetRepairDetail 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);

            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }

        /// <summary>
        /// 添加报事单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Insertfdc_pm_repair")]
        public JsonResult Insertfdc_pm_repair([FromBody] BaoshiDTO dto)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【Insertfdc_pm_repair 输入参数】dto:{JsonConvert.SerializeObject(dto)}");
                //res.ErrMsg = "输出";
                //return Json(res);
                var service = new fdc_pm_meterrecordManager();
                var resstr = service.Insertfdc_pm_repair(dto, 0);

                if (resstr != "success")
                {
                    res.ErrMsg = resstr;
                    logger.Info($"Insertfdc_pm_repair 输出参数】res:{JsonConvert.SerializeObject(res)}");
                    return Json(res);
                }
                res.IsSuccess = true;
                res.Data = resstr;
                logger.Info($"Insertfdc_pm_repair 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }

        /// <summary>
        /// 添加投诉单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Insertfdc_pm_repairByTousu")]
        public JsonResult Insertfdc_pm_repairByTousu([FromBody] BaoshiDTO dto)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【Insertfdc_pm_repairByTousu 输入参数】dto:{JsonConvert.SerializeObject(dto)}");

                var service = new fdc_pm_meterrecordManager();
                var resstr = service.Insertfdc_pm_repair(dto, 1);

                if (resstr != "success")
                {
                    res.ErrMsg = resstr;
                    logger.Info($"Insertfdc_pm_repairByTousu 输出参数】res:{JsonConvert.SerializeObject(res)}");
                    return Json(res);
                }
                res.IsSuccess = true;
                res.Data = resstr;
                logger.Info($"Insertfdc_pm_repairByTousu 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }
        /// <summary>
        /// 根据单据处理人获取所有报修单、投诉单
        /// </summary>
        /// <param name="voperatorid"></param>
        /// <param name="vbillstatus"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Getfdc_pm_repairapplyList")]
        public JsonResult Getfdc_pm_repairapplyList(string voperatorid, string vbillstatus, int pageNumber, int pageSize)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【Getfdc_pm_repairapplyByID 输入参数】voperatorid:{voperatorid} vbillstatus:{vbillstatus}");
                if (voperatorid.IsNullOrEmpty())
                {
                    res.ErrMsg = "参数错误";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var list = service.Getfdc_pm_repairapplyList(voperatorid, vbillstatus, pageNumber, pageSize);
                logger.Info($"Getfdc_pm_repairapplyByID 输出参数】list:{JsonConvert.SerializeObject(list)}");
                res.IsSuccess = true;
                res.Data = list;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }
        /// <summary>
        /// 根据单据处理人获取所有报修单、投诉单
        /// </summary>
        /// <param name="vrepairapplycode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Getfdc_pm_repairapplyByCode")]
        public JsonResult Getfdc_pm_repairapplyByCode(string vrepairapplycode)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【Getfdc_pm_repairapplyByID 输入参数】vrepairapplycode:{vrepairapplycode}");
                if (vrepairapplycode.IsNullOrEmpty())
                {
                    res.ErrMsg = "参数错误";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var model = service.Getfdc_pm_repairapplyByCode(vrepairapplycode);
                logger.Info($"Getfdc_pm_repairapplyByCode 输出参数】model:{JsonConvert.SerializeObject(model)}");
                res.IsSuccess = true;
                res.Data = model;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }
        /// <summary>
        /// 更新报事单状态
        /// </summary>
        /// <param name="vrepairapplycode"></param>
        /// <param name="vbillstatus"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Updatefdc_pm_repairapply")]
        [AllowAnonymous]
        public JsonResult Updatefdc_pm_repairapply(string vrepairapplycode, string vbillstatus)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【Updatefdc_pm_repairapply 输入参数】vrepairapplycode:{vrepairapplycode}");
                if (vrepairapplycode.IsNullOrEmpty())
                {
                    res.ErrMsg = "参数错误";
                    return Json(res);
                }
                var service = new fdc_pm_repairapplyManager();
                var model = service.Updatefdc_pm_repairapply(vrepairapplycode, vbillstatus);
                logger.Info($"Updatefdc_pm_repairapply 输出参数】model:{JsonConvert.SerializeObject(model)}");
                res.IsSuccess = true;
                res.Data = model;
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }
        /// <summary>
        /// 添加缴费单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertPay")]
        [AllowAnonymous]
        public JsonResult InsertPay([FromBody] PayDTO dto)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【InsertPay 输入参数】dto:{JsonConvert.SerializeObject(dto)}");

                var service = new fdc_pm_meterrecordManager();
                var pay = new JSAPIPay();
                var order = service.Db.Queryable<Order>().First(m => m.Farerowid == dto.farwarid);
                if (order != null)
                {
                    service.Db.Deleteable(order).ExecuteCommand();
                }
                var revfarecode = "";
                var resstr = service.InsertPay(dto.farwarid, dto.hname, dto.ownerid, dto.ownername, dto.paytype, dto.money.Value.ToString(), ref revfarecode);
                if (resstr != "success")
                {
                    res.IsSuccess = false;
                    res.ErrMsg = resstr;
                    logger.Info($"InsertPay 输出参数】res:{JsonConvert.SerializeObject(res)}");
                    return Json(res);
                }
                res.IsSuccess = true;
                res.Data = revfarecode;
                logger.Info($"InsertPay 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }
        /// <summary>
        /// 添加收费单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertRevfare")]
        public JsonResult InsertRevfare([FromBody] RevfareDTO dto)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【InsertRevfare 输入参数】dto:{JsonConvert.SerializeObject(dto)}");

                var service = new fdc_pm_meterrecordManager();
                var resstr = service.InsertRevfare(dto.farwarid, dto.hname, dto.ownername, dto.money.Value.ToString(), dto.voperatorid);

                if (resstr != "success")
                {
                    res.IsSuccess = false;
                    res.ErrMsg = resstr;
                    logger.Info($"InsertRevfare 输出参数】res:{JsonConvert.SerializeObject(res)}");
                    return Json(res);
                }
                res.IsSuccess = true;
                logger.Info($"InsertRevfare 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }
        #endregion


        /// <summary>
        /// 获取房产信息
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetOwnerHouse")]
        public JsonResult GetOwnerHouse(string phone)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【GetOwnerHouse 输入参数】phone:{phone}");
                if (phone.IsNullOrEmpty())
                {
                    res.ErrMsg = "参数错误";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var list = service.GetOwnerHouse(phone);
                res.IsSuccess = true;
                res.Data = list;
                logger.Info($"GetOwnerHouse 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }


        /// <summary>
        /// 获取车辆信息
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetvehicleList")]
        public JsonResult GetvehicleList(string phone)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【GetvehicleList 输入参数】phone:{phone}");
                if (phone.IsNullOrEmpty())
                {
                    res.ErrMsg = "参数错误";
                    return Json(res);
                }
                var service = new fdc_pm_meterrecordManager();
                var list = service.GetvehicleList(phone);
                res.IsSuccess = true;
                res.Data = list;
                logger.Info($"GetvehicleList 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }

        }

        /// <summary>
        /// 添加车辆信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Insertfdc_pm_vehicle")]
        public JsonResult Insertfdc_pm_vehicle([FromBody] OwnerDTO dto)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【Insertfdc_pm_vehicle 输入参数】dto:{JsonConvert.SerializeObject(dto)}");

                var service = new fdc_pm_meterrecordManager();
                var resstr = service.Insertfdc_pm_vehicle(dto);

                if (resstr != "success")
                {
                    res.IsSuccess = true;
                    res.ErrMsg = resstr;
                    logger.Info($"Insertfdc_pm_vehicle 输出参数】res:{JsonConvert.SerializeObject(res)}");
                    return Json(res);
                }
                res.IsSuccess = true;
                res.Data = resstr;
                logger.Info($"Insertfdc_pm_vehicle 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }

        /// <summary>
        /// 删除车辆信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Deletefdc_pm_vehicle")]
        public JsonResult Deletefdc_pm_vehicle([FromBody] OwnerDTO dto)
        {
            var res = new ApiResponse();

            try
            {
                logger.Info($"【Deletefdc_pm_vehicle 输入参数】dto:{JsonConvert.SerializeObject(dto)}");

                var service = new fdc_pm_meterrecordManager();
                var resstr = service.Deletefdc_pm_vehicle(dto);

                if (resstr != "success")
                {
                    res.IsSuccess = true;
                    res.ErrMsg = resstr;
                    logger.Info($"Deletefdc_pm_vehicle 输出参数】res:{JsonConvert.SerializeObject(res)}");
                    return Json(res);
                }
                res.IsSuccess = true;
                res.Data = resstr;
                logger.Info($"Deletefdc_pm_vehicle 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }

        /// <summary>
        /// 获取微信openid
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [Route("getOpenId")]
        public JsonResult GetOpenId(string code)
        {

            var res = new ApiResponse();
            try
            {
                logger.Info($"【GetOpenId 输入参数】dto:{code}");
                var openid = VxinUtils.GetOpenID(code);//获取微信openid
                fdc_pm_ownerManager ownerManager = new fdc_pm_ownerManager();
                bool isExist = ownerManager.CheckWxOpenId(openid);//校验openID是否存在

                if (isExist)
                {
                    var model = ownerManager.LoginOwnerByOpenId(openid);

                    //获取令牌，返回客户端
                    Dictionary<string, string> keyValuePairs = new Dictionary<string, string>
                    {
                        { "UserID", model.Ownerid }
                    };
                    var tnToken = tokenHelper.CreateToken(keyValuePairs);

                    res.Msg = "bind";
                    res.Data = model;
                    res.TnToken = tnToken;
                }
                else
                {
                    res.Msg = "newopenid";
                }
                res.IsSuccess = true;
                res.OpenId = openid;
                logger.Info($"【GetOpenId 输出参数】res:{JsonConvert.SerializeObject(res)}");
                return Json(res);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                res.ErrMsg = ex.Message;
                res.Code = 500;
                return Json(res);
            }
        }
    }
}
