base_distributor_target.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package base
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. dao "dashoo.cn/micro/app/dao/base"
  8. contractdao "dashoo.cn/micro/app/dao/contract"
  9. model "dashoo.cn/micro/app/model/base"
  10. "dashoo.cn/opms_libary/micro_srv"
  11. "dashoo.cn/opms_libary/myerrors"
  12. "dashoo.cn/opms_libary/request"
  13. "github.com/gogf/gf/os/gtime"
  14. "github.com/gogf/gf/util/gvalid"
  15. )
  16. type BaseDistributorTargetService struct {
  17. Dao *dao.BaseDistributorTargetDao
  18. ContractDao *contractdao.CtrContractDao
  19. Tenant string
  20. userInfo request.UserInfo
  21. }
  22. func NewBaseDistributorTargetService(ctx context.Context) (*BaseDistributorTargetService, error) {
  23. tenant, err := micro_srv.GetTenant(ctx)
  24. if err != nil {
  25. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  26. }
  27. // 获取用户信息
  28. userInfo, err := micro_srv.GetUserInfo(ctx)
  29. if err != nil {
  30. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  31. }
  32. return &BaseDistributorTargetService{
  33. Dao: dao.NewBaseDistributorTargetDao(tenant),
  34. ContractDao: contractdao.NewCtrContractDao(tenant),
  35. Tenant: tenant,
  36. userInfo: userInfo,
  37. }, nil
  38. }
  39. func (s BaseDistributorTargetService) List(ctx context.Context, req *model.BaseDistributorTargetListReq) (int, []*model.BaseDistributorTargetListRsp, error) {
  40. dao := &s.Dao.BaseDistributorTargetDao
  41. if req.DistId != 0 {
  42. dao = dao.Where("dist_id = ?", req.DistId)
  43. }
  44. total, err := dao.Count()
  45. if err != nil {
  46. return 0, nil, err
  47. }
  48. if req.PageNum != 0 {
  49. dao = dao.Page(req.GetPage())
  50. }
  51. orderby := "created_time desc"
  52. if req.OrderBy != "" {
  53. orderby = req.OrderBy
  54. }
  55. dao = dao.Order(orderby)
  56. ents := []*model.BaseDistributorTargetListRsp{}
  57. err = dao.Structs(&ents)
  58. if err != nil && err != sql.ErrNoRows {
  59. return 0, nil, err
  60. }
  61. for i, dist := range ents {
  62. statistic, err := s.statistic(dist.Id)
  63. if err != nil {
  64. return 0, nil, err
  65. }
  66. ents[i].BaseDistributorTargetStatistic = statistic
  67. }
  68. return total, ents, nil
  69. }
  70. func (s *BaseDistributorTargetService) statistic(id int) (stat model.BaseDistributorTargetStatistic, err error) {
  71. now := time.Now()
  72. ctr, err := s.ContractDao.
  73. Where("appro_status = '30'").
  74. Where("DATE_FORMAT(created_time, '%Y') = ?", now.Year()).
  75. Where("distributor_id = ?", id).All()
  76. var q1, q2, q3, q4 float64
  77. for _, c := range ctr {
  78. m := c.CreatedTime.Month()
  79. if m == 1 || m == 2 || m == 3 {
  80. q1 += c.ContractAmount
  81. }
  82. if m == 4 || m == 5 || m == 6 {
  83. q2 += c.ContractAmount
  84. }
  85. if m == 7 || m == 8 || m == 9 {
  86. q3 += c.ContractAmount
  87. }
  88. if m == 10 || m == 11 || m == 12 {
  89. q4 += c.ContractAmount
  90. }
  91. }
  92. stat.Q1Amount = q1 / 10000
  93. stat.Q2Amount = q2 / 10000
  94. stat.Q3Amount = q3 / 10000
  95. stat.Q4Amount = q4 / 10000
  96. stat.TotalAmount = q1 + q2 + q3 + q4
  97. return
  98. }
  99. func (s BaseDistributorTargetService) Add(ctx context.Context, req *model.BaseDistributorTargetAddReq) (int, error) {
  100. validErr := gvalid.CheckStruct(ctx, req, nil)
  101. if validErr != nil {
  102. return 0, myerrors.TipsError(validErr.Current().Error())
  103. }
  104. t, err := s.Dao.Where("dist_id = ? and year = ?", req.DistId, req.Year).One()
  105. if err != nil {
  106. return 0, err
  107. }
  108. if t != nil {
  109. return 0, myerrors.TipsError(fmt.Sprintf("%d 年指标已存在", req.Year))
  110. }
  111. id, err := s.Dao.InsertAndGetId(model.BaseDistributorTarget{
  112. DistId: req.DistId,
  113. Year: req.Year,
  114. Q1: req.Q1,
  115. Q2: req.Q2,
  116. Q3: req.Q3,
  117. Q4: req.Q4,
  118. Total: req.Q1 + req.Q2 + req.Q3 + req.Q4,
  119. Remark: req.Remark,
  120. CreatedBy: int(s.userInfo.Id),
  121. CreatedName: s.userInfo.NickName,
  122. CreatedTime: gtime.Now(),
  123. UpdatedBy: int(s.userInfo.Id),
  124. UpdatedName: s.userInfo.NickName,
  125. UpdatedTime: gtime.Now(),
  126. })
  127. if err != nil {
  128. return 0, err
  129. }
  130. return int(id), err
  131. }
  132. func (s BaseDistributorTargetService) Update(ctx context.Context, req *model.BaseDistributorTargetUpdateReq) error {
  133. validErr := gvalid.CheckStruct(ctx, req, nil)
  134. if validErr != nil {
  135. return myerrors.TipsError(validErr.Current().Error())
  136. }
  137. ent, err := s.Dao.Where("id = ?", req.Id).One()
  138. if err != nil {
  139. return err
  140. }
  141. if ent == nil {
  142. return myerrors.TipsError(fmt.Sprintf("指标不存在: %d", req.Id))
  143. }
  144. dao := &s.Dao.BaseDistributorTargetDao
  145. toupdate := map[string]interface{}{}
  146. q1 := ent.Q1
  147. q2 := ent.Q2
  148. q3 := ent.Q3
  149. q4 := ent.Q4
  150. if req.Q1 != nil {
  151. toupdate["q1"] = *req.Q1
  152. q1 = *req.Q1
  153. }
  154. if req.Q2 != nil {
  155. toupdate["q2"] = *req.Q2
  156. q2 = *req.Q2
  157. }
  158. if req.Q3 != nil {
  159. toupdate["q3"] = *req.Q3
  160. q3 = *req.Q3
  161. }
  162. if req.Q4 != nil {
  163. toupdate["q4"] = *req.Q4
  164. q4 = *req.Q4
  165. }
  166. if req.Remark != nil {
  167. toupdate["remark"] = *req.Remark
  168. }
  169. if len(toupdate) != 0 {
  170. toupdate["total"] = q1 + q2 + q3 + q4
  171. toupdate["updated_by"] = int(s.userInfo.Id)
  172. toupdate["updated_name"] = s.userInfo.NickName
  173. toupdate["updated_time"] = gtime.Now()
  174. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  175. if err != nil {
  176. return err
  177. }
  178. }
  179. return nil
  180. }
  181. func (s BaseDistributorTargetService) Delete(ctx context.Context, id []int) error {
  182. if len(id) == 0 {
  183. return nil
  184. }
  185. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  186. return err
  187. }