package final import ( "encoding/base64" "errors" "fmt" "log" "regexp" "strconv" "strings" "time" "zhiyuan/pkg/db" "zhiyuan/services/admin" "github.com/gin-gonic/gin" "github.com/xuri/excelize/v2" ) type FinalMaterialSettle struct { ID int64 `json:"id" prop:"add:false"` ShopId int64 `json:"shop_id" label:"门店" type:"int" prop:"add:false" default:"0" search:"="` SupplierId int64 `json:"supplier_id" label:"材料商" type:"int" prop:"add:false" default:"0" search:"="` CollectId int64 `json:"collect_id" label:"汇总" type:"int" prop:"add:false" default:"0" search:"="` Amount float64 `json:"amount" label:"订单金额" type:"float" prop:"add:false" search:"="` Deduction float64 `json:"deduction" label:"扣款金额" type:"float" prop:"add:false" search:"="` Total float64 `json:"total" label:"合计金额" type:"float" prop:"add:false" search:"="` State int64 `json:"state" label:"状态" type:"int" prop:"add:false" default:"0" search:"="` Orders string `json:"orders" label:"订单" type:"string" prop:"add:false" search:"find_in_set"` CreatedId int64 `json:"created_id" label:"创建人员" type:"int" prop:"add:false" search:"="` DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"` CreatedAt int64 `json:"created_at" prop:"add:false"` UpdatedAt int64 `json:"updated_at" prop:"add:false"` db.BaseModel } func (FinalMaterialSettle) TableName() string { return "zy_final_material_settle" } func (FinalMaterialSettle) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error { return errors.New("没有权限") } func (FinalMaterialSettle) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) { } func (model FinalMaterialSettle) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { return true } func (FinalMaterialSettle) OnePrivilege(c *gin.Context, id int64) bool { return true } func (FinalMaterialSettle) Page() bool { return false } func (FinalMaterialSettle) Count() bool { return true } type FinalMaterialSettleMobile struct { SupplierName string `json:"supplier_name" type:"string" prop:"select:supplier.name" search:"like"` SupplierAdminId string `json:"supplier_admin_id" type:"string" prop:"select:supplier.adminId"` Prepayment int64 `json:"prepayment" type:"int" prop:"select:supplier.prepayment"` AccountName string `json:"account_name" prop:"select:account.account_name"` AccountBank string `json:"account_bank" prop:"select:account.account_bank"` AccountNo string `json:"account_no" prop:"select:account.account_no"` ShopName string `json:"shop_name" prop:"select:shop.shop_name"` CollectInfo string `json:"collect_info" prop:"select:shop.collect_info"` FinalMaterialSettle } func (model FinalMaterialSettleMobile) GroupBy() string { return fmt.Sprintf("`%s`.`id`", model.TableName()) } func (model FinalMaterialSettleMobile) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { if !admin.IsSuperAdmin(c.GetInt("adminID")) { adminID := s.Param(c.GetInt("adminID")) where := fmt.Sprintf("`supplier`.`adminId` = %s OR `%s`.`created_id` = %s", adminID, model.TableName(), adminID) if admin.CheckAuth([]string{"final:verify"}, c.GetInt("adminID")) { info, _ := admin.GetInfoByID(c.GetInt("adminID"), nil, nil) if info == nil { return false } where = where + fmt.Sprintf(" OR FIND_IN_SET(`%s`.`shop_id`, %s)", model.TableName(), s.Param(info.ShopIds)) } s.Where = append(s.Where, fmt.Sprintf("(%s)", where)) } return model.FinalMaterialSettle.ListPrivilege(c, data, s) } func (model FinalMaterialSettleMobile) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel { return append(model.FinalMaterialSettle.LeftJoin(data, s), db.JoinModel{ Model: FinalSupplier{}, As: "supplier", On: []string{"`supplier`.`id` = " + model.TableName() + ".`supplier_id`"}, }, db.JoinModel{ Model: FinalSupplierAccount{}, As: "account", On: []string{"`account`.`id` = " + model.TableName() + ".`account_id`"}, }, db.JoinModel{ Model: JoinShop{}, As: "shop", On: []string{"`shop`.`id` = " + model.TableName() + ".`shop_id`"}, }) } type FinalMaterialSettlePC struct { FinalMaterialSettleMobile } func (model FinalMaterialSettlePC) ListAfter(c *gin.Context, data map[string]interface{}, list []map[string]interface{}) []map[string]interface{} { orderids := make([]int64, 0) orderidsMap := make(map[int64][]int64) for _, v := range list { ids := make([]int64, 0) id, _ := db.ToInt64(v["id"]) orders := db.ToString(v["orders"]) s := strings.Split(orders, ",") for _, i := range s { if n, ok := db.ToInt64(i); ok { ids = append(ids, n) } } orderids = append(orderids, ids...) orderidsMap[id] = ids } datas, _ := db.GetModelMap(db.Type(FinalMaterialOrderMobile{}), map[string]interface{}{ fmt.Sprintf("`%s`.`id` in", FinalMaterialOrderMobile{}.TableName()): orderids, fmt.Sprintf("`%s`.`deleted_at`", FinalMaterialOrderMobile{}.TableName()): 0, }, nil) FinalMaterialOrderMobile{}.ListAfter(c, data, datas) for n, settle := range list { id, _ := db.ToInt64(settle["id"]) if orderids, ok := orderidsMap[id]; ok { for _, v := range datas { orderid, _ := db.ToInt64(v["id"]) is := false for _, s := range orderids { if s == orderid { is = true break } } if is { order := make([]map[string]interface{}, 0) if s, ok := list[n]["order"].([]map[string]interface{}); ok { order = s } order = append(order, v) list[n]["order"] = order } } } } return list } func (model FinalMaterialSettlePC) ExportSpan() []string { return []string{"order", "items"} } func (model FinalMaterialSettlePC) ExportFields() []db.ExportField { return []db.ExportField{ { Label: "ID", Name: "id", }, { Label: "门店", Name: "shop_name", }, { Label: "订单号", Name: "order.order_no", Width: 30, }, { Label: "工地", Name: "order.address", Width: 20, }, { Label: "材料名称", Name: "order.items.mat_name", Width: 30, }, { Label: "单价", Name: "order.items.mat_price", }, { Label: "数量", Name: "order.items.num", }, { Label: "总价", Name: "order.items.total", }, { Label: "备注", Name: "order.items.remark", }, { Label: "订单金额", Name: "order.total", }, { Label: "订单合计", Name: "amount", }, { Label: "扣款金额", Name: "deduction", }, { Label: "材料商", Name: "supplier_name", Width: 20, }, { Label: "预付款", Name: "prepayment", }, { Label: "开户名", Name: "account_name", Width: 30, }, { Label: "开户行", Name: "account_bank", Width: 20, }, { Label: "账号", Name: "account_no", Width: 30, }, { Label: "结算金额", Name: "total", }, { Label: "状态", Name: "state", }, { Label: "财务审核", Name: "", }, { Label: "店长审核", Name: "", }, { Label: "财务审核", Name: "", }, } } func (FinalMaterialSettlePC) ExportValue(row map[string]interface{}, rowIndex int, field db.ExportField, data []map[string]interface{}) string { states := []string{"未提交", "工程队长审核", "财务审核", "店长审核", "财务付款", "已完成"} prepayments := []string{"否", "是"} switch field.Name { case "order.address": worksite := /*db.ToString(v["address"]) + " " +*/ db.ToString(row["order.village"]) + "" + db.ToString(row["order.room_no"]) if worksite == "" { worksite = db.ToString(row["order.address"]) } return worksite case "order.items.num": return db.ToString(row["order.items.num"]) + " " + db.ToString(row["order.items.unit"]) case "prepayment": prepayment := int64(0) if t, ok := db.ToInt64(row["prepayment"]); ok { prepayment = t } return prepayments[prepayment] case "state": state := int64(0) if t, ok := db.ToInt64(row["state"]); ok { state = t } return states[state] } return db.ToString(row[field.Name]) } func (FinalMaterialSettlePC) ExportAfter(data []map[string]interface{}, file *excelize.File) { ids := []interface{}{} collects := []interface{}{} for _, v := range data { ids = append(ids, v["id"]) collects = append(collects, v["collect_id"]) } collectMap := map[string]map[string]string{} ps, err := db.GetModelMap(db.Type(FinalMaterialSettleProcess{}), map[string]interface{}{ "collect_id in": collects, }, nil) if err != nil { return } for _, v := range ps { if _, ok := collectMap[db.ToString(v["collect_id"])]; !ok { collectMap[db.ToString(v["collect_id"])] = map[string]string{} } collectMap[db.ToString(v["collect_id"])][db.ToString(v["type"])] = db.ToString(v["signature"]) } for i, v := range data { //file.SetRowHeight("Sheet1", i+2, 40) if collect, ok := collectMap[db.ToString(v["collect_id"])]; ok { if signature, ok := collect[db.ToString(2)]; ok && signature != "" { signature = strings.Replace(signature, "data:image/png;base64,", "", 1) de, err := base64.StdEncoding.DecodeString(signature) if err == nil { cell, _ := excelize.CoordinatesToCellName(20, i+2) file.AddPictureFromBytes("Sheet1", cell, &excelize.Picture{ Extension: ".png", File: de, Format: &excelize.GraphicOptions{AutoFit: true}, }) } } if signature, ok := collect[db.ToString(3)]; ok && signature != "" { signature = strings.Replace(signature, "data:image/png;base64,", "", 1) de, err := base64.StdEncoding.DecodeString(signature) if err == nil { cell, _ := excelize.CoordinatesToCellName(21, i+2) file.AddPictureFromBytes("Sheet1", cell, &excelize.Picture{ Extension: ".png", File: de, Format: &excelize.GraphicOptions{AutoFit: true}, }) } } if signature, ok := collect[db.ToString(4)]; ok && signature != "" { signature = strings.Replace(signature, "data:image/png;base64,", "", 1) de, err := base64.StdEncoding.DecodeString(signature) if err == nil { cell, _ := excelize.CoordinatesToCellName(22, i+2) file.AddPictureFromBytes("Sheet1", cell, &excelize.Picture{ Extension: ".png", File: de, Format: &excelize.GraphicOptions{AutoFit: true}, }) } } } } } type FinalMaterialSettleCollect struct { FinalMaterialSettlePC } func (model FinalMaterialSettleCollect) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { if shopId, _ := db.ToInt64(data["shop_id"]); shopId == 0 { s.Where = append(s.Where, "(1!=1)") } return model.FinalMaterialSettleMobile.ListPrivilege(c, data, s) } type FinalMaterialSettleExport struct { FinalMaterialSettleMobile } func (model FinalMaterialSettleExport) ExportFields() []db.ExportField { return []db.ExportField{ { Label: "申请部门", Name: "shop_name", }, { Label: "申请日期", Name: "created_at", }, { Label: "收款单位", Name: "account_name", Width: 30, }, { Label: "开户银行", Name: "account_bank", Width: 20, }, { Label: "开户账号", Name: "account_no", Width: 30, }, { Label: "申请金额", Name: "dxtotal", }, { Label: "小写", Name: "total", }, } } func ConvertNumToCny(num float64) string { strnum := strconv.FormatFloat(num*100, 'f', 0, 64) sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"} // log.Println(sliceUnit[:len(sliceUnit)-2]) s := sliceUnit[len(sliceUnit)-len(strnum) : len(sliceUnit)] upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"} str := "" for k, v := range strnum[:] { str = str + upperDigitUnit[string(v)] + s[k] } reg, err := regexp.Compile(`零角零分$`) str = reg.ReplaceAllString(str, "整") reg, err = regexp.Compile(`零角`) str = reg.ReplaceAllString(str, "零") reg, err = regexp.Compile(`零分$`) str = reg.ReplaceAllString(str, "整") reg, err = regexp.Compile(`零[仟佰拾]`) str = reg.ReplaceAllString(str, "零") reg, err = regexp.Compile(`零{2,}`) str = reg.ReplaceAllString(str, "零") reg, err = regexp.Compile(`零亿`) str = reg.ReplaceAllString(str, "亿") reg, err = regexp.Compile(`零万`) str = reg.ReplaceAllString(str, "万") reg, err = regexp.Compile(`零*元`) str = reg.ReplaceAllString(str, "元") reg, err = regexp.Compile(`亿零{0, 3}万`) str = reg.ReplaceAllString(str, "^元") reg, err = regexp.Compile(`零元`) str = reg.ReplaceAllString(str, "零") if err != nil { log.Fatal(err) } return str } func (FinalMaterialSettleExport) ExportValue(row map[string]interface{}, rowIndex int, field db.ExportField, data []map[string]interface{}) string { switch field.Name { case "created_at": created_at := int64(0) if t, ok := db.ToInt64(row["created_at"]); ok { created_at = t } t := time.Unix(created_at, 0) return t.Format("2006.01.02") case "dxtotal": total := float64(0) if t, ok := db.ToFloat64(row["total"]); ok { total = t } return ConvertNumToCny(total) } return db.ToString(row[field.Name]) } type FinalMaterialSettlePC1 struct { FinalMaterialSettleMobile } func (model FinalMaterialSettlePC1) ListAfter(c *gin.Context, data map[string]interface{}, list []map[string]interface{}) []map[string]interface{} { orderids := make([]int64, 0) orderidsMap := make(map[int64][]int64) for _, v := range list { ids := make([]int64, 0) id, _ := db.ToInt64(v["id"]) orders := db.ToString(v["orders"]) s := strings.Split(orders, ",") for _, i := range s { if n, ok := db.ToInt64(i); ok { ids = append(ids, n) } } orderids = append(orderids, ids...) orderidsMap[id] = ids } datas, _ := db.GetModelMap(db.Type(FinalMaterialOrderMobile{}), map[string]interface{}{ fmt.Sprintf("`%s`.`id` in", FinalMaterialOrderMobile{}.TableName()): orderids, fmt.Sprintf("`%s`.`deleted_at`", FinalMaterialOrderMobile{}.TableName()): 0, }, nil) FinalMaterialOrderMobile{}.ListAfter(c, data, datas) for n, settle := range list { id, _ := db.ToInt64(settle["id"]) if orderids, ok := orderidsMap[id]; ok { for _, v := range datas { orderid, _ := db.ToInt64(v["id"]) is := false for _, s := range orderids { if s == orderid { is = true break } } if is { order := make([]map[string]interface{}, 0) if s, ok := list[n]["order"].([]map[string]interface{}); ok { order = s } order = append(order, v) list[n]["order"] = order } } } } return list } func (model FinalMaterialSettlePC1) ExportSpan() []string { return []string{"order"} } func (model FinalMaterialSettlePC1) ExportFields() []db.ExportField { return []db.ExportField{ { Label: "ID", Name: "id", }, { Label: "门店", Name: "shop_name", }, { Label: "订单号", Name: "order.order_no", Width: 30, }, { Label: "工地", Name: "order.address", Width: 20, }, { Label: "订单金额", Name: "order.total", }, { Label: "订单合计", Name: "amount", }, { Label: "扣款金额", Name: "deduction", }, { Label: "材料商", Name: "supplier_name", Width: 20, }, { Label: "预付款", Name: "prepayment", }, { Label: "开户名", Name: "account_name", Width: 30, }, { Label: "开户行", Name: "account_bank", Width: 20, }, { Label: "账号", Name: "account_no", Width: 30, }, { Label: "结算金额", Name: "total", }, { Label: "状态", Name: "state", }, { Label: "财务审核", Name: "", }, { Label: "店长审核", Name: "", }, { Label: "财务审核", Name: "", }, } } func (FinalMaterialSettlePC1) ExportValue(row map[string]interface{}, rowIndex int, field db.ExportField, data []map[string]interface{}) string { states := []string{"未提交", "工程队长审核", "财务审核", "店长审核", "财务付款", "已完成"} prepayments := []string{"否", "是"} switch field.Name { case "order.address": worksite := /*db.ToString(v["address"]) + " " +*/ db.ToString(row["order.village"]) + "" + db.ToString(row["order.room_no"]) if worksite == "" { worksite = db.ToString(row["order.address"]) } return worksite case "order.items.num": return db.ToString(row["order.items.num"]) + " " + db.ToString(row["order.items.unit"]) case "prepayment": prepayment := int64(0) if t, ok := db.ToInt64(row["prepayment"]); ok { prepayment = t } return prepayments[prepayment] case "state": if state, ok := db.ToInt64(row["state"]); ok { return states[state] } return "" } return db.ToString(row[field.Name]) } func (FinalMaterialSettlePC1) ExportAfter(data []map[string]interface{}, file *excelize.File) { ids := []interface{}{} collects := []interface{}{} for _, v := range data { ids = append(ids, v["id"]) collects = append(collects, v["collect_id"]) } collectMap := map[string]map[string]string{} ps, err := db.GetModelMap(db.Type(FinalMaterialSettleProcess{}), map[string]interface{}{ "collect_id in": collects, }, nil) if err != nil { return } for _, v := range ps { if _, ok := collectMap[db.ToString(v["collect_id"])]; !ok { collectMap[db.ToString(v["collect_id"])] = map[string]string{} } collectMap[db.ToString(v["collect_id"])][db.ToString(v["type"])] = db.ToString(v["signature"]) } for i, v := range data { //file.SetRowHeight("Sheet1", i+2, 40) if collect, ok := collectMap[db.ToString(v["collect_id"])]; ok { if signature, ok := collect[db.ToString(2)]; ok && signature != "" { signature = strings.Replace(signature, "data:image/png;base64,", "", 1) de, err := base64.StdEncoding.DecodeString(signature) if err == nil { cell, _ := excelize.CoordinatesToCellName(15, i+2) file.AddPictureFromBytes("Sheet1", cell, &excelize.Picture{ Extension: ".png", File: de, Format: &excelize.GraphicOptions{AutoFit: true}, }) } } if signature, ok := collect[db.ToString(3)]; ok && signature != "" { signature = strings.Replace(signature, "data:image/png;base64,", "", 1) de, err := base64.StdEncoding.DecodeString(signature) if err == nil { cell, _ := excelize.CoordinatesToCellName(16, i+2) file.AddPictureFromBytes("Sheet1", cell, &excelize.Picture{ Extension: ".png", File: de, Format: &excelize.GraphicOptions{AutoFit: true}, }) } } if signature, ok := collect[db.ToString(4)]; ok && signature != "" { signature = strings.Replace(signature, "data:image/png;base64,", "", 1) de, err := base64.StdEncoding.DecodeString(signature) if err == nil { cell, _ := excelize.CoordinatesToCellName(17, i+2) file.AddPictureFromBytes("Sheet1", cell, &excelize.Picture{ Extension: ".png", File: de, Format: &excelize.GraphicOptions{AutoFit: true}, }) } } } } } type FinalMaterialSettleCollect1 struct { FinalMaterialSettlePC1 } func (model FinalMaterialSettleCollect1) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { if shopId, _ := db.ToInt64(data["shop_id"]); shopId == 0 { s.Where = append(s.Where, "(1!=1)") } return model.FinalMaterialSettleMobile.ListPrivilege(c, data, s) }