package org.springblade.modules.dateConf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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 org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
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.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.dateConf.entity.DateConfEntity;
import org.springblade.modules.dateConf.excel.DateConfExcel;
import org.springblade.modules.dateConf.service.IDateConfService;
import org.springblade.modules.dateConf.vo.DateConfVO;
import org.springblade.modules.dateConf.wrapper.DateConfWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * DateConf 控制器
 *
 * @author BladeX
 * @since 2023-10-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-dateConf/dateConf")
@Api(value = "DateConf", tags = "DateConf接口")
public class DateConfController extends BladeController {

	private final IDateConfService dateConfService;

	/**
	 * DateConf 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入dateConf")
	public R<DateConfVO> detail(DateConfEntity dateConf) {
		DateConfEntity detail = dateConfService.getOne(Condition.getQueryWrapper(dateConf));
		return R.data(DateConfWrapper.build().entityVO(detail));
	}

	/**
	 * DateConf 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入dateConf")
	public R<IPage<DateConfVO>> list(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		IPage<DateConfVO> pages = dateConfService.selectDateConfPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * DateConf 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入dateConf")
	public R save(@Valid @RequestBody DateConfEntity dateConf) {
		return R.status(dateConfService.save(dateConf));
	}

	/**
	 * DateConf 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入dateConf")
	public R update(@Valid @RequestBody DateConfEntity dateConf) {
		return R.status(dateConfService.updateById(dateConf));
	}

	/**
	 * DateConf 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入dateConf")
	public R submit(@Valid @RequestBody DateConfEntity dateConf) {
		return R.status(dateConfService.saveOrUpdate(dateConf));
	}

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

	/**
	 * 导入数据
	 */
	@PostMapping("/import-dateConf")
	public R importDateConf(MultipartFile file) {
		List<DateConfExcel> dateConfExcelList = ExcelUtil.read(file, DateConfExcel.class);
		List<DateConfEntity> dateConfEntityList = new ArrayList<>();
		for (DateConfExcel dateConfExcel : dateConfExcelList) {
			int num = dateConfService.isExist(dateConfExcel.getRq());
			if (num == 0) {
				DateConfEntity dateConf = new DateConfEntity();
				BeanUtil.copy(dateConfExcel, dateConf);
				dateConfEntityList.add(dateConf);
			} else {
				dateConfService.updateByRq(dateConfExcel);
			}
		}
		dateConfService.saveBatch(dateConfEntityList);
		return R.success("操作成功");
	}

	/**
	 * 导出数据
	 */
	@GetMapping("/export-dateConf")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入dateConf")
	public void exportDateConf(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		List<DateConfExcel> list = dateConfService.exportDateConf(params);
		ExcelUtil.export(response, "公休日配置", "公休日配置数据", list, DateConfExcel.class);
	}
}
