package com.pj.tb_trade_area;

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

import com.pj.common.core.utils.poi.ExcelUtil;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import com.pj.current.satoken.StpUserUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * Controller: tb_trade_area -- 贸易区域
 * @author qzy 
 */
@RestController
@RequestMapping("/TbTradeArea/")
public class TbTradeAreaController {

	/** 底层 Service 对象 */
	@Autowired
	TbTradeAreaService tbTradeAreaService;

	/** 增 */  
	@RequestMapping("add")
	@SaCheckPermission(TbTradeArea.PERMISSION_CODE_ADD)
	public AjaxJson add(TbTradeArea t){
		tbTradeAreaService.add(t);
		t = tbTradeAreaService.getById(SP.publicMapper.getPrimarykey());
		return AjaxJson.getSuccessData(t);
	}

	/** 删 */  
	@RequestMapping("delete")
	@SaCheckPermission(TbTradeArea.PERMISSION_CODE_DEL)
	public AjaxJson delete(Long id){
		 tbTradeAreaService.delete(id);
		return AjaxJson.getSuccess();
	}
	
	/** 删 - 根据id列表 */  
	@RequestMapping("deleteByIds")
	@SaCheckPermission(TbTradeArea.PERMISSION_CODE_DEL)
	public AjaxJson deleteByIds(){
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class); 
		int line = SP.publicMapper.deleteByIds(TbTradeArea.TABLE_NAME, ids);
		return AjaxJson.getByLine(line);
	}
	
	/** 改 */  
	@RequestMapping("update")
	@SaCheckPermission(TbTradeArea.PERMISSION_CODE_EDIT)
	public AjaxJson update(TbTradeArea t){
		tbTradeAreaService.update(t);
		return AjaxJson.getSuccess();
	}

	/** 查 - 根据id */  
	@RequestMapping("getById")
		@SaCheckPermission(TbTradeArea.PERMISSION_CODE)
	public AjaxJson getById(String id){
		TbTradeArea t = tbTradeAreaService.getById(id);
		return AjaxJson.getSuccessData(t);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	@RequestMapping("getList")
		@SaCheckPermission(TbTradeArea.PERMISSION_CODE)
	public AjaxJson getList() { 
		SoMap so = SoMap.getRequestSoMap();
		so.set("deleteStatus", 1);
		List<TbTradeArea> list = tbTradeAreaService.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}


	/**
	 *
	 *给互市区添加商品
	 * @author loovi
	 * @date
	 */
	@RequestMapping("addGoodsToTradeArea")
	@SaCheckPermission(TbTradeArea.PERMISSION_CODE_EDIT)
	public AjaxJson addGoodsToTradeArea(@RequestParam("tradeAreaId") Long tradeAreaId,@RequestParam("goodsIds") Long [] goodsIds){
		int  line = tbTradeAreaService.addGoodsToTradeArea(tradeAreaId,goodsIds);
		return AjaxJson.getByLine(line);
	}

	/**
	 *
	 *删除互市区中的商品
	 * @author loovi
	 * @date
	 */
	@RequestMapping("delGoodsFromTradeArea")
	@SaCheckPermission(TbTradeArea.PERMISSION_CODE_EDIT)
	public AjaxJson delGoodsFromTradeArea(@RequestParam("tradeAreaId") Long tradeAreaId,@RequestParam("goodsIds") Long [] goodsIds){
		int  line = tbTradeAreaService.delGoodsFromTradeArea(tradeAreaId,goodsIds);
		return AjaxJson.getByLine(line);
	}

	/**
	 * 数据导入接口
	 * @param file
	 * @return
	 */
	@RequestMapping("tradeAreaImport")
	@ResponseBody
	public AjaxJson dataImport( @RequestParam("file") MultipartFile file){

		String importData = null;
		try {
			importData = tbTradeAreaService.importData(file);
			return AjaxJson.getSuccess(importData);
		} catch (IOException e) {
			e.printStackTrace();
			return AjaxJson.getError();

		}
	}

	/**
	 * 导出
	 * @param keyword
	 * @return
	 */
	@RequestMapping("tradeAreaOutport")
	@ResponseBody
	public AjaxJson dataOutport(HttpServletResponse response,@RequestParam(value = "keyword",required = false) String keyword,@RequestParam(value = "filepath",required = false)String filepath){
//
//		List<TbTradeArea> areaList = tbTradeAreaService.list();
//		ExcelUtil<TbTradeArea> excelUtil = new ExcelUtil<>(TbTradeArea.class);
//		excelUtil.exportExcel(response,areaList,"区域表");
		try {
			String importData = tbTradeAreaService.outportExcel(keyword,filepath);
			AjaxJson.getSuccess(importData);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return AjaxJson.getError();
	}








}
