package com.ruoyi.mall.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.MallCombineGoods;
import com.ruoyi.mall.domain.MallOrderGoods;
import com.ruoyi.mall.domain.MallOrderOrder;
import com.ruoyi.mall.mapper.MallCombineGoodsMapper;
import com.ruoyi.mall.mapper.MallOrderGoodsMapper;
import com.ruoyi.mall.mapper.MallOrderOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallCombineOrderLinkMapper;
import com.ruoyi.mall.domain.MallCombineOrderLink;
import com.ruoyi.mall.service.IMallCombineOrderLinkService;

import javax.annotation.Resource;

/**
 * 拼团订单关联Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-21
 */
@Service
public class MallCombineOrderLinkServiceImpl implements IMallCombineOrderLinkService
{
    @Resource
    private MallCombineOrderLinkMapper mallCombineOrderLinkMapper;
    @Resource
    private MallOrderOrderMapper mallOrderOrderMapper;
    @Resource
    private MallOrderGoodsMapper mallOrderGoodsMapper;
    @Resource
    private MallCombineGoodsMapper mallCombineGoodsMapper;

    /**
     * 查询拼团订单关联
     *
     * @param linkId 拼团订单关联主键
     * @return 拼团订单关联
     */
    @Override
    public MallCombineOrderLink selectMallCombineOrderLinkByLinkId(Long linkId)
    {
        return mallCombineOrderLinkMapper.selectMallCombineOrderLinkByLinkId(linkId);
    }

    /**
     * 查询拼团订单关联列表
     *
     * @param mallCombineOrderLink 拼团订单关联
     * @return 拼团订单关联
     */
    @Override
    public List<MallCombineOrderLink> selectMallCombineOrderLinkList(MallCombineOrderLink mallCombineOrderLink)
    {
        return mallCombineOrderLinkMapper.selectMallCombineOrderLinkList(mallCombineOrderLink);
    }

    /**
     * 新增拼团订单关联
     *
     * @param mallCombineOrderLink 拼团订单关联
     * @return 结果
     */
    @Override
    public int insertMallCombineOrderLink(MallCombineOrderLink mallCombineOrderLink)
    {
        mallCombineOrderLink.setCreateAt(DateUtils.getTime());
        return mallCombineOrderLinkMapper.insertMallCombineOrderLink(mallCombineOrderLink);
    }

    /**
     * 修改拼团订单关联
     *
     * @param mallCombineOrderLink 拼团订单关联
     * @return 结果
     */
    @Override
    public int updateMallCombineOrderLink(MallCombineOrderLink mallCombineOrderLink)
    {
        mallCombineOrderLink.setUpdateAt(DateUtils.getTime());
        return mallCombineOrderLinkMapper.updateMallCombineOrderLink(mallCombineOrderLink);
    }

    /**
     * 批量删除拼团订单关联
     *
     * @param linkIds 需要删除的拼团订单关联主键
     * @return 结果
     */
    @Override
    public int deleteMallCombineOrderLinkByLinkIds(Long[] linkIds)
    {
        return mallCombineOrderLinkMapper.deleteMallCombineOrderLinkByLinkIds(linkIds);
    }

    /**
     * 删除拼团订单关联信息
     *
     * @param linkId 拼团订单关联主键
     * @return 结果
     */
    @Override
    public int deleteMallCombineOrderLinkByLinkId(Long linkId)
    {
        return mallCombineOrderLinkMapper.deleteMallCombineOrderLinkByLinkId(linkId);
    }

    /**
     * 待拼团列表
     *
     * @param params 参数
     * @return 待拼团列表
     */
    @Override
    public List<Map<String, Object>> getWaitCombineList(Map<String, Object> params) {
        return mallCombineOrderLinkMapper.getWaitCombineList(params);
    }

    /**
     * 待拼团详情
     *
     * @param params 参数
     * @return 待拼团详情
     */
    @Override
    public Map<String, Object> getWaitCombineInfo(Map<String, Object> params) {
        Map<String, Object> waitCombineInfo = mallCombineOrderLinkMapper.getWaitCombineInfo(params);

        MallCombineOrderLink combineOrderLink = new MallCombineOrderLink();
        combineOrderLink.setTeamId(Long.parseLong(waitCombineInfo.get("teamId").toString()));
        combineOrderLink.setStatus(0);
        List<MallCombineOrderLink> waitCombineList = mallCombineOrderLinkMapper.selectMallCombineOrderLinkList(combineOrderLink);

        List<Map<String, Object>> combinePeopleListMap = new ArrayList<>();

        if (waitCombineList.size() > 0) {
            for (MallCombineOrderLink orderLink : waitCombineList) {
                long userId = orderLink.getUserId();

                Map<String, Object> peopleInfoMap = new HashMap<>();
                peopleInfoMap.put("userId", userId);
                peopleInfoMap.put("userName", orderLink.getUserName());
                peopleInfoMap.put("avatar", orderLink.getAvatar());
                combinePeopleListMap.add(peopleInfoMap);

            }
        }

        waitCombineInfo.put("combinePeopleList", combinePeopleListMap);
        return waitCombineInfo;
    }

    /**
     * 发起拼团或参与拼团
     *
     * @param params 参数
     * @return 结果
     */
    @Override
    public int initiateOrJoinCombine(Map<String, Object> params) {
        long orderId = Long.parseLong(params.get("orderId").toString());

        MallOrderOrder orderOrder = mallOrderOrderMapper.selectMallOrderOrderById(orderId);
        MallOrderGoods orderGoods = mallOrderGoodsMapper.selectOrderGoodsByOrderId(orderId);

        MallCombineOrderLink combineOrderLink = new MallCombineOrderLink();
        combineOrderLink.setOrderId(orderOrder.getId());
        combineOrderLink.setOrderNo(orderOrder.getOrderNo());
        combineOrderLink.setUserId(orderOrder.getUserId());
        combineOrderLink.setGoodsId(orderGoods.getGoodsId());

        MallCombineGoods combineGoods = mallCombineGoodsMapper.selectCombineGoodsByGoodsId(orderGoods.getGoodsId());
        combineOrderLink.setCombineId(combineGoods.getCombineId());
        combineOrderLink.setPeopleNum(combineGoods.getPeopleNum());
        combineOrderLink.setEndMinute(combineGoods.getEndMinute());
        combineOrderLink.setStatus(0);

        String time = DateUtils.getTime();
        // 获取当前时间
        Date nowTime = new Date();

        combineOrderLink.setCreateAt(time);



        // 发起拼团
        if (orderOrder.getTeamId() == null || orderOrder.getTeamId() == 0) {
            combineOrderLink.setParentId(0L);
            combineOrderLink.setTeamId(combineOrderLink.getLinkId());

            // 将当前时间加上X分钟
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(nowTime);
            calendar.add(Calendar.MINUTE, combineGoods.getEndMinute());

            // 格式化新的时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String endTime = sdf.format(calendar.getTime());

            combineOrderLink.setStartTime(time);
            combineOrderLink.setEndTime(endTime);

        } else {

            MallCombineOrderLink orderLink = mallCombineOrderLinkMapper.selectMallCombineOrderLinkByLinkId(orderOrder.getTeamId());
            if (orderLink.getStatus() != 0) {
                combineOrderLink.setParentId(0L);
                combineOrderLink.setTeamId(combineOrderLink.getLinkId());

                // 将当前时间加上X分钟
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(nowTime);
                calendar.add(Calendar.MINUTE, combineGoods.getEndMinute());

                // 格式化新的时间
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String endTime = sdf.format(calendar.getTime());

                combineOrderLink.setStartTime(time);
                combineOrderLink.setEndTime(endTime);
            } else {
                // 参与拼团
                combineOrderLink.setParentId(orderLink.getLinkId());
                combineOrderLink.setTeamId(orderOrder.getTeamId());

                combineOrderLink.setStartTime(orderLink.getStartTime());
                combineOrderLink.setEndTime(orderLink.getEndTime());
            }
        }

        mallCombineOrderLinkMapper.insertMallCombineOrderLink(combineOrderLink);
        if (combineOrderLink.getTeamId() == null) {
            combineOrderLink.setTeamId(combineOrderLink.getLinkId());
            mallCombineOrderLinkMapper.updateMallCombineOrderLink(combineOrderLink);
        }
        return 1;
    }
}
