using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Library;
using LitJson;
using System.Text;
using MySystem.Models.Main;

namespace MySystem.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }



        public string addsubmer()
        {
            WebCMSEntities db = new WebCMSEntities();
            List<string> data = new List<string>();
            // data.Add("015510108266178|成都红地餐饮管理有限公司|20241112");
            foreach(string sub in data)
            {
                string[] list = sub.Split('|');
                string merNo = list[0];
                string merName = list[1];
                string bindDateString = list[2];
                string bindDate = bindDateString.Substring(0, 4) + "-" + bindDateString.Substring(4, 2) + "-" + bindDateString.Substring(6, 2);
                if(!db.MerchantAddInfo.Any(m => m.MchtNo == merNo))
                {
                    MerchantAddInfo source = db.MerchantAddInfo.FirstOrDefault(m => m.CertMerchantName == merName && m.ParentId == 0);
                    int sourceCount = db.MerchantAddInfo.Count(m => m.CertMerchantName == merName && m.ParentId == 0);
                    if(source != null && sourceCount == 1)
                    {
                        MerchantInfo sourceInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == source.Id) ?? new MerchantInfo();
                        MerchantInfo info = db.MerchantInfo.Add(new MerchantInfo()
                        {
                            CreateDate = DateTime.Parse(bindDate),
                            UpdateDate = DateTime.Parse(bindDate),
                            Name = merName,
                            UserId = sourceInfo.UserId,
                            ParentId = source.Id,
                            Status = 2,
                            QueryCount = 2,
                            Sort = 1,
                        }).Entity;
                        db.SaveChanges();
                        db.MerchantAddInfo.Add(new MerchantAddInfo()
                        {
                            Id = info.Id,
                            CreateDate = DateTime.Parse(bindDate),
                            UpdateDate = DateTime.Parse(bindDate),
                            CertMerchantName = merName,
                            MchtNo = merNo,
                            ParentId = source.Id,
                            StoreNo = source.StoreNo,
                            OutMchtNo = source.OutMchtNo,
                            BrandId = source.BrandId,
                            AgentName = source.AgentName,
                            Status = 2,
                            QueryCount = 2,
                            HdStatus = 1,
                        });
                        db.SaveChanges();
                    }
                }
            }
            db.Dispose();
            return "ok";
        }
    }
}