package com.yinet.system.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yinet.system.mapper.BjlbMapper;
import com.yinet.system.domain.Bjlb;
import com.yinet.system.service.IBjlbService;
import com.yinet.common.core.text.Convert;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author yinet
 * @date 2020-04-22
 */
@Service
public class BjlbServiceImpl implements IBjlbService
{
    @Autowired
    private BjlbMapper bjlbMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public Bjlb selectBjlbById(Long id)
    {
        return bjlbMapper.selectBjlbById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param bjlb 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Bjlb> selectBjlbList(Bjlb bjlb)
    {
        return bjlbMapper.selectBjlbList(bjlb);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param bjlb 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertBjlb(Bjlb bjlb)
    {
        return bjlbMapper.insertBjlb(bjlb);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param bjlb 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateBjlb(Bjlb bjlb)
    {
        return bjlbMapper.updateBjlb(bjlb);
    }

    /**
     * 删除【请填写功能名称】对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBjlbByIds(String ids)
    {
        return bjlbMapper.deleteBjlbByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    public int deleteBjlbById(Long id)
    {
        return bjlbMapper.deleteBjlbById(id);
    }


    @Override
    public Map<String, Object> selectBjlbVoCountByConditiond(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");
            int twoMonthBeforeCount = bjlbMapper.selectBjlbVoCountByConditiond(selMap);
            selMap.put("selMonth",oneMonthBefore+"-01");
            int oneMonthBeforeCount = bjlbMapper.selectBjlbVoCountByConditiond(selMap);
            selMap.put("selMonth",currentMonth+"-01");
            int currentMonthCount = bjlbMapper.selectBjlbVoCountByConditiond(selMap);
            resultMap.put("twoMonthBeforeCount",twoMonthBeforeCount);
            resultMap.put("oneMonthBeforeCount",oneMonthBeforeCount);
            resultMap.put("currentMonthCount",currentMonthCount);
        }else if("week".equals(selObject)){//上周数据每日统计
            String[] week = {"周一","周二","周三","周四","周五","周六","周日"};
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
            Calendar cal1 = Calendar.getInstance();
            int w = cal1.get(Calendar.DAY_OF_WEEK) + 5;
            for(int i = 0; i < 7; i++){
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - w);
                Date today = cal.getTime();
                String selDay = sdf.format(today);
                selMap.put("selDay",selDay);
                int thisCount = bjlbMapper.selectBjlbVoCountByConditiond(selMap);
                resultMap.put(week[i],thisCount);
                w--;
            }
        }else {//查询全部
            List<Bjlb> list = bjlbMapper.selectBjlbVoByConditiond(selMap);
            resultMap.put("list",list);
        }
        return resultMap;
    }

}
