using Sugar.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;
using Utils;
using System.Data;
using System.IO;

public class fdc_pm_meterrecordManager : DbContext<fdc_pm_meterrecord>
{
    private NLog.Logger logger;
    public fdc_pm_meterrecordManager()
    {
        logger = NLog.LogManager.GetCurrentClassLogger();
    }


    //当前类已经继承了 DbContext增、删、查、改的方法

    //这里面写的代码不会给覆盖,如果要重新生成请删除 fdc_pm_meterrecordManager.cs

    /// <summary>
    /// 根据业主手机号返回业主信息
    /// </summary>
    /// <param name="dto"></param>
    public OwnerDTO LoginOwner(string phone)
    {
        var sql = $"SELECT a.ownerid,a.ownername,hcode='',hname='',bcode='',bname='',a.tel FROM dbo.fdc_pm_owner a WHERE a.tel like '%{phone}%'";

        logger.Info($"【LoginOwner】sql:{sql}");

        var model = Db.Ado.SqlQuerySingle<OwnerDTO>(sql);
        return model;
    }
    /// <summary>
    /// 根据手机号返回业主的房屋信息列表
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    public List<OwnerDTO> BindHouse(string phone)
    {
        var sql = $"SELECT a.ownerid,a.ownername,c.hcode,c.hname,d.bcode,d.bname,a.tel FROM dbo.fdc_pm_owner a INNER JOIN dbo.fdc_pm_ownerhouse b ON a.ownerid=b.ownerid INNER JOIN fdc_pm_house c ON b.hcode=c.hcode INNER JOIN dbo.fdc_pm_build d ON c.bcode=d.bcode WHERE a.tel like '%{phone}%'";
        var list = Db.Ado.SqlQuery<OwnerDTO>(sql);
        return list;
    }
    /// <summary>
    /// 抄表单添加
    /// </summary>
    /// <param name="chaoBiaos"></param>
    /// <returns></returns>
    public string Fdc_pm_meterAdd(List<ChaobiaoDTO> chaoBiaos)
    {
        try
        {
            Db.BeginTran();

            var maxbillcodeManager = new maxbillcodeManager(Db);


            foreach (var item in chaoBiaos)
            {
                //创建房产仪表表的对象
                fdc_pm_housemeter fdc_pm_housemeter = new fdc_pm_housemeter();
                //赋值房产编码
                fdc_pm_housemeter.hcode = item.hcode;
                //赋值仪表编码
                fdc_pm_housemeter.pk_meterid = item.pk_meterid;
                //获取房产仪表对象
                var housemeterModel = Db.Queryable<fdc_pm_housemeter>().Single(m => m.pk_meterid == item.pk_meterid);
                //创建抄表单对象
                fdc_pm_meterrecord_b fdc_pm_meterrecord_b = new fdc_pm_meterrecord_b();
                //赋值
                fdc_pm_meterrecord_b.hcode = item.hcode;
                fdc_pm_meterrecord_b.pk_meterid = item.pk_meterid;
                //主表id
                DateTime dt = DateTime.Now;
                //获取编号
                var billcode = maxbillcodeManager.GetMaxBillCode("", PublicField.ID, true);
                //仪表名称
                fdc_pm_meterrecord_b.autoid = string.IsNullOrEmpty(fdc_pm_meterrecord_b.autoid) ? billcode : fdc_pm_meterrecord_b.autoid;
                fdc_pm_meterrecord_b.metername = housemeterModel.metername;
                //水表名称               
                fdc_pm_meterrecord_b.billno = item.bcode + dt.ToString("yyyyMMdd") + item.userid;
                //楼栋实例
                var buildModel = Db.Queryable<fdc_pm_build>().Single(m => m.bcode == item.bcode);
                if (!Db.Queryable<fdc_pm_meterrecord_b>().Any(m => m.billno == fdc_pm_meterrecord_b.billno))
                {
                    fdc_pm_meterrecord fdc_pm_meterrecord = new fdc_pm_meterrecord();
                    //抄表单主键
                    fdc_pm_meterrecord.billno = fdc_pm_meterrecord_b.billno;
                    //表的类型
                    fdc_pm_meterrecord.pk_metertypeid = housemeterModel.metertype;
                    fdc_pm_meterrecord.bcode = item.bcode;
                    fdc_pm_meterrecord.pcode = buildModel.pcode;
                    fdc_pm_meterrecord.metertime = DateTime.Now;
                    fdc_pm_meterrecord.employeid = item.userid;
                    fdc_pm_meterrecord.flag = "0";
                    fdc_pm_meterrecord.voperatorid = item.userid;
                    fdc_pm_meterrecord.doperatordate = DateTime.Now;
                    fdc_pm_meterrecord.billstatus = "3";


                    fdc_pm_meterrecord.bdate = DateTime.Now;

                    fdc_pm_meterrecord.edate = DateTime.Now;
                    var secidModel = Db.Queryable<fdc_pm_userinmanagementsec>().First(it => it.staffcode == item.userid);

                    fdc_pm_meterrecord.pk_managementsecid = secidModel.pk_managementsecid;
                    Insert(fdc_pm_meterrecord);
                }


                var nlast = housemeterModel.amount;
                if (item.amount.ToDecimal() < nlast)
                {
                    return "本期表小于上期表";
                }
                fdc_pm_meterrecord_b.flag = "0";
                fdc_pm_meterrecord_b.nlast = nlast;
                fdc_pm_meterrecord_b.ncur = item.amount.ToDecimal();

                fdc_pm_meterrecord_b.ndosage = item.amount.ToDecimal() - nlast;
                fdc_pm_meterrecord_b.noldend = 0;
                fdc_pm_meterrecord_b.nnewbegin = 0;
                fdc_pm_meterrecord_b.iflag = "0";


                var fdc_pm_housemeterinfo = fdc_pm_housemeterDb.GetSingle(m => m.pk_meterid == item.pk_meterid);
                fdc_pm_housemeterinfo.amount = item.amount.ToDecimal();


                Db.Updateable(fdc_pm_housemeterinfo).ExecuteCommand();

                var success = new fdc_pm_meterrecord_bManager().Insert(fdc_pm_meterrecord_b);
            }
            Db.CommitTran();
            return "success";
        }
        catch (Exception ex)
        {
            Db.RollbackTran();
            logger.Error(ex);
            return "添加抄表单失败：" + ex.Message;
        }
    }

    /// <summary>
    /// 获取欠费对象
    /// </summary>
    /// <param name="phone"></param>
    public List<dynamic> Getfdc_pm_unfare(string phone)
    {
        var sql = $@"SELECT farerowid,e.weixinid,e.ownername,d.hname,f.billitemname,b.nneedpaymny,b.dbegindate,b.denddate,e.tel,e.ownerid FROM fdc_pm_fare_b  b
            left join fdc_pm_fare a on a.pk_farecode = b.pk_farecode
            inner join fdc_pm_house c on b.hcode = c.hcode
            inner join fdc_pm_ownerhouse d on c.hcode = d.hcode
            inner join fdc_pm_owner e on d.ownerid = e.ownerid
            inner join fdc_pm_billsitem f on b.billitemcode = f.billitemcode
            where b.nneedpaymny > isnull(nrevmny,0)  AND e.tel like '%{phone}%'";
        var list = Db.SqlQueryable<dynamic>(sql).ToList();
        return list;
    }
    /// <summary>
    /// 获取欠费对象
    /// </summary>
    /// <param name="phone"></param>
    public List<dynamic> Getfdc_pm_unfarebysearch(string ownername, string hname, string staffcode)
    {
        string strsql = "";
        if (!string.IsNullOrEmpty(ownername) && !string.IsNullOrEmpty(hname))
        {
            strsql = " AND ( c.hname LIKE '%" + hname + "%' or e.ownername LIKE '%" + ownername + "%')";
        }
        else if (!string.IsNullOrEmpty(ownername))
        {
            strsql = " AND e.ownername LIKE '%" + ownername + "%'";
        }
        else if (!string.IsNullOrEmpty(hname))
        {
            strsql = " AND c.hname LIKE '%" + hname + "%' ";
        }

        var sql = $@"SELECT farerowid,e.ownername,d.hname,f.billitemname,b.nneedpaymny,b.dbegindate,b.denddate,e.ownerid FROM  fdc_pm_fare_b b 
            inner join fdc_pm_house c on b.hcode = c.hcode
            inner join fdc_pm_ownerhouse d on c.hcode = d.hcode
            inner join fdc_pm_owner e on d.ownerid = e.ownerid
            inner join fdc_pm_billsitem f on b.billitemcode = f.billitemcode
			INNER JOIN dbo.fdc_pm_build g ON c.bcode=g.bcode
			INNER JOIN dbo.fdc_pm_project h ON g.pcode=h.pcode
			INNER JOIN fdc_pm_userinmanagementsec i on g.pk_managementsecid=i.pk_managementsecid
			WHERE  b.billitemcode in ( select billsitemId from dbo.MangmengBillsitem where  ManagementId in 
            (select pk_managementsecid from fdc_pm_userinmanagementsec where staffcode='{staffcode}')) " + strsql
            + " GROUP BY farerowid,e.ownername,d.hname,f.billitemname,b.nneedpaymny,b.dbegindate,b.denddate,e.ownerid";
        var list = Db.SqlQueryable<dynamic>(sql).ToList();
        return list;
    }
    /// <summary>
    /// 获取缴费记录
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    public List<dynamic> Getfdc_pm_revfare(string phone)
    {
        var sql = $@"SELECT e.weixinid,e.ownername,c.hname,f.billitemname,b.nrevmny,b.dbdate,b.dedate
            FROM fdc_pm_revfare a
            inner join fdc_pm_revfare_b b on a.pk_revfarecode=b.pk_revfarecode
            inner join fdc_pm_house c on b.hcode=c.hcode
            inner join fdc_pm_ownerhouse d on c.hcode=d.hcode
            inner join fdc_pm_owner e on d.ownerid=e.ownerid
            inner join fdc_pm_billsitem f on b.billitemcode=f.billitemcode WHERE  e.tel like '%{phone}%'";
        var list = Db.SqlQueryable<dynamic>(sql).ToList();
        return list;
    }

    /// <summary>
    /// 获取缴费记录
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    public List<dynamic> Getfdc_pm_revfarebysearch(string ownername, string hname, string staffcode)
    {
        string strsql = "";
        if (!string.IsNullOrEmpty(ownername) && !string.IsNullOrEmpty(hname))
        {
            strsql = " AND ( c.hname LIKE '%" + hname + "%' or e.ownername LIKE '%" + ownername + "%')";
        }
        else if (!string.IsNullOrEmpty(ownername))
        {
            strsql = " AND e.ownername LIKE '%" + ownername + "%'";
        }
        else if (!string.IsNullOrEmpty(hname))
        {
            strsql = " AND c.hname LIKE '%" + hname + "%' ";
        }

        var sql = @"SELECT h.pname,a.pk_revfarecode,e.ownername,c.hname,f.billitemname,b.nrevmny,b.dbdate,b.dedate,a.dbilldate,j.UserName
            FROM fdc_pm_revfare a
            inner join fdc_pm_revfare_b b on a.pk_revfarecode=b.pk_revfarecode
            inner join fdc_pm_house c on b.hcode=c.hcode
            inner join fdc_pm_ownerhouse d on c.hcode=d.hcode
            inner join fdc_pm_owner e on d.ownerid=e.ownerid
            inner join fdc_pm_billsitem f on b.billitemcode=f.billitemcode 
			INNER JOIN dbo.fdc_pm_build g ON c.bcode=g.bcode
			INNER JOIN dbo.fdc_pm_project h ON g.pcode=h.pcode
			INNER JOIN fdc_pm_userinmanagementsec i on g.pk_managementsecid=i.pk_managementsecid
	        INNER JOIN dbo.users j ON a.voperatorid=j.StaffCode
			WHERE  b.billitemcode in ( select billsitemId from dbo.MangmengBillsitem where  ManagementId in 
            (select pk_managementsecid from fdc_pm_userinmanagementsec where staffcode='" + staffcode + "')) " + strsql
            + " GROUP  BY h.pname,a.pk_revfarecode,e.ownername,c.hname,f.billitemname,b.nrevmny,b.dbdate,b.dedate,a.dbilldate,j.UserName";

        var list = Db.SqlQueryable<dynamic>(sql).ToList();
        return list;
    }
    /// <summary>
    /// 获取报修记录
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    public List<dynamic> GetRepaireList(string phone)
    {
        var sql = $@"SELECT e.username as voperatorname,f.username as vemployeename,d.deptname,a.*
              FROM fdc_pm_repairapply a 
                            INNER JOIN fdc_pm_house b on a.hcode=b.hcode 
                             INNER JOIN fdc_pm_owner c on a.ownerid=c.ownerid 
                            INNER JOIN  depts d on a.deptcode=d.deptcode 
                           LEFT JOIN users e on a.voperatorid=e.staffcode
                            LEFT JOIN users f on a.vemployeeid=f.staffcode
                           WHERE c.tel like '%{phone}%'  and a.billtype<>'3' ";
        //logger.Info($"sql:{sql}");
        var list = Db.SqlQueryable<dynamic>(sql).OrderBy("dapplydate").ToList();
        return list;
    }
    /// <summary>
    /// 获取所有报修数据
    /// </summary>
    /// <param name="voperatorid"></param>
    /// <param name="vbillstatus"></param>
    /// <param name="pageNumber"></param>
    /// <param name="pageSize"></param>
    /// <returns></returns>
    public List<dynamic> Getfdc_pm_repairapplyList(string voperatorid, string vbillstatus, int pageNumber, int pageSize)
    {
        int startindex = (pageNumber - 1) * pageSize + 1; int limitindex = (pageNumber - 1) * pageSize + pageSize;
        var sql = $@"select * from ( 
				select   ROW_NUMBER() OVER(ORDER BY a.vrepairapplycode ) AS RowNumber,a.vrepairapplycode,dapplydate= CONVERT(varchar(100), a.dapplydate, 23 ),a.billtype,drequiredate= REPLACE(a.drequiredate, '/','-' ),a.vbillstatus,billtypename=CASE WHEN a.billtype='1' THEN '报事单' WHEN a.billtype='2' THEN '整改通知单' WHEN a.billtype='3' THEN '投诉单' WHEN a.billtype='4' THEN '咨询单' WHEN a.billtype='5' THEN '增值服务单' ELSE '' END,vbillstatusname=CASE WHEN a.vbillstatus='1' THEN '已报事未处理' WHEN a.vbillstatus='2' THEN '已处理' END,COUNT(*) OVER(PARTITION BY '') AS TotalCount
				 from fdc_pm_repairapply a
                 where a.voperatorid= '{voperatorid}'  and a.vbillstatus = '{vbillstatus}') repair
				where  RowNumber between {startindex} and  {limitindex} ";
        //logger.Info($"sql:{sql}");
        var list = Db.SqlQueryable<dynamic>(sql).ToList();
        return list;
    }
    /// <summary>
    /// 获取维修详细信息
    /// </summary>
    /// <param name="vrepairapplycode"></param>
    /// <returns></returns>
    public RepairApplyDTO Getfdc_pm_repairapplyByCode(string vrepairapplycode)
    {
        RepairApplyDTO dto = new RepairApplyDTO();

        var sql = $@"SELECT  a.vbillno ,g.vtypename,c.hname ,a.vplace,a.dbilldate,a.drequiredate,a.vcontent,
                            f.UserName AS voperatorname
                            FROM    dbo.fdc_pm_repairapply a
                            INNER JOIN dbo.fdc_pm_house c ON a.hcode = c.hcode
                            INNER JOIN dbo.fdc_pm_owner d ON a.ownerid = d.ownerid
                            LEFT JOIN dbo.users e ON a.vemployeeid=e.StaffCode
                            LEFT JOIN dbo.users f ON a.voperatorid=f.StaffCode
                            LEFT JOIN fdc_pm_repairtype g on a.vtypecode=g.vtypecode 
			                WHERE a.vrepairapplycode ='{vrepairapplycode}'";
        var repairApply = Db.Ado.SqlQuerySingle<RepairApplyDTO>(sql);


        var strsql = $@"select fileurl from fdc_pm_file where code = '{vrepairapplycode}'";
        var fileList = Db.Ado.SqlQuery<FileImg>(strsql);

        //dto.vbillno = repairApply.vbillno;
        //dto.vtypename = repairApply.vtypename;
        //dto.hname = repairApply.hname;
        //dto.vplace = repairApply.vplace;
        //dto.dbilldate = repairApply.dbilldate;
        //dto.drequiredate = repairApply.drequiredate;
        //dto.voperatorname = repairApply.voperatorname;
        //dto.vcontent = repairApply.vcontent;

        if (fileList.Count > 0)
        {
            repairApply.fileimgList = fileList;
        }


        return repairApply;
    }
    /// <summary>
    /// 获取投诉记录
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    public List<dynamic> GetComplaintList(string phone)
    {
        var sql = $@"SELECT e.username as voperatorname,f.username as vemployeename,d.deptname,a.*
              FROM fdc_pm_repairapply a 
                            INNER JOIN fdc_pm_house b on a.hcode=b.hcode 
                             INNER JOIN fdc_pm_owner c on a.ownerid=c.ownerid 
                            INNER JOIN  depts d on a.deptcode=d.deptcode 
                           LEFT JOIN users e on a.voperatorid=e.staffcode
                            LEFT JOIN users f on a.vemployeeid=f.staffcode
                           WHERE c.tel like '%{phone}%'  and a.billtype='3' order by a.dapplydate ";
        var list = Db.Ado.SqlQuery<dynamic>(sql);
        return list;
    }
    /// <summary>
    /// 获取房产信息
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    public List<dynamic> GetOwnerHouse(string phone)
    {
        var sql = $@" SELECT  a.ownerid,a.ownername,c.hname ,d.bname
                    FROM fdc_pm_owner a 
                    INNER JOIN fdc_pm_ownerhouse b on a.ownerid=b.ownerid and b.flag=1 
                    INNER JOIN  fdc_pm_house c on b.hcode=c.hcode 
                    inner join fdc_pm_build d on c.bcode=d.bcode 
                    inner join fdc_pm_project p on d.pcode=p.pcode 
                    where  a.tel like '%{phone}%'";
        var list = Db.SqlQueryable<dynamic>(sql).ToList();
        return list;
    }

    /// <summary>
    /// 获取房产信息
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    public List<dynamic> GetvehicleList(string phone)
    {
        var sql = $@"SELECT  a.ownerid,a.ownername,b.parkcard,b.vcarcode,b.vehicleid
                    FROM fdc_pm_owner a 
                    INNER JOIN fdc_pm_vehicle b on a.ownerid=b.ownerid 
                    where a.tel like '%{phone}%'";
        var list = Db.SqlQueryable<dynamic>(sql).ToList();
        return list;
    }
    /// <summary>
    /// 添加车辆信息
    /// </summary>
    /// <param name="dto">需要插入的数据</param>
    /// <returns></returns>
    public string Insertfdc_pm_vehicle(OwnerDTO dto)
    {
        Db.BeginTran();
        try
        {
            var maxbillcodeManager = new maxbillcodeManager(Db);
            string sid = maxbillcodeManager.GetMaxBillCode("", PublicField.ID, true);//获取最大单号

            string sql1 = string.Format("SELECT o.ownerid FROM  dbo.fdc_pm_owner o WHERE o.ownerid='{0}'", dto.Ownerid);
            logger.Info(string.Format("【Insertfdc_pm_vehicle】sql1:{0}", sql1));
            var model = Db.SqlQueryable<dynamic>(sql1).Single();
            if (model == null)
            {
                Db.RollbackTran();
                return "不存在的用户";
            }

            string sql2 = string.Format("SELECT o.vehicleid FROM  dbo.fdc_pm_vehicle o WHERE o.vcarcode='{0}' and o.ownerid='{1}'", dto.Vcarcode, dto.Ownerid);
            logger.Info(string.Format("【Insertfdc_pm_vehicle】sql2:{0}", sql2));
            var model2 = Db.SqlQueryable<dynamic>(sql2).Single();
            if (model2 != null)
            {
                Db.RollbackTran();
                return "车牌号已存在";
            }

            //检验车辆数是否超过最多可停车数
            var itemdictionaryManager = new itemdictionaryManager();

            var itemdictionary = itemdictionaryManager.itemdictionaryDb.GetSingle(m => m.ItemID == "停车场编号");
            if (itemdictionary != null)
            {
                string parkcodelist = itemdictionary.ItemName;
                if (parkcodelist.Contains(","))
                {
                    parkcodelist = parkcodelist.Replace(",", "','");
                }


                string sql3 = string.Format(@"SELECT a.hcode
                                           FROM  dbo.fdc_pm_ownerhouse a
                                          INNER JOIN dbo.fdc_pm_house b ON a.hcode = b.hcode
                                          WHERE a.ownerid = '{0}' AND b.bcode IN('{1}')
                                            GROUP  BY a.hcode", dto.Ownerid, parkcodelist);
                logger.Info(string.Format("【Insertfdc_pm_vehicle】sql3:{0}", sql3));
                int parkcodeCount = Db.SqlQueryable<dynamic>(sql3).Count();


                var itemdictionary2 = itemdictionaryManager.itemdictionaryDb.GetSingle(m => m.ItemID == "车位可停车辆数");

                if (itemdictionary2 != null)
                {
                    int num = int.Parse(itemdictionary2.ItemName);

                    string sql4 = string.Format("SELECT vehicleid FROM dbo.fdc_pm_vehicle WHERE ownerid='{0}'", dto.Ownerid);
                    logger.Info(string.Format("【Insertfdc_pm_vehicle】sql4:{0}", sql4));
                    int vehicleCount = Db.SqlQueryable<dynamic>(sql4).Count();

                    //车位数*一个车位可停车数<车辆数
                    if (parkcodeCount * num < vehicleCount)
                    {
                        Db.RollbackTran();
                        return "车辆数已超过规定数量";
                    }

                }

            }

            //sql1 = $"select vehicleid from fdc_pm_vehicle where vehicleid='{sid}'";
            //var vehicleid = Db.Ado.GetString(sql1);
            //if (vehicleid.IsNotNullAndEmpty())   //表示自MaxBillCode获取的主键最大值在fdc_pm_repairapply表中已存在
            //{
            //    sql1 = $"SELECT TOP 1 vehicleid FROM fdc_pm_vehicle  ORDER BY vehicleid DESC ";
            //    var maxcode = Db.Ado.GetString(sql1);  //重新获取
            //    //去除前缀
            //    maxcode = maxcode.Replace(PublicField.RH + DateTime.Now.ToString("yy"), "");

            //    sql1 = $"update maxbillcode set maxcode={maxcode} where sgn='{PublicField.RH}' and AccountYear='{DateTime.Now.ToString("yy")}'";
            //    logger.Debug($"【Insertfdc_pm_repair】sql1:{sql1}");
            //    //更新MaxBillCode
            //    Db.Ado.ExecuteCommand(sql1);
            //    sid = maxbillcodeManager.GetMaxBillCode("", PublicField.RH, false);//重新获取最大单号
            //    logger.Debug($"【Insertfdc_pm_repair】sid:{sid}");
            //}
            //var hcode = model.hcode;
            //var ownerid = model.ownerid;
            fdc_pm_vehicle fdc_pm_vehicleinfo = new fdc_pm_vehicle();
            fdc_pm_vehicleinfo.vehicleid = sid;
            fdc_pm_vehicleinfo.ownerid = dto.Ownerid;
            fdc_pm_vehicleinfo.parkcard = "";
            fdc_pm_vehicleinfo.pk_corp = "";
            fdc_pm_vehicleinfo.carstatus = 1;

            fdc_pm_vehicleinfo.pk_managementsecid = "";
            fdc_pm_vehicleinfo.status = "";
            fdc_pm_vehicleinfo.ts = DateTime.Now;
            fdc_pm_vehicleinfo.vcarcode = dto.Vcarcode;

            //fdc_pm_vehicleinfo.vcolor = dto.txtdrequiredate;
            fdc_pm_vehicleinfo.vmemo = "";
            //fdc_pm_vehicleinfo.vmodel = ownerid;
            //fdc_pm_vehicleinfo.vowner = "";

            Db.Insertable(fdc_pm_vehicleinfo).ExecuteReturnEntity();


            Db.CommitTran();
            return "success";
        }
        catch (Exception ex)
        {
            Db.RollbackTran();
            //logger.Error(ex);
            throw;
        }
    }


    /// <summary>
    /// 删除车辆信息
    /// </summary>
    /// <param name="dto">需要删除的数据</param>
    /// <returns></returns>
    public string Deletefdc_pm_vehicle(OwnerDTO dto)
    {
        Db.BeginTran();
        try
        {

            string sql1 = string.Format("SELECT o.ownerid FROM  dbo.fdc_pm_owner o WHERE o.ownerid='{0}'", dto.Ownerid);
            logger.Info(string.Format("【Deletefdc_pm_vehicle】sql1:{0}", sql1));
            var model = Db.SqlQueryable<dynamic>(sql1).Single();
            if (model == null)
            {
                Db.RollbackTran();
                return "不存在的用户";
            }

            string sql2 = string.Format("SELECT  vehicleid from fdc_pm_vehicle WHERE vehicleid='{0}' AND carstatus=0", dto.Vehicleid);
            logger.Info(string.Format("【Deletefdc_pm_vehicle】sql2:{0}", sql2));
            var model2 = Db.SqlQueryable<dynamic>(sql2).Single();
            if (model2 != null)
            {
                Db.RollbackTran();
                return "车辆已停状态，无法删除";
            }

            //sql1 = $"select vehicleid from fdc_pm_vehicle where vehicleid='{sid}'";
            //var vehicleid = Db.Ado.GetString(sql1);
            //if (vehicleid.IsNotNullAndEmpty())   //表示自MaxBillCode获取的主键最大值在fdc_pm_repairapply表中已存在
            //{
            //    sql1 = $"SELECT TOP 1 vehicleid FROM fdc_pm_vehicle  ORDER BY vehicleid DESC ";
            //    var maxcode = Db.Ado.GetString(sql1);  //重新获取
            //    //去除前缀
            //    maxcode = maxcode.Replace(PublicField.RH + DateTime.Now.ToString("yy"), "");

            //    sql1 = $"update maxbillcode set maxcode={maxcode} where sgn='{PublicField.RH}' and AccountYear='{DateTime.Now.ToString("yy")}'";
            //    logger.Debug($"【Insertfdc_pm_repair】sql1:{sql1}");
            //    //更新MaxBillCode
            //    Db.Ado.ExecuteCommand(sql1);
            //    sid = maxbillcodeManager.GetMaxBillCode("", PublicField.RH, false);//重新获取最大单号
            //    logger.Debug($"【Insertfdc_pm_repair】sid:{sid}");
            //}
            //var hcode = model.hcode;
            //var ownerid = model.ownerid;
            fdc_pm_vehicle fdc_pm_vehicleinfo = new fdc_pm_vehicle();
            fdc_pm_vehicleinfo.vehicleid = dto.Vehicleid;

            //fdc_pm_vehicleinfo.vcolor = dto.txtdrequiredate;
            //fdc_pm_vehicleinfo.vmemo = "";
            //fdc_pm_vehicleinfo.vmodel = ownerid;
            //fdc_pm_vehicleinfo.vowner = "";

            Db.Deleteable(fdc_pm_vehicleinfo).ExecuteCommand();


            Db.CommitTran();
            return "success";
        }
        catch (Exception ex)
        {
            Db.RollbackTran();
            //logger.Error(ex);
            throw;
        }
    }

    /// <summary>
    /// 添加报事单或者投诉单
    /// </summary>
    /// <param name="dto">需要插入的数据</param>
    /// <param name="type">0:报事单 1：投诉单</param>
    /// <returns></returns>
    public string Insertfdc_pm_repair(BaoshiDTO dto, int type)
    {
        Db.BeginTran();
        try
        {
            var maxbillcodeManager = new maxbillcodeManager(Db);
            string sid = maxbillcodeManager.GetMaxBillCode("", PublicField.RH, false);//获取最大单号
            string sql1 = string.Format("SELECT oh.hcode,o.ownerid FROM  dbo.fdc_pm_ownerhouse oh  INNER join dbo.fdc_pm_owner o ON o.ownerid=oh.ownerid WHERE o.tel='{0}'", dto.tel);
            logger.Info(string.Format("【Insertfdc_pm_repair】sql1:{0}", sql1));
            var model = Db.SqlQueryable<dynamic>(sql1).Single();
            if (model == null)
            {
                Db.RollbackTran();
                return "不存在的用户";
            }
            sql1 = $"select vrepairapplycode from fdc_pm_repairapply where vrepairapplycode='{sid}'";
            var vrepairapplycode = Db.Ado.GetString(sql1);
            if (vrepairapplycode.IsNotNullAndEmpty())   //表示自MaxBillCode获取的主键最大值在fdc_pm_repairapply表中已存在
            {
                sql1 = $"SELECT TOP 1 vrepairapplycode FROM fdc_pm_repairapply  ORDER BY vrepairapplycode DESC ";
                var maxcode = Db.Ado.GetString(sql1);  //重新获取
                //去除前缀
                maxcode = maxcode.Replace(PublicField.RH + DateTime.Now.ToString("yy"), "");

                sql1 = $"update maxbillcode set maxcode={maxcode} where sgn='{PublicField.RH}' and AccountYear='{DateTime.Now.ToString("yy")}'";
                logger.Debug($"【Insertfdc_pm_repair】sql1:{sql1}");
                //更新MaxBillCode
                Db.Ado.ExecuteCommand(sql1);
                sid = maxbillcodeManager.GetMaxBillCode("", PublicField.RH, false);//重新获取最大单号
                logger.Debug($"【Insertfdc_pm_repair】sid:{sid}");
            }
            var hcode = model.hcode;
            var ownerid = model.ownerid;
            fdc_pm_repairapply fdc_pm_repairapplyinfo = new fdc_pm_repairapply();
            fdc_pm_repairapplyinfo.vtypecode = "10";//报事类型
            fdc_pm_repairapplyinfo.dapplydate = DateTime.Now;//日期
            fdc_pm_repairapplyinfo.dapplyer = "";
            fdc_pm_repairapplyinfo.dapprovedate = DateTime.Now;

            fdc_pm_repairapplyinfo.dclosedate = null;
            fdc_pm_repairapplyinfo.deptcode = "01";//处理部门编号
            fdc_pm_repairapplyinfo.dmakedate = DateTime.Now;
            fdc_pm_repairapplyinfo.drecievdate = null;

            fdc_pm_repairapplyinfo.drequiredate = dto.txtdrequiredate;
            fdc_pm_repairapplyinfo.hcode = hcode;
            fdc_pm_repairapplyinfo.ownerid = ownerid;
            fdc_pm_repairapplyinfo.pk_corp = "";

            fdc_pm_repairapplyinfo.pk_managementsecid = "";
            fdc_pm_repairapplyinfo.vapproveid = "";
            fdc_pm_repairapplyinfo.vapprovenote = "";
            fdc_pm_repairapplyinfo.vbillno = sid;

            fdc_pm_repairapplyinfo.vbillstatus = "1";
            fdc_pm_repairapplyinfo.vcontent = dto.content;//报事内容
            fdc_pm_repairapplyinfo.vemployeeid = "0002";//处理人编号
            fdc_pm_repairapplyinfo.voperatorid = "0001";//处理人编号

            fdc_pm_repairapplyinfo.vmemo = "暂无";//备注
            fdc_pm_repairapplyinfo.vplace = dto.place;
            fdc_pm_repairapplyinfo.vrepairapplycode = sid;
            fdc_pm_repairapplyinfo.vtel = dto.tel;

            fdc_pm_repairapplyinfo.bclose = "0";
            fdc_pm_repairapplyinfo.fsatisfied = "";
            fdc_pm_repairapplyinfo.dbilldate = DateTime.Now;
            fdc_pm_repairapplyinfo.billtype = type == 1 ? "3" : "1";//报事类型

            var entity = Db.Insertable(fdc_pm_repairapplyinfo).ExecuteReturnEntity();

            foreach (var item in dto.fileList)
            {
                var name = Guid.NewGuid().ToString();
                var filename = Guid.NewGuid().ToString() + Path.GetExtension(item.Name);
                var uploadSuccess = UploadFileHelper.Base64StringToFile(item.Content, filename);
                if (!uploadSuccess)
                {
                    Db.RollbackTran();
                    return "上传失败";
                }
                string sql = $"insert into Fdc_pm_file values ('{entity.vrepairapplycode}','{filename}','{name}')";
                var row = Db.Ado.ExecuteCommand(sql);
                if (row <= 0)
                {
                    Db.RollbackTran();
                    return "保存上传文件失败";
                }
            }

            Db.CommitTran();
            return "success";
        }
        catch (Exception ex)
        {
            Db.RollbackTran();
            //logger.Error(ex);
            throw;
        }
    }


    /// <summary>
    /// 插入缴费数据
    /// </summary>
    /// <param name="farwarid">欠费信息表fdc_pm_fare_b主键值</param>
    /// <param name="money">欠费金额</param>
    /// <param name="ownername">业主名称</param>
    /// <param name="hname">房屋名称</param>
    /// <param name="revfarecode">生成的收款单编号(fdc_pm_revfare)</param>
    /// <returns></returns>
    public string InsertPay(string farwarid, string hname, string voperatorid, string ownername, string paytype, string money, ref string revfarecode)
    {

        SetValue(farwarid, money);
        Db.BeginTran();
        try
        {
            var sql = " SELECT  ISNULL(a.nrevmny,0) nrevmny,b.rate,a1.voperatorid,f.psgn,b.bsgn,a1.cmemo,a1.vownername,'' as pk_billno,a.dneeddate,'N' as prerevflag,a.farerowid, a.pk_farecode, a.hcode,d.hname,d.define7,d.define8, a.ownerid, c.ownername,a.pk_meterid, a.vactymbegin, a.vactymend, a.dbegindate, a.denddate,a.dbegindate as dbdate,a.denddate as dedate, a.billitemcode,b.billitemname, a.memo,ISNULL(a.nneedpaymny,0) incomemny, (ISNULL(a.nneedpaymny,0)-ISNULL(a.nrevmny,0)) as  nneedpaymny, a.nremainmny, a.ndebitlatemny, a.ndebitmny, a.nlatefeemny, a.namount, a.ncurreading, a.nlastreading, a.nprice ,a.rentcontractid,a.pk_corp,a.deptcode,a.pk_managementsecid,a.isback,a.edate ,a.narea  " +
                                " FROM fdc_pm_fare_b a " +
                                " INNER JOIN fdc_pm_billsitem b on  a.billitemcode=b.billitemcode" +
                                " LEFT JOIN fdc_pm_owner c on a.ownerid=c.ownerid " +
                                " INNER JOIN fdc_pm_house d on a.hcode=d.hcode " +
                                " INNER JOIN fdc_pm_build e on d.bcode=e.bcode " +
                                " INNER JOIN fdc_pm_project f on e.pcode=f.pcode " +
                                " LEFT JOIN   (select pk_farecode,voperatorid,cmemo,vownername from fdc_pm_fare union all select pk_calfarecode,voperatorid,'',''  from fdc_pm_calfare union all select contractid,voperatorid, '','' from dbo.fdc_pm_rentcontract ) a1 on a.pk_farecode=a1.pk_farecode " +
                                " WHERE convert(varchar(20),a.farerowid) in('" + farwarid.ToString().Replace(",", "','") + "')   order by a.hcode, nneedpaymny ";

            var model = Db.Ado.SqlQuerySingle<dynamic>(sql);

            //收款主表
            fdc_pm_revfare fdc_pm_revfareinfo = new fdc_pm_revfare();
            //判断时候是预收款
            fdc_pm_revfareinfo.prerevflag = "N";
            //收款单编码
            //fdc_pm_revfareinfo.pk_revfarecode = "WXPay10001";
            //票据编码
            fdc_pm_revfareinfo.pk_billno = "";
            //收款日期
            fdc_pm_revfareinfo.dbilldate = DateTime.Now.Date;
            //操作日期
            fdc_pm_revfareinfo.doperatordate = DateTime.Now.Date;
            //房产编码
            fdc_pm_revfareinfo.hcode = model.hcode.ToString();

            //备注
            fdc_pm_revfareinfo.memo = "";
            //业主编码
            fdc_pm_revfareinfo.ownerid = model.ownerid.ToString();

            //业主名称
            fdc_pm_revfareinfo.vownername = model.ownername.ToString();
            if (ownername != model.ownername.ToString())
            {
                Db.RollbackTran();
                return "业主姓名不对";
            }
            if (hname != model.hname.ToString())
            {
                Db.RollbackTran();
                return "房产名称不对";
            }
            fdc_pm_revfareinfo.pk_actym = "";
            //收款单编码
            //fdc_pm_revfareinfo.pk_revfarecode = "WXPay10001";
            //实收金额
            fdc_pm_revfareinfo.revnmny = money.ToDecimal();

            //收款人id
            if (voperatorid == "wx")
            {
                var itemdictionaryManager = new itemdictionaryManager();
                string itemID = "微信默认收款人";
                string itemType = "param";
                var itemdictionaryInfo = itemdictionaryManager.GetItemdictionary(itemID, itemType);
                fdc_pm_revfareinfo.voperatorid = itemdictionaryInfo.ItemName;

            }
            else
            {
                fdc_pm_revfareinfo.voperatorid = voperatorid;
            }
            //实收金额   （重复）
            //fdc_pm_revfareinfo.revnmny = money.ToDecimal();
            //收费类型
            fdc_pm_revfareinfo.paytype = paytype;
            fdc_pm_revfareinfo.billtype = "S";//普通收款单
            fdc_pm_revfareinfo.billstatus = PublicField.APP_ORIGINAL;   //支付状态   0：
            //是否开票
            //fdc_pm_revfareinfo.Vouchflag = "flag";
            //缴费卡id
            fdc_pm_revfareinfo.vownerid = "";


            sql = $"SELECT a.csscode, a.cssname, a.jfcode, a.jfname, a.dfcode, a.dfname, a.cmemo,a.rate,a.ssgn  FROM settletype a  WHERE csscode='{fdc_pm_revfareinfo.paytype}' ";
            var model1 = Db.Ado.SqlQuerySingle<dynamic>(sql);

            //获取结算方式单据标示
            string ssgn = model1.ssgn.ToString();
            var psgn = model.psgn.ToString() + ssgn.Trim();



            /////////////////////////////////////////////////////////////////////////////////////
            decimal summny = 0;
            foreach (fdc_pm_revfare_b fdc_pm_revfare_bitem in fdc_pm_revfare_blist)
            {

                summny += Convert.ToDecimal(fdc_pm_revfare_bitem.nrevmny);

            }

            sql = " SELECT  ISNULL(a.nrevmny,0) nrevmny,b.rate,a1.voperatorid,f.psgn,b.bsgn,a1.cmemo,a1.vownername,'' as pk_billno,a.dneeddate,'N' as prerevflag,a.farerowid, a.pk_farecode, a.hcode,d.hname,d.define7,d.define8, a.ownerid, c.ownername,a.pk_meterid, a.vactymbegin, a.vactymend, a.dbegindate, a.denddate,a.dbegindate as dbdate,a.denddate as dedate, a.billitemcode,b.billitemname, a.memo,ISNULL(a.nneedpaymny,0) incomemny, (ISNULL(a.nneedpaymny,0)-ISNULL(a.nrevmny,0)) as  nneedpaymny, a.nremainmny, a.ndebitlatemny, a.ndebitmny, a.nlatefeemny, a.namount, a.ncurreading, a.nlastreading, a.nprice ,a.rentcontractid,a.pk_corp,a.deptcode,a.pk_managementsecid,a.isback,a.edate ,a.narea  " +
                                 " FROM fdc_pm_fare_b a " +
                                 " INNER JOIN fdc_pm_billsitem b on  a.billitemcode=b.billitemcode" +
                                 " LEFT JOIN fdc_pm_owner c on a.ownerid=c.ownerid " +
                                 " INNER JOIN fdc_pm_house d on a.hcode=d.hcode " +
                                 " INNER JOIN fdc_pm_build e on d.bcode=e.bcode " +
                                 " INNER JOIN fdc_pm_project f on e.pcode=f.pcode " +
                                 " LEFT JOIN   (select pk_farecode,voperatorid,cmemo,vownername from fdc_pm_fare union all select pk_calfarecode,voperatorid,'',''  from fdc_pm_calfare union all select contractid,voperatorid, '','' from dbo.fdc_pm_rentcontract ) a1 on a.pk_farecode=a1.pk_farecode " +
                                 " WHERE convert(varchar(20),a.farerowid) in('" + farwarid.Replace(",", "','") + "')   order by a.hcode, nneedpaymny ";
            //根据id查询账单数据
            var model3 = Db.Ado.SqlQuerySingle<dynamic>(sql);
            if (model3 != null)
            {

                fdc_pm_revfareinfo.vcreaterid = model3.voperatorid.ToString();

            }
            string id = "";
            string maxbillcode = "";
            string sgn = "S";
            sgn = psgn == "" ? "S" : psgn;

            var service = new maxbillcodeManager(Db);

            maxbillcode = service.GetMaxBillCode("", sgn, true);
            //收款单编码
            fdc_pm_revfareinfo.pk_revfarecode = maxbillcode;
            revfarecode = maxbillcode;

            if (Db.Queryable<fdc_pm_revfare>().Any(m => m.pk_revfarecode == maxbillcode))    //若存在主表数据，则删除
            {
                Db.Deleteable<fdc_pm_revfare>().In(maxbillcode).ExecuteCommand();
            }

            //再重新插入缴费主表
            Db.Insertable(fdc_pm_revfareinfo).ExecuteCommand();
            //先删除缴费明细数据
            Db.Deleteable<fdc_pm_revfare_b>().Where(it => it.pk_revfarecode == maxbillcode).ExecuteCommand();
            foreach (fdc_pm_revfare_b item in fdc_pm_revfare_blist)
            {
                item.pk_revfarecode = maxbillcode;
                item.revfarerowid = service.GetMaxBillCode("", PublicField.ID, true);
            }
            //重新插入明细数据
            Db.Insertable(fdc_pm_revfare_blist).ExecuteCommand();
            foreach (fdc_pm_revfare_b item in fdc_pm_revfare_blist)
            {
                string strsql = " UPDATE fdc_pm_fare_b SET nrevmny=ISNULL(nrevmny,0)+ISNULL(" + item.nrevmny + ",0),edate='" + item.dedate + "' WHERE farerowid='" + item.farerowid + "'" +
                     "UPDATE fdc_pm_revfare_b SET nrevlatemny=ISNULL(nrevlatemny,0)+ISNULL(" + item.nrevmny + ",0)  WHERE revfarerowid='" + item.prerevfarerowid + "'" +
                     "UPDATE  fdc_pm_vownerbalance SET balance=balance+(" + item.nrevmny.ToString().Trim() + ") WHERE autoid='" + item.vownerbalanceautoid + "' AND '" + item.billtype + "'='S' AND '" + item.prerevflag + "'='J'" +
                     "UPDATE  fdc_pm_vownerbalance SET balance=balance-(" + item.nrevmny.ToString().Trim() + ") WHERE autoid='" + item.vownerbalanceautoid + "' AND '" + item.billtype + "'='C' AND '" + item.prerevflag + "'='J'" +
                     "UPDATE  a set a.vownerbalance=b.balance  FROM fdc_pm_revfare a INNER JOIN dbo.fdc_pm_vownerbalance b on isnull(a.vownerid,'')=isnull(b.vownerid,'')  WHERE a.pk_revfarecode='" + maxbillcode + "'" +
                        " INSERT INTO fdc_pm_vownermny(balanceautoid,mny) SELECT autoid,balance=balance+(" + item.nrevmny.ToString().Trim() + ")  FROM fdc_pm_vownerbalance WHERE autoid='" + item.vownerbalanceautoid + "' AND '" + item.billtype + "'='S' AND '" + item.prerevflag + "'='J' " +
                        " INSERT INTO fdc_pm_vownermny(balanceautoid,mny) SELECT autoid,balance=balance-(" + item.nrevmny.ToString().Trim() + ")  FROM fdc_pm_vownerbalance WHERE autoid='" + item.vownerbalanceautoid + "' AND '" + item.billtype + "'='C' AND '" + item.prerevflag + "'='J' " +
                        " UPDATE fdc_pm_housebillsitemstand SET drevdate='" + item.dedate + "' WHERE hcode='" + item.hcode + "' AND billitemcode='" + item.billitemcode + "' AND case  drevdate when '' then 2001-01-01 else drevdate end >='" + item.dedate + "'";
                logger.Debug($"【InsertPay】sql1:{strsql}");

                Db.Ado.ExecuteCommand(strsql);
            }
            //更新MaxBillCode
            Db.CommitTran();
            return "success";

        }
        catch (Exception ex)
        {
            Db.RollbackTran();
            logger.Error(ex);
            throw;
        }

    }

    /// <summary>
    /// 插入缴费数据
    /// </summary>
    /// <param name="farwarid">欠费信息表fdc_pm_fare_b主键值</param>
    /// <param name="money">欠费金额</param>
    /// <param name="ownername">业主名称</param>
    /// <param name="hname">房屋名称</param>
    /// <returns></returns>
    public string InsertRevfare(string farwarid, string hname, string ownername, string money, string staffcode)
    {

        SetValue(farwarid, money);
        Db.BeginTran();
        try
        {
            var sql = " SELECT  ISNULL(a.nrevmny,0) nrevmny,b.rate,a1.voperatorid,f.psgn,b.bsgn,a1.cmemo,a1.vownername,'' as pk_billno,a.dneeddate,'N' as prerevflag,a.farerowid, a.pk_farecode, a.hcode,d.hname,d.define7,d.define8, a.ownerid, c.ownername,a.pk_meterid, a.vactymbegin, a.vactymend, a.dbegindate, a.denddate,a.dbegindate as dbdate,a.denddate as dedate, a.billitemcode,b.billitemname, a.memo,ISNULL(a.nneedpaymny,0) incomemny, (ISNULL(a.nneedpaymny,0)-ISNULL(a.nrevmny,0)) as  nneedpaymny, a.nremainmny, a.ndebitlatemny, a.ndebitmny, a.nlatefeemny, a.namount, a.ncurreading, a.nlastreading, a.nprice ,a.rentcontractid,a.pk_corp,a.deptcode,a.pk_managementsecid,a.isback,a.edate ,a.narea  " +
                                " FROM fdc_pm_fare_b a " +
                                " INNER JOIN fdc_pm_billsitem b on  a.billitemcode=b.billitemcode" +
                                " LEFT JOIN fdc_pm_owner c on a.ownerid=c.ownerid " +
                                " INNER JOIN fdc_pm_house d on a.hcode=d.hcode " +
                                " INNER JOIN fdc_pm_build e on d.bcode=e.bcode " +
                                " INNER JOIN fdc_pm_project f on e.pcode=f.pcode " +
                                " LEFT JOIN   (select pk_farecode,voperatorid,cmemo,vownername from fdc_pm_fare union all select pk_calfarecode,voperatorid,'',''  from fdc_pm_calfare union all select contractid,voperatorid, '','' from dbo.fdc_pm_rentcontract ) a1 on a.pk_farecode=a1.pk_farecode " +
                                " WHERE convert(varchar(20),a.farerowid) in('" + farwarid.ToString().Replace(",", "','") + "')   order by a.hcode, nneedpaymny ";

            var model = Db.Ado.SqlQuerySingle<dynamic>(sql);

            //收款主表
            fdc_pm_revfare fdc_pm_revfareinfo = new fdc_pm_revfare();
            //判断时候是预收款
            fdc_pm_revfareinfo.prerevflag = "N";
            //收款单编码
            //fdc_pm_revfareinfo.pk_revfarecode = "WXPay10001";
            //票据编码
            fdc_pm_revfareinfo.pk_billno = "";
            //收款日期
            fdc_pm_revfareinfo.dbilldate = DateTime.Now.Date;
            //操作日期
            fdc_pm_revfareinfo.doperatordate = DateTime.Now.Date;
            //房产编码
            fdc_pm_revfareinfo.hcode = model.hcode.ToString();

            //备注
            fdc_pm_revfareinfo.memo = "";
            //业主编码
            fdc_pm_revfareinfo.ownerid = model.ownerid.ToString();

            //业主名称
            fdc_pm_revfareinfo.vownername = model.ownername.ToString();
            if (ownername != model.ownername.ToString())
            {
                Db.RollbackTran();
                return "业主姓名不对";
            }
            if (hname != model.hname.ToString())
            {
                Db.RollbackTran();
                return "房产名称不对";
            }
            fdc_pm_revfareinfo.pk_actym = "";
            //收款单编码
            //fdc_pm_revfareinfo.pk_revfarecode = "WXPay10001";
            //实收金额
            fdc_pm_revfareinfo.revnmny = money.ToDecimal();
            //收款人id
            fdc_pm_revfareinfo.voperatorid = staffcode;
            //实收金额   （重复）
            //fdc_pm_revfareinfo.revnmny = money.ToDecimal();
            //收费类型
            fdc_pm_revfareinfo.paytype = "1001";
            fdc_pm_revfareinfo.billtype = "S";//普通收款单
            fdc_pm_revfareinfo.billstatus = PublicField.APP_ORIGINAL;   //支付状态   0：
            //是否开票
            //fdc_pm_revfareinfo.Vouchflag = "flag";
            //缴费卡id
            fdc_pm_revfareinfo.vownerid = "";


            sql = $"SELECT a.csscode, a.cssname, a.jfcode, a.jfname, a.dfcode, a.dfname, a.cmemo,a.rate,a.ssgn  FROM settletype a  WHERE csscode='{fdc_pm_revfareinfo.paytype}' ";
            var model1 = Db.Ado.SqlQuerySingle<dynamic>(sql);

            //获取结算方式单据标示
            string ssgn = model1.ssgn.ToString();
            var psgn = model.psgn.ToString() + ssgn.Trim();



            /////////////////////////////////////////////////////////////////////////////////////
            decimal summny = 0;
            foreach (fdc_pm_revfare_b fdc_pm_revfare_bitem in fdc_pm_revfare_blist)
            {

                summny += Convert.ToDecimal(fdc_pm_revfare_bitem.nrevmny);

            }

            sql = " SELECT  ISNULL(a.nrevmny,0) nrevmny,b.rate,a1.voperatorid,f.psgn,b.bsgn,a1.cmemo,a1.vownername,'' as pk_billno,a.dneeddate,'N' as prerevflag,a.farerowid, a.pk_farecode, a.hcode,d.hname,d.define7,d.define8, a.ownerid, c.ownername,a.pk_meterid, a.vactymbegin, a.vactymend, a.dbegindate, a.denddate,a.dbegindate as dbdate,a.denddate as dedate, a.billitemcode,b.billitemname, a.memo,ISNULL(a.nneedpaymny,0) incomemny, (ISNULL(a.nneedpaymny,0)-ISNULL(a.nrevmny,0)) as  nneedpaymny, a.nremainmny, a.ndebitlatemny, a.ndebitmny, a.nlatefeemny, a.namount, a.ncurreading, a.nlastreading, a.nprice ,a.rentcontractid,a.pk_corp,a.deptcode,a.pk_managementsecid,a.isback,a.edate ,a.narea  " +
                                 " FROM fdc_pm_fare_b a " +
                                 " INNER JOIN fdc_pm_billsitem b on  a.billitemcode=b.billitemcode" +
                                 " LEFT JOIN fdc_pm_owner c on a.ownerid=c.ownerid " +
                                 " INNER JOIN fdc_pm_house d on a.hcode=d.hcode " +
                                 " INNER JOIN fdc_pm_build e on d.bcode=e.bcode " +
                                 " INNER JOIN fdc_pm_project f on e.pcode=f.pcode " +
                                 " LEFT JOIN   (select pk_farecode,voperatorid,cmemo,vownername from fdc_pm_fare union all select pk_calfarecode,voperatorid,'',''  from fdc_pm_calfare union all select contractid,voperatorid, '','' from dbo.fdc_pm_rentcontract ) a1 on a.pk_farecode=a1.pk_farecode " +
                                 " WHERE convert(varchar(20),a.farerowid) in('" + farwarid.Replace(",", "','") + "')   order by a.hcode, nneedpaymny ";
            //根据id查询账单数据
            var model3 = Db.Ado.SqlQuerySingle<dynamic>(sql);
            if (model3 != null)
            {

                fdc_pm_revfareinfo.vcreaterid = model3.voperatorid.ToString();

            }
            string id = "";
            string maxbillcode = "";
            string sgn = "S";
            sgn = psgn == "" ? "S" : psgn;

            var service = new maxbillcodeManager(Db);

            maxbillcode = service.GetMaxBillCode("", sgn, true);
            //收款单编码
            fdc_pm_revfareinfo.pk_revfarecode = maxbillcode;

            if (Db.Queryable<fdc_pm_revfare>().Any(m => m.pk_revfarecode == maxbillcode))    //若存在主表数据，则删除
            {
                Db.Deleteable<fdc_pm_revfare>().In(maxbillcode).ExecuteCommand();
            }

            //再重新插入缴费主表
            Db.Insertable(fdc_pm_revfareinfo).ExecuteCommand();
            //先删除缴费明细数据
            Db.Deleteable<fdc_pm_revfare_b>().Where(it => it.pk_revfarecode == maxbillcode).ExecuteCommand();
            foreach (fdc_pm_revfare_b item in fdc_pm_revfare_blist)
            {
                item.pk_revfarecode = maxbillcode;
                item.revfarerowid = service.GetMaxBillCode("", PublicField.ID, true);
            }
            //重新插入明细数据
            Db.Insertable(fdc_pm_revfare_blist).ExecuteCommand();
            foreach (fdc_pm_revfare_b item in fdc_pm_revfare_blist)
            {
                string strsql = " UPDATE fdc_pm_fare_b SET nrevmny=ISNULL(nrevmny,0)+ISNULL(" + item.nrevmny + ",0),edate='" + item.dedate + "' WHERE farerowid='" + item.farerowid + "'" +
                     "UPDATE fdc_pm_revfare_b SET nrevlatemny=ISNULL(nrevlatemny,0)+ISNULL(" + item.nrevmny + ",0)  WHERE revfarerowid='" + item.prerevfarerowid + "'" +
                     "UPDATE  fdc_pm_vownerbalance SET balance=balance+(" + item.nrevmny.ToString().Trim() + ") WHERE autoid='" + item.vownerbalanceautoid + "' AND '" + item.billtype + "'='S' AND '" + item.prerevflag + "'='J'" +
                     "UPDATE  fdc_pm_vownerbalance SET balance=balance-(" + item.nrevmny.ToString().Trim() + ") WHERE autoid='" + item.vownerbalanceautoid + "' AND '" + item.billtype + "'='C' AND '" + item.prerevflag + "'='J'" +
                     "UPDATE  a set a.vownerbalance=b.balance  FROM fdc_pm_revfare a INNER JOIN dbo.fdc_pm_vownerbalance b on isnull(a.vownerid,'')=isnull(b.vownerid,'')  WHERE a.pk_revfarecode='" + maxbillcode + "'" +
                        " INSERT INTO fdc_pm_vownermny(balanceautoid,mny) SELECT autoid,balance=balance+(" + item.nrevmny.ToString().Trim() + ")  FROM fdc_pm_vownerbalance WHERE autoid='" + item.vownerbalanceautoid + "' AND '" + item.billtype + "'='S' AND '" + item.prerevflag + "'='J' " +
                        " INSERT INTO fdc_pm_vownermny(balanceautoid,mny) SELECT autoid,balance=balance-(" + item.nrevmny.ToString().Trim() + ")  FROM fdc_pm_vownerbalance WHERE autoid='" + item.vownerbalanceautoid + "' AND '" + item.billtype + "'='C' AND '" + item.prerevflag + "'='J' " +
                        " UPDATE fdc_pm_housebillsitemstand SET drevdate='" + item.dedate + "' WHERE hcode='" + item.hcode + "' AND billitemcode='" + item.billitemcode + "' AND case  drevdate when '' then 2001-01-01 else drevdate end >='" + item.dedate + "'";
                logger.Debug($"【InsertPay】sql1:{strsql}");

                Db.Ado.ExecuteCommand(strsql);
            }
            //更新MaxBillCode
            Db.CommitTran();
            return "success";

        }
        catch (Exception ex)
        {
            Db.RollbackTran();
            logger.Error(ex);
            throw;
        }

    }
    /// <summary>
    /// 插入缴费数据，非事务
    /// </summary>
    /// <param name="farwarid">欠费信息表fdc_pm_fare_b主键值</param>
    /// <param name="money">欠费金额</param>
    /// <param name="ownername">业主名称</param>
    /// <param name="hname">房屋名称</param>
    /// <param name="revfarecode">生成的收款单编号(fdc_pm_revfare)</param>
    /// <returns></returns>
    public string InsertPayWithNoTran(string farwarid, string money, string ownername, string hname, ref string revfarecode)
    {
        SetValue(farwarid, money);

        try
        {
            var sql = " SELECT  ISNULL(a.nrevmny,0) nrevmny,b.rate,a1.voperatorid,f.psgn,b.bsgn,a1.cmemo,a1.vownername,'' as pk_billno,a.dneeddate,'N' as prerevflag,a.farerowid, a.pk_farecode, a.hcode,d.hname,d.define7,d.define8, a.ownerid, c.ownername,a.pk_meterid, a.vactymbegin, a.vactymend, a.dbegindate, a.denddate,a.dbegindate as dbdate,a.denddate as dedate, a.billitemcode,b.billitemname, a.memo,ISNULL(a.nneedpaymny,0) incomemny, (ISNULL(a.nneedpaymny,0)-ISNULL(a.nrevmny,0)) as  nneedpaymny, a.nremainmny, a.ndebitlatemny, a.ndebitmny, a.nlatefeemny, a.namount, a.ncurreading, a.nlastreading, a.nprice ,a.rentcontractid,a.pk_corp,a.deptcode,a.pk_managementsecid,a.isback,a.edate ,a.narea  " +
                                " FROM fdc_pm_fare_b a " +
                                " INNER JOIN fdc_pm_billsitem b on  a.billitemcode=b.billitemcode" +
                                " LEFT JOIN fdc_pm_owner c on a.ownerid=c.ownerid " +
                                " INNER JOIN fdc_pm_house d on a.hcode=d.hcode " +
                                " INNER JOIN fdc_pm_build e on d.bcode=e.bcode " +
                                " INNER JOIN fdc_pm_project f on e.pcode=f.pcode " +
                                " LEFT JOIN   (select pk_farecode,voperatorid,cmemo,vownername from fdc_pm_fare union all select pk_calfarecode,voperatorid,'',''  from fdc_pm_calfare union all select contractid,voperatorid, '','' from dbo.fdc_pm_rentcontract ) a1 on a.pk_farecode=a1.pk_farecode " +
                                " WHERE convert(varchar(20),a.farerowid) in('" + farwarid.ToString().Replace(",", "','") + "')   order by a.hcode, nneedpaymny ";

            var model = Db.Ado.SqlQuerySingle<dynamic>(sql);

            //收款主表
            fdc_pm_revfare fdc_pm_revfareinfo = new fdc_pm_revfare();
            //判断时候是预收款
            fdc_pm_revfareinfo.prerevflag = "N";
            //收款单编码
            //fdc_pm_revfareinfo.pk_revfarecode = "WXPay10001";
            //票据编码
            fdc_pm_revfareinfo.pk_billno = "";
            //收款日期
            fdc_pm_revfareinfo.dbilldate = DateTime.Now.Date;
            //操作日期
            fdc_pm_revfareinfo.doperatordate = DateTime.Now.Date;
            //房产编码
            fdc_pm_revfareinfo.hcode = model.hcode.ToString();

            //备注
            fdc_pm_revfareinfo.memo = "";
            //业主编码
            fdc_pm_revfareinfo.ownerid = model.ownerid.ToString();

            //业主名称
            fdc_pm_revfareinfo.vownername = model.ownername.ToString();
            if (ownername != model.ownername.ToString())
            {

                return "业主姓名不对";
            }
            if (hname != model.hanme.ToString())
            {

                return "房产名称不对";
            }
            fdc_pm_revfareinfo.pk_actym = "";
            //收款单编码
            //fdc_pm_revfareinfo.pk_revfarecode = "WXPay10001";
            //实收金额
            fdc_pm_revfareinfo.revnmny = money.ToDecimal();
            //收款人id
            fdc_pm_revfareinfo.voperatorid = "1121";
            //实收金额   （重复）
            //fdc_pm_revfareinfo.revnmny = money.ToDecimal();
            //收费类型
            fdc_pm_revfareinfo.paytype = "1001";
            fdc_pm_revfareinfo.billtype = "S";//普通收款单
            fdc_pm_revfareinfo.billstatus = PublicField.APP_ORIGINAL;   //支付状态   0：
            //是否开票
            //fdc_pm_revfareinfo.Vouchflag = "flag";
            //缴费卡id
            fdc_pm_revfareinfo.vownerid = "";


            sql = $"SELECT a.csscode, a.cssname, a.jfcode, a.jfname, a.dfcode, a.dfname, a.cmemo,a.rate,a.ssgn  FROM settletype a  WHERE csscode='{fdc_pm_revfareinfo.paytype}' ";
            var model1 = Db.Ado.SqlQuerySingle<dynamic>(sql);

            //获取结算方式单据标示
            string ssgn = model1.ssgn.ToString();
            var psgn = model.psgn.ToString() + ssgn.Trim();



            /////////////////////////////////////////////////////////////////////////////////////
            decimal summny = 0;
            foreach (fdc_pm_revfare_b fdc_pm_revfare_bitem in fdc_pm_revfare_blist)
            {

                summny += Convert.ToDecimal(fdc_pm_revfare_bitem.nrevmny);

            }

            sql = " SELECT  ISNULL(a.nrevmny,0) nrevmny,b.rate,a1.voperatorid,f.psgn,b.bsgn,a.memo,a1.cmemo,a1.vownername,'' as pk_billno,a.dneeddate,'N' as prerevflag,a.farerowid, a.pk_farecode, a.hcode,d.hname,d.define7,d.define8, a.ownerid, c.ownername,a.pk_meterid, a.vactymbegin, a.vactymend, a.dbegindate, a.denddate,a.dbegindate as dbdate,a.denddate as dedate, a.billitemcode,b.billitemname, a.memo,ISNULL(a.nneedpaymny,0) incomemny, (ISNULL(a.nneedpaymny,0)-ISNULL(a.nrevmny,0)) as  nneedpaymny, a.nremainmny, a.ndebitlatemny, a.ndebitmny, a.nlatefeemny, a.namount, a.ncurreading, a.nlastreading, a.nprice ,a.rentcontractid,a.pk_corp,a.deptcode,a.pk_managementsecid,a.isback,a.edate ,a.narea  " +
                                 " FROM fdc_pm_fare_b a " +
                                 " INNER JOIN fdc_pm_billsitem b on  a.billitemcode=b.billitemcode" +
                                 " LEFT JOIN fdc_pm_owner c on a.ownerid=c.ownerid " +
                                 " INNER JOIN fdc_pm_house d on a.hcode=d.hcode " +
                                 " INNER JOIN fdc_pm_build e on d.bcode=e.bcode " +
                                 " INNER JOIN fdc_pm_project f on e.pcode=f.pcode " +
                                 " LEFT JOIN   (select pk_farecode,voperatorid,cmemo,vownername from fdc_pm_fare union all select pk_calfarecode,voperatorid,'',''  from fdc_pm_calfare union all select contractid,voperatorid, '','' from dbo.fdc_pm_rentcontract ) a1 on a.pk_farecode=a1.pk_farecode " +
                                 " WHERE convert(varchar(20),a.farerowid) in('" + farwarid.Replace(",", "','") + "')   order by a.hcode, nneedpaymny ";
            //根据id查询账单数据
            var model3 = Db.Ado.SqlQuerySingle<dynamic>(sql);
            if (model3 != null)
            {

                fdc_pm_revfareinfo.vcreaterid = model3.voperatorid.ToString();

            }
            string id = "";
            string maxbillcode = "";
            string sgn = "S";
            sgn = psgn == "" ? "S" : psgn;

            var service = new maxbillcodeManager(Db);

            maxbillcode = service.GetMaxBillCode("", sgn, false);
            //收款单编码
            fdc_pm_revfareinfo.pk_revfarecode = maxbillcode;
            revfarecode = maxbillcode;

            if (Db.Queryable<fdc_pm_revfare>().Any(m => m.pk_revfarecode == maxbillcode))    //若存在主表数据，则删除
            {
                Db.Deleteable<fdc_pm_revfare>().In(maxbillcode).ExecuteCommand();
            }

            //再重新插入缴费主表
            Db.Insertable(fdc_pm_revfareinfo).ExecuteCommand();
            //先删除缴费明细数据
            Db.Deleteable<fdc_pm_revfare_b>().Where(it => it.pk_revfarecode == maxbillcode).ExecuteCommand();
            foreach (fdc_pm_revfare_b item in fdc_pm_revfare_blist)
            {
                item.revfarerowid = service.GetMaxBillCode("", PublicField.ID, false);
            }
            //重新插入明细数据
            Db.Insertable(fdc_pm_revfare_blist).ExecuteCommand();

            return "success";

        }
        catch (Exception ex)
        {

            logger.Error(ex);
            throw;
        }

    }

    /// <summary>
    /// 收费详细列表
    /// </summary>
    private List<fdc_pm_revfare_b> fdc_pm_revfare_blist = new List<fdc_pm_revfare_b>();
    private void SetValue(string id, string money)
    {
        String flag = "new";
        fdc_pm_revfare_b fdc_pm_revfare_binfo;

        String sql = "  SELECT  ISNULL(a.nrevmny,0) nrevmny,b.rate,a1.voperatorid,f.psgn,b.bsgn,a1.cmemo,a1.vownername,'' as pk_billno,a.dneeddate,'N' as prerevflag,a.farerowid, a.pk_farecode, a.hcode,d.hname,d.define7,d.define8, a.ownerid, c.ownername,a.pk_meterid, a.vactymbegin, a.vactymend, a.dbegindate, a.denddate,a.dbegindate as dbdate,a.denddate as dedate, a.billitemcode,b.billitemname, a.memo,ISNULL(a.nneedpaymny,0) incomemny, (ISNULL(a.nneedpaymny,0)-ISNULL(a.nrevmny,0)) as  nneedpaymny, ISNULL(a.nremainmny,0) nremainmny, ISNULL(a.ndebitlatemny,0) ndebitlatemny, a.ndebitmny, a.nlatefeemny, ISNULL(a.namount,0) namount, a.ncurreading, a.nlastreading, a.nprice ,a.rentcontractid,a.pk_corp,a.deptcode,a.pk_managementsecid,a.isback,a.edate ,a.narea  " +
                            " FROM fdc_pm_fare_b a " +
                            " INNER JOIN fdc_pm_billsitem b on  a.billitemcode=b.billitemcode" +
                            " LEFT JOIN fdc_pm_owner c on a.ownerid=c.ownerid " +
                            " INNER JOIN fdc_pm_house d on a.hcode=d.hcode " +
                            " INNER JOIN fdc_pm_build e on d.bcode=e.bcode " +
                            " INNER JOIN fdc_pm_project f on e.pcode=f.pcode " +
                            " LEFT JOIN   (select pk_farecode,voperatorid,cmemo,vownername from fdc_pm_fare union all select pk_calfarecode,voperatorid,'',''  from fdc_pm_calfare union all select contractid,voperatorid, '','' from dbo.fdc_pm_rentcontract ) a1 on a.pk_farecode=a1.pk_farecode " +
                            " WHERE convert(varchar(20),a.farerowid) in('" + id.Replace(",", "','") + "')";
        DataTable dsi = new DataTable();
        if (flag == "new")
        {
            dsi = Db.Ado.GetDataTable(sql);

        }
        for (int i = 0; i < dsi.Rows.Count; i++)
        {

            fdc_pm_revfare_binfo = new fdc_pm_revfare_b();
            fdc_pm_revfare_binfo.billitemcode = dsi.Rows[i]["billitemcode"].ToString();
            //fdc_pm_revfare_binfo.billitemname = dsi.Rows[i]["billitemname"].ToString();
            fdc_pm_revfare_binfo.hcode = dsi.Rows[i]["hcode"].ToString();
            //fdc_pm_revfare_binfo.hname = dsi.Rows[i]["hname"].ToString();
            //fdc_pm_revfare_binfo.rate = dsi.Rows[i]["rate"].ToString();




            //如果收款则如果是修改则
            if (flag == "new")
            {
                fdc_pm_revfare_binfo.nneedmny = dsi.Rows[i]["nneedpaymny"].ToDecimal();
                fdc_pm_revfare_binfo.nrevmny = money.ToDecimal();
                if (dsi.Rows[i]["rate"].ToString() != "")
                {
                    var rate = dsi.Rows[i]["rate"].ToString();
                    fdc_pm_revfare_binfo.incomemny = Math.Round(Convert.ToDecimal(fdc_pm_revfare_binfo.nrevmny.ToString()) * Convert.ToDecimal("100") / (Convert.ToDecimal("100") + Convert.ToDecimal(rate)), 2, MidpointRounding.AwayFromZero).ToString();
                    fdc_pm_revfare_binfo.ratemny = Math.Round(Convert.ToDecimal(fdc_pm_revfare_binfo.nrevmny.ToString()) * Convert.ToDecimal(rate) / (Convert.ToDecimal("100") + Convert.ToDecimal(rate)), 2, MidpointRounding.AwayFromZero).ToString();

                }

                fdc_pm_revfare_binfo.prerevflag = dsi.Rows[i]["Prerevflag"].ToString();
                fdc_pm_revfare_binfo.nprice = dsi.Rows[i]["Nprice"].ToString();
                fdc_pm_revfare_binfo.memo = dsi.Rows[i]["memo"].ToString();

            }
            else
            {

                fdc_pm_revfare_binfo.nneedmny = dsi.Rows[i]["Needmny"].ToDecimal();
                fdc_pm_revfare_binfo.nrevmny = dsi.Rows[i]["Nrevmny"].ToDecimal();
                if (dsi.Rows[i]["rate"].ToString() != "")
                {
                    var rate = dsi.Rows[i]["rate"].ToString();
                    fdc_pm_revfare_binfo.incomemny = Math.Round(Convert.ToDecimal(fdc_pm_revfare_binfo.nrevmny.ToString()) * Convert.ToDecimal("100") / (Convert.ToDecimal("100") + Convert.ToDecimal(rate)), 2, MidpointRounding.AwayFromZero).ToString();
                    fdc_pm_revfare_binfo.ratemny = Math.Round(Convert.ToDecimal(fdc_pm_revfare_binfo.nrevmny.ToString()) * Convert.ToDecimal(rate) / (Convert.ToDecimal("100") + Convert.ToDecimal(rate)), 2, MidpointRounding.AwayFromZero).ToString();

                }
                fdc_pm_revfare_binfo.prerevflag = dsi.Rows[i]["Prerevflag_b"].ToString();

                fdc_pm_revfare_binfo.revfarerowid = dsi.Rows[i]["Revfarerowid"].ToString();
                fdc_pm_revfare_binfo.pk_revfarecode = dsi.Rows[i]["Pk_revfarecode"].ToString();
            }


            fdc_pm_revfare_binfo.dbdate = dsi.Rows[i]["edate"].ToString() == "" ? Convert.ToDateTime(dsi.Rows[i]["dbdate"].ToString()) : Convert.ToDateTime(dsi.Rows[i]["edate"].ToString()).AddDays(1);
            fdc_pm_revfare_binfo.dedate = Convert.ToDateTime(dsi.Rows[i]["dedate"].ToString());
            //fdc_pm_revfare_binfo.dneeddate = dsi.Rows[i]["edate"].ToString() == "" ? Convert.ToDateTime(dsi.Rows[i]["dneeddate"].ToString()).ToShortDateString() : Convert.ToDateTime(dsi.Rows[i]["edate"].ToString()).AddDays(1).ToShortDateString();

            // fdc_pm_revfare_binfo.Pk_revfarecode = this.txtpk_revfarecode.Text.ToString();

            fdc_pm_revfare_binfo.billtype = "S";
            fdc_pm_revfare_binfo.narea = dsi.Rows[i]["narea"].ToString();
            fdc_pm_revfare_binfo.nrevlatemny = 0;
            fdc_pm_revfare_binfo.nlatemny = 0;
            fdc_pm_revfare_binfo.farerowid = dsi.Rows[i]["Farerowid"].ToString();
            fdc_pm_revfare_binfo.issettle = "0";
            fdc_pm_revfare_binfo.isvouch = "0";
            fdc_pm_revfare_binfo.voucherid = "";
            fdc_pm_revfare_blist.Add(fdc_pm_revfare_binfo);
        }


        //hddfdc_pm_revfare_b = JSON.Serialize(fdc_pm_revfare_blist);//序列号
    }
    /// <summary>
    /// 获取报修详情
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public dynamic GetRepairDetail(string code)
    {
        var sql = $@"SELECT e.username as voperatorname,f.username as vemployeename,d.deptname,a.*
              FROM fdc_pm_repairapply a 
                            INNER JOIN fdc_pm_house b on a.hcode=b.hcode 
                             INNER JOIN fdc_pm_owner c on a.ownerid=c.ownerid 
                            INNER JOIN  depts d on a.deptcode=d.deptcode 
                           LEFT JOIN users e on a.voperatorid=e.staffcode
                            LEFT JOIN users f on a.vemployeeid=f.staffcode
                           WHERE a.vrepairapplycode='{code}' ";
        //logger.Info($"sql:{sql}");

        var model = Db.SqlQueryable<dynamic>(sql).First();
        sql = $"SELECT * FROM Fdc_pm_file where code='{code}'";
        var list1 = new List<TheUrl>();
        var list = Db.Ado.SqlQuery<fdc_pm_file>(sql);
        foreach (var item in list)
        {
            var theurl = new TheUrl();
            theurl.url = $"uploadimg/{item.fileurl}";
            list1.Add(theurl);
        }
        model.fileList = list1;
        return model;
    }
}
