package cygx

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

// 分析师管理
type IndustrialAnalystController struct {
	controllers.BaseAuthController
}

// @Title 添加分析师
// @Description 添加分析师接口
// @Param	request	body cygx.IndustrialAnalystAdd true "type json string"
// @Success Ret=200 添加分析师成功
// @router /industrialAnalyst/add [post]
func (this *IndustrialAnalystController) IndustrialAnalystAdd() {
	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.IndustrialAnalystAdd
	var pars []interface{}
	var condition string
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.AnalystName == "" {
		br.Msg = "请输分析师名称"
		return
	}
	condition = `AND analyst_name = '` + req.AnalystName + `' AND industrial_management_id = ` + strconv.Itoa(req.IndustrialManagementId)
	total, _ := cygx.GetIndustrialAnalystCount(condition, pars)
	if total > 0 {
		br.Msg = "名称已经存在，请重新填写"
		br.ErrMsg = "名称已经存在，请重新填写:" + req.AnalystName
		return
	}
	condition = `AND industrial_management_id = ` + strconv.Itoa(req.IndustrialManagementId)
	totalIndustrialManagement, _ := cygx.GetIndustrialManagementCount(condition, pars)
	if totalIndustrialManagement < 1 {
		br.Msg = "产业分类名称不存在，请重新填写"
		br.ErrMsg = "产业分类名称不存在，请重新填写:" + strconv.Itoa(req.IndustrialManagementId)
		return
	}
	item := new(cygx.CygxIndustrialAnalyst)
	item.IndustrialManagementId = req.IndustrialManagementId
	item.AnalystName = req.AnalystName
	item.CreateTime = time.Now()
	err = cygx.AddIndustrialAnalyst(item)
	if err != nil {
		br.Msg = "新增失败"
		br.ErrMsg = "新增失败 Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "新增成功"
	br.IsAddLog = true
}

// @Title 获取分析师列表
// @Description 获取分析师列表接口
// @Param   IndustrialManagementId   query   int  true       "分类ID"
// @Success Ret=200  {object} cygx.GetIndustrialAnalystList
// @router /industrialAnalyst/list [get]
func (this *IndustrialAnalystController) IndustrialAnalystlist() {
	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
	}
	IndustrialManagementId, _ := this.GetInt("IndustrialManagementId")
	if IndustrialManagementId < 1 {
		br.Msg = "请输入分类ID"
		return
	}
	list, err := cygx.GetIndustrialAnalystAll(IndustrialManagementId)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
		return
	}
	resp := new(cygx.GetIndustrialAnalystList)
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 修改
// @Description 修改接口
// @Param	request	body cygx.CygxIndustrialAnalyst true "type json string"
// @Success Ret=200 修改成功
// @router /industrialAnalyst/edit [post]
func (this *IndustrialAnalystController) IndustrialAnalystEdit() {
	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.CygxIndustrialAnalyst
	var pars []interface{}
	var condition string
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.AnalystName == "" {
		br.Msg = "请输入名称"
		return
	}

	condition = `AND industrial_analyst_id = ` + strconv.Itoa(req.IndustrialAnalystId)
	totalIndustrialAnalyst, _ := cygx.GetIndustrialAnalystCount(condition, pars)
	if totalIndustrialAnalyst < 1 {
		br.Msg = "修改失败"
		br.ErrMsg = "修改失败，信息不存在:"
		return
	}
	condition = `AND industrial_management_id = ` + strconv.Itoa(req.IndustrialManagementId)
	totalIndustrialManagement, _ := cygx.GetIndustrialManagementCount(condition, pars)
	if totalIndustrialManagement < 1 {
		br.Msg = "修改失败"
		br.ErrMsg = "修改失败，分析师不存在"
		return
	}
	condition = `AND analyst_name = '` + req.AnalystName + `'   AND  industrial_analyst_id != ` + strconv.Itoa(req.IndustrialAnalystId)
	total, _ := cygx.GetIndustrialAnalystCount(condition, pars)
	if total > 0 {
		br.Msg = "名称已经存在，请重新填写"
		br.ErrMsg = "名称已经存在，请重新填写:" + req.AnalystName
		return
	}
	item := new(cygx.CygxIndustrialAnalyst)
	item.IndustrialManagementId = req.IndustrialManagementId
	item.AnalystName = req.AnalystName
	item.IndustrialAnalystId = req.IndustrialAnalystId
	err = cygx.EditIndustrialAnalyst(item)
	if err != nil {
		br.Msg = "修改失败"
		br.ErrMsg = "修改失败 Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "修改成功"
	br.IsAddLog = true
}

// @Title 删除分析师
// @Description 删除分析师接口
// @Param   IndustrialAnalystId   query   int  true       "分析师ID"
// @Success Ret=200
// @router /industrialAnalyst/delete [post]
func (this *IndustrialAnalystController) IndustrialAnalystDelete() {
	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.IndustrialAnalystDelete
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	IndustrialAnalystId := req.IndustrialAnalystId
	if IndustrialAnalystId < 1 {
		br.Msg = "请输入分析师ID"
		return
	}
	err = cygx.DeleteIndustrialAnalyst(IndustrialAnalystId)
	if err != nil {
		br.Msg = "删除信息失败"
		br.ErrMsg = "删除信息失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "删除成功"
	br.IsAddLog = true
}
