| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- package base
- import (
- "context"
- "database/sql"
- "fmt"
- "time"
- dao "dashoo.cn/micro/app/dao/base"
- contractdao "dashoo.cn/micro/app/dao/contract"
- model "dashoo.cn/micro/app/model/base"
- "dashoo.cn/opms_libary/micro_srv"
- "dashoo.cn/opms_libary/myerrors"
- "dashoo.cn/opms_libary/request"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gvalid"
- )
- type BaseDistributorTargetService struct {
- Dao *dao.BaseDistributorTargetDao
- ContractDao *contractdao.CtrContractDao
- Tenant string
- userInfo request.UserInfo
- }
- func NewBaseDistributorTargetService(ctx context.Context) (*BaseDistributorTargetService, error) {
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
- }
- // 获取用户信息
- userInfo, err := micro_srv.GetUserInfo(ctx)
- if err != nil {
- return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
- }
- return &BaseDistributorTargetService{
- Dao: dao.NewBaseDistributorTargetDao(tenant),
- ContractDao: contractdao.NewCtrContractDao(tenant),
- Tenant: tenant,
- userInfo: userInfo,
- }, nil
- }
- func (s BaseDistributorTargetService) List(ctx context.Context, req *model.BaseDistributorTargetListReq) (int, []*model.BaseDistributorTargetListRsp, error) {
- dao := &s.Dao.BaseDistributorTargetDao
- if req.DistId != 0 {
- dao = dao.Where("dist_id = ?", req.DistId)
- }
- total, err := dao.Count()
- if err != nil {
- return 0, nil, err
- }
- if req.PageNum != 0 {
- dao = dao.Page(req.GetPage())
- }
- orderby := "created_time desc"
- if req.OrderBy != "" {
- orderby = req.OrderBy
- }
- dao = dao.Order(orderby)
- ents := []*model.BaseDistributorTargetListRsp{}
- err = dao.Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- for i, dist := range ents {
- statistic, err := s.statistic(dist.Id)
- if err != nil {
- return 0, nil, err
- }
- ents[i].BaseDistributorTargetStatistic = statistic
- }
- return total, ents, nil
- }
- func (s *BaseDistributorTargetService) statistic(id int) (stat model.BaseDistributorTargetStatistic, err error) {
- now := time.Now()
- ctr, err := s.ContractDao.
- Where("appro_status = '30'").
- Where("DATE_FORMAT(created_time, '%Y') = ?", now.Year()).
- Where("distributor_id = ?", id).All()
- var q1, q2, q3, q4 float64
- for _, c := range ctr {
- m := c.CreatedTime.Month()
- if m == 1 || m == 2 || m == 3 {
- q1 += c.ContractAmount
- }
- if m == 4 || m == 5 || m == 6 {
- q2 += c.ContractAmount
- }
- if m == 7 || m == 8 || m == 9 {
- q3 += c.ContractAmount
- }
- if m == 10 || m == 11 || m == 12 {
- q4 += c.ContractAmount
- }
- }
- stat.Q1Amount = q1 / 10000
- stat.Q2Amount = q2 / 10000
- stat.Q3Amount = q3 / 10000
- stat.Q4Amount = q4 / 10000
- stat.TotalAmount = q1 + q2 + q3 + q4
- return
- }
- func (s BaseDistributorTargetService) Add(ctx context.Context, req *model.BaseDistributorTargetAddReq) (int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return 0, myerrors.TipsError(validErr.Current().Error())
- }
- t, err := s.Dao.Where("dist_id = ? and year = ?", req.DistId, req.Year).One()
- if err != nil {
- return 0, err
- }
- if t != nil {
- return 0, myerrors.TipsError(fmt.Sprintf("%d 年指标已存在", req.Year))
- }
- id, err := s.Dao.InsertAndGetId(model.BaseDistributorTarget{
- DistId: req.DistId,
- Year: req.Year,
- Q1: req.Q1,
- Q2: req.Q2,
- Q3: req.Q3,
- Q4: req.Q4,
- Total: req.Q1 + req.Q2 + req.Q3 + req.Q4,
- Remark: req.Remark,
- CreatedBy: int(s.userInfo.Id),
- CreatedName: s.userInfo.NickName,
- CreatedTime: gtime.Now(),
- UpdatedBy: int(s.userInfo.Id),
- UpdatedName: s.userInfo.NickName,
- UpdatedTime: gtime.Now(),
- })
- if err != nil {
- return 0, err
- }
- return int(id), err
- }
- func (s BaseDistributorTargetService) Update(ctx context.Context, req *model.BaseDistributorTargetUpdateReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.TipsError(validErr.Current().Error())
- }
- ent, err := s.Dao.Where("id = ?", req.Id).One()
- if err != nil {
- return err
- }
- if ent == nil {
- return myerrors.TipsError(fmt.Sprintf("指标不存在: %d", req.Id))
- }
- dao := &s.Dao.BaseDistributorTargetDao
- toupdate := map[string]interface{}{}
- q1 := ent.Q1
- q2 := ent.Q2
- q3 := ent.Q3
- q4 := ent.Q4
- if req.Q1 != nil {
- toupdate["q1"] = *req.Q1
- q1 = *req.Q1
- }
- if req.Q2 != nil {
- toupdate["q2"] = *req.Q2
- q2 = *req.Q2
- }
- if req.Q3 != nil {
- toupdate["q3"] = *req.Q3
- q3 = *req.Q3
- }
- if req.Q4 != nil {
- toupdate["q4"] = *req.Q4
- q4 = *req.Q4
- }
- if req.Remark != nil {
- toupdate["remark"] = *req.Remark
- }
- if len(toupdate) != 0 {
- toupdate["total"] = q1 + q2 + q3 + q4
- toupdate["updated_by"] = int(s.userInfo.Id)
- toupdate["updated_name"] = s.userInfo.NickName
- toupdate["updated_time"] = gtime.Now()
- _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (s BaseDistributorTargetService) Delete(ctx context.Context, id []int) error {
- if len(id) == 0 {
- return nil
- }
- _, err := s.Dao.Where("Id IN (?)", id).Delete()
- return err
- }
|