| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package base
- import (
- "context"
- "fmt"
- "strconv"
- "github.com/gogf/gf/errors/gerror"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/text/gstr"
- "github.com/gogf/gf/util/gconv"
- "dashoo.cn/micro/app/dao/base"
- model "dashoo.cn/micro/app/model/base"
- "dashoo.cn/micro/app/service"
- )
- type distributorService struct {
- *service.ContextService
- Dao *base.BaseDistributorDao
- }
- func NewDistributorService(ctx context.Context) (svc *distributorService, err error) {
- svc = new(distributorService)
- if svc.ContextService, err = svc.Init(ctx); err != nil {
- return nil, err
- }
- svc.Dao = base.NewBaseDistributorDao(svc.Tenant)
- return svc, nil
- }
- //经销商信息列表
- func (d *distributorService) GetList(req *model.BaseDistributorSearchReq) (total int, distributorList []*model.DistributorRonp, err error) {
- distributorModel := d.Dao.M
- distributorModel = distributorModel.Where(d.Dao.Columns.DeletedTime + " is null")
- if req.DistCode != "" {
- distributorModel = distributorModel.Where(d.Dao.Columns.DistCode+"like ?", "%"+req.DistCode+"%")
- }
- if req.DistName != "" {
- distributorModel = distributorModel.Where(d.Dao.Columns.DistName+"like ?", "%"+req.DistName+"%")
- }
- if req.BelongSale != "" {
- distributorModel = distributorModel.Where(d.Dao.Columns.BelongSale+"like ?", "%"+req.BelongSale+"%")
- }
- if req.ProvinceId > 0 {
- distributorModel = distributorModel.Where(d.Dao.Columns.ProvinceId+" in (?)", req.ProvinceId)
- }
- g.Log().Info("搜索条件", req.ProvinceId)
- total, err = distributorModel.Count()
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("获取总行数失败")
- return
- }
- if req.PageNum == 0 {
- req.PageNum = 1
- }
- err = distributorModel.Page(req.PageNum, req.PageSize).Order("id desc").Scan(&distributorList)
- g.Log().Info("返回列表", distributorList)
- return
- }
- //经销商创建
- func (d *distributorService) Create(req *model.AddDistributor) (err error) {
- DistributorData := new(model.BaseDistributor)
- if err = gconv.Struct(req, DistributorData); err != nil {
- return
- }
- service.SetCreatedInfo(DistributorData, d.GetCxtUserId(), d.GetCxtUserName())
- Model := d.Dao.M
- DistributorData.DistCode = gstr.SubStr(strconv.Itoa(int(gtime.Now().UnixNano()/1e6))+"Code", 0, -5)
- res, err := Model.Insert(DistributorData)
- if err != nil {
- return
- }
- InsertId, _ := res.LastInsertId()
- fmt.Println(InsertId)
- return
- }
- //修改
- //修改数据
- func (p *distributorService) UpdateById(req *model.UpdateDistributorReq) (err error) {
- //uptime := gtime.New(time.Now())
- Model := p.Dao.M
- record, err := Model.FindOne("Id", req.Id)
- if err != nil || record.IsEmpty() {
- err = gerror.New("该数据不存在")
- return err
- }
- proInfo := record.Map()
- fmt.Println(proInfo["created_time"])
- distData := new(model.BaseDistributor)
- if err = gconv.Struct(req, distData); err != nil {
- return
- }
- service.SetUpdatedInfo(distData, p.GetCxtUserId(), p.GetCxtUserName())
- _, err = Model.FieldsEx(p.Dao.Columns.DistCode, p.Dao.Columns.Id, p.Dao.Columns.CreatedName, p.Dao.Columns.CreatedBy, p.Dao.Columns.CreatedTime).WherePri(p.Dao.Columns.Id, req.Id).Update(distData)
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("修改信息失败")
- return
- }
- return
- }
- //删除
- func (p *distributorService) DeleteByIds(req int) (err error) {
- Model := p.Dao.M
- distributor := new(model.BaseDistributor)
- err = Model.Where(base.BaseProduct.Columns.Id, req).Scan(&distributor)
- g.Log().Info("DeleteByIds", distributor)
- if err != nil || distributor.Id == 0 {
- g.Log().Error(err)
- err = gerror.New("没有要删除的数据")
- return
- }
- distributor.DeletedTime = gtime.Now()
- _, err = Model.FieldsEx(EgionDetailFieldEx).
- WherePri(p.Dao.Columns.Id, req).Update(distributor)
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("删除数据失败")
- return err
- }
- return
- }
|