文章目录
- 1.setmeal.html
- 2.SetmealControlle.java
- 3.SetmealService.java
- 4.SetmealServiceImpl.java
- 5.SetmealDao.java
- 6.SetmealDao.xml
由于此笔记是完成整体项目之后总结的,因此下面代码是完整代码,有的是前面详细功能中没有提及的在后续功能实现中也会陆续介绍到。
1.setmeal.html
<!DOCTYPE html>
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>传智健康</title>
<meta name="description" content="传智健康">
<meta name="keywords" content="传智健康">
<meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
<!-- 引入样式 -->
<link rel="stylesheet" href="../plugins/elementui/index.css">
<link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../css/style.css">
<!-- 引入组件库 -->
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<style>
.avatar-uploader .el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader .el-upload:hover {
border-color: #409EFF;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
}
.avatar {
width: 178px;
height: 178px;
display: block;
}
.datatable {
position: relative;
box-sizing: border-box;
-webkit-box-flex: 1;
width: 100%;
max-width: 100%;
font-size: 14px;
color: rgb(96, 98, 102);
overflow: hidden;
flex: 1 1 0%;
}
.datatable td, .datatable th {
padding: 12px 0;
min-width: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
text-overflow: ellipsis;
vertical-align: middle;
position: relative;
text-align: left;
}
</style>
</head>
<body class="hold-transition">
<div id="app">
<div class="content-header">
<h1>预约管理<small>套餐管理</small></h1>
<el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
<el-breadcrumb-item>预约管理</el-breadcrumb-item>
<el-breadcrumb-item>套餐管理</el-breadcrumb-item>
</el-breadcrumb>
</div>
<div class="app-container">
<div class="box">
<div class="filter-container">
<el-input placeholder="编码/名称/助记码" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
<el-button @click="findPage()" class="dalfBut">查询</el-button>
<el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
</div>
<el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
<el-table-column type="index" align="center" label="序号"></el-table-column>
<el-table-column prop="code" label="套餐编码" align="center"></el-table-column>
<el-table-column prop="name" label="套餐名称" align="center"></el-table-column>
<el-table-column label="适用性别" align="center">
<template slot-scope="scope">
<span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
</template>
</el-table-column>
<el-table-column prop="age" label="适用年龄" align="center"></el-table-column>
<el-table-column prop="helpCode" label="助记码" align="center"></el-table-column>
<el-table-column prop="remark" label="说明" align="center"></el-table-column>
<el-table-column label="操作" align="center">
<template slot-scope="scope">
<el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
<el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<div class="pagination-container">
<el-pagination
class="pagiantion"
@current-change="handleCurrentChange"
:current-page="pagination.currentPage"
:page-size="pagination.pageSize"
layout="total, prev, pager, next, jumper"
:total="pagination.total">
</el-pagination>
</div>
<!-- 新增标签弹层 -->
<div class="add-form">
<el-dialog title="新增套餐" :visible.sync="dialogFormVisible">
<template>
<el-tabs v-model="activeName" type="card">
<el-tab-pane label="基本信息" name="first">
<el-form label-position="right" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="编码">
<el-input v-model="formData.code"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="名称">
<el-input v-model="formData.name"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="适用性别">
<el-select v-model="formData.sex">
<el-option label="不限" value="0"></el-option>
<el-option label="男" value="1"></el-option>
<el-option label="女" value="2"></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="助记码">
<el-input v-model="formData.helpCode"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="套餐价格">
<el-input v-model="formData.price"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="适用年龄">
<el-input v-model="formData.age"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="上传图片">
<!--el-upload文件上传组件
-->
<el-upload
class="avatar-uploader"
action="/setmeal/upload.do"
:auto-upload="autoUpload"
name="imgFile"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<!--大图标-->
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="说明">
<el-input v-model="formData.remark" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="注意事项">
<el-input v-model="formData.attention" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
</el-tab-pane>
<el-tab-pane label="检查组信息" name="second">
<div class="checkScrol">
<table class="datatable">
<thead>
<tr>
<th>选择</th>
<th>项目编码</th>
<th>项目名称</th>
<th>项目说明</th>
</tr>
</thead>
<tbody>
<tr v-for="c in tableData">
<td>
<input :id="c.id" v-model="checkgroupIds" type="checkbox" :value="c.id">
</td>
<td><label :for="c.id">{{c.code}}</label></td>
<td><label :for="c.id">{{c.name}}</label></td>
<td><label :for="c.id">{{c.remark}}</label></td>
</tr>
</tbody>
</table>
</div>
</el-tab-pane>
</el-tabs>
</template>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取消</el-button>
<el-button type="primary" @click="handleAdd()">确定</el-button>
</div>
</el-dialog>
</div>
<!-- 编辑套餐 -->
<div class="add-form">
<el-dialog title="编辑套餐" :visible.sync="dialogFormVisible4Edit">
<template>
<el-tabs v-model="activeName" type="card">
<el-tab-pane label="基本信息" name="first">
<el-form label-position="right" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="编码">
<el-input v-model="formData.code"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="名称">
<el-input v-model="formData.name"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="适用性别">
<el-select v-model="formData.sex">
<el-option label="不限" value="0"></el-option>
<el-option label="男" value="1"></el-option>
<el-option label="女" value="2"></el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="助记码">
<el-input v-model="formData.helpCode"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="套餐价格">
<el-input v-model="formData.price"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="适用年龄">
<el-input v-model="formData.age"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="上传图片">
<el-upload
class="avatar-uploader"
action="/setmeal/upload.do"
:auto-upload="autoUpload"
name="imgFile"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="说明">
<el-input v-model="formData.remark" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="注意事项">
<el-input v-model="formData.attention" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
</el-tab-pane>
<el-tab-pane label="检查组信息" name="second">
<div class="checkScrol">
<table class="datatable">
<thead>
<tr>
<th>选择</th>
<th>项目编码</th>
<th>项目名称</th>
<th>项目说明</th>
</tr>
</thead>
<tbody>
<tr v-for="c in tableData">
<td>
<input :id="c.id" v-model="checkgroupIds" type="checkbox" :value="c.id">
</td>
<td><label :for="c.id">{{c.code}}</label></td>
<td><label :for="c.id">{{c.name}}</label></td>
<td><label :for="c.id">{{c.remark}}</label></td>
</tr>
</tbody>
</table>
</div>
</el-tab-pane>
</el-tabs>
</template>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取消</el-button>
<el-button type="primary" @click="handleEdit()">确定</el-button>
</div>
</el-dialog>
</div>
</div>
</div>
</div>
</body>
<script>
var vue = new Vue({
el: '#app',
data:{
autoUpload:true,//自动上传
imageUrl:null,//模型数据,用于上传图片完成后图片预览
activeName:'first',//添加/编辑窗口Tab标签名称
pagination: {//分页相关属性
currentPage: 1,
pageSize:10,
total:100,
queryString:null,
},
dataList: [],//列表数据
formData: {},//表单数据
tableData:[],//添加表单窗口中检查组列表数据
checkgroupIds:[],//添加表单窗口中检查组复选框对应id
dialogFormVisible: false,//控制添加窗口显示/隐藏
dialogFormVisible4Edit:false//控制编辑窗口显示/隐藏
},
created() {
this.findPage();
},
methods: {
//文件上传成功后的钩子,response为服务端返回的值,file为当前上传的文件封装成的js对象
//response=response.data
handleAvatarSuccess(response, file) {
//回显图片(上传使用)
this.imageUrl="http://qn6m5pe0x.hd-bkt.clouddn.com/"+response.data;
//要将图片的名称设置到表单中(保存到套餐中使用)
this.formData.img=response.data;
},
//上传图片之前执行
beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpeg';
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('上传套餐图片只能是 JPG 格式!');
}
if (!isLt2M) {
this.$message.error('上传套餐图片大小不能超过 2MB!');
}
return isJPG && isLt2M;
},
//添加
handleAdd () {
//2.窗口关闭
this.dialogFormVisible = false;
//3.提交数据 套餐对象 检查组ids
axios.post("/setmeal/add.do?checkgroupIds="+this.checkgroupIds,this.formData).then(res=>{
//成功或失败提示
this.$message({
message: res.data.message,
type: res.data.flag ? 'success':"error"
});
//查询数据
this.findPage();
})
},
//分页查询
findPage() {
var queryPageBean={
currentPage: this.pagination.currentPage,//当前页码
pageSize: this.pagination.pageSize,//当前显示条数
queryString: this.pagination.queryString//查询条件
}
axios.post("/setmeal/findPage.do",queryPageBean).then((res)=>{
this.pagination.total=res.data.total;
this.dataList=res.data.rows;
})
},
//编辑
handleEdit() {
//发送ajax请求,提交模型数据
axios.post("/setmeal/edit.do?checkgroupIds="+this.checkgroupIds,this.formData).then((res)=> {
//隐藏编辑窗口
this.dialogFormVisible4Edit = false;
// 返回true,表示成功,否则表示失败
if(res.data.flag){
this.$message({
message: res.data.message,
type: 'success'
});
}else{
this.$message.error(res.data.message);
}
}).finally(()=> {
this.findPage();
});
},
//编辑
//前端:编辑->回显套餐数据,检查组列表,检查组跟套餐关系勾选
//修改套餐数据->提交套餐对象,检查组ids
//后端:更新套餐表 先删除套餐检查组的关系 重新建立套餐检查组的关系
handleUpdate(row) {
//1.弹出编辑窗口
//this.dialogFormVisible4Edit=true;
//2.根据套餐id回显
//3.查询所有检查组数据
//4.根据套餐id查询关联的检查组ids
axios.get("/setmeal/findById.do?id=" + row.id).then((res)=>{
if(res.data.flag){
//1.弹出编辑窗口
this.dialogFormVisible4Edit = true;
//默认选中第一个标签页
this.activeName='first';
//为模型数据赋值,通过VUE数据双向绑定进行信息的回显
this.formData = res.data.data;
this.$message({
type:"success",
message:res.data.message
})
// 回显图片在指定的正方形的输入框
this.imageUrl = "http://qn6m5pe0x.hd-bkt.clouddn.com/"+res.data.data.img;
//2:发送ajax请求查询所有的检查组信息,用于检查组表格列表展示
axios.get("/checkgroup/findAll.do").then((res)=> {
if(res.data.flag){
//为模型数据赋值,通过VUE数据双向绑定进行信息的回显
this.tableData = res.data.data;
//3:查询当前检查组包含的所有检查项id,用于页面复选框回显
axios.get("/setmeal/findCheckGroupIdsBySetmealId.do?id=" + row.id).then((res)=> {
//为模型数据赋值,通过VUE数据双向绑定进行信息的回显
this.checkgroupIds = res.data;
});
}else{
this.$message.error(res.data.message);
}
});
}else{
this.$message.error(res.data.message);
}
});
},
handleDelete(row) {
this.$confirm('确认要删除套餐吗, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
//发送删除请求
axios.get('/setmeal/deleteById.do?id='+row.id).then(res=>{
this.$message({
message: res.data.message,
type: res.data.flag ? 'success':"error"
});
//重新加载数据
this.findPage();
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
},
// 重置表单
resetForm() {
//清空表单
this.formData={};
//检查组勾选清空
this.checkgroupIds=[];
//图片清空
this.imageUrl=null;
//选项卡默认选中
this.activeName='first';
},
// 弹出添加窗口
handleCreate() {
//弹出窗口
this.dialogFormVisible=true;
this.resetForm();
//查询全部检查组列表
//弹出窗口
//查询检查组列表
axios.get('/checkgroup/findAll.do').then(res=>{
this.$message({
message: res.data.message,
type: res.data.flag ? 'success':"error"
});
if(res.data.flag){
this.tableData = res.data.data;
}
})
},
//切换页码
handleCurrentChange(currentPage) {
//重新设置页码,重新查询
this.pagination.currentPage=currentPage;
this.findPage();
}
}
})
</script>
</html>
2.SetmealControlle.java
package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;
import java.util.List;
import java.util.UUID;
/**
* 套餐
*/
@RestController
@RequestMapping(value="/setmeal")
public class SetmealController {
@Reference
private SetmealService setmealService;
@Autowired
private JedisPool jedisPool;
/**
* 上传图片
*
*/
@RequestMapping(value = "/upload")
public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
try {
//将原始的文件名改成唯一的文件名(保证文件不会被覆盖)
//将图片上传七牛云
String oldFileName = imgFile.getOriginalFilename();
int lastIndexOf = oldFileName.lastIndexOf(".");
//获取后缀
String suffx = oldFileName.substring(lastIndexOf);
//UUID生成唯一的文件名
String newFileName= UUID.randomUUID().toString()+suffx;
//上传七牛云
QiniuUtils.upload2Qiniu(imgFile.getBytes(),newFileName);
//页面还需要回显图片,第一步将图片上传成功记录到redis
jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newFileName);
return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,newFileName);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.PIC_UPLOAD_SUCCESS);
}
}
/**
* 查询所有检查组
* @return
*/
@RequestMapping(value = "/add")
public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
try {
setmealService.add(setmeal,checkgroupIds);
//第二步新增成功之后记录redis
jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
}
}
@RequestMapping("/findPage")
public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
try {
PageResult pageResult= setmealService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
return pageResult;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据id查询套餐
* @param id
* @return
*/
@RequestMapping(value = "/findById")
public Result findById( Integer id) {
try {
Setmeal setmeal=setmealService.findById(id);
return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
}
}
/**
* 使用套餐id,查询检查组的id集合,返回List<Integer>
*/
@RequestMapping(value = "/findCheckGroupIdsBySetmealId")
public List<Integer> findCheckGroupIdsBySetmealId(Integer id){
List<Integer> list = setmealService.findCheckGroupIdsBySetmealId(id);
return list;
}
/**
* 编辑套餐
* @param setmeal
* @param checkgroupIds
* @return
*/
@RequestMapping(value = "/edit")
public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds) {
try {
setmealService.edit(setmeal,checkgroupIds);
return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
}
}
@RequestMapping(value = "/deleteById")
public Result deleteById(Integer id) {
try {
setmealService.deleteById(id);
return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
} catch (RuntimeException e) {
e.printStackTrace();
return new Result(false, e.getMessage());
}catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
}
}
}
3.SetmealService.java
package com.itheima.service;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Setmeal;
import java.util.List;
import java.util.Map;
public interface SetmealService {
/**
* 添加检查项
* @param setmeal
* @param checkgroupIds
*/
void add(Setmeal setmeal, Integer[] checkgroupIds);
/**
* 分页查询
* @param currentPage
* @param pageSize
* @param queryString
* @return
*/
PageResult findPage(Integer currentPage, Integer pageSize, String queryString);
/**
* 根据id查询套餐
* @param id
* @return
*/
Setmeal findById(Integer id);
/**
* 使用套餐id,查询检查组的id集合,返回List<Integer>
* @param id
* @return
*/
List<Integer> findCheckGroupIdsBySetmealId(Integer id);
/**
* 编辑套餐
* @param setmeal
* @param checkgroupIds
*/
void edit(Setmeal setmeal, Integer[] checkgroupIds);
/**
* 删除套餐
* @param id
*/
void deleteById(Integer id);
/**
* 点击体检预约直接跳转到体检套餐列表页面
* @return
*/
List<Setmeal> findAll();
/**
* 套餐占比统计
* @return
*/
List<Map<String, Object>> findSetmealCount();
}
4.SetmealServiceImpl.java
package com.itheima.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@Autowired
private SetmealDao setmealDao;
@Value("${out_put_path}")//从属性文件读取输出目录的路径
private String outputpath ;
/**
* 添加套餐
* @param setmeal
* @param checkgroupIds
*/
public void add(Setmeal setmeal,Integer[] checkgroupIds){
/* //1.往检查组表新增一条记录
setmealDao.add(setmeal);
//2.往中间表检查组id记录,在下面的方法中循环遍历
// 抽取代码,别的也可以调用
setCheckGroupAndSetmeal(setmeal.getId(),checkgroupIds);*/
setmealDao.add(setmeal);
Integer setmealId = setmeal.getId();//获取套餐id
this.setCheckGroupAndSetmeal(setmealId,checkgroupIds);
//完成数据库操作后需要将图片名称保存到redis
//jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
//新增套餐后需要重新生成静态页面
generateMobileStaticHtml();
}
//生成静态页面
public void generateMobileStaticHtml() {
//准备模板文件中所需的数据
List<Setmeal> setmealList = this.findAll();
//生成套餐列表静态页面
generateMobileSetmealListHtml(setmealList);
//生成套餐详情静态页面(多个)
generateMobileSetmealDetailHtml(setmealList);
}
//生成套餐列表静态页面
public void generateMobileSetmealListHtml(List<Setmeal> setmealList) {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("setmealList", setmealList);
this.generateHtml("mobile_setmeal.ftl","m_setmeal.html",dataMap);
}
//生成套餐详情静态页面(多个)
public void generateMobileSetmealDetailHtml(List<Setmeal> setmealList) {
for (Setmeal setmeal : setmealList) {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("setmeal", this.findById(setmeal.getId()));
this.generateHtml("mobile_setmeal_detail.ftl",
"setmeal_detail_"+setmeal.getId()+".html",
dataMap);
}
}
public void generateHtml(String templateName,String htmlPageName,Map<String, Object> dataMap){
Configuration configuration = freeMarkerConfigurer.getConfiguration();
Writer out = null;
try {
// 加载模版文件
Template template = configuration.getTemplate(templateName);
// 生成数据
File docFile = new File(outputpath + "\\" + htmlPageName);
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile),"UTF-8"));
// 输出文件
template.process(dataMap, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != out) {
out.flush();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/**
* 分页查询
* @param currentPage
* @param pageSize
* @param queryString
* @return
*/
@Override
public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
//设置分页参数
PageHelper.startPage(currentPage,pageSize);
//需要分页的语句
Page<Setmeal> setmealPage= setmealDao.selectByCondition(queryString);
return new PageResult(setmealPage.getTotal(),setmealPage.getResult());
}
/**
* 根据id查询套餐
* @param id
* @return
*/
@Override
public Setmeal findById(Integer id) {
return setmealDao.findById(id);
/*List<CheckGroup> checkGroupList = checkGroupDao .findCheckGroupListBySetmealId(setmeal.getId());
// 遍历checkGroupList
for(CheckGroup checkgroup:checkGroupList){
List<CheckItem> checkItemList = checkItemDao.findCheckItemListByCheckGroupId(checkgroup.getId());
checkgroup.setCheckItems(checkItemList);
}
setmeal.setCheckGroups(checkGroupList);
return setmeal;*/
}
/**
* 根据SetmealId查询CheckGroupIds
* @param id
* @return
*/
@Override
public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
return setmealDao.findCheckGroupIdsBySetmealId(id);
}
/**
* 编辑套餐
* @param setmeal
* @param checkgroupIds
*/
@Override
public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
// 使用套餐id,查询数据库对应的套餐,获取数据库存放的img
Setmeal setmeal_db = setmealDao.findById(setmeal.getId());
String img = setmeal_db.getImg();
// 如果页面传递的图片名称和数据库存放的图片名称不一致,说明图片更新,需要删除七牛云之前数据库的图片
if(setmeal.getImg()!=null && !setmeal.getImg().equals(img)){
QiniuUtils.deleteFileFromQiniu(img);
}
//1:根据套餐id删除中间表数据(清理原有关联关系)
setmealDao.deleteAssociation(setmeal.getId());
//2:向中间表(t_setmeal_checkgroup)插入数据(建立套餐和检查组关联关系)
setCheckGroupAndSetmeal(setmeal.getId(),checkgroupIds);
//3:更新套餐基本信息
setmealDao.edit(setmeal);
}
/**
* 根据id删除套餐
* @param id
*/
@Override
public void deleteById(Integer id) {
//1.根据套餐id查询数据库对象
Setmeal setmeal = setmealDao.findById(id);
//2.根据套餐id查询套餐和检查组中间表
int count= setmealDao.findSetmealAndCheckGroupCountBySetMealId(id);
//如果有关联,则不允许删除
if(count>0){
throw new RuntimeException("当前套餐和检查组存在关系,不能直接删除");
}
//4.删除套餐
setmealDao.deleteById(id);
//5.将七牛云的图片删除
String img = setmeal.getImg();
if(!StringUtil.isEmpty(img)){
QiniuUtils.deleteFileFromQiniu(img);
}
}
/**
* 点击体检预约直接跳转到体检套餐列表页面
* @return
*/
@Override
public List<Setmeal> findAll() {
return setmealDao.findAll();
}
/**
* 套餐占比统计
* @return
*/
@Override
public List<Map<String, Object>> findSetmealCount() {
return setmealDao.findSetmealCount();
}
//2.往中间表新增套餐记录
public void setCheckGroupAndSetmeal(Integer setmealId,Integer[] checkgroupIds){
if(checkgroupIds!=null && checkgroupIds.length>0){
for (Integer checkgroupId : checkgroupIds) {
Map<String,Integer> map=new HashMap<>();
map.put("setmealId",setmealId);
map.put("checkgroupId",checkgroupId);
setmealDao.setCheckGroupAndSetmeal(map);
}
}
}
}
5.SetmealDao.java
package com.itheima.dao;
import com.github.pagehelper.Page;
import com.itheima.pojo.Setmeal;
import java.util.List;
import java.util.Map;
public interface SetmealDao {
/**
* 新增套餐
* @param setmeal
*/
void add(Setmeal setmeal);
/**
* 套餐检查组中间表插入
* @param map
*/
void setCheckGroupAndSetmeal(Map<String, Integer> map);
/**
* 根据条件查询分页
* @param queryString
* @return
*/
Page<Setmeal> selectByCondition(String queryString);
Setmeal findById(Integer id);
/**
* 根据SetmealId查询CheckGroupIds
* @param id
* @return
*/
List<Integer> findCheckGroupIdsBySetmealId(Integer id);
/**
* 根据套餐id删除中间表数据(清理原有关联关系)
* @param id
*/
void deleteAssociation(Integer id);
/**
* 编辑套餐
* @param setmeal
*/
void edit(Setmeal setmeal);
/**
* 根据套餐id查询套餐和检查组中间表
* @param id
* @return
*/
int findSetmealAndCheckGroupCountBySetMealId(Integer id);
/**
* 根据id删除套餐
* @param id
*/
void deleteById(Integer id);
/**
* 点击体检预约直接跳转到体检套餐列表页面
* @return
*/
List<Setmeal> findAll();
/**
* 套餐占比统计
* @return
*/
List<Map<String, Object>> findSetmealCount();
}
6.SetmealDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.SetmealDao">
<!--新增套餐 -->
<insert id="add" parameterType="com.itheima.pojo.Setmeal">
/*获取检查组id*/
<selectKey resultType="int" order="AFTER" keyProperty="id">
select LAST_INSERT_ID()
</selectKey>
INSERT INTO t_setmeal (name,code,helpCode,sex,age,price,remark,attention,img)
VALUES (#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})
</insert>
<!--往检查组和套餐中间表插入记录setCheckGroupAndSetmeal-->
<insert id="setCheckGroupAndSetmeal" parameterType="map">
INSERT INTO t_setmeal_checkgroup (setmeal_id,checkgroup_id)
VALUES (#{setmealId},#{checkgroupId})
</insert>
<select id="selectByCondition" parameterType="string" resultType="com.itheima.pojo.Setmeal">
select * from t_setmeal
<if test="value !=null and value.length>0">
where name like concat('%',#{value},'%') or code =#{value} or helpCode =#{helpCode}
</if>
</select>
<!--套餐ID查询-->
<!-- <select id="findById" parameterType="int" resultType="com.itheima.pojo.Setmeal">
select * from t_setmeal where id = #{id}
</select>-->
<!--使用套餐id,查询检查组的集合ID-->
<select id="findCheckGroupIdsBySetmealId" parameterType="int" resultType="int">
select checkgroup_id from t_setmeal_checkgroup where setmeal_id = #{setmealId}
</select>
<!--使用套餐id,删除套餐和检查组中间表数据-->
<delete id="deleteAssociation" parameterType="int">
delete from t_setmeal_checkgroup where setmeal_id = #{id}
</delete>
<!--绑定套餐和检查组多对多关系-->
<insert id="setSetmealAndCheckGroup" parameterType="hashmap">
insert into t_setmeal_checkgroup
(setmeal_id,checkgroup_id)
values
(#{setmeal_id},#{checkgroup_id})
</insert>
<!--编辑-->
<update id="edit" parameterType="com.itheima.pojo.Setmeal">
update t_setmeal
<set>
<if test="name != null and name.length>0">
name = #{name},
</if>
<if test="code != null and code.length>0">
code = #{code},
</if>
<if test="helpCode != null and helpCode.length>0">
helpCode = #{helpCode},
</if>
<if test="sex != null and sex.length>0">
sex = #{sex},
</if>
<if test="age != null and age.length>0">
age = #{age},
</if>
<if test="price != null">
price = #{price},
</if>
<if test="remark != null and remark.length>0">
remark = #{remark},
</if>
<if test="attention != null and attention.length>0">
attention = #{attention},
</if>
<if test="img != null and img.length>0">
img = #{img},
</if>
</set>
where id = #{id}
</update>
<!--根据套餐id查询套餐和检查组中间表-->
<select id="findSetmealAndCheckGroupCountBySetMealId" parameterType="int" resultType="int">
select count(*) from t_setmeal_checkgroup where setmeal_id=#{setmealId}
</select>
<!--根据id删除套餐-->
<delete id="deleteById" parameterType="int">
delete from t_setmeal where id=#{id}
</delete>
<!--查询所有-->
<select id="findAll" resultType="com.itheima.pojo.Setmeal">
select * from t_setmeal
</select>
<resultMap type="com.itheima.pojo.Setmeal" id="findByIdResultMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="code" property="code"/>
<result column="helpCode" property="helpCode"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
<result column="price" property="price"/>
<result column="remark" property="remark"/>
<result column="attention" property="attention"/>
<result column="img" property="img"/>
<!--关联查询检查组数据
property将检查结果放入property中
column="id查询条件
select 查询下语句
-->
<collection property="checkGroups" column="id"
select="com.itheima.dao.CheckGroupDao.findCheckGroupListById">
</collection>
</resultMap>
<select id="findById" parameterType="int" resultMap="findByIdResultMap">
select * from t_setmeal where id=#{id}
</select>
<select id="findSetmealCount" resultType="map">
select s.name,count(o.setmeal_id) as value from t_order o ,t_setmeal s where o.setmeal_id = s.id group by s.name
</select>
</mapper>