CustomerController.groovy
上传用户:steveyhw
上传日期:2019-05-13
资源大小:307k
文件大小:9k
- import java.text.SimpleDateFormat
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- class CustomerController {
- def dataSource
-
- def index = { redirect(action:list,params:params) }
- // the delete, save and update actions only accept POST requests
- def allowedMethods = [delete:'POST', save:'POST', update:'POST']
- def list = {
- //JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource)
- //def list = jdbcTemplate.queryForList("select * from customer")
- //println "list=${list}"
- def conn = dataSource.getConnection();
- PreparedStatement ps = conn.prepareStatement("select * from customer");
- ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- println rs.getString("cus_name")
- }
-
-
- 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._cusName)params._cusName = ''
- String cusProfessionSql = ""
- String cusTypeSql = ""
- if(params._cusProfession_id)cusProfessionSql = " and str(c.cusProfession.id) = '${params._cusProfession_id}' "
- if(params._cusType_id)cusTypeSql = " and str(c.cusType.id) = '${params._cusType_id}' "
- java.util.Date _cusLastchangefrom =null
- java.util.Date _cusLastchangeto =null
- if(!params._cusLastchangefrom){
- _cusLastchangefrom = df.parse('1970-01-01')
- }else{_cusLastchangefrom = df.parse(params._cusLastchangefrom)}
- if(!params._cusLastchangeto){
- _cusLastchangeto = new Date()
- }else{_cusLastchangeto = df.parse(params._cusLastchangeto)}
- if(!params._cusDel)params._cusDel = null
- def _cusDelState = '0'
- if(params._cusDel == 'on'){
- _cusDelState = ''
- } else if(params._cusDel == null || params._cusDel == ""){ _cusDelState = '0' }
- def results=null
- results = Customer.findAll("""from Customer c where c.cusName like :_cusName ${cusProfessionSql+cusTypeSql} and c.cusLastchange between :_cusLastchangefrom and :_cusLastchangeto and c.cusDel like :_cusDelState order by ${params.sort} ${params.order} """,
- [_cusName:'%'+params._cusName+'%',_cusLastchangefrom:_cusLastchangefrom,_cusLastchangeto:_cusLastchangeto,_cusDelState:'%'+_cusDelState+'%'],[max:Integer.parseInt(params.max),offset:Integer.parseInt(params.offset)])
- params.count=Customer.findAll("""from Customer c where c.cusName like :_cusName ${cusProfessionSql+cusTypeSql} and c.cusLastchange between :_cusLastchangefrom and :_cusLastchangeto and c.cusDel like :_cusDelState order by ${params.sort} ${params.order} """,
- [_cusName:'%'+params._cusName+'%',_cusLastchangefrom:_cusLastchangefrom,_cusLastchangeto:_cusLastchangeto,_cusDelState:'%'+_cusDelState+'%']).size()
- render(view:'list',model:[customerList:results])
- }
- def show = {
- def customer = Customer.get( params.id )
- if(!customer) {
- flash.message = "customer.not.found"
- flash.args = [params.id]
- flash.defaultMessage = "Customer not found with id ${params.id}"
- redirect(action:list,params:[_cusName:params._cusName,_cusProfession_id:params._cusProfession_id,_cusType_id:params._cusType_id,_cusLastchangefrom:params._cusLastchangefrom,_cusLastchangeto:params._cusLastchangeto,_cusDel:params._cusDel,max:params.max,sort:params.sort,offset:params.offset,order:params.order])
- }
- else { return [ customer : customer ] }
- }
- def delete = {
- def customer = Customer.get( params.id )
- if(customer) {
- //customer.delete()
- //�趨ɾ��״̬ �����˸���ʱ��
- customer.cusDel = '1'
- customer.cusLastchanger = 'session'
- customer.cusLastchange = new Date()
- customer.save()
- flash.message = "customer.deleted"
- flash.args = [params.id]
- flash.defaultMessage = "Customer ${params.id} deleted"
- redirect(action:list,params:[_cusName:params._cusName,_cusProfession_id:params._cusProfession_id,_cusType_id:params._cusType_id,_cusLastchangefrom:params._cusLastchangefrom,_cusLastchangeto:params._cusLastchangeto,_cusDel:params._cusDel,max:params.max,sort:params.sort,offset:params.offset,order:params.order])
- }
- else {
- flash.message = "customer.not.found"
- flash.args = [params.id]
- flash.defaultMessage = "Customer not found with id ${params.id}"
- redirect(action:list,params:[_cusName:params._cusName,_cusProfession_id:params._cusProfession_id,_cusType_id:params._cusType_id,_cusLastchangefrom:params._cusLastchangefrom,_cusLastchangeto:params._cusLastchangeto,_cusDel:params._cusDel,max:params.max,sort:params.sort,offset:params.offset,order:params.order])
- }
- }
- def edit = {
- def customer = Customer.get( params.id )
- if(!customer) {
- flash.message = "customer.not.found"
- flash.args = [params.id]
- flash.defaultMessage = "Customer not found with id ${params.id}"
- redirect(action:list,params:[_cusName:params._cusName,_cusProfession_id:params._cusProfession_id,_cusType_id:params._cusType_id,_cusLastchangefrom:params._cusLastchangefrom,_cusLastchangeto:params._cusLastchangeto,_cusDel:params._cusDel,max:params.max,sort:params.sort,offset:params.offset,order:params.order])
- }
- else {
- return [ customer : customer ]
- }
- }
- def update = {
- def customer = Customer.get( params.id )
- if(customer) {
- customer.properties = params
- //�趨ɾ��״̬ �����˸���ʱ��
- //customer.cusDel = '0'
- customer.cusLastchanger = 'session'
- customer.cusLastchange = new Date()
- if(!customer.hasErrors() && customer.save()) {
- flash.message = "customer.updated"
- flash.args = [params.id]
- flash.defaultMessage = "Customer ${params.id} updated"
- redirect(action:show,id:customer.id,params:[_cusName:params._cusName,_cusProfession_id:params._cusProfession_id,_cusType_id:params._cusType_id,_cusLastchangefrom:params._cusLastchangefrom,_cusLastchangeto:params._cusLastchangeto,_cusDel:params._cusDel,max:params.max,sort:params.sort,offset:params.offset,order:params.order])
- }
- else {
- render(view:'edit',model:[customer:customer])
- }
- }
- else {
- flash.message = "customer.not.found"
- flash.args = [params.id]
- flash.defaultMessage = "Customer not found with id ${params.id}"
- redirect(action:edit,id:params.id,params:[_cusName:params._cusName,_cusProfession_id:params._cusProfession_id,_cusType_id:params._cusType_id,_cusLastchangefrom:params._cusLastchangefrom,_cusLastchangeto:params._cusLastchangeto,_cusDel:params._cusDel,max:params.max,sort:params.sort,offset:params.offset,order:params.order])
- }
- }
- def create = {
- def customer = new Customer()
- //customer.properties = params
- return ['customer':customer]
- }
- def save = {
- String cusid=Customer.executeQuery("select max(c.cusId) from Customer c ")[0]
- println cusid
- if(cusid=="null"||cusid==null){
- cusid = 'CUS100000000'
- }else { cusid='CUS'+(Integer.parseInt(cusid.substring(3,12))+1)}
- def customer = new Customer(params)
- customer.cusId = cusid //���ñ��
- //�趨ɾ��״̬ �����˴���ʱ�� �����˸���ʱ��
- customer.cusDel = '0'
- customer.cusCreater = 'session'
- customer.cusCreatetime = new Date()
- customer.cusLastchanger = 'session'
- customer.cusLastchange = new Date()
-
- if(!customer.hasErrors() && customer.save()) {
- flash.message = "customer.created"
- flash.args = ["${customer.id}"]
- flash.defaultMessage = "Customer ${customer.id} created"
- redirect(action:show,id:customer.id,params:[_cusName:params._cusName,_cusProfession_id:params._cusProfession_id,_cusType_id:params._cusType_id,_cusLastchangefrom:params._cusLastchangefrom,_cusLastchangeto:params._cusLastchangeto,_cusDel:params._cusDel,max:params.max,sort:params.sort,offset:params.offset,order:params.order])
- }
- else {
- render(view:'create',model:[customer:customer])
- }
- }
- }