final_site_collect.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package final
  2. import (
  3. "errors"
  4. "fmt"
  5. "zhiyuan/pkg/db"
  6. "zhiyuan/pkg/logger"
  7. "zhiyuan/pkg/utils"
  8. "zhiyuan/pkg/weixin/mp"
  9. "zhiyuan/services/admin"
  10. "zhiyuan/services/shop"
  11. "github.com/gin-gonic/gin"
  12. )
  13. type FinalSiteCollect struct {
  14. ID int64 `json:"id" prop:"add:false"`
  15. ShopID int64 `json:"shop_id" label:"门店" type:"int" prop:"add:false" default:"0" search:"multiple"`
  16. Type int64 `json:"type" label:"类型" type:"int" default:"0" search:"multiple"`
  17. Total float64 `json:"total" label:"合计金额" type:"float" prop:"add:false" search:"="`
  18. State int64 `json:"state" label:"状态" type:"int" prop:"add:false" default:"0" search:"multiple"`
  19. CreatedId int64 `json:"created_id" label:"创建人员" type:"int" prop:"add:false" search:"="`
  20. DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
  21. CreatedAt int64 `json:"created_at" prop:"add:false"`
  22. UpdatedAt int64 `json:"updated_at" prop:"add:false"`
  23. db.BaseModel
  24. }
  25. func (FinalSiteCollect) TableName() string {
  26. return "zy_final_site_collect"
  27. }
  28. func (FinalSiteCollect) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  29. typ, _ := db.ToInt64(post["type"])
  30. signature := db.ToString(post["signature"])
  31. if signature == "" {
  32. return errors.New("没有权限")
  33. }
  34. if typ == 0 {
  35. type FinalSiteTableCollects struct {
  36. ShopId int64 `json:"shop_id" label:"门店" type:"int" prop:"select:finalsite.shop_id" search:"="`
  37. Total float64 `json:"total" label:"合计" type:"float" prop:"select:ROUND(sum(finalsiteitem.price*num))"`
  38. FinalSiteTableCollect
  39. }
  40. if tableIds, ok := db.ToArray(post["table_ids"]); ok {
  41. models := make([]FinalSiteTableCollects, 0)
  42. db.GetModel(map[string]interface{}{
  43. "id in": tableIds,
  44. "collect_id": 0,
  45. "state": 2,
  46. "deleted_at": 0,
  47. }, &models)
  48. if len(tableIds) != len(models) {
  49. return errors.New("项目错误")
  50. }
  51. if len(models) == 0 {
  52. return errors.New("没有权限")
  53. }
  54. shopId := models[0].ShopId
  55. total := float64(0)
  56. for _, v := range models {
  57. if v.ShopId != shopId {
  58. return errors.New("没有权限")
  59. }
  60. total += v.Total
  61. }
  62. data["shop_id"] = shopId
  63. data["total"] = total
  64. data["state"] = 3
  65. data["created_id"] = c.GetInt("adminID")
  66. return nil
  67. }
  68. } else if typ == 1 {
  69. type FinalSettleCollects struct {
  70. ShopId int64 `json:"shop_id" label:"门店" type:"int" prop:"select:finalsite.shop_id"`
  71. //ProjectLeaderId int `json:"project_leader_id" label:"工程队长" type:"int" prop:"select:finalsite.project_leader_id"`
  72. Total float64 `json:"total" label:"合计" type:"float"`
  73. FinalSettleMobile
  74. }
  75. if settleIds, ok := db.ToArray(post["settle_ids"]); ok {
  76. models := make([]FinalSettleCollects, 0)
  77. db.GetModel(map[string]interface{}{
  78. "id in": settleIds,
  79. "collect_id": 0,
  80. "state": 3,
  81. "deleted_at": 0,
  82. }, &models)
  83. if len(settleIds) != len(models) {
  84. return errors.New("项目错误")
  85. }
  86. if len(models) == 0 {
  87. return errors.New("没有权限")
  88. }
  89. shopId := models[0].ShopId
  90. total := float64(0)
  91. //leaderIds := []string{}
  92. for _, v := range models {
  93. if v.ShopId != shopId {
  94. return errors.New("没有权限")
  95. }
  96. total += v.Total
  97. /*leaderId := strconv.Itoa(v.ProjectLeaderId)
  98. for _, id := range leaderIds {
  99. if id == leaderId {
  100. leaderId = ""
  101. break
  102. }
  103. }
  104. if leaderId != "" {
  105. leaderIds = append(leaderIds, strconv.Itoa(v.ProjectLeaderId))
  106. }*/
  107. }
  108. data["shop_id"] = shopId
  109. data["total"] = total
  110. data["state"] = 3
  111. //data["leader_ids"] = strings.Join(leaderIds, ",")
  112. data["created_id"] = c.GetInt("adminID")
  113. return nil
  114. }
  115. } else if typ == 2 {
  116. /*type FinalMaterialOrderCollects struct {
  117. ProjectLeaderId int `json:"project_leader_id" label:"工程队长" type:"int" prop:"select:finalsite.project_leader_id"`
  118. FinalMaterialOrderMobile
  119. }*/
  120. if settleIds, ok := db.ToArray(post["settle_ids"]); ok {
  121. models := make([]FinalMaterialSettle, 0)
  122. db.GetModel(map[string]interface{}{
  123. "id in": settleIds,
  124. "collect_id": 0,
  125. "state": 2,
  126. "deleted_at": 0,
  127. }, &models)
  128. if len(settleIds) != len(models) {
  129. return errors.New("项目错误")
  130. }
  131. if len(models) == 0 {
  132. return errors.New("没有权限")
  133. }
  134. /*orders := make([]FinalMaterialOrderCollects, 0)
  135. db.GetModel(map[string]interface{}{
  136. "settle.id in": settleIds,
  137. }, &orders)
  138. if len(orders) == 0 {
  139. return errors.New("没有权限")
  140. }*/
  141. shopId := models[0].ShopId
  142. total := float64(0)
  143. //leaderIds := []string{}
  144. for _, v := range models {
  145. if v.ShopId != shopId {
  146. return errors.New("没有权限")
  147. }
  148. total += v.Total
  149. }
  150. /*for _, v := range orders {
  151. leaderId := strconv.Itoa(v.ProjectLeaderId)
  152. for _, id := range leaderIds {
  153. if id == leaderId {
  154. leaderId = ""
  155. break
  156. }
  157. }
  158. if leaderId != "" {
  159. leaderIds = append(leaderIds, strconv.Itoa(v.ProjectLeaderId))
  160. }
  161. }*/
  162. data["shop_id"] = shopId
  163. data["total"] = total
  164. data["state"] = 4
  165. //data["leader_ids"] = strings.Join(leaderIds, ",")
  166. data["created_id"] = c.GetInt("adminID")
  167. return nil
  168. }
  169. }
  170. return errors.New("没有权限")
  171. }
  172. func (FinalSiteCollect) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) {
  173. signature := db.ToString(post["signature"])
  174. explain := db.ToString(post["explain"])
  175. pictures := db.ToString(post["pictures"])
  176. typ, _ := db.ToInt64(post["type"])
  177. if typ == 0 {
  178. db.InsertModel(db.Type(FinalSiteProcess{}), map[string]interface{}{
  179. "collect_id": id,
  180. "explain": explain,
  181. "pictures": pictures,
  182. "type": 2,
  183. "signature": signature,
  184. "state": 0,
  185. "created_id": c.GetInt("adminID"),
  186. })
  187. if tableIds, ok := db.ToArray(post["table_ids"]); ok {
  188. db.UpdateModels(db.Type(FinalSiteTable{}), map[string]interface{}{
  189. "id in": tableIds,
  190. }, map[string]interface{}{
  191. "state": 3,
  192. "collect_id": id,
  193. })
  194. }
  195. var collect FinalSiteCollect
  196. db.GetModel(map[string]interface{}{
  197. "id": id,
  198. }, &collect)
  199. s, _ := shop.GetOne(map[string]interface{}{
  200. "id": collect.ShopID,
  201. }, nil, nil)
  202. if s == nil {
  203. return
  204. }
  205. query := "SELECT `id` from `zy_admin` where FIND_IN_SET({{shopID}}, `shop_ids`) and FIND_IN_SET({{roleID}}, `role_ids`) and `state` = 1"
  206. params := make(map[string]interface{})
  207. params["shopID"] = collect.ShopID
  208. params["roleID"] = 18
  209. admins, _ := db.QueryMap(query, params, nil)
  210. for _, admin := range admins {
  211. id, _ := db.ToInt64(admin["id"])
  212. SendCollectMessage(int(id), collect.ID, s.ShopName, "工人汇总请审核")
  213. }
  214. } else if typ == 1 {
  215. db.InsertModel(db.Type(FinalSettleProcess{}), map[string]interface{}{
  216. "collect_id": id,
  217. "explain": explain,
  218. "pictures": pictures,
  219. "type": 3,
  220. "signature": signature,
  221. "state": 0,
  222. "created_id": c.GetInt("adminID"),
  223. })
  224. if settleIds, ok := db.ToArray(post["settle_ids"]); ok {
  225. db.UpdateModels(db.Type(FinalSettle{}), map[string]interface{}{
  226. "id in": settleIds,
  227. }, map[string]interface{}{
  228. "state": 4,
  229. "collect_id": id,
  230. })
  231. }
  232. var collect FinalSiteCollect
  233. db.GetModel(map[string]interface{}{
  234. "id": id,
  235. }, &collect)
  236. s, _ := shop.GetOne(map[string]interface{}{
  237. "id": collect.ShopID,
  238. }, nil, nil)
  239. if s == nil {
  240. return
  241. }
  242. query := "SELECT `id` from `zy_admin` where FIND_IN_SET({{shopID}}, `shop_ids`) and FIND_IN_SET({{roleID}}, `role_ids`) and `state` = 1"
  243. params := make(map[string]interface{})
  244. params["shopID"] = collect.ShopID
  245. params["roleID"] = 18
  246. admins, _ := db.QueryMap(query, params, nil)
  247. for _, admin := range admins {
  248. id, _ := db.ToInt64(admin["id"])
  249. SendCollectMessage(int(id), collect.ID, s.ShopName, "结算汇总请审核")
  250. }
  251. } else if typ == 2 {
  252. db.InsertModel(db.Type(FinalMaterialSettleProcess{}), map[string]interface{}{
  253. "collect_id": id,
  254. "explain": explain,
  255. "pictures": pictures,
  256. "type": 2,
  257. "signature": signature,
  258. "state": 0,
  259. "created_id": c.GetInt("adminID"),
  260. })
  261. if settleIds, ok := db.ToArray(post["settle_ids"]); ok {
  262. db.UpdateModels(db.Type(FinalMaterialSettle{}), map[string]interface{}{
  263. "id in": settleIds,
  264. }, map[string]interface{}{
  265. "state": 4,
  266. "collect_id": id,
  267. })
  268. }
  269. }
  270. }
  271. func (model FinalSiteCollect) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  272. if !admin.IsSuperAdmin(c.GetInt("adminID")) {
  273. adminID := s.Param(c.GetInt("adminID"))
  274. where := fmt.Sprintf("`%s`.`created_id` = %s", model.TableName(), adminID)
  275. if admin.CheckAuth([]string{"final:verify"}, c.GetInt("adminID")) {
  276. info, _ := admin.GetInfoByID(c.GetInt("adminID"), nil, nil)
  277. if info == nil {
  278. return false
  279. }
  280. where = fmt.Sprintf("%s OR FIND_IN_SET(`%s`.`shop_id`, %s)", where, model.TableName(), s.Param(info.ShopIds))
  281. }
  282. s.Where = append(s.Where, fmt.Sprintf("(%s)", where))
  283. }
  284. if state, ok := data["state"]; ok {
  285. if n, ok := db.ToInt64(state); ok {
  286. if n != 0 {
  287. s.Having = append(s.Having, fmt.Sprintf("`%s`.`state` = %s", model.TableName(), s.Param(n)))
  288. }
  289. }
  290. }
  291. return true
  292. }
  293. func (FinalSiteCollect) OnePrivilege(c *gin.Context, id int64) bool {
  294. return true
  295. }
  296. func (FinalSiteCollect) Page() bool {
  297. return false
  298. }
  299. func (FinalSiteCollect) Count() bool {
  300. return true
  301. }
  302. type FinalSiteCollectMobile struct {
  303. CreatedName string `json:"created_name" prop:"select:admin.username"`
  304. CreatedPhone string `json:"created_phone" prop:"select:admin.phone"`
  305. ShopName string `json:"shop_name" prop:"select:shop.shop_name"`
  306. FinalSiteCollect
  307. }
  308. func (model FinalSiteCollectMobile) GroupBy() string {
  309. return fmt.Sprintf("`%s`.`id`", model.TableName())
  310. }
  311. type JoinShop struct {
  312. FinalSiteCollect
  313. }
  314. func (JoinShop) TableName() string {
  315. return "zy_shop"
  316. }
  317. func (model FinalSiteCollectMobile) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  318. return append(model.FinalSiteCollect.LeftJoin(data, s), db.JoinModel{
  319. Model: JoinShop{},
  320. As: "shop",
  321. On: []string{"`shop`.`id` = " + model.TableName() + ".`shop_id`"},
  322. }, db.JoinModel{
  323. Model: JoinAdmin{},
  324. As: "admin",
  325. On: []string{"`admin`.`id` = " + model.TableName() + ".`created_id`"},
  326. })
  327. }
  328. func SendCollectMessage(adminID int, orderId int64, address string, text string) error {
  329. logger.Sugar.Infof("SendCollectMessage, send: %v %v %v %v", adminID, orderId, address, text)
  330. client, err := mp.NewClient(1)
  331. if err != nil {
  332. return err
  333. }
  334. adminInfo, err := admin.GetInfoByID(adminID, nil, nil)
  335. if err != nil {
  336. return err
  337. }
  338. if adminInfo == nil || adminInfo.OpenId == "" {
  339. return nil
  340. }
  341. url := ""
  342. if orderId != 0 {
  343. url = "https://jz.nczyzs.com/final/#/collect/" + db.ToString(orderId)
  344. }
  345. ret, err := client.SendTemplateMessage(adminInfo.OpenId, "0tdzToVtXoCUrafmN-ub9J69HKKCfUWEsCan-WkuPKk", url, map[string]interface{}{
  346. "character_string1": map[string]interface{}{
  347. "value": utils.SubStr(db.ToString(orderId), 0, 32),
  348. },
  349. "thing2": map[string]interface{}{
  350. "value": utils.SubStr(address, 0, 20),
  351. },
  352. "thing5": map[string]interface{}{
  353. "value": utils.SubStr(text, 0, 20),
  354. },
  355. })
  356. logger.Sugar.Infof("SendCollectMessage, ret: %v %v", ret, err)
  357. return err
  358. }