package com.xy.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.xy.annotate.RestMappingController;
import com.xy.dto.DeviceDataDto;
import com.xy.service.DeviceDataServiceImpl;
import com.xy.utils.MercAuthUtils;
import com.xy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/***
 * Desc:商户小程序设备订单统计
 * @author 谭斌
 * @date 2023/3/17 10:30
 */
@RestMappingController("merc-mini/device/order")
@RequiredArgsConstructor
@Api(tags = "商户小程序设备订单统计")
public class MercMiniOrderController {

    private final DeviceDataServiceImpl deviceDataService;


    @ApiOperation("订单列表页统计-作废，转移到分页接口直接返回，因为要根据各种条件筛选统计")
    @PostMapping("count")
    public R<DeviceDataDto.MercOrdersDayCountVO> mercHomeStatistical(@RequestBody @Valid DeviceDataDto.MercOrdersDayCountDTO dto) {
        DeviceDataDto.MercOrdersDayCountVO vo = new DeviceDataDto.MercOrdersDayCountVO().setOrderNum(0).setOrderTotalAmount(0);
        String dateValue = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
        Long mercId = MercAuthUtils.getMercId();
        DateTime orderDate = dto.getOrderDate();
        if (orderDate != null) {
            dateValue = DateUtil.format(orderDate, DatePattern.PURE_DATE_PATTERN);
        }

        List<DeviceDataDto.Vo> mercDataOneDay = deviceDataService.getMercDataOneDay(mercId, dateValue);
        if (CollUtil.isEmpty(mercDataOneDay)) {
            return R.ok(vo);
        }
        vo.setOrderTotalAmount(mercDataOneDay.stream().mapToInt(DeviceDataDto.Vo::getSalesMoney).sum());
        int sumSalesCount = mercDataOneDay.stream().mapToInt(DeviceDataDto.Vo::getSalesCount).sum();
        int sumRiskCount = mercDataOneDay.stream().mapToInt(DeviceDataDto.Vo::getRiskCount).sum();
        int sumZeroCount = mercDataOneDay.stream().mapToInt(DeviceDataDto.Vo::getZeroCount).sum();
        vo.setOrderNum(sumSalesCount + sumRiskCount + sumZeroCount);
        return R.ok(vo);
    }


}
