company_todo.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package company_todo
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. // CompanyTodo 客户任务 数据表
  7. type CompanyTodo struct {
  8. Id int `orm:"column(id);pk"`
  9. CompanyId int `description:"客户id"`
  10. ProductId int `description:"客户产品id"`
  11. Content string `description:"任务描述"`
  12. SellerId int `description:"客户所属销售id"`
  13. SellerName string `description:"客户所属销售名称"`
  14. CreatedCompanyStatus string `description:"创建时,客户状态"`
  15. CreateUserId int `description:"创建人用户id"`
  16. CreateUserName string `description:"创建人用户姓名"`
  17. ApproveUserId int `description:"审批人用户id"`
  18. ApproveUserName string `description:"审批人用户姓名"`
  19. ApprovedSellerId int `description:"审批时,客户所属销售id"`
  20. ApprovedSellerName string `description:"审批时,客户所属销售名称"`
  21. ApprovedCompanyStatus string `description:"审批时,客户状态"`
  22. Status string `description:"待办单状态,枚举值:进行中,已完成,已作废"`
  23. ModifyTime time.Time `description:"修改时间"`
  24. ApproveTime time.Time `description:"审核时间"`
  25. CreateTime time.Time `description:"创建时间"`
  26. EndTime time.Time `description:"截止时间"`
  27. IsDelete uint8 `json:"-" description:"是否已经删除,0:未删除,1:已删除;默认:0"`
  28. Remark string `description:"审批备注"`
  29. }
  30. type CompanyTodoListItem struct {
  31. *CompanyTodo
  32. EndTimeStr string `description:"格式化后的截止时间"`
  33. }
  34. // Update 客户任务 数据表
  35. func (companyTodo *CompanyTodo) Update(cols []string) (err error) {
  36. o := orm.NewOrm()
  37. _, err = o.Update(companyTodo, cols...)
  38. return
  39. }
  40. // AddCompanyTodo 新增任务请求
  41. func AddCompanyTodo(item *CompanyTodo) (lastId int64, err error) {
  42. o := orm.NewOrm()
  43. to, err := o.Begin()
  44. if err != nil {
  45. return
  46. }
  47. defer func() {
  48. if err != nil {
  49. _ = to.Rollback()
  50. } else {
  51. _ = to.Commit()
  52. }
  53. }()
  54. lastId, err = to.Insert(item)
  55. if err != nil {
  56. return
  57. }
  58. //更新客户产品的状态
  59. sql := `UPDATE company_product SET todo_status='未完成',todo_create_time=NOW(),todo_modify_time=NOW(),modify_time=NOW(),todo_end_time=? WHERE company_id=? AND product_id=? `
  60. _, err = to.Raw(sql, item.EndTime, item.CompanyId, item.ProductId).Exec()
  61. return
  62. }
  63. // EditCompanyTodo 编辑任务
  64. func (companyTodo *CompanyTodo) EditCompanyTodo(cols []string) (err error) {
  65. o := orm.NewOrm()
  66. to, err := o.Begin()
  67. if err != nil {
  68. return
  69. }
  70. defer func() {
  71. if err != nil {
  72. _ = to.Rollback()
  73. } else {
  74. _ = to.Commit()
  75. }
  76. }()
  77. _, err = to.Update(companyTodo, cols...)
  78. //更新客户产品的状态
  79. //sql := `UPDATE company_product SET todo_status='未完成',modify_time=NOW() WHERE company_id=? AND product_id=? `
  80. //_, err = to.Raw(sql, companyTodo.CompanyId, companyTodo.ProductId).Exec()
  81. return
  82. }
  83. // ApproveCompanyTodo 审批通过任务
  84. func (companyTodo *CompanyTodo) ApproveCompanyTodo(cols []string) (err error) {
  85. o := orm.NewOrm()
  86. to, err := o.Begin()
  87. if err != nil {
  88. return
  89. }
  90. defer func() {
  91. if err != nil {
  92. _ = to.Rollback()
  93. } else {
  94. _ = to.Commit()
  95. }
  96. }()
  97. _, err = to.Update(companyTodo, cols...)
  98. //更新客户产品的状态
  99. sql := `UPDATE company_product SET todo_status='已完成',todo_approve_time=NOW(),todo_modify_time=NOW(),modify_time=NOW() WHERE company_id=? AND product_id=? `
  100. _, err = to.Raw(sql, companyTodo.CompanyId, companyTodo.ProductId).Exec()
  101. return
  102. }
  103. // GetCountDoingCompanyTodo 获取正在进行中的任务数量
  104. func GetCountDoingCompanyTodo(companyId, productId int) (total int, err error) {
  105. o := orm.NewOrm()
  106. sql := `select count(1) as total from company_todo where status="进行中" and company_id = ? and product_id =? AND is_delete=0 `
  107. err = o.Raw(sql, companyId, productId).QueryRow(&total)
  108. return
  109. }
  110. // GetCompanyTodoById 根据任务id获取客户任务
  111. func GetCompanyTodoById(id int) (item *CompanyTodo, err error) {
  112. o := orm.NewOrm()
  113. sql := `select * from company_todo where id = ? `
  114. err = o.Raw(sql, id).QueryRow(&item)
  115. return
  116. }
  117. // GetCompanyTodoList 获取客户任务列表
  118. func GetCompanyTodoList(companyId, productId int) (items []*CompanyTodo, err error) {
  119. o := orm.NewOrm()
  120. var pars []interface{}
  121. sql := `select a.* from company_todo a join company_product b on a.company_id=b.company_id and a.product_id=b.product_id and a.seller_id=b.seller_id where a.company_id = ? and a.status !="已作废" and a.is_delete=0 `
  122. pars = append(pars, companyId)
  123. //如果有传入产品id,那么只查询该类型下的产品
  124. if productId != 0 {
  125. sql += ` and a.product_id = ? `
  126. pars = append(pars, productId)
  127. }
  128. sql += ` order by a.create_time asc`
  129. _, err = o.Raw(sql, pars...).QueryRows(&items)
  130. return
  131. }
  132. // GetCompanyTodoListV2 获取客户任务列表
  133. func GetCompanyTodoListV2(condition string, pars []interface{}) (items []*CompanyTodo, err error) {
  134. o := orm.NewOrm()
  135. sql := `select * from company_todo where 1 = 1 `
  136. sql += condition + ` order by create_time asc`
  137. _, err = o.Raw(sql, pars...).QueryRows(&items)
  138. return
  139. }
  140. // GetDoingCompanyTodoList 获取客户的进行中任务列表
  141. func GetDoingCompanyTodoList(companyId, productId int) (items []*CompanyTodo, err error) {
  142. o := orm.NewOrm()
  143. var pars []interface{}
  144. sql := `select a.* from company_todo a join company_product b on a.company_id=b.company_id and a.product_id=b.product_id and a.seller_id=b.seller_id where a.status="进行中" and a.company_id = ? and a.is_delete=0 `
  145. pars = append(pars, companyId)
  146. //如果有传入产品id,那么只查询该类型下的产品
  147. if productId != 0 {
  148. sql += ` and a.product_id = ? `
  149. pars = append(pars, productId)
  150. }
  151. sql += ` order by a.product_id asc,a.create_time asc `
  152. _, err = o.Raw(sql, pars...).QueryRows(&items)
  153. return
  154. }
  155. // GetDoingCompanyTodoByCompanyIds 根据客户id集合字符串获取所有客户待办的任务列表
  156. func GetDoingCompanyTodoByCompanyIds(companyIds, condition string, pars []interface{}) (items []*CompanyTodo, err error) {
  157. if companyIds == `` {
  158. return
  159. }
  160. sql := `SELECT * FROM company_todo WHERE status="进行中" and company_id in (` + companyIds + `) and is_delete=0 `
  161. sql += condition
  162. sql += ` ORDER BY id ASC `
  163. o := orm.NewOrm()
  164. _, err = o.Raw(sql, pars...).QueryRows(&items)
  165. return
  166. }
  167. type CompanyTodoReportRecordGroup struct {
  168. AdminId int `description:"所属销售id"`
  169. AdminName string `description:"所属销售名称"`
  170. Num int `description:"汇总次数"`
  171. CompanyIds string `description:"客户id字符串"`
  172. }
  173. // GetDoingGroupCompanyReportRecordGroupList 获取销售分组数据(进行中)
  174. func GetDoingGroupCompanyReportRecordGroupList(condition string, pars []interface{}) (list []*CompanyTodoReportRecordGroup, err error) {
  175. o := orm.NewOrm()
  176. //sql := ` SELECT DISTINCT b.id, a.seller_id as admin_id,a.seller_name admin_name,count(1) num,GROUP_CONCAT(DISTINCT a.company_id SEPARATOR ',') AS company_ids
  177. // FROM company_report_record a join company_todo b on a.company_id=b.company_id and a.product_id=b.product_id
  178. // WHERE 1=1
  179. //`
  180. //if condition != "" {
  181. // sql += condition
  182. //}
  183. //sql += ` GROUP BY a.seller_id`
  184. //sql := ` SELECT a.seller_id as admin_id,a.seller_name as admin_name,count(1) num,GROUP_CONCAT(DISTINCT a.company_id SEPARATOR ',') AS company_ids
  185. // FROM company_todo a
  186. //join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  187. // WHERE 1=1
  188. //`
  189. sql := ` SELECT b.seller_id as admin_id,b.seller_name as admin_name,count(1) num,GROUP_CONCAT(DISTINCT a.company_id SEPARATOR ',') AS company_ids
  190. FROM company_todo a
  191. join company_product b on a.company_id=b.company_id and a.product_id=b.product_id and a.seller_id =b.seller_id
  192. WHERE 1=1 AND a.is_delete=0
  193. `
  194. if condition != "" {
  195. sql += condition
  196. }
  197. sql += ` GROUP BY admin_id`
  198. _, err = o.Raw(sql, pars).QueryRows(&list)
  199. return
  200. }
  201. // GetFinishedGroupCompanyReportRecordGroupList 获取销售分组数据(已完成)
  202. func GetFinishedGroupCompanyReportRecordGroupList(condition string, pars []interface{}) (list []*CompanyTodoReportRecordGroup, err error) {
  203. o := orm.NewOrm()
  204. //sql := ` SELECT a.approved_seller_id as admin_id,a.approved_seller_name as admin_name,count(1) num,GROUP_CONCAT(DISTINCT a.company_id SEPARATOR ',') AS company_ids
  205. // FROM company_todo a
  206. //join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  207. // WHERE 1=1
  208. //`
  209. sql := ` SELECT b.seller_id as admin_id,b.seller_name as admin_name,count(1) num,GROUP_CONCAT(DISTINCT a.company_id SEPARATOR ',') AS company_ids
  210. FROM company_todo a
  211. join company_product b on a.company_id=b.company_id and a.product_id=b.product_id and a.approved_seller_id=b.seller_id
  212. WHERE 1=1 AND a.is_delete=0
  213. `
  214. if condition != "" {
  215. sql += condition
  216. }
  217. sql += ` GROUP BY admin_id`
  218. _, err = o.Raw(sql, pars).QueryRows(&list)
  219. return
  220. }
  221. // CompanyTodoAddReq 新增任务请求
  222. type CompanyTodoAddReq struct {
  223. CompanyId int `description:"客户id"`
  224. ProductId int `description:"产品id,ficc传:1,权益传:2"`
  225. Description string `description:"任务描述"`
  226. EndTime string `description:"截止时间"`
  227. }
  228. // CompanyTodoEditReq 编辑任务请求
  229. type CompanyTodoEditReq struct {
  230. Id int `description:"客户任务记录id"`
  231. CompanyId int `description:"客户id"`
  232. ProductId int `description:"产品id,ficc传:1,权益传:2"`
  233. Description string `description:"任务描述"`
  234. EndTime string `description:"截止时间"`
  235. }
  236. // ApproveCompanyTodoReq 审批任务请求
  237. type ApproveCompanyTodoReq struct {
  238. Id int `description:"客户任务记录id"`
  239. Remark string `description:"审批备注"`
  240. }