using DM_Weight.Models;
using DM_Weight.util;
using DM_Weight.Views.Dialog;
using gregn6Lib;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DM_Weight.Services
{
    /// 
    /// 账册服务类
    /// 
    public class MachineRecordService
    {
        ////private SqlSugarScope SqlSugarHelper.Db;
        public MachineRecordService()
        {
            ////this.SqlSugarHelper.Db = sqlSugarScope;
        }
        public List ReportAccountBook(DateTime? startDate, DateTime? endDate,int type, string drug_id="")
        {
            DateTime? p_startDate = startDate ?? Convert.ToDateTime("2010-1-1");
            DateTime? p_endDate = endDate ?? DateTime.Now.AddDays(1);
            string p_machine_id = (ConfigurationManager.AppSettings["machineId"] ?? "DM1");
            string SQL = $@"SELECT TYPE, stockQuantity,inQuantity,outQuantity,operationTime,invoiceId,drugName,drugId,drugSpec,packUnit,dosage,manufactory,manuNo,effDate,
                        operatorName,reviewerName,supplierDept,receiveDept FROM
                        (SELECT   mr.type,mr.`stock_quantity` AS `stockQuantity`, 
                        -- IF(mr.`type` IN (1, 31), mr.`quantity`,IF(mr.`type` = 4 AND mr.`quantity` > 0, mr.`quantity`, 0)) AS `inQuantity`,
                        -- IF(mr.`type` = 2, mr.`quantity`, IF(mr.`type` = 4 AND mr.`quantity` < 0, (0 - mr.`quantity`), 0)) AS `outQuantity`,
                         if(mr.drawer_no>2 and mr.`type` IN (1, 31),quantity,IF(mr.`type` = 4 AND mr.`quantity` > 0, mr.`quantity`,0)) AS `inQuantity`,
                         if(OI.ORDER_NO is not null,mr.quantity,IF(mr.`type` = 4 AND mr.`quantity` < 0, (0 - mr.`quantity`), 0)) AS `outQuantity`,
                         DATE_FORMAT(mr.`operation_time`,'%m/%d')  AS `operationTime`,IF(mr.`type`=1,mr.`invoice_id`,NULL) AS `invoiceId`, di.`drug_name` AS `drugName`, di.`drug_id` AS `drugId`,
                         di.`drug_spec` AS `drugSpec`, di.`pack_unit` AS `packUnit`, di.`dosage` AS `dosage`, di.`manufactory` AS `manufactory`,
                        mr.`manu_no` AS `manuNo`, DATE_FORMAT(mr.`eff_date`,'%y/%m/%d') AS `effDate`, u1.`user_name` AS `operatorName`, u2.`user_name` AS `reviewerName`,mr.supplierDept,mr.receiveDept FROM 
                        dm_machine_record mr  LEFT JOIN drug_info di ON mr.`drug_id` = di.`drug_id`  LEFT JOIN user_list u1 ON mr.`operator` = u1.`id`  
                        LEFT JOIN user_list u2 ON mr.`reviewer` = u2.`id`
                        LEFT JOIN ORDER_INFO OI ON OI.ORDER_NO=mr.invoice_id
                        WHERE mr.`machine_id` = '{p_machine_id}' AND mr.`operation_time` > '{p_startDate}'
                        AND mr.`operation_time` < '{p_endDate}'  and IF(mr.`type` = 4 AND mr.`quantity` = 0, 99999, mr.`quantity`) <>99999 ";
            if (!string.IsNullOrEmpty(drug_id))
            {
                SQL += " AND mr.drug_id='" + drug_id + "' ";
            }
            SQL += " ORDER BY mr.`drug_id`, mr.`operation_time`, mr.`id`) AS T";
            if(type>0)
            {
                if(type==1)
                {
                    SQL += " WHERE inQuantity>0 ";
                }
                if(type==2)
                {
                    SQL += " WHERE outQuantity>0 ";
                }
                if(type==5)
                {
                    SQL += " WHERE type=5 ";
                }
            }
            //ChannelStocks = q.Select(it => { it.CheckQuantity = it.Quantity; return it; }).ToList();
            List accountList = SqlSugarHelper.Db.SqlQueryable(SQL)
                                                                         .AddParameters(new
                                                                         {
                                                                             machineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1"
                                                                         })
                                                                        //.Select(it => new { o = new AccountModel(), i = new DrugInfo() })
                                                                        //.Select(it=>new AccountModel())
                                                                        //.Select("*") //让上面一行不生成sql
                                                                       .ToList();
            //List accountList=new List();
            return accountList;
        }
    }
}