package com.ym.mec.web.controller.education;


import com.ym.mec.biz.dal.dao.SporadicChargeInfoDao;
import com.ym.mec.biz.dal.dao.StudentPaymentOrderDao;
import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.dto.LuckStatisDto;
import com.ym.mec.biz.dal.dto.OrderStatisDto;
import com.ym.mec.biz.dal.dto.OrganMaxAmountDto;
import com.ym.mec.biz.dal.entity.Organization;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RequestMapping("activity")
@Api(tags = "活动")
@RestController
public class ActivityController extends BaseController {
    @Autowired
    private StudentPaymentOrderDao studentPaymentOrderDao;
    @Autowired
    private SporadicChargeInfoDao sporadicChargeInfoDao;
    @Autowired
    private StudentRegistrationDao studentRegistrationDao;

    @ApiOperation(value = "分部双11活动统计")
    @GetMapping("/doubleEleven2020Statis")
    public HttpResponseResult<LuckStatisDto> doubleEleven2020Statis() {
        //获取所有参与分部
        List<Organization> organs = sporadicChargeInfoDao.getActiveOrgans(12);
        //获取所有分部参与的订单
        List<OrderStatisDto> orders = studentPaymentOrderDao.doubleEleven2020Statis(organs);
        Set<Integer> orderOrganIds = orders.stream().map(OrderStatisDto::getOrganId).collect(Collectors.toSet());
        //获取所有分部预计的人数
        List<OrderStatisDto> organNormalStudents = studentRegistrationDao.getOrganNormalStudent();
        //获取分部的目标金额
        List<OrganMaxAmountDto> maxAmount4Organs = sporadicChargeInfoDao.getActiveOrgansMaxAmount(12);

        for (Organization organ : organs) {
            if (!orderOrganIds.contains(organ.getId())) {
                OrderStatisDto orderStatisDto = new OrderStatisDto();
                orderStatisDto.setOrganId(organ.getId());
                orderStatisDto.setOrganName(organ.getName());
                orders.add(orderStatisDto);
            }
        }
        BigDecimal totalMoney = BigDecimal.ZERO;
        Integer totalEstimatedNums = 0;
        Integer totalNum = 0;
        Integer totalTimes = 0;
        BigDecimal totalMaxAmount = BigDecimal.ZERO;
        for (OrderStatisDto order : orders) {
            for (OrganMaxAmountDto maxAmount4Organ : maxAmount4Organs) {
                if (order.getOrganId().equals(maxAmount4Organ.getId())) {
                    order.setMaxAmount(maxAmount4Organ.getMaxAmount() == null ? BigDecimal.ZERO.setScale(2,RoundingMode.HALF_UP) : maxAmount4Organ.getMaxAmount().setScale(2,RoundingMode.HALF_UP));
                    break;
                }
            }

            for (OrderStatisDto organNormalStudent : organNormalStudents) {
                if (order.getOrganId().equals(organNormalStudent.getOrganId())) {
                    order.setEstimatedNums(organNormalStudent.getEstimatedNums());
                    break;
                }
            }
            totalEstimatedNums += order.getEstimatedNums();
            totalNum += order.getNums();
            totalTimes += order.getTimes();
            totalMoney = totalMoney.add(order.getMoney());
            totalMaxAmount = totalMaxAmount.add(order.getMaxAmount());
            if (order.getMaxAmount().compareTo(BigDecimal.ZERO) <= 0) {
                order.setScale(new BigDecimal("100.00"));
            } else {
                order.setScale(order.getMoney().multiply(new BigDecimal(100)).divide(order.getMaxAmount(), 2, BigDecimal.ROUND_HALF_UP));
            }
        }
        orders.sort(Comparator.comparing(OrderStatisDto::getScale).reversed());

        LuckStatisDto luckStatisDto = new LuckStatisDto();
        luckStatisDto.setOrderStatisDtoList(orders);
        luckStatisDto.setTotalEstimatedNums(totalEstimatedNums);
        luckStatisDto.setTotalNum(totalNum);
        luckStatisDto.setTotalTimes(totalTimes);
        luckStatisDto.setTotalMoney(totalMoney);
        luckStatisDto.setTotalMaxAmount(totalMaxAmount);
        if (luckStatisDto.getTotalMaxAmount().compareTo(BigDecimal.ZERO) <= 0) {
            totalMaxAmount = BigDecimal.ONE;
        }
        luckStatisDto.setScale(totalMoney.multiply(new BigDecimal(100)).divide(totalMaxAmount, 2, BigDecimal.ROUND_HALF_UP));
        return succeed(luckStatisDto);
    }
}
