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

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.statement.domain.DmEvaluate;
import me.zhengjie.modules.dm.statement.repository.DmEvaluateRepository;
import me.zhengjie.modules.dm.statement.service.DmEvaluateService;
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.mapstruct.DmEvaluateMapper;
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.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@RequiredArgsConstructor
public class DmEvaluateServiceImpl implements DmEvaluateService {
    private final DmEvaluateRepository dmEvaluateRepository;
    private final DmEvaluateMapper dmEvaluateMapper;

    @Override
    public Map<String, Object> queryAll(DmEvaluateQueryCriteria criteria, Pageable pageable) {
        Page<DmEvaluate> page = dmEvaluateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dmEvaluateMapper::toDto));
    }

    @Override
    public List<DmEvaluateDto> queryAll(DmEvaluateQueryCriteria criteria) {
        return dmEvaluateMapper.toDto(dmEvaluateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public Map<String, Object> selectServicePj(String stateDate, String endDate, String type, Pageable pageable) {
        Page<DmEvaluate> 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 = dmEvaluateRepository.selectYearServicePj(year.format(cal),year1.format(calendar.getTime()),pageable);
            }else {
                page = dmEvaluateRepository.selectYearServicePj(stateDate,endDate,pageable);
            }
        }else if (type.equals("month")){
            if (stateDate==null && endDate ==null){
                calendar.setTime(cal);
                calendar.add(Calendar.YEAR,-1);
                page = dmEvaluateRepository.selectMonthServicePj(year.format(calendar.getTime()),year.format(cal),pageable);
            }else {
                page = dmEvaluateRepository.selectMonthServicePj(stateDate,endDate,pageable);
            }
        }else {
            calendar.setTime(cal);
            calendar.add(Calendar.YEAR,-1);
            page = dmEvaluateRepository.selectMonthServicePj(year.format(calendar.getTime()),year.format(cal),pageable);
        }
        return PageUtil.toPage(page.map(dmEvaluateMapper::toDto));
    }

    @Override
    public void download(List<DmEvaluateDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmEvaluateDto dmEvaluate : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("味道", dmEvaluate.getWdPoint());
            map.put("品种", dmEvaluate.getPzPoint());
            map.put("分量", dmEvaluate.getFlPoint());
            map.put("服务态度", dmEvaluate.getFwtdPoint());
            map.put("环境卫生", dmEvaluate.getHjwsPoint());
            map.put("综合服务", dmEvaluate.getZhfwPoint());
            map.put("持续改进", dmEvaluate.getCxgjPoint());
            map.put("综合评分", dmEvaluate.getTotalPoint());
            map.put("统计时段", dmEvaluate.getPjTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
