package io.hmit.modules.serviceOrder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.hmit.common.constant.Constant;
import io.hmit.common.page.PageData;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.modules.serviceOrder.dao.PensionOrderDao;
import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
import io.hmit.modules.serviceOrder.dto.PensionOrderServiceDTO;
import io.hmit.modules.serviceOrder.dto.ReservationDTO;
import io.hmit.modules.serviceOrder.entity.PensionOrderEntity;
import io.hmit.modules.serviceOrder.service.PensionOrderService;
import io.hmit.modules.serviceOrder.service.PensionOrderServiceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 订单表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-01-26
 */
@Service
public class PensionOrderServiceImpl extends CrudServiceImpl<PensionOrderDao, PensionOrderEntity, PensionOrderDTO> implements PensionOrderService {

    @Override
    public QueryWrapper<PensionOrderEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PensionOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Autowired
    private PensionOrderDao pensionOrderDao;

    @Autowired
    private PensionOrderServiceService pensionOrderServiceService;

    //查询全部订单
    @Override
    public PageData<PensionOrderDTO> getAllOrderPage(Map<String, Object> params, Long userId) {
        //分页
        IPage<PensionOrderEntity> page = getPage(params, Constant.CREATE_DATE, false);
        IPage<PensionOrderDTO> pensionOrderEntityIPage = pensionOrderDao.getOrderPageByStatusOrUserId(page, userId, null);
        return getPageData(pensionOrderEntityIPage, PensionOrderDTO.class);
    }

    //查询今日订单
    @Override
    public PageData<PensionOrderDTO> getTodayOrderPage(Map<String, Object> params) {
        IPage<PensionOrderEntity> page = getPage(params, Constant.CREATE_DATE, false);
//        IPage<PensionOrderEntity> pensionOrderEntityIPage = pensionOrderDao.getTodayOrderPage(page, userId); //TODO 今日订单中展示未接单订单
        IPage<PensionOrderDTO> pensionOrderDTOIPage = pensionOrderDao.getTodayNoProceedOrderPage(page);
        return getPageData(pensionOrderDTOIPage, PensionOrderDTO.class);
    }

    //根据订单Id和订单状态查询 (接单时使用)
    @Override
    public PensionOrderDTO getOrderInfoByIdAndStatus(Long id, Integer status) {
        PensionOrderEntity orderEntity = pensionOrderDao.getOrderInfoByIdAndStatus(id, status);
        return ConvertUtils.sourceToTarget(orderEntity, PensionOrderDTO.class);
    }

    @Override
    public void reservation(PensionOrderDTO pensionOrderDTO) {
        save(pensionOrderDTO);
        PensionOrderServiceDTO orderServiceDTO = new PensionOrderServiceDTO().assemblePensionOrderService(pensionOrderDTO);
        pensionOrderServiceService.save(orderServiceDTO);

    }

    @Override
    public PageData<PensionOrderDTO> getOrderPageByStatus(Map<String, Object> params, Long userId, Integer status) {
        IPage<PensionOrderEntity> page = getPage(params, Constant.CREATE_DATE, false);
        IPage<PensionOrderDTO> pensionOrderDTOIPage = pensionOrderDao.getOrderPageByStatusOrUserId(page, userId, status);
        return getPageData(pensionOrderDTOIPage, PensionOrderDTO.class);
    }

    //根据用户ID或订单状态查询(带图)
    @Override
    public PensionOrderDTO getOrderInfoByIdAndUserIdOrStatus(Long orderId, Long userId, Integer status) {
        PensionOrderDTO pensionOrderDTO = pensionOrderDao.getOrderInfoByIdAndUserIdOrStatus(orderId, userId, status);
        return pensionOrderDTO;
    }

    @Override
    public PageData<PensionOrderDTO> getElderTodayOrderPage(Map<String, Object> params, Long creator) {
        IPage<PensionOrderEntity> page = getPage(params, Constant.CREATE_DATE, false);
        IPage<PensionOrderDTO> pensionOrderDTOIPage = pensionOrderDao.getElderTodayOrderPage(page, creator);
        return getPageData(pensionOrderDTOIPage, PensionOrderDTO.class);
    }

    @Override
    public PageData<PensionOrderDTO> getElderAllOrderOrByStatusPage(Map<String, Object> params, Long creator, Integer status) {
        IPage<PensionOrderEntity> page = getPage(params, Constant.CREATE_DATE, false);
        List<PensionOrderDTO> list = pensionOrderDao.getElderAllOrderOrByStatusList(params, creator, status);
        return getPageData(list, page.getTotal(), PensionOrderDTO.class);
    }

}
