package com.tofly.feesapi.bmgl.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tofly.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.bmgl.controller.common.bmscyz;
import com.tofly.feesapi.bmgl.entity.BmCbfs;
import com.tofly.feesapi.bmgl.mapper.BmCbfsMapper;
import com.tofly.feesapi.bmgl.service.BmCbfsService;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
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/bmcbfs")
@Api(tags = "编码-抄表方式接口")
public class BmCbfsController {

    @Autowired
    private BmCbfsService bmCbfsService;
    @Autowired
    private BmCbfsMapper bmCbfsMapper;
    @Autowired
    private bmscyz bmscyz;

    @GetMapping("/list")
    @ApiOperation(value = "抄表方列表list")
    public ResultRespone getFffsList() {
        List<Map<String, Object>> list = bmCbfsMapper.selectListMap();
        return ResultRespone.success(new CollectionData(list, list.size()));
    }

    /**
     * 分页查询
     *
     * @param page   分页对象
     * @param bmCbfs 抄表方式
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getBmCbfsPage(Page page, @ModelAttribute BmCbfs bmCbfs) {
        return ResultRespone.success(bmCbfsService.page(page, Wrappers.query(bmCbfs)));
    }

    @ApiOperationSupport(order = 4)
    @GetMapping("/pageList")
    @ApiOperation(value = "抄表方式(分页)")
    public ResultRespone<PageResult<BmCbfs>> getPage(@ModelAttribute PageDto pageDto,
                                                          @ModelAttribute BmCbfs filter) {


        return ResultRespone.success(bmCbfsService.getPage(pageDto, filter));
    }

    /**
     * 通过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(bmCbfsService.getById(code));
    }

    /**
     * 新增抄表方式
     *
     * @param bmCbfs 抄表方式
     * @return ResultRespone
     */
    @ApiOperation(value = "新增抄表方式")
    @PostMapping
    public ResultRespone save(@RequestBody BmCbfs bmCbfs) {
        return ResultRespone.success(bmCbfsService.save(bmCbfs));
    }

    /**
     * 修改抄表方式
     *
     * @param bmCbfs 抄表方式
     * @return ResultRespone
     */
    @ApiOperation(value = "修改抄表方式")
    @PutMapping
    public ResultRespone updateById(@RequestBody BmCbfs bmCbfs) {
        return ResultRespone.success(bmCbfsService.updateById(bmCbfs));
    }

    /**
     * 通过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.Isczcblrsy("cbfs", code.toString())) {
            return ResultRespone.failed("该抄表方式已被使用，不能删除");
        }
        return ResultRespone.success(bmCbfsService.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.Isczcblrsy("cbfs", ids)) {
            return ResultRespone.failed("该抄表方式已被使用，不能删除");
        }
        return ResultRespone.success(bmCbfsService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
