package com.template.controller;


import com.template.annotation.DESRespondSecret;
import com.template.api.SmartDeviceControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartDevice;
import com.template.model.pojo.SmartDevice;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.SmartDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 应用管理 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-03-26
 */
@RestController
public class SmartDeviceController implements SmartDeviceControllerAPI {

    @Autowired
    private SmartDeviceService smartDeviceService;

    /**
     * 新增设备
     *
     * @param smartDevice    设备数据
     * @param bindingResult
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult insertSmartDevice(SmartDevice smartDevice, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = smartDeviceService.insertSmartDevice(smartDevice);

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

    /**
     * 更新设备
     *
     * @param sa            设备数据
     * @param bindingResult
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult updateSmartDeviceById(SmartDevice sa, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

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

    /**
     * 设备分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param name        查询名称
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult queryPageSmartDevices(int currentPage, int pageCount, String name) {
        PageUtils<SmartDevice> result = smartDeviceService.queryPageSmartDevices(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }


    @Override
    @DESRespondSecret(validated = true)
    public CommonResult deleteSmartDeviceById(int id) {

        SmartDevice data = smartDeviceService.getSmartById(id);

        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = smartDeviceService.deleteSmartDeviceById(id);

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

