package com.tofly.feesapi.bmgl.controller;

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


import com.tofly.feesapi.bmgl.controller.common.bmscyz;
import com.tofly.feesapi.bmgl.entity.SysChildCode;
import com.tofly.feesapi.bmgl.mapper.SysChildCodeMapper;
import com.tofly.feesapi.bmgl.service.SysChildCodeService;
import com.tofly.feesapi.common.model.CollectionData;
import lombok.AllArgsConstructor;
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;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/bmgl/syschildcode")
@Api(tags = "编码-子集编码表接口")
public class SysChildCodeController {

    @Autowired
    private SysChildCodeService sysChildCodeService;
    @Autowired
    private SysChildCodeMapper sysChildCodeMapper;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getSysChildCodePage(Page page, @ModelAttribute SysChildCode sysChildCode) {
        return ResultRespone.success(sysChildCodeService.page(page,
                Wrappers.query(sysChildCode).lambda()
                        .and(StringUtil.isNotEmpty(sysChildCode.getSearchText()), e ->
                                e.like(SysChildCode::getCcname, sysChildCode.getSearchText())
                                        .or()
                                        .like(SysChildCode::getPcname, sysChildCode.getSearchText()))
        ));
    }

    @GetMapping("/list")
    @ApiOperation(value = "list父级码表列表")
    public ResultRespone getSysCodeList() {
        //父级编码表列表
        List<Map<String, Object>> list = sysChildCodeMapper.selectListMapParentCode();
        return ResultRespone.success(new CollectionData(list, list.size()));
    }

    /**
     * list子级码表列表
     *
     * @param pcId
     * @return
     */
    @GetMapping("/list/{pcId}")
    @ApiOperation(value = "list子级码表列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pcId", value = "所属父级节点", required = true, dataType = "String"),

    })
    public ResultRespone getSysChildCodeList(@PathVariable("pcId") String pcId) {
        //子级码表列表
        List<Map<String, Object>> list = sysChildCodeMapper.selectListMapChildCode(pcId);
        return ResultRespone.success(new CollectionData(list, list.size()));
    }


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

    /**
     * 新增子集编码表
     *
     * @param sysChildCode 子集编码表
     * @return ResultRespone
     */
    @ApiOperation(value = "新增子集编码表")
    @PostMapping
    public ResultRespone save(@RequestBody SysChildCode sysChildCode) {
        //添加
        BigDecimal num1 = new BigDecimal(1);
        sysChildCode.setCcid(sysChildCodeMapper.selectMaxId().add(num1));
        return ResultRespone.success(sysChildCodeService.save(sysChildCode));
    }

    /**
     * 修改子集编码表
     *
     * @param sysChildCode 子集编码表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改子集编码表")
    @PutMapping
    public ResultRespone updateById(@RequestBody SysChildCode sysChildCode) {
        return ResultRespone.success(sysChildCodeService.updateById(sysChildCode));
    }

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

        return ResultRespone.success(sysChildCodeService.removeById(ccid));
    }

    /**
     * 通过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(sysChildCodeService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
