package repository

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

	"github.com/uniplaces/carbon"
)

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

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

	// 计算时间 及格式化的模板
	var formatSQL, formatTime string
	var sub int

	if len(t) == 10 {
		formatSQL = "%Y-%m-%d"
		formatTime = "2006-01-02"
		sub = 6
	} else if len(t) == 7 {
		formatSQL = "%Y-%m"
		formatTime = "2006-01"
		sub = 6
	} else if len(t) == 4 {
		formatSQL = "%Y"
		formatTime = "2006"
		sub = 3
	} else {
		return nil, errors.New("time error")
	}

	c, err := carbon.Parse(formatTime, t, "UTC")
	if err != nil {
		return nil, err
	}

	var endT *carbon.Carbon

	if len(t) == 10 {
		endT = c.SubDays(sub)
	} else if len(t) == 7 {
		endT = c.SubMonths(sub)
	} else if len(t) == 4 {
		endT = c.SubYears(sub)
	} else {
		return nil, errors.New("time error")
	}

	var response models.BusinessTrend

	response.List = make([]models.BusinessTrendList, 0)
	response.Chart = make([]models.LineChart, 0)

	// 构造list 数据
	for i := 0; i <= sub; i++ {
		var str string
		if len(t) == 10 {
			str = endT.AddDays(i).Format(formatTime)
		} else if len(t) == 7 {
			str = endT.AddMonths(i).Format(formatTime)
		} else {
			str = endT.AddYears(i).Format(formatTime)
		}

		var temp models.BusinessTrendList

		temp.Time = str

		//1 - 首页营业额:
		//按已结账单 + 次卡充值+赠送金额结算 （不计算次卡消费）

		billOfSettlement, err := BillOfSettlement(db, str, formatSQL, hospitalLocalId)
		if err != nil {
			return nil, err
		}

		meterCardRecharge, err := MeterCardRecharge(db, str, formatSQL, 0, hospitalLocalId)
		if err != nil {
			return nil, err
		}

		temp.Turnover = billOfSettlement + meterCardRecharge

		//3、毛利
		//毛利=营业额-成本
		billByCost, err := BillByCost(db, str, formatSQL, hospitalLocalId, chainCode)
		if err != nil {
			return nil, err
		}

		temp.Grossprofit = temp.Turnover - billByCost

		//2 - 首页支出:
		//1. 退押金
		retreatDepositMoney, err := RetreatDepositMoney(db, str, formatSQL, hospitalLocalId)
		if err != nil {
			return nil, err
		}

		//2. 退会员卡
		retreatConsumerCard, err := RetreatConsumerCard(db, str, formatSQL, hospitalLocalId)
		if err != nil {
			return nil, err
		}

		//3. 退次卡
		retreatMeterCard, err := RetreatMeterCard(db, str, formatSQL, hospitalLocalId)
		if err != nil {
			return nil, err
		}

		//4. 支出管理中的
		outChargeBill, err := OutChargeBill(db, str, formatSQL, hospitalLocalId)
		if err != nil {
			return nil, err
		}

		//5. 预付款
		preOutFundBill, err := PreOutFundBill(db, str, formatSQL, hospitalLocalId)
		if err != nil {
			return nil, err
		}

		//6. 入库结算
		stockInBill, err := StockInBill(db, str, formatSQL, hospitalLocalId)
		if err != nil {
			return nil, err
		}

		temp.Expend = retreatDepositMoney + retreatConsumerCard + retreatMeterCard + outChargeBill + preOutFundBill + stockInBill

		// 支出减掉 退预付款的金额
		//RetreatPreOutFundBill
		retreatPreOutFundBill, err := RetreatPreOutFundBill(db, str, formatSQL, hospitalLocalId)
		if err != nil {
			return nil, err
		}

		temp.Expend -= retreatPreOutFundBill

		response.List = append(response.List, temp)

		// 当天
		if t == str {
			response.Turnover = temp.Turnover
			response.Grossprofit = temp.Grossprofit
			response.Expend = temp.Expend
		}

		var c1 models.LineChart
		var c2 models.LineChart
		var c3 models.LineChart
		c1.Year = str
		c1.Type = "营业额"
		c1.Value = fmt.Sprintf("%.2f", temp.Turnover)
		c2.Year = str
		c2.Type = "毛利"
		c2.Value = fmt.Sprintf("%.2f", temp.Grossprofit)
		c3.Year = str
		c3.Type = "支出"
		c3.Value = fmt.Sprintf("%.2f", temp.Expend)

		response.Chart = append(response.Chart, c1, c2, c3)
	}

	fmt.Println(time.Now())

	return response, nil
}
