package com.bohuikeji.delivery.backstage.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.delivery.backstage.entity.rental.TRentalCarOrder;
import com.bohuikeji.delivery.backstage.entity.rental.TRentalConfig;
import com.bohuikeji.delivery.backstage.mapper.TRentalCarOrderMapper;
import com.bohuikeji.delivery.backstage.service.ITRentalCarOrderService;
import com.bohuikeji.delivery.backstage.service.ITRentalConfigService;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.entity.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 货架信息表 服务实现类
 * </p>
 *
 * @author 10552
 * @since 2019-08-22
 */
@Service
public class TRentalCarOrderServiceImpl extends ServiceImplBase<TRentalCarOrderMapper, TRentalCarOrder> implements ITRentalCarOrderService {
    @Autowired
    ITRentalConfigService rentalConfigService;

    @Override
    public TRentalCarOrder getCarOrderCharg(TRentalCarOrder rentalCarOrder) {
        TRentalConfig tRentalConfig = rentalConfigService.getById(rentalCarOrder.getCarConfigId());
        if(tRentalConfig != null){
            BigDecimal carRentalPrice=new BigDecimal(tRentalConfig.getConfigValue().getString("车辆租赁及服务费"));
            BigDecimal basicServicePrice=new BigDecimal(tRentalConfig.getConfigValue().getString("基础服务费"));
            BigDecimal equipmentPrice=new BigDecimal(tRentalConfig.getConfigValue().getString("车辆装备费"));
            rentalCarOrder.setRentalExpectPrice(rentalCarOrder.getRentalExpectPrice().add(carRentalPrice).add(basicServicePrice).add(equipmentPrice));
        }
        LocalDateTime takeTime=LocalDateTime.parse(rentalCarOrder.getTakeCarTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
        LocalDateTime dstillTime=LocalDateTime.parse(rentalCarOrder.getStillCarTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
        if(getTimeNum(takeTime,dstillTime).getInteger("time")<24){
            rentalCarOrder.setRentalCarInterval(getTimeNum(takeTime,dstillTime).getInteger("time")+"小时");
        }else {
            BigDecimal datePrice=new BigDecimal(tRentalConfig.getConfigValue().getString("司驾服务费"));
            long dateNum = getTimeNum(takeTime,dstillTime).getInteger("date");
            rentalCarOrder.setRentalCarInterval(dateNum+"天");
            BigDecimal prices = datePrice.multiply(new BigDecimal(dateNum));
            rentalCarOrder.setRentalExpectPrice(rentalCarOrder.getRentalExpectPrice().add(prices));
        }
        AddressVo sendVo=rentalCarOrder.getTakeAddress().fillAddress();//取车地址
        if(rentalCarOrder.getIsReturn()){
            AddressVo receiveVo=rentalCarOrder.getStillAddress().fillAddress();//还车地址
            rentalCarOrder.setStillAddress(receiveVo);
            rentalCarOrder.setIsReturn(rentalCarOrder.getIsReturn());
        }

        rentalCarOrder.setRentalOrderTime(LocalDateTime.now());
        rentalCarOrder.setTakeAddress(sendVo);
        rentalCarOrder.setTakeCarTime(rentalCarOrder.getTakeCarTime());
        rentalCarOrder.setStillCarTime(rentalCarOrder.getStillCarTime());
        return rentalCarOrder;
    }

    @Override
    public JSONArray getDateTimeArray(LocalDateTime takeCarTime,LocalDateTime stillCarTime) {
        JSONObject dateTimeTake = new JSONObject();//取车
        JSONObject dateTimeStill = new JSONObject();//还车
        JSONObject dateTimeNum = new JSONObject();//间隔时间
        JSONArray tsArray = new JSONArray();
        String takeDateStr = takeCarTime.getMonthValue()+"月"+takeCarTime.getDayOfMonth()+"日";//取车日期
        String takeTimeStr = takeCarTime.getHour()+":"+takeCarTime.getMinute();//取车时间
        String takeWeekStr = "星期"+takeCarTime.getDayOfWeek().getValue();//取车星期
        dateTimeTake.put("takeDateStr",takeDateStr);
        dateTimeTake.put("takeTimeStr",takeTimeStr);
        dateTimeTake.put("takeWeekStr",takeWeekStr);
        String stillDateStr = stillCarTime.getMonthValue()+"月"+stillCarTime.getDayOfMonth()+"日";//还车日期
        String stillTimeStr = stillCarTime.getHour()+":"+stillCarTime.getMinute();//还车时间
        String stillWeekStr = "星期"+stillCarTime.getDayOfWeek().getValue();//还车星期
        dateTimeStill.put("stillDateStr",stillDateStr);
        dateTimeStill.put("stillTimeStr",stillTimeStr);
        dateTimeStill.put("stillWeekStr",stillWeekStr);
        tsArray.add(dateTimeTake);
        tsArray.add(dateTimeStill);
        if(getTimeNum(takeCarTime,stillCarTime).getInteger("time")<12){
            dateTimeNum.put("num",getTimeNum(takeCarTime,stillCarTime).getInteger("time"));
            dateTimeNum.put("type","小时");
        }else {
            dateTimeNum.put("num",getTimeNum(takeCarTime,stillCarTime).getInteger("date"));
            dateTimeNum.put("type","天");
        }
        tsArray.add(dateTimeNum);
        return tsArray;
    }

    private static JSONObject getTimeNum(LocalDateTime takeCarTime,LocalDateTime stillCarTime){
        JSONObject jsonObject = new JSONObject();
        long hoursNum = Duration.between(takeCarTime,stillCarTime).toHours();//4
        long daysNum = Duration.between(takeCarTime,stillCarTime).toDays();//4
        jsonObject.put("time",hoursNum);
        jsonObject.put("date",daysNum);
        return jsonObject;
    }

}
