package cygx

import (
	"encoding/json"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/company"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/services"
	"time"
)

// 联系人列表，交流反馈
type UserFeedbackController struct {
	controllers.BaseAuthController
}

// @Title 添加交流反馈接口
// @Description 添加交流反馈接口
// @Param	request	body cygx.AddCygxUserFeedbackReq true "type json string"
// @Success 200 操作成功
// @router /user_feedback/add [post]
func (this *UserFeedbackController) UserFeedbackAdd() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req cygx.AddCygxUserFeedbackReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	userId := req.UserId
	content := req.Content
	if content == "" {
		br.Msg = "内容不能为空！"
		return
	}
	wxUser, err := models.GetWxUserItemByUserId(userId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败, GetWxUserItemByUserId  Err:" + err.Error()
		return
	}
	item := new(cygx.CygxUserFeedback)
	item.Content = content
	item.UserId = wxUser.UserId
	item.Mobile = wxUser.Mobile
	item.Email = wxUser.Email
	item.CompanyId = wxUser.CompanyId
	item.CompanyName = wxUser.CompanyName
	item.RealName = wxUser.RealName
	item.AdminId = sysUser.AdminId
	item.CreateTime = time.Now()
	var newId int64
	newId, err = cygx.AddCygxUserFeedback(item)
	if err != nil {
		br.Msg = "新增失败."
		br.ErrMsg = "新增失败,Err:" + err.Error()
		return
	}

	//权益客户新增历史备注总表
	itemRecord := new(company.CompanyHistoryRemark)
	itemRecord.CompanyId = wxUser.CompanyId
	itemRecord.Content = req.Content
	itemRecord.SysAdminId = sysUser.AdminId
	itemRecord.SysAdminName = sysUser.RealName
	itemRecord.CreateTime = time.Now().Local()
	itemRecord.ModifyTime = time.Now().Local()
	itemRecord.ShowTime = time.Now().Local()
	itemRecord.ProductId = 2
	itemRecord.UserId = wxUser.UserId
	itemRecord.Mobile = wxUser.Mobile
	itemRecord.Email = wxUser.Email
	itemRecord.RealName = wxUser.RealName
	itemRecord.TableName = "cygx_user_feedback"
	itemRecord.TableId = int(newId)
	go services.AddCompanyHistoryRemark(itemRecord)

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// @Title 交流反馈列表接口
// @Description 交流反馈列表接口
// @Param   UserId   query   int	  false       "用户ID"
// @Success 200 {object} cygx.CygxCygxUserFeedbackResp
// @router /user_feedback/list [get]
func (this *RaiServeCoAntroller) UserFeedbackList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	userId, _ := this.GetInt("UserId")
	if userId < 1 {
		br.Msg = "请输入用户ID"
		return
	}
	resp := new(cygx.CygxCygxUserFeedbackResp)
	list, err := cygx.GetCygxUserFeedbackListByUserId(userId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 删除交流反馈接口
// @Description 删除交流反馈接口
// @Param	request	body cygx.CygxUserFeedbackIdReq true "type json string"
// @Success 200 操作成功
// @router /user_feedback/del [post]
func (this *UserFeedbackController) UserFeedbackDel() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req cygx.CygxUserFeedbackIdReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	userFeedbackId := req.UserFeedbackId
	if userFeedbackId < 0 {
		br.Msg = "参数错误！"
		return
	}

	err = cygx.DeleteCygxUserFeedback(userFeedbackId)
	if err != nil {
		br.Msg = "删除失败"
		br.ErrMsg = "删除失败,Err:" + err.Error()
		return
	}

	go services.DelCompanyHistoryRemark("cygx_user_feedback", userFeedbackId) // 删除

	br.Ret = 200
	br.Success = true
	br.Msg = "删除成功"
}
