company_apply.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package company
  2. import (
  3. "errors"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  6. "time"
  7. )
  8. // 申请服务更新请求参数
  9. type CompanyApplyServiceUpdateReq struct {
  10. CompanyId int `description:"客户id"`
  11. CompanyApprovalId int `description:"申请单id,没有传0"`
  12. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  13. CompanyType string `description:"客户类型,ficc/权益"`
  14. StartDate string `description:"合同开始日期"`
  15. EndDate string `description:"合同结束日期"`
  16. Money float64 `description:"合同金额"`
  17. PayMethod string `description:"付款方式"`
  18. PayChannel string `description:"付款渠道"`
  19. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  20. ImgUrl string `description:"合同图片,多个用英文#隔开"`
  21. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  22. RaiPackageType int `description:"权益套餐类型: 0-无; 1-70w套餐; 2-45w套餐"`
  23. Points float64 `description:"研选扣点包点数"`
  24. }
  25. type ApproveUser struct {
  26. AdminId int
  27. RealName string
  28. Mobile string
  29. }
  30. // 获取审批人
  31. func GetApproveUser(roleTypeCode string) (items []*ApproveUser, err error) {
  32. o := orm.NewOrm()
  33. sql := `SELECT admin_id,real_name,mobile FROM admin WHERE role_type_code=? AND admin_name<>'qshi' `
  34. _, err = o.Raw(sql, roleTypeCode).QueryRows(&items)
  35. return
  36. }
  37. type CompanyContractHistory struct {
  38. CompanyContractId int `json:"-" description:"合同id"`
  39. CompanyId int `description:"客户id"`
  40. ContractCode string `description:"合同编码"`
  41. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  42. StartDate string `description:"合同开始时间"`
  43. EndDate string `description:"合同结束时间"`
  44. Money float64 `description:"合同金额"`
  45. PayMethod string `description:"支付方式"`
  46. PayChannel string `description:"支付渠道"`
  47. ImgUrl string `description:"合同图片,多个用#隔开"`
  48. CreateTime time.Time `description:"合同创建时间"`
  49. ModifyTime time.Time `description:"合同修改时间";json:"-"`
  50. ModifyTimeStr string `description:"合同修改时间"`
  51. Status int `description:"状态"`
  52. ProductId int `description:"产品id"`
  53. ContractId int `description:"合同ID"`
  54. RaiPackageType int `description:"权益套餐类型: 0-无; 1-70w套餐; 2-45w套餐"`
  55. PermissionList []*ContractPermissionList
  56. }
  57. type CompanyContractHistoryResp struct {
  58. List []*CompanyContractHistory
  59. }
  60. func GetCompanyContractHistoryList(companyId int, productId string) (items []*CompanyContractHistory, err error) {
  61. o := orm.NewOrm()
  62. //sql := `SELECT * FROM company_contract WHERE company_id=? AND product_id IN(` + productId + `) AND status="1" ORDER BY modify_time DESC `
  63. sql := `SELECT
  64. a.*,
  65. b.contract_id
  66. FROM
  67. company_contract AS a
  68. LEFT JOIN contract AS b ON a.contract_code = b.contract_code
  69. WHERE
  70. a.company_id = ? AND a.product_id IN (` + productId + `) AND a.status = 1
  71. ORDER BY
  72. a.modify_time DESC`
  73. _, err = o.Raw(sql, companyId).QueryRows(&items)
  74. return
  75. }
  76. func GetCompanyContractHistoryListByContractCode(contractCode string) (items []*CompanyContractHistory, err error) {
  77. o := orm.NewOrm()
  78. sql := `SELECT
  79. a.*,
  80. b.contract_id
  81. FROM
  82. company_contract AS a
  83. LEFT JOIN contract AS b ON a.contract_code = b.contract_code
  84. WHERE
  85. a.contract_code = ? AND a.status = 1
  86. ORDER BY
  87. a.modify_time DESC`
  88. _, err = o.Raw(sql, contractCode).QueryRows(&items)
  89. return
  90. }
  91. type CompanyContractDetail struct {
  92. CompanyContractId int
  93. CompanyId int `description:"客户id"`
  94. ProductId int `description:"产品id"`
  95. ContractCode string `description:"合同编码"`
  96. ContractType string `description:"合同类型:枚举值:'新签合同','续约合同','补充协议'"`
  97. StartDate string `description:"合同开始时间"`
  98. EndDate string `description:"合同结束时间"`
  99. Quarter string `description:"季度(X类试用客户使用)"`
  100. Money float64 `description:"合同金额"`
  101. PayMethod string `description:"支付方式"`
  102. PayChannel string `description:"支付渠道"`
  103. ImgUrl string `description:"合同图片,多个用#隔开"`
  104. CreateTime time.Time `description:"合同创建时间"`
  105. ModifyTime time.Time `description:"合同修改时间"`
  106. Status int `description:"状态"`
  107. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  108. RaiPackageType int `description:"权益套餐类型: 0-无; 1-70w套餐; 2-45w套餐"`
  109. Source string `description:"合同来源:上传附件, 系统合同"`
  110. SourceTag string `description:"合同来源标签:非标合同,标准合同"`
  111. AllocationType int `description:"分配方式,0手动分配,1X录分之后自动分配,默认0"`
  112. PermissionList []*PermissionLookList
  113. }
  114. type CompanyContractDetailResp struct {
  115. Item *CompanyContractDetail
  116. }
  117. func GetCompanyContractDetail(companyId, productId, companyContractId int) (item *CompanyContractDetail, err error) {
  118. o := orm.NewOrm()
  119. if companyContractId == 0 {
  120. if productId <= 0 {
  121. err = errors.New("客户类型异常")
  122. }
  123. sql := `SELECT * FROM company_contract WHERE company_id=? AND product_id=? AND status=0 `
  124. err = o.Raw(sql, companyId, productId).QueryRow(&item)
  125. } else {
  126. sql := `SELECT * FROM company_contract WHERE company_contract_id=? `
  127. err = o.Raw(sql, companyContractId).QueryRow(&item)
  128. }
  129. return
  130. }
  131. // 获取客户大于今天的最后一份有效合同
  132. func GetCompanyLastContractDetail(companyId, productId int) (item *CompanyContractDetail, err error) {
  133. o := orm.NewOrm()
  134. endDate := utils.GetToday(utils.FormatDate)
  135. sql := `SELECT * FROM company_contract WHERE company_id=? AND product_id=? AND status=1 AND end_date >= ? AND contract_type in ("新签合同","续约合同") order by end_date desc limit 1`
  136. err = o.Raw(sql, companyId, productId, endDate).QueryRow(&item)
  137. return
  138. }
  139. // 最后一次
  140. type ContractPermissionLookItemRes struct {
  141. ChartPermissionId int `description:"权限id"`
  142. PermissionName string `description:"权限名称"`
  143. StartDate time.Time `description:"权限开始日期"`
  144. EndDate time.Time `description:"权限结束日期"`
  145. //Status string `description:"'正式','试用','关闭'"`
  146. //ExpireDay string `description:"到期天数"`
  147. ClassifyName string `description:"分类"`
  148. }
  149. // 获取客户大于今天的所有有效合同
  150. func GetCompanyWillContractList(companyId, productId int) (list []*CompanyContractDetail, err error) {
  151. o := orm.NewOrm()
  152. endDate := utils.GetToday(utils.FormatDate)
  153. sql := `SELECT * FROM company_contract WHERE company_id=? AND product_id=? AND status=1 AND end_date >= ? AND contract_type in ("新签合同","续约合同") order by end_date desc`
  154. _, err = o.Raw(sql, companyId, productId, endDate).QueryRows(&list)
  155. return
  156. }
  157. func GetCompanyContractApproveCount(companyId, productId int) (count int, err error) {
  158. o := orm.NewOrm()
  159. sql := `SELECT COUNT(1) AS count FROM company_contract WHERE company_id=? AND product_id=? AND status=0 `
  160. err = o.Raw(sql, companyId, productId).QueryRow(&count)
  161. return
  162. }
  163. type CompanyApplyApproveReq struct {
  164. CompanyId int `description:"客户id"`
  165. Status int `description:"审批状态,1:通过,2:拒绝"`
  166. Remark string `description:"审批理由"`
  167. CompanyContractId int `description:"合同id"`
  168. ProductId int `description:"客户产品id"`
  169. }
  170. // 审批通过
  171. func ApproveAgree(companyId, productId int, approveStatus, approveRemark, startDate, endDate string, applyMethod int) (err error) {
  172. o := orm.NewOrm()
  173. to, err := o.Begin()
  174. if err != nil {
  175. return
  176. }
  177. defer func() {
  178. if err != nil {
  179. _ = to.Rollback()
  180. } else {
  181. _ = to.Commit()
  182. }
  183. }()
  184. sql := `UPDATE company_product
  185. SET
  186. approve_status = ?,
  187. approve_time = NOW(),
  188. approve_remark = ?,
  189. start_date=?,
  190. end_date=?,
  191. modify_time=NOW()
  192. WHERE company_id = ? AND product_id=? `
  193. _, err = to.Raw(sql, approveStatus, approveRemark, startDate, endDate, companyId, productId).Exec()
  194. if err != nil {
  195. return
  196. }
  197. if applyMethod == 6 {
  198. sql := `UPDATE company_contract
  199. SET
  200. status = 1,
  201. modify_time=NOW()
  202. WHERE company_id = ? AND product_id=? `
  203. _, err = to.Raw(sql, companyId, productId).Exec()
  204. }
  205. return
  206. }
  207. // 审批拒绝
  208. func ApproveRefuse(companyId, productId int, approveStatus, approveRemark string) (err error) {
  209. o := orm.NewOrm()
  210. to, err := o.Begin()
  211. if err != nil {
  212. return
  213. }
  214. defer func() {
  215. if err != nil {
  216. _ = to.Rollback()
  217. } else {
  218. _ = to.Commit()
  219. }
  220. }()
  221. sql := `UPDATE company_product
  222. SET
  223. approve_status = '驳回',
  224. modify_time=NOW()
  225. WHERE company_id = ? AND product_id=? `
  226. _, err = to.Raw(sql, companyId, productId).Exec()
  227. if err != nil {
  228. return
  229. }
  230. sql = ` UPDATE company_approval
  231. SET
  232. approve_status = '驳回',
  233. approve_remark=?,
  234. approve_time=NOW(),
  235. modify_time=NOW()
  236. WHERE company_id = ? AND product_id=? AND approve_status='待审批' `
  237. _, err = to.Raw(sql, approveRemark, companyId, productId).Exec()
  238. return
  239. }
  240. // 申请转正请求参数
  241. type CompanyApplyTurnPositiveReq struct {
  242. CompanyId int
  243. CompanyApprovalId int `description:"申请单id,没有传0"`
  244. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  245. StartDate string `description:"合同开始日期"`
  246. EndDate string `description:"合同结束日期"`
  247. Money float64 `description:"合同金额"`
  248. PayMethod string `description:"付款方式"`
  249. PayChannel string `description:"付款渠道"`
  250. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  251. PermissionNames string `description:"权限名称,多个用英文逗号隔开"`
  252. ImgUrl string `description:"合同图片,多个用英文#隔开"`
  253. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  254. RaiPackageType int `description:"权益套餐类型: 0-无; 1-70w大套餐; 2-45w大套餐"`
  255. Points float64 `description:"研选扣点包点数"`
  256. }
  257. // 申请转正请求参数
  258. type CompanyApplyBySystemContractReq struct {
  259. CompanyId int
  260. CompanyApprovalId int `description:"申请单id,没有传0"`
  261. ContractId int `description:"合同id,没有传0"`
  262. }
  263. // 申请解冻参数
  264. type CompanyApplyThawReq struct {
  265. CompanyId int `description:"客户id"`
  266. CompanyApprovalId int `description:"申请单id,没有传0"`
  267. ApplyRemark string `description:"申请解冻理由"`
  268. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  269. }
  270. // 申请延期参数
  271. type CompanyApplyDelayReq struct {
  272. CompanyId int `description:"客户id"`
  273. CompanyApprovalId int `description:"申请单id,没有传0"`
  274. ApplyRemark string `description:"申请解冻理由"`
  275. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  276. }
  277. // 申请领取参数
  278. type CompanyApplyReceiveReq struct {
  279. CompanyId int `description:"客户id"`
  280. CompanyApprovalId int `description:"申请单id,没有传0"`
  281. ApplyRemark string `description:"申请解冻理由"`
  282. PermissionIds string `description:"权限id,多个用英文逗号隔开"`
  283. }
  284. type CompanyApplyDetailResp struct {
  285. Item *CompanyDetail `description:"客户信息"`
  286. FiccItem *CompanyProductDetail `description:"Ficc客户产品详情"`
  287. RaiItem *CompanyProductDetail `description:"权益客户产品详情"`
  288. ProductName string `description:"部门名称:ficc/权益/admin"`
  289. Approval *CompanyApproval `description:"审批详情"`
  290. }
  291. type CompanyApplyRevokeReq struct {
  292. CompanyId int `description:"客户id"`
  293. }
  294. func ApplyRevoke(companyApprovalId, companyId, productId int) (err error) {
  295. o := orm.NewOrm()
  296. to, err := o.Begin()
  297. if err != nil {
  298. return
  299. }
  300. defer func() {
  301. if err != nil {
  302. _ = to.Rollback()
  303. } else {
  304. _ = to.Commit()
  305. }
  306. }()
  307. sql := `UPDATE company_approval SET approve_status='已撤回' WHERE company_approval_id=? `
  308. _, err = to.Raw(sql, companyApprovalId).Exec()
  309. sql = `UPDATE company_product SET approve_status='已撤回' WHERE company_id=? AND product_id=? `
  310. _, err = to.Raw(sql, companyId, productId).Exec()
  311. sql = `UPDATE company_approval_message SET operation_status=2 WHERE company_approval_id=? AND operation_status=1 `
  312. _, err = to.Raw(sql, companyApprovalId).Exec()
  313. return
  314. }
  315. func GetCompanyApprovalByCompanyApprovalId(companyApprovalId int) (item *CompanyApproval, err error) {
  316. sql := `SELECT * FROM company_approval WHERE company_approval_id=? `
  317. o := orm.NewOrm()
  318. err = o.Raw(sql, companyApprovalId).QueryRow(&item)
  319. return
  320. }
  321. func GetCompanyContractById(companyContractId int) (item *CompanyContractDetail, err error) {
  322. o := orm.NewOrm()
  323. sql := `SELECT * FROM company_contract WHERE company_contract_id=? `
  324. err = o.Raw(sql, companyContractId).QueryRow(&item)
  325. return
  326. }
  327. type ApplyContractResp struct {
  328. Item *CompanyContractDetail
  329. }
  330. // 获取申请转正时的合同类型
  331. type ApplyContractTypeResp struct {
  332. ContractType string
  333. }