package com.repair.controller;


import com.repair.api.RepairSystemMessagesControllerAPI;
import com.repair.common.utils.paramUtils;
import com.repair.model.pojo.RepairSystemMessages;
import com.repair.model.result.CommonResult;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.SystemMessagePageVo;
import com.repair.services.RepairSystemMessagesService;
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 RepairSystemMessagesController implements RepairSystemMessagesControllerAPI {

    @Autowired
    private RepairSystemMessagesService repairSystemMessagesService;

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

        int result = repairSystemMessagesService.insertRepairSystemMessages(rc);

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

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

        int result = repairSystemMessagesService.updateRepairSystemMessages(rc);

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

    @Override
    public CommonResult queryPagerepairSystemMessagess(int currentPage, int pageCount, String name) {
        PageUtils<RepairSystemMessages> result = repairSystemMessagesService.queryPageRepairSystemMessagess(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }

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

        int result = repairSystemMessagesService.deleteRepairSystemMessagesById(id);

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

    @Override
    public CommonResult querySystemMessagePage(int currentPage, int pageCount, int userId) {
        PageUtils<SystemMessagePageVo> result = repairSystemMessagesService.querySystemMessagePage(currentPage, pageCount, userId);
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult readSystemMessage(int id) {
        RepairSystemMessages rsm = new RepairSystemMessages();
        rsm.setId(id);
        rsm.setIsRead(1);
        int uRsm = repairSystemMessagesService.updateRepairSystemMessages(rsm);
        return CommonResult.ok();
    }
}

