using Sugar.Enties.DbModels;
using Sugar.Enties.DTO;
using System;
using System.Collections.Generic;
using System.Text;

namespace PMS.NetCore.BusinessCore.BaseCore
{
    public class fdc_pm_inspection_routeManager : DbContext<fdc_pm_inspection_route>
    {

        private NLog.Logger logger;
        public fdc_pm_inspection_routeManager()
        {
            logger = NLog.LogManager.GetCurrentClassLogger();
        }

        /// <summary>
        /// 删除线路
        /// </summary>
        /// <param name="routeCode"></param>
        /// <returns></returns>
        public string RouteDelete(int routeCode)
        {

            Db.BeginTran();
            try
            {                

                //更新线路主表
                CurrentDb.Update(it => new fdc_pm_inspection_route() { isdel = true,createtime =DateTime.Now }, it => it.routecode == routeCode);
               
                //更新线路子表
                var  routedetailManager = new fdc_pm_inspection_routedetailManager();

                routedetailManager.CurrentDb.Update(it => new fdc_pm_inspection_routedetail() { isdel = true,createtime = DateTime.Now }, it => it.routecode == routeCode);


                Db.CommitTran();
                return "success";

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


        /// <summary>
        /// 获取巡更路线详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<RouteDetail> GetRouteDetail(InspectionDTO input)
        {
            var sql=@$"select TOP 100 PERCENT a.pointcode,b.sort,c.routecode,c.routename,f.pcode, f.pname, d.bname,e.hname from fdc_pm_inspection_point a 
                    left join fdc_pm_inspection_routedetail b on b.pointcode = a.pointcode
                    left join fdc_pm_inspection_route c on c.routecode = b.routecode
                    left join fdc_pm_build d on d.pcode = a.pcode and d.bcode = a.bcode
                    left join fdc_pm_house e on e.hcode = a.hcode
                    left join fdc_pm_project f on f.pcode = a.pcode
                    where a.isdel = 0 and b.isdel = 0 and c.isdel = 0
                    and c.routecode = '{input.routeCode}'
                    order by b.sort asc";

            var detailList = Db.SqlQueryable<RouteDetail>(sql).ToList();


            return detailList;
        }
    }
}
