public ActionResult Index()
        {
            List <PProductViewModel> pam = new List <PProductViewModel>();

            ViewData["cfgUnit"] = new UnitsRepository().getAll();
            List <Product>      pd  = new MallRepository().getAll().ToList();
            List <Preferential> pft = new PreferentialRepository().getAllvaild();

            foreach (var p in pd)
            {
                PProductViewModel pm = new PProductViewModel();
                pm.id             = p.id;
                pm.image          = p.image;
                pm.inpdate        = p.inpdate;
                pm.Price          = p.Price;
                pm.ProductContent = p.ProductContent;
                pm.transform      = p.transform;
                pm.type           = p.type;
                pm.unitSn         = p.unitSn;
                pm.valid          = p.valid;
                pm.ProductName    = p.ProductName;
                pm.createDate     = p.createDate;



                if (p.type == 1 || p.type == 2)
                {
                    if (pft.Where(x => x.offerModel == 3 && x.productId == null).FirstOrDefault() != null)
                    {
                        pm.offer = pft.Where(x => x.offerModel == 3 && x.productId == null).FirstOrDefault().offer;
                        pm.Pname = pft.Where(x => x.offerModel == 3 && x.productId == null).FirstOrDefault().Pname;
                    }

                    foreach (var pt in pft.Where(x => x.offerModel == 3))
                    {
                        if (pt.productId == p.id)
                        {
                            pm.offer = pt.offer;
                            pm.Pname = pt.Pname;
                        }
                    }
                }

                pam.Add(pm);
            }
            //new MailServiceMailgun().ChangeSend("熊i猜兌獎通知信", "test","*****@*****.**", "");
            ViewData["ProductM"] = new MallRepository().PMenuGetAll();
            return(View(pam));
        }
        public int MallChange(int id, float?money, string name, string phone, string address)
        {
            var userId = User.Identity.GetUserId();
            // var malldata = new MangerRepository().GetTransferRecords(userId);
            Product product = new MallRepository().Get(id);
            // 1 現金換魚骨 -2 兌獎 -3 鮭魚換魚骨

            int offer = 100;

            List <Preferential> pft = new PreferentialRepository().getAllvaild();

            if (pft != null)
            {
                if (pft.Where(x => x.offerModel == 3 && x.productId == null).FirstOrDefault() != null)
                {
                    offer = (int)pft.Where(x => x.offerModel == 3 && x.productId == null).FirstOrDefault().offer;
                }

                foreach (var pt in pft.Where(x => x.offerModel == 3))
                {
                    if (pt.productId == id)
                    {
                        offer = (int)pft.Where(x => x.offerModel == 3 && x.productId == id).FirstOrDefault().offer;
                    }
                }
            }

            ProductRecord assr = new ProductRecord
            {
                UserID    = User.Identity.GetUserId(),
                unitSn    = product.unitSn,
                assets    = (money != null) ? -money : -product.Price * offer / 100,
                inpdate   = DateTime.Now,
                type      = (money != null)?-3:-2,
                ProductId = product.id,
                recipient = name,
                phone     = phone,
                address   = address
            };
            bool aset = new AssetsRepository().AddBearByAssets(assr);

            if (product.unitSn == 2 && product.type == 3)
            {
                assr = new ProductRecord
                {
                    UserID    = User.Identity.GetUserId(),
                    unitSn    = 1,
                    assets    = money * product.Price,
                    inpdate   = DateTime.Now,
                    type      = 3,
                    ProductId = product.id,
                    recipient = name,
                    phone     = phone,
                    address   = address
                };
                aset = new AssetsRepository().AddBearByAssets(assr);
            }
            if (money == null)
            {
                var m = Math.Ceiling((double)product.Price / 1.2 / 30000 * 30);
                var d = 0;
                if (m < 1000)
                {
                    d = 1;
                }
                else if (999 < m && m < 20000)
                {
                    d = 2;
                }
                else if (m > 19999)
                {
                    d = 3;
                }
                var    user         = UserManager.FindById(User.Identity.GetUserId());
                var    content      = ConfirmChange(d, assr, product);
                var    EmailContent = EmailTemplatesService.GetChangeEmailHTML(content);
                var    h            = Server.MapPath("\\Content\\Pdf\\Change\\");
                string url          = (product.pdf_file != null) ?  product.pdf_file : "";
                new MailServiceMailgun().ChangeSend("熊i猜兌獎通知信", EmailContent, user.Email, url);
            }



            return(aset?1:0);
        }
        public async Task <ActionResult> _MallChange(int id)
        {
            aJaxDto ajd = new aJaxDto();

            try {
                if (User.Identity.GetUserId() == null)
                {
                    ajd.ErrorMsg = "登入後才可以購買";
                    ajd.Title    = "未登入!";
                    ajd.isTrue   = true;
                    throw new Exception("error");
                }
                var user = UserManager.FindById(User.Identity.GetUserId());
                if (!user.EmailConfirmed)
                {
                    ajd.ErrorMsg = "請先至會員中心進行驗證,謝謝";
                    ajd.Title    = "Email尚未驗證!";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }
                if (!user.PhoneNumberConfirmed)
                {
                    ajd.ErrorMsg = "請先至會員中心進行驗證,謝謝";
                    ajd.Title    = "手機尚未驗證!";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }
                ProductApiModel pm = new ProductApiModel();

                Product product = new MallRepository().Get(id);
                // List<Product> productAll = new MallRepository().getAll();
                cfgUnit unit = new UnitsRepository().getValid(product.unitSn);

                pm.unit    = unit;
                pm.product = product;
                //pm.producList = productAll.Where(x => x.id < 5).ToList();
                int offer = 100;

                List <Preferential> pft = new PreferentialRepository().getAllvaild();
                ViewBag.offer = 100;
                if (pft != null)
                {
                    if (pft.Where(x => x.offerModel == 3 && x.productId == null).FirstOrDefault() != null)
                    {
                        ViewBag.offer = pft.Where(x => x.offerModel == 3 && x.productId == null).FirstOrDefault().offer;
                        offer         = (int)pft.Where(x => x.offerModel == 3 && x.productId == null).FirstOrDefault().offer;
                    }

                    foreach (var pt in pft.Where(x => x.offerModel == 3))
                    {
                        if (pt.productId == id)
                        {
                            ViewBag.offer = pt.offer;
                            offer         = (int)pft.Where(x => x.offerModel == 3 && x.productId == id).FirstOrDefault().offer;
                        }
                    }
                }

                var gold = new AssetsRepository().getAssetsListByUserID(User.Identity.GetUserId()).Where(x => x.unitSn == product.unitSn).FirstOrDefault();
                ViewBag.gold   = (gold != null) ? gold.Asset : 0;
                ViewBag.change = (product.Price * offer / 100 > ViewBag.gold) ? 0 : 1;



                return(View(pm));
            }
            catch
            {
                //ajd.isTrue = false;
                return(Json(ajd, JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult _FishPay(int id)
        {
            aJaxDto ajd = new aJaxDto();

            try
            {
                if (User.Identity.GetUserId() == null)
                {
                    ajd.ErrorMsg = "登入後才可以購買";
                    ajd.Title    = "未登入!";
                    ajd.isTrue   = true;
                    throw new Exception("error");
                }
                var user = UserManager.FindById(User.Identity.GetUserId());
                if (!user.EmailConfirmed)
                {
                    ajd.ErrorMsg = "請先至會員中心進行驗證,謝謝";
                    ajd.Title    = "Email尚未驗證!";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }
                if (!user.PhoneNumberConfirmed)
                {
                    ajd.ErrorMsg = "請先至會員中心進行驗證,謝謝";
                    ajd.Title    = "手機尚未驗證!";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }

                //var pr = new PreferentialRepository().getPRecords(User.Identity.GetUserId());

                //普通首儲

                /*if (pr == null)
                 * {
                 *  var prm = new PreferentialRecords {
                 *      UserId = User.Identity.GetUserId(),
                 *      Count = 1,
                 *      inpdate = DateTime.Now,
                 *      PreferentialID = 1
                 *  };
                 *
                 *  new PreferentialRepository().PRecordsCreate(prm);
                 *  pr = new PreferentialRepository().getPRecords(User.Identity.GetUserId());
                 * }*/
                var pr = new PreferentialRepository().getPRecordsAll(User.Identity.GetUserId()).Where(x => x.PreferentialID == 5);
                //2倍首儲
                if (pr.Count() == 0)
                {
                    var m = new MallRepository().getAll().Where(x => x.unitSn == 3 && x.type == 3).ToList();
                    foreach (var md in m)
                    {
                        var prm = new PreferentialRecords
                        {
                            UserId         = User.Identity.GetUserId(),
                            Count          = 1,
                            inpdate        = DateTime.Now,
                            PreferentialID = 5,
                            productID      = md.id
                        };

                        new PreferentialRepository().PRecordsCreate(prm);
                        //pr = new PreferentialRepository().getPRecords(User.Identity.GetUserId());
                    }
                }
                pr = new PreferentialRepository().getPRecordsAll(User.Identity.GetUserId()).Where(x => x.PreferentialID == 5);



                Product         product    = new MallRepository().Get(id);
                List <Product>  productAll = new MallRepository().getAll();
                cfgUnit         unit       = new UnitsRepository().getValid(product.unitSn);
                ProductApiModel pm         = new ProductApiModel();
                pm.unit       = unit;
                pm.product    = product;
                pm.producList = productAll.Where(x => x.unitSn == 3 && x.type == 3).ToList();

                List <int> fta = new List <int>();
                foreach (var p in pr)
                {
                    if (p.Count != 0)
                    {
                        fta.Add(1);
                    }
                    else
                    {
                        fta.Add(0);
                    }
                }
                pm.firstTypeArray = fta;
                if (pr.Where(x => x.Count != 0).Count() != 0)
                {
                    pm.firstType = 1;
                }
                else
                {
                    pm.firstType = 0;
                }
                //pm.firstType = (pr.Count != 0) ? 1 : 0;
                pm.preferential = new PreferentialRepository().getpreferential(1);
                return(View(pm));
            }
            catch
            {
                return(Json(ajd, JsonRequestBehavior.AllowGet));
            }
        }