123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- package work
- import (
- "time"
- "zhiyuan/models"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/form"
- "zhiyuan/services/work/worksite"
- "github.com/gin-gonic/gin"
- )
- func WorkSiteList(c *gin.Context) {
- page := app.HandlePageNums(c)
- where := map[string]string{
- "where": "`zy_work_site`.`id`>0 AND `zy_work_site`.`deleted_at`=0",
- "_group_by": "`zy_work_site`.`id`",
- "_order_by": "`zy_work_site`.`id` desc",
- }
- if page.PageSize != 0 {
- where["_page_size"] = utils.ToStr(page.PageSize)
- where["_page_num"] = utils.ToStr(page.PageNum)
- }
- param := make(map[string]interface{})
- username := c.Query("username")
- if username != "" {
- where["where"] = where["where"] + " AND `zy_work_site`.`username` LIKE {{username}}"
- param["username"] = "%" + username + "%"
- }
- phone := c.Query("phone")
- if phone != "" {
- where["where"] = where["where"] + " AND `zy_work_site`.`phone` LIKE {{phone}}"
- param["phone"] = "%" + phone + "%"
- }
- village := c.Query("village")
- if village != "" {
- where["where"] = where["where"] + " AND `zy_work_site`.`village` LIKE {{village}}"
- param["village"] = "%" + village + "%"
- }
- address := c.Query("address")
- if address != "" {
- where["where"] = where["where"] + " AND `zy_work_site`.`address` LIKE {{address}}"
- param["address"] = "%" + address + "%"
- }
- room_no := c.Query("room_no")
- if room_no != "" {
- where["where"] = where["where"] + " AND `zy_work_site`.`room_no` LIKE {{room_no}}"
- param["room_no"] = "%" + room_no + "%"
- }
- area := c.Query("area")
- if area != "" {
- where["where"] = where["where"] + " AND `zy_work_site`.`area` LIKE {{area}}"
- param["area"] = "%" + area + "%"
- }
- search := c.Query("search")
- if search != "" {
- where["where"] = where["where"] + " AND (`zy_work_site`.`username` LIKE {{search}} OR `zy_work_site`.`phone` LIKE {{search}} OR `zy_work_site`.`village` LIKE {{search}} OR `zy_work_site`.`address` LIKE {{search}} OR `zy_work_site`.`room_no` LIKE {{search}} OR `zy_work_site`.`area` LIKE {{search}})"
- param["search"] = "%" + search + "%"
- }
- state := utils.ToInt(c.Query("state"))
- if state != 0 {
- where["where"] = where["where"] + " AND `zy_work_site`.`state`={{state}}"
- param["state"] = state - 1
- }
- if utils.ToInt(c.Query("my")) != 0 {
- where["where"] = where["where"] + " AND (`zy_work_site`.`manager_id`={{adminID}} OR `zy_work_site`.`designer_id`={{adminID}} OR `zy_work_site`.`salesman_id`={{adminID}} OR `zy_work_site`.`project_manager_id`={{adminID}} OR `zy_work_site`.`project_leader_id`={{adminID}} OR `zy_work_site`.`servicer_id`={{adminID}} OR `zy_work_site`.`quality_id`={{adminID}} OR `zy_work_site`.`assistant_id`={{adminID}} OR `zy_work_site`.`created_id`={{adminID}} OR FIND_IN_SET({{adminID}}, `zy_work_site`.`admin_ids`))"
- param["adminID"] = c.GetInt("adminID")
- }
- total, err := worksite.CountRaw(where["where"], param)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- type WorksiteList struct {
- ID int `json:"id"`
- PkgId int `json:"pkg_id"`
- Username string `json:"username"`
- Phone string `json:"phone"`
- Village string `json:"village"`
- Address string `json:"address"`
- RoomNo string `json:"room_no"`
- Area string `json:"area"`
- Longitude string `json:"longitude"`
- Latitude string `json:"latitude"`
- ManagerId int `json:"manager_id"`
- DesignerId int `json:"designer_id"`
- CreatedId int `json:"created_id"`
- AdminIds string `json:"admin_ids"`
- StartTime int `json:"starttime"`
- EndTime int `json:"endtime"`
- StopTime int `json:"stoptime"`
- State int `json:"state"`
- CreatedAt int `json:"created_at"`
- UpdatedAt int `json:"updated_at"`
- ManagerName string `json:"manager_name"`
- ManagerPhone string `json:"manager_phone"`
- DesignerName string `json:"designer_name"`
- DesignerPhone string `json:"designer_phone"`
- CreatedName string `json:"created_name"`
- AdminNames string `json:"admin_names"`
- }
- worksiteList := make([]WorksiteList, 0)
- if err = worksite.GetWorkSitesRaw(where, param, &worksiteList); err != nil {
- app.Error(c, err.Error())
- return
- }
- data := gin.H{
- "list": worksiteList,
- "total": total,
- "limit": page.PageSize,
- }
- app.Success(c, data)
- }
- func WorkSiteInfo(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.ErrorMsg(c, "department id must be a number", nil)
- return
- }
- type WorksiteInfo struct {
- ID int `json:"id"`
- PkgId int `json:"pkg_id"`
- Username string `json:"username"`
- Phone string `json:"phone"`
- Village string `json:"village"`
- Address string `json:"address"`
- RoomNo string `json:"room_no"`
- Area string `json:"area"`
- Longitude string `json:"longitude"`
- Latitude string `json:"latitude"`
- ManagerId int `json:"manager_id"`
- DesignerId int `json:"designer_id"`
- CreatedId int `json:"created_id"`
- AdminIds string `json:"admin_ids"`
- StartTime int `json:"starttime"`
- EndTime int `json:"endtime"`
- StopTime int `json:"stoptime"`
- State int `json:"state"`
- CreatedAt int `json:"created_at"`
- UpdatedAt int `json:"updated_at"`
- ManagerName string `json:"manager_name"`
- ManagerPhone string `json:"manager_phone"`
- DesignerName string `json:"designer_name"`
- DesignerPhone string `json:"designer_phone"`
- CreatedName string `json:"created_name"`
- AdminNames string `json:"admin_names"`
- Discount string `json:"discount"`
- Promotion string `json:"promotion"`
- Gifts string `json:"gifts"`
- Other string `json:"other"`
- Hose string `json:"hose"`
- Bear string `json:"bear"`
- }
- var worksiteInfo WorksiteInfo
- err := worksite.GetWorkSiteByID(id, &worksiteInfo)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, worksiteInfo)
- }
- func WorkSiteAdd(c *gin.Context) {
- var addForm form.WorkSiteAdd
- if app.Bind(c, &addForm) != nil {
- return
- }
- id, err := worksite.Add(addForm, c.GetInt("adminID"))
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, gin.H{"id": id})
- }
- func WorkSiteEdit(c *gin.Context) {
- id := utils.ToInt(c.Param("id"))
- if id <= 0 {
- app.ErrorMsg(c, "id must be a number", nil)
- return
- }
- var addForm form.WorkSiteEdit
- if app.Bind(c, &addForm) != nil {
- return
- }
- err := worksite.EditByID(addForm, id)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- func WorkSiteDispatch(c *gin.Context) {
- id := utils.ToInt(c.Param("id"))
- if id <= 0 {
- app.ErrorMsg(c, "id must be a number", nil)
- return
- }
- var adminForm form.WorkSiteAdminID
- if app.Bind(c, &adminForm) != nil {
- return
- }
- err := worksite.DispatchByID(adminForm, id)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- func WorkSiteStart(c *gin.Context) {
- id := utils.ToInt(c.Param("id"))
- if id <= 0 {
- app.ErrorMsg(c, "id must be a number", nil)
- return
- }
- err := worksite.StartByID(id, c.GetInt("adminID"))
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- func WorkSiteEditTime(c *gin.Context) {
- id := utils.ToInt(c.Param("id"))
- if id <= 0 {
- app.ErrorMsg(c, "id must be a number", nil)
- return
- }
- var timeForm form.WorkSiteTime
- if app.Bind(c, &timeForm) != nil {
- return
- }
- err := worksite.EditTime(timeForm, id, c.GetInt("adminID"))
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- func WorkSiteDel(c *gin.Context) {
- id := utils.ToInt(c.Param("id"))
- if id <= 0 {
- app.ErrorMsg(c, "id must be a number", nil)
- return
- }
- err := worksite.DeleteByID(id)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, nil)
- }
- type WorkSiteReturnVisitModel struct {
- ID int `json:"id"`
- SiteId int `json:"site_id"`
- Cycle int `json:"cycle"`
- PlanTime int `json:"plan_time"`
- ReturnVisitID int `json:"return_visit_id"`
- ReturnVisitContent string `json:"return_visit_content"`
- ReturnVisitAdminId int `json:"return_visit_admin_id"`
- ReturnVisitTime int `json:"return_visit_time"`
- ReturnVisitName string `json:"return_visit_name" prop:"select:admin.username"`
- ReturnVisitPhone string `json:"return_visit_phone" prop:"select:admin.phone"`
- ReturnVisitHeadimgurl string `json:"return_visit_headimgurl" prop:"select:admin.headimgurl"`
- ReturnVisitFrom map[int]form.ReturnVisitItem `json:"return_visit_from" prop:"select:false"`
- ReturnVisit *models.WorkReturnVisit `json:"return_visit" prop:"select:false"`
- models.WorkSiteReturnVisit
- }
- func (model WorkSiteReturnVisitModel) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
- return []db.JoinModel{
- {
- Model: models.Admin{},
- As: "admin",
- On: []string{
- "`admin`.`id` = " + model.TableName() + ".`return_visit_admin_id`",
- },
- },
- }
- }
- func WorkSiteReturnVisitInfo(c *gin.Context) {
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.ErrorMsg(c, "id must be a number", nil)
- return
- }
- var workSiteReturnVisitInfo *WorkSiteReturnVisitModel
- db.GetModel(map[string]interface{}{
- "id": id,
- }, &workSiteReturnVisitInfo)
- if workSiteReturnVisitInfo == nil {
- app.ErrorMsg(c, "id must be a number", nil)
- return
- }
- if workSiteReturnVisitInfo.ReturnVisitID != 0 {
- workSiteReturnVisitInfo.ReturnVisit = models.GetWorkReturnVisitModel(int64(workSiteReturnVisitInfo.ReturnVisitID), false)
- }
- utils.JsonDecode(workSiteReturnVisitInfo.ReturnVisitContent).To(&workSiteReturnVisitInfo.ReturnVisitFrom)
- app.Success(c, workSiteReturnVisitInfo)
- }
- func WorkSiteReturnVisit(c *gin.Context) {
- id := utils.ToInt(c.Param("id"))
- if id <= 0 {
- app.ErrorMsg(c, "id must be a number", nil)
- return
- }
- var returnForm form.ReturnVisitFrom
- if app.Bind(c, &returnForm) != nil {
- return
- }
- var workSiteReturnVisit *models.WorkSiteReturnVisit
- db.GetModel(map[string]interface{}{
- "id": id,
- }, &workSiteReturnVisit)
- if workSiteReturnVisit == nil {
- app.ErrorMsg(c, "invalid id", nil)
- return
- }
- if workSiteReturnVisit.ReturnVisitID == 0 {
- app.ErrorMsg(c, "无需回访", nil)
- return
- }
- data := map[string]interface{}{
- "return_visit_content": utils.JsonEncode(returnForm.ReturnVisit),
- }
- if workSiteReturnVisit.ReturnVisitContent == "" {
- data["return_visit_admin_id"] = c.GetInt("adminID")
- data["return_visit_time"] = time.Now().Unix()
- }
- err := db.UpdateModel(db.Type(workSiteReturnVisit), int64(id), data)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
|