/*
 * Author: Zev
 * 生成controller
 */
package com.tofly.gpsboot.controller.plan.config;

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.common.Constant;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.plan.dto.InspecttypeDto;

import com.tofly.pojo.plan.vo.InspecttypeVo;

import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.pojo.plan.Inspecttype;

import com.tofly.gpsboot.service.config.InspecttypeService;
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.List;
import java.util.Optional;

/**
 * 巡检类型配置信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/inspecttype")
@Api(tags = "巡检类型配置信息")
public class InspecttypeController {

    @Autowired
    private InspecttypeService service;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询巡检类型配置信息")
    public ResultRespone<Page<Inspecttype>> getInspecttypePage(Page page, @ModelAttribute InspecttypeDto parDto) {
        ResultRespone<Page<Inspecttype>> rest = new ResultRespone<>();
        try {
            Page<Inspecttype> inspectTypePage = service.getInspectTypePage(page, parDto);
            rest.setResult(inspectTypePage);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;


    }

    @GetMapping("/getByGroupInspectType")
    @ApiOperation(value = "根据归属查询对应巡检类型")
    @ToFlyAppLog(title = "根据归属查询对应巡检类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupCode", value = "groupCode 码表pcid52000 code 1为管线巡检 2设备维护", required = true, dataType = "String")
    })
    public ResultRespone<List<Inspecttype>> getByGroupInspectType(String groupCode) {
        ResultRespone<List<Inspecttype>> rest = new ResultRespone<>();
        try {
            List<Inspecttype> inspectTypePage = service.getByGroupInspectType(groupCode);
            rest.setResult(inspectTypePage);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;


    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增巡检类型配置信息")
    public ResultRespone<Object> saveInspecttype(@Valid @RequestBody InspecttypeDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回

            try {
                if(!checkIsExit(parDto,0L)){
                    return ResultRespone.failed("巡检类别，类型名称，图层名称重复，不能添加");
               }else{
                  boolean save = service.save(JSON.parseObject(JSON.toJSONString(parDto), Inspecttype.class));
                  rest.setResult(save);
                }
            } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }

        return rest;

    }

    public  boolean checkIsExit(InspecttypeDto parDto,Long id){
      Integer count=service.count(
                Wrappers.<Inspecttype>lambdaQuery()
                        .eq(Inspecttype::getName,parDto.getName())
                        .eq(Inspecttype::getGroupCode,parDto.getGroupCode())
                        .eq(Inspecttype::getFlag,"1")
                        .ne(Inspecttype::getId,id)
        );
           if(count>0){
               return false;
           }
        return true;
    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改巡检类型配置信息")
    public ResultRespone<Object> updateByIdInspecttype(@Valid @RequestBody InspecttypeDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回

            try {
                if(!checkIsExit(parDto,parDto.getId())){

                    return ResultRespone.failed("巡检类别，类型名称，图层名称重复，不能修改");

                }else{

                boolean b = service.updateById(JSON.parseObject(JSON.toJSONString(parDto), Inspecttype.class));
                rest.setResult(b);
                }
            } 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<Inspecttype> getByIdInspecttype(@PathVariable Long id) {
        ResultRespone<Inspecttype> rest = new ResultRespone<>();
        try {
            rest.setResult(service.getById(id));
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }


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


}
