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.basic.entity.UserInfo;
import com.unis.crk.hardware.entity.Hardwareloadometer;
import com.unis.crk.hardware.entity.Unithardware;
import com.unis.crk.hardware.entity.vo.HardwareloadometerVo;
import com.unis.crk.hardware.service.IHardwareloadometerService;
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;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  地磅 前端控制器
 * </p>
 *
 * @author ghh
 * @since 2021-09-15
 */
@Api(value = "地磅表",tags = {"地磅表"})
@RestController
@RequestMapping("hardwareloadometer")
@CrossOrigin
public class HardwareloadometerController {

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


    @Autowired
    private IHardwareloadometerService hardwareloadometerService;

    /**
     * 根据单位ID查询分页数据
     * @param hardwareloadometerVo
     * @return
     */
    @ApiOperation(value="查询", notes="pageNum,pageSize,UnitId分页高级查询")
    @PostMapping(value="/getHardwaredLoadometer")
    public  ResultUtil<IPage<Hardwareloadometer>> getHardwaredLoadometer(@RequestBody HardwareloadometerVo hardwareloadometerVo) {
        ResultUtil<IPage<Hardwareloadometer>> data = new ResultUtil<>();
        Page<Hardwareloadometer> objectPage = new Page<>(hardwareloadometerVo.getPageNum(), hardwareloadometerVo.getPageSize());

        QueryWrapper<Hardwareloadometer> hardwareloadometerQueryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(hardwareloadometerVo.getUnitId())){
            hardwareloadometerQueryWrapper.eq("UnitId",hardwareloadometerVo.getUnitId());
        }
        IPage<Hardwareloadometer> page = hardwareloadometerService.getHardwaredLoadometer(objectPage, hardwareloadometerQueryWrapper);
        data.success(page);
        return data;
    }

    /**
     * 根据ID查询数据
     * @return
     */
    @ApiOperation(value="查询", notes="根据sbbh查询数据")
    @PostMapping(value="/getOne")
    public ResultUtil getOne(@RequestBody HardwareloadometerVo hardwareloadometerVo){
        LOGGER.info("入参为:{}",hardwareloadometerVo.getSBBH());
        ResultUtil<Hardwareloadometer> data = new ResultUtil<>();
        if (StringUtils.isEmpty(hardwareloadometerVo.getSBBH())){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.hardwareloadometerService.getById(hardwareloadometerVo.getSBBH()));
        return data;
    }


    /**
     * 查询全部数据
     * @return
     */
    @ApiOperation(value="查询", notes="查询全部数据")
    @GetMapping("getAll")
    public ResultUtil getAll(){
        ResultUtil<List<Hardwareloadometer>> data = new ResultUtil<>();
        data.success(this.hardwareloadometerService.list());
        return data;
    }


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


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



    /**
     * 根据ID删除数据
     * @return
     */
    @ApiOperation(value="删除", notes="根据sbbh删除数据")
    @PostMapping(value="/delete")
    public ResultUtil delete(@RequestBody HardwareloadometerVo hardwareloadometerVo){
        LOGGER.info("入参为:{}", hardwareloadometerVo.getSBBH());
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StrUtil.isEmpty(hardwareloadometerVo.getSBBH())){
            data.failed("所接收id为null");
            return data;
        }
        data.success(this.hardwareloadometerService.removeById(hardwareloadometerVo.getSBBH()));
        return data;
    }

    /**
     * 根据单位id,工作机IP,查询已启用地磅信息
     * @return
     */
    @ApiOperation(value="根据单位id,工作机IP,查询已启用地磅信息", notes="根据单位id,工作机IP,是否启用.查询已启用地磅信息")
    @PatchMapping("getHardwareloadometerByUI")
    public ResultUtil getHardwareloadometerByUI(@RequestBody HardwareloadometerVo hardwareloadometerVo){
        ResultUtil<List<Hardwareloadometer>> data = new ResultUtil<>();
        String filterIp = hardwareloadometerVo.getFilterIp();
        if(StringUtils.isEmpty(filterIp)){
            data.failed("请填写IP标识");
            return data;
        }
        String isenable = hardwareloadometerVo.getIsenable();
        if(StringUtils.isEmpty(isenable)){
            isenable="1";
        }
        String unitId = hardwareloadometerVo.getUnitId();
        if(StringUtils.isEmpty(unitId)){
            data.failed("传入的参数为空");
            return data;
        }
        data.success(this.hardwareloadometerService.getHardwareloadometerByUI(unitId,filterIp,isenable));
        return data;
    }
}

