RosterController.groovy
上传用户:steveyhw
上传日期:2019-05-13
资源大小:307k
文件大小:8k
- import java.text.SimpleDateFormat
- class RosterController {
- def index = { redirect(action: list, params: params) }
- // the delete, save and update actions only accept POST requests
- def allowedMethods = [list: ['GET', 'POST'], delete: 'POST', save: 'POST', update: 'POST']
- def list = {
- //从页面传来的查询参数: 1._rosRequestman(申请人) 2._rosState_id(执勤状态) 3._rosLastchangefrom(开始时间) 4. _rosLastchangeto(结束时间) 5._rosDel(是否查询全部)
- def df = new SimpleDateFormat('yyyy-MM-dd')
- if (!params.max) params.max = '10'
- if (!params.sort) params.sort = 'id'
- if (!params.offset) params.offset = '0'
- if (!params.order) params.order = 'asc'
- if (!params._rosRequestman) params._rosRequestman = ''
- //涉及到种类查询不能用like,所以先做好sql段然后拼接到后面sql中去
- String rosStateSql = params._rosState_id?" and str(r.rosState) = '${params._rosState_id}' ":""
- //查询时间: 如果开始时间不填就默认成1970年,如果结束时间不填就默认为当前系统时间
- java.util.Date _rosLastchangefrom = params._rosLastchangefrom?df.parse(params._rosLastchangefrom) :df.parse('1970-01-01')
- java.util.Date _rosLastchangeto = params._rosLastchangeto?df.parse(params._rosLastchangeto):new Date()
- //由于我做了逻辑删除,表中rosDel这个字段标示是否被删除,当要全部查询的时候可以吧删除的数据也查出来,这里结合了grails中的checkBox标签
- def _rosDelState = (params._rosDel == 'on')?'':'0'
- //results是查询结果
- def results = Roster.findAll("""from Roster r where str(r.rosRequestman.staName) like :_rosRequestman ${rosStateSql} and r.rosLastchange between :_rosLastchangefrom and :_rosLastchangeto and r.rosDel like :_rosDelState order by ${params.sort} ${params.order} """,
- [_rosRequestman: '%' + params._rosRequestman + '%', _rosLastchangefrom: _rosLastchangefrom, _rosLastchangeto: _rosLastchangeto, _rosDelState: '%' + _rosDelState + '%'],
- [max: Integer.parseInt(params.max), offset: Integer.parseInt(params.offset)])
- //count是用来分页的参数
- params.count = Roster.findAll("""from Roster r where str(r.rosRequestman.staName) like :_rosRequestman ${rosStateSql} and r.rosLastchange between :_rosLastchangefrom and :_rosLastchangeto and r.rosDel like :_rosDelState order by ${params.sort} ${params.order} """,
- [_rosRequestman: '%' + params._rosRequestman + '%', _rosLastchangefrom: _rosLastchangefrom, _rosLastchangeto: _rosLastchangeto, _rosDelState: '%' + _rosDelState + '%']).size()
- render(view: 'list', model: [rosterList: results])
- }
- def show = {
- def roster = Roster.get(params.id)
- if (!roster) {
- flash.message = "roster.not.found"
- flash.args = [params.id]
- flash.defaultMessage = "Roster not found with id ${params.id}"
- redirect(action: list, params: [_rosRequestman: params._rosRequestman, _rosState_id: params._rosState_id, _rosLastchangefrom: params._rosLastchangefrom, _rosLastchangeto: params._rosLastchangeto, _rosDel: params._rosDel, max: params.max, sort: params.sort, offset: params.offset, order: params.order])
- }
- else { return [roster: roster] }
- }
- def delete = {
- def roster = Roster.get(params.id)
- if (roster) {
- //设定删除状态 创建/更新时间 或人
- roster.rosDel = '1'
- roster.rosLastchange = new Date()
- roster.rosLastchanger = 'session'
- roster.save()
- flash.message = "roster.deleted"
- flash.args = [params.id]
- flash.defaultMessage = "Roster ${params.id} deleted"
- redirect(action: list, params: [_rosRequestman: params._rosRequestman, _rosState_id: params._rosState_id, _rosLastchangefrom: params._rosLastchangefrom, _rosLastchangeto: params._rosLastchangeto, _rosDel: params._rosDel, max: params.max, sort: params.sort, offset: params.offset, order: params.order])
- }
- else {
- flash.message = "roster.not.found"
- flash.args = [params.id]
- flash.defaultMessage = "Roster not found with id ${params.id}"
- redirect(action: list, params: [_rosRequestman: params._rosRequestman, _rosState_id: params._rosState_id, _rosLastchangefrom: params._rosLastchangefrom, _rosLastchangeto: params._rosLastchangeto, _rosDel: params._rosDel, max: params.max, sort: params.sort, offset: params.offset, order: params.order])
- }
- }
- def edit = {
- def roster = Roster.get(params.id)
- if (!roster) {
- flash.message = "roster.not.found"
- flash.args = [params.id]
- flash.defaultMessage = "Roster not found with id ${params.id}"
- redirect(action: list, params: [_rosRequestman: params._rosRequestman, _rosState_id: params._rosState_id, _rosLastchangefrom: params._rosLastchangefrom, _rosLastchangeto: params._rosLastchangeto, _rosDel: params._rosDel, max: params.max, sort: params.sort, offset: params.offset, order: params.order])
- }
- else {
- return [roster: roster]
- }
- }
- def update = {
- def roster = Roster.get(params.id)
- if (roster) {
- roster.properties = params
- roster.rosRequestman = Staff.findById(params.rosRequestman.id)
- //设定删除状态 创建人创建时间 更新人更新时间
- roster.rosDel = '0'
- roster.rosLastchanger = 'session'
- roster.rosLastchange = new Date()
- println 'rosRequestman:' + roster.rosRequestman
- if (!roster.hasErrors() && roster.save()) {
- flash.message = "roster.updated"
- flash.args = [params.id]
- flash.defaultMessage = "Roster ${params.id} updated"
- redirect(action: show, id: roster.id, params: [_rosRequestman: params._rosRequestman, _rosState_id: params._rosState_id, _rosLastchangefrom: params._rosLastchangefrom, _rosLastchangeto: params._rosLastchangeto, _rosDel: params._rosDel, max: params.max, sort: params.sort, offset: params.offset, order: params.order])
- }
- else {
- render(view: 'edit', model: [roster: roster])
- }
- }
- else {
- flash.message = "roster.not.found"
- flash.args = [params.id]
- flash.defaultMessage = "Roster not found with id ${params.id}"
- redirect(action: edit, id: params.id, params: [_rosRequestman: params._rosRequestman, _rosState_id: params._rosState_id, _rosLastchangefrom: params._rosLastchangefrom, _rosLastchangeto: params._rosLastchangeto, _rosDel: params._rosDel, max: params.max, sort: params.sort, offset: params.offset, order: params.order])
- }
- }
- def create = {
- def roster = new Roster()
- roster.properties = params
- return ['roster': roster]
- }
- def save = {
- def roster = new Roster(params)
- roster.rosRequestman = Staff.findById(params.rosRequestman.id)
- roster.rosState = '0'
- //设定删除状态 创建人创建时间 更新人更新时间
- roster.rosDel = '0'
- roster.rosCreater = 'session'
- roster.rosCreatetime = new Date()
- roster.rosLastchanger = 'session'
- roster.rosLastchange = new Date()
- if (!roster.hasErrors() && roster.save()) {
- flash.message = "roster.created"
- flash.args = ["${roster.id}"]
- flash.defaultMessage = "Roster ${roster.id} created"
- redirect(action: show, id: roster.id, params: [_rosRequestman: params._rosRequestman, _rosState_id: params._rosState_id, _rosLastchangefrom: params._rosLastchangefrom, _rosLastchangeto: params._rosLastchangeto, _rosDel: params._rosDel, max: params.max, sort: params.sort, offset: params.offset, order: params.order])
- }
- else {
- render(view: 'create', model: [roster: roster])
- }
- }
- }