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

171 lines
6.5 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;
2020-08-27 14:35:21 +08:00
import com.cowr.common.enums.UserTypeEnum;
2020-08-07 17:11:12 +08:00
import com.cowr.common.view.PageParam;
2020-08-27 14:35:21 +08:00
import com.cowr.common.view.Result;
2020-08-07 17:11:12 +08:00
import com.cowr.model.Customer;
2020-08-27 14:35:21 +08:00
import com.cowr.model.CustomerReceiver;
2020-12-26 15:52:20 +08:00
import com.cowr.model.PrepayCustomer;
2020-08-27 14:35:21 +08:00
import com.cowr.model.Sysuser;
2020-12-26 15:52:20 +08:00
import com.cowr.ssjygl.prepay.prepaycustomer.PrepayCustomerService;
2020-08-07 17:11:12 +08:00
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();
2020-08-27 14:35:21 +08:00
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());
}
2020-12-26 15:52:20 +08:00
PrepayCustomer prepayCustomer = PrepayCustomerService.me.getPrepayCustomer(id);
if (prepayCustomer != null) {
out.set("surplus", prepayCustomer.getSurplus());
}
2020-08-27 14:35:21 +08:00
return Result.success(out);
}
2020-08-07 17:11:12 +08:00
public Page<Record> find(PageParam pp, String name, Integer del, Integer type) {
2020-08-21 23:23:58 +08:00
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" +
2020-08-21 17:22:15 +08:00
" 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" +
2020-08-21 23:23:58 +08:00
" ) a on a.customer_id = t.id\n" +
" \n" +
" left join (\n" +
2021-03-26 09:27:47 +08:00
" select t.id,"+
" t.customer_id,\n"+
" t.supermarket_id,\n"+
"t.recharge_time,\n"+
" t.spend_time,\n"+
"t.first_recharge_time,\n"+
" t.threshold,\n"+
" t.notice,\n"+
" (t.surplus-ifnull(m.amount,0)) surplus from prepay_customer t\n"+
"left join (select customer_id,ifnull(sum(amount),0) amount from refund_detail where state<3 group by customer_id) m\n"+
" on t.customer_id=m.customer_id\n"+
2020-08-21 23:23:58 +08:00
" 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<Object> paraList = new ArrayList<>();
if (del == null) {
2020-08-07 17:11:12 +08:00
fromsql += " and t.del = 0 \n";
2020-08-21 23:23:58 +08:00
} else if (del == Const.LOGIC_DEL_VALID || del == Const.LOGIC_DEL_INVALID) {
2020-08-07 17:11:12 +08:00
fromsql += " and t.del = ? \n";
paraList.add(del);
}
2020-08-21 23:23:58 +08:00
if (StrKit.notBlank(name)) {
2020-08-07 17:11:12 +08:00
fromsql += " and t.name like ? \n";
2020-10-13 14:16:22 +08:00
paraList.add("%" + name.trim() + "%");
2020-08-07 17:11:12 +08:00
}
2020-08-21 23:23:58 +08:00
if (type != null) {
2020-08-07 17:11:12 +08:00
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;
}
}
2020-08-21 23:23:58 +08:00
// 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 Db.paginateByFullSql(pp.getPage(), pp.getSize(), totalRowSql, findSql, paraList.toArray());
2020-08-07 17:11:12 +08:00
}
public List<Customer> list() {
return Customer.dao.find("select * from customer");
}
}