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.BmSbsccj;
import com.tofly.feesapi.bmgl.mapper.BmSbsccjMapper;
import com.tofly.feesapi.bmgl.service.BmSbsccjService;
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/bmsbsccj")
@Api(tags="编码-水表生产厂家接口")
public class BmSbsccjController {

    @Autowired
    private  BmSbsccjService bmSbsccjService;
    @Autowired
    private BmSbsccjMapper bmSbsccjMapper;
    @Autowired
    private BmSbxhService bmSbxhService;
    @Autowired
    private bmscyz bmscyz;

    @GetMapping("/list")
    @ApiOperation(value = "水表生产厂家列表list")
    public ResultRespone getsbsccjList() {
        List<Map<String, Object>> list  = bmSbsccjMapper.selectListMap();
        return ResultRespone.success(new CollectionData(list, list.size()));
    }
    /**
    * 分页查询
    * @param page 分页对象
    * @param
    * @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 getBmSbsccjPage(Page page,String searchText,String isdisabled) {
        //条件构造
        QueryWrapper<BmSbsccj> queryWrapper = new QueryWrapper<BmSbsccj>();
        if(StringUtils.isNotBlank(searchText))
        {
            if(StringUtils.isNotBlank(isdisabled))
            {//like ‘%编码\水表厂家名称\水表类别%’
                queryWrapper.in("isdisabled",isdisabled)   //是否启用
                        .and(i->i.like("code",searchText).or().like("name",searchText).or()
                                .like("cjdz",searchText) //水表厂家地址
                                .or()
                                .like("czryxm",searchText));
            }
            else
            {
                queryWrapper.like("code",searchText)  //编码
                        .or()
                        .like("name",searchText) //水表厂家名称
                        .or()
                        .like("cjdz",searchText) //水表厂家地址
                        .or()
                        .like("czryxm",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<BmSbsccj> sbcjPage = bmSbsccjService.page(page,queryWrapper);
        // 遍历查询结果进行赋值
       // List<BmSbsccj> sbcjList = sbcjPage.getRecords();
        //水表类型查询
      //  List<BmSbxh> sblxList=bmSbxhService.list();
      // Map<String,String> map = new HashMap<>();
       // sblxList.forEach(e->map.put(e.getCode().toString(), e.getName()));

      // for (BmMeterFactory sbcj : sbcjList)
      //  {
            //水表类型
//             sbcj.setMeterType(map.get(sbcj.getMeterType()));
//             if(sbcj.getIsdisabled().equals("1"))
//             {
//                 sbcj.setIsdisabled("启用");
//             }
//             else {
//                 sbcj.setIsdisabled("禁用");
//             }
       // }
        return ResultRespone.success(sbcjPage);
    }


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

    /**
     * 新增水表生产厂家
     * @param bmSbsccj 水表生产厂家
     * @return ResultRespone
     */
    @ApiOperation(value = "新增水表生产厂家")
    @PostMapping
    public ResultRespone save(@RequestBody BmSbsccj bmSbsccj) {
        //添加
        BigDecimal num1=new BigDecimal(1);
        bmSbsccj.setCode(bmSbsccjMapper.selectMaxId().add(num1).toString());
        return ResultRespone.success(bmSbsccjService.save(bmSbsccj));

    }

    /**
     * 修改水表生产厂家
     * @param bmSbsccj 水表生产厂家
     * @return ResultRespone
     */
    @ApiOperation(value = "修改水表生产厂家")
    @PutMapping
    public ResultRespone updateById(@RequestBody BmSbsccj bmSbsccj) {
      return ResultRespone.success(bmSbsccjService.updateById(bmSbsccj));
    }

    /**
     * 通过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("sbsccj",code.toString())){
            return ResultRespone.failed("该水表生产厂家已使用，不可删除");
        }
      return ResultRespone.success(bmSbsccjService.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("sbsccj",ids)){
            return ResultRespone.failed("该水表生产厂家已使用，不可删除");
        }
        return ResultRespone.success(bmSbsccjService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
