package com.gree.mall.manager.logic.policy;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.policy.PolicyOrderEmVO;
import com.gree.mall.manager.bean.policy.PolicyOrderInVO;
import com.gree.mall.manager.bean.policy.PolicyOrderVO;
import com.gree.mall.manager.bean.policy.PolicyVo;
import com.gree.mall.manager.commonmapper.PolicyCMapper;
import com.gree.mall.manager.enums.IsYesNoEnum;
import com.gree.mall.manager.enums.PolicyOrderEmStutasEnum;
import com.gree.mall.manager.enums.PolicyOrderStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.*;
import com.gree.mall.manager.utils.StringUtil;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Log4j
public class PolicyOrderLogic {


    @Autowired
    PolicyCMapper policyCMapper;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    PolicyOrderService policyOrderService;

    @Autowired
    AdminWebsitService adminWebsitService;

    @Autowired
    WebsitUserService websitUserService;

    @Autowired
    SysDictCompanyService sysDictCompanyService;

    @Autowired
    WorkerPolicyService workerPolicyService;

    @Autowired
    UserService userService;

    public IPage<PolicyOrderVO> listPolicyOrder(Page page, ZfireParamBean zfireParam) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, PolicyOrderVO.class, adminUser);

        IPage<PolicyOrderVO> policyOrderVOIPage = policyCMapper.listPolicyOrder(page, zfireParam, adminCompanyIds);
        return policyOrderVOIPage;
    }

    public IPage<PolicyOrderEmVO> listEm(Page page, ZfireParamBean zfireParam) {

        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, PolicyOrderEmVO.class, adminUser);

        IPage<PolicyOrderEmVO> policyOrderVOIPage = policyCMapper.listEm(page, zfireParam, adminCompanyIds);

        return policyOrderVOIPage;
    }


    public IPage<PolicyOrderInVO> listIn(Page page, ZfireParamBean zfireParam) {

        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, PolicyOrderInVO.class, adminUser);

        IPage<PolicyOrderInVO> policyOrderVOIPage = policyCMapper.listIn(page, zfireParam, adminCompanyIds);

        return policyOrderVOIPage;
    }

    public PolicyOrder detail(String id) {
        PolicyOrder byId = policyOrderService.getById(id);
        return byId;
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(PolicyOrder policyOrder) {

        if (new Date().after(policyOrder.getStartTime()) && new Date().before(policyOrder.getEndTime()))
            policyOrder.setPolicyOrderStatus("BZZ");
        else if (new Date().after(policyOrder.getEndTime()))
            policyOrder.setPolicyOrderStatus("YSX");
        else {
            policyOrder.setPolicyOrderStatus("DSX");
        }
        policyOrder.updateById();

        if (policyOrder.getPolicyOrderStatus().equals(PolicyOrderStatusEnum.BZZ.getKey()) ||
                policyOrder.getPolicyOrderStatus().equals(PolicyOrderStatusEnum.DSX.getKey())) {

            WorkerPolicy workerPolicy = new WorkerPolicy();
            workerPolicy.setWorkerNumber(policyOrder.getWorkerNumber());
            workerPolicy.setWorkerId(policyOrder.getWebsitUserId());
            workerPolicy.setType("IN");
            workerPolicy.setOrderId(policyOrder.getId());
            workerPolicy.setPolicyName(policyOrder.getPolicyName());
            workerPolicy.setCompany(policyOrder.getBelongCompany());
            workerPolicy.setPolicyOrder(policyOrder.getPolicyNumber());
            workerPolicy.setPolicyType("EM");
            workerPolicy.setStartTime(policyOrder.getStartTime());
            workerPolicy.setEndTime(this.getEndOfDay(policyOrder.getEndTime()));
            workerPolicy.setWebsitUserId(policyOrder.getWebsitUserId());
            workerPolicy.setWebsitId(policyOrder.getWebsitId());
            workerPolicy.setWebsitName(policyOrder.getWebsitName());
            workerPolicy.setWorkerName(policyOrder.getWorkerName());

            if (new Date().after(workerPolicy.getStartTime()) && new Date().before(workerPolicy.getEndTime()))
                workerPolicy.setStatus("BZZ");
            else if (new Date().after(policyOrder.getEndTime()))
                workerPolicy.setStatus("YSX");
            else {
                workerPolicy.setStatus("DSX");
            }
            workerPolicy.insert();

            if (workerPolicy.getPolicyType().equals("EM")) {

                List<WorkerPolicy> workerPolicyList = workerPolicyService.lambdaQuery().eq(WorkerPolicy::getWebsitUserId, policyOrder.getWebsitUserId())
                        .eq(WorkerPolicy::getPolicyType, "EM").list();

                Boolean wp = this.hasOverlap(workerPolicyList);

                if (wp)
                    throw new RemoteServiceException(policyOrder.getWorkerIdcard() + "雇主险有相同覆盖范围内的保险");
            }


        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void importData(List<Object> objects) {

        Integer index = 0;
        for (Object o : objects) {
            List<Object> row = (List<Object>) o;

            ++index;

            String errPrefix = "第" + (index) + "行";

            AdminWebsit adminWebsit = adminWebsitService.lambdaQuery().eq(AdminWebsit::getWebsitNumber, (String) row.get(1)).one();

            User user = userService.lambdaQuery().eq(User::getIdCard, (String) row.get(6))
                    .one();

            if (user == null)
                throw new RemoteServiceException((String) row.get(4)+"身份证找不到对应师傅");

            WebsitUser websitUser = websitUserService.lambdaQuery()
                    .eq(WebsitUser::getWebsitId, (String) row.get(1))
                    .eq(WebsitUser::getUserId, user.getUserId())
                    .one();


            log.info("打印导入数据:"+row.toString());

            if (websitUser==null)
                throw new RemoteServiceException(errPrefix + "未找到该师傅");


            if (StringUtils.isAnyBlank((String) row.get(1), (String) row.get(6))) {
                throw new RemoteServiceException(errPrefix + "黄色区域为必填项");
            }
            PolicyOrder policyOrder = policyOrderService.lambdaQuery()
                    .eq(PolicyOrder::getWebsitId, (String) row.get(1))
                    .eq(PolicyOrder::getWorkerIdcard, (String) row.get(6))
                    .eq(PolicyOrder::getType, "IN")
                    .eq(PolicyOrder::getPolicyOrderStatus, "DGM").one();

            SysDictCompany sysDictCompany = sysDictCompanyService.lambdaQuery().eq(SysDictCompany::getDictValue, row.get(0))
                    .eq(SysDictCompany::getDictType,"INSURE_STEAD_UNIT").one();



            if (policyOrder != null) {


                workerPolicyService.lambdaUpdate().eq(WorkerPolicy::getOrderId,policyOrder.getId()).remove();


                WorkerPolicy workerPolicy = new WorkerPolicy();
                workerPolicy.setWorkerId(policyOrder.getWorkerNumber());
                workerPolicy.setType("IN");
                workerPolicy.setOrderId(policyOrder.getId());
                workerPolicy.setPolicyName(policyOrder.getPolicyName());
                workerPolicy.setCompany(policyOrder.getCompany());
                workerPolicy.setPolicyOrder(policyOrder.getPolicyNumber());
                workerPolicy.setPolicyType("IN");
                workerPolicy.setStartTime(DateUtil.parse((String) row.get(9), "yyyy-MM-dd"));
                workerPolicy.setEndTime(this.getEndOfDay(DateUtil.parse((String) row.get(10), "yyyy-MM-dd")));
                workerPolicy.setWebsitUserId(policyOrder.getWebsitUserId());
                workerPolicy.setWebsitId(policyOrder.getWebsitId());
                workerPolicy.setWebsitName(policyOrder.getWebsitName());
                workerPolicy.setWorkerName(policyOrder.getWorkerName());

                if (new Date().after(workerPolicy.getStartTime()) && new Date().before(workerPolicy.getEndTime()))
                    workerPolicy.setStatus("BZZ");
                else if (new Date().after(workerPolicy.getEndTime()))
                    workerPolicy.setStatus("YSX");
                else {
                    workerPolicy.setStatus("DSX");
                }
                workerPolicy.insert();

                policyOrder.setStartTime(DateUtil.parse((String) row.get(9), "yyyy-MM-dd"));
                policyOrder.setEndTime(this.getEndOfDay(DateUtil.endOfDay(DateUtil.parse((String) row.get(10), "yyyy-MM-dd"))));
                policyOrder.setClassWebsitId((String) row.get(11));
                policyOrder.setClassWebsitName((String) row.get(12));
                policyOrder.setPayAmount(Convert.toBigDecimal(row.get(8)));
                policyOrder.setIsImport(IsYesNoEnum.YES.getKey());
                policyOrder.setBuyUnit(sysDictCompany.getDictValue());
                policyOrder.setBuyUnitId(sysDictCompany.getDictCode());


                if (policyOrder.getStartTime().after(policyOrder.getEndTime()))
                    throw new RemoteServiceException(policyOrder.getWorkerIdcard()+"开始时间不能大于结束时间");

                if (!this.areInSameMonth(policyOrder.getStartTime(),policyOrder.getEndTime()))
                    throw new RemoteServiceException(policyOrder.getWorkerIdcard()+"工伤险的有效时间必须在同一个月内");


                if (new Date().after(policyOrder.getStartTime()) && new Date().before(policyOrder.getEndTime()))
                    policyOrder.setPolicyOrderStatus("BZZ");
                else if (new Date().after(policyOrder.getEndTime()))
                    policyOrder.setPolicyOrderStatus("YSX");
                else {
                    policyOrder.setPolicyOrderStatus("DSX");
                }

                policyOrder.updateById();

            }else {


                PolicyOrder policyOrderup = new PolicyOrder();

                policyOrderup.setId(IdWorker.getIdStr());
                policyOrderup.setPolicyId(policyOrderup.getId());
                policyOrderup.setPolicyNumber(policyOrderup.getId());
                policyOrderup.setPolicyName((String)row.get(7));

                policyOrderup.setType("IN");

                policyOrderup.setWebsitUserId(websitUser.getId());

                policyOrderup.setStartTime(DateUtil.parse((String) row.get(9), "yyyy-MM-dd"));
                policyOrderup.setEndTime(this.getEndOfDay(DateUtil.parse((String) row.get(10), "yyyy-MM-dd")));

                policyOrderup.setIsSend(IsYesNoEnum.YES.getKey());
                policyOrderup.setIsReceipt(IsYesNoEnum.YES.getKey());


                policyOrderup.setBelongCompany(adminWebsit.getBelongCompany());
                policyOrderup.setBelongCompanyCode(adminWebsit.getBelongCompanyCode());

                policyOrderup.setCompanyWechatId(adminWebsit.getCompanyWechatId());
                policyOrderup.setCompanyWechatName(adminWebsit.getCompanyWechatName());

                policyOrderup.setWebsitId(adminWebsit.getWebsitId());
                policyOrderup.setWebsitName(adminWebsit.getName());

                policyOrderup.setWorkerIdcard(user.getIdCard());
                policyOrderup.setWorkerMobile(user.getMobile());
                policyOrderup.setWorkerName(user.getNickName());
                policyOrderup.setWorkerNumber(user.getWorkerNumber());
                policyOrderup.setIsImport(IsYesNoEnum.YES.getKey());
                policyOrderup.setIsImport(IsYesNoEnum.YES.getKey());
                policyOrderup.setBuyUnit(sysDictCompany.getDictValue());
                policyOrderup.setBuyUnitId(sysDictCompany.getDictCode());

                policyOrderup.setClassWebsitId((String)row.get(11));
                policyOrderup.setClassWebsitName((String)row.get(12));
                policyOrderup.setPayAmount(Convert.toBigDecimal(row.get(8)));

                if (policyOrderup.getStartTime().after(policyOrderup.getEndTime()))
                    throw new RemoteServiceException(policyOrderup.getWorkerIdcard()+"开始时间不能大于结束时间");

                if (!this.areInSameMonth(policyOrderup.getStartTime(),policyOrderup.getEndTime()))
                    throw new RemoteServiceException(policyOrderup.getWorkerIdcard()+"工伤险的有效时间必须在同一个月内");


                if (new Date().after(policyOrderup.getStartTime()) && new Date().before(policyOrderup.getEndTime()))
                    policyOrderup.setPolicyOrderStatus("BZZ");
                else if (new Date().after(policyOrderup.getEndTime()))
                    policyOrderup.setPolicyOrderStatus("YSX");
                else {
                    policyOrderup.setPolicyOrderStatus("DSX");
                }

                Integer count = policyOrderService.lambdaQuery().le(PolicyOrder::getStartTime, policyOrderup.getEndTime())
                        .ge(PolicyOrder::getEndTime, policyOrderup.getStartTime())
                        .eq(PolicyOrder::getType, "IN")
                        .ne(PolicyOrder::getPolicyOrderStatus, "YSX")
                        .eq(PolicyOrder::getWebsitUserId, policyOrderup.getWebsitUserId()).count();
                log.error("打印保险导入1+"+count);
                if (count > 0){
                    throw new RemoteServiceException(errPrefix + "师傅已导入该时间段得保险");
                }

                policyOrderup.insert();



                WorkerPolicy workerPolicy = new WorkerPolicy();
                workerPolicy.setWorkerId(websitUser.getWorkerNumber());
                workerPolicy.setType("IN");
                workerPolicy.setOrderId(policyOrderup.getId());
                workerPolicy.setPolicyName(policyOrderup.getPolicyName());
                workerPolicy.setCompany(policyOrderup.getCompany());

                workerPolicy.setPolicyType("IN");
                workerPolicy.setStartTime(policyOrderup.getStartTime());
                workerPolicy.setEndTime(policyOrderup.getEndTime());
                workerPolicy.setWebsitUserId(websitUser.getId());
                workerPolicy.setWebsitId(websitUser.getWebsitId());
                workerPolicy.setWebsitName(adminWebsit.getName());
                workerPolicy.setWorkerName(user.getNickName());

                if (new Date().after(workerPolicy.getStartTime()) && new Date().before(workerPolicy.getEndTime()))
                    workerPolicy.setStatus("BZZ");
                else if (new Date().after(workerPolicy.getEndTime()))
                    workerPolicy.setStatus("YSX");
                else {
                    workerPolicy.setStatus("DSX");
                }

                if (workerPolicyService.lambdaQuery().le(WorkerPolicy::getStartTime,policyOrderup.getEndTime())
                        .ge(WorkerPolicy::getEndTime,policyOrderup.getStartTime())
                        .eq(WorkerPolicy::getPolicyType,"IN")
                        .ne(WorkerPolicy::getStatus,"YSX")
                        .eq(WorkerPolicy::getWebsitUserId,policyOrderup.getWebsitUserId()).count() > 0){
                    throw new RemoteServiceException(errPrefix + "师傅已导入该时间段得保险");
                }

                workerPolicy.insert();



            }

            List<WorkerPolicy> workerPolicyList = workerPolicyService.lambdaQuery().eq(WorkerPolicy::getWebsitUserId, websitUser.getId())
                    .eq(WorkerPolicy::getPolicyType, "IN").list();

            Boolean wp = this.hasOverlap(workerPolicyList);

            if (wp)
                throw new RemoteServiceException(user.getIdCard()+"工伤险有相同覆盖范围内的保险");



        }
    }

    public boolean areInSameMonth(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        cal1.setTime(date1);
        cal2.setTime(date2);

        return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
    }

    @Transactional(rollbackFor = Exception.class)
    public void importEm(List<Object> objects) {

        for (Object o : objects) {
            List<Object> row = (List<Object>) o;

            AdminWebsit adminWebsit = adminWebsitService.lambdaQuery().eq(AdminWebsit::getWebsitNumber, (String) row.get(1)).one();

            if (!adminWebsit.getAttr().equals("SELF"))
                throw new RemoteServiceException(adminWebsit.getName()+"第三方网点请去师傅管理添加雇主险");

            User user = userService.lambdaQuery().eq(User::getIdCard, (String) row.get(4))
                    .one();

            if (user == null)
                throw new RemoteServiceException((String) row.get(4)+"身份证找不到对应师傅");

            WebsitUser websitUser = websitUserService.lambdaQuery()
                    .eq(WebsitUser::getWebsitId, (String) row.get(1))
                    .eq(WebsitUser::getUserId, user.getUserId())
                    .one();


            List<PolicyOrder> policyOrders = policyOrderService.lambdaQuery()
                    .eq(PolicyOrder::getWebsitId, (String) row.get(1))
                    .eq(PolicyOrder::getWorkerNumber, (String) row.get(3))
                    .eq(PolicyOrder::getType, "EM")
                    .in(PolicyOrder::getPolicyOrderStatus, PolicyOrderEmStutasEnum.XG.getKey(),
                            PolicyOrderEmStutasEnum.DSX.getKey(),
                            PolicyOrderEmStutasEnum.KTH.getKey()

                    ).list();
            for (PolicyOrder order : policyOrders) {
                workerPolicyService.lambdaUpdate().eq(WorkerPolicy::getOrderId,order.getId()).remove();
            }


            policyOrderService.lambdaUpdate()
                    .eq(PolicyOrder::getWebsitId, (String) row.get(1))
                    .eq(PolicyOrder::getWebsitUserId, websitUser.getId())
                    .eq(PolicyOrder::getType, "EM")
                    .in(PolicyOrder::getPolicyOrderStatus, PolicyOrderEmStutasEnum.XG.getKey(),
                            PolicyOrderEmStutasEnum.KTH.getKey(),
                            PolicyOrderEmStutasEnum.DSX.getKey(),
                            PolicyOrderEmStutasEnum.DTH.getKey()

                    ).remove();



            PolicyOrder policyOrder = new PolicyOrder();
            policyOrder.setId("PW"+ IdWorker.getIdStr());

            policyOrder.setPolicyName("雇主险");
            policyOrder.setPolicyId(policyOrder.getId());
            policyOrder.setPolicyNumber(policyOrder.getId());

            policyOrder.setWebsitUserId(websitUser.getId());

            policyOrder.setStartTime(DateUtil.parse((String) row.get(6), "yyyy-MM-dd"));
            policyOrder.setEndTime(this.getEndOfDay(DateUtil.parse((String) row.get(7), "yyyy-MM-dd")));

            policyOrder.setIsSend(IsYesNoEnum.NO.getKey());
            policyOrder.setIsReceipt(IsYesNoEnum.NO.getKey());


            policyOrder.setBelongCompany(adminWebsit.getBelongCompany());
            policyOrder.setBelongCompanyCode(adminWebsit.getBelongCompanyCode());

            policyOrder.setCompanyWechatId(adminWebsit.getCompanyWechatId());
            policyOrder.setCompanyWechatName(adminWebsit.getCompanyWechatName());

            policyOrder.setWebsitId(adminWebsit.getWebsitId());
            policyOrder.setWebsitName(adminWebsit.getName());

            policyOrder.setWorkerIdcard((String) row.get(4));
            policyOrder.setWorkerMobile((String) row.get(5));
            policyOrder.setWorkerName((String) row.get(2));
            policyOrder.setWorkerNumber((String) row.get(3));
            policyOrder.setIsImport(IsYesNoEnum.YES.getKey());

            policyOrder.setType("EM");

            if (policyOrder.getStartTime().after(policyOrder.getEndTime()))
                throw new RemoteServiceException((String) row.get(4)+"开始时间不能大于结束时间");

            if (new Date().after(policyOrder.getStartTime()) && new Date().before(policyOrder.getEndTime()))
                policyOrder.setPolicyOrderStatus("BZZ");
            else if (new Date().after(policyOrder.getEndTime()))
                policyOrder.setPolicyOrderStatus("YSX");
            else {
                policyOrder.setPolicyOrderStatus("DSX");
            }

            policyOrder.insert();




            WorkerPolicy workerPolicy = new WorkerPolicy();
            workerPolicy.setWorkerId(websitUser.getWorkerNumber());
            workerPolicy.setType("LINE");
            workerPolicy.setOrderId(policyOrder.getId());
            workerPolicy.setPolicyName(policyOrder.getPolicyName());
            workerPolicy.setCompany(policyOrder.getBelongCompany());
            workerPolicy.setPolicyOrder(policyOrder.getPolicyNumber());
            workerPolicy.setPolicyType("EM");
            workerPolicy.setStartTime(DateUtil.parse((String) row.get(6), "yyyy-MM-dd"));
            workerPolicy.setEndTime(this.getEndOfDay(DateUtil.endOfDay(DateUtil.parse((String) row.get(7), "yyyy-MM-dd"))));
            workerPolicy.setWebsitUserId(policyOrder.getWebsitUserId());
            workerPolicy.setWebsitId(policyOrder.getWebsitId());
            workerPolicy.setWebsitName(policyOrder.getWebsitName());
            workerPolicy.setWorkerName(policyOrder.getWorkerName());

            if (new Date().after(workerPolicy.getStartTime()) && new Date().before(workerPolicy.getEndTime()))
                workerPolicy.setStatus("BZZ");
            else if (new Date().after(policyOrder.getEndTime()))
                workerPolicy.setStatus("YSX");
            else {
                workerPolicy.setStatus("DSX");
            }
            workerPolicy.insert();

            List<WorkerPolicy> workerPolicyList = workerPolicyService.lambdaQuery().eq(WorkerPolicy::getWebsitUserId, websitUser.getId())
                    .eq(WorkerPolicy::getPolicyType, "EM").list();

            Boolean wp = this.hasOverlap(workerPolicyList);

            if (wp)
                throw new RemoteServiceException(user.getIdCard()+"雇主险有相同覆盖范围内的保险");



        }
    }


    public Boolean hasOverlap(List<WorkerPolicy> workerPolicies){

        for (int i= 0; i<workerPolicies.size();i++){
            WorkerPolicy workerPolicy = workerPolicies.get(i);
            for (int j = i+1; j <workerPolicies.size() ; j++) {
                WorkerPolicy workerPolicy2 = workerPolicies.get(j);
                if (workerPolicy.getEndTime().after(workerPolicy2.getStartTime()) && workerPolicy2.getEndTime().after(workerPolicy.getStartTime())){
                    return true;
                }
            }
        }

        return false;

    }

    /**
     * 获取某天最小时间
     * @param date 时间
     * @return 获取某天最小时间
     */
    public  Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
        LocalTime localTime = LocalTime.of(23, 59, 59);
        LocalDateTime endOfDay = localDateTime.with(localTime);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

}
