/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.distribution.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.base.BaseEntity;
import org.springblade.common.enums.OrderStateEnum;
import org.springblade.modules.api.vo.ExportAllAppointGoodsVO;
import org.springblade.modules.api.vo.ExportAppointGoodsVO;
import org.springblade.modules.api.vo.ExportTakeAppointGoodsVO;
import org.springblade.modules.distribution.dto.AppointDTO;
import org.springblade.modules.distribution.entity.Appoint;
import org.springblade.modules.distribution.vo.AppointStoreVO;
import org.springblade.modules.distribution.vo.AppointVO;
import org.springblade.modules.distribution.mapper.AppointMapper;
import org.springblade.modules.distribution.service.IAppointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.finance.entity.Order;
import org.springblade.modules.finance.entity.OrderGroupon;
import org.springblade.modules.finance.mapper.AppointGoodsMapper;
import org.springblade.modules.finance.service.IOrderGrouponService;
import org.springblade.modules.finance.service.IOrderService;
import org.springblade.modules.platform.entity.Message;
import org.springblade.modules.platform.service.IMessageService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 履约单表列表 服务实现类
 *
 * @author BladeX
 * @since 2023-06-06
 */
@Service
public class AppointServiceImpl extends ServiceImpl<AppointMapper, Appoint> implements IAppointService {

	@Resource
	private IMessageService messageService;

	@Resource
	private IOrderService orderService;

	@Resource
	private IOrderGrouponService orderGrouponService;

	@Resource
	private AppointGoodsMapper appointGoodsMapper;

	@Override
	public IPage<AppointVO> selectAppointPage(IPage<AppointVO> page, AppointVO appoint) {
		return page.setRecords(baseMapper.selectAppointPage(page, appoint));
	}

	@Override
	public List<AppointDTO> countAppointList() {
		return baseMapper.countAppointList();
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void commitAppoint(Long appointId) {
		Appoint appoint = getById(appointId);
		if(ObjectUtil.isEmpty(appoint) || appoint.getState() != 1){
			throw new RuntimeException("该履约单不能重复核销");
		}
		update(Wrappers.lambdaUpdate(Appoint.class).eq(BaseEntity::getId, appointId).eq(Appoint::getState, 1)
				.set(Appoint::getState, 2)
				.set(Appoint::getArrivalTime, new Date()));
		List<Appoint> list = list(Wrappers.lambdaQuery(Appoint.class).lt(Appoint::getState, 2)
				.eq(Appoint::getTakeId, appoint.getTakeId()).eq(Appoint::getGrouponId, appoint.getGrouponId()));
		if (ObjectUtil.isEmpty(list) || list.size() == 0) {//当提货点所有订单都核销完成后进行消息发送
			List<OrderGroupon> orderGroupons = orderGrouponService.list(Wrappers.lambdaQuery(OrderGroupon.class)
					.eq(OrderGroupon::getSelfTakeId, appoint.getTakeId()).eq(OrderGroupon::getGrouponId, appoint.getGrouponId()));
			for (OrderGroupon orderGroupon : orderGroupons) {//修改团购订单到货状态
				orderGroupon.setIsArrive(1);
			}
			orderGrouponService.updateBatchById(orderGroupons);
			List<Long> orderIds = orderGroupons.stream().map(OrderGroupon::getOrderId).collect(Collectors.toList());
			List<Order> orders = orderService.list(Wrappers.lambdaQuery(Order.class).eq(Order::getOrderState, OrderStateEnum.WAIT_DELIVERY.getValue()).in(BaseEntity::getId, orderIds));
			List<Message> messages = new ArrayList<>();
			for (Order order : orders) {//查询出所有需要发消息的订单给用户发消息
				Message message = new Message();
				message.setMainTitle("可以进行提货");
				message.setOrderNo(order.getOrderNo());
				message.setUserId(order.getUserId());
				messages.add(message);
			}
			messageService.saveBatch(messages);
		}
	}

	@Override
	public List<ExportAllAppointGoodsVO> exportAllGoods(Long grouponId) {
		return appointGoodsMapper.exportAllAppointGoods(grouponId);
	}

	@Override
	public List<ExportAppointGoodsVO> exportStoreGoods(Long grouponId, Long storeId) {
		return appointGoodsMapper.exportAppointGoods(grouponId, storeId);
	}

	@Override
	public List<ExportTakeAppointGoodsVO> exportTakeGoods(Long grouponId, Long takeId) {
		return appointGoodsMapper.exportTakeAppointGoods(grouponId, takeId);
	}

	@Override
	public IPage<AppointStoreVO> getAppointStoreVOs(IPage<AppointStoreVO> page, Integer state, Long storeId) {
		return page.setRecords(baseMapper.getAppointStoreVOS(page, state, storeId, 1));
	}

	@Override
	public IPage<AppointStoreVO> getAppointAllVOs(IPage<AppointStoreVO> page, Integer state) {
		return page.setRecords(baseMapper.getAppointStoreVOS(page, state, 0L, 3));
	}

	@Override
	public IPage<AppointStoreVO> getAppointTakeVOs(IPage<AppointStoreVO> page, Integer state, Long takeId) {
		return page.setRecords(baseMapper.getAppointStoreVOS(page, state, takeId, 2));
	}

	@Override
	public IPage<AppointVO> appointListBy(IPage<AppointVO> page, Appoint appoint) {
		return page.setRecords(baseMapper.appointListBy(page, appoint));
	}

}
