package com.repair.controller;


import com.repair.api.RepairTransferRecordControllerAPI;
import com.repair.common.utils.paramUtils;
import com.repair.model.pojo.RepairTransferRecord;
import com.repair.model.result.CommonResult;
import com.repair.model.result.PageUtils;
import com.repair.services.RepairTransferRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-07-20
 */
@RestController
public class RepairTransferRecordController implements RepairTransferRecordControllerAPI {

    @Autowired
    private RepairTransferRecordService repairTransferRecordService;

    @Override
    public CommonResult InsertrepairTransferRecord(RepairTransferRecord rc, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = repairTransferRecordService.insertRepairTransferRecord(rc);

        return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
    }

    @Override
    public CommonResult updaterepairTransferRecordById(RepairTransferRecord rc, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = repairTransferRecordService.updateRepairTransferRecord(rc);

        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    @Override
    public CommonResult queryPagerepairTransferRecords(int currentPage, int pageCount, String name) {
        PageUtils<RepairTransferRecord> result = repairTransferRecordService.queryPageRepairTransferRecords(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult deleterepairTransferRecordById(int id) {
        RepairTransferRecord data = repairTransferRecordService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = repairTransferRecordService.deleteRepairTransferRecordById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }
}

