package com.xy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.collections.list.JArrayList;
import com.xy.dto.DeviceAlgorithmChargingHistoryDto;
import com.xy.dto.OrderMercManageDto;
import com.xy.entity.DeviceAlgorithmChargingHistory;
import com.xy.mapper.DeviceAlgorithmChargingHistoryMapper;
import com.xy.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

import static com.xy.utils.Beans.copy;
import static com.xy.utils.PlusBeans.toPageBean;

/**
 * <p>
 * 设备算法计费历史表 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2023-06-29
 */
@Service
@AllArgsConstructor
@Api(tags = "设备算法计费历史表")
public class DeviceAlgorithmChargingHistoryServiceImpl extends ServiceImpl<DeviceAlgorithmChargingHistoryMapper, DeviceAlgorithmChargingHistory> implements DeviceAlgorithmChargingHistoryService {

    private OrderMercManageService orderMercManageService;

    @Override
    @ApiOperation("分页查询")
    public R<PageBean<DeviceAlgorithmChargingHistoryDto.Vo>> page(@RequestBody DeviceAlgorithmChargingHistoryDto.Page page) {
        LambdaQueryWrapper<DeviceAlgorithmChargingHistory> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceAlgorithmChargingHistory.class).build();
        IPage<DeviceAlgorithmChargingHistory> iPage = page(PlusBeans.toIPage(page.getPage()), lambdaQueryWrapper);
        PageBean<DeviceAlgorithmChargingHistoryDto.Vo> pageBean = toPageBean(DeviceAlgorithmChargingHistoryDto.Vo.class, iPage);
        List<DeviceAlgorithmChargingHistoryDto.Vo> records = pageBean.getRecords();
        if (Emptys.check(records)) {
            copy(records).target(
                    () -> orderMercManageService.list(new OrderMercManageDto.SelectList().setId(new JArrayList<>(records).getProperty(DeviceAlgorithmChargingHistoryDto.Vo::getOrderId))).getData()
                    , DeviceAlgorithmChargingHistoryDto.Vo::getOrderId, DeviceAlgorithmChargingHistoryDto.Vo::getFiles, OrderMercManageDto.Vo::getId, OrderMercManageDto.Vo::getFiles
            ).builder();
        }
        return R.ok(pageBean);
    }
}