using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AMESCoreStudio.WebApi;
using AMESCoreStudio.WebApi.Models.AMES;
using AMESCoreStudio.CommonTools.Result;

namespace AMESCoreStudio.WebApi.Controllers.AMES
{
    /// <summary>
    /// 檢驗單結果資料檔
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class FqcResultMasterController : ControllerBase
    {
        private readonly AMESContext _context;

        public FqcResultMasterController(AMESContext context)
        {
            _context = context;
        }

        // GET: api/FqcResultMaster
        [HttpGet]
        public async Task<ActionResult<IEnumerable<FqcResultMaster>>> GetFqcResultMasters()
        {
            return await _context.FqcResultMasters.ToListAsync();
        }

        // GET: api/FqcResultMaster/5
        [HttpGet("{id}")]
        public async Task<ActionResult<FqcResultMaster>> GetFqcResultMaster(int id)
        {
            var fqcResultMaster = await _context.FqcResultMasters.FindAsync(id);
            return fqcResultMaster;
        }

        /// <summary>
        /// 用入庫單號 and 序號找資料
        /// </summary>
        /// <param name="inhouseNo">入庫單號</param>
        /// <param name="seq">序號</param>
        /// <returns></returns>
        [HttpGet("ByInhouseNo/{inhouseNo}/{seq}")]
        public async Task<ActionResult<IEnumerable<FqcResultMaster>>> GetFqcResultMasterByInhouseNo(string inhouseNo , int seq)
        {
            var fqcResultMaster = await _context.FqcResultMasters
                                  .Where(w => w.InhouseNo == inhouseNo && w.SeqID == seq)
                                  .ToListAsync();

            //if (fqcResultMaster == null)
            //{
            //    return NotFound();
            //}

            return fqcResultMaster;
        }

        /// <summary>
        /// FQC查詢報表QRS016 細項統計
        /// </summary>
        /// <returns></returns>
        [Route("[action]")]
        [HttpGet]
        public async Task<ResultModel<DTO.AMES.QRS016Detail>> GetFQCHeaderData4QRS016ByDetail(string wipNO, string itemNO, string modelNO, string dateStart, string dateEnd, string factoryID)
        {
            ResultModel<DTO.AMES.QRS016Detail> result = new ResultModel<DTO.AMES.QRS016Detail>();
            var q = from q1 in _context.FqcResultMasters
                    join q2 in _context.WipInfos on q1.WipNo equals q2.WipNO
                    join q3 in _context.FqcInhouseMasters on new { q1.InhouseNo, q1.SeqID } equals new { q3.InhouseNo, q3.SeqID }
                    select new DTO.AMES.QRS016Detail
                    {
                        wipNo = q1.WipNo,
                        itemNo = q1.ItemNo,
                        modelNo = q1.ModelNo,
                        werks = q2.Werks,
                        startTime = q3.CreateDate,
                        qaResult = q1.QaResult,
                        customerMedical = q2.CustomerMedical,
                        unitNo = q2.UnitNO
                    };

            if (wipNO != null && wipNO != "")
            {
                q = q.Where(w => w.wipNo.Trim().ToUpper() == wipNO.Trim().ToUpper());
            }
            if (itemNO != null && itemNO != "")
            {
                q = q.Where(w => w.itemNo.Trim().ToUpper() == itemNO.Trim().ToUpper());
            }
            if (modelNO != null && modelNO != "")
            {
                q = q.Where(w => w.modelNo.Trim().ToUpper() == modelNO.Trim().ToUpper());
            }

            if (factoryID != null && factoryID != "")
            {
                q = q.Where(w => w.werks == factoryID);
            }

            if (dateStart != null && dateStart != "" && dateEnd != null && dateEnd != "")
            {
                q = q.Where(w => w.startTime >= DateTime.Parse(dateStart + " 00:00:00") && w.startTime <= DateTime.Parse(dateEnd + " 23:59:59"));
            }

            var query = new List<DTO.AMES.QRS016Detail>();
            int allQty = 0;
            int passQty = 0;
            int failQty = 0;
            double rejectRate = 0;

            // 醫療
            var data = q.Where(w => w.customerMedical == "Y" && w.qaResult != "A").ToList();
            allQty = data.Count();
            passQty = data.Where(w => w.qaResult == "P").Count();
            failQty = data.Where(w => w.qaResult == "R").Count();
            rejectRate = failQty == 0 ? 0 : Math.Round(((double)failQty / (double)allQty), 2, MidpointRounding.AwayFromZero) * 100;

            query.Add(new DTO.AMES.QRS016Detail
            {
                type = "醫療",
                allQty = allQty,
                passQty = passQty,
                failQty = failQty,
                rejectRate = rejectRate
            });

            // 系統組裝
            data.Clear();
            data = q.Where(w => w.customerMedical == "N" && w.unitNo == "B" && w.qaResult != "A").ToList();
            allQty = data.Count();
            passQty = data.Where(w => w.qaResult == "P").Count();
            failQty = data.Where(w => w.qaResult == "R").Count();
            rejectRate = failQty == 0 ? 0 : Math.Round(((double)failQty / (double)allQty), 2, MidpointRounding.AwayFromZero) * 100;
            query.Add(new DTO.AMES.QRS016Detail
            {
                type = "系統組裝",
                allQty = allQty,
                passQty = passQty,
                failQty = failQty,
                rejectRate = rejectRate
            });

            // 單板
            data.Clear();
            data = q.Where(w => w.customerMedical == "N" && w.unitNo == "S" && w.qaResult != "A").ToList();
            allQty = data.Count();
            passQty = data.Where(w => w.qaResult == "P").Count();
            failQty = data.Where(w => w.qaResult == "R").Count();
            rejectRate = failQty == 0 ? 0 : Math.Round(((double)failQty / (double)allQty), 2, MidpointRounding.AwayFromZero) * 100;
            query.Add(new DTO.AMES.QRS016Detail
            {
                type = "單板",
                allQty = allQty,
                passQty = passQty,
                failQty = failQty,
                rejectRate = rejectRate
            });

            //紀錄筆數
            result.DataTotal = query.Count();

            result.Data = query;

            if (query.Count == 0)
            {
                result.Msg = "查無資料";
                result.Success = false;
                return result;
            }

            result.Success = true;
            result.Msg = "OK";
            return result;
        }


        /// <summary>
        /// FQC查詢報表QRS016
        /// </summary>
        /// <returns></returns>
        [Route("[action]")]
        [HttpGet]
        public async Task<ResultModel<dynamic>> GetFQCHeaderData4QRS016(string wipNO, string itemNO, string modelNO, string dateStart, string dateEnd, string factoryID)
        {
            ResultModel<dynamic> result = new ResultModel<dynamic>();
            var q = from q1 in _context.FqcResultMasters
                    join q2 in _context.CalendarTables on q1.CreateDate.Date equals q2.TimeID
                    join q3 in _context.WipInfos on q1.WipNo equals q3.WipNO
                    join q4 in _context.FqcInhouseMasters on new { q1.InhouseNo, q1.SeqID } equals new { q4.InhouseNo, q4.SeqID }
                    select new
                    {
                        q1.FqcID,
                        q1.CreateDate,
                        q1.WipNo,
                        q1.ItemNo,
                        q1.ModelNo,
                        q1.LotQty,
                        q1.PassQty,
                        q1.FailQty,
                        q1.QaResult,
                        q2.WeekOfYear,
                        q3.Werks,
                        InhouseCreateDate = q4.CreateDate
                    };

            if (wipNO != null && wipNO != "")
            {
                q = q.Where(w => w.WipNo == wipNO);
            }
            if (itemNO != null && itemNO != "")
            {
                q = q.Where(w => w.ItemNo == itemNO);
            }
            if (modelNO != null && modelNO != "")
            {
                q = q.Where(w => w.ModelNo == modelNO);
            }
            if (factoryID != null && factoryID != "")
            {
                q = q.Where(w => w.Werks == factoryID);
            }
            if (dateStart != null && dateStart != "" && dateEnd != null && dateEnd != "")
            {
                q = q.Where(w => w.InhouseCreateDate >= DateTime.Parse(dateStart + " 00:00:00") && w.InhouseCreateDate <= DateTime.Parse(dateEnd + " 23:59:59"));
            }

            q = q.OrderBy(w => w.InhouseCreateDate);

            /*
            //EF.Functions
            var g = q.GroupBy(x => new { x.WeekOfYear }).Select(x => new
            {
                FQCWeek = x.Key.WeekOfYear,
                //FQCCnt = x.Count(),
                FQCCnt = x.Sum(i => i.LotQty),
                PassCnt = x.Sum(i => i.PassQty),
                FailCnt = x.Sum(i => i.FailQty)
            });
            */

            var g = q.GroupBy(x => new { x.WeekOfYear }).Select(x => new
            {
                FQCWeek = x.Key.WeekOfYear,
                FQCCnt = x.Count(),
                //FQCCnt = x.Sum(i => i.LotQty),
                PassCnt = x.Count(i => i.QaResult == "P"),
                FailCnt = x.Count(i => i.QaResult == "R")
            });

            g = g.OrderBy(y => y.FQCWeek);

            //紀錄筆數
            result.DataTotal = g.Count();

            result.Data = await g.ToListAsync();

            if (result == null)
            {
                result.Msg = "查無資料";
                result.Success = false;
                return result;
            }

            result.Success = true;
            result.Msg = "OK";
            return result;
        }

        /// <summary>
        /// 查詢FQC明細資料
        /// </summary>
        /// <returns></returns>
        [Route("[action]")]
        [HttpGet]
        public async Task<ResultModel<dynamic>> GetFQCDetailData(string wipNO, string itemNO, string modelNO, string dateStart, string dateEnd, string weekCode, int page = 0, int limit = 10)
        {
            ResultModel<dynamic> result = new ResultModel<dynamic>();
            var q = from q1 in _context.FqcResultMasters
                    join q2 in _context.CalendarTables on q1.CreateDate.Date equals q2.TimeID
                    select new
                    {
                        q1.InhouseNo,
                        q1.WipNo,
                        q1.ModelNo,
                        q1.ItemNo,
                        q1.LotQty,
                        q1.QaResult,
                        q1.SpecialPo,
                        q1.CreateDate,
                        q2.WeekOfYear
                    };

            if (wipNO != null && wipNO != "")
            {
                q = q.Where(w => w.WipNo == wipNO);
            }
            if (itemNO != null && itemNO != "")
            {
                q = q.Where(w => w.ItemNo == itemNO);
            }
            if (modelNO != null && modelNO != "")
            {
                q = q.Where(w => w.ModelNo == modelNO);
            }
            if (dateStart != null && dateStart != "" && dateEnd != null && dateEnd != "")
            {
                q = q.Where(w => w.CreateDate >= DateTime.Parse(dateStart) && w.CreateDate <= DateTime.Parse(dateEnd));
            }
            if (weekCode != null && weekCode != "")
            {
                q = q.Where(w => w.WeekOfYear.Equals(int.Parse(weekCode)));
            }

            //紀錄筆數
            result.DataTotal = q.Count();

            //Table 頁數
            if (page > 0)
            {
                q = q.Skip((page - 1) * limit).Take(limit);
            }

            result.Data = await q.ToListAsync();

            if (result == null)
            {
                result.Msg = "查無資料";
                result.Success = false;
                return result;
            }

            result.Success = true;
            result.Msg = "OK";
            return result;
        }

        /// <summary>
        /// 更新FQC檢驗單結果
        /// </summary>
        /// <param name="fqcResultMaster"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<FqcResultMaster>> PutFqcResultMaster(FqcResultMaster fqcResultMaster)
        {
            ResultModel<FqcResultMaster> result = new ResultModel<FqcResultMaster>();
            _context.Entry(fqcResultMaster).State = EntityState.Modified;
            fqcResultMaster.UpdateDate = DateTime.Now;

            try
            {
                await _context.SaveChangesAsync();
                result.Success = true;
                result.Msg = "OK";
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.InnerException.Message;
            }
            return result;
        }


        /// <summary>
        /// 新增FQC檢驗單結果
        /// </summary>
        /// <param name="fqcResultMaster"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<FqcResultMaster>> PostFqcResultMaster(FqcResultMaster fqcResultMaster)
        {
            ResultModel<FqcResultMaster> result = new ResultModel<FqcResultMaster>();
            Helper helper = new Helper(_context);
            fqcResultMaster.FqcID = helper.GetIDKey("FQC_ID").Result;
            _context.FqcResultMasters.Add(fqcResultMaster);
            try
            {
                await _context.SaveChangesAsync();
                result.Data = new List<FqcResultMaster> { fqcResultMaster };
                result.Success = true;
                result.Msg = "OK";
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.InnerException.Message;
            }
            return result;
        }

        // DELETE: api/FqcResultMaster/5
        [HttpDelete("{id}")]
        public async Task<ResultModel<FqcResultMaster>> DeleteFqcResultMaster(int id)
        {
            ResultModel<FqcResultMaster> result = new ResultModel<FqcResultMaster>();
            var fqcResultMaster = await _context.FqcResultMasters.FindAsync(id);
            if (fqcResultMaster == null)
            {
                result.Success = false;
                result.Msg = "單號不存在";
                return result;
            }
            try
            {
                _context.FqcResultMasters.Remove(fqcResultMaster);
                await _context.SaveChangesAsync();
                result.Data = new List<FqcResultMaster> { fqcResultMaster };
                result.Success = true;
                result.Msg = "OK";

            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.InnerException.Message;
            }


            return result;
        }

        private bool FqcResultMasterExists(int id)
        {
            return _context.FqcResultMasters.Any(e => e.FqcID == id);
        }
    }
}