|
- package svc
-
- import (
- "applet/app/admin/md"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/enum"
- "applet/app/utils"
- "errors"
- "fmt"
- "time"
- )
-
- func DeleteFactoryIdentity(id int) (err error) {
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
-
- //1、删除 `role`
- factoryIdentityDb := db.FactoryIdentityDb{}
- factoryIdentityDb.Set(id)
- _, err = factoryIdentityDb.FactoryIdentityDeleteBySession(session, id)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- //2、删除 `factory_identity_with_user_identity`
- factoryIdentityWithUserIdentityDb := db.FactoryIdentityWithUserIdentityDb{}
- factoryIdentityWithUserIdentityDb.Set()
- _, err = factoryIdentityWithUserIdentityDb.FactoryIdentityWithUserIdentityDeleteForRoleBySession(session, id)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- return session.Commit()
- }
-
- //func RoleBindPermissionGroup(req md.RoleBindPermissionGroupReq) (err error) {
- // session := db.Db.NewSession()
- // defer session.Close()
- // session.Begin()
- // //1、查询 `role`
- // roleDb := db.RoleDb{}
- // roleDb.Set(req.RoleId)
- // role, err := roleDb.GetRole()
- // if err != nil {
- // return
- // }
- // if role == nil {
- // return errors.New("未查询到相应记录")
- // }
- //
- // //1、删除 `role_permission_group`
- // rolePermissionGroupDb := db.RolePermissionGroupDb{}
- // rolePermissionGroupDb.Set()
- // _, err = rolePermissionGroupDb.RolePermissionGroupDeleteForRoleBySession(session, req.RoleId)
- // if err != nil {
- // _ = session.Rollback()
- // return
- // }
- //
- // //2、新增 `role_permission_group``
- // var mm []*model.RolePermissionGroup
- // now := time.Now()
- // for _, v := range req.PermissionIds {
- // mm = append(mm, &model.RolePermissionGroup{
- // RoleId: role.Id,
- // GroupId: v,
- // CreateAt: now.Format("2006-01-02 15:04:05"),
- // UpdateAt: now.Format("2006-01-02 15:04:05"),
- // })
- // }
- // _, err = rolePermissionGroupDb.BatchAddRolePermissionGroupBySession(session, mm)
- // if err != nil {
- // _ = session.Rollback()
- // return
- // }
- //
- // return session.Commit()
- //}
-
- func BindUserFactoryIdentity(req md.BindFactoryIdentityReq) (err error) {
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
- //1、查询 `role`
- userIdentityDb := db.UserIdentityDb{}
- userIdentityDb.Set(0)
- role, err := userIdentityDb.GetUserIdentity(req.UserIdentitiesId)
- if err != nil {
- return
- }
- if role == nil {
- return errors.New("未查询到相应记录")
- }
-
- //2、删除 `factory_identity_with_user_identity`
- factoryIdentityWithUserIdentityDb := db.FactoryIdentityWithUserIdentityDb{}
- factoryIdentityWithUserIdentityDb.Set()
- _, err = factoryIdentityWithUserIdentityDb.FactoryIdentityWithUserIdentityDeleteForRoleBySession(session, req.FactoryIdentityId)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- //3、新增 `删除 `factory_identity_with_user_identity``
- now := time.Now()
- _, err = factoryIdentityWithUserIdentityDb.AddFactoryIdentityWithUserIdentityBySession(session, &model.FactoryIdentityWithUserIdentity{
- UserIdentityId: req.UserIdentitiesId,
- FactoryIdentityId: req.FactoryIdentityId,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- return session.Commit()
- }
-
- func FactoryInfo(enterpriseId int) (err error, resp md.FactoryInfoResp) {
- //1、查询`enterprise`
- enterpriseDb := db.EnterpriseDb{}
- enterpriseDb.Set()
- enterprise, err := enterpriseDb.GetEnterprise(enterpriseId)
- if err != nil {
- return
- }
- resp.Name = enterprise.Name
- resp.Memo = enterprise.Memo
- resp.Kind = enum.EnterpriseKind(enterprise.Kind).String()
- resp.Mode = enum.EnterpriseMode(enterprise.Mode).String()
- resp.Pvd = enum.EnterprisePvd(enterprise.Pvd).String()
- resp.State = enum.EnterpriseState(enterprise.State).String()
-
- //2、统计 "用户"、 "工人" 数量
- userIdentityDb := db.UserIdentityDb{}
- userIdentityDb.Set(0)
- resp.WorkerNum, err = userIdentityDb.CountUserIdentityForEnterprise(enterpriseId, enum.UserIdentityForFactory)
- if err != nil {
- return
- }
- //userDb := db.UserDb{}
- //userDb.Set()
- //resp.UserNum, err = userDb.CountUserForEnterprise(enterpriseId)
- //if err != nil {
- // return
- //}
-
- //3、统计 订单数据
- now := time.Now()
- today := now.AddDate(0, 0, +1).Format("2006-01-02") //今日
- thisMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).Format("2006-01-02") //本月
- sql1 := fmt.Sprintf("SELECT SUM(total_amount) AS this_month_consume_amount FROM factory_ord where gmt_payment >= '%s' and gmt_payment <= '%s'", thisMonth, today)
- sql2 := fmt.Sprintf("SELECT count(*) AS this_month_order_num FROM factory_ord where gmt_payment >= '%s' and gmt_payment <= '%s'", thisMonth, today)
- sql3 := fmt.Sprintf("SELECT SUM(service_fee) AS this_month_service_amount FROM factory_ord where gmt_payment >= '%s' and gmt_payment <= '%s'", thisMonth, today)
- sql4 := fmt.Sprintf("SELECT SUM(receipt_amount) AS this_month_receipt_amount FROM factory_ord where gmt_payment >= '%s' and gmt_payment <= '%s'", thisMonth, today)
-
- mapArr1, err := db.QueryNativeString(db.Db, sql1)
- if err != nil {
- return
- }
- if mapArr1 == nil {
- resp.ThisMonthConsumeAmount = 0
- } else {
- resp.ThisMonthConsumeAmount = utils.StrToFloat64(mapArr1[0]["this_month_consume_amount"])
- }
-
- mapArr2, err := db.QueryNativeString(db.Db, sql2)
- if err != nil {
- return
- }
- if mapArr2 == nil {
- resp.ThisMonthOrderNum = 0
- } else {
- resp.ThisMonthOrderNum = utils.StrToFloat64(mapArr2[0]["this_month_order_num"])
- }
-
- mapArr3, err := db.QueryNativeString(db.Db, sql3)
- if err != nil {
- return
- }
- if mapArr3 == nil {
- resp.ThisMonthServiceAmount = 0
- } else {
- resp.ThisMonthServiceAmount = utils.StrToFloat64(mapArr3[0]["this_month_service_amount"])
- }
-
- mapArr4, err := db.QueryNativeString(db.Db, sql4)
- if err != nil {
- return
- }
-
- if mapArr4 == nil {
- resp.ThisMonthReceiptAmount = 0
- } else {
- resp.ThisMonthReceiptAmount = utils.StrToFloat64(mapArr4[0]["receipt_amount"])
- }
- return
- }
|