package com.crm.manager.controller;

import com.crm.manager.service.ControlPanelService;
import com.crm.manager.service.CustomInfoService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.constant.StatusConstants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.model.entity.user.info.*;
import com.crm.rely.backend.model.dto.statistics.ManagerStatisticsDto;
import com.crm.rely.backend.model.entity.custom.info.CustomSearchEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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;


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

    @Autowired
    private ControlPanelService controlPanelService;
    @Autowired
    private CustomInfoService customInfoService;

    /**
     * 销售统计
     * <p>
     * 今日客户量
     * 今日交易量
     * 今日存款数
     * 今日取款数
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/sale/statistics")
    public BaseResultDto saleStatistics(@RequestBody @Validated CustomSearchEntity entity,
                                        UserInfoEntity infoEntity) throws Exception {

        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
            entity.setAgentId(infoEntity.getId());
        }

        entity.setStamp(infoEntity.getStamp());

        if (!StatusConstants.PASSED_STATUS.equals(entity.getStatus())) {
            entity.setStatus(0);
        }
        return customInfoService.getPageList(entity);
    }

    /**
     * 管理员统计
     * <p>
     * 存款申请
     * 活动申请
     * 待核验用户
     * 待审批账户
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/statistics")
    public BaseResultDto managerStatistics(UserInfoEntity 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);
    }

}
