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.BmFffs;
import com.tofly.feesapi.bmgl.mapper.BmFffsMapper;
import com.tofly.feesapi.bmgl.service.IBmFffsService;
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/bmfffs")
@Api(tags = "编码-付费方式接口")
public class BmFffsController {

    @Autowired
    private IBmFffsService IbmFffsService;
    @Autowired
    private BmFffsMapper bmFffsMapper;
    @Autowired
    private bmscyz bmscyz;

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

    @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 getBmFffsPage(Page page, String searchText) {
        QueryWrapper<BmFffs> queryWrapper = new QueryWrapper<BmFffs>();
        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.like("name", searchText)
                    .or()
                    .like("code", searchText)
                    .or()
                    .like("oname", searchText); //模糊查询
        }
        //queryWrapper.eq("isdisabled", "1");
        // 排序
        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<BmFffs> sfqdPage = IbmFffsService.page(page, queryWrapper);
        // 遍历查询结果进行赋值
        // List<BmSfqd> sfqdList = sfqdPage.getRecords();
        //水表厂家查询
        // List<BmFffs> sbcjList=IbmFffsService.list();
        // Map<String,String> map = new HashMap<>();
        // sbcjList.forEach(e->map.put(e.getCode().toString(), e.getName()));

        return ResultRespone.success(sfqdPage);
//        QueryWrapper<BmFffs> queryWrapper = new QueryWrapper<BmFffs>();
//        queryWrapper.like(StringUtils.isNotEmpty(bmFffs.getName()),"name",bmFffs.getName() )
//                .or()
//                .like(StringUtils.isNotEmpty(bmFffs.getOname()),"oname",bmFffs.getOname());
//        return ResultRespone.success(IbmFffsService.page(page, queryWrapper));
        //  return ResultRespone.success(IbmFffsService.page(page, Wrappers.query(bmFffs)));
    }


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

    /**
     * 新增付费方式
     *
     * @param bmFffs 付费方式
     * @return ResultRespone
     */
    @ApiOperation(value = "新增付费方式")
    @PostMapping
    public ResultRespone save(@RequestBody BmFffs bmFffs) {
        //添加
        BigDecimal num1 = new BigDecimal(1);
        bmFffs.setCode(bmFffsMapper.selectMaxId().add(num1).toString());
        return ResultRespone.success(IbmFffsService.save(bmFffs));
    }

    /**
     * 修改付费方式
     *
     * @param bmFffs 付费方式
     * @return ResultRespone
     */
    @ApiOperation(value = "修改付费方式")
    @PutMapping
    public ResultRespone updateById(@RequestBody BmFffs bmFffs) {
        return ResultRespone.success(IbmFffsService.updateById(bmFffs));
    }

    /**
     * 通过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.IsczSSPJsy("fffs", code.toString())) {
            return ResultRespone.failed("该收费方式已使用，不可删除");
        }
        return ResultRespone.success(IbmFffsService.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.IsczSSPJsy("fffs", ids)) {
            return ResultRespone.failed("该收费方式已使用，不可删除");
        }
        return ResultRespone.success(IbmFffsService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
