package com.jiayue.insu.incloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayue.insu.common.core.util.R;
import com.jiayue.insu.incloud.entity.Record;
import com.jiayue.insu.incloud.entity.Station;
import com.jiayue.insu.incloud.service.RecordService;
import com.jiayue.insu.incloud.service.StationService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * RecordController
 *
 * @author yh
 * @date 2022-03-18 15:48:48
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/record")
public class RecordController {

    private final RecordService recordService;

    /**
     * 分页查询
     *
     * @param page   分页对象
     * @param record record
     * @return
     */

    @GetMapping("/page")
    public R getPage(Page page, Record record) {
        LambdaQueryWrapper<Record> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(record.getStationCode()!=null){
            lambdaQueryWrapper.eq(Record::getStationCode,record.getStationCode());
        }
        if(record.getInCode()!=null){
            lambdaQueryWrapper.eq(Record::getInCode,record.getInCode());
        }
        if(record.getStartTime()!=null && record.getEndTime()!=null){
            lambdaQueryWrapper.between(Record::getCreateTime,record.getStartTime(),record.getEndTime());
        }

        return R.ok(recordService.page(page, lambdaQueryWrapper));
    }

    @GetMapping("/all")
    public R getAll() {
        return R.ok(recordService.list());
    }

    /**
     * 新增record
     *
     * @param record record
     * @return R
     */

    @PostMapping
    public R save(@RequestBody Record record) {
        return R.ok(recordService.save(record));
    }

    /**
     * 修改record
     *
     * @param record record
     * @return R
     */

    @PutMapping
    public R updateById(@RequestBody Record record) {
        return R.ok(recordService.updateById(record));
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return R
     */
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id) {
        return R.ok(recordService.removeById(id));
    }


}
