
package com.yami.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.MyOrderDto;
import com.yami.shop.bean.app.dto.MyOrderItemDto;
import com.yami.shop.bean.model.Order;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.OrderMapper;
import com.yami.shop.service.MyOrderService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author lgh on 2018/09/15.
 */
@Service
@AllArgsConstructor
public class MyOrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements MyOrderService {

    private final OrderMapper orderMapper;

    @Override
    public IPage<MyOrderDto> pageMyOrderByUserIdAndStatus(Page<MyOrderDto> page, String userId, Integer status,Integer dvyType) {
        List<MyOrderDto> myOrderDtos = orderMapper.listMyOrderByUserIdAndStatus(new PageAdapter(page), userId, status,dvyType);
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日$HH:mm");
        for (MyOrderDto myOrderDto : myOrderDtos) {
            if (StrUtil.isBlank(myOrderDto.getShopName())) {
                myOrderDto.setShopName(Constant.PLATFORM_SHOP_NAME);
            }
//            List<MyOrderItemDto> orderItemDtos = myOrderDto.getOrderItemDtos();
//            int score = orderItemDtos.stream().mapToInt(MyOrderItemDto::getUseScore).sum();
            if(null!=myOrderDto.getOffsetPoints()){
                myOrderDto.setUserScore(Math.toIntExact(myOrderDto.getOffsetPoints()));
            }
            if (null != myOrderDto.getPayTime()) {
                // 转换为 LocalDateTime 并增加 1 小时
                LocalDateTime delayedTime = myOrderDto.getPayTime().toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime()
                        .plusHours(1);
                // 转换回 Date 类型
                Date delayedDate = Date.from(delayedTime.atZone(ZoneId.systemDefault()).toInstant());

                // 格式化延迟后的时间
                String format = sdf.format(delayedDate);
                String[] split = format.split("\\$");
                myOrderDto.setEstimatedTimeStr(split[0]);
                myOrderDto.setEstimatedTime(split[1]);
            }
        }
        page.setRecords(myOrderDtos);
        page.setTotal(orderMapper.countMyOrderByUserIdAndStatus(userId, status));
        return page;
    }

    @Override
    public IPage<MyOrderDto> pageMyOrderByParams(Page<MyOrderDto> page, String userId, Integer status,
                                                 String orderName, Integer orderTimeStatus, Integer orderType, String orderNumber) {
        String preTime = "";
        if (Objects.equals(1, orderTimeStatus)) {
            preTime = getStatetime(7);
        }
        if (Objects.equals(2, orderTimeStatus) || Objects.equals(3, orderTimeStatus)) {
            preTime = getReduceMonth(30);
        }
        List<MyOrderDto> myOrderDtoList = orderMapper.listMyOrderByParams(new PageAdapter(page), userId, status, orderName, orderTimeStatus, preTime, orderType, orderNumber);
        for (MyOrderDto myOrderDto : myOrderDtoList) {
            if (Objects.equals(myOrderDto.getShopId(), Constant.PLATFORM_SHOP_ID)) {
                myOrderDto.setShopName(Constant.PLATFORM_SHOP_NAME);
            }
            List<MyOrderItemDto> orderItemDtos = myOrderDto.getOrderItemDtos();
            int score = orderItemDtos.stream().mapToInt(MyOrderItemDto::getUseScore).sum();
            myOrderDto.setUserScore(score);
        }
        page.setRecords(myOrderDtoList);
        page.setTotal(orderMapper.countMyOrderByParams(userId, status, orderName, orderTimeStatus, preTime, orderType, orderNumber));
        return page;
    }

    @Override
    public IPage<MyOrderDto> myOrderComment(PageParam<MyOrderDto> page, String userId, Integer isComm) {
        List<MyOrderDto> myOrderDtos = orderMapper.OrderCommentByUserIdAndStatus(new PageAdapter(page), userId, isComm);
        for (MyOrderDto myOrderDto : myOrderDtos) {
            if (StrUtil.isBlank(myOrderDto.getShopName())) {
                myOrderDto.setShopName(Constant.PLATFORM_SHOP_NAME);
            }
        }
        page.setRecords(myOrderDtos);
        page.setTotal(orderMapper.countMyOrderByUserIdAndStatus(userId, 0));
        return page;
    }

    @Override
    public IPage<MyOrderItemDto> myOrderItemsComment(PageParam<MyOrderItemDto> page, String userId, Integer isComm) {
        List<MyOrderItemDto> myOrderDtos = orderMapper.orderItemCommentByUserIdAndStatus(new PageAdapter(page), userId, isComm);
        page.setRecords(myOrderDtos);
        page.setTotal(orderMapper.countOrderItemComment(userId, isComm));
        return page;
    }

    /**
     * 获取当前时间前7天的时间点
     */
    private String getStatetime(int reduce) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        // reduce = -7 前7天
        c.add(Calendar.DATE, -reduce);
        Date monday = c.getTime();
        String preMonday = sdf.format(monday);
        return preMonday;
    }

    /**
     * 获取当前时间 前三个月的时间点
     */
    private String getReduceMonth(int reduce) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        // reduce = -7 前7天
        c.add(Calendar.MONTH, -reduce);
        Date month = c.getTime();
        String priMonth = sdf.format(month);
        return priMonth;
    }
}
