package com.zcxk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.common.StringUtils;
import com.zcxk.dto.AlarmSettingDto;
import com.zcxk.entity.AlarmSetting;
import com.zcxk.entity.SceneEntity;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.AlarmSettingService;
import com.zcxk.service.SceneService;
import com.zcxk.util.UserUtil;
import com.zcxk.util.OrgInfoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报警设置控制层
 *
 * @author yjy
 * @since 2020-11-17
 */
@RestController
@RequestMapping("/alarmSetting")
@Api(tags = "报警管理")
public class AlarmSettingController {
    /**
     * 服务对象
     */
    @Autowired
    private AlarmSettingService alarmSettingService;
    @Autowired
    private SceneService sceneService;
    @Autowired
    private OrgInfoUtil orgInfoUtil;
    /**
     * 删除多条数据
     *
     * @param ids
     * @return Response对象
     */
    @RequestMapping(value = "deleteByAlarmIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除报警配置信息")
    public AjaxMessage<Boolean> deleteByAlarmIds(@ApiParam(value = "报警配置ids", required = true) @RequestBody Long[] ids) {
        boolean result = alarmSettingService.deleteByAlarmIds(ids);
        //修改实时报警数据
        LoginUser currentUser = UserUtil.getCurrentUser();
        alarmSettingService.updateRealtimeAlarm(ids,currentUser.getUsername());
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 修改数据
     *
     * @param
     * @return Response对象
     */
    @RequestMapping(value = "updateByAlarmId", method = RequestMethod.POST)
    @ApiOperation(value = "修改报警配置信息")
    public AjaxMessage<Boolean> updateByAlarmId(@ApiParam(value = "报警配置信息", required = true) @RequestBody AlarmSetting alarmSetting) {
        if(alarmSetting.getId()==null) {
            return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
        }
        LoginUser currentUser = UserUtil.getCurrentUser();
        alarmSetting.setUpdateBy(currentUser.getUsername());
        boolean result = alarmSettingService.updateByAlarmId(alarmSetting);
        //修改实时报警数据
        alarmSettingService.updateRealtimeAlarm(new Long[]{alarmSetting.getId().longValue()},currentUser.getUsername());
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 增加数据
     *
     * @param
     * @return Response对象
     */
    @RequestMapping(value = "insertBatch", method = RequestMethod.POST)
    @ApiOperation(value = "批量增加报警配置信息",notes = "alarmType 为参数报警或状态报警")
    public AjaxMessage<Boolean> insertBatch(@ApiParam(value = "报警配置列表", required = true) @RequestBody List<AlarmSetting> listAlarmSetting) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        SceneEntity sceneEntity=new  SceneEntity();
        if(listAlarmSetting.size()>0){
             sceneEntity=sceneService.findParentSceneById(listAlarmSetting.get(0).getSceneId());
        }
        for (AlarmSetting alarm:listAlarmSetting) {
            if(!(alarm.getAlarmType().equals("参数报警")||alarm.getAlarmType().equals("状态报警"))||alarm.getDeviceId()==null||alarm.getSceneId()==null||
                    (alarm.getAlarmType().equals("参数报警")&&(alarm.getAttributeId()==null|| StringUtils.isEmpty(alarm.getAlarmCondition())||alarm.getAlarmValue()==null))||
                    (alarm.getAlarmType().equals("状态报警")&&(StringUtils.isEmpty(alarm.getAlarmCondition())||alarm.getAlarmValue()==null))){
                return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
            }
            alarm.setSceneName(sceneEntity.getSceneName());
            alarm.setParentSceneId(sceneEntity.getParentSceneId());
            alarm.setParentSceneName(sceneEntity.getParentSceneName());
            alarm.setCreateBy(currentUser.getUsername());
            alarm.setTenantId(currentUser.getTenantId());
            alarm.setStatus(1);
        }

        boolean result = alarmSettingService.insertBatch(listAlarmSetting);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 分页查询报警设置数据
     *
     * @param attributeName 报警参数
     * @param sceneId 场景id
     * @return
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页查询报警设置数据")
    public AjaxMessage<Pagination<AlarmSettingDto>> selectPage(
            @ApiParam(value = "页数", required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "条数", required = true)@RequestParam Integer pageSize,
            @ApiParam(value = "报警设置id", required = false) @RequestParam(required = false) Integer id,
            @ApiParam(value = "报警参数", required = false) @RequestParam(required = false) String attributeName,
            @ApiParam(value = "场景id", required = false) @RequestParam(required = false) Long sceneId) {
        AlarmSettingDto alarmSettingDto=new AlarmSettingDto();
        alarmSettingDto.setId(id);
        alarmSettingDto.setAttributeName(attributeName);
        alarmSettingDto.setSceneId(sceneId);
        alarmSettingDto.setSceneIds(sceneService.findByParentIdsLike(sceneId));
        LoginUser loginUser = UserUtil.getCurrentUser();
        alarmSettingDto.setTenantId(loginUser.getTenantId());
        alarmSettingDto.setProgramItems(loginUser.getProgramItemList());
        alarmSettingDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        alarmSettingDto.setPermissonType(loginUser.getPermissonType());

        IPage<AlarmSettingDto> iPage = new Page<>(pageNum, pageSize);
        iPage = alarmSettingService.selectPage(iPage, alarmSettingDto);
        Pagination<AlarmSettingDto> pages = new Pagination<>(iPage);
        for(AlarmSettingDto alarm : pages.getList()){
            alarm.setCompanyOrgName(orgInfoUtil.getOrgName(alarm.getCompanyOrgId()));
            alarm.setDeptOrgName(orgInfoUtil.getOrgName(alarm.getDeptOrgId()));
        }
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }
    /**
     * 根据设备id查询属性信息
     *
     * @param deviceId
     * @return
     */
    @RequestMapping(value = "selectAttributes", method = RequestMethod.GET)
    @ApiOperation(value = "根据设备id查询属性信息")
    public AjaxMessage<List<AlarmSettingDto>> selectAttributes(
            @ApiParam(value = "场景id", required = true)@RequestParam Integer sceneId,
            @ApiParam(value = "设备id", required = true)@RequestParam Integer deviceId) {
        if(deviceId==null||sceneId==null) {
            return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
        }
        List<AlarmSettingDto> result = alarmSettingService.selectAttributes(deviceId,sceneId);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
}