package com.bonus.sharedstation.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.sharedstation.app.domain.GreenUserInfo;
import com.bonus.sharedstation.app.dto.RepairServiceDTO;
import com.bonus.sharedstation.app.dto.RepairServiceOrderDTO;
import com.bonus.sharedstation.app.entity.RepairService;
import com.bonus.sharedstation.app.entity.RepairServiceOrder;
import com.bonus.sharedstation.app.mapper.RepairServiceMapper;
import com.bonus.sharedstation.app.mapper.RepairServiceOrderMapper;
import com.bonus.sharedstation.app.service.RepairServiceOrderService;
import com.bonus.sharedstation.common.utils.OrderNumberGeneratorUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 便民服务信息表保洁(CleaningServicesOrder)表服务实现类
 *
 * @author makejava
 * @since 2024-12-10 17:02:11
 */
@Service("repairServiceOrderService")
@Slf4j
public class RepairServiceOrderServiceImpl extends ServiceImpl<RepairServiceOrderMapper, RepairServiceOrder> implements RepairServiceOrderService {

    @Resource
    private RepairServiceOrderMapper dao;

    @Resource
    private BaseService baseService;


    @Resource
    private RepairServiceMapper repairServiceMapper;

    @Override
    public AjaxResult getListPageApp(RepairServiceOrderDTO dto) {
        log.info("查询 入参: {}", dto);
        LambdaQueryWrapper<RepairServiceOrder> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象

        Page<RepairServiceOrder> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        if (dto.getUserId() != null) {
            queryWrapper.eq(RepairServiceOrder::getUserId, dto.getUserId());
        } else {
            throw new RuntimeException("请先登录!");
        }
        queryWrapper
                .eq(RepairServiceOrder::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(RepairServiceOrder::getCreatedTime);     // 降序排序

        Page<RepairServiceOrder> result = dao.selectPage(page, queryWrapper);
        //处理参数问题
        return AjaxResult.success(result);
    }


    @Override
    public AjaxResult getListWeb(RepairServiceOrderDTO dto) {
        log.info("查询 入参: {}", dto);
        LambdaQueryWrapper<RepairServiceOrder> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<RepairServiceOrder> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        queryWrapper
                .eq(RepairServiceOrder::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(RepairServiceOrder::getCreatedTime);     // 降序排序

        Page<RepairServiceOrder> result = dao.selectPage(page, queryWrapper);
        //处理参数问题
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult selectInfoById(RepairServiceOrder dto) {
        RepairServiceOrder result = dao.selectById(dto.getId());
        if (StringUtils.isNull(result)) {
            return AjaxResult.error("此维修服务订单不存在");
        }
        //处理参数问题
        return AjaxResult.success(result);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getListPage(RepairServiceDTO dto) {
        log.info("查询 入参: {}", dto);
        LambdaQueryWrapper<RepairService> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<RepairService> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        if (StringUtils.isNotEmpty(dto.getServiceName())) {
            queryWrapper.like(RepairService::getServiceName, dto.getServiceName());
        }
        queryWrapper
                .eq(RepairService::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(RepairService::getCreatedTime);     // 降序排序

        Page<RepairService> result = repairServiceMapper.selectPage(page, queryWrapper);
        //处理参数问题
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult insertToPay(RepairServiceOrder dto) {
        log.info("新增入参 : " + dto);
        GreenUserInfo userInfo = baseService.getUserInfo(dto.getUserId());
        //三方扣款
        //流水号
        dto.setOrdno(OrderNumberGeneratorUtils.generateOrderNumber());
        //支付相关接入第三方小牛支付
//        return payByNiu(userInfo, dto);
        return null;
    }


}

