/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.gpsboot.controller.maintain;

import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;

import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.build.vo.BulidclearVo;
import com.tofly.pojo.maintain.dto.RepairtypeDto;
import com.tofly.pojo.maintain.vo.RepairtypeVo;

import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.maintain.RepairtypeService;
import com.tofly.pojo.maintain.Repairtype;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.validation.Valid;

import java.util.Optional;

/**
 * 维修类型信息表
 *
 * @author admin
 * @date Tue Apr 06 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/repairtype")
@Api(tags="维修类型信息表接口")
public class RepairtypeController extends BaseController<Repairtype, RepairtypeVo> {
    @Autowired
    private RepairtypeService service;
    @Autowired
    public RepairtypeController(RepairtypeService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询维修类型信息")
    public ResultRespone<Page<RepairtypeVo>> getRepairtypePage(Page page, @ModelAttribute RepairtypeDto parDto) {
        ResultRespone<Page<RepairtypeVo>> rest = new ResultRespone<>();
        Page<RepairtypeVo> buildPage = service.getRepairTypePage(page, parDto);
        rest.setResult(buildPage);
        return rest;
    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增维修类型信息")
    public ResultRespone<Object> saveRepairtype(@Valid @RequestBody RepairtypeDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if(!checkIsExit(parDto,0L)){
          return ResultRespone.failed("数据已存在，不能重复添加");
        }else{
        return Optional.ofNullable(super.validRest(rest, result)).orElseGet(() -> super.save(JSON.parseObject(JSON.toJSONString(parDto), Repairtype.class)));
        }
    }

    public  boolean checkIsExit(RepairtypeDto parDto,Long id){
       Integer count=service.count(Wrappers.<Repairtype>lambdaQuery()
               .eq(Repairtype::getName,parDto.getName())
               .eq(Repairtype::getGroupName,parDto.getGroupName())
               .eq(Repairtype::getUnit,parDto.getUnit())
               .ne(Repairtype::getId,id));
        return count>0?false:true;
    }
    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改维修类型信息")
    public ResultRespone<Object> updateByIdRepairtype(@Valid @RequestBody RepairtypeDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if(!checkIsExit(parDto,parDto.getId())){
            return ResultRespone.failed("数据已存在，不能修改");
        }else {
            return Optional.ofNullable(super.validRest(rest, result)).orElseGet(() -> super.updateById(JSON.parseObject(JSON.toJSONString(parDto), Repairtype.class)));
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "通过ID查询维修类型信息")
    public ResultRespone<Repairtype> getByIdRepairtype(@PathVariable Long id) {
        return super.getById(id);
    }



    @ApiOperation(value = "通过ID批量删除基本信息对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    @ToFlyAppLog(title = "通过ID批量删除维修类型信息")
    public ResultRespone<Object> removeByIdListRepairtype(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            service.deleteRepairTypeInfo(ids);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }
}
