package com.gszc.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.gszc.entity.Custom; import com.gszc.entity.Invoice; import com.gszc.entity.InvoiceHeader; import com.gszc.entity.PcUser; import com.gszc.mapper.CustomMapper; import com.gszc.mapper.InvoiceHeaderMapper; import com.gszc.mapper.InvoiceMapper; import com.gszc.mapper.PcUserMapper; import com.gszc.service.CountService; import com.gszc.service.IInvoiceService; import com.gszc.util.KdniaoTrackQueryAPI; import com.gszc.util.Uuid8Utils; import org.apache.ibatis.session.RowBounds; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author ky * @since 2020-04-27 */ @Service @Transactional public class InvoiceServiceImpl extends ServiceImpl implements IInvoiceService { @Autowired InvoiceMapper invoiceMapper; @Autowired InvoiceHeaderMapper invoiceHeaderMapper; @Autowired CustomServiceImpl customService; @Autowired CustomMapper customMapper; @Autowired CountService countService; @Autowired PcUserMapper pcUserMapper; public void addInvoice(Invoice invoice) { String id = String.format("%06d",new Random().nextInt(999999)+1); Invoice isHave = invoiceMapper.selectById(id); while(null!=isHave){ id = String.format("%06d",new Random().nextInt(999999)+1); isHave = invoiceMapper.selectById(id); } invoice.setId(id); invoice.setStatusCode("disable"); invoice.setCreateDate(new Date()); invoice.setModifyDate(new Date()); BigDecimal money = invoice.getMoney(); String customId = invoice.getCustomId(); Custom custom = customMapper.selectById(customId); try { String companyType; String invoiceType; String taxType; // JSONObject jsonObject = countService.VATCount(custom.getRegisterType(), invoice.getInvoiceType(), "1", money.doubleValue()); JSONObject jsonObject = countService.VATSurchargeCount(money.toString()); BigDecimal money1 = jsonObject.getBigDecimal("result"); invoice.setTaxMoney(money1.toString()); } catch (Exception e) { e.printStackTrace(); } invoiceMapper.insert(invoice); } public List invoiceList(String userId, String type) { List list = new ArrayList<>(); JSONArray customs = customService.customList(userId, "all"); for (int i = 0; i < customs.size(); i++) { Wrapper customId = new EntityWrapper().eq("custom_id", customs.getJSONObject(i).getJSONObject("custom").getString("id")); if (null == type) { } else if (type.equals("disable")) { customId.eq("status_code", "disable"); } else if (type.equals("enable")) { customId.eq("status_code", "enable"); } customId.orderBy("create_date", false); List invoiceList = invoiceMapper.selectList(customId); for (Invoice invoice : invoiceList) { invoice.setCustomName(customs.getJSONObject(i).getJSONObject("custom").getString("companyName")); invoice.setCustomType(customs.getJSONObject(i).getJSONObject("custom").getString("registerType")); String invoiceHeaderId = invoice.getInvoiceHeaderId(); InvoiceHeader invoiceHeader = invoiceHeaderMapper.selectById(invoiceHeaderId); invoice.setInvoiceHeaderName(invoiceHeader.getCompanyName()); invoice.setTaxCode(invoiceHeader.getTaxCode()); } if (invoiceList.size() > 0) { list.addAll(invoiceList); } } return list; } public JSONObject invoiceDetail(String invoiceId) { JSONObject jsonObject = new JSONObject(); Invoice invoice = invoiceMapper.selectById(invoiceId); String customId = invoice.getCustomId(); Custom custom = customMapper.selectById(customId); invoice.setCustomName(custom.getCompanyName()); invoice.setCustomSocialCreditCode(custom.getSocialCreditCode()); InvoiceHeader invoiceHeader = invoiceHeaderMapper.selectById(invoice.getInvoiceHeaderId()); jsonObject.put("invoice", invoice); jsonObject.put("invoiceHeader", invoiceHeader); return jsonObject; } public List invoiceHeaderList(String userId) { List invoiceHeaderList = invoiceHeaderMapper.selectList(new EntityWrapper().eq("mini_user_id", userId)); return invoiceHeaderList; } public void addInvoiceHeader(InvoiceHeader invoiceHeader) { String uuid = UUID.randomUUID().toString(); invoiceHeader.setId(uuid); invoiceHeader.setCreateDate(new Date()); invoiceHeader.setModifyDate(new Date()); invoiceHeaderMapper.insert(invoiceHeader); } public void updateInvoiceHeader(InvoiceHeader invoiceHeader) { invoiceHeader.setModifyDate(new Date()); invoiceHeaderMapper.updateById(invoiceHeader); } public void deleteInvoiceHeader(String invoiceHeaderId) { if (invoiceMapper.selectCount(new EntityWrapper().eq("invoice_header_id", invoiceHeaderId)) > 0) { throw new RuntimeException("抬头已被使用"); } invoiceHeaderMapper.deleteById(invoiceHeaderId); } public InvoiceHeader invoiceHeaderDetail(String invoiceHeaderId) { InvoiceHeader invoiceHeader = invoiceHeaderMapper.selectById(invoiceHeaderId); return invoiceHeader; } public JSONObject pcDisableInvoiceList(Integer pageNum, Integer pageSize, String copycatId) { Integer count = 0; List invoiceList = new ArrayList<>(); if (null == copycatId) { count = invoiceMapper.selectCount(new EntityWrapper().eq("status_code", "disable")); invoiceList = invoiceMapper.selectPage(new RowBounds((pageNum - 1) * pageSize, pageSize), new EntityWrapper().eq("status_code", "disable").orderBy("create_date", false)); } else { List customList = customMapper.selectList(new EntityWrapper().eq("copycat_id", copycatId)); if (customList.size() != 0) { count = invoiceMapper.selectCount(new EntityWrapper().eq("status_code", "disable").in("custom_id", customList.stream().map(item -> item.getId() ).collect(Collectors.toList()))); invoiceList = invoiceMapper.selectPage(new RowBounds((pageNum - 1) * pageSize, pageSize), new EntityWrapper().eq("status_code", "disable").in("custom_id", customList.stream().map(item -> item.getId() ).collect(Collectors.toList())).orderBy("create_date", false)); } } for (Invoice invoice : invoiceList) { String invoiceHeaderId = invoice.getInvoiceHeaderId(); String customId = invoice.getCustomId(); Custom custom = customMapper.selectById(customId); InvoiceHeader invoiceHeader = invoiceHeaderMapper.selectById(invoiceHeaderId); invoice.setCustomName(custom.getCompanyName()); invoice.setInvoiceHeaderName(invoiceHeader.getCompanyName()); if (null != custom.getCopycatId()) { PcUser pcUser = pcUserMapper.selectById(custom.getCopycatId()); invoice.setCopycatName(pcUser.getName()); } } JSONObject jsonObject = new JSONObject(); jsonObject.put("count", count); jsonObject.put("invoiceList", invoiceList); return jsonObject; } public JSONObject pcEnableInvoiceList(Integer pageNum, Integer pageSize, String copycatId,String keyword,Long beginTime,Long endTime) { Integer count = null; List invoiceList = new ArrayList<>(); Wrapper invoiceWrapper = new EntityWrapper().eq("status_code", "enable"); if (null == copycatId) { Wrapper wrapper = new EntityWrapper(); if(null!=keyword){ wrapper.like("company_name",keyword); } if(null!=beginTime){ invoiceWrapper.ge("create_date",new Date(beginTime)); } if(null!=endTime){ invoiceWrapper.le("create_date",new Date(endTime)); } List customList = customMapper.selectList(wrapper); if (customList.size() != 0) { count = invoiceMapper.selectCount(invoiceWrapper.in("custom_id", customList.stream().map(item -> item.getId() ).collect(Collectors.toList()))); invoiceList = invoiceMapper.selectPage(new RowBounds((pageNum - 1) * pageSize, pageSize), invoiceWrapper.in("custom_id", customList.stream().map(item -> item.getId() ).collect(Collectors.toList())).orderBy("create_date", false)); } } else { Wrapper wrapper = new EntityWrapper().eq("copycat_id", copycatId); if(null!=keyword){ wrapper.like("company_name",keyword); } if(null!=beginTime){ invoiceWrapper.ge("create_date",new Date(beginTime)); } if(null!=endTime){ invoiceWrapper.le("create_date",new Date(endTime)); } List customList = customMapper.selectList(wrapper); if (customList.size() != 0) { count = invoiceMapper.selectCount(invoiceWrapper.in("custom_id", customList.stream().map(item -> item.getId() ).collect(Collectors.toList()))); invoiceList = invoiceMapper.selectPage(new RowBounds((pageNum - 1) * pageSize, pageSize), invoiceWrapper.in("custom_id", customList.stream().map(item -> item.getId() ).collect(Collectors.toList())).orderBy("create_date", false)); } } for (Invoice invoice : invoiceList) { String invoiceHeaderId = invoice.getInvoiceHeaderId(); InvoiceHeader invoiceHeader = invoiceHeaderMapper.selectById(invoiceHeaderId); String customId = invoice.getCustomId(); Custom custom = customMapper.selectById(customId); invoice.setCustomName(custom.getCompanyName()); invoice.setInvoiceHeaderName(invoiceHeader.getCompanyName()); if (null != custom.getCopycatId()) { PcUser pcUser = pcUserMapper.selectById(custom.getCopycatId()); invoice.setCopycatName(pcUser.getName()); } } JSONObject jsonObject = new JSONObject(); jsonObject.put("count", count); jsonObject.put("invoiceList", invoiceList); return jsonObject; } public void invoiceConfirm(String invoiceId, String trackingCompany, String trackingCode, String invoiceImg) { Invoice invoice = new Invoice(); invoice.setId(invoiceId); invoice.setStatusCode("enable"); invoice.setModifyDate(new Date()); invoice.setTrackingCompany(trackingCompany); invoice.setTrackingCode(trackingCode); invoice.setInvoiceImg(invoiceImg); invoiceMapper.updateById(invoice); } public JSONObject trackingDetail(String trackingCompany, String trackingCode) { KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI(); JSONObject orderTracesByJson = new JSONObject(); try { String orderTracesByJsonString = api.getOrderTracesByJson(trackingCompany, trackingCode); orderTracesByJson = JSONObject.parseObject(orderTracesByJsonString); } catch (Exception e) { e.printStackTrace(); return orderTracesByJson; } return orderTracesByJson; } public JSONObject queryInvoiceCount(String copycatId) { Integer count = null; if (null == copycatId) { count = invoiceMapper.selectCount(new EntityWrapper().eq("status_code", "disable")); } else { List customList = customMapper.selectList(new EntityWrapper().eq("copycat_id", copycatId)); count = invoiceMapper.selectCount(new EntityWrapper().eq("status_code", "disable").in("custom_id", customList.stream().map(item -> item.getId() ).collect(Collectors.toList()))); } JSONObject jsonObject = new JSONObject(); jsonObject.put("count", count); return jsonObject; } }