package com.stylefeng.guns.modular.system.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.*;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.utils.GoogleMap.GeocodeVo;
import com.stylefeng.guns.modular.system.utils.GoogleMap.GoogleMapUtil;
import com.stylefeng.guns.modular.system.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author stylefeng
* @since 2023-02-03
*/
@Service
public class TOrderServiceImpl extends ServiceImpl implements ITOrderService {
@Autowired
private TUserMapper tUserMapper;
@Autowired
private UserMapper userMapper;
@Autowired
private TGoodsMapper tGoodsMapper;
@Autowired
private TCompanyMapper tCompanyMapper;
@Autowired
private TPriceMapper priceMapper;
@Autowired
private TUserFeeSettingMapper tUserFeeSettingMapper;
@Autowired
private TGroupMapper tGroupMapper;
@Autowired
private TQuoteMapper tQuoteMapper;
@Autowired
private ITBoxSizeService sizeService;
@Autowired
private GoogleMapUtil googleMapUtil;
@Autowired
private TPortMapper portMapper;
@Autowired
private WareHouseMapper wareHouseMapper;
@Autowired
private DriverNoteMapper driverNoteMapper;
@Autowired
private ITDriverService driverService;
@Resource
private TCarriersMapper carriersMapper;
@Resource
private TTransportationService tTransportationService;
@Resource
private ITPortService portService;
@Resource
private IWarehouseService warehouseService;
@Override
public List getReceivable(Page receivableVoPage, String time, Long id, Integer state,int userId) {
String sTime=null;
String eTime=null;
if(ToolUtil.isNotEmpty(time)){
sTime=time.split(" - ")[0]+" 00:00:00";
eTime=time.split(" - ")[1]+" 23:59:59";
}
List receivable = this.baseMapper.getReceivable(receivableVoPage, sTime, eTime, id, state, userId);
for (ReceivableVo receivableVo : receivable) {
List prices = priceMapper.selectList(new EntityWrapper().eq("order_id", receivableVo.getId()));
BigDecimal reduce = prices.stream().map(TPrice::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
receivableVo.setTotal(reduce.doubleValue());
}
return receivable;
}
@Override
public List getPayList(Page receivableVoPage, String time, String name) {
List list = this.baseMapper.getPayList(receivableVoPage,name,1);
Calendar instance = Calendar.getInstance();
instance.setTime(new Date());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(new Date())+" 23:59:59";
// 30天
instance.add(Calendar.DATE,-30);
String time1 = simpleDateFormat.format(instance.getTime())+" 00:00:01";
// 60tian
instance.add(Calendar.DATE,-30);
String time2 = simpleDateFormat.format(instance.getTime())+" 00:00:01";
// 90tian
instance.add(Calendar.DATE,-30);
String time3 = simpleDateFormat.format(instance.getTime())+" 00:00:01";
for (ShouldPayVo shouldPayVo : list) {
shouldPayVo.setTwentyDay(this.baseMapper.getTwentyDayPay(shouldPayVo.getUserId(),format,time1)==null?0:this.baseMapper.getTwentyDayPay(shouldPayVo.getUserId(),format,time1));
// 30-60tian
shouldPayVo.setThirtyDay(this.baseMapper.getTwentyDayPay(shouldPayVo.getUserId(),time1,time2)==null?0:this.baseMapper.getTwentyDayPay(shouldPayVo.getUserId(),time1,time2));
shouldPayVo.setSixtyDay(this.baseMapper.getTwentyDayPay(shouldPayVo.getUserId(),time2,time3)==null?0:this.baseMapper.getTwentyDayPay(shouldPayVo.getUserId(),time2,time3));
shouldPayVo.setNinetyDay(this.baseMapper.getNineDayPay(shouldPayVo.getUserId(),time3)==null?0:this.baseMapper.getNineDayPay(shouldPayVo.getUserId(),time3));
}
return list;
}
@Override
public List listOfReceivables(Page receivableVoPage, String time, String name, Integer invoicesId, Integer orderId, Integer state) {
String sTime=null;
String eTime =null;
if(Objects.nonNull(time)){
sTime = time.split(" - ")[0]+" 00:00:01";
eTime = time.split(" - ")[1]+" 23:59:59";
}
List receivablesLists = this.baseMapper.listOfReceivables(receivableVoPage, sTime, eTime, name, invoicesId, orderId, state,1);
for (ReceivablesList receivablesList : receivablesLists) {
Integer id= this.baseMapper.getPayStatus(receivablesList.getOrderId());
if(id==null){
receivablesList.setPayStatus(1);
}else {
receivablesList.setPayStatus(2);
}
}
return receivablesLists;
}
@Override
public List listOfPay(Page receivableVoPage, String time, String name, Integer billId, Integer orderId, Integer state) {
String sTime=null;
String eTime =null;
if(Objects.nonNull(time)){
sTime = time.split(" - ")[0]+" 00:00:01";
eTime = time.split(" - ")[1]+" 23:59:59";
}
List list = this.baseMapper.listOfPay(receivableVoPage, sTime, eTime, name, billId, orderId, state,1);
return list;
}
@Override
public InvoicesCarVo listOPayInfo(Long number) {
InvoicesVo info1 = tUserMapper.getOrderInfo(number);
InvoicesCarVo info = new InvoicesCarVo();
BeanUtil.copyProperties(info1,info);
// 根据订单id 获取货物信息
Long id = info.getId();
TOrder tOrder = this.baseMapper.selectById(id);
List orders = tGoodsMapper.selectList(new EntityWrapper().eq("order_id", id));
ArrayList tGoodsVos = new ArrayList<>();
TCompany tCompany = tCompanyMapper.selectById(info.getCompanyId());
List tBoxSizes = sizeService.selectList(null);
for (TGoods order : orders) {
TGoodsVo tGoodsVo = new TGoodsVo();
tGoodsVo.setName(tCompany.getName());
tGoodsVo.setSize(order.getSize());
tGoodsVo.setKg(order.getKg());
String size = order.getSize();
for (TBoxSize tBoxSize : tBoxSizes) {
if(size.equals(tBoxSize.getBoxName())){
}
}
tGoodsVos.add(tGoodsVo);
}
// 付卡车公司钱
BigDecimal payMoney = tOrder.getPayMoney();
Integer carriersId = tOrder.getCarriersId();
TCarriers tCarriers = carriersMapper.selectById(carriersId);
Double commission = tCarriers.getCommission();
BigDecimal multiply = new BigDecimal(commission).multiply(new BigDecimal(0.01));
BigDecimal multiply1 = payMoney.multiply(multiply);
info.setGoodsVos(tGoodsVos);
info.setAmount(multiply1.doubleValue());
return info;
}
@Override
public Boolean confirmationOfPayment(Long number, int type, String remark) {
Boolean b =false;
// 同意
if(type==1){
b = this.baseMapper.agreePay(number);
}else {
b = this.baseMapper.noAgreePay(number,remark);
}
return b;
}
@Override
public Boolean agreeOfPayment(Long number) {
return this.baseMapper.agreeOfPayment(number);
}
@Resource
private TDriverMapper driverMapper;
@Resource
private TPowerUnitsMapper powerUnitsMapper;
// @Override
// public IndexInfo indexList(String time,int id) {
// IndexInfo indexInfo = new IndexInfo();
// // 卡车公司收入时间
// String sTime=null;
// String eTime=null;
// if(ToolUtil.isNotEmpty(time)){
// sTime=time.split(" - ")[0]+" 00:00:01";
// eTime=time.split(" - ")[1]+" 23:59:59";
// }
// // 获取卡车公司收入
// Double companyIncome = this.baseMapper.getCompanyIncome(id, sTime, eTime);
//
// indexInfo.setAccountsReceivable(new BigDecimal(companyIncome));
// // 获取卡车公司支出-》 是否存在承运商 -》算价格
// EntityWrapper wrapper = new EntityWrapper<>();
// wrapper.eq("company_id",id);
// wrapper.eq("carriers_pay_status",1);
// if(sTime!=null){
// wrapper.between("success_time",sTime,eTime);
// }
// List orders = this.baseMapper.selectList(wrapper);
// // 找出有承运商的订单
// List collect = orders.stream().filter(o -> ToolUtil.isNotEmpty(o.getCarriersId())).collect(Collectors.toList());
// // 付给承运商的钱
// BigDecimal disburse = new BigDecimal(BigInteger.ZERO);
// for (TOrder tOrder : collect) {
// TCarriers tCarriers = carriersMapper.selectById(tOrder.getCarriersId());
// Double commission = tCarriers.getCommission();
// BigDecimal payMoney = tOrder.getPayMoney();
// if(ToolUtil.isNotEmpty(commission)){
// BigDecimal bigDecimal = new BigDecimal(commission);
// BigDecimal multiply = payMoney.multiply(bigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
// disburse = disburse.add(multiply);
// }
// }
// indexInfo.setAccountsPayable(disburse);
// indexInfo.setPlatformRevenue(indexInfo.getAccountsReceivable().subtract(indexInfo.getAccountsPayable()));
// // 所有的信息
// // 所有司机
// Integer integer = driverMapper.selectCount(new EntityWrapper().eq("company_id", id).eq("remove",0));
// indexInfo.setDriver(integer);
// //车头
// Integer integer1 = powerUnitsMapper.selectCount(new EntityWrapper().eq("company_id", id).eq("type", 1));
// indexInfo.setPowerUnits(integer1);
// // 车架
// indexInfo.setChassises(powerUnitsMapper.selectCount(new EntityWrapper().eq("company_id", id).eq("type", 2)));
// // 总订单
// indexInfo.setOrdersTotal(this.baseMapper.selectCount(new EntityWrapper().eq("company_id",id)));
// //
// indexInfo.setInTransit(this.baseMapper.selectCount(new EntityWrapper().eq("company_id",id).eq("status","7")));
// indexInfo.setDispatching(this.baseMapper.selectCount(new EntityWrapper().eq("company_id",id).eq("status","0")));
// indexInfo.setCanceled(this.baseMapper.selectCount(new EntityWrapper().eq("company_id",id).eq("status","16")));
// indexInfo.setCompleted(this.baseMapper.selectCount(new EntityWrapper().eq("company_id",id).eq("status","1")));
// //获取当前公司的订单数量 根据当前月推算前12个月
// ArrayList indexMonths = new ArrayList<>();
// //获取当前月份
//
// Calendar instance = Calendar.getInstance();
// instance.setTime(new Date());
// for (int i = 0; i <12; i++) {
// if(i==0){
// IndexMonth indexMonth = new IndexMonth();
// int month = DateUtil.thisMonth();
// indexMonth.setMonth(month);
// indexMonth.setOrderNumber(this.baseMapper.selectCount(new EntityWrapper().eq("company_id",id).between("pay_time",DateUtil.beginOfMonth(new Date()),DateUtil.endOfMonth(new Date()))));
// indexMonths.add(indexMonth);
// }else {
// IndexMonth indexMonth = new IndexMonth();
// instance.add(Calendar.MONTH, -1);
// Date time1 = instance.getTime();
// int month = DateUtil.month(time1);
// indexMonth.setMonth(month);
// indexMonth.setOrderNumber(this.baseMapper.selectCount(new EntityWrapper().eq("company_id",id).between("pay_time",DateUtil.beginOfMonth(time1),DateUtil.endOfMonth(time1))));
// indexMonths.add(indexMonth);
// }
// }
// indexInfo.setList(indexMonths);
// return indexInfo;
// }
//
@Override
public InvoicesVoOne listOfReceivablesInfo(Long orderId) {
InvoicesVoOne info = tUserMapper.getOrderInfoOne(orderId);
//
// 根据订单id 获取货物信息
Long id = info.getId();
TOrder tOrder = this.baseMapper.selectById(id);
if(info.getType().equals("Live Unload")){
// 还柜码头
TTransportation tTransportation = tTransportationService.selectOne(new EntityWrapper().eq("order_id", id).eq("type", 4));
Integer portId = tTransportation.getPortId();
if(portId!=null){
TPort tPort = portService.selectById(portId);
info.setEPortName(tPort.getAddress());
}
}
if(!"0".equals(tOrder.getWarehouse())){
TWarehouse code = warehouseService.selectList(new EntityWrapper().eq("code", tOrder.getWarehouse())).get(0);
info.setEAddress(code.getAddress());
}else {
info.setEAddress(tOrder.geteAddress());
}
List orders = tGoodsMapper.selectList(new EntityWrapper().eq("order_id", id));
ArrayList tGoodsVos = new ArrayList<>();
TCompany tCompany = tCompanyMapper.selectById(info.getCompanyId());
List tBoxSizes = sizeService.selectList(null);
for (TGoods order : orders) {
TGoodsVo tGoodsVo = new TGoodsVo();
tGoodsVo.setName(tCompany.getName());
tGoodsVo.setSize(order.getSize());
tGoodsVo.setKg(order.getKg());
String size = order.getSize();
tGoodsVo.setType(size);
tGoodsVos.add(tGoodsVo);
}
// 根据订单id 获取价格
// TQuote tQuote = tQuoteMapper.selectList(new EntityWrapper().eq("order_id", tOrder.geteZipZ()).eq("state", 1)).get(0);
// 根据订单id 获取价格
List prices = priceMapper.selectList(new EntityWrapper().eq("order_id", tOrder.getId()));
ArrayList priceVos = new ArrayList();
for (TPrice price : prices) {
TPriceVo tPriceVo = new TPriceVo();
tPriceVo.setId(price.getId());
tPriceVo.setPrice(price.getPrice());
tPriceVo.setType(price.getType());
priceVos.add(tPriceVo);
}
info.setGoodsVos(tGoodsVos);
info.setPriceVos(priceVos);
return info;
}
@Override
public InvoicesVoOne listOfReceivablesInfoOne(Long orderId) {
InvoicesVoOne info = tUserMapper.getOrderInfoOne(orderId);
// 根据订单id 获取货物信息
Long id = info.getId();
TOrder tOrder = this.baseMapper.selectById(id);
List orders = tGoodsMapper.selectList(new EntityWrapper().eq("order_id", id));
ArrayList tGoodsVos = new ArrayList<>();
TCompany tCompany = tCompanyMapper.selectById(info.getCompanyId());
List tBoxSizes = sizeService.selectList(null);
for (TGoods order : orders) {
TGoodsVo tGoodsVo = new TGoodsVo();
tGoodsVo.setName(tCompany.getName());
tGoodsVo.setSize(order.getSize());
tGoodsVo.setKg(order.getKg());
String size = order.getSize();
for (TBoxSize tBoxSize : tBoxSizes) {
if(size.equals(tBoxSize.getBoxName())){
}
}
tGoodsVos.add(tGoodsVo);
}
// 根据订单id 获取价格
List prices = priceMapper.selectList(new EntityWrapper().eq("order_id", tOrder.getId()));
ArrayList priceVos = new ArrayList();
for (TPrice price : prices) {
TPriceVo tPriceVo = new TPriceVo();
tPriceVo.setId(price.getId());
tPriceVo.setPrice(price.getCarPrice());
tPriceVo.setType(price.getType());
priceVos.add(tPriceVo);
}
info.setGoodsVos(tGoodsVos);
info.setPriceVos(priceVos);
return info;
}
@Override
public List getOrderList(Page orderListVoPage, OrderListDto dto) {
String sTime =null;
String eTime = null;
if(ToolUtil.isNotEmpty(dto.getDateRange())){
String[] split = dto.getDateRange().split(" - ");
sTime=split[0]+" 00:00:01";
eTime=split[1]+" 23:59:59";
}
Integer id = dto.getUserId();
List list = this.baseMapper.getOrderListOne(orderListVoPage,sTime,eTime,dto.getCustomerName(),dto.getId(),dto.getGroupId(),dto.getSaleId(),dto.getState(),id,1);
for (OrderListVo orderListVo : list) {
// TODO 对接第三方 看是否能提柜
if(Objects.nonNull(dto.getState())){
if(dto.getState()==1){
}else {
}
}
}
return list;
}
@Override
public List getGoodsInfo(Long orderId) {
TOrder tOrder = this.baseMapper.selectById(orderId);
ArrayList goodsInfoVos = new ArrayList<>();
List goods = tGoodsMapper.selectList(new EntityWrapper().eq("order_id", orderId));
for (TGoods g : goods) {
GoodsInfoVo goodsInfoVo = new GoodsInfoVo();
goodsInfoVo.setAwbNo(g.getBillNumber());
goodsInfoVo.setContainer(g.getContainerNumber());
goodsInfoVo.setHazmat(g.getDanger());
goodsInfoVo.setOverWeight(g.getWeight());
goodsInfoVo.setShippingLine(g.getShippingLine());
goodsInfoVo.setSize(g.getSize());
goodsInfoVo.setType(g.getType());
goodsInfoVo.setTypeClass(g.getTypeClass());
goodsInfoVo.setWeight(g.getKg());
goodsInfoVos.add(goodsInfoVo);
}
return goodsInfoVos;
}
@Autowired
private RedisUtil redisUtil;
@Autowired
private ITExamSiteService examSiteService;
@Autowired
private TYardService yardService;
private HashMap getLat(String a){
HashMap map = new HashMap<>();
try {
GeocodeVo geocode = googleMapUtil.getGeocode(a);
map.put("lon",String.valueOf(geocode.getLng()));
map.put("lat",String.valueOf(geocode.getLat()));
}catch (Exception e){
e.printStackTrace();
map.put("lon",null);
map.put("lat",null);
}
return map;
}
@Override
public HashMap getOrderInfo(Long orderId) {
OrderInfo orderInfo = new OrderInfo();
ArrayList lonLats = new ArrayList<>();
TOrder tOrder = this.baseMapper.selectById(orderId);
String value = redisUtil.getValue(String.valueOf(orderId));
orderInfo.setDriverLat(value);
ArrayList