package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.BasDcsOperationLog;
import cn.cecep.talroad.domain.BasDeviceParam;
import cn.cecep.talroad.service.IBasDcsOperationLogService;
import cn.cecep.talroad.service.IBasDeviceParamService;
import cn.cecep.talroad.vo.BasDeviceParamVo;
import cn.cecep.talroad.vo.query.BasDcsOperationLogQuery;
import cn.cecep.talroad.vo.query.BasDeviceParamQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.List;


/**
 * * 开发公司：中节能天融科技有限公司
 * 各设备的监测参数属性Controller 控制层
 *
 * @author szhpt
 * @date 2023-01-10
 */
@Api(tags = "基础信息--各设备的监测参数属性")
@RestController
@RequestMapping("/basDeviceParam")
public class BasDeviceParamController extends BasesController {
    @Autowired
    private IBasDeviceParamService basDeviceParamService;
    @Autowired
    private IBasDcsOperationLogService basDcsOperationLogService;

    /**
     * 查询各设备的监测参数属性列表
     */
    @ApiOperation(value = "查询各设备的监测参数属性")
    @GetMapping("/list")
    public AjaxResults<IPage<BasDeviceParam>> list(BasDeviceParamQuery basDeviceParam) {
        Page<BasDeviceParam> page = new Page<>(basDeviceParam.getPageNum(), basDeviceParam.getPageSize());
        basDeviceParam.setIsStatus("1");
        IPage<BasDeviceParam> list = basDeviceParamService.page(page, BasDeviceParamQuery.createLambdaQueryWrapper(basDeviceParam));
        return AjaxResults.success(list);
    }


    /**
     * 导出各设备的监测参数属性列表
     */
    @ApiOperation(value = "导出各设备的监测参数属性")
    @Log(title = "各设备的监测参数属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasDeviceParam basDeviceParam) {
        basDeviceParam.setIsStatus("1");
        List<BasDeviceParam> list = basDeviceParamService.selectBasDeviceParamList(basDeviceParam);
        ExcelUtil<BasDeviceParam> util = new ExcelUtil<BasDeviceParam>(BasDeviceParam.class);
        util.exportExcel(response, list, "各设备的监测参数属性数据");
    }

    /**
     * 获取各设备的监测参数属性详细信息
     */
    @ApiOperation(value = "获取各设备的监测参数属性详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<BasDeviceParam> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(basDeviceParamService.getById(id));
    }

    /**
     * 新增各设备的监测参数属性
     */
    @ApiOperation(value = "新增各设备的监测参数属性")
    @Log(title = "各设备的监测参数属性", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody BasDeviceParam basDeviceParam) {
        return toAjax(basDeviceParamService.save(basDeviceParam));
    }

    /**
     * 修改各设备的监测参数属性
     */
    @ApiOperation(value = "修改各设备的监测参数属性")
    @Log(title = "各设备的监测参数属性", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody BasDeviceParamVo basDeviceParamVo) {
        if (ObjectUtil.isEmpty(basDeviceParamVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(basDeviceParamVo.getId())) {
            throw new InvalidParameterException("参数id为空");
        }
        BasDeviceParam basDeviceParam = basDeviceParamService.getById(basDeviceParamVo.getId());
        if (ObjectUtil.isEmpty(basDeviceParam)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", basDeviceParamVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(basDeviceParamVo, basDeviceParam);
        return toAjax(basDeviceParamService.saveOrUpdate(basDeviceParam));
    }

    /**
     * 批量修改各设备的监测参数属性
     */
    @ApiOperation(value = "批量修改各设备的监测参数属性")
    @Log(title = "批量各设备的监测参数属性", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editBatch")
    public AjaxResults<Boolean> editBatch(@RequestBody BasDeviceParamVo[] basDeviceParamVoArr) {
        if (basDeviceParamVoArr == null || basDeviceParamVoArr.length < 1) {
            throw new InvalidParameterException("参数为空");
        }

        basDeviceParamService.editBatch(basDeviceParamVoArr);
        return toAjax(true);
    }

    /**
     * 删除各设备的监测参数属性
     */
    @ApiOperation(value = "删除各设备的监测参数属性")
    @RequiresPermissions("talroad:param:remove")
    @Log(title = "各设备的监测参数属性", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable Long[] ids) {
        return toAjax(basDeviceParamService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 操作日志
     */
    @ApiOperation(value = "操作日志")
    @GetMapping("/editLogList")
    public AjaxResults<IPage<BasDcsOperationLog>> editLogList(BasDcsOperationLogQuery query) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            query.setFactoryId(factoryId);
        }
        Page<BasDcsOperationLog> page = new Page<>(query.getPageNum(), query.getPageSize());
        IPage<BasDcsOperationLog> list = basDcsOperationLogService.page(page, query.createLambdaQueryWrapper(query));
        return AjaxResults.success(list);
    }


    /**
     * 操作日志
     */
    @ApiOperation(value = "设置参数信息")
    @GetMapping("/setParamInfo")
    public AjaxResults<Integer> setParamInfo() {
            basDeviceParamService.setParamInfo();
        return AjaxResults.success(1);
    }

}
