import Magix from 'magix'
import * as View from '../../../view'
import * as Helper from 'dmp-new/views/mixins/helper'
Magix.applyStyle('@./create-crowd.less')
export default View.extend({
tmpl: '@subside-crowds.html',
async init (options) {
this.options = options
},
async render () {
const selected = ''
const templates = await this.getTemplates()
const { cates, shopId, permissions } = this.options
const mainCateId = cates[0] && cates[0].cateId
const _cates = [cates[0]] // 取第一个类目展示出来,未来可选择
// templates = {}
this.updater.digest({
$,
_cates,
mainCateId,
permissions,
shopId,
templates,
selected
})
},
async getTemplates () {
try {
const params = {
bizCode: this.options.bizCode,
empowerBizType: 1
}
const models = [{
name: 'appone_api_dmp_crowd_template_list_get',
params
}]
const [model] = await this.fetch(models)
const data = model.get('data.list', [])
// ext
const dataMap = {}
for (const crowd of data) {
try {
crowd.ext = JSON.parse(crowd.ext) || {}
} catch (error) {
crowd.ext = {}
}
// 根据ext.groupName分组下
if (!dataMap[crowd.ext.groupName]) {
dataMap[crowd.ext.groupName] = [crowd]
} else {
dataMap[crowd.ext.groupName].push(crowd)
}
// 按照热度排序下
for (const key in dataMap) {
dataMap[key].sort((a, b) => {
return b.ext.usageIndex - a.ext.usageIndex
})
}
// 汇总人群下所有标签的选择信息
if (crowd.crowdRule.selectTagOptionSetDTO) {
const _tagSets = []
Helper.recurSelectTagOptionSet([crowd.crowdRule.selectTagOptionSetDTO], selects => {
const tagSet = {
tagName: selects[0].tagName,
optionNames: []
}
for (const select of selects) {
tagSet.optionNames.push(select.names)
}
_tagSets.push(tagSet)
}, 'selectTagOptionSetDTO')
let _tagSetString = '人群描述'
// for (const tagSet of _tagSets) {
// _tagSetString += `
${tagSet.tagName}:${tagSet.optionNames.join('、')}
`
// }
_tagSetString += ` ${crowd.description}
`
crowd.crowdRule._tagSets = _tagSets
crowd.crowdRule._tagSetString = _tagSetString
}
}
return dataMap
} catch (error) {
this.alert('错误提示', error.msg || error.message || error, null, { mask: true })
}
},
// 汇总人群下所有标签的选择信息
sumTagSets (list) {
for (const crowd of list) {
if (crowd.selectTagOptionSetDTO) {
const _tagSets = []
Helper.recurSelectTagOptionSet([crowd.selectTagOptionSetDTO], selects => {
const tagSet = {
tagName: selects[0].tagName,
optionNames: []
}
for (const select of selects) {
tagSet.optionNames.push(select.names)
}
_tagSets.push(tagSet)
}, 'selectTagOptionSetDTO')
let _tagSetString = '人群描述'
for (const tagSet of _tagSets) {
_tagSetString += ` ${tagSet.tagName}:${tagSet.optionNames.join('、')}
`
}
crowd._tagSets = _tagSets
crowd._tagSetString = _tagSetString
}
}
},
// 'expand' (e) {
// const { template } = e.params
// const viewPath = '@./subside-expand'
// const viewOptions = {
// times: template.times || 10,
// callback: (times) => {
// template.times = times
// }
// }
// const dialogOptions = {
// width: 360,
// top: 100
// }
// this.mxDialog(viewPath, viewOptions, dialogOptions)
// },
async 'selectTmpl' (e) {
const selectedTmpl = this.getSelected()
const selectTagOptionSetDTO = selectedTmpl.crowdRule.selectTagOptionSetDTO
// 处理下values是变量的情形
// 同时计算下总共有多少个标签
this.setVariable(selectTagOptionSetDTO)
this.updater.set({
selectTagOptionSetDTO
})
// 父view里更新下覆盖数
const pv = this.owner.parent().$v
await pv.getCoverage()
const { scaleType } = pv.updater.get()
if (scaleType == 2) {
pv.getCoverageAdjust()
}
},
// 处理下values是变量的情形
// adapterType=1 或者 values有${shopId}的,赋值店铺id(shopId)
// adapterType=2 或者 values有${mainCateId}的,赋值主营类目id的第一个(mainCateId)
// 同时计算下总共有多少个标签
setVariable (selectTagOptionSetDTO) {
let count = 0
const { mainCateId, shopId, user } = this.updater.get()
// recurSelectTagOptionSet(selectTagOptionSet, cb, key = 'selectTagOptionSet')
Helper.recurSelectTagOptionSet([selectTagOptionSetDTO], selects => {
for (const select of selects) {
if (select.values === '${shopId}' || select.adapterType == 1) {
select.values = shopId.toString()
}
if (select.values === '${mainCateId}' || select.adapterType == 2) {
select.values = mainCateId.toString()
}
if (select.values === '${memberId}' || select.adapterType == 3) {
select.values = user.loginUser.userId.toString()
}
delete select.adapterType
}
count++
}, 'selectTagOptionSetDTO')
selectTagOptionSetDTO._selectedTagCounts = count
},
getSelected () {
const { selected, templates } = this.updater.get()
let selectedTmpl
if (selected) {
const [groupName, index] = selected.split('_')
for (const key in templates) {
if (key === groupName) {
templates[key].forEach((template, i) => {
if (i == index) {
selectedTmpl = template
}
})
}
}
}
return selectedTmpl
}
})