package com.crm.manager.controller;

import com.crm.manager.service.ControlPanelService;
import com.crm.manager.service.CustomNewsService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.control.panel.statistics.*;
import com.crm.rely.backend.core.entity.base.PageEntity;
import com.crm.rely.backend.core.entity.control.panel.statistics.ControlPanelStatisticsEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.custom.news.CustomNewsSearchEntity;
import com.crm.rely.backend.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


@RestController
@RequestMapping("/control/panel")
public class ControlPanelController {

    @Autowired
    private ControlPanelService controlPanelService;

    @Autowired
    private CustomNewsService customNewsService;

    /**
     * 客户动态
     *
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/custom/dynamic")
    public BaseResultDto customDynamic(InfoEntity infoEntity) throws Exception {
        CustomNewsSearchEntity entity = new CustomNewsSearchEntity();
        PageEntity page = new PageEntity();
        page.setCurrent(1);
        page.setRow(5);
        entity.setPage(page);
//        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            entity.setStamp(infoEntity.getIbInfo().getStamp());
//        }else {
//            entity.setStamp(null);
//        }
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        } else {
            entity.setStamp(null);
        }
        if (entity.getStartDate() != null) {
            entity.setStartDate(DateUtil.parseDate(entity.getStartDate()));
        }
        if (entity.getEndDate() != null) {
            Date endDate = DateUtil.operationDay(entity.getEndDate(), 1);
            entity.setEndDate(DateUtil.parseDate(endDate));
        }
        int dId;
        if (infoEntity.getIbInfo().getDepartmentId() == null) {
            dId = -1;
        } else {
            dId = infoEntity.getIbInfo().getDepartmentId().intValue();
        }
        List<Integer> types = new ArrayList<>(7);
        switch (dId) {
            case 1:
            case 4:
                types.add(1);
                types.add(4);
                types.add(3);
                types.add(5);
                types.add(7);
                types.add(6);
                break;
            case 2:
                types.add(6);
                types.add(3);
                types.add(4);
                break;
            case 3:
                types.add(7);
                break;
            case 5:
                types = null;
                break;
            default:
                if (PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
                    types = null;
                } else {

                    types.add(-1);
                }
                break;
        }
        entity.setTypes(types);
        List<Integer> status = new ArrayList<>(2);
        status.add(1);
        status.add(2);
        entity.setStatus(status);
        return customNewsService.searchPageList(entity);
    }

    /**
     * 财务存取款 内转 佣金统计
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/finance/statistics")
    public BaseResultDto financeStatistics(InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_FINANCE.equals(infoEntity.getPrefix()) || !PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        List<FinanceStatisticsDto> financeStatisticsDtos = controlPanelService.financeStatistics();
        return BaseResultDto.success(financeStatisticsDtos);
    }

    /**
     * 销售统计
     * <p>
     * 今日客户量
     * 今日交易量
     * 今日存款数
     * 今日取款数
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/sale/statistics")
    public BaseResultDto saleStatistics(InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix()) || !PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        SaleStatisticsDto saleStatisticsDto = controlPanelService.saleStatistics();
        return BaseResultDto.success(saleStatisticsDto);
    }

    /**
     * 法务统计
     * <p>
     * 用户数
     * 未激活客户数
     * 待核验用户
     * 待审批账户
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/legal/affairs/statistics")
    public BaseResultDto legalAffairsStatistics(InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_LEGAL_AFFAIRS.equals(infoEntity.getPrefix()) || !PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        LegalAffairsStatisticsDto legalAffairsStatisticsDto = controlPanelService.legalAffairsStatistics();
        return BaseResultDto.success(legalAffairsStatisticsDto);
    }

    /**
     * 市场统计
     * 今日新增注册
     * 今日新增模拟
     * 客户活动参与
     * 代理活动参与
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/market/statistics")
    public BaseResultDto marketStatistics(InfoEntity infoEntity) throws Exception {

//        if (!PrefixEnum.PREFIX_MARKET.equals(infoEntity.getPrefix()) || !PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        MarketStatisticsDto marketStatisticsDto = controlPanelService.marketStatistics();
        return BaseResultDto.success(marketStatisticsDto);
    }

    /**
     * 管理员统计
     * <p>
     * 存款申请
     * 活动申请
     * 待核验用户
     * 待审批账户
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/statistics")
    public BaseResultDto managerStatistics(InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_ADMIN.equals(infoEntity.getPrefix()) || !PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }

        ManagerStatisticsDto statisticsDto = controlPanelService.managerStatistics();
        return BaseResultDto.success(statisticsDto);
    }

    /**
     * 财务存取款待处理数量
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/finance/balance/pending")
    public BaseResultDto financeBalancePending(InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_FINANCE.equals(infoEntity.getPrefix()) || !PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        FinanceBalancePendingStatisticsDto statisticsDto = controlPanelService.financeBalancePendingStatistics();
        return BaseResultDto.success(statisticsDto);
    }

    /**
     * 财务 根据时间 存取款 内转 外佣数量
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/finance/date/balance")
    public BaseResultDto financeDateBalance(@RequestBody ControlPanelStatisticsEntity entity, InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_FINANCE.equals(infoEntity.getPrefix()) || !PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        FinanceStatisticsDto financeStatisticsDto = controlPanelService.financeStatistics(entity);
        return BaseResultDto.success(financeStatisticsDto);
    }


}
