package com.unis.crk.hardware.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unis.crk.hardware.entity.Devicecode;
import com.unis.crk.hardware.entity.vo.DevicecodeVo;
import com.unis.crk.hardware.service.IDevicecodeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.unis.crk.utils.ResultUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 设备编码表 前端控制器
 * </p>
 *
 * @author ghh
 * @since 2021-09-15
 */
@CrossOrigin
@Api(value = "设备编码表",tags = {"设备编码表"})
@RestController
@RequestMapping("devicecode")
public class DevicecodeController {

    private final static Logger LOGGER = LoggerFactory.getLogger(DevicecodeController.class);


    @Autowired
    private IDevicecodeService devicecodeService;


    /**
     *根据ID查询数据
     * @param devicecodeVo
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @PostMapping(value="/getOne")
    public ResultUtil getOne(@RequestBody DevicecodeVo devicecodeVo){
        LOGGER.info("入参为:{}",devicecodeVo.getId());
        ResultUtil<Devicecode> data = new ResultUtil<>();
        if (StringUtils.isEmpty(devicecodeVo.getId())){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.devicecodeService.getById(devicecodeVo.getId()));
        return data;
    }
    /**
     * 分页高级查询
     * @return
     */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/getDevicecode")
    public ResultUtil getDevicecodePage(@RequestBody DevicecodeVo devicecodeVo) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        Page<Devicecode> objectPage = new Page<>(devicecodeVo.getPageNum(), devicecodeVo.getPageSize());
        QueryWrapper<Devicecode> buttonInfoQueryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(devicecodeVo.getHIID())) {
            buttonInfoQueryWrapper.eq("d.HIID",devicecodeVo.getHIID());
        }
        IPage<Map<String, Object>> mapIPage  = devicecodeService.getDevicecodePage(objectPage, buttonInfoQueryWrapper);
        data.success(mapIPage);
        return data;
    }
    /**
     * 查询全部数据
     * @return
     */
    @ApiOperation(value="查询", notes="查询全部数据")
    @GetMapping("getAll")
    public ResultUtil getAll(){
        ResultUtil<List<Devicecode>> data = new ResultUtil<>();
        data.success(this.devicecodeService.list());
        return data;
    }


    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody Devicecode param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        data.success(this.devicecodeService.save(param));
        return data;
    }


    /**
     * 修改一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody Devicecode param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        data.success(this.devicecodeService.updateById(param));
        return data;
    }


    /**
     * 根据ID删除数据
     * @return
     */

    @ApiOperation(value="删除", notes="根据ID删除数据")
    @PostMapping(value="/delete")
    public ResultUtil delete(@RequestBody DevicecodeVo devicecodeVo){
        LOGGER.info("入参为:{}", devicecodeVo.getId());
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(devicecodeVo.getId())){
            data.failed("所接收id为null");
            return data;
        }
        data.success(this.devicecodeService.removeById(devicecodeVo.getId()));
        return data;
    }
    /**
     * 获取硬件类型下拉
     * @return
     */
    @ApiOperation(value="根据硬件类型获取硬件编码下拉", notes="根据硬件类型获取硬件编码下拉")
    @GetMapping(value="getDropDown/{HIID}")
    public ResultUtil getDropDown(@PathVariable("HIID") String HIID){
        ResultUtil<List> data = new ResultUtil<>();
        QueryWrapper<Devicecode> devicecodeQueryWrapper = new QueryWrapper<>();
        devicecodeQueryWrapper.eq("HIID",HIID);
        devicecodeQueryWrapper.select("DCID,DEVICECODE");
        List<Devicecode> list = devicecodeService.getDropDown(devicecodeQueryWrapper);
        data.success(list);
        return data;
    }

}

