user.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package customer
  2. import (
  3. "time"
  4. "zhiyuan/models"
  5. "zhiyuan/models/budget"
  6. "zhiyuan/pkg/app"
  7. "zhiyuan/pkg/config"
  8. "zhiyuan/pkg/db"
  9. "zhiyuan/pkg/utils"
  10. "zhiyuan/services/form"
  11. "zhiyuan/services/user"
  12. "zhiyuan/services/user/check"
  13. "zhiyuan/services/user/checks"
  14. "github.com/gin-gonic/gin"
  15. )
  16. func Login(c *gin.Context) {
  17. var form form.UserLogin
  18. if app.Bind(c, &form) != nil {
  19. return
  20. }
  21. token, err := user.Login(form)
  22. if err != nil {
  23. app.Error(c, err.Error())
  24. return
  25. }
  26. app.Success(c, map[string]string{
  27. "token": token,
  28. })
  29. }
  30. func Logout(c *gin.Context) {
  31. err := user.Logout(c.GetInt("userID"))
  32. if err != nil {
  33. app.ErrorMsg(c, err.Error(), nil)
  34. return
  35. }
  36. app.Success(c, nil)
  37. }
  38. func Info(c *gin.Context) {
  39. type House struct {
  40. ID int `json:"id"`
  41. Address string `json:"address"`
  42. Province int `json:"province"`
  43. City int `json:"city"`
  44. Region int `json:"region"`
  45. RegionName string `json:"region_name"`
  46. LinkName string `json:"link_name"`
  47. LinkPhone string `json:"link_phone"`
  48. WarrantyStart string `json:"warranty_start"`
  49. }
  50. type SettingInfo struct {
  51. ImgHost string `json:"img_host"`
  52. ServiceTel string `json:"service_tel"`
  53. PageSize int `json:"page_size"`
  54. }
  55. type UserInfo struct {
  56. ID int `json:"id"`
  57. Name string `json:"name"`
  58. NickName string `json:"nickname"`
  59. Phone string `json:"phone"`
  60. HeadImgUrl string `json:"headimgurl"`
  61. Book int `json:"book"`
  62. House []*House `json:"house"`
  63. Setting SettingInfo `json:"setting"`
  64. Integral int `json:"integral"`
  65. }
  66. var userInfo UserInfo
  67. _, err := user.GetInfoByID(c.GetInt("userID"), nil, &userInfo)
  68. if err != nil {
  69. app.ErrorMsg(c, err.Error(), nil)
  70. return
  71. }
  72. houseList := make([]*House, 0)
  73. if _, err = user.GetHouseList(map[string]interface{}{"user_id": c.GetInt("userID"), "deleted_at": 0}, nil, app.Page{}, &houseList); err != nil {
  74. app.ErrorMsg(c, err.Error(), nil)
  75. return
  76. }
  77. for k, v := range houseList {
  78. v.WarrantyStart = utils.DateS(v.WarrantyStart, "YYYY-MM-DD")
  79. houseList[k] = v
  80. }
  81. userInfo.House = houseList
  82. userInfo.Setting = SettingInfo{
  83. ImgHost: config.Cfg.App.ImgHost,
  84. ServiceTel: config.Cfg.App.ServiceTel,
  85. PageSize: config.Cfg.App.PageLimitMin,
  86. }
  87. var integral models.UserIntegralStatist
  88. db.GetModel(map[string]interface{}{
  89. "user_id": userInfo.ID,
  90. }, &integral)
  91. userInfo.Integral = int(integral.Integral)
  92. app.Success(c, userInfo)
  93. }
  94. func InfoEdit(c *gin.Context) {
  95. var form user.EditInfoForm
  96. if app.Bind(c, &form) != nil {
  97. return
  98. }
  99. err := user.EditInfoByID(form, c.GetInt("userID"))
  100. if err != nil {
  101. app.ErrorMsg(c, err.Error(), nil)
  102. return
  103. }
  104. app.Success(c, nil)
  105. }
  106. func UnBindWeixin(c *gin.Context) {
  107. if err := user.UnBindWeixin(c.GetInt("userID")); err != nil {
  108. app.ErrorMsg(c, err.Error(), nil)
  109. return
  110. }
  111. app.Success(c, nil)
  112. }
  113. func CheckBook(c *gin.Context) {
  114. userMap := map[string]interface{}{
  115. "book": 1,
  116. }
  117. if _, err := db.Update(models.User{}.TableName(), map[string]interface{}{"id": c.GetInt("userID")}, userMap); err != nil {
  118. app.ErrorMsg(c, err.Error(), nil)
  119. }
  120. app.Success(c, nil)
  121. }
  122. func CheckCourse(c *gin.Context) {
  123. id, err := check.CheckCourse(c.GetInt("userID"))
  124. if err != nil {
  125. app.Error(c, err.Error())
  126. return
  127. }
  128. app.Success(c, gin.H{"id": id})
  129. }
  130. func CheckList(c *gin.Context) {
  131. page := app.HandlePageNums(c)
  132. where := map[string]string{
  133. "where": "`zy_user_check`.`id`>0 AND `zy_user_check`.`state` < 2",
  134. "_group_by": "`zy_user_check`.`id`",
  135. "_order_by": "`zy_user_check`.`id` desc",
  136. }
  137. if page.PageSize != 0 {
  138. where["_page_size"] = utils.ToStr(page.PageSize)
  139. where["_page_num"] = utils.ToStr(page.PageNum)
  140. }
  141. param := make(map[string]interface{})
  142. now := time.Now()
  143. today0 := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
  144. where["where"] = where["where"] + " AND `zy_user_check`.`user_id` = {{user_id}}"
  145. param["user_id"] = c.GetInt("userID")
  146. where["where"] = where["where"] + " AND `zy_user_check`.`created_at` >= {{today}}"
  147. param["today"] = today0
  148. total, err := check.CountRaw(where["where"], param)
  149. if err != nil {
  150. app.Error(c, err.Error())
  151. return
  152. }
  153. type CheckList struct {
  154. ID int `json:"id"`
  155. UserId int `json:"user_id"`
  156. Total int `json:"total"`
  157. Progress int `json:"progress"`
  158. Right int `json:"right"`
  159. State int `json:"state"`
  160. CreatedAt string `json:"created_at"`
  161. UpdatedAt int `json:"updated_at"`
  162. }
  163. checkList := make([]CheckList, 0)
  164. if err = check.GetChecksRaw(where, param, &checkList); err != nil {
  165. app.Error(c, err.Error())
  166. return
  167. }
  168. for k, v := range checkList {
  169. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  170. checkList[k] = v
  171. }
  172. data := gin.H{
  173. "list": checkList,
  174. "total": total,
  175. "limit": page.PageSize,
  176. }
  177. app.Success(c, data)
  178. }
  179. func CheckInfo(c *gin.Context) {
  180. check_id := utils.ToInt(c.Param("id"))
  181. if check_id <= 0 {
  182. app.Error(c, "考核id有误")
  183. return
  184. }
  185. checkInfo, err := check.GetOne(map[string]interface{}{"id": check_id}, nil, nil)
  186. if err != nil {
  187. app.Error(c, err.Error())
  188. return
  189. }
  190. where := map[string]string{
  191. "where": "`zy_user_checks`.`id`>0",
  192. "_group_by": "`zy_user_checks`.`id`",
  193. "_order_by": "`zy_user_checks`.`id` asc",
  194. }
  195. param := make(map[string]interface{})
  196. where["where"] = where["where"] + " AND `zy_user_checks`.`check_id` = {{check_id}}"
  197. param["check_id"] = checkInfo.ID
  198. type ChecksList struct {
  199. ID int `json:"id"`
  200. CheckId int `json:"check_id"`
  201. Type int `json:"type"`
  202. QuestionId int `json:"question_id"`
  203. Content string `json:"content"`
  204. Options string `json:"options"`
  205. Answer string `json:"answer"`
  206. Right string `json:"right"`
  207. CreatedAt string `json:"created_at"`
  208. UpdatedAt string `json:"updated_at"`
  209. }
  210. checksList := make([]ChecksList, 0)
  211. if err = checks.GetCheckssRaw(where, param, &checksList); err != nil {
  212. app.Error(c, err.Error())
  213. return
  214. }
  215. for k, v := range checksList {
  216. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  217. v.UpdatedAt = utils.DateS(v.UpdatedAt, "YYYY-MM-DD HH:mm")
  218. checksList[k] = v
  219. }
  220. data := gin.H{
  221. "info": checkInfo,
  222. "checks": checksList,
  223. }
  224. app.Success(c, data)
  225. }
  226. func SelectChecks(c *gin.Context) {
  227. var form form.ChecksSelect
  228. if app.Bind(c, &form) != nil {
  229. return
  230. }
  231. err := checks.SelectChecks(form, c.GetInt("userID"))
  232. if err != nil {
  233. app.Error(c, err.Error())
  234. return
  235. }
  236. app.Success(c, nil)
  237. }
  238. func SubmitCheck(c *gin.Context) {
  239. check_id := utils.ToInt(c.Param("id"))
  240. if check_id <= 0 {
  241. app.Error(c, "考核id有误")
  242. return
  243. }
  244. err := check.SubmitCheck(check_id, c.GetInt("userID"))
  245. if err != nil {
  246. app.Error(c, err.Error())
  247. return
  248. }
  249. app.Success(c, nil)
  250. }
  251. func CheckContract(c *gin.Context) {
  252. customer_id := utils.ToInt(c.Param("id"))
  253. if customer_id <= 0 {
  254. app.Error(c, "房屋不存在")
  255. return
  256. }
  257. var customer budget.Customer
  258. db.GetModel(map[string]interface{}{"id": customer_id}, &customer)
  259. if customer.ID == 0 {
  260. app.ErrorMsg(c, "客户不存在", nil)
  261. return
  262. }
  263. if customer.State != 1 {
  264. app.ErrorMsg(c, "状态错误", nil)
  265. return
  266. }
  267. type UserInfo struct {
  268. ID int `json:"id"`
  269. Phone string `json:"phone"`
  270. }
  271. var userInfo UserInfo
  272. _, err := user.GetInfoByID(c.GetInt("userID"), nil, &userInfo)
  273. if err != nil {
  274. app.ErrorMsg(c, "客户不存在", nil)
  275. return
  276. }
  277. if userInfo.Phone != customer.Phone {
  278. app.ErrorMsg(c, "客户信息错误", nil)
  279. return
  280. }
  281. err = db.UpdateModel(db.Type(customer), customer.ID, map[string]interface{}{
  282. "state": 2,
  283. })
  284. if err != nil {
  285. app.Error(c, err.Error())
  286. return
  287. }
  288. db.InsertModel(db.Type(budget.BudgetCustomerProcess{}), map[string]interface{}{
  289. "customer_id": customer.ID,
  290. "order_id": customer.OrderId,
  291. "type": 2,
  292. "created_id": c.GetInt("userID"),
  293. })
  294. app.Success(c, nil)
  295. }
  296. func CheckStart(c *gin.Context) {
  297. customer_id := utils.ToInt(c.Param("id"))
  298. if customer_id <= 0 {
  299. app.Error(c, "房屋不存在")
  300. return
  301. }
  302. var customer budget.Customer
  303. db.GetModel(map[string]interface{}{"id": customer_id}, &customer)
  304. if customer.ID == 0 {
  305. app.ErrorMsg(c, "客户不存在", nil)
  306. return
  307. }
  308. if customer.State != 3 {
  309. app.ErrorMsg(c, "状态错误", nil)
  310. return
  311. }
  312. type UserInfo struct {
  313. ID int `json:"id"`
  314. Phone string `json:"phone"`
  315. }
  316. var userInfo UserInfo
  317. _, err := user.GetInfoByID(c.GetInt("userID"), nil, &userInfo)
  318. if err != nil {
  319. app.ErrorMsg(c, "客户不存在", nil)
  320. return
  321. }
  322. if userInfo.Phone != customer.Phone {
  323. app.ErrorMsg(c, "客户信息错误", nil)
  324. return
  325. }
  326. err = db.UpdateModel(db.Type(customer), customer.ID, map[string]interface{}{
  327. "state": 4,
  328. })
  329. if err != nil {
  330. app.Error(c, err.Error())
  331. return
  332. }
  333. db.InsertModel(db.Type(budget.BudgetCustomerProcess{}), map[string]interface{}{
  334. "customer_id": customer.ID,
  335. "order_id": customer.OrderId,
  336. "site_id": customer.SiteId,
  337. "type": 4,
  338. "created_id": c.GetInt("userID"),
  339. })
  340. app.Success(c, nil)
  341. }