package com.sckw.system.controller.forkliftapp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.web.response.BaseResult;
import com.sckw.system.model.LoadingRecords;
import com.sckw.system.model.vo.res.LoadingRecordsResVo;
import com.sckw.system.repository.LoadingRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 装载作业记录Controller
 * @author chenxiaofei
 * @date 2026-01-07
 */
@RestController
@RequestMapping("/loadingRecords")
@Tag(name = "装载记录相关接口")
public class LoadingRecordsController {

    @Autowired
    private LoadingRecordsService loadingRecordsService;

    /**
     * 获取订单列表（装载记录列表）
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param operatorId 操作员ID（可选）
     * @param workDate 作业日期（可选）
     * @return BaseResult
     */
    @GetMapping("/orders")
    @Operation(summary = "获取订单列表", description = "分页获取装载记录列表")
    public BaseResult<IPage<LoadingRecordsResVo>> getOrders(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long operatorId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date workDate) {
        
        Page<LoadingRecords> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LoadingRecords> wrapper = new LambdaQueryWrapper<>();
        
        if (operatorId != null) {
            wrapper.eq(LoadingRecords::getOperatorId, operatorId);
        }
        if (workDate != null) {
            wrapper.eq(LoadingRecords::getWorkDate, workDate);
        }
        
        wrapper.orderByDesc(LoadingRecords::getCreatedAt);
        
        IPage<LoadingRecords> result = loadingRecordsService.page(page, wrapper);
        
        // 转换为ResVo
        Page<LoadingRecordsResVo> resVoPage = new Page<>(result.getCurrent(), result.getSize(), result.getTotal());
        List<LoadingRecordsResVo> resVoList = result.getRecords().stream()
                .map(record -> {
                    LoadingRecordsResVo resVo = new LoadingRecordsResVo();
                    BeanUtils.copyProperties(record, resVo);
                    return resVo;
                })
                .collect(Collectors.toList());
        resVoPage.setRecords(resVoList);
        
        return BaseResult.success(resVoPage);
    }

    /**
     * 创建装载记录
     * @param loadingRecord 装载记录
     * @return BaseResult
     */
    @PostMapping("/create")
    @Operation(summary = "创建装载记录", description = "创建新的装载作业记录")
    public BaseResult<String> createLoadingRecord(@RequestBody LoadingRecords loadingRecord) {
        boolean success = loadingRecordsService.save(loadingRecord);
        if (success) {
            return BaseResult.success("创建成功");
        }
        return BaseResult.failed("创建失败");
    }

    /**
     * 根据ID获取装载记录详情
     * @param id 记录ID
     * @return BaseResult
     */
    @GetMapping("/detail")
    @Operation(summary = "获取装载记录详情", description = "根据ID获取装载记录详细信息")
    public BaseResult<LoadingRecordsResVo> getLoadingRecordDetail(@RequestParam Long id) {
        LoadingRecords record = loadingRecordsService.getById(id);
        if (record == null) {
            return BaseResult.success(null);
        }
        LoadingRecordsResVo resVo = new LoadingRecordsResVo();
        BeanUtils.copyProperties(record, resVo);
        return BaseResult.success(resVo);
    }

}

