|
|
@@ -0,0 +1,193 @@
|
|
|
+package com.uas.platform.b2c.fa.settlement.service.impl;
|
|
|
+
|
|
|
+import com.uas.platform.b2c.core.constant.SplitChar;
|
|
|
+import com.uas.platform.b2c.core.support.SystemSession;
|
|
|
+import com.uas.platform.b2c.fa.settlement.dao.BillDao;
|
|
|
+import com.uas.platform.b2c.fa.settlement.dao.BillSubmitDao;
|
|
|
+import com.uas.platform.b2c.fa.settlement.model.Bill;
|
|
|
+import com.uas.platform.b2c.fa.settlement.model.BillSubmit;
|
|
|
+import com.uas.platform.b2c.fa.settlement.service.BillSubmitService;
|
|
|
+import com.uas.platform.b2c.trade.order.dao.OrderDao;
|
|
|
+import com.uas.platform.b2c.trade.order.model.Order;
|
|
|
+import com.uas.platform.core.exception.IllegalOperatorException;
|
|
|
+import com.uas.platform.core.model.PageInfo;
|
|
|
+import com.uas.platform.core.model.Status;
|
|
|
+import com.uas.platform.core.model.Type;
|
|
|
+import com.uas.platform.core.persistence.criteria.CriterionExpression;
|
|
|
+import com.uas.platform.core.persistence.criteria.LogicalExpression;
|
|
|
+import com.uas.platform.core.persistence.criteria.PredicateUtils;
|
|
|
+import com.uas.platform.core.persistence.criteria.SimpleExpression;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
+import javax.persistence.criteria.CriteriaQuery;
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
+import javax.persistence.criteria.Root;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 开票申请
|
|
|
+ * @author wangyc
|
|
|
+ * @version 2017/8/24 17:10 wangyc
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class BillSubmitServiceImpl implements BillSubmitService {
|
|
|
+
|
|
|
+ private final OrderDao orderDao;
|
|
|
+
|
|
|
+ private final BillDao billDao;
|
|
|
+
|
|
|
+ private final BillSubmitDao billSubmitDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public BillSubmitServiceImpl (OrderDao orderDao, BillDao billDao, BillSubmitDao billSubmitDao) {
|
|
|
+ this.orderDao = orderDao;
|
|
|
+ this.billDao = billDao;
|
|
|
+ this.billSubmitDao = billSubmitDao;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<BillSubmit> save(String[] orderids, Long billid) {
|
|
|
+ // 获取验证发票信息
|
|
|
+ Bill bill = billDao.findOne(billid);
|
|
|
+ if (bill == null)
|
|
|
+ throw new IllegalOperatorException("发票信息不存在");
|
|
|
+
|
|
|
+ Long buyeruu = SystemSession.getUser().getUserUU();
|
|
|
+ Long buyerEnuu = null;
|
|
|
+ if (SystemSession.getUser().getEnterprise() != null)
|
|
|
+ buyerEnuu = SystemSession.getUser().getEnterprise().getUu();
|
|
|
+
|
|
|
+ if ((!bill.getUseruu().equals(buyeruu)) || (buyerEnuu != null && !bill.getEnuu().equals(buyerEnuu)))
|
|
|
+ throw new IllegalOperatorException("发票信息不属于您,请重新确认发票信息");
|
|
|
+
|
|
|
+ // 获取验证订单信息
|
|
|
+ List<String> orderidList = Arrays.asList(orderids);
|
|
|
+ List<Order> orders = orderDao.findByOrderIds(orderidList);
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(orders))
|
|
|
+ throw new IllegalOperatorException("订单号错误,请重新确认订单信息");
|
|
|
+
|
|
|
+ // 验证订单号是否正确
|
|
|
+ if (orders.size() != orderidList.size()) {
|
|
|
+ List<String> orderIds = new ArrayList<String>();
|
|
|
+ for (Order order : orders) {
|
|
|
+ orderIds.add(order.getOrderid());
|
|
|
+ }
|
|
|
+ orderidList.removeAll(orderIds);
|
|
|
+
|
|
|
+ String errorIdStr = com.alibaba.dubbo.common.utils.StringUtils.join(orderids, SplitChar.COMMA);
|
|
|
+ throw new IllegalOperatorException("订单号: " + errorIdStr + " 不存在,请重新确认订单信息");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<Long, List<Order>> map = new HashMap<Long, List<Order>>();
|
|
|
+
|
|
|
+ // 将订单按卖家分组
|
|
|
+ for (Order order : orders) {
|
|
|
+ if (map.get(order.getSellerenuu()) == null) {
|
|
|
+ List<Order> afterGroup = new ArrayList<Order>();
|
|
|
+ afterGroup.add(order);
|
|
|
+ map.put(order.getSellerenuu(), afterGroup);
|
|
|
+ } else {
|
|
|
+ map.get(order.getSellerenuu()).add(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ List<BillSubmit> billSubmits = new ArrayList<BillSubmit>();
|
|
|
+ for (Long sellerenuu : map.keySet()) {
|
|
|
+ BillSubmit billSubmit = createBillSubmit(bill, map.get(sellerenuu), buyerEnuu);
|
|
|
+ billSubmits.add(billSubmit);
|
|
|
+ }
|
|
|
+
|
|
|
+ return billSubmitDao.save(billSubmits);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化发票申请
|
|
|
+ public BillSubmit createBillSubmit(Bill bill, List<Order> orders, Long buyerEnuu) {
|
|
|
+ Long buyeruu = SystemSession.getUser().getUserUU();
|
|
|
+ // 发票基本信息
|
|
|
+ BillSubmit billSubmit = new BillSubmit();
|
|
|
+ billSubmit.setInvoicetitle(bill.getHead());
|
|
|
+ billSubmit.setInvoiceid(bill.getId());
|
|
|
+ billSubmit.setInvoiceAddress(bill.getArea() + bill.getDetailAddress());
|
|
|
+ billSubmit.setInvoicetype(bill.getKind());
|
|
|
+ billSubmit.setCreateTime(new Date());
|
|
|
+ billSubmit.setReceiverName(bill.getName());
|
|
|
+ billSubmit.setRecTel(bill.getTelephone());
|
|
|
+ billSubmit.setStatus(Status.INPUTTING.value());
|
|
|
+ billSubmit.setSubmituu(SystemSession.getUser().getUserUU());
|
|
|
+ if (SystemSession.getUser().getEnterprise() != null)
|
|
|
+ billSubmit.setSubmitEnuu(SystemSession.getUser().getEnterprise().getUu());
|
|
|
+
|
|
|
+ String orderIds = "";
|
|
|
+ Double price = 0d;
|
|
|
+ for (int i = 0; i < orders.size(); i++) {
|
|
|
+ if (buyerEnuu != null) {
|
|
|
+ Order order = orders.get(i);
|
|
|
+ if (buyerEnuu.equals(order.getBuyerenuu()) && buyeruu.equals(order.getBuyeruu())) {
|
|
|
+ billSubmit.setSellername(order.getSellername());
|
|
|
+ billSubmit.setSellerenuu(order.getSellerenuu());
|
|
|
+ order.setInvoicetype(bill.getKind());
|
|
|
+ order.setInvoiceAddress(bill.getArea() + bill.getDetailAddress());
|
|
|
+ order.setInvoiceid(bill.getId());
|
|
|
+ order.setInvoicetitle(bill.getHead());
|
|
|
+ order.setVatBillStatus(Status.TOBEMAKE_BILL.value());
|
|
|
+ price += order.getPrice();
|
|
|
+ orderIds += order.getOrderid();
|
|
|
+ if (i < orders.size() - 1)
|
|
|
+ orderIds += SplitChar.COMMA;
|
|
|
+ } else {
|
|
|
+ throw new IllegalOperatorException("订单号: " + order.getOrderid() + " ,不属于您的订单,请重新确认订单号");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ billSubmit.setPrice(price);
|
|
|
+ billSubmit.setOrderids(orderIds);
|
|
|
+
|
|
|
+ return billSubmit;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<BillSubmit> getAll(final PageInfo pageInfo, String keyword, String role) {
|
|
|
+ if (Type.BUYER.name().equals(role)) {
|
|
|
+ pageInfo.expression(PredicateUtils.eq("submituu", SystemSession.getUser().getUserUU(), false));
|
|
|
+ if (SystemSession.getUser().getEnterprise() != null)
|
|
|
+ pageInfo.expression(PredicateUtils.eq("submitEnuu", SystemSession.getUser().getEnterprise().getUu(), false));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Type.SELLER.name().equals(role)) {
|
|
|
+ if (SystemSession.getUser().getEnterprise() != null) {
|
|
|
+ pageInfo.expression(PredicateUtils.eq("sellerenuu", SystemSession.getUser().getEnterprise().getUu(), false));
|
|
|
+ } else {
|
|
|
+ throw new IllegalOperatorException("请选择企业账套后查看开票申请");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.hasText(keyword)) {
|
|
|
+ SimpleExpression sellername = new SimpleExpression("sellername", keyword, CriterionExpression.Operator.LIKE);
|
|
|
+ SimpleExpression orderids = new SimpleExpression("orderids", keyword, CriterionExpression.Operator.LIKE);
|
|
|
+ SimpleExpression invoicetitle = new SimpleExpression("invoicetitle", keyword, CriterionExpression.Operator.LIKE);
|
|
|
+ SimpleExpression receiverName = new SimpleExpression("receiverName", keyword, CriterionExpression.Operator.LIKE);
|
|
|
+ SimpleExpression recTel = new SimpleExpression("recTel", keyword, CriterionExpression.Operator.LIKE);
|
|
|
+ SimpleExpression[] simpleExpressions = new SimpleExpression[]{sellername, orderids, invoicetitle, receiverName, recTel};
|
|
|
+ LogicalExpression logicalExpression = PredicateUtils.or(simpleExpressions);
|
|
|
+ pageInfo.expression(logicalExpression);
|
|
|
+ }
|
|
|
+
|
|
|
+ return billSubmitDao.findAll(new Specification<BillSubmit>() {
|
|
|
+ @Override
|
|
|
+ public Predicate toPredicate(Root<BillSubmit> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
|
|
|
+ query.where(pageInfo.getPredicates(root, query, cb));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }, pageInfo);
|
|
|
+ }
|
|
|
+}
|