package org.springblade.modules.nh.controller;

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.common.utils.CommonUtil;
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.nh.excel.InefficientSettingImporter;
import org.springblade.modules.system.excel.UserExcel;
import org.springblade.modules.system.excel.UserImporter;
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.InefficientSettingEntity;
import org.springblade.modules.nh.pojo.vo.InefficientSettingVO;
import org.springblade.modules.nh.excel.InefficientSettingExcel;
import org.springblade.modules.nh.wrapper.InefficientSettingWrapper;
import org.springblade.modules.nh.service.IInefficientSettingService;
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.ArrayList;
import java.util.Map;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

/**
 * 低效电站设置 控制器
 *
 * @author AlexWang
 * @since 2024-06-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("nh/inefficientSetting")
@Tag(name = "低效电站设置", description = "低效电站设置接口")
public class InefficientSettingController extends BladeController {

	private final IInefficientSettingService inefficientSettingService;

	/**
	 * 低效电站设置 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description  = "传入inefficientSetting")
	public R<InefficientSettingVO> detail(InefficientSettingEntity inefficientSetting) {
		InefficientSettingEntity detail = inefficientSettingService.getOne(Condition.getQueryWrapper(inefficientSetting));
		return R.data(InefficientSettingWrapper.build().entityVO(detail));
	}
	/**
	 * 低效电站设置 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description  = "传入inefficientSetting")
	public R<IPage<InefficientSettingVO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> inefficientSetting, Query query) {
		IPage<InefficientSettingEntity> pages = inefficientSettingService.page(Condition.getPage(query), Condition.getQueryWrapper(inefficientSetting, InefficientSettingEntity.class));
		return R.data(InefficientSettingWrapper.build().pageVO(pages));
	}

	/**
	 * 低效电站设置 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description  = "传入inefficientSetting")
	public R<IPage<InefficientSettingVO>> page(InefficientSettingVO inefficientSetting, Query query) {
		IPage<InefficientSettingVO> pages = inefficientSettingService.selectInefficientSettingPage(Condition.getPage(query), inefficientSetting);
		return R.data(pages);
	}

	/**
	 * 低效电站设置 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description  = "传入inefficientSetting")
	public R save(@Valid @RequestBody InefficientSettingEntity inefficientSetting) {
		return R.status(inefficientSettingService.save(inefficientSetting));
	}

	/**
	 * 低效电站设置 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description  = "传入inefficientSetting")
	public R update(@Valid @RequestBody InefficientSettingEntity inefficientSetting) {
		return R.status(inefficientSettingService.updateById(inefficientSetting));
	}

	/**
	 * 低效电站设置 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description  = "传入inefficientSetting")
	public R submit(@Valid @RequestBody InefficientSettingEntity inefficientSetting) {

		//累加12个月的合计有效发电时间
		Double total = 0D;
		for(int i=1;i<=12;i++){
			Object val = CommonUtil.reflectGetField(inefficientSetting,"guardHourM"+i);
			if(val!=null){
				total += (Double) val;
			}
		}
		inefficientSetting.setGuardHourTotal(total);

		return R.status(inefficientSettingService.saveOrUpdate(inefficientSetting));
	}

	/**
	 * 低效电站设置 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "删除", description  = "传入ids")
	public R remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
		return R.status(inefficientSettingService.removeByIds(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-inefficientSetting")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入inefficientSetting")
	public void exportInefficientSetting(@Parameter(hidden = true) @RequestParam Map<String, Object> inefficientSetting, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<InefficientSettingEntity> queryWrapper = Condition.getQueryWrapper(inefficientSetting, InefficientSettingEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(InefficientSetting::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(InefficientSettingEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<InefficientSettingExcel> list = inefficientSettingService.exportInefficientSetting(queryWrapper);
		ExcelUtil.export(response, "低效电站设置数据" + DateUtil.time(), "低效电站设置数据表", list, InefficientSettingExcel.class);
	}


	/**
	 * 导入
	 */
	@PostMapping("import-inefficientSetting")
	@ApiOperationSupport(order = 12)
	@Operation(summary = "导入用户", description = "传入excel")
	public R importUser(MultipartFile file) {
		InefficientSettingImporter userImporter = new InefficientSettingImporter(inefficientSettingService,false);
		ExcelUtil.save(file, userImporter, InefficientSettingExcel.class);
		return R.success("操作成功");
	}

	/**
	 * 导出模板
	 */
	@GetMapping("export-template")
	@ApiOperationSupport(order = 14)
	@Operation(summary = "导出模板")
	public void exportUser(HttpServletResponse response) {
		List<InefficientSettingExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "低效电站配置数据模板", "低效电站配置数据表", list, InefficientSettingExcel.class);
	}
}
