partner_info_controller.go 10.0 KB
package controllers

import (
	"crypto/sha1"
	"errors"
	"fmt"
	"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     []*domain.PartnerCategory `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"`
		Remark          string                    `json:"remark"`
	}
	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
	}
	companyId := c.GetUserCompany()
	cmd := partnerInfoCmd.CreatePartnerInfoCommand{
		PartnerName:     param.PartnerName,
		Account:         param.Account,
		Password:        defaultPwd,
		Status:          param.State,
		PartnerCategory: param.PartnerType,
		CooperateTime:   cooperateTime,
		CompanyId:       companyId,
		Remark:          param.Remark,
	}
	if len(param.SalesmanName) > 0 || len(param.Phone) > 0 {
		cmd.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)
	newPartner, err := serve.CreatePartnerInfo(&cmd)
	if err != nil {
		c.ResponseError(err)
		return
	}
	if newPartner != nil {
		c.ResponseData(map[string]interface{}{
			"id": newPartner.Partner.Id,
		})
		return
	}
	c.ResponseData(nil)
	return
}

//UpdatePartnerInfo 更新合伙人
func (c *PartnerInfoController) UpdatePartnerInfo() {
	//用与适配前端定义的数据结构
	type Parameter struct {
		ID              int64                     `json:"id"`
		PartnerName     string                    `json:"partnerName"`
		PartnerType     []*domain.PartnerCategory `json:"partnerType"`
		Area            string                    `json:"area"`
		State           int                       `json:"state"`
		CooperationTime string                    `json:"cooperationTime"`
		SalesmanName    string                    `json:"salesmanName"`
		Phone           string                    `json:"phone"`
		Remark          string                    `json:"remark"`
	}
	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
	}
	companyId := c.GetUserCompany()
	cmd := partnerInfoCmd.UpdatePartnerInfoCommand{
		Id:              param.ID,
		PartnerName:     param.PartnerName,
		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,
			},
		},
		CompanyId: companyId,
		Remark:    param.Remark,
	}
	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
	}
	companyId := c.GetUserCompany()
	newPartnerService := partnerInfoService.NewPartnerInfoService(nil)
	partnerInfo, err := newPartnerService.GetPartnerInfo(partnerQuery.GetPartnerInfoQuery{
		Id:        param.Id,
		CompanyId: companyId,
	})
	if err != nil {
		c.ResponseError(err)
		return
	}
	//数据适配
	rspResult := map[string]interface{}{
		"account":     partnerInfo.Partner.Account,
		"partnerName": partnerInfo.Partner.PartnerName,
		// "partnerType":     []map[string]interface{}{},
		"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.Partner.Id,
		"remark":          partnerInfo.Remark,
	}
	if len(partnerInfo.Salesman) > 0 {
		rspResult["salesmanName"] = partnerInfo.Salesman[0].Name
		rspResult["phone"] = partnerInfo.Salesman[0].Telephone
	}
	var partnerTypes []map[string]interface{}
	for _, v := range partnerInfo.PartnerCategoryInfos {
		m := map[string]interface{}{
			"id":   v.Id,
			"name": v.Name,
			"code": v.Code,
		}
		partnerTypes = append(partnerTypes, m)
	}
	rspResult["partnerType"] = partnerTypes
	c.ResponseData(rspResult)
	return
}

/**
 * @Author SteveChan
 * @Description // 移除合伙人
 * @Date 15:31 2020/12/29
 * @Param
 * @return
 **/
func (c *PartnerInfoController) RemovePartnerInfo() {
	//用与适配前端定义的数据结构
	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
	}
	cmd := partnerInfoCmd.RemovePartnerInfoCommand{
		Id: param.ID,
	}
	serve := partnerInfoService.NewPartnerInfoService(nil)
	err = serve.RemovePartnerInfo(cmd)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	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
	}
	companyId := c.GetUserCompany()
	var cmd partnerInfoCmd.StatusPartnerInfoCommand
	switch param.Status {
	case 0:
		cmd = partnerInfoCmd.StatusPartnerInfoCommand{
			Ids:       param.Id,
			CompanyId: companyId,
			Status:    domain.PARTNER_STATUS_NO,
		}
	case 1:
		cmd = partnerInfoCmd.StatusPartnerInfoCommand{
			Ids:       param.Id,
			CompanyId: companyId,
			Status:    domain.PARTNER_STATUS_YES,
		}
	default:
		c.ResponseError(errors.New("参数错误"))
		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 int    `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
	}

	if param.PageNumber == 0 {
		param.PageNumber = 1
	}
	if param.PageSize == 0 {
		param.PageSize = 20
	}
	companyId := c.GetUserCompany()
	query := partnerQuery.ListPartnerInfoQuery{
		PartnerType: param.PartnerType,
		PartnerName: param.PartnerName,
		RegionInfo:  param.Area,
		Limit:       param.PageSize,
		Offset:      (param.PageNumber - 1) * param.PageSize,
		CompanyId:   companyId,
	}

	newPartnerService := partnerInfoService.NewPartnerInfoService(nil)
	count, partners, err := newPartnerService.ListPartnerInfo(&query)
	if err != nil {
		c.ResponseError(err)
		return
	}
	var resp []map[string]interface{}
	indexBegin := query.Offset
	for i := range partners {
		m := map[string]interface{}{
			"index":           indexBegin + (i + 1),
			"id":              partners[i].Partner.Id,
			"account":         partners[i].Partner.Account,
			"partnerName":     partners[i].Partner.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].PartnerCategoryInfos,
			"salesmanName":    "",
			"phone":           "",
			"remark":          partners[i].Remark,
		}
		if len(partners[i].Salesman) > 0 {
			m["salesmanName"] = partners[i].Salesman[0].Name
			m["phone"] = partners[i].Salesman[0].Telephone
		}
		resp = append(resp, m)
	}
	if len(resp) == 0 {
		resp = []map[string]interface{}{}
	}
	c.ResponsePageList(resp, count, param.PageNumber)
	return
}