cy
2022-06-22 6e06bba1c89f8077e29d0fbf0ce12f89f027d8d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package ${packageName};
 
import java.util.HashMap;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
 
 
import cn.ksource.core.dao.BaseDao;
import cn.ksource.core.dao.SqlParameter;
import cn.ksource.core.page.PageInfo;
import cn.ksource.core.util.StringUtil;
<#if attFlag=="1">
import cn.ksource.web.service.file.FileService;
</#if>
@SuppressWarnings("rawtypes")
@Service
public class ${className}FacadeImpl implements ${className}Facade{
 
    @Resource
    private BaseDao baseDao;
    <#if attFlag=="1">
    @Resource
    private FileService fileService;
    </#if>
    @Override
    public void save${className}(Map<String, String> params) {
        if(StringUtil.isBlank(params.get("id"))){//新增
            params.put("id", StringUtil.getUUID());
            baseDao.execute(getInsertSql(params), params);
        }else{//更新
            baseDao.execute(getUpdateSql(params), params);
        }
        <#if attFlag=="1">
        //文件上传
        params.put("business_id",params.get("id"));
        fileService.uploadFile(params);
        </#if>
    }
    
    private String getInsertSql(Map<String, String> params){
        StringBuilder sql = new StringBuilder();
        StringBuilder frontsql = new StringBuilder();
        StringBuilder endsql = new StringBuilder();
        <#list fieldList as item>
        if(params.get("${item.fieldName}")!=null){
            frontsql.append("${item.fieldName},");
            endsql.append(":${item.fieldName},");
        }
        </#list>
        sql.append(" insert into ${tableName} ( ");
        sql.append(StringUtils.removeEnd(frontsql.toString(),","));
        sql.append(" ) values ( ");
        sql.append(StringUtils.removeEnd(endsql.toString(),","));
        sql.append(" ) ");
        return sql.toString();
    }
    
    private String getUpdateSql(Map<String, String> params){
        StringBuilder sql = new StringBuilder();
        StringBuilder frontsql = new StringBuilder();
        <#list fieldList as item>
        if(params.get("${item.fieldName}")!=null){
            frontsql.append("${item.fieldName}=:${item.fieldName},");
        }
        </#list>
        sql.append(" update ${tableName} set ");
        sql.append(StringUtils.removeEnd(frontsql.toString(),","));
        sql.append(" where id= :id ");
        return sql.toString();
    }
    
    
    @Override
    public void del${className}ById(String id) {
        baseDao.execute(" update ${tableName} set state=0 where id = :id ", new SqlParameter("id",id));
    }
 
    @Override
    public PageInfo get${className}ListData(PageInfo pageInfo, Map<String, String> param) {
        Map support = get${className}ListSupport(param);
        String sql = support.get("sql").toString();
        return baseDao.queryforSplitPageInfo(pageInfo, sql, param);
    }
 
    @Override
    public Integer get${className}ListCount(Map<String, String> param) {
        Map support = get${className}ListSupport(param);
        String sql = "select count(*) from ( " + support.get("sql").toString() + " ) t ";
        return baseDao.queryForInteger(sql, param);
    }
    
    /**
     * 列表分页查询支持
     * @author chenlong 
     * @param param
     * @return
     */
    private Map get${className}ListSupport(Map<String, String> param){
        Map<String, Object> support = new HashMap<String, Object>();
        StringBuilder sql = new StringBuilder();
        <#if listSql!="">
        sql.append(" ${listSql} ");
        <#else>
            <#assign fields=""/>
            <#list fieldList as item>
                <#if fields=="">
                    <#assign fields="a."+item.fieldName/>
                <#else>
                    <#assign fields=fields+",a."+item.fieldName/>
                </#if>
            </#list>
        sql.append(" select  ${fields?substring(0)} ");
        sql.append(" from ${tableName} a ");
        sql.append(" where 1=1 ");
        //sql.append(" order by a.order_column desc ");
        </#if>
        support.put("sql", sql.toString());
        support.put("param", param);
        return support;
    }
 
    @Override
    public Map get${className}ById(String id) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select a.* from ${tableName} a where a.id=:id ");
        return baseDao.queryForMap(sql.toString(),new SqlParameter("id",id));
    }
    
}