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

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

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

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.common.util.ToolKits;
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.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemByCargoOwnerPageDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemByEncodingNameDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCheckDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemPopupDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemUnitDTO;
import com.prolog.rdc.tms.model.vo.IntfItemSaveVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemAddVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemBatchDeleteVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemByCargoOwnerPageVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemByEncodingNameVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCheckVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCopyVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemModifyVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemPagingQueryVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemPopupVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.xlsimport.ItemImportVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.basicData.ItemService;

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

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

	@Autowired
	private ItemService itemService;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@UserLog("商品资料分页查询")
	@ApiOperation(value = "商品资料分页查询", notes = "商品资料分页查询")
	@PostMapping("/paging_query")
	public RestMessage<Page<ItemPagingQueryDTO>> pageItemExecute(@RequestBody ItemPagingQueryVO itemPagingQueryVO) {
		try {
			Page<ItemPagingQueryDTO> itemPagingQueryDTOs = itemService.pageItemData(itemPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", itemPagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品资料查看")
	@ApiOperation(value = "商品资料查看", notes = "商品资料查看")
	@PostMapping("/check")
	public RestMessage<ItemCheckDTO> getItemExecute(@RequestBody ItemCheckVO itemCheckVO) {
		try {
			ItemCheckDTO itemCheckDTO = itemService.getItemData(itemCheckVO);
			return RestMessage.newInstance(true, "查询成功", itemCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品资料弹窗")
	@ApiOperation(value = "商品资料弹窗", notes = "商品资料弹窗")
	@PostMapping("/popup")
	public RestMessage<Page<ItemPopupDTO>> popupItemExecute(@RequestBody ItemPopupVO itemPopupVO) {
		try {
			Page<ItemPopupDTO> itemPopupDTOs = itemService.popupItemData(itemPopupVO);
			return RestMessage.newInstance(true, "查询成功", itemPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品资料根据货主分页查询")
	@ApiOperation(value = "商品资料根据货主分页查询", notes = "商品资料根据货主分页查询")
	@PostMapping("/byCargoOwner/page")
	public RestMessage<Page<ItemByCargoOwnerPageDTO>> pageItemByCargoOwnerExecute(@RequestBody ItemByCargoOwnerPageVO itemByCargoOwnerPageVO) {
		try {
			Page<ItemByCargoOwnerPageDTO> itemByCargoOwnerPageDTOs = itemService.pageItemByCargoOwnerData(itemByCargoOwnerPageVO);
			return RestMessage.newInstance(true, "查询成功", itemByCargoOwnerPageDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品资料新增")
	@ApiOperation(value = "商品资料新增", notes = "商品资料新增")
	@PostMapping("/add")
	public RestMessage<String> addItemExecute(@RequestBody @Valid ItemAddVO itemAddVO) {
		try {
			itemService.addItemData(itemAddVO);
			return RestMessage.newInstance(true, "新增成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品资料复制")
	@ApiOperation(value = "商品资料复制", notes = "商品资料复制")
	@PostMapping("/copy")
	public RestMessage<String> copyItemExecute(@RequestBody @Valid ItemCopyVO itemCopyVO) {
		try {
			itemService.copyItemData(itemCopyVO);
			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> modifyItemExecute(@RequestBody @Valid ItemModifyVO itemModifyVO) {
		try {
			itemService.modifyItemData(itemModifyVO);
			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> batchDeleteItemExecute(@RequestBody List<ItemBatchDeleteVO> itemBatchDeleteVOs) {
		try {
			itemService.batchDeleteItemData(itemBatchDeleteVOs);
			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> whetherEnableItemExecute(@RequestBody ItemWhetherEnableVO itemWhetherEnableVO) {
		try {
			itemService.whetherEnableItemData(itemWhetherEnableVO);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("商品资料导入")
	@ApiOperation(value = "商品资料导入", notes = "商品资料导入")
	@PostMapping("/import")
	public RestMessage<Object> improtItemExecute(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws Exception {
		// 导入批次
		String importBatchNumber = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.IMPORT_BATCHNO, "L");
		ToolKits.isParamNull(importBatchNumber, "导入批次获取失败");
		itemService.improtItemData(file, importBatchNumber);
		return RestMessage.newInstance(true, "导入成功", null);
	}

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

	@UserLog("根据商品编码名称查询商品资料")
	@ApiOperation(value = "根据商品编码名称查询商品资料", notes = "根据商品编码名称查询商品资料")
	@PostMapping("/by_encoding_name")
	public RestMessage<ItemByEncodingNameDTO> getItemByEncodingNameExecute(@RequestBody ItemByEncodingNameVO itemByEncodingNameVO) {
		try {
			ItemByEncodingNameDTO itemByEncodingNameDTO = itemService.getItemByEncodingNameData(itemByEncodingNameVO);
			return RestMessage.newInstance(true, "查询成功", itemByEncodingNameDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据商品编码名称查询商品资料")
	@ApiOperation(value = "根据商品编码名称查询商品资料", notes = "根据商品编码名称查询商品资料")
	@PostMapping("/by_encoding_name_intf")
	public RestMessage<ItemByEncodingNameDTO> getItemByEncodingNameDataIntf(@RequestBody ItemByEncodingNameVO itemByEncodingNameVO) {
		try {
			ItemByEncodingNameDTO itemByEncodingNameDTO = itemService.getItemByEncodingNameDataIntf(itemByEncodingNameVO);
			return RestMessage.newInstance(true, "查询成功", itemByEncodingNameDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据商品ID查询单位重量体积")
	@ApiOperation(value = "根据商品ID查询单位重量体积", notes = "根据商品ID查询单位重量体积")
	@PostMapping("/by/itemId")
	public RestMessage<List<ItemUnitDTO>> listItemUnitExecute(@RequestBody List<BigDecimal> itemIds) {
		try {
			List<ItemUnitDTO> itemUnitDTOs = itemService.listItemUnitData(itemIds);
			return RestMessage.newInstance(true, "查询成功", itemUnitDTOs);
		} 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>> saveItemInterfaceExecute(@RequestBody List<IntfItemSaveVO> intfItemSaveVOs) {
		try {
			List<IntfErrorLogDTO> intfErrorLogDTOs = itemService.saveItemInterfaceData(intfItemSaveVOs);
			return RestMessage.newInstance(true, "保存成功", intfErrorLogDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("临时商品资料新增")
	@ApiOperation(value = "临时商品资料新增", notes = "临时商品资料新增")
	@PostMapping("/tempItem/add")
	public RestMessage<String> addTempItemExecute(@RequestBody @Valid ItemAddVO itemAddVO) {
		try {
			itemService.addTempItemData(itemAddVO);
			return RestMessage.newInstance(true, "新增成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据商品名称取商品资料")
	@ApiOperation(value = "根据商品名称取商品资料", notes = "根据商品名称取商品资料")
	@PostMapping("/by_name")
	public RestMessage<ItemCheckDTO> getItemByNameExecute(@RequestBody ItemCheckVO itemCheckVO) {
		try {
			ItemCheckDTO itemCheckDTO = itemService.getItemByNameData(itemCheckVO);
			return RestMessage.newInstance(true, "查询成功", itemCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
