package overseas_custom

import (
	"encoding/json"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/overseas_custom"
	"hongze/hz_crm_api/utils"
	"time"
)

// 海外客户

type OverseasCustomController struct {
	controllers.BaseAuthController
}

// @Title 保存销售
// @Description 保存销售
// @Param	request  body  overseas_custom.OverseasCustomerSaleSaveReq  true  "type json string"
// @Success Ret=200 操作成功
// @router /sale/save [post]
func (this *OverseasCustomController) SaleSave() {
	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 overseas_custom.OverseasCustomerSaleSaveReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if len(req.AddSellerIdArr) <= 0 && len(req.DelSellerIdArr) <= 0 {
		br.Msg = "参数错误！"
		return
	}
	obj := new(overseas_custom.OverseasCustomerSale)
	list, err := obj.GetOverseasCustomerSaleAll()
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "保存失败！"
		br.ErrMsg = "保存失败,GetOverseasCustomerSaleAll,Err:" + err.Error()
		return
	}

	existMap := make(map[int]string)
	for _, v := range list {
		existMap[v.SellerId] = v.RealName
	}
	if len(req.AddSellerIdArr) > 0 {
		for _, v := range req.AddSellerIdArr {
			if _, ok := existMap[v]; !ok {
				item := new(overseas_custom.OverseasCustomerSale)
				item.SellerId = v
				item.CreateTime = time.Now()
				err = item.Create()
				if err != nil {
					br.Msg = "保存失败！"
					br.ErrMsg = "保存失败,Err:" + err.Error()
					return
				}
				err = obj.HandelOverseasCustomerSale(v, 1)
				if err != nil {
					br.Msg = "保存失败！"
					br.ErrMsg = "保存失败,处理特定销售客户信息失败,Err:" + err.Error()
					return
				}
				//修改客户状态
				err = obj.HandelCompanyStatus(v)
				if err != nil {
					br.Msg = "保存失败！"
					br.ErrMsg = "保存失败,同步客户状态失败,Err:" + err.Error()
					return
				}
			}
		}
	}

	if len(req.DelSellerIdArr) > 0 {
		for _, id := range req.DelSellerIdArr {
			obj.SellerId = id
			err = obj.Delete()
			if err != nil {
				br.Msg = "保存失败！"
				br.ErrMsg = "删除销售失败,Err:" + err.Error()
				return
			}
			err = obj.HandelOverseasCustomerSale(id, 1)
			if err != nil {
				br.Msg = "保存失败！"
				br.ErrMsg = "保存失败,处理特定销售客户信息失败,Err:" + err.Error()
				return
			}
			////修改客户状态
			//err = obj.HandelCompanyStatus(id)
			//if err != nil {
			//	br.Msg = "保存失败！"
			//	br.ErrMsg = "保存失败,同步客户状态失败,Err:" + err.Error()
			//	return
			//}
		}
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "保存成功"
}

// @Title 获取已保存销售列表
// @Description 获取已保存销售列表
// @Success 200 {object} overseas_custom.OverseasCustomerSaleView
// @router /sale/list [get]
func (this *OverseasCustomController) SaleList() {
	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
	}

	obj := new(overseas_custom.OverseasCustomerSale)
	list, err := obj.GetOverseasCustomerSaleAll()
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取失败！"
		br.ErrMsg = "获取失败,GetOverseasCustomerSaleAll,Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = list
}
