package me.zhengjie.modules.dm.statement.service.impl;

import com.zaxxer.hikari.HikariDataSource;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.statement.domain.DmHaveDinner;
import me.zhengjie.modules.dm.statement.repository.DmHaveDinnerRepository;
import me.zhengjie.modules.dm.statement.service.DmHaveDinnerService;
import me.zhengjie.modules.dm.statement.service.dto.DmHaveDinnerDto;
import me.zhengjie.modules.dm.statement.service.dto.DmStatementDto;
import me.zhengjie.modules.dm.statement.service.mapstruct.DmHaveDinnerMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.*;

@Transactional
@Service
@RequiredArgsConstructor
public class DmHaveDinnerServiceImpl implements DmHaveDinnerService {
    private static HikariDataSource JDBCUtils;
    private final DmHaveDinnerMapper dmHaveDinnerMapper;
    private final DmHaveDinnerRepository dmHaveDinnerRepository;

    @Override
    public Map<String, Object> selectZgHaveDinner(String stateDate, String endDate, String type, Pageable pageable) {
        Page<DmHaveDinner> page = null;
        Date cal = new Date();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat year = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//yyyy-MM-dd hh：mm：ss
        SimpleDateFormat year1 = new SimpleDateFormat("yyyy");
        if (type==null){
            calendar.setTime(cal);
            calendar.add(Calendar.YEAR,-1);
            dmHaveDinnerRepository.dropByZgYear();
            dmHaveDinnerRepository.addZgYear(year.format(calendar.getTime()),year.format(cal));
            page = dmHaveDinnerRepository.selectByZgMonth(pageable);
        }
        if (type.equals("year")){
            if (stateDate==null && endDate ==null){
                calendar.setTime(cal);
                calendar.add(Calendar.YEAR,-2);
                dmHaveDinnerRepository.dropByZgYear();
                dmHaveDinnerRepository.addZgYear(year.format(cal),year1.format(calendar.getTime()));
                page = dmHaveDinnerRepository.selectByZgYear(pageable);
            }else {
                dmHaveDinnerRepository.dropByZgYear();
                dmHaveDinnerRepository.addZgYear(stateDate,endDate);
                page = dmHaveDinnerRepository.selectByZgYear(pageable);
            }
        }else if (type.equals("month")){
            if (stateDate==null || endDate ==null){
                calendar.setTime(cal);
                calendar.add(Calendar.YEAR,-1);
                dmHaveDinnerRepository.dropByZgMonth();
                dmHaveDinnerRepository.addZgMonth(year.format(cal),year.format(calendar.getTime()));
                page = dmHaveDinnerRepository.selectByZgMonth(pageable);
            }else {
                dmHaveDinnerRepository.dropByZgMonth();
                dmHaveDinnerRepository.addZgMonth(stateDate,endDate);
                page = dmHaveDinnerRepository.selectByZgMonth(pageable);
            }
        }else if (type.equals("day")){
            if (stateDate==null || endDate ==null){
                calendar.setTime(cal);
                calendar.add(Calendar.DAY_OF_MONTH, -90);
                dmHaveDinnerRepository.dropByZgDay();
                dmHaveDinnerRepository.addZgDay(year.format(cal),year.format(calendar.getTime()));
                page = dmHaveDinnerRepository.selectByZgDay(pageable);
            }else {
                dmHaveDinnerRepository.dropByZgDay();
                dmHaveDinnerRepository.addZgDay(stateDate,endDate);
                page = dmHaveDinnerRepository.selectByZgDay(pageable);
            }
        }else if (type.equals("week")){
            if (stateDate==null || endDate ==null){
                calendar.setTime(cal);
                calendar.add(Calendar.WEEK_OF_YEAR,-15);
                dmHaveDinnerRepository.dropByZgWeek();
                dmHaveDinnerRepository.addZgWeek(year.format(cal),year.format(calendar.getTime()));
                page = dmHaveDinnerRepository.selectByZgWeek(pageable);
            }else {
                dmHaveDinnerRepository.dropByZgWeek();
                dmHaveDinnerRepository.addZgWeek(stateDate,endDate);
                page = dmHaveDinnerRepository.selectByZgWeek(pageable);
            }
        }
        return PageUtil.toPage(page.map(dmHaveDinnerMapper::toDto));
    }

    @Override
    public Map<String, Object> selectZdHaveDinner(String stateDate, String endDate, String type, Pageable pageable) {
        Page<DmHaveDinner> page = null;
        Date cal = new Date();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat year = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//yyyy-MM-dd hh：mm：ss
        SimpleDateFormat year1 = new SimpleDateFormat("yyyy");
        if (type.equals("year")){
            if (stateDate==null && endDate ==null){
                calendar.setTime(cal);
                calendar.add(Calendar.YEAR,-3);
                dmHaveDinnerRepository.dropByZdYear();
                dmHaveDinnerRepository.addZdYear(year.format(cal),year1.format(calendar.getTime()));
                page = dmHaveDinnerRepository.selectByZdYear(pageable);
            }else {
                dmHaveDinnerRepository.dropByZdYear();
                dmHaveDinnerRepository.addZdYear(stateDate,endDate);
                page = dmHaveDinnerRepository.selectByZdYear(pageable);
            }
        }else if (type.equals("month")){
            if (stateDate==null || endDate ==null){
                calendar.setTime(cal);
                calendar.add(Calendar.YEAR,-1);
                dmHaveDinnerRepository.dropByZdMonth();
                dmHaveDinnerRepository.addZdMonth(year.format(cal),year.format(calendar.getTime()));
                page = dmHaveDinnerRepository.selectByZdMonth(pageable);
            }else {
                dmHaveDinnerRepository.dropByZdMonth();
                dmHaveDinnerRepository.addZdMonth(stateDate,endDate);
                page = dmHaveDinnerRepository.selectByZdMonth(pageable);
            }
        }else if (type.equals("day")){
            if (stateDate==null || endDate ==null){
//                Calendar calendar1 = new GregorianCalendar();
                calendar.setTime(cal);
                calendar.add(Calendar.DAY_OF_MONTH, -90);
                dmHaveDinnerRepository.dropByZdDay();
                dmHaveDinnerRepository.addZdDay(year.format(cal),year.format(calendar.getTime()));
                page = dmHaveDinnerRepository.selectByZdDay(pageable);
            }else {
                dmHaveDinnerRepository.dropByZdDay();
                dmHaveDinnerRepository.addZdDay(stateDate,endDate);
                page = dmHaveDinnerRepository.selectByZdDay(pageable);
            }
        }else if (type.equals("week")){
            if (stateDate==null || endDate ==null){
                calendar.setTime(cal);
                calendar.add(Calendar.WEEK_OF_YEAR,-15);
                dmHaveDinnerRepository.dropByZdWeek();
                dmHaveDinnerRepository.addZdWeek(year.format(cal),year.format(calendar.getTime()));
                page = dmHaveDinnerRepository.selectByZdWeek(pageable);
            }else {
                dmHaveDinnerRepository.dropByZdWeek();
                dmHaveDinnerRepository.addZdWeek(stateDate,endDate);
                page = dmHaveDinnerRepository.selectByZdWeek(pageable);
            }
        }

        return PageUtil.toPage(page.map(dmHaveDinnerMapper::toDto));
    }

    @Override
    public void download(List<DmHaveDinnerDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmHaveDinnerDto dmHaveDinnerDto : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("菜品受欢迎前三-菜品名称", dmHaveDinnerDto.getZgName());
            map.put("菜品受欢迎前三-购买次数", dmHaveDinnerDto.getZgNum());
            map.put("最不受欢迎菜品", dmHaveDinnerDto.getZdName());
            map.put("最不受欢迎菜品-购买次数", dmHaveDinnerDto.getZdNum());
            map.put("最不受欢迎菜品-总消费", dmHaveDinnerDto.getMoney());
            map.put("统计时段", dmHaveDinnerDto.getTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
    @Override
    public void downWeekload(List<DmHaveDinnerDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmHaveDinnerDto dmHaveDinnerDto : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("菜品受欢迎前三-菜品名称", dmHaveDinnerDto.getZgName());
            map.put("菜品受欢迎前三-购买次数", dmHaveDinnerDto.getZgNum());
            map.put("最不受欢迎菜品", dmHaveDinnerDto.getZdName());
            map.put("最不受欢迎菜品-购买次数", dmHaveDinnerDto.getZdNum());
            map.put("最不受欢迎菜品-总消费", dmHaveDinnerDto.getMoney());
            map.put("统计时段", dmHaveDinnerDto.getWeekTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
