package repository

import (
	"database/sql"
	"errors"
	"fmt"
	"gin-vue-admin/models"
)

func RevenueTodayListTemp(db *sql.DB, t string, employeeID int) (interface{}, error) {

	hospitalLocalId, err := QueryHospitalLocalIDByEmployeeLocalID(db, employeeID)
	if err != nil {
		return nil, err
	}

	// 计算时间 及格式化的模板
	var formatSQL string

	if len(t) == 10 {
		formatSQL = "%Y-%m-%d"
	} else if len(t) == 7 {
		formatSQL = "%Y-%m"
	} else if len(t) == 4 {
		formatSQL = "%Y"
	} else {
		return nil, errors.New("time error")
	}

	rows, err := db.Query(`select c.bussinesstype,
convert(ifnull(sum(c.quantity*c.payprice), 0) / 1000000, decimal(11,3))  
from his_consumption as c
left join his_bill as b on b.id = c.his_bill_id
where c.delflag = 0 
and c.his_retreatbill_id = 0 
and c.commodity_ismeal = 0
and c.meter_num = 0
and b.billtype in (1,4,6,7)
and DATE_FORMAT(b.paytime, ?) = ?
GROUP BY c.bussinesstype`, formatSQL, t)
	if err != nil {
		return nil, err
	}

	// DB:
	// 1：商品销售 2：挂号 3：住院每日消费 4.住院门诊 41：住院门诊化验  42：住院门诊影像 43：住院门诊处方
	// 44：住院门诊手术 45：住院门诊处置  5：住院商品销售 6：普通门诊 61：普通门诊化验  62：普通门诊影像
	// 63：普通门诊处方 64：普通门诊手术 65：普通门诊处置 7：美容 8：寄养每日消费 9：寄养商品销售 10：疫苗 11:驱虫

	// Map
	// 1 商品销售
	// 2 挂号
	// 3 住院每日消费
	// 4 化验
	// 5 影像
	// 6 处方
	// 7 手术
	// 8 处置
	// 9 美容
	// 10 寄养
	// 11 疫苗
	// 12 驱虫

	tempRes := map[int]float64{
		1:  0,
		2:  0,
		3:  0,
		4:  0,
		5:  0,
		6:  0,
		7:  0,
		8:  0,
		9:  0,
		10: 0,
		11: 0,
		12: 0,
	}
	for rows.Next() {
		var businessType int
		var money float64
		rows.Scan(&businessType, &money)

		switch businessType {
		case 1:
			tempRes[1] += money
		case 2:
			tempRes[2] += money
		case 3:
			tempRes[3] += money
		case 4:
			tempRes[3] += money
		case 41:
			tempRes[4] += money
		case 42:
			tempRes[5] += money
		case 43:
			tempRes[6] += money
		case 44:
			tempRes[7] += money
		case 45:
			tempRes[8] += money
		case 5:
			tempRes[3] += money
		case 6:
			tempRes[6] += money
		case 61:
			tempRes[4] += money
		case 62:
			tempRes[5] += money
		case 63:
			tempRes[6] += money
		case 64:
			tempRes[7] += money
		case 65:
			tempRes[8] += money
		case 7:
			tempRes[9] += money
		case 8:
			tempRes[10] += money
		case 9:
			tempRes[10] += money
		case 10:
			tempRes[11] += money
		case 11:
			tempRes[12] += money
		}
	}

	var r models.Today

	response := make([]models.RevenueToday, 0)

	business := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

	for _, v := range business {
		var t1 models.RevenueToday
		var t2 models.RevenueToday
		switch v {
		case 1:
			t1.Type = "收入"
			t1.Name = "商品销售"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "商品销售"
			t2.Value = 0
		case 2:
			t1.Type = "收入"
			t1.Name = "挂号"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "挂号"
			t2.Value = 0
		case 3:
			t1.Type = "收入"
			t1.Name = "住院"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "住院"
			t2.Value = 0
		case 4:
			t1.Type = "收入"
			t1.Name = "化验"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "化验"
			t2.Value = 0
		case 5:
			t1.Type = "收入"
			t1.Name = "影像"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "影像"
			t2.Value = 0
		case 6:
			t1.Type = "收入"
			t1.Name = "处方"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "处方"
			t2.Value = 0
		case 7:
			t1.Type = "收入"
			t1.Name = "手术"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "手术"
			t2.Value = 0
		case 8:
			t1.Type = "收入"
			t1.Name = "处置"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "处置"
			t2.Value = 0
		case 9:
			t1.Type = "收入"
			t1.Name = "美容"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "美容"
			t2.Value = 0
		case 10:
			t1.Type = "收入"
			t1.Name = "寄养"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "寄养"
			t2.Value = 0
		case 11:
			t1.Type = "收入"
			t1.Name = "疫苗"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "疫苗"
			t2.Value = 0
		case 12:
			t1.Type = "收入"
			t1.Name = "驱虫"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "驱虫"
			t2.Value = 0
		}

		if _, ok := tempRes[v]; ok {
			r.Income += tempRes[v]
		}

		response = append(response, t1, t2)
	}

	// 会员卡 收入 与 支出
	consumerCardRecharge, err := ConsumerCardRecharge(db, t, formatSQL)
	if err != nil {
		return nil, err
	}
	retreatConsumerCard, err := RetreatConsumerCard(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	var card1 models.RevenueToday
	var card2 models.RevenueToday
	card1.Type = "收入"
	card1.Name = "会员卡"
	card1.Value = consumerCardRecharge
	card2.Type = "支出"
	card2.Name = "会员卡"
	card2.Value = retreatConsumerCard
	response = append(response, card1)
	response = append(response, card2)
	r.Income += consumerCardRecharge
	r.Expend += retreatConsumerCard

	// 押金收入与支出
	depositMoneyRecharge, err := DepositMoneyRecharge(db, t, formatSQL)
	if err != nil {
		return nil, err
	}
	retreatDepositMoney, err := RetreatDepositMoney(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	var deposit1 models.RevenueToday
	var deposit2 models.RevenueToday
	deposit1.Type = "收入"
	deposit1.Name = "押金"
	deposit1.Value = depositMoneyRecharge
	deposit2.Type = "支出"
	deposit2.Name = "押金"
	deposit2.Value = retreatDepositMoney
	response = append(response, deposit1)
	response = append(response, deposit2)
	r.Income += depositMoneyRecharge
	r.Expend += retreatDepositMoney

	// 次卡收入与支出
	meterCardRecharge, err := MeterCardRecharge(db, t, formatSQL, 0, hospitalLocalId)
	if err != nil {
		return nil, err
	}
	retreatMeterCard, err := RetreatMeterCard(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	var meterCard1 models.RevenueToday
	var meterCard2 models.RevenueToday
	meterCard1.Type = "收入"
	meterCard1.Name = "次卡"
	meterCard1.Value = meterCardRecharge
	meterCard2.Type = "支出"
	meterCard2.Name = "次卡"
	meterCard2.Value = retreatMeterCard
	response = append(response, meterCard1)
	response = append(response, meterCard2)
	r.Income += meterCardRecharge
	r.Expend += retreatMeterCard

	//  4. 已结账单中: 现金、支付宝、微信、银行卡 (会员卡、次卡、押金结算 不算其中)
	//	5. 销售出库-现金结算
	//	6. 采购退货-现金结算
	//	7. 预收款
	//	8. 收入单
	billOfSettlementIncome, err := BillOfSettlementIncome(db, t, formatSQL)
	if err != nil {
		return nil, err
	}
	stockOutBill, err := StockOutBill(db, t, formatSQL)
	if err != nil {
		return nil, err
	}

	stockRetreatBill, err := StockRetreatBill(db, t, formatSQL)
	if err != nil {
		return nil, err
	}

	preInFundBill, err := PreInFundBill(db, t, formatSQL)
	if err != nil {
		return nil, err
	}
	retreatPreInFundBill, err := RetreatPreInFundBill(db, t, formatSQL)
	if err != nil {
		return nil, err
	}
	inChargeBill, err := InChargeBill(db, t, formatSQL)
	if err != nil {
		return nil, err
	}

	var other1 models.RevenueToday
	other1.Type = "收入"
	other1.Name = "其它"
	other1.Value = billOfSettlementIncome + stockOutBill + stockRetreatBill + preInFundBill - retreatPreInFundBill + inChargeBill
	response = append(response, other1)

	//4. 支出管理中的
	//5. 采购入库-现金结算
	//6.  预付款

	outChargeBill, err := OutChargeBill(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	stockInBill, err := StockInBill(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	preOutFundBill, err := PreOutFundBill(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}
	retreatPreOutFundBill, err := RetreatPreOutFundBill(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	var other2 models.RevenueToday
	other2.Type = "支出"
	other2.Name = "其它"
	other2.Value = outChargeBill + stockInBill + preOutFundBill - retreatPreOutFundBill
	response = append(response, other2)

	r.Income += other1.Value
	r.Expend += other2.Value

	r.RevenueTodayList = response

	return r, nil
}

func RevenueTodayList(db *sql.DB, t string, employeeId int) (interface{}, error) {

	hospitalLocalId, err := QueryHospitalLocalIDByEmployeeLocalID(db, employeeId)
	if err != nil {
		return nil, err
	}

	// 计算时间 及格式化的模板
	var formatSQL string

	if len(t) == 10 {
		formatSQL = "%Y-%m-%d"
	} else if len(t) == 7 {
		formatSQL = "%Y-%m"
	} else if len(t) == 4 {
		formatSQL = "%Y"
	} else {
		return nil, errors.New("time error")
	}

	rows, err := db.Query(fmt.Sprintf(`select c.bussinesstype,
convert(ifnull(sum(c.quantity*c.payprice), 0) / 1000000, decimal(11,3))  
from his_consumption as c
left join his_bill as b on b.id = c.his_bill_id
where c.delflag = 0 
and c.his_retreatbill_id = 0 
and c.commodity_ismeal = 0
and c.meter_num = 0
and b.billtype in (1,4,6,7)
and DATE_FORMAT(b.paytime, ?) = ?
and  c.sys_hospital_id in (0,%v) 
GROUP BY c.bussinesstype`, hospitalLocalId), formatSQL, t)
	if err != nil {
		return nil, err
	}

	// DB:
	// 1：商品销售 2：挂号 3：住院每日消费 4.住院门诊 41：住院门诊化验  42：住院门诊影像 43：住院门诊处方
	// 44：住院门诊手术 45：住院门诊处置  5：住院商品销售 6：普通门诊 61：普通门诊化验  62：普通门诊影像
	// 63：普通门诊处方 64：普通门诊手术 65：普通门诊处置 7：美容 8：寄养每日消费 9：寄养商品销售 10：疫苗 11:驱虫

	// Map
	// 1 商品销售
	// 2 挂号
	// 3 住院每日消费
	// 4 化验
	// 5 影像
	// 6 处方
	// 7 手术
	// 8 处置
	// 9 美容
	// 10 寄养
	// 11 疫苗
	// 12 驱虫

	tempRes := map[int]float64{
		1:  0,
		2:  0,
		3:  0,
		4:  0,
		5:  0,
		6:  0,
		7:  0,
		8:  0,
		9:  0,
		10: 0,
		11: 0,
		12: 0,
	}
	for rows.Next() {
		var businessType int
		var money float64
		rows.Scan(&businessType, &money)

		switch businessType {
		case 1:
			tempRes[1] += money
		case 2:
			tempRes[2] += money
		case 3:
			tempRes[3] += money
		case 4:
			tempRes[3] += money
		case 41:
			tempRes[4] += money
		case 42:
			tempRes[5] += money
		case 43:
			tempRes[6] += money
		case 44:
			tempRes[7] += money
		case 45:
			tempRes[8] += money
		case 5:
			tempRes[3] += money
		case 6:
			tempRes[6] += money
		case 61:
			tempRes[4] += money
		case 62:
			tempRes[5] += money
		case 63:
			tempRes[6] += money
		case 64:
			tempRes[7] += money
		case 65:
			tempRes[8] += money
		case 7:
			tempRes[9] += money
		case 8:
			tempRes[10] += money
		case 9:
			tempRes[10] += money
		case 10:
			tempRes[11] += money
		case 11:
			tempRes[12] += money
		}
	}

	var r models.Today

	response := make([]models.RevenueToday, 0)

	business := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

	for _, v := range business {
		var t1 models.RevenueToday
		var t2 models.RevenueToday
		switch v {
		case 1:
			t1.Type = "营业额"
			t1.Name = "商品销售"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "商品销售"
			t2.Value = 0
		case 2:
			t1.Type = "营业额"
			t1.Name = "挂号"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "挂号"
			t2.Value = 0
		case 3:
			t1.Type = "营业额"
			t1.Name = "住院"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "住院"
			t2.Value = 0
		case 4:
			t1.Type = "营业额"
			t1.Name = "化验"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "化验"
			t2.Value = 0
		case 5:
			t1.Type = "营业额"
			t1.Name = "影像"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "影像"
			t2.Value = 0
		case 6:
			t1.Type = "营业额"
			t1.Name = "处方"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "处方"
			t2.Value = 0
		case 7:
			t1.Type = "营业额"
			t1.Name = "手术"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "手术"
			t2.Value = 0
		case 8:
			t1.Type = "营业额"
			t1.Name = "处置"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "处置"
			t2.Value = 0
		case 9:
			t1.Type = "营业额"
			t1.Name = "美容"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "美容"
			t2.Value = 0
		case 10:
			t1.Type = "营业额"
			t1.Name = "寄养"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "寄养"
			t2.Value = 0
		case 11:
			t1.Type = "营业额"
			t1.Name = "疫苗"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "疫苗"
			t2.Value = 0
		case 12:
			t1.Type = "营业额"
			t1.Name = "驱虫"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "驱虫"
			t2.Value = 0
		}

		if _, ok := tempRes[v]; ok {
			r.Income += tempRes[v]
		}

		response = append(response, t1)
	}

	r.RevenueTodayList = response

	return r, nil
}
