package com.seeyon.apps.src_ydctleasebill.util;

import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.*;

public class LeaseBillUtil {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 生成租金账单
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param isMaxDay 是否每月最后一天
     * @param jfzqNum 缴费周期月
     * @param jsonObject 请求参数
     * @param jifeifs 计费方式
     * @return
     */
    public List<Map<String,String>> getLeaseBill(Calendar startTime, Calendar endTime, boolean isMaxDay, int jfzqNum,
                                                 JSONObject jsonObject,String jifeifs){
        List<Map<String,String>> rents = new ArrayList<>();
        DateUtil dateUtil = new DateUtil();
//        		两个日期相差多少月
        int monthNum = dateUtil.betweenMonthByTwoCalendar(startTime,endTime);
        if(jfzqNum==99){
//            一次性账单生成
            Map<String,String> rent = new HashMap<>();
            float zlmjFloat = 0;
            double zf = 0;
            if("面积计费".equals(jifeifs)){
                String mjStr = jsonObject.getString("mj");
                zlmjFloat = Float.parseFloat(mjStr);
                String mjzjStr = jsonObject.getString("mjzj");
                double mjzj = dateUtil.stringToNum(mjzjStr);
                zf = mjzj*zlmjFloat;
            }else if ("固定租金".equals(jifeifs)){
                zlmjFloat = 1;
                String gdzjStr = jsonObject.getString("gdzj");
                double gdzj = dateUtil.stringToNum(gdzjStr);
                zf = gdzj*zlmjFloat;
            }
            rent.put("开始日期",sdf.format(startTime.getTime()));
            endTime.add(Calendar.DATE, -1);
            rent.put("结束日期",sdf.format(endTime.getTime()));
            rent.put("租费",zf+"");
            rents.add(rent);
        }else{
//            月，季，半年，账单生成
            int termNum = monthNum/jfzqNum;
            int mo = monthNum%jfzqNum;
            if(mo!=0){
                termNum++;
            }
            float zlmjFloat = 0;
            double zf = 0;
            if("面积计费".equals(jifeifs)){
                String mjStr = jsonObject.getString("mj");
                zlmjFloat = Float.parseFloat(mjStr);
                String mjzjStr = jsonObject.getString("mjzj");
                double mjzj = dateUtil.stringToNum(mjzjStr);
                zf = mjzj*zlmjFloat;
            }else if ("固定租金".equals(jifeifs)){
                zlmjFloat = 1;
                String gdzjStr = jsonObject.getString("gdzj");
                double gdzj = dateUtil.stringToNum(gdzjStr);
                zf = gdzj*zlmjFloat;
            }
            for(int i = 0 ; i < termNum ; i ++) {
                if (startTime.getTime().getTime() < endTime.getTime().getTime()) {
                    Map<String, String> rentMap = new HashMap<String, String>();
//        			设置租费时间
                    rentMap.put("开始日期", sdf.format(startTime.getTime()));
                    if (!isMaxDay) {
                        startTime.add(Calendar.MONTH, jfzqNum);
                        startTime.add(Calendar.DAY_OF_MONTH, -1);
                    }else{
                        startTime.add(Calendar.MONTH, jfzqNum);
                        startTime.set(Calendar.DAY_OF_MONTH, startTime.getActualMaximum(Calendar.DAY_OF_MONTH)-1);
                    }
                    if (startTime.getTime().getTime() < endTime.getTime().getTime()) {
//                        rentMap.put("期间月数-租金",jfzqNum+"");
                        rentMap.put("租费", (zf * jfzqNum) + "");
                        rentMap.put("结束日期", sdf.format(startTime.getTime()));
                    } else {
                        //两个日期相差多少月
                        int qijianyueshu = dateUtil.betweenMonthByTwoCalendar(startTime, endTime);
//                        rentMap.put("期间月数-租金",qijianyueshu+"");
                        rentMap.put("租费", (zf * qijianyueshu) + "");
                        endTime.add(Calendar.DAY_OF_MONTH, -1);
                        rentMap.put("结束日期", sdf.format(endTime.getTime()));
                    }
                    startTime.add(Calendar.DAY_OF_MONTH, 1);
                    if (isMaxDay) {
                        startTime.set(Calendar.DAY_OF_MONTH, startTime.getActualMaximum(Calendar.DAY_OF_MONTH));
                    }
                    rents.add(rentMap);
                }
            }
        }
        return rents;
    }


    /**
     * 生成租金账单
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param isMaxDay 是否每月最后一天
     * @param jfzqNum 缴费周期月
     * @param jsonObject 请求参数
     * @param jifeifs 计费方式
     * @return
     */
    public List<Map<String,String>> getLeaseBillYers1(Calendar startTime, Calendar endTime, boolean isMaxDay, int jfzqNum,
                                                 JSONObject jsonObject,String jifeifs){
        List<Map<String,String>> rents = new ArrayList<>();
        DateUtil dateUtil = new DateUtil();
//        		两个日期相差多少月
        int monthNum = dateUtil.betweenMonthByTwoCalendar(startTime,endTime);
            int termNum = monthNum/jfzqNum;
            int mo = monthNum%jfzqNum;
            if(mo!=0){
                termNum++;
            }
            float zlmjFloat = 0;
            double zf = 0;
            if("面积计费".equals(jifeifs)){
                String mjStr = jsonObject.getString("mj");
                zlmjFloat = Float.parseFloat(mjStr);
                String mjzjStr = jsonObject.getString("mjzj");
                double mjzj = dateUtil.stringToNum(mjzjStr);
                zf = mjzj*zlmjFloat;
            }else if ("固定租金".equals(jifeifs)){
                zlmjFloat = 1;
                String gdzjStr = jsonObject.getString("gdzj");
                double gdzj = dateUtil.stringToNum(gdzjStr);
                zf = gdzj*zlmjFloat;
            }
        for(int i = 0 ; i < termNum ; i ++) {
            if (startTime.getTime().getTime() < endTime.getTime().getTime()) {
                Map<String, String> rentMap = new HashMap<String, String>();
//        			设置租费时间
                rentMap.put("开始日期", sdf.format(startTime.getTime()));
                startTime.add(Calendar.MONTH, jfzqNum);
                startTime.add(Calendar.DAY_OF_MONTH, -1);
                if (startTime.getTime().getTime() < endTime.getTime().getTime()) {
//                        rentMap.put("期间月数-租金",jfzqNum+"");
                    rentMap.put("租费", (zf * jfzqNum) + "");
                    rentMap.put("结束日期", sdf.format(startTime.getTime()));
                } else {
                    //两个日期相差多少月
                    int qijianyueshu = dateUtil.betweenMonthByTwoCalendar(startTime, endTime);
//                        rentMap.put("期间月数-租金",qijianyueshu+"");
                    rentMap.put("租费", (zf * qijianyueshu) + "");
                    endTime.add(Calendar.DAY_OF_MONTH, -1);
                    rentMap.put("结束日期", sdf.format(endTime.getTime()));
                }
                startTime.add(Calendar.DAY_OF_MONTH, 1);
                if (isMaxDay) {
                    startTime.set(Calendar.DAY_OF_MONTH, startTime.getActualMaximum(Calendar.DAY_OF_MONTH));
                }
                rents.add(rentMap);
            }
        }
        return rents;
    }


 /**
     * 生成租金账单
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param isMaxDay 是否每月最后一天
     * @param jfzqNum 缴费周期月
     * @param jsonObject 请求参数
     * @param jifeifs 计费方式
     * @return
     */
    public List<Map<String,String>> getLeaseBillYers2(Calendar startTime, Calendar endTime, boolean isMaxDay, int jfzqNum,
                                                 JSONObject jsonObject,String jifeifs){
        List<Map<String,String>> rents = new ArrayList<>();
        DateUtil dateUtil = new DateUtil();
//        		两个日期相差多少年
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime.getTime());
        int starYear = startTime.get(Calendar.YEAR);
        int endYear = endTime.get(Calendar.YEAR);
        int termNum = endYear-starYear+1;
            float zlmjFloat = 0;
            double zf = 0;
            if("面积计费".equals(jifeifs)){
                String mjStr = jsonObject.getString("mj");
                zlmjFloat = Float.parseFloat(mjStr);
                String mjzjStr = jsonObject.getString("mjzj");
                double mjzj = dateUtil.stringToNum(mjzjStr);
                zf = mjzj*zlmjFloat;
            }else if ("固定租金".equals(jifeifs)){
                zlmjFloat = 1;
                String gdzjStr = jsonObject.getString("gdzj");
                double gdzj = dateUtil.stringToNum(gdzjStr);
                zf = gdzj*zlmjFloat;
            }

        for(int i = 0 ; i < termNum ; i ++) {
            if (startTime.getTime().getTime() < endTime.getTime().getTime()) {
                Map<String, String> rentMap = new HashMap<String, String>();
//        			设置租费时间
                rentMap.put("开始日期", sdf.format(startTime.getTime()));
                startTime.set(Calendar.MONTH, 11);
                startTime.set(Calendar.DATE, 31);
                if (startTime.getTime().getTime() < endTime.getTime().getTime()) {
//                        rentMap.put("期间月数-租金",jfzqNum+"");
                    startTime.add(Calendar.DAY_OF_MONTH, 1);
                    int qijianyueshu = dateUtil.betweenMonthByTwoCalendar(calendar, startTime);
                    startTime.add(Calendar.DAY_OF_MONTH, -1);
                    rentMap.put("租费", (zf * qijianyueshu) + "");
                    rentMap.put("结束日期", sdf.format(startTime.getTime()));
                    calendar.set(Calendar.MONTH, 11);
                    calendar.set(Calendar.DATE, 31);
                } else {
                    //两个日期相差多少月
                    int qijianyueshu = dateUtil.betweenMonthByTwoCalendar(calendar, endTime);
//                        rentMap.put("期间月数-租金",qijianyueshu+"");
                    rentMap.put("租费", (zf * qijianyueshu) + "");
                    endTime.add(Calendar.DAY_OF_MONTH, -1);
                    rentMap.put("结束日期", sdf.format(endTime.getTime()));
                }
                startTime.add(Calendar.DAY_OF_MONTH, 1);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                if (isMaxDay) {
                    startTime.set(Calendar.DAY_OF_MONTH, startTime.getActualMaximum(Calendar.DAY_OF_MONTH));
                }
                rents.add(rentMap);
            }
        }
        return rents;
    }

}
