package com.prolog.rdc.tms.controller.basicData;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.multipart.MultipartFile;

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.easyexcel.util.ExcelUtils;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCategoryCheckDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCategoryPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCategoryPopupDTO;
import com.prolog.rdc.tms.model.vo.ItemCategorySaveVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryAddVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryBatchDeleteVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryCheckVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryModifyVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryPagingQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCategoryWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.xlsimport.ItemCategoryImportVO;
import com.prolog.rdc.tms.service.basicData.ItemCategoryService;

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

/**
 *
 *
 * 基础数据-商品品类管理控制层
 *
 * @className :ItemCategoryController
 * @author :fanzw
 * @creationTime :2022/02/24
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/02/24
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Api(tags = "基础数据-商品品类管理控制层")
@RestController
@RequestMapping("/item_category")
public class ItemCategoryController {

	@Autowired
	private ItemCategoryService baseItemCategoryService;

	@UserLog("商品品类分页查询")
	@ApiOperation(value = "商品品类分页查询", notes = "商品品类分页查询")
	@PostMapping("/paging_query")
	public RestMessage<Page<ItemCategoryPagingQueryDTO>> pageItemCategoryExecute(@RequestBody ItemCategoryPagingQueryVO itemCategoryPagingQueryVO) {
		try {
			Page<ItemCategoryPagingQueryDTO> itemCategoryPagingQueryDTOs = baseItemCategoryService.pageItemCategoryData(itemCategoryPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", itemCategoryPagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品品类查看")
	@ApiOperation(value = "商品品类查看", notes = "商品品类查看")
	@PostMapping("/check")
	public RestMessage<ItemCategoryCheckDTO> getItemCategoryExecute(@RequestBody ItemCategoryCheckVO itemCategoryCheckVO) {
		try {
			ItemCategoryCheckDTO itemCategoryCheckDTO = baseItemCategoryService.getItemCategoryData(itemCategoryCheckVO);
			return RestMessage.newInstance(true, "查询成功", itemCategoryCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品品类弹窗")
	@ApiOperation(value = "商品品类弹窗", notes = "商品品类弹窗")
	@PostMapping("/popup")
	public RestMessage<Page<ItemCategoryPopupDTO>> popupItemCategoryExecute(@RequestBody ItemCategoryPopupVO itemCategoryPopupVO) {
		try {
			Page<ItemCategoryPopupDTO> itemCategoryPopupDTOs = baseItemCategoryService.popupItemCategoryData(itemCategoryPopupVO);
			return RestMessage.newInstance(true, "查询成功", itemCategoryPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品品类新增")
	@ApiOperation(value = "商品品类新增", notes = "商品品类新增")
	@PostMapping("/add")
	public RestMessage<String> addItemCategoryExecute(@RequestBody ItemCategoryAddVO itemCategoryAddVO) {
		try {
			baseItemCategoryService.addItemCategoryData(itemCategoryAddVO);
			return RestMessage.newInstance(true, "新增成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品品类修改")
	@ApiOperation(value = "商品品类修改", notes = "商品品类修改")
	@PostMapping("/modify")
	public RestMessage<String> modifyItemCategoryExecute(@RequestBody ItemCategoryModifyVO itemCategoryModifyVO) {
		try {
			baseItemCategoryService.modifyItemCategoryData(itemCategoryModifyVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品品类批量删除")
	@ApiOperation(value = "商品品类批量删除", notes = "商品品类批量删除")
	@PostMapping("/batch_delete")
	public RestMessage<String> batchDeleteItemCategoryExecute(@RequestBody List<ItemCategoryBatchDeleteVO> itemCategoryBatchDeleteVOs) {
		try {
			baseItemCategoryService.batchDeleteItemCategoryData(itemCategoryBatchDeleteVOs);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品品类是否启用")
	@ApiOperation(value = "商品品类是否启用", notes = "商品品类是否启用")
	@PostMapping("/whether_to_enable")
	public RestMessage<String> whetherEnableItemCategoryExecute(@RequestBody ItemCategoryWhetherEnableVO itemCategoryWhetherEnableVO) {
		try {
			baseItemCategoryService.whetherEnableItemCategoryData(itemCategoryWhetherEnableVO);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品品类导入")
	@ApiOperation(value = "商品品类导入", notes = "商品品类导入")
	@PostMapping("/importExcel")
	public RestMessage<Object> importItemCategoryExecute(@RequestParam("file") MultipartFile file, HttpServletRequest req) {
		try {

			baseItemCategoryService.importItemCategoryData(file);
			return RestMessage.newInstance(true, "导入成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品品类模板")
	@ApiOperation(value = "商品品类模板", notes = "商品品类模板")
	@PostMapping("/download_template")
	public RestMessage<Object> downloadTemplateExecute(HttpServletResponse response) throws IOException {
		ExcelUtils.export(response, ItemCategoryImportVO.class, "商品品类模板", null, null, null, true);
		return null;
	}

	@UserLog("根据商品品类名称查看商品品类资料")
	@ApiOperation(value = "根据商品品类名称查看商品品类资料", notes = "根据商品品类名称查看商品品类资料")
	@PostMapping("/by_name")
	public RestMessage<ItemCategoryCheckDTO> getItemCategoryByNameExecute(@RequestBody ItemCategoryCheckVO itemCategoryCheckVO) {
		try {
			ItemCategoryCheckDTO itemCategoryCheckDTO = baseItemCategoryService.getItemCategoryByNameData(itemCategoryCheckVO);
			return RestMessage.newInstance(true, "查询成功", itemCategoryCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品品类接口转TMS")
	@ApiOperation(value = "商品品类接口转TMS", notes = "商品品类接口转TMS")
	@PostMapping("/save/interface")
	public RestMessage<List<IntfErrorLogDTO>> saveItemCategoryInterfaceExecute(@RequestBody List<ItemCategorySaveVO> itemCategorySaveVOs) {
		try {
			List<IntfErrorLogDTO> intfErrorLogDTOs = baseItemCategoryService.saveItemCategoryInterfaceData(itemCategorySaveVOs);
			return RestMessage.newInstance(true, "保存成功", intfErrorLogDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
