package com.repair.controller;


import com.repair.api.RepairCollaborateRecordControllerAPI;
import com.repair.common.utils.paramUtils;
import com.repair.model.pojo.RepairCollaborateRecord;
import com.repair.model.result.CommonResult;
import com.repair.model.result.PageUtils;
import com.repair.services.RepairCollaborateRecordService;
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 RepairCollaborateRecordController implements RepairCollaborateRecordControllerAPI {

    @Autowired
    private RepairCollaborateRecordService repairCollaborateRecordService;

    /**
     * 新增协作申请数据
     * @param rc 协作申请数据
     * @param bindingResult 是否为空判断
     * @return
     */
    @Override
    public CommonResult InsertRepairCollaborateRecord(RepairCollaborateRecord rc, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = repairCollaborateRecordService.insertRepairCollaborateRecord(rc);

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

    /**
     * 更新协作申请数据
     * @param rc 协作申请数据
     * @param bindingResult 是否为空判断
     * @return
     */
    @Override
    public CommonResult updateRepairCollaborateRecordById(RepairCollaborateRecord rc, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = repairCollaborateRecordService.updateRepairCollaborateRecord(rc);

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

    /**
     * 协作申请分页数据查询
     * @param currentPage 当前页数
     * @param pageCount 一页数据条数
     * @param name 协作名称
     * @return
     */
    @Override
    public CommonResult queryPageRepairCollaborateRecords(int currentPage, int pageCount, String name) {
        PageUtils<RepairCollaborateRecord> result = repairCollaborateRecordService.queryPageRepairCollaborateRecords(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }

    /**
     * 根据数据ID删除协作申请数据
     * @param id 数据ID
     * @return
     */
    @Override
    public CommonResult deleteRepairCollaborateRecordById(int id) {
        RepairCollaborateRecord data = repairCollaborateRecordService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = repairCollaborateRecordService.deleteRepairCollaborateRecordById(id);

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

