package com.tofly.feesapi.bmgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
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.BmSblb;
import com.tofly.feesapi.bmgl.mapper.BmSblbMapper;
import com.tofly.feesapi.bmgl.service.BmSblbService;
import com.tofly.feesapi.common.model.CollectionData;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
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 java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("api/bmgl/bmsblb")
@Api(tags = "编码-水表类别接口")
public class BmSblbController {
    @Autowired
    private BmSblbService bmSblbService;
    @Autowired
    private BmSblbMapper bmSblbMapper;
    @Autowired
    private bmscyz bmscyz;

    @GetMapping("/list")
    @ApiOperation(value = "水表类别表list")
    public ResultRespone getsblbList() {
        List<Map<String, Object>> list = bmSblbMapper.selectListMap();
        return ResultRespone.success(new CollectionData(list, list.size()));
    }

    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param searchText 模糊查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页显示数据条数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pages", value = "共几页,输出参数", dataType = "int"),
            @ApiImplicitParam(name = "maxLimit", value = "每页最大显示数据", dataType = "int"),
            @ApiImplicitParam(name = "orders[0].asc", value = "排序,bool类型", dataType = "String"),
            @ApiImplicitParam(name = "orders[0].column", value = "排序,字段名", dataType = "String"),
            @ApiImplicitParam(name = "searchText", value = "模糊查询(编码，名称)", dataType = "String"),
            //@ApiImplicitParam(name = "isdisabled", value = "是否启用,0,1",  dataType = "String")
    })
    public ResultRespone getBmSblbPage(Page page, String searchText) {
        QueryWrapper<BmSblb> queryWrapper = new QueryWrapper<BmSblb>();
        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.like("name", searchText)
                    .or()
                    .like("code", searchText); //模糊查询
        }
        // 排序处理
        List<OrderItem> orders = page.getOrders();
        if (orders.isEmpty()) {
            orders.add(new OrderItem("clcode", true));
        } else {
            orders = orders.stream().map(item -> {
                OrderItem order = new OrderItem();
                if (item.getColumn().equals("code")) {
                    order.setColumn("clcode");
                }
                order.setAsc(item.isAsc());
                return order;
            }).collect(Collectors.toList());
        }
        page.setOrders(orders);
        // 执行分页查询
        IPage<BmSblb> sblbPage = bmSblbService.page(page, queryWrapper);
        return ResultRespone.success(sblbPage);
        //QueryWrapper<BmSblb> queryWrapper = new QueryWrapper<BmSblb>();
        // queryWrapper.like(StringUtils.isNotEmpty(bmSblb.getName()),"name",bmSblb.getName() );
        // return ResultRespone.success(bmSblbService.page(page, queryWrapper));
        //return ResultRespone.success(bmSblbService.page(page, Wrappers.query(bmSblb)));
    }


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

    /**
     * 新增水表类别
     *
     * @param bmSblb 水表类别
     * @return ResultRespone
     */
    @ApiOperation(value = "新增水表类别")
    @PostMapping
    public ResultRespone save(@RequestBody BmSblb bmSblb) {
        //添加
        BigDecimal num1 = new BigDecimal(1);
        bmSblb.setCode(bmSblbMapper.selectMaxId().add(num1).toString());
        return ResultRespone.success(bmSblbService.save(bmSblb));
    }

    /**
     * 修改水表类别
     *
     * @param bmSblb 水表类别
     * @return ResultRespone
     */
    @ApiOperation(value = "修改水表类别")
    @PutMapping
    public ResultRespone updateById(@RequestBody BmSblb bmSblb) {
        return ResultRespone.success(bmSblbService.updateById(bmSblb));
    }

    /**
     * 通过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.Isczcbsy("sblb", code.toString())) {
            return ResultRespone.failed("该水表类别已被使用，不可删除");
        }
        return ResultRespone.success(bmSblbService.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 removeById(String ids) {
        if (bmscyz.Isczcbsy("sblb", ids)) {
            return ResultRespone.failed("该水表类别已被使用，不可删除");
        }
        return ResultRespone.success(bmSblbService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
