package com.xjrsoft.module.personnel.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.personnel.dto.CarParkRecordsPageDto;
import com.xjrsoft.module.personnel.entity.CarParkRecords;
import com.xjrsoft.module.personnel.service.ICarParkRecordsService;
import com.xjrsoft.module.personnel.vo.CarParkRecordsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/personnel" + "/carparkrecords")
@Api(value = "/personnel"  + "/carparkrecords",tags = "车辆进出记录")
@AllArgsConstructor
public class CarParkRecordsController {
    private  final ICarParkRecordsService carParkRecordsService;

    @GetMapping(value = "/page")
    @ApiOperation(value="车辆进出记录(分页)")
    @SaCheckPermission("carparkrecords:detail")
    public RT<PageOutput<CarParkRecordsVo>> Page(@Valid CarParkRecordsPageDto dto) {
        MPJLambdaWrapper<CarParkRecords> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(dto.getCarNo()), CarParkRecords::getCarNo, dto.getCarNo())
                .eq( ObjectUtil.isNotNull(dto.getStatus())&&dto.getStatus() > 0, CarParkRecords::getStatus, dto.getStatus())
                .eq(ObjectUtil.isNotNull(dto.getInPassing())&& dto.getInPassing()> 0, CarParkRecords::getInPassing, dto.getInPassing())
                .eq(ObjectUtil.isNotNull(dto.getOutPassing())&& dto.getOutPassing()> 0, CarParkRecords::getOutPassing, dto.getOutPassing());

        if (ObjectUtil.isNotNull(dto.getStartInTime()) && ObjectUtil.isNotNull(dto.getEndInTime())) {
            queryWrapper.ge(CarParkRecords::getInTime, dto.getStartInTime());
            queryWrapper.le(CarParkRecords::getInTime, dto.getEndInTime());
        }
        if (ObjectUtil.isNotNull(dto.getStartOutTime()) && ObjectUtil.isNotNull(dto.getEndOutTime())) {
            queryWrapper.gt(CarParkRecords::getOutTime, DateUtil.beginOfDay(dto.getStartOutTime()));
            queryWrapper.lt(CarParkRecords::getOutTime, DateUtil.endOfDay(dto.getEndOutTime()));
        }
        queryWrapper.select(CarParkRecords.class, x -> VoToColumnUtil.fieldsToColumns(CarParkRecordsVo.class).contains(x.getProperty()))
                .select(CarParkRecords::getId)
                .orderByDesc(CarParkRecords::getInTime,CarParkRecords::getOutTime);

        IPage<CarParkRecords> page = carParkRecordsService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<CarParkRecordsVo> pageOutput = ConventPage.getPageOutput(page, CarParkRecordsVo.class);
        return RT.ok(pageOutput);
    }
}
