123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package user
- import (
- "errors"
- "fmt"
- "time"
- "zhiyuan/models"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/admin"
- "zhiyuan/services/form"
- "zhiyuan/services/log"
- "zhiyuan/services/structs"
- )
- func GetHouseList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.UserHouse, error) {
- if page.PageNum > 0 && page.PageSize > 0 {
- where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
- }
- return UserHouse.GetMulti(where, fields, retVal)
- }
- func GetHouseInfoByID(id int, fields []string, retVal interface{}) (*models.UserHouse, error) {
- return UserHouse.GetOne(map[string]interface{}{"id": id}, fields, retVal)
- }
- func GetHouseMap(form form.HouseAdd) (map[string]interface{}, error) {
- houseMap := map[string]interface{}{
- "user_id": form.UserID,
- "pkg_id": form.PkgID,
- "shop_id": form.ShopID,
- "district": form.District,
- "salesman": form.Salesman,
- "project_manager": form.ProjectManager,
- "project_leader": form.ProjectLeader,
- "address": form.Address,
- "designer": form.Designer,
- "supervisor": form.Supervisor,
- "project_start": utils.DateParseUnix(form.ProjectStart, "Y-m-d"),
- "mark": form.Mark,
- }
- if userInfo, _ := GetOne(map[string]interface{}{"id": form.UserID}, nil, nil); userInfo == nil {
- return nil, errors.New("用户不存在")
- } else {
- houseMap["link_name"] = userInfo.Name
- houseMap["link_phone"] = userInfo.Phone
- return houseMap, nil
- }
- }
- func HouseAdd(form form.HouseAdd) (int64, error) {
- if houseMap, err := GetHouseMap(form); err != nil {
- return 0, err
- } else {
- if admin.CheckAuth([]string{"user:warranty:edit"}, form.AdminID) {
- houseMap["project_end"] = utils.DateParseUnix(form.ProjectEnd, "Y-m-d")
- houseMap["warranty_start"] = utils.DateParseUnix(form.WarrantyStart, "Y-m-d")
- houseMap["warranty_mark"] = form.WarrantyMark
- houseMap["warranty_period_base"] = form.WarrantyPeriodBase
- houseMap["warranty_period_material"] = form.WarrantyPeriodMaterial
- houseMap["warranty_period_electric"] = form.WarrantyPeriodElectric
- houseMap["warranty_period_base_adddate"] = form.WarrantyPeriodBaseAddDate
- houseMap["warranty_period_material_adddate"] = form.WarrantyPeriodMaterialAddDate
- houseMap["warranty_period_electric_adddate"] = form.WarrantyPeriodElectricAddDate
- houseMap["pictures"] = form.Pictures
- }
- return db.InsertOne(UserHouse.TableName(), houseMap)
- }
- }
- func HouseEdit(f form.HouseAdd, id int) (int64, error) {
- houseInfo, _ := GetHouseInfoByID(id, nil, nil)
- if houseMap, err := GetHouseMap(f); err != nil {
- return 0, err
- } else {
- if admin.CheckAuth([]string{"user:warranty:edit"}, f.AdminID) {
- houseMap["project_end"] = utils.DateParseUnix(f.ProjectEnd, "Y-m-d")
- houseMap["warranty_start"] = utils.DateParseUnix(f.WarrantyStart, "Y-m-d")
- houseMap["warranty_mark"] = f.WarrantyMark
- houseMap["warranty_period_base"] = f.WarrantyPeriodBase
- houseMap["warranty_period_material"] = f.WarrantyPeriodMaterial
- houseMap["warranty_period_electric"] = f.WarrantyPeriodElectric
- houseMap["warranty_period_base_adddate"] = f.WarrantyPeriodBaseAddDate
- houseMap["warranty_period_material_adddate"] = f.WarrantyPeriodMaterialAddDate
- houseMap["warranty_period_electric_adddate"] = f.WarrantyPeriodElectricAddDate
- houseMap["pictures"] = f.Pictures
- }
- go func() {
- log.Add(form.LogAdd{
- Type: log.TypeMap.EditUser.ID,
- Title: "编辑房屋信息_" + utils.ToStr(id),
- UserID: f.UserID,
- AdminID: f.AdminID,
- Content: map[string]interface{}{
- "before": houseInfo,
- "after": houseMap,
- },
- })
- }()
- return db.Update(UserHouse.TableName(), map[string]interface{}{"id": id}, houseMap)
- }
- }
- func GetHouseCustomerMap(form form.HouseAddByCustomer) map[string]interface{} {
- return map[string]interface{}{
- "user_id": form.UserID,
- "address": form.Address,
- "link_name": form.LinkName,
- "link_phone": form.LinkPhone,
- "origin": 2,
- }
- }
- func HouseEditByCustomer(form form.HouseAddByCustomer, id int) error {
- houseInfo, err := GetHouseInfoByID(id, []string{"user_id"}, nil)
- if err != nil {
- return err
- }
- if houseInfo.UserID != form.UserID {
- return errors.New("无法编辑该房屋")
- }
- houseMap := GetHouseCustomerMap(form)
- _, err = db.Update(UserHouse.TableName(), map[string]interface{}{"id": id}, houseMap)
- return err
- }
- func HouseAddByCustomer(form form.HouseAddByCustomer) (int64, error) {
- houseMap := GetHouseCustomerMap(form)
- return db.InsertOne(UserHouse.TableName(), houseMap)
- }
- func InsertOneHouse(data map[string]interface{}) (int64, error) {
- return db.InsertOne(UserHouse.TableName(), data)
- }
- func HouseDelByID(id int) (int64, error) {
- return db.DeleteSoft(UserHouse.TableName(), map[string]interface{}{"id": id})
- }
- func GetWarrantyPeriod(warrantyStart int64, warrantyType *structs.WarrantyType, houseItem *structs.HouseList) *structs.WarrantyType {
- switch warrantyType.ID {
- case 1:
- warrantyType.WarrantyPeriod = houseItem.WarrantyPeriodMaterial
- warrantyType.WarrantyPeriodAddDate = houseItem.WarrantyPeriodMaterialAddDate
- case 2:
- warrantyType.WarrantyPeriod = houseItem.WarrantyPeriodBase
- warrantyType.WarrantyPeriodAddDate = houseItem.WarrantyPeriodBaseAddDate
- case 3:
- warrantyType.WarrantyPeriod = houseItem.WarrantyPeriodElectric
- warrantyType.WarrantyPeriodAddDate = houseItem.WarrantyPeriodElectricAddDate
- }
- currentTime := utils.ToInt(utils.GetZeroTime(time.Now()).Unix())
- warrantyType.WarrantyEnd = utils.ToInt(time.Unix(warrantyStart, 0).AddDate(warrantyType.WarrantyPeriod, 0, warrantyType.WarrantyPeriodAddDate).Unix())
- warrantyType.LeftDate = (warrantyType.WarrantyEnd - currentTime) / 86400
- if currentTime >= utils.ToInt(warrantyStart) && currentTime <= warrantyType.WarrantyEnd {
- warrantyType.InWarranty = 1
- }
- fmt.Println(utils.ToStr(warrantyType.ID) + "======" + utils.ToStr(warrantyType.WarrantyPeriod))
- return warrantyType
- }
|