package com.repair.controller;


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

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-08-04
 */
@RestController
public class RepairCloseController implements RepairCloseControllerAPI {

    @Autowired
    private RepairCloseService repairCloseService;

    /**
     * 新增关单原因
     * @param rc 关单原因数据
     * @param bindingResult 是否为空判断
     * @return
     */
    @Override
    public CommonResult InsertRepairClose(RepairClose rc, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = repairCloseService.insertRepairClose(rc);

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

    /**
     * 根据数据ID更新关单原因
     * @param rc 关单原因
     * @param bindingResult 是否为空判断
     * @return
     */
    @Override
    public CommonResult updateRepairCloseById(RepairClose rc, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = repairCloseService.updateRepairClose(rc);

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

    /**
     * 关单原因分页查询
     * @param currentPage 当前页数
     * @param pageCount 一页数据条数
     * @param name 关单名称
     * @return
     */
    @Override
    public CommonResult queryPageRepairCloses(int currentPage, int pageCount, String name) {
        PageUtils<RepairClose> result = repairCloseService.queryPageRepairCloses(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }

    /**
     * 根据数据ID删除关单原因
     * @param id 数据ID
     * @return
     */
    @Override
    public CommonResult deleteRepairCloseById(int id) {
        RepairClose data = repairCloseService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = repairCloseService.deleteRepairCloseById(id);

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

    /**
     * 查询关单原因集合数据
     * @return
     */
    @Override
    public CommonResult queryRepairCloses() {
        List<RepairClose> result = repairCloseService.getRepairs();
        return CommonResult.ok(result);
    }
}

