/*
 *      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.device.controller;

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

import org.springblade.community.entity.Residential;
import org.springblade.community.feign.IResidentialClient;
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.system.entity.DictBiz;
import org.springblade.system.feign.IDictBizClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.device.entity.ElevatorAlarmSetting;
import org.springblade.device.vo.ElevatorAlarmSettingVO;
import org.springblade.device.wrapper.ElevatorAlarmSettingWrapper;
import org.springblade.device.service.IElevatorAlarmSettingService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 电梯告警设置 控制器
 *
 * @author BladeX
 * @since 2021-04-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/elevatoralarmsetting")
@Api(value = "电梯告警设置", tags = "电梯告警设置接口")
public class ElevatorAlarmSettingController extends BladeController {

	private final IElevatorAlarmSettingService elevatorAlarmSettingService;
	private final IElevatorAlarmSettingService alarmSettingService;
	private final IDictBizClient dictBizClient;
	private final IResidentialClient residentialClient;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入elevatorAlarmSetting")
	public R<ElevatorAlarmSettingVO> detail(ElevatorAlarmSetting elevatorAlarmSetting) {
		ElevatorAlarmSetting detail = elevatorAlarmSettingService.getOne(Condition.getQueryWrapper(elevatorAlarmSetting));
		return R.data(ElevatorAlarmSettingWrapper.build().entityVO(detail));
	}

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


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

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

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

	/**
	 * 新增或修改 电梯告警设置
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入elevatorAlarmSetting")
	public R submit(@Valid @RequestBody ElevatorAlarmSetting elevatorAlarmSetting) {
		return R.status(elevatorAlarmSettingService.saveOrUpdate(elevatorAlarmSetting));
	}


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

	/**
	 * 获取小区未配置的告警类型
	 */
	@GetMapping("/getAlarmTypeSelect/{residentialId}")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入alarmSetting")
	public R getAlarmTypeSelect(@PathVariable("residentialId") String residentialId) {

		LambdaQueryWrapper<ElevatorAlarmSetting> eq = new QueryWrapper<ElevatorAlarmSetting>().lambda().eq(ElevatorAlarmSetting::getLevel, 2)
			.eq(ElevatorAlarmSetting::getResidentialId, Long.parseLong(residentialId));
		List<ElevatorAlarmSetting> settings = alarmSettingService.list(eq);
		List<String> alarmTypes = new ArrayList<>();
		settings.forEach(item -> {
			alarmTypes.add(item.getAlarmType());
		});

		List<ElevatorAlarmSetting> list = alarmSettingService.getNoUseAlarmSetting(alarmTypes);
		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);
		R<List<DictBiz>> yt_alarm_type = dictBizClient.getList("yt_alarm_type");
		List<DictBiz> list = yt_alarm_type.getData();
		List<ElevatorAlarmSetting> settingList = new ArrayList<>();
		list.forEach(item -> {
			ElevatorAlarmSetting alarmSetting = new ElevatorAlarmSetting();
			LambdaQueryWrapper<ElevatorAlarmSetting> eq = new QueryWrapper<>(new ElevatorAlarmSetting()).lambda().eq(ElevatorAlarmSetting::getAlarmType, item.getDictKey()).eq(ElevatorAlarmSetting::getLevel, 1);
			ElevatorAlarmSetting 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 = residentialClient.getById(residentialId);

//		LambdaQueryWrapper<DictBiz> ytAlarmTypeList = new QueryWrapper<>(new DictBiz()).lambda().eq(DictBiz::getCode, "yt_alarm_type").ne(DictBiz::getDictKey, -1);
		R<List<DictBiz>> yt_alarm_type = dictBizClient.getList("yt_alarm_type");
		List<DictBiz> list = yt_alarm_type.getData();
		List<ElevatorAlarmSetting> settingList = new ArrayList<>();
		list.forEach(item -> {
			ElevatorAlarmSetting alarmSetting = new ElevatorAlarmSetting();
			LambdaQueryWrapper<ElevatorAlarmSetting> wrapper = new QueryWrapper<>(new ElevatorAlarmSetting()).lambda().eq(ElevatorAlarmSetting::getAlarmType, item.getDictKey())
				.eq(ElevatorAlarmSetting::getLevel, 2).eq(ElevatorAlarmSetting::getResidentialId, residentialId);
			ElevatorAlarmSetting 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));
	}
}
