package mobile

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"time"
)

func PointsAddByMobile(c *gin.Context) {
	var req request.PointsLogInsertReq
	_ = c.ShouldBindJSON(&req)

	var err error
	cTime := time.Now()

	if req.Phone == "" {
		response.ApiFailWithMessage(2002, "医生手机号不得为空", c)
		return
	}

	if service.IsPointsLog(req.OtherNo) > 0 {
		response.ApiFailWithMessage(2001, "数据重复", c)
		return
	}

	userData := service.FindUserByMobile(req.Phone)

	pointsNum := new(request.PointsNumByMobileReply)
	pointsNum.HospitalNum, pointsNum.DoctorNum = service.GetPointsLogNum(req.HospitalCode, int(userData.ID))

	if pointsNum.HospitalNum > 1 {

	}

	if pointsNum.DoctorNum > 1 {

	}

	//积分流水
	reqPointsLog := model.PointsLog{}
	reqPointsLog.HospitalCode = req.HospitalCode
	reqPointsLog.UserId = int(userData.ID)
	reqPointsLog.ChangeType = 2
	reqPointsLog.ChangeTime = cTime
	reqPointsLog.ChangeNum = 100
	reqPointsLog.NowPointsNum = userData.PointsNum + reqPointsLog.ChangeNum
	reqPointsLog.Remark = "提交金像奖病历"
	reqPointsLog.OtherNo = req.OtherNo
	reqPointsLog.CreateTime = cTime
	if req.Source < 2 {
		reqPointsLog.Source = 1
	}

	err = service.CreatePointsLog(reqPointsLog)
	if err != nil {
		global.GVA_LOG.Error("赠送失败!", zap.Any("err", err))
		response.FailWithMessage("赠送失败", c)
		return
	}

	//修改积分
	err = service.UpdateUserPoints(int(userData.ID), 1, reqPointsLog.ChangeNum)

	//用户操作日志
	apiLog := model.ApiLog{
		UserId: userData.ID,
		Api:    "/points/addByMobile",
		Req:    string(utils.SerializeJson(req)),
		Reply:  "",
	}
	service.CreateApiLog(apiLog)
	//fmt.Println(reply)
	response.ApiOk(c)
	return
}

func PointsNumByMobile(c *gin.Context) {
	var req request.PointsLogInsertReq
	_ = c.ShouldBindJSON(&req)

	if req.Phone == "" {
		response.ApiFailWithMessage(2002, "医生手机号不得为空", c)
		return
	}

	userData := service.FindUserByMobile(req.Phone)

	reply := new(request.PointsNumByMobileReply)
	reply.HospitalNum, reply.DoctorNum = service.GetPointsLogNum(req.HospitalCode, int(userData.ID))

	//用户操作日志
	apiLog := model.ApiLog{
		UserId: userData.ID,
		Api:    "/points/pointsDataByMobile",
		Req:    string(utils.SerializeJson(req)),
		Reply:  string(utils.SerializeJson(reply)),
	}
	service.CreateApiLog(apiLog)
	//fmt.Println(reply)
	response.ApiOkWithData(reply, c)
	return
}

func GetPointsList(c *gin.Context) {
	var req request.GetPointsLogListReq
	_ = c.ShouldBindJSON(&req)

	userId := getUserID(c)
	req.UserId = int(userId)
	err, list, total := service.GetPointsLogList(req)

	_, userData := service.GetUserInfo(userId)

	pointsData := request.PointsData{}
	pointsData.PointsTotal = userData.PointsNum
	pointsData.PointsIncomeTotal = service.GetPointsData(int(userId), 2)
	pointsData.PointsExpendTotal = service.GetPointsData(int(userId), 3)

	reply := request.GetPointsLogListReply{}
	reply.List = list
	reply.Total = total
	reply.PointsData = pointsData

	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(reply, c)
	}
	return
}
