package com.tofly.feesapi.bmgl.controller;

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

import com.tofly.feesapi.bmgl.controller.common.bmscyz;
import com.tofly.feesapi.bmgl.entity.BmSflx;
import com.tofly.feesapi.bmgl.mapper.BmSflxMapper;
import com.tofly.feesapi.bmgl.service.BmSflxService;
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/bmsflx")
@Api(tags = "编码-收费类型接口")
public class BmSflxController {

    @Autowired
    private BmSflxService bmSflxService;
    @Autowired
    private BmSflxMapper bmSflxMapper;
    @Autowired
    private bmscyz bmscyz;

    @GetMapping("/list")
    @ApiOperation(value = "收费类型列表list")
    public ResultRespone getsflxList() {
        List<Map<String, Object>> list = bmSflxMapper.selectListMap();
        return ResultRespone.success(new CollectionData(list, list.size()));
    }

    /**
     * 分页查询
     *
     * @param page   分页对象
     * @param bmSflx 收费类型
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getBmSflxPage(Page page, @ModelAttribute BmSflx bmSflx) {
        return ResultRespone.success(bmSflxService.page(page, Wrappers.query(bmSflx)));
    }


    /**
     * 通过id查询收费类型
     *
     * @param code id
     * @return ResultRespone
     */
    @GetMapping("/{code}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "BigDecimal")
    })
    public ResultRespone getById(@PathVariable("code") BigDecimal code) {
        return ResultRespone.success(bmSflxService.getById(code));
    }

    /**
     * 新增收费类型
     *
     * @param bmSflx 收费类型
     * @return ResultRespone
     */
    @ApiOperation(value = "新增收费类型")
    @PostMapping
    public ResultRespone save(@RequestBody BmSflx bmSflx) {
        BigDecimal num1 = new BigDecimal(1);
        bmSflx.setCode(bmSflxMapper.selectMaxId().add(num1).toString());
        return ResultRespone.success(bmSflxService.save(bmSflx));
    }

    /**
     * 修改收费类型
     *
     * @param bmSflx 收费类型
     * @return ResultRespone
     */
    @ApiOperation(value = "修改收费类型")
    @PutMapping
    public ResultRespone updateById(@RequestBody BmSflx bmSflx) {
        return ResultRespone.success(bmSflxService.updateById(bmSflx));
    }

    /**
     * 通过id删除收费类型
     *
     * @param code id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID删除收费类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "BigDecimal")
    })
    @DeleteMapping("/{code}")
    public ResultRespone removeById(@PathVariable BigDecimal code) {
        if (bmscyz.IsczSSPJsy("sflx", code.toString())) {
            return ResultRespone.failed("收费类型已使用，不可删除");
        }
        return ResultRespone.success(bmSflxService.removeById(code));
    }

    /**
     * 通过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) {
        if (bmscyz.IsczSSPJsy("sflx", ids)) {
            return ResultRespone.failed("收费类型已使用，不可删除");
        }
        return ResultRespone.success(bmSflxService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
