package com.tofly.feesapi.bmgl.controller;

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.BmCbpq;
import com.tofly.feesapi.bmgl.entity.dto.BmCbpqFilter;
import com.tofly.feesapi.bmgl.entity.dto.BmCbpqSelect;
import com.tofly.feesapi.bmgl.mapper.BmCbpqMapper;
import com.tofly.feesapi.bmgl.service.BmCbpqService;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("api/bmgl/bmcbpq")
@Api(tags = "抄表片区")
public class BmCbpqController {
    @Autowired
    private BmCbpqService bmCbpqService;
    @Autowired
    private BmCbpqMapper bmCbpqMapper;

    @ApiOperationSupport(order = 2)
    @GetMapping("")
    @ApiOperation(value = "查询抄表片区全部", response = BmCbpq.class, responseContainer = "List")
    public ResultRespone<List<BmCbpq>> getAll() {
        return ResultRespone.success(bmCbpqService.list());
    }

    @ApiOperationSupport(order = 4)
    @GetMapping("/page")
    @ApiOperation(value = "查询抄表片区(分页)")
    public ResultRespone<PageResult<BmCbpqSelect>> getPage(@ModelAttribute PageDto pageDto,
                                                           @ModelAttribute BmCbpqFilter filter) {
        return ResultRespone.success(bmCbpqService.getPage(pageDto, filter));
    }

    @ApiOperationSupport(order = 6)
    @GetMapping("/{cbpqbm}")
    @ApiOperation(value = "通过ID查询抄表片区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cbpqbm", value = "抄表片区编码", required = true, dataType = "String")
    })
    public ResultRespone<BmCbpq> getById(@PathVariable("cbpqbm") String cbpqbm) {
        return ResultRespone.success(bmCbpqService.getById(cbpqbm));
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "新增抄表片区")
    @PostMapping
    public ResultRespone<Boolean> save(@RequestBody BmCbpq bmCbpq) {
        BigDecimal num1 = new BigDecimal(1);
        bmCbpq.setCbpqbm(String.valueOf(bmCbpqMapper.selectMaxId().add(num1)));
        return ResultRespone.success(bmCbpqService.save(bmCbpq));
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "修改抄表片区")
    @PutMapping
    public ResultRespone<Boolean> updateById(@RequestBody BmCbpq bmCbpq) {
        Long userId = SecurityUtils.getUserId();
        bmCbpq.setXgry(String.valueOf(userId));
        return ResultRespone.success(bmCbpqService.updateById(bmCbpq));
    }

    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "删除抄表片区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cbpqbm", value = "抄表片区编码", required = true, dataType = "String")
    })
    @DeleteMapping("/{cbpqbm}")
    public ResultRespone<Boolean> removeById(@PathVariable String cbpqbm) {
        return ResultRespone.success(bmCbpqService.removeById(cbpqbm));
    }

    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "批量删除抄表片区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个抄表片区编码以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone<Boolean> removeByIds(String ids) {
        return ResultRespone.success(bmCbpqService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
