package com.emergency.avenue.storage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.emergency.avenue.admin.exception.MyCustomException;
import com.emergency.avenue.common.core.constant.FieldParam;
import com.emergency.avenue.common.core.constant.TranslationDict;
import com.emergency.avenue.common.core.util.R;
import com.emergency.avenue.storage.api.request.DmmatypeRequest;
import com.emergency.avenue.storage.api.request.IrptDimWzdwRequest;
import com.emergency.avenue.storage.api.response.DmmatypeResponse;
import com.emergency.avenue.storage.api.response.IrkzqXxb1Response;
import com.emergency.avenue.storage.service.DmmatypeService;
import com.emergency.avenue.storage.util.MyFileUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 物资统计，列表
 * </P>
 *
 * @Author: wenpeng_qing
 * @Date: 2019-05-27 03:59
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/sys/dmmatype")
@Tag(description = "dmmatype", name = "物资统计，列表")
public class DmmatypeController {


	@Resource
	private DmmatypeService dmmatypeService;

	/**
	 * 简单分页查询
	 *
	 */
	@PostMapping("/list")
	@Operation(description = "物资类型树状图转列表", summary = "物资类型树状图转列表")
	public R<IPage<DmmatypeResponse>> getList(Page p,@RequestBody DmmatypeRequest dmmatypeRequest) {
		return R.ok(dmmatypeService.getList(p,dmmatypeRequest));
	}

	/**
	 * 应急物资分类-导出模板.xlsx
	 */
	@PostMapping("/exportyjwzflb")
	@Operation(description = "应急物资分类-导出excel", summary = "应急物资分类-导出excel")
	public void exportyjwzflb(Page p,@RequestBody DmmatypeRequest dmmatypeRequest,
							  HttpServletResponse response) {
		// 调用查询接口，参数传-1 查询全部，不分页
		p = new Page(-1,-1);
		IPage<DmmatypeResponse> iPage =  dmmatypeService.getList(p,dmmatypeRequest);
		String fileName = "exportyjwzflb.xlsx";
		String templateFile = "应急物资分类-导出模板.xlsx";
		try {
			dmmatypeService.exportyjwzflb(fileName,templateFile, iPage.getRecords(),response);
		} catch (MyCustomException e) {
			throw new RuntimeException("导出失败");
		}
	}

	/**
	 * 简单列表查询
	 *
	 */
	@PostMapping("/getAllList")
	@Operation(description = "物资价值统计表头", summary = "物资价值统计表头")
	public R<List<DmmatypeResponse>> getAllList(@RequestBody DmmatypeRequest dmmatypeRequest) {
		return R.ok(dmmatypeService.getAllList(dmmatypeRequest));
	}

	/**
	 * 统计分类库存-救灾数量价值
	 *
	 * @return 统计信息
	 */
	@PostMapping("/selectIrkzqXxb")
	@Operation(description = "物资价值统计列表", summary = "物资价值统计列表")
	public R<List<IrkzqXxb1Response>> selectIrkzqXxb(@RequestBody IrptDimWzdwRequest irptDimWzdwRequest) {
		List<IrkzqXxb1Response> list = dmmatypeService.selectIrkzqXxb(irptDimWzdwRequest);
		return R.ok(list);
	}

	/**
	 * 救灾物资数量价值表-导出模板.xlsx
	 */
	@PostMapping("/exportJzwzsljzb")
	@Operation(description = "救灾物资数量价值表导出excel", summary = "救灾物资数量价值表导出excel")
	public void exportJzwzsljzb(@RequestBody IrptDimWzdwRequest irptDimWzdwRequest,
								HttpServletResponse response) {
		String fileName = "救灾物资数量价值表.xlsx";
		String templateFile = "救灾物资数量价值表-导出模板.xlsx";
		try {
			dmmatypeService.selectIrkzqXxbExport(fileName,templateFile, irptDimWzdwRequest,response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("导出失败");
		}
	}
	/**
	 * 防汛抗旱物资数量价值表-导出模板.xlsx
	 */
	@PostMapping("/exportFxkhwzsljzb")
	@Operation(description = "防汛抗旱物资数量价值表导出excel", summary = "防汛抗旱物资数量价值表导出excel")
	public void exportFxkhwzsljzb(@RequestBody IrptDimWzdwRequest irptDimWzdwRequest,
								  HttpServletResponse response) {
		String fileName = "exportFxkhwzsljzb.xlsx";
		String templateFile = "防汛抗旱物资数量价值表-导出模板.xlsx";
		try {
			dmmatypeService.selectIrkzqXxbExport(fileName,templateFile, irptDimWzdwRequest,response);
		} catch (MyCustomException e) {
			throw new RuntimeException("导出失败");
		}
	}

	/**
	 * 简单分页查询
	 *
	 */
	@GetMapping("/test")
	@TranslationDict({
			@FieldParam(dictType = "province_ch", targetField = "userid"
			//		, targetFieldValueClazz = Integer.class
			)
	})
	public R test() {
		IPage<IrkzqXxb1Response> iPage = new Page<>();
		List<IrkzqXxb1Response> list = new ArrayList<>();
		IrkzqXxb1Response irkzqXxb1Response = new IrkzqXxb1Response();
		irkzqXxb1Response.setUserid("110000");
		list.add(irkzqXxb1Response);
		iPage.setRecords(list);
		return R.ok(iPage);
	}
}
