package org.springblade.modules.nh.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import jakarta.validation.Valid;

import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.material.pojo.entity.MaterialEntity;
import org.springblade.modules.nh.excel.PowerStationImport;
import org.springblade.modules.nh.excel.PowerStationImporter;
import org.springblade.modules.nh.pojo.entity.DeviceMsgEntity;
import org.springblade.modules.nh.pojo.entity.PowerStationEntity;
import org.springblade.modules.nh.pojo.entity.PowerStationInefficientEntity;
import org.springblade.modules.nh.service.IMonitorInverterDailyStatService;
import org.springblade.modules.nh.service.IPowerStationService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.nh.pojo.entity.DeviceEntity;
import org.springblade.modules.nh.pojo.vo.DeviceVO;
import org.springblade.modules.nh.excel.DeviceExcel;
import org.springblade.modules.nh.wrapper.DeviceWrapper;
import org.springblade.modules.nh.service.IDeviceService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.constant.RoleConstant;
import java.util.Map;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备管理 控制器
 *
 * @author AlexWang
 * @since 2024-05-31
 */
@RestController
@AllArgsConstructor
@RequestMapping("nh/device")
@Tag(name = "设备管理", description = "设备管理接口")
public class DeviceController extends BladeController {

	private final IDeviceService deviceService;

	private final IPowerStationService powerStationService;

	private final IMonitorInverterDailyStatService dailyStatService;

	/**
	 * 设备管理 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入device")
	public R<DeviceVO> detail(DeviceEntity device) {
		DeviceVO detail = deviceService.getDetail(device.getId());
		return R.data(detail);
	}

	/**
	 * 设备管理 详情
	 */
	@GetMapping("/getInverterGraph")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入device")
	public R getInverterGraph(String sn, String time) {
		return R.data(dailyStatService.getInverterGraph(sn,time));
	}

	/**
	 * 设备管理 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description  = "传入device")
	public R<IPage<DeviceVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> device, Query query) {
		if(device.get("stationCode") != null){
			PowerStationEntity entity = powerStationService.getStationDetailByPowCode(device.get("stationCode").toString());
			device.put("stationId",entity.getId());
			device.remove("stationCode");
		}
		IPage<DeviceEntity> pages = deviceService.page(Condition.getPage(query), Condition.getQueryWrapper(device, DeviceEntity.class));

		IPage<DeviceVO> result = DeviceWrapper.build().pageVO(pages);
		result.getRecords().forEach(vo->{
			deviceService.fillDetail(vo);
		});

		return R.data(result);
	}

	/**
	 * 设备管理 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入device")
	public R<IPage<DeviceVO>> page(DeviceVO device, Query query) {
		IPage<DeviceVO> pages = deviceService.selectDevicePage(Condition.getPage(query), device);
		return R.data(pages);
	}

	/**
	 * 设备管理 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description  = "传入device")
	public R save(@Valid @RequestBody DeviceEntity device) {
		return R.status(deviceService.save(device));
	}

	/**
	 * 设备管理 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description  = "传入device")
	public R update(@Valid @RequestBody DeviceEntity device) {
		return R.status(deviceService.updateById(device));
	}

	/**
	 * 设备管理 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description  = "传入device")
	public R submit(@Valid @RequestBody DeviceEntity device) {
		return R.status(deviceService.saveOrUpdate(device));
	}

	/**
	 * 设备管理 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "删除电站和设备", description  = "电站ids")
	public R remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {

		powerStationService.removeByIds(Func.toLongList(ids));
		LambdaUpdateWrapper<DeviceEntity> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.in(DeviceEntity::getStationId,Func.toLongList(ids))
			.set(DeviceEntity::getIsDeleted,1);
		return R.status(deviceService.update(updateWrapper));
	}

	/**
	 * 更新SN
	 */
	@GetMapping("/updateSn")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "更新SN", description  = "")
	public R updateSn(@Parameter(name = "主键集合", required = true) @RequestParam String id,
					  @Parameter(name = "SN编码", required = true) @RequestParam String sn) {
		deviceService.updateSn(Long.parseLong(id),sn);
		return R.success("更新成功");
	}

	//获取修改sn过程信息
	@GetMapping("/editSnMsg")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "获取修改sn过程信息",description = "")
	public R<List<DeviceMsgEntity>> editSnMsg(@Parameter(name = "设备id", required = true) @RequestParam String id){
		List<DeviceMsgEntity> list=deviceService.editSnMsg(id);
		return R.data(list);
	}

	/**
	 * 根棍首日发电时间
	 */
	@PostMapping("/updateGenDate")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "更新首日发电时间", description  = "")
	public R updateGenDate(@Parameter(name = "id",description = "电站id", required = true) @RequestParam String id,
						   @Parameter(name = "time",description = "日期", required = true) @RequestParam String time){

		return R.status(deviceService.updateGenDate(id, time));
	}

	/**
	 * 导出数据
	 */
	@GetMapping("/export-device")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入device")
	public void exportDevice(@Parameter(hidden = true) @RequestParam Map<String, Object> device, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<DeviceEntity> queryWrapper = Condition.getQueryWrapper(device, DeviceEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Device::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(DeviceEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<DeviceExcel> list = deviceService.exportDevice(queryWrapper);
		ExcelUtil.export(response, "设备管理数据" + DateUtil.time(), "设备管理数据表", list, DeviceExcel.class);
	}

	/**
	 * 导入历史电站
	 */
	@PostMapping("import-powerStation")
	@ApiOperationSupport(order = 13)
	@Operation(summary = "导入电站", description = "传入excel")
	public R importUser(MultipartFile file) {
		PowerStationImporter importer = new PowerStationImporter(deviceService,false);
		ExcelUtil.save(file, importer, PowerStationImport.class);
		return R.success("操作成功");
	}
}
