package com.yifu.cloud.plus.v1.yifu.order.service.impl;

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.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.order.constants.OrderConstants;
import com.yifu.cloud.plus.v1.yifu.order.entity.TOrder;
import com.yifu.cloud.plus.v1.yifu.order.mapper.TOrderMapper;
import com.yifu.cloud.plus.v1.yifu.order.service.TOrderService;
import com.yifu.cloud.plus.v1.yifu.order.vo.OrderListParam;
import com.yifu.cloud.plus.v1.yifu.order.vo.OrderListVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author licancan
 * @description 针对表【t_order(订单表)】的数据库操作Service实现
 * @date 2022-08-26 17:22:18
 */
@Slf4j
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

	/**
	 * 订单列表分页查询
	 *
	 * @param page
	 * @param param
	 * @return {@link IPage <OrderListVO>}
	 * @author licancan
	 */
	@Override
	public IPage<OrderListVO> getOrderPageList(Page<OrderListParam> page, OrderListParam param) {
		IPage<OrderListVO> orderList = baseMapper.getOrderPageList(page,param);
		return orderList;
	}

	/**
	 * 订单列表不分页查询
	 *
	 * @param param
	 * @return {@link List<OrderListVO>}
	 * @author licancan
	 */
	@Override
	public List<OrderListVO> getOrderList(OrderListParam param) {
		List<OrderListVO> list;
		if (CollectionUtils.isNotEmpty(param.getIdList())){
			list = baseMapper.getOrderListBySelect(param.getIdList());
		}else {
			list = baseMapper.getOrderList(param);
		}
		return list;
	}

	/**
	 * 变更订单状态
	 *
	 * @param id
	 * @param status
	 * @return {@link R <String>}
	 * @author licancan
	 */
	@Override
	public R<String> handleOrder(String id, Integer status) {
		if (StringUtils.isEmpty(id)){
			return R.failed(OrderConstants.ORDER_ID_IS_EMPTY);
		}
		if (CommonConstants.TWO_INT != status && CommonConstants.THREE_INT != status){
			return R.failed(OrderConstants.STATUS_ERROR);
		}
		YifuUser user = SecurityUtils.getUser();
		TOrder byId = this.getById(id);
		if (!Optional.ofNullable(byId).isPresent()){
			return R.failed(OrderConstants.DATA_IS_EMPTY);
		}
		if (CommonConstants.THREE_INT == byId.getOrderStatus()){
			return R.failed(OrderConstants.ORDER_IS_HANDLED);
		}
		byId.setOrderStatus(status);
		byId.setUpdateBy(user.getId());
		byId.setUpdateName(user.getNickname());
		byId.setUpdateTime(LocalDateTime.now());
		this.updateById(byId);
		return R.ok(OrderConstants.OPERATE_SUCCESS);
	}

}
