package com.yinet.bjtj.service.impl;

import com.yinet.bjtj.domain.FormBjtj;
import com.yinet.bjtj.mapper.FormBjtjMapper;
import com.yinet.bjtj.service.IFormBjtjService;
import com.yinet.common.core.text.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 办件统计Service业务层处理
 *
 * @author yinet
 * @date 2020-04-02
 */
@Service
public class FormBjtjServiceImpl implements IFormBjtjService {
    @Autowired
    private FormBjtjMapper formBjtjMapper;

    /**
     * 查询办件统计
     *
     * @param id 办件统计ID
     * @return 办件统计
     */
    @Override
    public FormBjtj selectFormBjtjById(Long id) {
        return formBjtjMapper.selectFormBjtjById(id);
    }

    /**
     * 查询办件统计列表
     *
     * @param formBjtj 办件统计
     * @return 办件统计
     */
    @Override
    public List<FormBjtj> selectFormBjtjList(FormBjtj formBjtj) {
        return formBjtjMapper.selectFormBjtjList(formBjtj);
    }

    /**
     * 新增办件统计
     *
     * @param formBjtj 办件统计
     * @return 结果
     */
    @Override
    public int insertFormBjtj(FormBjtj formBjtj) {
        formBjtj.setAddtime(new Date());
        return formBjtjMapper.insertFormBjtj(formBjtj);
    }

    /**
     * 修改办件统计
     *
     * @param formBjtj 办件统计
     * @return 结果
     */
    @Override
    public int updateFormBjtj(FormBjtj formBjtj) {
        return formBjtjMapper.updateFormBjtj(formBjtj);
    }

    /**
     * 删除办件统计对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFormBjtjByIds(String ids) {
        return formBjtjMapper.deleteFormBjtjByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除办件统计信息
     *
     * @param id 办件统计ID
     * @return 结果
     */
    @Override
    public int deleteFormBjtjById(Long id) {
        return formBjtjMapper.deleteFormBjtjById(id);
    }

    /**
     * 查询办件数据（前台）
     *
     * @return
     */
    @Override
    public Map selectTongjiList() {
        return formBjtjMapper.selectTongjiList();
    }

    @Override
    public Map<String, Object> selectFormBjtjVoCountByConditiond(Map<String, Object> map) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> selMap = new HashMap<String, Object>();
        String selObject = (String)map.get("selObject");
        if("month".equals(selObject)){//前两个月和当月数据统计
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); //设置时间格式
            Date currentDate = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentDate);
            calendar.add(Calendar.MONTH, -2);
            currentDate = calendar.getTime();
            String twoMonthBefore = sdf.format(currentDate);
            calendar.add(Calendar.MONTH, +1);
            currentDate = calendar.getTime();
            String oneMonthBefore = sdf.format(currentDate);
            calendar.add(Calendar.MONTH, +1);
            currentDate = calendar.getTime();
            String currentMonth = sdf.format(currentDate);
            selMap.put("selMonth",twoMonthBefore+"-01");
            String twoMonthBeforeCount =  formBjtjMapper.selectFormBjtjVoCountByConditiond(selMap);
            selMap.put("selMonth",oneMonthBefore+"-01");
            String oneMonthBeforeCount = formBjtjMapper.selectFormBjtjVoCountByConditiond(selMap);
            selMap.put("selMonth",currentMonth+"-01");
            String currentMonthCount = formBjtjMapper.selectFormBjtjVoCountByConditiond(selMap);
            int threecurrentMonthCount = Integer.parseInt(twoMonthBeforeCount) + Integer.parseInt(oneMonthBeforeCount) + Integer.parseInt(currentMonthCount);
            resultMap.put("twoMonthBeforeCount",twoMonthBeforeCount);
            resultMap.put("oneMonthBeforeCount",oneMonthBeforeCount);
            resultMap.put("currentMonthCount",currentMonthCount);
            resultMap.put("threecurrentMonthCount",threecurrentMonthCount);
            //上周数据每日统计
        }else if("week".equals(selObject)){
            String[] week = {"周一","周二","周三","周四","周五","周六","周日"};
            //设置时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal1 = Calendar.getInstance();
            int w = 0;
            if(cal1.get(Calendar.DAY_OF_WEEK) ==1){
                w=cal1.get(Calendar.DAY_OF_WEEK)+12;
                System.out.println(w+"qweqe");
            }else{
                w=cal1.get(Calendar.DAY_OF_WEEK) + 5;
                System.out.println(w+"qweqe");
            }
            System.out.println(w);
            int seven=0;
            for(int i = 0; i <7; i++){
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - w);
                System.out.println(cal);
                Date today = cal.getTime();
                String selDay = sdf.format(today);
                System.out.println(selDay);
                selMap.put("selDay",selDay);
                String thisCount = formBjtjMapper.selectFormBjtjVoCountByConditiond(selMap);
                if(thisCount==null){
                    thisCount="0";
                }
                seven+=Integer.parseInt(thisCount);
                resultMap.put(week[i],thisCount);
                w--;
            }
            resultMap.put("seven",seven);
            System.err.println("seven:"+seven);
        }
        return resultMap;
    }
    @Override
    public List<Map<String, Integer>> selectFormBjtjByMonthes() {
        return formBjtjMapper.selectFormBjtjByMonthes();
    }
}
