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.BmSbxh;
import com.tofly.feesapi.bmgl.mapper.BmSbxhMapper;
import com.tofly.feesapi.bmgl.service.BmSbxhService;
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/bmsbxh")
@Api(tags = "编码-水表型号接口")
public class BmSbxhController {
    @Autowired
    private BmSbxhService bmSbxhService;
    @Autowired
    private BmSbxhMapper bmSbxhMapper;
    @Autowired
    private bmscyz bmscyz;

    @GetMapping("/list")
    @ApiOperation(value = "水表型号列表list")
    public ResultRespone getsbxhList() {
        List<Map<String, Object>> list = bmSbxhMapper.selectListMap();
        return ResultRespone.success(new CollectionData(list, list.size()));
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @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 getBmSbxhPage(Page page, String searchText, String isdisabled) {
        QueryWrapper<BmSbxh> queryWrapper = new QueryWrapper<BmSbxh>();
        if (StringUtils.isNotBlank(searchText)) {

            if (StringUtils.isNotBlank(isdisabled)) {
                //like ‘%类型名称%’
                queryWrapper.in("ISDISABLED", isdisabled)   //是否启用
                        .and(i -> i.like("name", searchText).or()
                                .like("code", searchText)
                                .or()
                                .like("sbcj", searchText)
                                .or()
                                .like("sbdx", searchText)
                                .or()
                                .like("jccs", searchText)
                                .or()
                                .like("cyd", searchText));
            } else {
                queryWrapper.like("name", searchText)
                        .or()
                        .like("code", searchText)
                        .or()
                        .like("sbcj", searchText)
                        .or()
                        .like("sbdx", searchText)
                        .or()
                        .like("jccs", searchText)
                        .or()
                        .like("cyd", searchText)
                ;   //是否启用
            }
        } else {
            if (StringUtils.isNotBlank(isdisabled)) {
                queryWrapper.in("ISDISABLED", isdisabled);  //是否启用
            }
        }
        // 排序处理
        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<BmSbxh> sblbPage = bmSbxhService.page(page, queryWrapper);
        return ResultRespone.success(sblbPage);
    }


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

    /**
     * 新增水表型号
     *
     * @param bmSbxh 水表型号
     * @return ResultRespone
     */
    @ApiOperation(value = "新增水表型号")
    @PostMapping
    public ResultRespone save(@RequestBody BmSbxh bmSbxh) {
        //添加
        BigDecimal num1 = new BigDecimal(1);
        bmSbxh.setCode(bmSbxhMapper.selectMaxId().add(num1).toString());
        return ResultRespone.success(bmSbxhService.save(bmSbxh));

    }

    /**
     * 修改水表型号
     *
     * @param bmSbxh 水表型号
     * @return ResultRespone
     */
    @ApiOperation(value = "修改水表型号")
    @PutMapping
    public ResultRespone updateById(@RequestBody BmSbxh bmSbxh) {
        return ResultRespone.success(bmSbxhService.updateById(bmSbxh));
    }

    /**
     * 通过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("sbxh",code.toString()))
        {
            return ResultRespone.failed("该水表型号已使用，不能删除！");
        }
        return ResultRespone.success(bmSbxhService.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("sbxh",ids))
        {
            return ResultRespone.failed("该水表型号已使用，不能删除！");
        }
        return ResultRespone.success(bmSbxhService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
