package com.jojubanking.boot.module.system.service.usecount;

import com.jojubanking.boot.module.system.dal.dataobject.usecount.UseCountToDO;
import com.jojubanking.boot.module.system.dal.dataobject.usecount.UseCountToDateDO;
import com.jojubanking.boot.module.system.dal.mysql.usecount.UseCountXMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.text.SimpleDateFormat;
import java.util.*;
import com.jojubanking.boot.module.system.controller.admin.usecount.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.usecount.UseCountDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.usecount.UseCountConvert;
import com.jojubanking.boot.module.system.dal.mysql.usecount.UseCountMapper;

import static com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jojubanking.boot.module.system.enums.ErrorCodeConstants.*;

/**
 * 统计 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class UseCountServiceImpl implements UseCountService {

    @Resource
    private UseCountMapper useCountMapper;

    @Resource
    private UseCountXMapper useCountXMapper;
    @Override
    public Integer createUseCount(UseCountCreateReqVO createReqVO) {
        // 插入
        UseCountDO useCount = UseCountConvert.INSTANCE.convert(createReqVO);
        useCountMapper.insert(useCount);
        // 返回
        return useCount.getId();
    }

    @Override
    public void updateUseCount(UseCountUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateUseCountExists(updateReqVO.getId());
        // 更新
        UseCountDO updateObj = UseCountConvert.INSTANCE.convert(updateReqVO);
        useCountMapper.updateById(updateObj);
    }

    @Override
    public void deleteUseCount(Integer id) {
        // 校验存在
        this.validateUseCountExists(id);
        // 删除
        useCountMapper.deleteById(id);
    }

    private void validateUseCountExists(Integer id) {
        if (useCountMapper.selectById(id) == null) {
            throw exception(USE_COUNT_NOT_EXISTS);
        }
    }

    @Override
    public UseCountDO getUseCount(Integer id) {
        return useCountMapper.selectById(id);
    }

    @Override
    public List<UseCountDO> getUseCountList(Collection<Integer> ids) {
        return useCountMapper.selectBatchIds(ids);
    }
    @Override
    public List<UseCountToDO> getUseCountTo() {
        return useCountXMapper.selectCountTo();
    }
    @Override
    public List<UseCountToDO> getUseCountTo1() {
        return useCountXMapper.selectCountTo1();
    }
    @Override
    public List<UseCountToDO> getUseCountTo2() {
        return useCountXMapper.selectCountTo2();
    }
    @Override
    public List<UseCountToDO> getUseCountTo3() {
        return useCountXMapper.selectCountTo3();
    }
    @Override
    public List<UseCountToDO> getUseCountTo4() {
        return useCountXMapper.selectCountTo4();
    }
    @Override
    public List<UseCountToDO> getUseCountTo5() {
        return useCountXMapper.selectCountTo5();
    }
    @Override
    public List<UseCountToDO> getUseCountTo6() {
        return useCountXMapper.selectCountTo6();
    }
    @Override
    public List<UseCountToDO> getUseCountToSection(Date startDate, Date endDate) {
        return useCountXMapper.selectCountToSection(startDate, endDate);
    }
    @Override
    public List<UseCountToDateDO> getUseCountToDate() {
        List<UseCountToDateDO> lst = new ArrayList<>();
        UseCountToDateDO dateDO = new UseCountToDateDO();

        //指定日期格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //日历对象
        Calendar calendar = Calendar.getInstance();
        //获取当前日期
        calendar.setTime(new Date());
        System.out.println("当前时间：" + simpleDateFormat.format(calendar.getTime()));
        dateDO.setDate(simpleDateFormat.format(calendar.getTime()));

        //当前时间减去一天，即一天前的时间
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        String day = simpleDateFormat.format(calendar.getTime());
        System.out.println("昨天日期：" + day);
        dateDO.setDate1(simpleDateFormat.format(calendar.getTime()));

        calendar.add(Calendar.DAY_OF_MONTH,-1);
        day = simpleDateFormat.format(calendar.getTime());
        System.out.println("昨天日期：" + day);
        dateDO.setDate2(simpleDateFormat.format(calendar.getTime()));

        calendar.add(Calendar.DAY_OF_MONTH,-1);
        day = simpleDateFormat.format(calendar.getTime());
        System.out.println("昨天日期：" + day);
        dateDO.setDate3(simpleDateFormat.format(calendar.getTime()));

        calendar.add(Calendar.DAY_OF_MONTH,-1);
        day = simpleDateFormat.format(calendar.getTime());
        System.out.println("昨天日期：" + day);
        dateDO.setDate4(simpleDateFormat.format(calendar.getTime()));

        calendar.add(Calendar.DAY_OF_MONTH,-1);
        day = simpleDateFormat.format(calendar.getTime());
        System.out.println("昨天日期：" + day);
        dateDO.setDate5(simpleDateFormat.format(calendar.getTime()));

        calendar.add(Calendar.DAY_OF_MONTH,-1);
        day = simpleDateFormat.format(calendar.getTime());
        System.out.println("昨天日期：" + day);
        dateDO.setDate6(simpleDateFormat.format(calendar.getTime()));

        lst.add(dateDO);

        return lst;
    }
    @Override
    public PageResult<UseCountDO> getUseCountPage(UseCountPageReqVO pageReqVO) {
        return useCountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UseCountDO> getUseCountList(UseCountExportReqVO exportReqVO) {
        return useCountMapper.selectList(exportReqVO);
    }

}
