using DocumentFormat.OpenXml.Bibliography;
using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Math;
using DocumentFormat.OpenXml.Spreadsheet;
using Infrastructure;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Moq;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.ServiceApp.BiddingInfoManager.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using Quartz.Impl.Triggers;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;

namespace OpenAuth.App.ServiceApp.BiddingInfoManager
{
    public class BiddingInfoApp:SqlSugarBaseApp<PpBiddinginfo, SugarDbContext>
    {
        private ISqlSugarClient client;

        #region 构造函数
        public BiddingInfoApp(
            ISugarUnitOfWork<SugarDbContext> unitWork,
            ISimpleClient<PpBiddinginfo> repository,
            IAuth auth,
            ISqlSugarClient sqlSugarClient
            ) : base(unitWork, repository, auth)
        {
            this.client = sqlSugarClient;
        }
        #endregion

        #region 数据查询
        /// <summary>
        /// 分页查询招标信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response<PageInfo<List<dynamic>>>> LoadBiddingInfo(BiddingInfoReq req)
        {
            var user = _auth.GetCurrentUser();

            RefAsync<int> totalNumber = 0;
            List<string> status = new List<string>();
            if (!string.IsNullOrEmpty(req.nowstatus))
            {
                status = req.nowstatus.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            List<dynamic> table = new List<dynamic>();

            if (user != null && user.Roles[0].Name.Contains("甲方公司"))
            {
                //查询当前账号关联的公司
                var companyids = base.Repository.ChangeRepository<SugarRepositiry<PpCompanyaccount>>().AsQueryable().Where(r => r.UserId == user.User.Id.ToString())?.Select(r => r.CompanyId).ToList();

                //根据账号关联的公司，查询招标公司发布的招标信息
                table = await base.Repository.AsQueryable()
                   .Where(r=>companyids.Contains(r.BiddingCompanyId))
                .WhereIF(!string.IsNullOrEmpty(req.nowstatus), r => status.Contains(r.NowStatus))
                .WhereIF(!string.IsNullOrEmpty(req.projectname), r => r.ProjectName.Contains(req.projectname))
                .WhereIF(!string.IsNullOrEmpty(req.projectnum), r => r.ProjectNum.Contains(req.projectnum))
                .WhereIF(!string.IsNullOrEmpty(req.biddingtype), r => r.BiddingType == req.biddingtype)
                .WhereIF(!string.IsNullOrEmpty(req.biddingform), r => r.BiddingForm == req.biddingform)
                .WhereIF(req.year != null && req.year != 0, r => r.CreateTime != null && r.CreateTime.Value.Year == req.year)
                .LeftJoin<PpBiddingagency>((r, a) => r.BidAgent == a.Id)
                .OrderByDescending((r, a) => r.CreateTime)
                .Select<dynamic>((r, a) => new
                {
                    id = r.Id,
                    projectName = r.ProjectName,
                    projectNum = r.ProjectNum,
                    biddingType = r.BiddingType,
                    biddingAmount = SqlFunc.Round(r.BiddingAmount, 2),
                    biddingForm = r.BiddingForm,
                    procuringEntity = r.ProcuringEntity,
                    registrationDeadline = r.RegistrationDeadline,
                    bidOpeningTime = r.BidOpeningTime,
                    nowStatus = r.NowStatus,
                    bidWinningAmount = SqlFunc.Round(r.BidWinningAmount, 2),
                    bidAgent = r.BidAgent,
                    createTime = r.CreateTime,
                    createUser = r.CreateUser,
                    remark = r.Remark,
                    winningCompanyId = r.WinningCompanyId,
                    name = a.Name,
                    overtimeflag = r.BidOpeningTime == null ? false : (((DateTime)r.BidOpeningTime).AddDays(2) < DateTime.Now ? true : false)
                })
                .ToPageListAsync(req.page, req.limit, totalNumber);
            }
            //招标代理
            if (user != null && user.Roles[0].Name.Contains("招标代理"))
            {
                //查询当前账号关联的公司
                var companyids = base.Repository.ChangeRepository<SugarRepositiry<PpAgencyaccount>>().AsQueryable().Where(r => r.UserId == user.User.Id.ToString())?.Select(r => r.AgencyId).ToList();

                table = await base.Repository.AsQueryable()
                    .Where(r => companyids.Contains(r.BidAgent))
                    .WhereIF(!string.IsNullOrEmpty(req.nowstatus), r => status.Contains(r.NowStatus))
                    .WhereIF(!string.IsNullOrEmpty(req.projectname), r => r.ProjectName.Contains(req.projectname))
                    .WhereIF(!string.IsNullOrEmpty(req.projectnum), r => r.ProjectNum.Contains(req.projectnum))
                    .WhereIF(!string.IsNullOrEmpty(req.biddingtype), r => r.BiddingType == req.biddingtype)
                    .WhereIF(!string.IsNullOrEmpty(req.biddingform), r => r.BiddingForm == req.biddingform)
                    .WhereIF(req.year != null && req.year != 0, r => r.CreateTime != null && r.CreateTime.Value.Year == req.year)
                    .LeftJoin<PpBiddingagency>((r, a) => r.BidAgent == a.Id)
                    .OrderByDescending((r, a) => r.CreateTime)
                    .Select<dynamic>((r, a) => new
                    {
                        id = r.Id,
                        projectName = r.ProjectName,
                        projectNum = r.ProjectNum,
                        biddingType = r.BiddingType,
                        biddingAmount = SqlFunc.Round(r.BiddingAmount, 2),
                        biddingForm = r.BiddingForm,
                        procuringEntity = r.ProcuringEntity,
                        registrationDeadline = r.RegistrationDeadline,
                        bidOpeningTime = r.BidOpeningTime,
                        nowStatus = r.NowStatus,
                        bidWinningAmount = SqlFunc.Round(r.BidWinningAmount, 2),
                        bidAgent = r.BidAgent,
                        createTime = r.CreateTime,
                        createUser = r.CreateUser,
                        remark = r.Remark,
                        winningCompanyId = r.WinningCompanyId,
                        name = a.Name,
                        overtimeflag = r.BidOpeningTime == null ? false : (((DateTime)r.BidOpeningTime).AddDays(2) < DateTime.Now ? true : false)
                    })
                    .ToPageListAsync(req.page, req.limit, totalNumber);
            }

            //供应商
            if (user != null && user.Roles[0].Name.Contains("供应商"))
            {
                //查询当前账号关联的公司
                var companyids = base.Repository.ChangeRepository<SugarRepositiry<PpCompanyaccount>>().AsQueryable().Where(r => r.UserId == user.User.Id.ToString())?.Select(r => r.CompanyId).ToList();

                //查询供应商服务的招标公司
                var biddingcomid = base.Repository.ChangeRepository<SugarRepositiry<PpBiddingsupplier>>().AsQueryable().Where(r => companyids.Contains(r.SupplierId))?.Select(r => r.BiddingCompanyId).ToList();

                table = await base.Repository.AsQueryable()
                    .Where(r => biddingcomid.Contains(r.BiddingCompanyId)&&r.IsReview==true)
                    .WhereIF(!string.IsNullOrEmpty(req.nowstatus), r => status.Contains(r.NowStatus))
                    .WhereIF(!string.IsNullOrEmpty(req.projectname), r => r.ProjectName.Contains(req.projectname))
                    .WhereIF(!string.IsNullOrEmpty(req.projectnum), r => r.ProjectNum.Contains(req.projectnum))
                    .WhereIF(!string.IsNullOrEmpty(req.biddingtype), r => r.BiddingType == req.biddingtype)
                    .WhereIF(!string.IsNullOrEmpty(req.biddingform), r => r.BiddingForm == req.biddingform)
                    .WhereIF(req.year != null && req.year != 0, r => r.CreateTime != null && r.CreateTime.Value.Year == req.year)
                    .LeftJoin<PpBiddingagency>((r, a) => r.BidAgent == a.Id)
                    .OrderByDescending((r, a) => r.CreateTime)
                    .Select<dynamic>((r, a) => new
                    {
                        id = r.Id,
                        projectName = r.ProjectName,
                        projectNum = r.ProjectNum,
                        biddingType = r.BiddingType,
                        biddingAmount = SqlFunc.Round(r.BiddingAmount, 2),
                        biddingForm = r.BiddingForm,
                        procuringEntity = r.ProcuringEntity,
                        registrationDeadline = r.RegistrationDeadline,
                        bidOpeningTime = r.BidOpeningTime,
                        nowStatus = r.NowStatus,
                        bidWinningAmount = SqlFunc.Round(r.BidWinningAmount, 2),
                        bidAgent = r.BidAgent,
                        createTime = r.CreateTime,
                        createUser = r.CreateUser,
                        remark = r.Remark,
                        winningCompanyId = r.WinningCompanyId,
                        name = a.Name,
                        overtimeflag = r.BidOpeningTime == null ? false : (((DateTime)r.BidOpeningTime).AddDays(2) < DateTime.Now ? true : false)
                    })
                    .ToPageListAsync(req.page, req.limit, totalNumber);
            }

            //超级管理员
            if (user != null && user.User.Id == -1)
            {
                table = await base.Repository.AsQueryable()
                    .WhereIF(!string.IsNullOrEmpty(req.nowstatus), r => status.Contains(r.NowStatus))
                    .WhereIF(!string.IsNullOrEmpty(req.projectname), r => r.ProjectName.Contains(req.projectname))
                    .WhereIF(!string.IsNullOrEmpty(req.projectnum), r => r.ProjectNum.Contains(req.projectnum))
                    .WhereIF(!string.IsNullOrEmpty(req.biddingtype), r => r.BiddingType == req.biddingtype)
                    .WhereIF(!string.IsNullOrEmpty(req.biddingform), r => r.BiddingForm == req.biddingform)
                    .WhereIF(req.year != null && req.year != 0, r => r.CreateTime != null && r.CreateTime.Value.Year == req.year)
                    .LeftJoin<PpBiddingagency>((r, a) => r.BidAgent == a.Id)
                    .OrderByDescending((r, a) => r.CreateTime)
                    .Select<dynamic>((r, a) => new
                    {
                        id = r.Id,
                        projectName = r.ProjectName,
                        projectNum = r.ProjectNum,
                        biddingType = r.BiddingType,
                        biddingAmount = SqlFunc.Round(r.BiddingAmount, 2),
                        biddingForm = r.BiddingForm,
                        procuringEntity = r.ProcuringEntity,
                        registrationDeadline = r.RegistrationDeadline,
                        bidOpeningTime = r.BidOpeningTime,
                        nowStatus = r.NowStatus,
                        bidWinningAmount = SqlFunc.Round(r.BidWinningAmount, 2),
                        bidAgent = r.BidAgent,
                        createTime = r.CreateTime,
                        createUser = r.CreateUser,
                        remark = r.Remark,
                        winningCompanyId = r.WinningCompanyId,
                        name = a.Name,
                        overtimeflag = r.BidOpeningTime == null ? false : (((DateTime)r.BidOpeningTime).AddDays(2) < DateTime.Now ? true : false)
                    })
                    .ToPageListAsync(req.page, req.limit, totalNumber);
            }
            return new Response<PageInfo<List<dynamic>>>()
            {
                Result = new PageInfo<List<dynamic>>
                {
                    Items = table,
                    Total = totalNumber
                }
            };
        }

        /// <summary>
        /// 根据id查询单个招标信息--编辑信息加载使用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Response<PpBiddinginfo>> LoadBiddingInfoById(string id)
        {
            var data = await base.Repository.AsQueryable().Where(r=>r.Id == id).FirstAsync();

            return new Response<PpBiddinginfo>()
            {
                Result = data
            };
        }
        /// <summary>
        /// 添加招标信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<Response<bool>> AddBiddingInfo(PpBiddinginfo info)
        {
            using (var db = UnitWork.CreateContext())
            {
                if (db.PpBiddinginfo.GetFirst(r => r.ProjectNum == info.ProjectNum) != null)
                {
                    return new Response<bool> { Result = false, Message = "项目编号已存在" };
                }
                else
                {
                    var user = _auth.GetCurrentUser().User;
                    info.Id=Guid.NewGuid().ToString();  
                    info.CreateTime=DateTime.Now;
                    info.CreateUser=user.Id.ToString();
                    var flag = await db.PpBiddinginfo.InsertAsync(info);
                    var flags = db.Commit();
                    if (flag && flags)
                    {
                        return new Response<bool> { Result = true, Message = "添加成功，"+info.Id };
                    }
                    else
                    {
                        return new Response<bool> { Result = false, Message = "添加失败" };
                    }
                }
            }
        }

        /// <summary>
        /// 编辑招标信息
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public async Task<Response<bool>> EditBiddingInfo(PpBiddinginfo info)
        {
            using (var db = UnitWork.CreateContext())
            {
                if (db.PpBiddinginfo.GetFirst(r => r.ProjectNum == info.ProjectNum && r.Id!=info.Id) != null)
                {
                    return new Response<bool> { Result = false, Message = "项目编号已存在" };
                }
                else
                {
                    var flag = await db.PpBiddinginfo.UpdateAsync(it => new PpBiddinginfo()
                    {
                        ProjectName = info.ProjectName,
                        ProjectNum = info.ProjectNum,
                        BiddingType = info.BiddingType,
                        BiddingForm = info.BiddingForm,
                        BiddingAmount = info.BiddingAmount,
                        ProcuringEntity = info.ProcuringEntity,
                        BidOpeningTime = info.BidOpeningTime,
                        RegistrationDeadline = info.RegistrationDeadline,
                        NowStatus = info.NowStatus,
                        BidWinningAmount = info.BidWinningAmount,
                        BidAgent = info.BidAgent,
                        Remark = info.Remark
                    }, it => it.Id == info.Id);

                    var flags = db.Commit();
                    if (flag && flags)
                    {
                        return new Response<bool> { Result = true, Message = "编辑成功" };
                    }
                    else
                    {
                        return new Response<bool> { Result = false, Message = "编辑失败" };
                    }
                }                
            }
        }

        /// <summary>
        /// 删除招标信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeleteBiddingInfo(string id)
        {
            using (var db = UnitWork.CreateContext())
            {
                var flag=await db.PpBiddinginfo.DeleteAsync(r=>r.Id==id);
                if (db.Commit() && flag)
                {
                    return new Response<bool> { Result = true, Message = "删除成功" };
                }
                else
                {
                    return new Response<bool> { Result = false, Message = "删除失败" };
                }                
            }
        }
        #endregion

        #region  招标代理
        /// <summary>
        /// 查询招标信息--代理公司的查询，只看自己代理的招标项目
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response<PageInfo<List<dynamic>>>> LoadAgencyBiddingInfo(BiddingInfoReq req)
        {
            var user=_auth.GetCurrentUser();
            //查询当前账号关联的公司
            if (user != null)
            {
                var acc = await base.Repository.ChangeRepository<SugarRepositiry<PpAgencyaccount>>().AsQueryable()
                    .Where(r => r.UserId == user.User.Id.ToString()).FirstAsync();
                if (acc != null)
                {
                    RefAsync<int> totalNumber = 0;
                    List<string> status = new List<string>();
                    if (!string.IsNullOrEmpty(req.nowstatus))
                    {
                        status = req.nowstatus.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
                    }
                    var table = await base.Repository.AsQueryable()
                        .Where(r=>r.BidAgent==acc.AgencyId)
                        .WhereIF(!string.IsNullOrEmpty(req.nowstatus), r => status.Contains(r.NowStatus))
                        .WhereIF(!string.IsNullOrEmpty(req.projectname), r => r.ProjectName.Contains(req.projectname))
                        .WhereIF(!string.IsNullOrEmpty(req.projectnum), r => r.ProjectNum.Contains(req.projectnum))
                        .WhereIF(!string.IsNullOrEmpty(req.biddingtype), r => r.BiddingType == req.biddingtype)
                        .WhereIF(!string.IsNullOrEmpty(req.biddingform), r => r.BiddingForm == req.biddingform)
                        .WhereIF(req.year != null && req.year != 0, r => r.CreateTime != null && r.CreateTime.Value.Year == req.year)
                        .LeftJoin<PpBiddingagency>((r, a) => r.BidAgent == a.Id)
                        .OrderByDescending((r, a) => r.CreateTime)
                        .Select<dynamic>((r, a) => new
                        {
                            id = r.Id,
                            projectName = r.ProjectName,
                            projectNum = r.ProjectNum,
                            biddingType = r.BiddingType,
                            biddingAmount = SqlFunc.Round(r.BiddingAmount, 2),
                            biddingForm = r.BiddingForm,
                            procuringEntity = r.ProcuringEntity,
                            registrationDeadline = r.RegistrationDeadline,
                            bidOpeningTime = r.BidOpeningTime,
                            nowStatus = r.NowStatus,
                            bidWinningAmount = SqlFunc.Round(r.BidWinningAmount, 2),
                            bidAgent = r.BidAgent,
                            createTime = r.CreateTime,
                            createUser = r.CreateUser,
                            remark = r.Remark,
                            winningCompanyId = r.WinningCompanyId,
                            name = a.Name,
                            overtimeflag = r.BidOpeningTime == null ? false : (((DateTime)r.BidOpeningTime).AddDays(2) < DateTime.Now ? true : false)
                        })
                        .ToPageListAsync(req.page, req.limit, totalNumber);

                    return new Response<PageInfo<List<dynamic>>>()
                    {
                        Result = new PageInfo<List<dynamic>>
                        {
                            Items = table,
                            Total = totalNumber
                        }
                    };
                }
                else
                {
                    return new Response<PageInfo<List<dynamic>>>()
                    {
                        Result = new PageInfo<List<dynamic>>
                        {
                            Items = null,
                            Total = 0
                        }
                    };
                }
            }
            else
            {
                throw new Exception("请登录");
            }
        }
        #endregion

        #region 供应商企业
        /// <summary>
        /// 查询招标信息--代理商公司的查询，只看自己报名的招标项目
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response<PageInfo<List<dynamic>>>> LoadCompanyBiddingInfo(BiddingInfoReq req)
        {
            var user = _auth.GetCurrentUser();
            //查询当前账号关联的公司
            if (user != null)
            {
                var acc = await base.Repository.ChangeRepository<SugarRepositiry<PpCompanyaccount>>().AsQueryable()
                    .Where(r => r.UserId == user.User.Id.ToString()).FirstAsync();
                if (acc != null)
                {
                    ////查询企业信息
                    //var cominfo=base.Repository.ChangeRepository<SugarRepositiry<PpCompanyinfo>>().AsQueryable().Where(r=>r.Id==acc.CompanyId).First();

                    //查询关联的招标企业
                    var bidcominfoid= base.Repository.ChangeRepository<SugarRepositiry<PpBiddingsupplier>>()
                        .AsQueryable()
                        .Where(r => r.SupplierId == acc.CompanyId)?.Select(r=>r.BiddingCompanyId).ToList();
                    if (bidcominfoid.Count>0) 
                    {
                        RefAsync<int> totalNumber = 0;
                        List<string> status = new List<string>();
                        if (!string.IsNullOrEmpty(req.nowstatus))
                        {
                            status = req.nowstatus.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
                        }
                        var table = await base.Repository.AsQueryable()
                            .Where(r =>bidcominfoid.Contains(r.BiddingCompanyId)&&r.IsReview==true)
                            .WhereIF(!string.IsNullOrEmpty(req.nowstatus), r => status.Contains(r.NowStatus))
                            .WhereIF(!string.IsNullOrEmpty(req.projectname), r => r.ProjectName.Contains(req.projectname))
                            .WhereIF(!string.IsNullOrEmpty(req.projectnum), r => r.ProjectNum.Contains(req.projectnum))
                            .WhereIF(!string.IsNullOrEmpty(req.biddingtype), r => r.BiddingType == req.biddingtype)
                            .WhereIF(!string.IsNullOrEmpty(req.biddingform), r => r.BiddingForm == req.biddingform)
                            .WhereIF(req.year != null && req.year != 0, r => r.CreateTime != null && r.CreateTime.Value.Year == req.year)
                            .LeftJoin<PpRegistration>((r, g) => r.Id == g.BiddingInfoId)
                            .Where((r, g) => g.CompanyId == acc.CompanyId)
                            .LeftJoin<PpBiddingagency>((r, g, a) => r.BidAgent == a.Id)
                            .OrderByDescending((r, g, a) => r.CreateTime)
                            .Select<dynamic>((r, g, a) => new
                            {
                                id = r.Id,
                                projectName = r.ProjectName,
                                projectNum = r.ProjectNum,
                                biddingType = r.BiddingType,
                                biddingAmount = SqlFunc.Round(r.BiddingAmount, 2),
                                biddingForm = r.BiddingForm,
                                procuringEntity = r.ProcuringEntity,
                                registrationDeadline = r.RegistrationDeadline,
                                bidOpeningTime = r.BidOpeningTime,
                                nowStatus = r.NowStatus,
                                bidWinningAmount = SqlFunc.Round(r.BidWinningAmount, 2),
                                bidAgent = r.BidAgent,
                                createTime = r.CreateTime,
                                createUser = r.CreateUser,
                                remark = r.Remark,
                                winningCompanyId = r.WinningCompanyId,
                                name = a.Name,
                                overtimeflag = r.BidOpeningTime == null ? false : (((DateTime)r.BidOpeningTime).AddDays(2) < DateTime.Now ? true : false)
                            })
                            .ToPageListAsync(req.page, req.limit, totalNumber);

                        return new Response<PageInfo<List<dynamic>>>()
                        {
                            Result = new PageInfo<List<dynamic>>
                            {
                                Items = table,
                                Total = totalNumber
                            }
                        };
                    }
                    else
                    {
                        return new Response<PageInfo<List<dynamic>>>()
                        {
                            Result = new PageInfo<List<dynamic>>
                            {
                                Items = null,
                                Total = 0
                            }
                        };
                    }
                }
                else
                {
                    return new Response<PageInfo<List<dynamic>>>()
                    {
                        Result = new PageInfo<List<dynamic>>
                        {
                            Items = null,
                            Total = 0
                        }
                    };
                }
            }
            else
            {
                throw new Exception("请登录");
            }
        }
        #endregion


        #region 招商代理--当前招标
        public async Task<Response<PageInfo<List<dynamic>>>> LoadAgencyBidding(BiddingInfoReq req)
        {
            RefAsync<int> totalNumber = 0;
            List<string> status = new List<string>();
            if (!string.IsNullOrEmpty(req.nowstatus))
            {
                status = req.nowstatus.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            else
            {
                throw new Exception("请传参");
            }
            var user = _auth.GetCurrentUser();
            if (user == null) 
            {
                throw new Exception("请登录");
            }
            var acc = await base.Repository.ChangeRepository<SugarRepositiry<PpAgencyaccount>>()
                .AsQueryable().Where(r => r.UserId == user.User.Id.ToString()).FirstAsync();
            //全部正常和超期
            //var q1 = base.Repository.AsQueryable()
            //    .Where(r => r.NowStatus == status[1] || r.NowStatus == status[2])
            //    .WhereIF(!string.IsNullOrEmpty(req.projectname), r => r.ProjectName.Contains(req.projectname))
            //    .WhereIF(!string.IsNullOrEmpty(req.projectnum), r => r.ProjectNum.Contains(req.projectnum))
            //    .WhereIF(!string.IsNullOrEmpty(req.biddingtype), r => r.BiddingType == req.biddingtype)
            //    .WhereIF(!string.IsNullOrEmpty(req.biddingform), r => r.BiddingForm == req.biddingform)
            //    .WhereIF(req.year != null && req.year != 0, r => r.CreateTime != null && r.CreateTime.Value.Year == req.year)
            //    .LeftJoin<PpBiddingagency>((r, a) => r.BidAgent == a.Id)
            //    .OrderByDescending((r, a) => r.CreateTime)
            //    .Select<dynamic>((r, a) => new
            //    {
            //        id = r.Id,
            //        projectName = r.ProjectName,
            //        projectNum = r.ProjectNum,
            //        biddingType = r.BiddingType,
            //        biddingAmount = SqlFunc.Round(r.BiddingAmount, 2),
            //        biddingForm = r.BiddingForm,
            //        procuringEntity = r.ProcuringEntity,
            //        registrationDeadline = r.RegistrationDeadline,
            //        bidOpeningTime = r.BidOpeningTime,
            //        nowStatus = r.NowStatus,
            //        bidWinningAmount = SqlFunc.Round(r.BidWinningAmount, 2),
            //        bidAgent = r.BidAgent,
            //        createTime = r.CreateTime,
            //        createUser = r.CreateUser,
            //        remark = r.Remark,
            //        winningCompanyId = r.WinningCompanyId,
            //        name = a.Name,
            //        overtimeflag = r.BidOpeningTime == null ? false : (((DateTime)r.BidOpeningTime).AddDays(2) < DateTime.Now ? true : false)
            //    });
            if (acc != null)
            {
                //自己参与的待审核
                var q2 = base.Repository.AsQueryable()
                        .Where(r => r.BidAgent == acc.AgencyId && status.Contains(r.NowStatus))
                        .WhereIF(!string.IsNullOrEmpty(req.nowstatus), r => status.Contains(r.NowStatus))
                        .WhereIF(!string.IsNullOrEmpty(req.projectname), r => r.ProjectName.Contains(req.projectname))
                        .WhereIF(!string.IsNullOrEmpty(req.projectnum), r => r.ProjectNum.Contains(req.projectnum))
                        .WhereIF(!string.IsNullOrEmpty(req.biddingtype), r => r.BiddingType == req.biddingtype)
                        .WhereIF(!string.IsNullOrEmpty(req.biddingform), r => r.BiddingForm == req.biddingform)
                        .WhereIF(req.year != null && req.year != 0, r => r.CreateTime != null && r.CreateTime.Value.Year == req.year)
                        .LeftJoin<PpBiddingagency>((r, a) => r.BidAgent == a.Id)
                        .OrderByDescending((r, a) => r.CreateTime)
                        .Select<dynamic>((r, a) => new
                        {
                            id = r.Id,
                            projectName = r.ProjectName,
                            projectNum = r.ProjectNum,
                            biddingType = r.BiddingType,
                            biddingAmount = SqlFunc.Round(r.BiddingAmount, 2),
                            biddingForm = r.BiddingForm,
                            procuringEntity = r.ProcuringEntity,
                            registrationDeadline = r.RegistrationDeadline,
                            bidOpeningTime = r.BidOpeningTime,
                            nowStatus = r.NowStatus,
                            bidWinningAmount = SqlFunc.Round(r.BidWinningAmount, 2),
                            bidAgent = r.BidAgent,
                            createTime = r.CreateTime,
                            createUser = r.CreateUser,
                            remark = r.Remark,
                            winningCompanyId = r.WinningCompanyId,
                            name = a.Name,
                            overtimeflag = r.BidOpeningTime == null ? false : (((DateTime)r.BidOpeningTime).AddDays(2) < DateTime.Now ? true : false)
                        });
                var data=await q2.ToPageListAsync(req.page, req.limit, totalNumber);
                return new Response<PageInfo<List<dynamic>>>()
                {
                    Result = new PageInfo<List<dynamic>>
                    {
                        Items = data,
                        Total = totalNumber
                    }
                };
            }
            else
            {
                //var data = await q1.ToPageListAsync(req.page, req.limit, totalNumber);
                return new Response<PageInfo<List<dynamic>>>()
                {
                    Result = new PageInfo<List<dynamic>>
                    {
                        Items = null,
                        Total = totalNumber
                    }
                };
            }
        }
        #endregion

        #region 查询报名企业
        public async Task<Response<List<PpCompanyinfo>>> LoadBidCompanyInfo(string bidinfoid)
        {
            var acc = await base.Repository.ChangeRepository<SugarRepositiry<PpRegistration>>()
                .AsQueryable().Where(r => r.BiddingInfoId == bidinfoid)
                .LeftJoin<PpCompanyinfo>((r, c) => r.CompanyId == c.Id)
                .Select((r, c) => c).ToListAsync();
            return new Response<List<PpCompanyinfo>>()
            {
                Result = acc
            };
        }
        #endregion

        #region  审核招标信息
        /// <summary>
        /// 审核招标信息
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public async Task<Response<bool>> ReviewBiddingInfo(string biddinginfoid,bool isagree)
        {
            using (var db = UnitWork.CreateContext())
            {
                if(isagree)
                {
                    var flag = await db.PpBiddinginfo.UpdateAsync(it => new PpBiddinginfo()
                    {
                        NowStatus = "正常",
                        IsReview = isagree
                    }, it => it.Id == biddinginfoid);

                    var flags = db.Commit();
                    if (flag && flags)
                    {
                        return new Response<bool> { Result = true, Message = "审核成功" };
                    }
                    else
                    {
                        return new Response<bool> { Result = false, Message = "审核失败" };
                    }
                }
                else
                {
                    var flag = await db.PpBiddinginfo.UpdateAsync(it => new PpBiddinginfo()
                    {
                        IsReview = isagree
                    }, it => it.Id == biddinginfoid);

                    var flags = db.Commit();
                    if (flag && flags)
                    {
                        return new Response<bool> { Result = true, Message = "操作成功" };
                    }
                    else
                    {
                        return new Response<bool> { Result = false, Message = "操作失败" };
                    }
                }            
            }
        }
        #endregion
    }
}
