package cn.com.ty.lift.ud.templateBase.controller;

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.DelReq;
import cn.com.ty.lift.ud.templateBase.controller.query.MaintenanceSetQuery;
import cn.com.ty.lift.ud.templateBase.mapper.entity.MaintenanceSetEntity;
import cn.com.ty.lift.ud.templateBase.mapper.entity.MaintenanceSetVo;
import cn.com.ty.lift.ud.templateBase.service.IMaintenanceSetService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * 系统/企业模板管理 API
 *
 * @author Xson
 * @date 2020-04-29
 */
@Api(tags = "系统/企业模板管理 API- 小陶")
@RestController
@RequestMapping("/api/maintenanceSets")
public class MaintenanceSetController extends BaseController<MaintenanceSetEntity, MaintenanceSetQuery> {

    private final Logger log = LoggerFactory.getLogger(MaintenanceSetController.class);

    private @Autowired
    IMaintenanceSetService maintenanceSetService;

    @Override
    protected IService<MaintenanceSetEntity> getService() {
        return maintenanceSetService;
    }

    @ApiOperation(value = "批量启用")
    @PostMapping("/useBath")
    public RestResponse useBath(@RequestBody DelReq req) {
        maintenanceSetService.updateByIds(req.getIds(), 1);
        return RestResponse.success(null, "启用成功");
    }

    @ApiOperation(value = "批量关闭")
    @PostMapping("/noUseBath")
    public RestResponse noUseBath(@RequestBody DelReq req) {
        maintenanceSetService.updateByIds(req.getIds(), 0);
        return RestResponse.success(null, "关闭成功");
    }


    //根据id删除重写
    @DeleteMapping("/delete")
    @ApiOperation(value = "根据id删除")
    @Override
    public RestResponse delete(@RequestBody DelReq req) {
        maintenanceSetService.delete(req.getId());
        return RestResponse.success(null, "删除成功");
    }

    //根据id批量删除重写
    @DeleteMapping("/delete-batch")
    @ApiOperation(value = "批量删除")
    @Override
    public RestResponse deleteBath(@RequestBody DelReq req) {
        maintenanceSetService.deleteBatch(req.getIds());
        return RestResponse.success(null, "删除成功");
    }

    /**
     * 重写条件查询的方法
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("/query-page")
    @Override
    public RestResponse getCondition(@RequestBody MaintenanceSetQuery request) {
        if(request.getPageNum() < 1){
            return RestResponse.fail("当前页不能小于1");
        }else{
            return maintenanceSetService.selectByExample(request);
        }
    }

    /**
     * 新建
     * @param entity
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    @Override
    public RestResponse<MaintenanceSetEntity> create(@RequestBody MaintenanceSetEntity entity) {
        if(null == entity.getType()){
            return RestResponse.fail( "模板类型不能为空");
        }
        return maintenanceSetService.saveInfo(entity);

    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/query/{id}")
    @ApiOperation(value = "获取详情")
    @Override
    public RestResponse get(@PathVariable Long id) {
        MaintenanceSetVo entity = maintenanceSetService.getInfoById(id);
        if( null != entity){
            return RestResponse.success(entity, "查询成功");
        }else{
            return  RestResponse.fail(null, "查询失败");
        }

    }

    /**
     * 重写修改
     * @param request
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @Override
    public RestResponse update(@RequestBody MaintenanceSetEntity request) {
        return maintenanceSetService.updateInfoById(request);
    }
}