package com.repair.controller;


import com.repair.api.RepairSystemSettingControllerAPI;
import com.repair.common.utils.paramUtils;
import com.repair.model.pojo.RepairSystemSetting;
import com.repair.model.result.CommonResult;
import com.repair.services.RepairSystemSettingService;
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-11
 */
@RestController
public class RepairSystemSettingController implements RepairSystemSettingControllerAPI {
    @Autowired
    private RepairSystemSettingService repairSystemSettingService;

    /**
     * 查询系统设置数据
     * @return
     */
    @Override
    public CommonResult QueryRepairSystemSetting() {
        RepairSystemSetting result = repairSystemSettingService.queryRepairSystemSetting();
        return CommonResult.ok(result);
    }

    /**
     * 新增系统设置数据
     * @param repairSystemSetting
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult InsertRepairSystemSetting(RepairSystemSetting repairSystemSetting, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        int result = repairSystemSettingService.insertRepairSystemSetting(repairSystemSetting);

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

    /**
     * 修改系统设置数据
     * @param ra
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult updateRepairSystemSettingById(RepairSystemSetting ra, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        ra.setHour(ra.getDay() * 24);

        int result = repairSystemSettingService.updateRepaiSystemSetting(ra);

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

