package com.tofly.feesapi.bmgl.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.bmgl.controller.common.bmscyz;
import com.tofly.feesapi.bmgl.entity.BmCbb;
import com.tofly.feesapi.bmgl.entity.dto.BmCbbFilter;
import com.tofly.feesapi.bmgl.entity.dto.BmCbbSelect;
import com.tofly.feesapi.bmgl.entity.dto.BmCbbSelect2;
import com.tofly.feesapi.bmgl.mapper.BmCbbMapper;
import com.tofly.feesapi.bmgl.service.BmCbbService;
import com.tofly.feesapi.common.Excel.ExcleFileUtil;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
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 javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("api/bmgl/bmcbb")
@Api(tags = "编码-抄表本(册)")
public class BmCbbController {
    @Autowired
    private BmCbbService bmCbbService;
    @Autowired
    private BmCbbMapper bmCbbMapper;
    @Autowired
    private bmscyz bmscyz;


    @ApiOperationSupport(order = 2)
    @GetMapping("")
    @ApiOperation(value = "查询抄表本(册)全部", response = BmCbb.class, responseContainer = "List")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cbpqbm", value = "抄表片区编码", required = true)
    })
    public ResultRespone<List<BmCbb>> getAll(@RequestParam String cbpqbm) {
        return ResultRespone.success(bmCbbService.list(Wrappers.lambdaQuery(BmCbb.class).eq(BmCbb::getSscbpq, cbpqbm)));
    }

    @ApiOperationSupport(order = 4)
    @GetMapping("/page")
    @ApiOperation(value = "查询抄表本(册)(分页)")
    public ResultRespone<PageResult<BmCbbSelect>> getPage(@ModelAttribute PageDto pageDto,
                                                          @ModelAttribute BmCbbFilter filter) {
        return ResultRespone.success(bmCbbService.getPage(pageDto, filter));
    }

    @GetMapping("/exportlist")
    @ApiOperation(value = "导出查询抄表本(册)")
    public void exportYsqd(HttpServletResponse response, @ModelAttribute BmCbbFilter filter) throws IOException {
        List<BmCbbSelect> list = bmCbbService.cbblist(filter);
        if (!list.isEmpty()) {
            ExcleFileUtil.exportExcel(response, list, BmCbbSelect.class, "查询抄表本(册).xlsx", filter.getExportFields());
        }
    }

    @ApiOperationSupport(order = 5)
    @GetMapping("/page2")
    @ApiOperation(value = "查询抄表本(册)2(分页)", tags = "抄表算费")
    public ResultRespone<PageResult<BmCbbSelect2>> getPage2(@ModelAttribute PageDto pageDto,
                                                            @ModelAttribute BmCbbFilter filter) {
        return ResultRespone.success(bmCbbService.getPage2(pageDto, filter));
    }

    @ApiOperationSupport(order = 5)
    @GetMapping("/page3")
    @ApiOperation(value = "抄表本分配统计", tags = "普表抄表报表")
    public ResultRespone<PageResult<BmCbbSelect2>> getPage3(@ModelAttribute PageDto pageDto,
                                                            @ModelAttribute BmCbbFilter filter) {
        return ResultRespone.success(bmCbbService.getPage3(pageDto, filter));
    }

    @ApiOperationSupport(order = 6)
    @GetMapping("/{cbbbm}")
    @ApiOperation(value = "通过ID查询抄表本(册)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cbbbm", value = "抄表本(册)编码", required = true, dataType = "String")
    })
    public ResultRespone<BmCbb> getById(@PathVariable("cbbbm") String cbbbm) {
        BmCbb bmCbb = bmCbbService.getByIdNewly(cbbbm);
        return ResultRespone.success(bmCbb);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "新增抄表本(册)")
    @PostMapping
    public ResultRespone<Boolean> save(@RequestBody BmCbb bmCbb) {
        BigDecimal num1 = new BigDecimal(1);
        if(!StringUtils.isEmpty(bmCbb.getCbbbm())) {

            BmCbb exIscbb=bmCbbService.getById(bmCbb.getCbbbm());
            if(StringUtil.isNotNull(exIscbb)){
                return ResultRespone.failed("已检测重复抄表册编码");
            }
          //  bmCbb.setCbbbm(String.valueOf(bmCbbMapper.selectMaxId().add(num1)));
        }

        return ResultRespone.success(bmCbbService.save(bmCbb));
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "修改抄表本(册)")
    @PutMapping
    public ResultRespone<Boolean> updateById(@RequestBody BmCbb bmCbb) {
        return ResultRespone.success(bmCbbService.updateById(bmCbb));
    }

    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "删除抄表本(册)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cbbbm", value = "抄表本(册)编码", required = true, dataType = "BigDecimal")
    })
    @DeleteMapping("/{cbbbm}")
    public ResultRespone<Boolean> removeById(@PathVariable String cbbbm) {
        if (bmscyz.Isczkhsy("sscbb", cbbbm)) {
            return ResultRespone.failed("抄表本(册)已使用，不可删除");
        }
        return ResultRespone.success(bmCbbService.removeById(cbbbm));
    }

    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "批量删除抄表本(册)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个抄表本(册)编码以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone<Boolean> removeByIds(String ids) {
        if (bmscyz.Isczkhsy("sscbb", ids)) {
            return ResultRespone.failed("抄表本(册)已使用，不可删除");
        }
        return ResultRespone.success(bmCbbService.removeByIds(Arrays.asList(ids.split(","))));
    }


    @ApiOperationSupport(order = 15)
    @ApiOperation(value = "根据抄表人员获取抄表本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rybm", value = "查表人员编码", required = true, dataType = "String")
    })
    @GetMapping("/getCbbByRybm")
    public ResultRespone<Object> getCbbByRybm(String rybm) {
        // ResultRespone<Object> rs=new ResultRespone<Object>();
        if (StringUtils.isBlank(rybm)) {
            return ResultRespone.failed("抄表编码为空");
        }
        return ResultRespone.success(bmCbbService.getListCbbByRybm(rybm));
    }
}
