ssjygl-xsct-service/ssjygl-xsx-common/src/main/java/com/cowr/ssjygl/customer/CustomerService.java

99 lines
3.2 KiB
Java
Raw Normal View History

2020-08-07 17:11:12 +08:00
package com.cowr.ssjygl.customer;
import com.cowr.common.Const;
import com.cowr.common.base.BaseService;
import com.cowr.common.view.PageParam;
import com.cowr.model.Customer;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.ArrayList;
import java.util.List;
/**
* Generated by COWR Mon Apr 06 21:35:28 CST 2020
* TableName: customer
* Remarks:
* PrimaryKey: id
*/
public class CustomerService extends BaseService {
public static final CustomerService me = new CustomerService();
public Page<Record> find(PageParam pp, String name, Integer del, Integer type) {
String selectsql = "select * ";
String fromsql = "from customer t where 1=1 \n";
List<Object> paraList = new ArrayList<>();
if(del == null){
fromsql += " and t.del = 0 \n";
}else if (del == Const.LOGIC_DEL_VALID || del == Const.LOGIC_DEL_INVALID) {
fromsql += " and t.del = ? \n";
paraList.add(del);
}
if(StrKit.notBlank(name)){
fromsql += " and t.name like ? \n";
paraList.add("%" + name + "%");
}
if(type != null){
fromsql += " and t.type = ? \n";
paraList.add(type);
}
String totalRowSql = "select count(*) " + fromsql;
String findSql = selectsql + fromsql;
// 前端传了排序字段,并且排序字段存在相关表中
if (StrKit.notBlank(pp.getSort_field()) && Customer.dao.hasColunm(pp.getSort_field())) {
findSql += " order by t." + pp.getSort_field() + " is null, t." + pp.getSort_field();
if (Const.ORDER_BY_ASC.equals(pp.getSort_order())) {
findSql += " " + Const.ORDER_BY_ASC;
} else {
findSql += " " + Const.ORDER_BY_DESC;
}
}
Page<Record> page = Db.paginateByFullSql(pp.getPage(), pp.getSize(), totalRowSql, findSql, paraList.toArray());
List<Record> list = page.getList();
if(!list.isEmpty()) {
List<String> ids = new ArrayList<>();
for (Record record : list) {
ids.add(record.getStr("id"));
}
List<Record> prepaycustomers = Db.find(" select t.*, s.name supermarket_name from prepay_customer t \n" +
" left join supermarket s on s.id = t.supermarket_id \n" +
" where t.customer_id in(" + StrKit.join(ids, ",") + ")");
for (Record record : list) {
int id = record.getInt("id");
if (record.get("prepay_customer") == null) {
record.set("prepay_customer", new ArrayList<>());
}
for (Record p : prepaycustomers) {
int customer_id = p.getInt("customer_id");
if (id == customer_id) {
((ArrayList) record.get("prepay_customer")).add(p);
}
}
}
}
return page;
}
public List<Customer> list() {
return Customer.dao.find("select * from customer");
}
}