package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.ControlPanelStatisticsMapper;
import com.crm.manager.dao.repository.ControlPanelStatisticsRepository;
import com.crm.manager.service.ControlPanelService;
import com.crm.rely.backend.core.dto.control.panel.statistics.*;
import com.crm.rely.backend.core.entity.control.panel.statistics.ControlPanelStatisticsEntity;
import com.crm.rely.backend.core.pojo.table.ControlPanelStatisticsTable;
import com.crm.rely.backend.core.pojo.view.*;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: houn
 * @Date: 2020/8/13 13:55
 * @Description:
 */
@Service
public class ControlPanelServiceImpl implements ControlPanelService {

    @Autowired
    private ControlPanelStatisticsRepository controlPanelStatisticsRepository;

    @Autowired
    private ControlPanelStatisticsMapper controlPanelStatisticsMapper;

    @Scheduled(fixedDelay = 1000 * 60 * 60)
    @Transactional(rollbackFor = Exception.class)
    public synchronized void scheduleStatisticsTask() throws ServiceException {

        Date endTime = DateUtil.parseDate(new Date());

        Date date = DateUtil.operationDay(endTime, -1);

        boolean bl = controlPanelStatisticsRepository.existsByDate(date);
        if (!bl) {
            ControlPanelStatisticsEntity entity = new ControlPanelStatisticsEntity();
            entity.setStartDate(date);
            entity.setEndDate(endTime);
            ControlPanelStatisticsView controlPanelStatisticsView = controlPanelStatisticsMapper.searchByDate(entity);
            ControlPanelStatisticsTable controlPanelStatisticsTable = new ControlPanelStatisticsTable();

            BeanUtils.copyProperties(controlPanelStatisticsView, controlPanelStatisticsTable);
            controlPanelStatisticsTable.setDate(date);
            controlPanelStatisticsTable.setAddIp("127.0.0.1");
            controlPanelStatisticsTable.setAddUser(-1L);
            controlPanelStatisticsTable.setAddTime(new Date());
            controlPanelStatisticsRepository.save(controlPanelStatisticsTable);
        }
    }

    @Override
    public List<FinanceStatisticsDto> financeStatistics() throws ServiceException {

        Date date = DateUtil.parseDate(new Date());

        Date endTime = DateUtil.operationDay(date, 1);

        ControlPanelStatisticsEntity entity = new ControlPanelStatisticsEntity();
        entity.setStartDate(date);
        entity.setEndDate(endTime);
        FinanceStatisticsDto statisticsDto = financeStatistics(entity);

        endTime = date;
        date = DateUtil.operationDay(date, -6);
        entity.setStartDate(date);
        entity.setEndDate(endTime);
        List<ControlPanelStatisticsView> controlPanelStatisticsViews = controlPanelStatisticsMapper.searchListByDate(entity);

        List<FinanceStatisticsDto> financeStatisticsDtos = new ArrayList<>(7);
        controlPanelStatisticsViews.forEach(v -> {
            FinanceStatisticsDto financeStatisticsDto = new FinanceStatisticsDto();
            BeanUtils.copyProperties(v, financeStatisticsDto);
            financeStatisticsDtos.add(financeStatisticsDto);
        });
        financeStatisticsDtos.add(statisticsDto);
        return financeStatisticsDtos;
    }

    @Override
    public FinanceStatisticsDto financeStatistics(ControlPanelStatisticsEntity entity) throws ServiceException {

        ControlPanelStatisticsView controlPanelStatisticsView = controlPanelStatisticsMapper.searchByDate(entity);

        FinanceStatisticsDto financeStatisticsDto = new FinanceStatisticsDto();
        BeanUtils.copyProperties(controlPanelStatisticsView, financeStatisticsDto);

        return financeStatisticsDto;
    }

    @Override
    public FinanceBalancePendingStatisticsDto financeBalancePendingStatistics() throws ServiceException {
        ControlPanelFinanceBalancePendingNUmberView controlPanelFinanceBalancePendingNUmberView = controlPanelStatisticsMapper.searchFinanceBalancePendingNumber();

        FinanceBalancePendingStatisticsDto managerStatisticsDto = new FinanceBalancePendingStatisticsDto();
        BeanUtils.copyProperties(controlPanelFinanceBalancePendingNUmberView, managerStatisticsDto);
        return managerStatisticsDto;
    }

    @Override
    public ManagerStatisticsDto managerStatistics() throws ServiceException {

        ControlPanelManagerApplyNumberView controlPanelApplyNumberView = controlPanelStatisticsMapper.searchManagerApplyNumber();

        ManagerStatisticsDto managerStatisticsDto = new ManagerStatisticsDto();
        BeanUtils.copyProperties(controlPanelApplyNumberView, managerStatisticsDto);
        return managerStatisticsDto;
    }

    @Override
    public LegalAffairsStatisticsDto legalAffairsStatistics() throws ServiceException {
        ControlPanelCustomNumberView controlPanelCustomNumberView = controlPanelStatisticsMapper.searchCustomNumber();

        LegalAffairsStatisticsDto legalAffairsStatisticsDto = new LegalAffairsStatisticsDto();
        BeanUtils.copyProperties(controlPanelCustomNumberView, legalAffairsStatisticsDto);
        return legalAffairsStatisticsDto;
    }

    @Override
    public MarketStatisticsDto marketStatistics() throws ServiceException {
        Date date = DateUtil.parseDate(new Date());

        Date endTime = DateUtil.operationDay(date, 1);

        ControlPanelStatisticsEntity entity = new ControlPanelStatisticsEntity();
        entity.setStartDate(date);
        entity.setEndDate(endTime);
        ControlPanelManagerNumberView controlPanelManagerNumberView = controlPanelStatisticsMapper.searchManagerByData(entity);

        MarketStatisticsDto marketStatisticsDto = new MarketStatisticsDto();
        BeanUtils.copyProperties(controlPanelManagerNumberView, marketStatisticsDto);
        return marketStatisticsDto;
    }

    @Override
    public SaleStatisticsDto saleStatistics() throws ServiceException {
        return null;
    }
}
