final_site_contract.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. package final
  2. import (
  3. "errors"
  4. "fmt"
  5. "zhiyuan/pkg/db"
  6. "zhiyuan/pkg/utils"
  7. "zhiyuan/services/admin"
  8. "zhiyuan/services/user"
  9. "github.com/gin-gonic/gin"
  10. )
  11. type FinalSiteContract struct {
  12. ID int64 `json:"id" prop:"add:false"`
  13. PkgId int64 `json:"pkg_id" label:"套餐" type:"int" prop:"add edit" search:"="`
  14. TypeId int64 `json:"type_id" label:"开工类型" type:"int" prop:"add:false edit" search:"="`
  15. Username string `json:"username" label:"业主姓名" type:"string" prop:"add edit" search:"like"`
  16. Phone string `json:"phone" label:"手机号码" type:"string" prop:"add edit" search:"like"`
  17. AreaId int64 `json:"area_id" label:"区域" type:"int" prop:"add edit" search:"="`
  18. CustomerId int64 `json:"customer_id" label:"客户" type:"int" prop:"edit:false" search:"="`
  19. OrderId int64 `json:"order_id" label:"合同" type:"int" prop:"edit:false" search:"="`
  20. Village string `json:"village" label:"小区名称" type:"string" prop:"edit" search:"like"`
  21. Address string `json:"address" label:"房屋地址" type:"string" prop:"edit" search:"like"`
  22. RoomNo string `json:"room_no" label:"楼栋房号" type:"string" prop:"edit" search:"like"`
  23. Areacode string `json:"areacode" label:"区划代码" type:"string" prop:"edit" search:"rightlike"`
  24. Name string `json:"name" label:"名称" type:"string" prop:"add:false select:concat(zy_final_site.village,'\\t',zy_final_site.address,'\\t',zy_final_site.room_no)" search:"like"`
  25. Names string `json:"names" label:"名称" type:"string" prop:"add:false select:concat(zy_final_site.village,zy_final_site.address,zy_final_site.room_no)" search:"like"`
  26. Area float64 `json:"area" label:"面积" type:"float" prop:"add edit"`
  27. ShopId int64 `json:"shop_id" label:"门店" type:"int" prop:"edit" search:"="`
  28. ManagerId int64 `json:"manager_id" label:"项目经理" type:"int" prop:"add:false edit" search:"="`
  29. ProjectLeaderId int64 `json:"project_leader_id" label:"工程队长" type:"int" prop:"add:false edit" search:"="`
  30. DesignerId int64 `json:"designer_id" label:"设计师" type:"int" prop:"add edit" search:"="`
  31. StartTime int64 `json:"starttime" label:"开工日期" type:"int" prop:"add:false" search:"="`
  32. EndTime int64 `json:"endtime" label:"完工日期" type:"int" prop:"add:false" search:"="`
  33. CreatedId int64 `json:"created_id" label:"创建人员" type:"int" prop:"add:false" search:"="`
  34. ChangeState int64 `json:"change_state" label:"状态" type:"int" prop:"add:false edit" default:"0" search:"="`
  35. Remark string `json:"remark" label:"备注" type:"string" prop:"edit"`
  36. Activity string `json:"activity" label:"活动" type:"string" prop:"edit"`
  37. Attachment string `json:"attachment" label:"附件" type:"string" prop:"edit"`
  38. DeptId int64 `json:"dept_id" label:"归属部门" type:"int" prop:"edit" search:"="`
  39. SalesmanId int64 `json:"salesman_id" label:"业务员" type:"int" prop:"edit" search:"="`
  40. State int64 `json:"state" label:"状态" type:"int" prop:"add:false edit" default:"0" search:"="`
  41. StateId int64 `json:"state_id" label:"状态ID" type:"int" prop:"add:false"`
  42. Room1 int64 `json:"room_1" label:"户型" type:"int" prop:"add edit" search:"="`
  43. Room2 int64 `json:"room_2" label:"户型" type:"int" prop:"add edit" search:"="`
  44. Room3 int64 `json:"room_3" label:"户型" type:"int" prop:"add edit" search:"="`
  45. Room4 int64 `json:"room_4" label:"户型" type:"int" prop:"add edit" search:"="`
  46. Room5 int64 `json:"room_5" label:"户型" type:"int" prop:"edit" search:"="`
  47. ContactTime int64 `json:"contacttime" label:"接触时间" type:"int" prop:"edit" search:"="`
  48. StoreTime int64 `json:"storetime" label:"到店时间" type:"int" prop:"edit" search:"="`
  49. SignTime int64 `json:"signtime" label:"签订时间" type:"int" prop:"edit" search:"="`
  50. DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
  51. CreatedAt int64 `json:"created_at" prop:"add:false"`
  52. UpdatedAt int64 `json:"updated_at" prop:"add:false"`
  53. db.BaseModel
  54. }
  55. func (FinalSiteContract) TableName() string {
  56. return "zy_final_site"
  57. }
  58. func (model FinalSiteContract) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  59. s.Where = append(s.Where, fmt.Sprintf("`%s`.`state` != %s", model.TableName(), s.Param(2)))
  60. if !admin.IsSuperAdmin(c.GetInt("adminID")) && !admin.CheckAuth([]string{"final:finalsitecontract:all"}, c.GetInt("adminID")) {
  61. adminID := s.Param(c.GetInt("adminID"))
  62. where := fmt.Sprintf("`%s`.`designer_id` = %s OR `%s`.`created_id` = %s", model.TableName(), adminID, model.TableName(), adminID)
  63. if admin.CheckAuth([]string{"final:verify"}, c.GetInt("adminID")) {
  64. info, _ := admin.GetInfoByID(c.GetInt("adminID"), nil, nil)
  65. if info == nil {
  66. return false
  67. }
  68. where = fmt.Sprintf("%s OR FIND_IN_SET(`%s`.`shop_id`, %s)", where, model.TableName(), s.Param(info.ShopIds))
  69. }
  70. s.Where = append(s.Where, fmt.Sprintf("(%s)", where))
  71. }
  72. if search, ok := data["search"]; ok {
  73. search = s.Param(fmt.Sprintf("%%%s%%", search))
  74. s.Where = append(s.Where, fmt.Sprintf("`%s`.`username` LIKE %s OR `%s`.`phone` LIKE %s OR concat(`%s`.`village`,`%s`.`address`,`%s`.`room_no`) LIKE %s", model.TableName(), search, model.TableName(), search, model.TableName(), model.TableName(), model.TableName(), search))
  75. }
  76. if signtime1s, ok := data["signtime1"]; ok {
  77. if signtime1, ok := db.ToInt64(signtime1s); ok {
  78. s.Where = append(s.Where, fmt.Sprintf("`%s`.`signtime` >= %s", model.TableName(), s.Param(signtime1)))
  79. }
  80. }
  81. if signtime2s, ok := data["signtime2"]; ok {
  82. if signtime2, ok := db.ToInt64(signtime2s); ok {
  83. s.Where = append(s.Where, fmt.Sprintf("`%s`.`signtime` < %s", model.TableName(), s.Param(signtime2+60*60*24)))
  84. }
  85. }
  86. return true
  87. }
  88. func (FinalSiteContract) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  89. /*var model FinalSite
  90. db.GetModel(map[string]interface{}{
  91. "village": data["village"],
  92. "room_no": data["room_no"],
  93. "deleted_at": 0,
  94. }, &model)
  95. if model.ID != 0 {
  96. return errors.New("工地已存在")
  97. }
  98. user, err := user.GetOne(map[string]interface{}{"phone": data["phone"]}, nil, nil)
  99. if err != nil {
  100. return errors.New("获取用户信息失败")
  101. }
  102. if user == nil {
  103. return errors.New("用户未关注公众号")
  104. }*/
  105. data["created_id"] = c.GetInt("adminID")
  106. return nil
  107. }
  108. func (FinalSiteContract) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) {
  109. var site FinalSite
  110. db.GetModel(map[string]interface{}{
  111. "id": id,
  112. "deleted_at": 0,
  113. }, &site)
  114. from := make(map[string]float64)
  115. var fields []FinalPkgField
  116. db.GetModel(map[string]interface{}{"pkg_id": site.PkgId}, &fields)
  117. for _, v := range fields {
  118. value, _ := db.ToFloat64(v.Value)
  119. from[v.Name] = value
  120. }
  121. db.InsertModel(db.Type(FinalSiteAmount{}), map[string]interface{}{
  122. "site_id": id,
  123. "content": utils.JsonEncode(from),
  124. "created_id": c.GetInt("adminID"),
  125. })
  126. }
  127. func (FinalSiteContract) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
  128. var model FinalSiteContract
  129. db.GetModel(map[string]interface{}{
  130. "id": id,
  131. "deleted_at": 0,
  132. }, &model)
  133. if !admin.CheckShop(int(model.ShopId), c.GetInt("adminID")) && int(model.DesignerId) != c.GetInt("adminID") {
  134. return errors.New("没有权限")
  135. }
  136. /*if type_id, ok := data["type_id"]; ok {
  137. typeId, _ := db.ToInt64(type_id)
  138. if typeId != 0 {
  139. var model FinalSite
  140. db.GetModel(map[string]interface{}{
  141. "id": id,
  142. "deleted_at": 0,
  143. }, &model)
  144. if model.State == 1 {
  145. payments := make([]FinalSitePayment, 0)
  146. db.GetModel(map[string]interface{}{
  147. "site_id": id,
  148. "deleted_at": 0,
  149. }, &payments)
  150. if len(payments) == 0 {
  151. var typ FinalSiteType
  152. db.GetModel(map[string]interface{}{
  153. "id": typeId,
  154. "deleted_at": 0,
  155. }, &typ)
  156. settlements := make([]int64, 0)
  157. if typ.ID != 0 {
  158. for _, s := range strings.Split(typ.Settlement, ",") {
  159. if n, ok := db.ToInt64(s); ok {
  160. settlements = append(settlements, n)
  161. }
  162. }
  163. }
  164. ptypes := make([]FinalPaymentType, 0)
  165. db.GetModel(map[string]interface{}{
  166. "deleted_at": 0,
  167. }, &ptypes)
  168. for _, v := range ptypes {
  169. data := map[string]interface{}{
  170. "type_id": v.ID,
  171. "site_id": id,
  172. }
  173. for _, s := range settlements {
  174. if s == v.ID {
  175. data["state"] = 1
  176. }
  177. }
  178. id, err := db.InsertModel(db.Type(FinalSitePayment{}), data)
  179. if err == nil {
  180. FinalSitePayment{}.AddAfter(c, id, data, data)
  181. }
  182. }
  183. var order budget2.Order
  184. if model.OrderId != 0 {
  185. db.GetModel(map[string]interface{}{
  186. "id": model.OrderId,
  187. "deleted_at": 0,
  188. }, &order)
  189. }
  190. if order.ID == 0 {
  191. var pkg FinalPkg
  192. db.GetModel(map[string]interface{}{
  193. "id": model.PkgId,
  194. "deleted_at": 0,
  195. }, &pkg)
  196. types := make([]FinalType, 0)
  197. db.GetModel(map[string]interface{}{
  198. "state": 1,
  199. "deleted_at": 0,
  200. }, &types)
  201. for _, v := range types {
  202. data := map[string]interface{}{
  203. "type_id": v.ID,
  204. "site_id": id,
  205. }
  206. if v.CalcValue != "" {
  207. expression, err := govaluate.NewEvaluableExpression(v.CalcValue)
  208. if err == nil {
  209. parameters := make(map[string]interface{}, 8)
  210. parameters["面积"] = model.Area
  211. parameters["套餐"] = pkg.Name
  212. result, err := expression.Evaluate(parameters)
  213. if err == nil {
  214. data["budget"], _ = db.ToFloat64(result)
  215. }
  216. }
  217. }
  218. id, err := db.InsertModel(db.Type(FinalSiteControl{}), data)
  219. if err == nil {
  220. FinalSiteControl{}.AddAfter(c, id, data, data)
  221. }
  222. }
  223. mauxiliarys := make([]FinalMatAuxiliary, 0)
  224. db.GetModel(map[string]interface{}{
  225. "preset": 1,
  226. "deleted_at": 0,
  227. }, &mauxiliarys)
  228. for _, v := range mauxiliarys {
  229. if v.Name == "开工形象" && (model.ShopId == 18 || typeId == 4) {
  230. continue
  231. }
  232. data := map[string]interface{}{
  233. "type_id": v.ID,
  234. "site_id": id,
  235. }
  236. if v.CalcValue != "" {
  237. expression, err := govaluate.NewEvaluableExpression(v.CalcValue)
  238. if err == nil {
  239. parameters := make(map[string]interface{}, 8)
  240. parameters["面积"] = model.Area
  241. parameters["套餐"] = pkg.Name
  242. result, err := expression.Evaluate(parameters)
  243. if err == nil {
  244. data["budget"], _ = db.ToFloat64(result)
  245. }
  246. }
  247. }
  248. id, err := db.InsertModel(db.Type(FinalMaterial{}), data)
  249. if err == nil {
  250. FinalMaterial{}.AddAfter(c, id, data, data)
  251. }
  252. }
  253. address := model.Address
  254. if model.Village != " " {
  255. address += " " + model.Village
  256. }
  257. if model.RoomNo != " " {
  258. address += " " + model.RoomNo
  259. }
  260. switch model.ShopId {
  261. case 9, 13, 14, 18:
  262. SendSiteMessage(1003, id, address, "工地已创建,请及时下单")
  263. case 10, 12, 15:
  264. SendSiteMessage(1004, id, address, "工地已创建,请及时下单")
  265. case 6, 16:
  266. SendSiteMessage(1257, id, address, "工地已创建,请及时下单")
  267. case 7, 17:
  268. SendSiteMessage(1173, id, address, "工地已创建,请及时下单")
  269. }
  270. } else {
  271. o, err := order.LoadOrder()
  272. }
  273. }
  274. }
  275. }
  276. }*/
  277. if state, ok := data["state"]; ok {
  278. states, _ := db.ToInt64(state)
  279. if states == 1 {
  280. var model FinalSite
  281. db.GetModel(map[string]interface{}{
  282. "id": id,
  283. "deleted_at": 0,
  284. }, &model)
  285. if model.ID == 0 {
  286. return errors.New("工地不存在")
  287. }
  288. if model.ShopId == 0 {
  289. return errors.New("公司名称未填")
  290. }
  291. if model.DeptId == 0 {
  292. return errors.New("部门未填")
  293. }
  294. if model.SalesmanId == 0 {
  295. return errors.New("业务员未填")
  296. }
  297. if model.RoomNo == "" {
  298. return errors.New("楼栋房号未填")
  299. }
  300. if model.Areacode == "" {
  301. return errors.New("所在县区未填")
  302. }
  303. if model.ContactTime == 0 {
  304. return errors.New("接触时间未填")
  305. }
  306. if model.StoreTime == 0 {
  307. return errors.New("到店时间未填")
  308. }
  309. if model.SignTime == 0 {
  310. return errors.New("签订时间未填")
  311. }
  312. var site FinalSite
  313. db.GetModel(map[string]interface{}{
  314. "id !=": id,
  315. "village": model.Village,
  316. "room_no": model.RoomNo,
  317. "state": 1,
  318. "deleted_at": 0,
  319. }, &site)
  320. if site.ID != 0 {
  321. return errors.New("房屋地址重复")
  322. }
  323. user, err := user.GetOne(map[string]interface{}{"phone": model.Phone}, nil, nil)
  324. if err != nil {
  325. return errors.New("获取用户信息失败")
  326. }
  327. if user == nil {
  328. return errors.New("用户未关注公众号")
  329. }
  330. }
  331. var record FinalSiteStateRecord
  332. db.GetModel(map[string]interface{}{"id": id}, &record)
  333. if record.State != state {
  334. db.InsertModel(db.Type(FinalSiteStateRecord{}), map[string]interface{}{
  335. "site_id": id,
  336. "state": state,
  337. "created_id": c.GetInt("adminID"),
  338. })
  339. }
  340. }
  341. return nil
  342. }
  343. func (FinalSiteContract) DelPrivilege(c *gin.Context, id int64) error {
  344. return nil
  345. }
  346. func (FinalSiteContract) Page() bool {
  347. return false
  348. }
  349. func (FinalSiteContract) Count() bool {
  350. return true
  351. }