package com.cowr.ssjygl.customer; import com.cowr.common.Const; import com.cowr.common.base.BaseService; import com.cowr.common.enums.UserTypeEnum; import com.cowr.common.view.PageParam; import com.cowr.common.view.Result; import com.cowr.model.Customer; import com.cowr.model.CustomerReceiver; import com.cowr.model.PrepayCustomer; import com.cowr.model.Sysuser; import com.cowr.ssjygl.prepay.prepaycustomer.PrepayCustomerService; 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 Result get(int id, Sysuser sysuser) { if (sysuser.getType() == UserTypeEnum.CUSTOMER.getTypeid()) { if (sysuser.getType() == UserTypeEnum.CUSTOMER.getTypeid() && sysuser.getEntityId() != null && !sysuser.getEntityId().equals(id) ) { return Result.failed("登录用户和订单信息不匹配"); } } Customer customer = Customer.dao.findById(id); if (customer == null) { return Result.failed("未找到记录"); } Record out = customer.toRecord(); CustomerReceiver receiver = CustomerReceiver.dao.findFirst( "select * from customer_receiver t where t.customer_id = ? limit 1", id); if (receiver != null) { out.set("receiver_name", receiver.getName()); out.set("receiver_phone", receiver.getPhone()); out.set("receiver_address", receiver.getAddress()); } PrepayCustomer prepayCustomer = PrepayCustomerService.me.getPrepayCustomer(id); if (prepayCustomer != null) { out.set("surplus", prepayCustomer.getSurplus()); } return Result.success(out); } public Page find(PageParam pp, String name, Integer del, Integer type) { String selectsql = " select \n" + " t.*\n" + " , a.name receiver_name, a.phone receiver_phone, a.address receiver_address \n" + " , b.surplus, b.recharge_time, b.spend_time, b.first_recharge_time, b.threshold \n"; 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" + " \n" + " left join (\n" + " select t.* from prepay_customer t\n" + " left join (\n" + " select max(t.id) prepay_customer_id, t.customer_id from prepay_customer t\n" + " group by t.customer_id\n" + " ) a on t.customer_id = a.customer_id\n" + " where a.prepay_customer_id is not null\n" + " ) b on b.customer_id = t.id \n" + " \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.trim() + "%"); } 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 Db.paginateByFullSql(pp.getPage(), pp.getSize(), totalRowSql, findSql, paraList.toArray()); } public List list() { return Customer.dao.find("select * from customer"); } }