package CheckYbjc

import (
	"git.listensoft.net/tool/jspkit/common"
	"git.listensoft.net/tool/jspkit/common/models"
	"git.listensoft.net/tool/jspkit/common/models/sb"
	"math"
	"reflect"
	"strings"
)

func CheckSmallVat(vat []sb.GsSmallVat, basevat []sb.GsSmallVat, hw bool) (status int, errLog string) {
	status = 1
	var errLogs []string
	// 强制校验
	{
		index1 := []string{"本期数-货物及劳务", "本期数-服务、不动产和无形资产", "本年累计-货物及劳务", "本年累计-服务、不动产和无形资产"}
		for k, v := range index1 {
			//if hw {
			//	if k == 1 || k == 3 {
			//		continue
			//	}
			//} else {
			//	if k == 0 || k == 2 {
			//		continue
			//	}
			//}
			if Tran(basevat[k].Yzzzsbhsxse) < Tran(vat[k].Yzzzsbhsxse) {
				errLogs = append(errLogs, v+"-应征增值税不含税销售额（3%征收率）不一致税局为:税局为"+vat[k].Yzzzsbhsxse+"系统为:"+basevat[k].Yzzzsbhsxse)
			}
			if Tran(basevat[k].Swjgdkdzzszyfpbhsxse) < Tran(vat[k].Swjgdkdzzszyfpbhsxse) {
				errLogs = append(errLogs, v+"-增值税专用发票不含税销售额:税局为"+vat[k].Swjgdkdzzszyfpbhsxse+"系统为:"+basevat[k].Swjgdkdzzszyfpbhsxse)
			}
			if Tran(basevat[k].Skqjkjdptfpbhsxse) < Tran(vat[k].Skqjkjdptfpbhsxse) {
				errLogs = append(errLogs, v+"-其他增值税发票不含税销售额:税局为"+vat[k].Skqjkjdptfpbhsxse+"系统为:"+basevat[k].Skqjkjdptfpbhsxse)
			}
			//if Tran(basevat[k].Xsczbdcbhsxse) != Tran(vat[k].Xsczbdcbhsxse) {
			//	errLogs = append(errLogs, v+"-（二）应征增值税不含税销售额（5%征收率）:税局为"+vat[k].Xsczbdcbhsxse+"系统为:"+basevat[k].Xsczbdcbhsxse)
			//}
			//if Tran(basevat[k].Swjgdkdzzszyfpbhsxse1) != Tran(vat[k].Swjgdkdzzszyfpbhsxse1) {
			//	errLogs = append(errLogs, v+"-增值税专用发票不含税销售额:税局为"+vat[k].Swjgdkdzzszyfpbhsxse1+"系统为:"+basevat[k].Swjgdkdzzszyfpbhsxse1)
			//}
			//if Tran(basevat[k].Skqjkjdptfpbhsxse2) != Tran(vat[k].Skqjkjdptfpbhsxse2) {
			//	errLogs = append(errLogs, v+"-其他增值税发票不含税销售额:税局为"+vat[k].Skqjkjdptfpbhsxse2+"系统为:"+basevat[k].Skqjkjdptfpbhsxse2)
			//}

			if Tran(basevat[k].Msxse) < Tran(vat[k].Msxse) {
				errLogs = append(errLogs, v+"-免税销售额:税局为"+vat[k].Msxse+"系统为:"+basevat[k].Msxse)
			}
			if Tran(basevat[k].Xwqymsxse) < Tran(vat[k].Xwqymsxse) {
				errLogs = append(errLogs, v+"-小微企业免税销售额:税局为"+vat[k].Xwqymsxse+"系统为:"+basevat[k].Xwqymsxse)
			}
			if Tran(basevat[k].Wdqzdxse) < Tran(vat[k].Wdqzdxse) {
				errLogs = append(errLogs, v+"-未达起征点免税销售额:税局为"+vat[k].Wdqzdxse+"系统为:"+basevat[k].Wdqzdxse)
			}
			if Tran(basevat[k].Qtmsxse) < Tran(vat[k].Qtmsxse) {
				errLogs = append(errLogs, v+"-其他免税销售额:税局为"+vat[k].Qtmsxse+"系统为:"+basevat[k].Qtmsxse)
			}

			if math.Abs(Tran(basevat[k].Bqynsejze)-Tran(vat[k].Bqynsejze)) > 0.5 { // 五毛误差
				errLogs = append(errLogs, v+"-本期应纳税额减征额:税局为"+vat[k].Bqynsejze+"系统为:"+basevat[k].Bqynsejze)
			}
			//if Tran(basevat[k].Bqmse) < Tran(vat[k].Bqmse) {
			//	errLogs = append(errLogs, v+"-本期免税额:税局为"+vat[k].Bqmse+"系统为:"+basevat[k].Bqmse)
			//}
			//if Tran(basevat[k].Xwqymse) < Tran(vat[k].Xwqymse) {
			//	errLogs = append(errLogs, v+"-小微企业免税额:税局为"+vat[k].Xwqymse+"系统为:"+basevat[k].Xwqymse)
			//}
			//if Tran(basevat[k].Wdqzdmse) < Tran(vat[k].Wdqzdmse) {
			//	errLogs = append(errLogs, v+"-未达起征点免税额:税局为"+vat[k].Wdqzdmse+"系统为:"+basevat[k].Wdqzdmse)
			//}
			//if Tran(basevat[k].Bqybtse) != Tran(vat[k].Bqybtse) {
			//	errLogs = append(errLogs, v+"-本期应补（退）税额:税局为"+vat[k].Bqybtse+"系统为:"+basevat[k].Bqybtse)
			//}
			//zaplog.LoggerS.Info(Tran(basevat[k].Cjs), Tran(vat[k].Cjs))
			//if Tran(basevat[k].Cjs) != Tran(vat[k].Cjs) {
			//	errLogs = append(errLogs, v+"-城市维护建设税本期应补（退）税额:税局为"+vat[k].Cjs+"系统为:"+basevat[k].Cjs)
			//}
			//if Tran(basevat[k].Jyf) != Tran(vat[k].Jyf) {
			//	errLogs = append(errLogs, v+"-教育费附加本期应补（退）费额:税局为"+vat[k].Jyf+"系统为:"+basevat[k].Jyf)
			//}
			//if Tran(basevat[k].Dfjyf) != Tran(vat[k].Dfjyf) {
			//	errLogs = append(errLogs, v+"-地方教育附加本期应补（退）费额:税局为"+vat[k].Dfjyf+"系统为:"+basevat[k].Dfjyf)
			//}
			//if Tran(basevat[k].Bqyjse1) != Tran(vat[k].Bqyjse1) {
			//	errLogs = append(errLogs, v+"本期预缴税额:税局为"+vat[k].Bqyjse1+"系统为:"+basevat[k].Bqyjse1)
			//}
		}
	}
	// 普通检验

	if len(errLogs) > 0 {
		status = 3
		errLog = strings.Join(errLogs, ",")
	}
	return status, errLog
}

func CheckSmallAttach2(Attach2 []models.GsVatFjs, baseAttach2 []models.GsVatFjs) (status int, errLog string) {
	status = 1
	// 非强制校验
	{
		var KeyNAme = []string{
			"Zzsse",
			"Bqybtse",
		}
		for _, v1 := range Attach2 {
			for _, v2 := range baseAttach2 {
				if v1.ZsxmDm == v2.ZsxmDm {
					r1 := reflect.ValueOf(v1)
					r2 := reflect.ValueOf(v2)
					for _, key_name := range KeyNAme {
						if common.StrToFloat(r1.FieldByName(key_name).String()) != common.StrToFloat(r2.FieldByName(key_name).String()) {
							status = 2
							break
						}
					}
					if status == 2 {
						break
					}
				}
			}
			if status == 2 {
				break
			}
		}
	}
	var errLogs []string
	if len(errLogs) > 0 {
		status = 3
		errLog = strings.Join(errLogs, ",")
	}
	return status, errLog
}

func CheckSmallJm(jm []sb.GsSmallVatReduce, Jm []sb.GsSmallVatReduce) (status int, errLog string) {
	status = 1
	var errLogs []string
	var jsxm1 []sb.GsSmallVatReduce
	for _, v := range jm {
		if v.Type == 1 && (common.StrToFloat(v.Qcye) != 0 || common.StrToFloat(v.Qmye) != 0 || common.StrToFloat(v.Bqfse) != 0 || common.StrToFloat(v.Bqydjse) != 0 || common.StrToFloat(v.Bqsjdjse) != 0) {
			jsxm1 = append(jsxm1, v)
		}
	}

	var jsxm2 []sb.GsSmallVatReduce
	for _, v := range Jm {
		if v.Type == 1 && (common.StrToFloat(v.Qcye) != 0 || common.StrToFloat(v.Qmye) != 0 || common.StrToFloat(v.Bqfse) != 0 || common.StrToFloat(v.Bqydjse) != 0 || common.StrToFloat(v.Bqsjdjse) != 0) {
			jsxm2 = append(jsxm2, v)
		}
	}

	// 非强制校验
	{
		// 减税项目
		if len(jsxm1) != len(jsxm2) {
			status = 2
			//errLogs = append(errLogs, fmt.Sprintf("税局减税项目有%d条系统有%d条", len(jsxm1), len(jsxm2)))
		}
		for _, v1 := range jsxm1 {
			find := false
			for _, v2 := range jsxm2 {
				if v1.SbJmxzdmJmHm == v2.SbJmxzdmJmHm {
					find = true
					if common.StrToFloat(v1.Qcye) == common.StrToFloat(v2.Qcye) && common.StrToFloat(v1.Qmye) == common.StrToFloat(v2.Qmye) && common.StrToFloat(v1.Bqfse) == common.StrToFloat(v2.Bqfse) && common.StrToFloat(v1.Bqydjse) == common.StrToFloat(v2.Bqydjse) {
						status = 1
					} else {
						status = 2
					}
					break
				}

			}
			if !find {
				status = 2
				//errLogs = append(errLogs, fmt.Sprintf("税局减税项目:%s 为在系统中找到,请核实", len(jsxm1), len(jsxm2)))
			}
		}
	}
	if len(errLogs) > 0 {
		status = 3
		errLog = strings.Join(errLogs, ",")
	}
	return
}

func Tran(a string) float64 {
	if a == "" {
		return 0
	}
	return common.StrToFloat(strings.TrimSpace(strings.ReplaceAll(a, ",", "")))
}
