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 find(PageParam pp, String name, Integer del, Integer type) { String selectsql = "select t.*, a.name receiver_name, a.phone receiver_phone, a.address receiver_address "; String fromsql = "from customer t\n" + " left join (\n" + " select t.* from customer_receiver t\n" + " left join (\n" + " select max(t.id) customer_receiver_id, t.customer_id from customer_receiver t\n" + " group by t.customer_id\n" + " ) a on a.customer_receiver_id = t.id and t.customer_id = a.customer_id\n" + " where a.customer_receiver_id is not null\n" + " ) a on a.customer_id = t.id \n" + " where 1=1 \n"; List 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 page = Db.paginateByFullSql(pp.getPage(), pp.getSize(), totalRowSql, findSql, paraList.toArray()); List list = page.getList(); if(!list.isEmpty()) { List ids = new ArrayList<>(); for (Record record : list) { ids.add(record.getStr("id")); } List 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 list() { return Customer.dao.find("select * from customer"); } }