package com.ghy.order.service.impl;

import com.ghy.common.constant.UserConstants;
import com.ghy.common.core.text.Convert;
import com.ghy.order.domain.OrderDetail;
import com.ghy.order.mapper.OrderDetailMapper;
import com.ghy.order.service.OrderDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import static java.time.temporal.ChronoField.*;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;

@Service
public class OrderDetailServiceImpl implements OrderDetailService {

    private static final AtomicLong INDEX = new AtomicLong(1L);

    public static final DateTimeFormatter MINI_FORMATTER = new DateTimeFormatterBuilder()
            .appendValue(YEAR, 4).appendValue(MONTH_OF_YEAR, 2)
            .appendValue(DAY_OF_MONTH, 2).appendValue(HOUR_OF_DAY, 2)
            .appendValue(MINUTE_OF_HOUR, 2).appendValue(SECOND_OF_MINUTE, 2).toFormatter();

    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Override
    public int insertOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.insertOrderDetail(orderDetail);
    }

    @Override
    public int updateOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.updateOrderDetail(orderDetail);
    }

    @Override
    public List<OrderDetail> selectOrderDetailList(OrderDetail orderDetail) {
        return orderDetailMapper.selectOrderDetailList(orderDetail);
    }

    @Override
    public OrderDetail selectById(Long orderDetailId) {
        return orderDetailMapper.selectById(orderDetailId);
    }

    @Override
    public List<OrderDetail> selectByOrderMasterId(Long orderMasterId) {
        return orderDetailMapper.selectByOrderMasterId(orderMasterId);
    }

    @Override
    public int deleteOrderDetailByIds(String ids) {
        Long[] orderDetailIds = Convert.toLongArray(ids);
        return orderDetailMapper.deleteOrderDetailByIds(orderDetailIds);
    }

    @Override
    public String checkOrderDetailCodeUnique(OrderDetail orderDetail) {
        long orderDetailId = orderDetail.getId() == null ? -1L : orderDetail.getId();
        OrderDetail info = orderDetailMapper.checkOrderDetailCodeUnique(orderDetail.getCode());
        if (info != null && info.getId() != orderDetailId) {
            return UserConstants.ORDER_CODE_NOT_UNIQUE;
        }
        return UserConstants.ORDER_CODE_UNIQUE;
    }

    @Override
    public String createCode() {
        INDEX.compareAndSet(9999L, 1L);
        LocalDateTime now = LocalDateTime.now();
        return "od" + now.format(MINI_FORMATTER) + INDEX.getAndIncrement();
    }

    @Override
    public Map<Long, List<OrderDetail>> byOrderIdInMap(List<Long> orderIdList) {
        Map<Long, List<OrderDetail>> longByOrderDetailListMap = new HashMap<>();
        if(orderIdList != null && orderIdList.size() > 0){
            List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderIdList(orderIdList);
            if(orderDetailList != null && orderDetailList.size() > 0){
                for(OrderDetail orderDetail : orderDetailList){
                    List<OrderDetail> orderDetailListByMapCrw = longByOrderDetailListMap.get(orderDetail.getOrderMasterId());
                    if(orderDetailListByMapCrw != null && orderDetailListByMapCrw.size() > 0){
                        List<OrderDetail> orderDetailListByMapNew = new ArrayList<>();
                        orderDetailListByMapNew.add(orderDetail);
                        longByOrderDetailListMap.put(orderDetail.getOrderMasterId(), orderDetailListByMapNew);
                    }else {
                        List<OrderDetail> orderDetailListByMapNew = new ArrayList<>();
                        orderDetailListByMapNew.add(orderDetail);
                        longByOrderDetailListMap.put(orderDetail.getOrderMasterId(), orderDetailListByMapNew);
                    }
                }
            }
        }
        return longByOrderDetailListMap;
    }

}
