/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.community.elevator.elevatoralarmsetting.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.community.elevator.elevator.entity.Elevator;
import org.springblade.community.elevator.elevator.service.IElevatorService;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.entity.Role;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.modules.system.service.IRoleService;
import org.springblade.system.orgposition.entity.OrgPosition;
import org.springblade.system.orgposition.service.IOrgPositionService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.community.elevator.elevatoralarmsetting.entity.AlarmSetting;
import org.springblade.community.elevator.elevatoralarmsetting.vo.AlarmSettingVO;
import org.springblade.community.elevator.elevatoralarmsetting.wrapper.AlarmSettingWrapper;
import org.springblade.community.elevator.elevatoralarmsetting.service.IAlarmSettingService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 电梯告警设置 控制器
 *
 * @author BladeX
 * @since 2021-03-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("community/elevator/alarmsetting")
@Api(value = "电梯告警设置", tags = "电梯告警设置接口")
public class AlarmSettingController extends BladeController {

	private IAlarmSettingService alarmSettingService;
	private IDictBizService dictService;
	private IOrgPositionService orgPositionService;
	private IRoleService roleService;
	private IElevatorService elevatorService;
	private IResidentialService residentialService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入alarmSetting")
	public R<AlarmSettingVO> detail(AlarmSetting alarmSetting) {
		AlarmSetting detail = alarmSettingService.getOne(Condition.getQueryWrapper(alarmSetting));
		return R.data(AlarmSettingWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 电梯告警设置
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入alarmSetting")
	public R<IPage<AlarmSettingVO>> list(AlarmSetting alarmSetting, Query query) {
		IPage<AlarmSetting> pages = alarmSettingService.page(Condition.getPage(query), Condition.getQueryWrapper(alarmSetting));
		return R.data(AlarmSettingWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 电梯告警设置
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入alarmSetting")
	public R<IPage<AlarmSettingVO>> page(AlarmSettingVO alarmSetting, Query query) {
		IPage<AlarmSettingVO> pages = alarmSettingService.selectAlarmSettingPage(Condition.getPage(query), alarmSetting);
		return R.data(pages);
	}

	/**
	 * 获取小区未配置的告警类型
	 */
	@GetMapping("/getAlarmTypeSelect/{residentialId}")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入alarmSetting")
	public R getAlarmTypeSelect(@PathVariable("residentialId") String residentialId) {
		List<DictBiz> list = alarmSettingService.getNoUseAlarmSetting(residentialId);
		return R.data(list);
	}

	/**
	 * 初始化数据 - 全局维度
	 */
	@GetMapping("/initData")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入alarmSetting")
	public R initData(BladeUser bladeUser) {

		LambdaQueryWrapper<DictBiz> yt_alarm_type = new QueryWrapper<>(new DictBiz()).lambda().eq(DictBiz::getCode, "yt_alarm_type").ne(DictBiz::getDictKey, -1);
		List<DictBiz> list = dictService.list(yt_alarm_type);
		List<AlarmSetting> settingList = new ArrayList<>();
		list.forEach(item -> {
			AlarmSetting alarmSetting = new AlarmSetting();
			LambdaQueryWrapper<AlarmSetting> eq = new QueryWrapper<>(new AlarmSetting()).lambda().eq(AlarmSetting::getAlarmType, item.getDictKey()).eq(AlarmSetting::getLevel, 1);
			AlarmSetting one = alarmSettingService.getOne(eq);
			// 若已经有的，则只做更新
			if (one != null){
				alarmSetting = one;
				alarmSetting.setAlarmName(item.getDictValue());
			}

			alarmSetting.setAlarmType(item.getDictKey());
			alarmSetting.setAlarmName(item.getDictValue());
			alarmSetting.setLevel(1); //全局配置的标识
			settingList.add(alarmSetting);
		});
		return R.status(alarmSettingService.saveOrUpdateBatch(settingList));
	}

	/**
	 * 初始化数据 - 小区维度
	 */
	@GetMapping("/initDataWithResidential/{residentialId}")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入residentialId")
	public R initDataWithResidential(@PathVariable("residentialId") Long residentialId, BladeUser bladeUser) {

//		List<Residential> residentialList = residentialService.getListWithYtelevatorXq();
		Residential residential = residentialService.getById(residentialId);

		LambdaQueryWrapper<DictBiz> ytAlarmTypeList = new QueryWrapper<>(new DictBiz()).lambda().eq(DictBiz::getCode, "yt_alarm_type").ne(DictBiz::getDictKey, -1);
		List<DictBiz> list = dictService.list(ytAlarmTypeList);
		List<AlarmSetting> settingList = new ArrayList<>();
		list.forEach(item -> {
			AlarmSetting alarmSetting = new AlarmSetting();
			LambdaQueryWrapper<AlarmSetting> wrapper = new QueryWrapper<>(new AlarmSetting()).lambda().eq(AlarmSetting::getAlarmType, item.getDictKey())
				.eq(AlarmSetting::getLevel, 2).eq(AlarmSetting::getResidentialId, residentialId);
			AlarmSetting one = alarmSettingService.getOne(wrapper);
			// 若已经有的，则只做更新
			if (one != null){
				alarmSetting = one;
			}
			alarmSetting.setResidentialId(residentialId);
			alarmSetting.setResidentialName(residential.getName());
			alarmSetting.setAlarmType(item.getDictKey());
			alarmSetting.setAlarmName(item.getDictValue());
			alarmSetting.setLevel(2); //小区配置的标识
			settingList.add(alarmSetting);
		});
		return R.status(alarmSettingService.saveOrUpdateBatch(settingList));
	}

	/**
	 * 新增 电梯告警设置
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入alarmSetting")
	public R save(@Valid @RequestBody AlarmSetting alarmSetting) {
		return R.status(alarmSettingService.saveOrUpdate(alarmSetting));
	}

	/**
	 * 修改 电梯告警设置
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入alarmSetting")
	public R update(@Valid @RequestBody AlarmSetting alarmSetting) {
		return R.status(alarmSettingService.updateById(alarmSetting));
	}

	/**
	 * 新增或修改 电梯告警设置
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入alarmSetting")
	public R submit(@Valid @RequestBody AlarmSetting alarmSetting) {
		if (alarmSetting.getResidentialId() != null){
			Residential residential = residentialService.getById(alarmSetting.getResidentialId());
			alarmSetting.setResidentialName(residential.getName());
		}
		return R.status(alarmSettingService.saveOrUpdate(alarmSetting));
	}


	/**
	 * 删除 电梯告警设置
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(alarmSettingService.deleteLogic(Func.toLongList(ids)));
	}


}
