最近遇到一个需求,要求给设备组下的所有商品设置商品会员价格,其流程为:选择会员等级和设备组获取未给此设备组设定该会员价格的商品,页面自动生成商品价格表单,进行设定商品会员价格。实现的代码如下:
一、前端:
1.HTML
<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible"> <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm"> <el-form-item label="会员等级" prop="levelId"> <el-select v-model="form.levelId" @change="selectLevelOption($event)" filterable placeholder="请选择会员"> <el-option v-for="item in levels" :key="item.id" :label="item.levelName" :value="item.id"></el-option> </el-select> </el-form-item> <el-form-item label="设备组" prop="dgId"> <el-select v-model="form.dgId" @change="selectGoodsByGroupIdAdd($event)" filterable placeholder="请选择设备组"> <el-option v-for="item in deviceGroups" :key="item.id" :label="item.groupName" :value="item.id"></el-option> </el-select> </el-form-item> <el-tabs type="border-card"> <el-tab-pane> <span slot="label"><i class="el-icon-arrow-right"></i>设置商品会员价格</span> <el-form-item v-for="item in domains" :label="item.goodsName" :key="item.goodsId"> <el-input v-model.number="item.goodsPrice" min="0" step="0.5" style="width:220px" type="number" placeholder="请输入会员价格"></el-input> </el-form-item> </el-tab-pane> </el-tabs> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="cancel('form')">取 消</el-button> <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button> </div> </el-dialog>
2.JS
selectLevelOption(levelId){//选择会员等级时触发
let dgId = this.form.dgId;
if (dgId !=null && dgId != "" && dgId != undefined){
//先将domains数组清空
this.domains = [];
/* let formData = new FormData();
formData.append("groupId",dgId);
formData.append("levelId",levelId);*/
let query = {
groupId:dgId,
levelId:levelId
};
selectGoodsByDgIdLevelId(query)
.then(response => {
if (response.length == 0){
this.$message({
showClose:true,
message:'目前没有商品可设置会员价格!',
type:'warning'
})
} else {
for (let i = 0; i < response.length; i++) {
this.domains.push({
goodsName:response[i].goodsName,
goodsId:response[i].id,
goodsPrice:'',
});
}
}
})
}
},
selectGoodsByGroupIdAdd(val){//根据设备组id获取相应的商品(添加使用)
let levelId = this.form.levelId;
if(levelId ==null || levelId == "" || levelId == undefined){
this.$message({
showClose:true,
message:'请选择会员等级',
type:"warning"
})
}else {
//先将domains数组清空
this.domains = [];
//console.log(val);
if(val != null && val != '' && val != undefined){
/*let formData = new FormData();
formData.append("groupId",val);
formData.append("levelId",levelId);*/
let query = {
groupId:val,
levelId:levelId
};
selectGoodsByDgIdLevelId(query)
.then(response => {
if (response.length == 0){
this.$message({
showClose:true,
message:'目前没有商品可设置会员价格!',
type:'warning'
})
} else {
for (let i = 0; i < response.length; i++) {
this.domains.push({
goodsName:response[i].goodsName,
goodsId:response[i].id,
goodsPrice:'',
});
}
}
})
}
}
},
create(formName) {
const set = this.$refs;
set[formName].validate(valid => {
if (valid) {
//console.log(JSON.stringify(this.domains));
let formData = new FormData();
formData.append("domains",JSON.stringify(this.domains));
formData.append("dgId",this.form.dgId);
formData.append("levelId",this.form.levelId);
//formData.append("isDelete","0");
addObj(formData)
.then(response => {
if (response.status == 500) {
this.dialogFormVisible = false;
this.$message({
showClose:true,
message:'添加失败,请检查商品是否已经设置过会员价格!',
type:'error'
})
}else {
this.dialogFormVisible = false;
this.getList();
this.$notify({
title: '成功',
message: '创建成功',
type: 'success',
duration: 2000
});
}
})
} else {
return false;
}
});
},
//跨域的路径
export function selectGoodsByDgIdLevelId(query) {
return fetch({
url:'/api/goods/goods/selectGoodsByDgIdLevelId',
method:'get',
params: query
})
}
export function selectGoodsByDgIdLevelId(query) {
return fetch({
url:'/api/goods/goods/selectGoodsByDgIdLevelId',
method:'get',
params: query
})
}
二、后端:
1.controller层
@RequestMapping(value = "/save", method = RequestMethod.POST)
@ResponseBody
public ObjectRestResponse<LevelDevicePrice> save(@RequestParam("domains") String domains,LevelDevicePrice levelDevicePrice){
ObjectRestResponse<LevelDevicePrice> objectRestResponse = new ObjectRestResponse<>();
List<LevelDevicePrice> levelDevicePrices = null;
Map<String, Object> params = new HashMap<>();
params.put("dg_id",levelDevicePrice.getDgId());
params.put("level_id",levelDevicePrice.getLevelId());
List<LevelDevicePrice> list = JSON.parseArray(domains,LevelDevicePrice.class);
if(list.size()>0){
for (int i = 0; i < list.size(); i++) {
params.put("goods_id",list.get(i).getGoodsId());
levelDevicePrices = baseBiz.selectByProperty(params);
if(levelDevicePrices.size()>0){
break;
}
}
if (levelDevicePrices.size()>0){
objectRestResponse.setStatus(500);
objectRestResponse.setMessage("添加失败!");
}else {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getGoodsPrice() != null){
list.get(i).setDgId(levelDevicePrice.getDgId());
list.get(i).setLevelId(levelDevicePrice.getLevelId());
list.get(i).setIsDelete("0");
baseBiz.insertSelective(list.get(i));
}
}
}
}
return objectRestResponse;
}
2.biz层
/**
* 根据property动态的条件获取数据
* @param params
* @return
*/
public List<LevelDevicePrice> selectByProperty(Map<String, Object> params) {
return mapper.selectByProperty(params);
}
3.mapper层
/**
* 根据property动态的条件获取数据
* @param params
*/
List<LevelDevicePrice> selectByProperty(@Param("params") Map<String, Object> params);
4.mybatis.xml
<resultMap id="BaseResultMap" type="com.jmk.frame.goods.common.entity.LevelDevicePrice">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="com_id" jdbcType="INTEGER" property="companyId"/>
<result column="level_id" jdbcType="INTEGER" property="levelId"/>
<result column="dg_id" jdbcType="INTEGER" property="dgId"/>
<result column="goods_id" jdbcType="INTEGER" property="goodsId"/>
<result column="goods_price" jdbcType="DECIMAL" property="goodsPrice"/>
<result column="create_time" jdbcType="DATE" property="createTime"/>
<result column="update_time" jdbcType="DATE" property="updateTime"/>
<result column="is_delete" jdbcType="VARCHAR" property="isDelete"/>
<result column="create_person" jdbcType="INTEGER" property="createPerson"/>
<result column="update_person" jdbcType="INTEGER" property="updatePerson"/>
<result column="user_host" jdbcType="VARCHAR" property="userHost"/>
<result column="upd_host" jdbcType="VARCHAR" property="updHost"/>
</resultMap>
<!-- selectByProperty方法 -->
<select id="selectByProperty" parameterType="java.util.Map" resultMap="BaseResultMap">
select * from ue_tb_level_device_price
<where>
<foreach collection="params" item="item" index="key">
<if test="key == 'dg_id'">
and ${key} = #{item}
</if>
<if test="key == 'level_id'">
and ${key} = #{item}
</if>
<if test="key == 'goods_id'">
and ${key} = #{item}
</if>
</foreach>
</where>
</select>