package com.prolog.rdc.tms.controller.basicConfigure.xlsimport;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

import com.prolog.rdc.tms.model.vo.TemplateIdVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.product.core.framework.controller.BaseController;
import com.prolog.rdc.tms.model.dto.basicConfigure.StandardTemplateDetailsListDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.SysImportColDto;
import com.prolog.rdc.tms.model.dto.basicConfigure.SysImportTemplethPageDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.SysImportTemplethQueryHdDTO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysImportTemplethAddVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysImportTemplethPageVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysImportTemplethUpdateVO;
import com.prolog.rdc.tms.service.basicConfigure.SysImportTemplethService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *
 * 导入配置模板汇总 Controller
 *
 *
 * @className: SysImportTempletController
 * @author: kh
 * @creationTime: 2022-01-12 10:12:37
 * @description:
 * @modifier:
 * @modifyTime:
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@RestController
@Api(tags = "导入配置模板汇总前端控制器")
@RequestMapping("/sysimport/templet")
@Validated
public class SysImportTempletController extends BaseController {

	@Autowired
	private SysImportTemplethService sysImportTemplethService;

	@UserLog("根据主键查询汇总和明细")
	@ApiOperation(httpMethod = "GET", value = "根据主键查询汇总和明细")
	@GetMapping(value = "/queryHd/{id}")
	public RestMessage<SysImportTemplethQueryHdDTO> queryHd(@PathVariable("id") Long id) throws Exception {
		SysImportTemplethQueryHdDTO hd = sysImportTemplethService.queryHd(id);
		return RestMessage.newInstance(true, "查询成功", hd);
	}

	@UserLog("分页查询导入模板")
	@ApiOperation(httpMethod = "POST", value = "分页查询导入模板")
	@PostMapping(value = { "/listPage" })
	public RestMessage<Page<?>> listPage(@RequestBody SysImportTemplethPageVO sysImportTemplethPageVO) throws Exception {
		Page<SysImportTemplethPageDTO> list = sysImportTemplethService.listPage(sysImportTemplethPageVO);
		return RestMessage.newInstance(true, "查询成功", list);
	}

	@UserLog("新增汇总和明细")
	@ApiOperation(httpMethod = "POST", value = "新增汇总和明细", notes = "ID为空代表保存")
	@PostMapping(value = "/add")
	public RestMessage<String> add(@RequestBody @Valid SysImportTemplethAddVO sysImportTemplethAddVO) throws Exception {
		sysImportTemplethService.add(sysImportTemplethAddVO);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("更新汇总和明细")
	@ApiOperation(httpMethod = "POST", value = "更新汇总和明细", notes = "主键更新")
	@PostMapping(value = "/update")
	public RestMessage<String> update(@RequestBody @Valid SysImportTemplethUpdateVO sysImportTemplethUpdateVO) throws Exception {
		sysImportTemplethService.update(sysImportTemplethUpdateVO);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("删除汇总和明细")
	@ApiOperation(httpMethod = "POST", value = "删除汇总和明细", notes = "多个用','分隔放入主键IDS中")
	@PostMapping(value = "/delete")
	public RestMessage<String> delete(@RequestParam("ids") @Valid @NotEmpty String ids) throws Exception {
		sysImportTemplethService.delete(ids);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("开启或停用")
	@ApiOperation(httpMethod = "GET", value = "开启或停用", notes = "0停用，1启用")
	@GetMapping(value = "/enable/{id}/{status}")
	public RestMessage<String> enable(@PathVariable("id") Long id, @PathVariable("status") Integer status) throws Exception {
		sysImportTemplethService.enable(id, status);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("验证模板名称是否重复")
	@ApiOperation(httpMethod = "GET", value = "验证模板名称是否重复", notes = "name，模板名称")
	@GetMapping(value = "/validTemplete/{name}")
	public RestMessage<String> validTemplete(@PathVariable("name") String name) throws Exception {
		sysImportTemplethService.queryValidTemplteName(name, "");
		return RestMessage.newInstance(true, "名称不存在", null);
	}

	@UserLog("查询表头")
	@ApiOperation(httpMethod = "GET", value = "查询表头", notes = "查询表头")
	@GetMapping(value = "listColumns/{importObjNo}")
	public RestMessage<List<SysImportColDto>> listColumns(@PathVariable("importObjNo") String importObjNo) throws Exception {
		List<SysImportColDto> list = sysImportTemplethService.listColumns(importObjNo);
		return RestMessage.newInstance(true, "查询成功", list);
	}

	@UserLog("标准版模板详情查询")
	@ApiOperation(value = "标准版模板详情查询", notes = "标准版模板详情查询")
	@PostMapping(value = "/template_details")
	public RestMessage<List<StandardTemplateDetailsListDTO>> listStandardTemplateDetailsExecute(@RequestParam("importObjNo") String importObjNo) {
		List<StandardTemplateDetailsListDTO> standardTemplateDetailsListDTOs = sysImportTemplethService.listStandardTemplateDetailsData(importObjNo);
		return RestMessage.newInstance(true, "查询成功", standardTemplateDetailsListDTOs);
	}

	@UserLog("根据主键查询模板详情")
	@ApiOperation(value = "根据主键查询模板详情", notes = "根据主键查询模板详情")
	@PostMapping(value = "/templateDetail")
	public RestMessage<List<StandardTemplateDetailsListDTO>> templateDetail(@RequestParam("templateId") String templateId) throws Exception {
		return RestMessage.newInstance(true, "查询成功", sysImportTemplethService.templateDetail(templateId));
	}

	@UserLog("自定义模板下载")
	@ApiOperation(value = "自定义模板下载", notes = "自定义模板下载")
	@PostMapping("/downloadTemplateById")
	public void downloadTemplateById(@RequestBody TemplateIdVO vo, HttpServletResponse response) throws Exception {
		sysImportTemplethService.downloadTemplateById(vo.getTemplateId(), response);
	}
}
