package com.tofly.feesapi.bmgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.bmgl.entity.SysParentCode;
import com.tofly.feesapi.bmgl.mapper.SysParentCodeMapper;
import com.tofly.feesapi.bmgl.service.SysParentCodeService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 java.math.BigDecimal;
import java.util.Arrays;

@RestController
@RequestMapping("api/bmgl/sysparentcode")
@Api(tags="编码-父级码表接口")
public class SysParentCodeController {

    @Autowired
    private  SysParentCodeService sysParentCodeService;
    @Autowired
    private  SysParentCodeMapper sysParentCodeMapper;

    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getSysParentCodePage(Page page, @ModelAttribute SysParentCode sysParentCode) {
        QueryWrapper<SysParentCode> queryWrapper = new QueryWrapper<SysParentCode>();
        queryWrapper.like(StringUtils.isNotEmpty(sysParentCode.getPcname()),"pcname",sysParentCode.getPcname() );
        return ResultRespone.success(sysParentCodeService.page(page,queryWrapper));
      //return ResultRespone.success(sysParentCodeService.page(page, Wrappers.query(sysParentCode)));
    }


    /**
     * 通过id查询父级码表
     * @param pcid id
     * @return ResultRespone
     */
    @GetMapping("/{pcid}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pcid", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("pcid" ) Long pcid) {
      return ResultRespone.success(sysParentCodeService.getById(pcid));
    }

    /**
     * 新增父级码表
     * @param sysParentCode 父级码表
     * @return ResultRespone
     */
    @ApiOperation(value = "新增父级码表")
    @PostMapping
    public ResultRespone save(@RequestBody SysParentCode sysParentCode) {
        BigDecimal num1=new BigDecimal(1);
        sysParentCode.setPcid(sysParentCodeMapper.selectMaxId().add(num1));
      return ResultRespone.success(sysParentCodeService.save(sysParentCode));
    }

    /**
     * 修改父级码表
     * @param sysParentCode 父级码表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改父级码表")
    @PutMapping
    public ResultRespone updateById(@RequestBody SysParentCode sysParentCode) {
      return ResultRespone.success(sysParentCodeService.updateById(sysParentCode));
    }

    /**
     * 通过id删除父级码表
     * @param pcid id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID删除父级码表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pcid", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{pcid}" )
    public ResultRespone removeById(@PathVariable Long pcid) {

      int sl=  sysParentCodeMapper.selectCount(pcid);
      if(sl>0)
      {
          return ResultRespone.failed("子级已存在");
      }
      return ResultRespone.success(sysParentCodeService.removeById(pcid));
    }

    /**
     * 通过id批量删除父级码表
     * @param ids id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID批量删除父级码表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeByIds(String ids) {
        return ResultRespone.success(sysParentCodeService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
