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

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.common.oauth.exception.ToflyDeniedException;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.trouble.dto.ToubletypeDto;
import com.tofly.pojo.trouble.vo.ToubletypeVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.pojo.trouble.Toubletype;

import com.tofly.gpsboot.service.trouble.ToubletypeService;
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 Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/toubletyped")
@Api(tags="隐患原因分类信息")
public class ToubletypeController extends BaseController<Toubletype, ToubletypeVo> {

    @Autowired
    private ToubletypeService toubletypeService;

    @Autowired
    public ToubletypeController(ToubletypeService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询隐患原因分类信息")
    public ResultRespone<Page<ToubletypeVo>> getToubletypePage(Page page, @ModelAttribute ToubletypeDto parDto) {
        ResultRespone<Page<ToubletypeVo>> rest = new ResultRespone<>();
        try {
            Page<ToubletypeVo> toubletypeVoPage = toubletypeService.getToubletypePage(page, parDto);
            rest.setResult(toubletypeVoPage);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增隐患原因分类信息")
    public ResultRespone<Object> saveToubletype(@Valid @RequestBody ToubletypeDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        super.validRest(rest, result);
        Toubletype toubletype = JSON.parseObject(JSON.toJSONString(parDto), Toubletype.class);
        //判断隐患名称重复

        int count = toubletypeService.count(Wrappers.<Toubletype>lambdaQuery().eq(Toubletype::getName, toubletype.getName()));
        if(count>0){
            rest.setCode(Constant.DATA_VALIDATION_CODE);
            rest.setMessage("隐患名称已存在，不能重复保存");
            throw  new ToflyDeniedException("隐患名称已存在，不能重复保存");
        }

        super.save(toubletype);

        return rest;

    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改隐患原因分类信息")
    public ResultRespone<Object> updateByIdToubletype(@Valid @RequestBody ToubletypeDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();


        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {

            Toubletype toubletype = JSON.parseObject(JSON.toJSONString(parDto), Toubletype.class);
            //判断隐患名称重复

            int count = toubletypeService.count(Wrappers.<Toubletype>lambdaQuery()
                    .eq(Toubletype::getName, toubletype.getName())
                    .ne(toubletype.getId()!=null,Toubletype::getId,toubletype.getId()));
            if(count>0){
                rest.setCode(Constant.DATA_VALIDATION_CODE);
                rest.setMessage("隐患名称已存在，不能重复保存");
                throw  new ToflyDeniedException("隐患名称已存在，不能重复保存");
            }

            try {
                super.updateById(JSON.parseObject(JSON.toJSONString(parDto), Toubletype.class));
            } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }

        }
        return rest;

    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "通过ID查询隐患原因分类信息")
    public ResultRespone<Toubletype> getByIdToubletype(@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> removeByIdListToubletype(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
            toubletypeService.deleteTroubleTypeInfo(ids);
        return rest;
    }
}
