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

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.order.domain.DmOrderRecord;
import me.zhengjie.modules.dm.order.repository.DmOrderRecordRepository;
import me.zhengjie.modules.dm.order.service.dto.DmOrderRecordDto;
import me.zhengjie.modules.dm.order.service.mapstruct.DmOrderRecordMapper;
import me.zhengjie.modules.dm.service.domain.DmServicePj;
import me.zhengjie.modules.dm.service.repository.DmServicePjRepository;
import me.zhengjie.modules.dm.service.service.mapstruct.DmServicePjMapper;
import me.zhengjie.modules.dm.statement.domain.DmStatement;
import me.zhengjie.modules.dm.statement.repository.DmStatementRepository;
import me.zhengjie.modules.dm.statement.service.DmStatementService;
import me.zhengjie.modules.dm.statement.service.dto.DmEvaluateDto;
import me.zhengjie.modules.dm.statement.service.dto.DmEvaluateQueryCriteria;
import me.zhengjie.modules.dm.statement.service.dto.DmStatementDto;
import me.zhengjie.modules.dm.statement.service.dto.DmStatementQueryCriteria;
import me.zhengjie.modules.dm.statement.service.mapstruct.DmStatementMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@RequiredArgsConstructor
public class DmStatementServiceImpl implements DmStatementService {
    private final DmStatementRepository dmStatementRepository;
    private final DmStatementMapper dmStatementMapper;

    @Override
    public Map<String, Object> selectOrderRecord(String stateDate, String endDate, String type, Pageable pageable) {
        Page<DmStatement> 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,-2);
                page = dmStatementRepository.selectYearStatement(year1.format(calendar.getTime()),year.format(cal),pageable);
            }else {
                page = dmStatementRepository.selectYearStatement(stateDate,endDate,pageable);
            }
        }else if (type.equals("month")){
            if (stateDate==null || endDate ==null){
                calendar.setTime(cal);
                calendar.add(Calendar.YEAR,-1);
                page = dmStatementRepository.selectMonthStatement(year.format(calendar.getTime()),year.format(cal),pageable);
            }else {
                page = dmStatementRepository.selectMonthStatement(stateDate,endDate,pageable);
            }
        }else {
            calendar.setTime(cal);
            calendar.add(Calendar.YEAR,-1);
            page= dmStatementRepository.selectMonthStatement(year.format(calendar.getTime()),year.format(cal),pageable);
        }
        return PageUtil.toPage(page.map(dmStatementMapper::toDto));
    }

    @Override
    public void download(List<DmStatementDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmStatementDto dmStatementDto : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("组织名称", dmStatementDto.getName());
            map.put("消费金额", dmStatementDto.getMoney());
            map.put("人均消费", dmStatementDto.getAvgMoney());
            map.put("统计时段", dmStatementDto.getOrderTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<DmStatementDto> queryAll(DmStatementQueryCriteria criteria) {
        return dmStatementMapper.toDto(dmStatementRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
}
