package com.jd.lightapplication.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jd.lightapplication.common.lang.R;
import com.jd.lightapplication.model.FaultHandlingRecord;
import com.jd.lightapplication.service.FaultHandlingRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Resource;


@Slf4j
@RestController()
@RequestMapping("/api/fault/handling/record")
@Api(tags = "故障处理记录")
public class FaultHandlingRecordController {

    @Resource
    private FaultHandlingRecordService faultHandlingRecordService;

    @GetMapping("/list")
    @ApiOperation(value = "1、分页查询")
    public R queryPageBean(Page page, FaultHandlingRecord faultHandlingRecord) {
        LambdaQueryWrapper<FaultHandlingRecord> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(faultHandlingRecord.getTitle())) {
            wrapper.like(FaultHandlingRecord::getTitle, faultHandlingRecord.getTitle());
        }
        wrapper.orderByDesc(FaultHandlingRecord::getCreateTime);
        wrapper.orderByAsc(FaultHandlingRecord::getStatus);
        return R.success(faultHandlingRecordService.page(page, wrapper));
    }

    @PostMapping("/addBean")
    @ApiOperation(value = "2、新增")
    public R addBean(@RequestBody FaultHandlingRecord faultHandlingRecord) {
        faultHandlingRecord.setStatus(10);
        faultHandlingRecord.setCreateTime(DateUtil.now());
        faultHandlingRecordService.save(faultHandlingRecord);
        return R.success();
    }

    @PostMapping("/updateBean")
    @ApiOperation(value = "3、修改")
    public R updateBean(@RequestBody FaultHandlingRecord faultHandlingRecord) {
        //如果处理时间不为空，把状态改成已处理
        if(StrUtil.isNotBlank(faultHandlingRecord.getUpdateTime())){
            faultHandlingRecord.setStatus(20);
        }
        faultHandlingRecordService.updateById(faultHandlingRecord);
        return R.success();
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "4、删除")
    public R deleteById(long id) {
        faultHandlingRecordService.removeById(id);
        return R.success();
    }

}