partner_info_controller.go 7.7 KB
package controllers

import (
	"crypto/sha1"
	"errors"
	"fmt"
	"strconv"
	"time"

	"github.com/astaxie/beego/logs"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"

	partnerQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/query"

	partnerInfoCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/command"
	partnerInfoService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/service"
)

type PartnerInfoController struct {
	BaseController
}

////Prepare 重写 BaseController 的Prepare方法
func (c *PartnerInfoController) Prepare() {
	c.BaseController.Prepare()
	if ok := c.ValidJWTToken(); !ok {
		return
	}
	if ok := c.ValidAdminPermission(domain.PERMINSSION_PARTNER); !ok {
		return
	}
}

//CreatePartnerInfo 创建合伙人
func (c *PartnerInfoController) CreatePartnerInfo() {
	//用与适配前端定义的数据结构
	type Parameter struct {
		PartnerName     string `json:"partnerName"`
		PartnerType     int    `json:"partnerType"`
		Area            string `json:"area"`
		Account         string `json:"account"`
		State           int    `json:"state"`
		CooperationTime string `json:"cooperationTime"`
		SalesmanName    string `json:"salesmanName"`
		Phone           string `json:"phone"`
	}
	var (
		param Parameter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	defaultPwd := fmt.Sprintf("%x", sha1.Sum([]byte("123456")))
	cooperateTime, err := time.ParseInLocation("2006-01-02", param.CooperationTime, time.Local)
	if err != nil {
		e := lib.ThrowError(lib.ARG_ERROR, err.Error())
		c.ResponseError(e)
		return
	}
	cmd := partnerInfoCmd.CreatePartnerInfoCommand{
		PartnerName:     param.PartnerName,
		Account:         param.Account,
		Password:        defaultPwd,
		Status:          param.State,
		PartnerCategory: param.PartnerType,
		CooperateTime:   cooperateTime,
		Salesman: []domain.Salesman{
			domain.Salesman{
				Name:      param.SalesmanName,
				Telephone: param.Phone,
			},
		},
	}
	if len(param.Area) > 0 {
		cmd.RegionInfo = &domain.RegionInfo{
			RegionName: param.Area,
		}
	}
	serve := partnerInfoService.NewPartnerInfoService(nil)
	_, err = serve.CreatePartnerInfo(&cmd)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}

//UpdatePartnerInfo 更新合伙人
func (c *PartnerInfoController) UpdatePartnerInfo() {
	//用与适配前端定义的数据结构
	type Parameter struct {
		ID              int64  `json:"id"`
		PartnerType     int    `json:"partnerType"`
		Area            string `json:"area"`
		State           int    `json:"state"`
		CooperationTime string `json:"cooperationTime"`
		SalesmanName    string `json:"salesmanName"`
		Phone           string `json:"phone"`
	}
	var (
		param Parameter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	cooperateTime, err := time.ParseInLocation("2006-01-02", param.CooperationTime, time.Local)
	if err != nil {
		e := lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
		c.ResponseError(e)
		return
	}
	cmd := partnerInfoCmd.UpdatePartnerInfoCommand{
		Id:              param.ID,
		Status:          param.State,
		PartnerCategory: param.PartnerType,
		CooperateTime:   cooperateTime,
		RegionInfo: &domain.RegionInfo{
			RegionName: param.Area,
		},
		Salesman: []domain.Salesman{
			domain.Salesman{
				Name:      param.SalesmanName,
				Telephone: param.Phone,
			},
		},
	}
	serve := partnerInfoService.NewPartnerInfoService(nil)
	err = serve.UpdatePartnerInfo(&cmd)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}

//GetPartnerInfo 获取合伙人
func (c *PartnerInfoController) GetPartnerInfo() {
	//用与适配前端定义的数据结构
	type Parameter struct {
		Id int64 `json:"id"`
	}
	var (
		param Parameter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	newPartnerService := partnerInfoService.NewPartnerInfoService(nil)
	partnerInfo, err := newPartnerService.GetPartnerInfo(partnerQuery.GetPartnerInfoQuery{
		Id: param.Id,
	})
	if err != nil {
		c.ResponseError(err)
		return
	}
	//数据适配
	rspResult := map[string]interface{}{
		"account":         partnerInfo.Account,
		"partnerName":     partnerInfo.PartnerName,
		"partnerType":     partnerInfo.PartnerCategory,
		"area":            partnerInfo.RegionInfo.RegionName,
		"salesmanName":    "",
		"phone":           "",
		"createTime":      partnerInfo.CreateAt.Local().Format("2006-01-02 15:04:05"),
		"cooperationTime": partnerInfo.CooperateTime.Local().Format("2006-01-02"),
		"state":           partnerInfo.Status,
		"id":              partnerInfo.Id,
	}
	if len(partnerInfo.Salesman) > 0 {
		rspResult["salesmanName"] = partnerInfo.Salesman[0].Name
		rspResult["phone"] = partnerInfo.Salesman[0].Telephone
	}
	c.ResponseData(rspResult)
	return
}

//PartnerInfoSetState 合伙人禁用启用
func (c *PartnerInfoController) PartnerInfoSetState() {
	//用与适配前端定义的数据结构
	type Parameter struct {
		Id     int64 `json:"id"`
		Status int   `json:"status"`
	}
	var (
		param Parameter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}

	cmd := partnerInfoCmd.StatusPartnerInfoCommand{
		Id: param.Id,
	}
	if param.Status == 1 {
		//禁用操作
		cmd.Status = domain.PARTNER_STATUS_NO
	} else if param.Status == 2 {
		//启用操作
		cmd.Status = domain.PARTNER_STATUS_YES
	} else {
		//错误
		c.ResponseError(lib.ThrowError(lib.ARG_ERROR, "状态错误"))
		return
	}
	newPartnerService := partnerInfoService.NewPartnerInfoService(nil)
	err = newPartnerService.UpdateStatus(cmd)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}

//ListPartnerInfo 合伙人列表
func (c *PartnerInfoController) ListPartnerInfo() {
	type Parameter struct {
		Partnertype string `json:"partnerType"`
		Area        string `json:"area"`
		PartnerName string `json:"partnerName"`
		PageSize    int    `json:"pageSize"`
		PageNumber  int    `json:"pageNumber"`
	}
	var (
		param Parameter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	partnertype, _ := strconv.Atoi(param.Partnertype)
	if param.PageNumber == 0 {
		param.PageNumber = 1
	}
	if param.PageSize == 0 {
		param.PageSize = 20
	}
	query := partnerQuery.ListPartnerInfoQuery{
		Partnertype: partnertype,
		PartnerName: param.PartnerName,
		RegionInfo:  param.Area,
		Limit:       param.PageSize,
		Offset:      (param.PageNumber - 1) * param.PageSize,
	}

	newPartnerService := partnerInfoService.NewPartnerInfoService(nil)
	count, partners, err := newPartnerService.ListPartnerInfo(&query)
	if err != nil {
		c.ResponseError(err)
		return
	}
	resp := []map[string]interface{}{}
	for i := range partners {
		m := map[string]interface{}{
			"id":              partners[i].Id,
			"account":         partners[i].Account,
			"partnerName":     partners[i].PartnerName,
			"area":            partners[i].RegionInfo.RegionName,
			"createTime":      partners[i].CreateAt.Local().Format("2006-01-02 15:04:05"),
			"cooperationTime": partners[i].CooperateTime.Local().Format("2006-01-02"),
			"state":           partners[i].Status,
			"partnerType":     partners[i].PartnerCategory,
			"salesmanName":    "",
			"phone":           "",
		}
		if len(partners[i].Salesman) > 0 {
			m["salesmanName"] = partners[i].Salesman[0].Name
			m["phone"] = partners[i].Salesman[0].Telephone
		}
		resp = append(resp, m)
	}
	c.ResponsePageList(resp, count, param.PageNumber)
	return
}