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.ReservationDTO;
import io.hmit.modules.serviceOrder.entity.PensionOrderEntity;
import io.hmit.modules.serviceOrder.service.PensionOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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 {

    private static final Integer INCOMPLETE_ORDER = 0;
    private static final Integer TAKING_ORDER = 1;
    private static final Integer FINISHED_ORDER = 2;
    private static final Integer CANCELED_ORDER = 3;

    @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;

    @Override
    public PageData<PensionOrderDTO> getAllOrderPage(Map<String, Object> params, Long userId) {
        //分页
        IPage<PensionOrderEntity> page = getPage(params, Constant.CREATE_DATE, false);

        //查询 -- 自定义分页
        IPage<PensionOrderEntity> pensionOrderEntityIPage = pensionOrderDao.getAllOrderPage(page, userId);
        return getPageData(pensionOrderEntityIPage, PensionOrderDTO.class);
    }

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

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

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

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

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

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

    @Override
    public PensionOrderDTO getOrderInfoByIdAndUserId(Long id, Long userId) {
        PensionOrderEntity orderEntity = pensionOrderDao.getOrderInfoByIdAndUserId(id, userId);
        orderEntity.setServiceId(null);
        orderEntity.setOrderRecipientId(null);
        orderEntity.setDeptId(null);
        orderEntity.setCreator(null);
        orderEntity.setUpdater(null);
        orderEntity.setUpdateDate(null);
        return ConvertUtils.sourceToTarget(orderEntity, PensionOrderDTO.class);
    }

    @Override
    public PensionOrderDTO getOrderInfoByIdAndStatus(Long id, Integer status) {
        PensionOrderEntity orderEntity = pensionOrderDao.getOrderInfoByIdAndStatus(id, status);
        return ConvertUtils.sourceToTarget(orderEntity, PensionOrderDTO.class);
    }

    @Override
    public void reservation(ReservationDTO reservationDTO) {


    }

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

}

