package cn.iocoder.yudao.module.member.service.customertraffic;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.customertraffic.vo.CustomerTrafficPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.customertraffic.vo.CustomerTrafficSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.customertraffic.CustomerTrafficDO;
import cn.iocoder.yudao.module.member.dal.dataobject.customertraffic.CustomerTrafficDetailDO;
import cn.iocoder.yudao.module.member.dal.mysql.customertraffic.CustomerTrafficMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.CUSTOMER_TRAFFIC_NOT_EXISTS;

/**
 * 顾客流量 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class CustomerTrafficServiceImpl implements CustomerTrafficService {

    @Resource
    private CustomerTrafficMapper customerTrafficMapper;
    private static  final  String ZC="ZC"; //早餐
    private static  final  String WC="WC"; //午餐
    private static  final  String W="W"; //晚餐
    @Override
    public Long createCustomerTraffic(CustomerTrafficSaveReqVO createReqVO) {
        // 插入
        CustomerTrafficDO customerTraffic = BeanUtils.toBean(createReqVO, CustomerTrafficDO.class);
        customerTrafficMapper.insert(customerTraffic);
        // 返回
        return customerTraffic.getId();
    }

    @Override
    public void updateCustomerTraffic(CustomerTrafficSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerTrafficExists(updateReqVO.getId());
        // 更新
        CustomerTrafficDO updateObj = BeanUtils.toBean(updateReqVO, CustomerTrafficDO.class);
        customerTrafficMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerTraffic(Long id) {
        // 校验存在
        validateCustomerTrafficExists(id);
        // 删除
        customerTrafficMapper.deleteById(id);
    }

    private void validateCustomerTrafficExists(Long id) {
        if (customerTrafficMapper.selectById(id) == null) {
            throw exception(CUSTOMER_TRAFFIC_NOT_EXISTS);
        }
    }

    @Override
    public CustomerTrafficDO getCustomerTraffic(Long id) {
        return customerTrafficMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerTrafficDO> getCustomerTrafficPage(CustomerTrafficPageReqVO pageReqVO) {
        return customerTrafficMapper.selectPage(pageReqVO);
    }

    @Override
    public void insertBatch(List<CustomerTrafficDO> list) {
        customerTrafficMapper.insertBatch(list);
    }

    @Override
    public List<CustomerTrafficDO> getData(Long storeId) {
        LocalDateTime now = LocalDateTime.now();
        List<CustomerTrafficDO> customerTrafficDOS = customerTrafficMapper.selectList(Wrappers.<CustomerTrafficDO>lambdaQuery()
                //.lt(CustomerTrafficDO::getCreateTime, now)
                .eq(storeId!=null,CustomerTrafficDO::getStoreId,storeId)
                .orderByDesc(CustomerTrafficDO::getCreateTime)
                .last("limit 7"));
        return customerTrafficDOS;
    }

    /**
     * @param storeId
     * @return
     * @Description: 半小时刻度，查询最新7个时间点的数据
     * @Author: qjq
     * @Date: 2024/4/19 下午2:39
     */
    @Override
    public List<CustomerTrafficDetailDO> getHalfHour(Long storeId, DateTime startDate,DateTime endDate) {
        if(ObjUtil.isEmpty(storeId)){
            DateTime end = DateUtil.endOfDay(DateUtil.date());
            DateTime begin = DateUtil.beginOfDay(end);
            List<CustomerTrafficDetailDO> list=new ArrayList<>();
            customerTrafficMapper.getAllCustomerNum(null,begin, end)
                    .stream()
                    .collect(Collectors.groupingBy(CustomerTrafficDetailDO::getStoreId))
                    .forEach((k,v)->{
                        BigDecimal reduce = v.stream()
                                .map(f -> BigDecimal.valueOf(f.getCustomerNum()))
                                .filter(ObjUtil::isNotNull)
                                .reduce(BigDecimal.ZERO, BigDecimal::add);
                        CustomerTrafficDetailDO aDo = BeanUtil.toBean(v.get(0), CustomerTrafficDetailDO.class);
                        aDo.setCustomerNum(reduce.intValue());
                        list.add(aDo);
                    });
            return   list;
        }else{
            List<CustomerTrafficDetailDO> all = customerTrafficMapper.getAllCustomerNum(storeId, startDate, endDate);
            all.sort(Comparator.comparing(CustomerTrafficDetailDO::getCreateTime));
            List<CustomerTrafficDetailDO> list=new ArrayList<>();
            //拆分
            List<List<CustomerTrafficDetailDO>> split = ListUtil.split(all, 3);
            for (List<CustomerTrafficDetailDO> dos : split) {
                CustomerTrafficDetailDO aDo = BeanUtil.toBean(dos.get(0), CustomerTrafficDetailDO.class);
                aDo.setCustomerNum(dos.stream().mapToInt(CustomerTrafficDetailDO::getCustomerNum).sum());
                list.add(aDo);
            }
            return list;
        }
    }
}