package com.pj.tb_goods_transit;

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

import com.pj.enummj.DeleteStatus;
import com.pj.tb_goods_transit.param.JudgeTransitParam;
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;


/**
 * Controller: tb_goods_transit -- 审核商户在APP端发布的商品
 * @author qzy
 */
@RestController
@RequestMapping("/TbGoodsTransit/")
public class TbGoodsTransitController {

	/** 底层 Service 对象 */
	@Autowired
	TbGoodsTransitService tbGoodsTransitService;

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

	/** 删 */
	@RequestMapping("delete")
//	@SaCheckPermission(TbGoodsTransit.PERMISSION_CODE_DEL)
	public AjaxJson delete(Long id){
		 tbGoodsTransitService.delete(id);
		return AjaxJson.getSuccess();
	}

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

	/** 改 */
	@RequestMapping("update")
//	@SaCheckPermission(TbGoodsTransit.PERMISSION_CODE_EDIT)
	public AjaxJson update(TbGoodsTransit t){
		tbGoodsTransitService.update(t);
		return AjaxJson.getSuccess();
	}

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

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



	/** 改 - 商品状态(0=下架,1=在售) */
	@RequestMapping("updateGoodsStatus")
//	@SaCheckPermission(TbGoodsTransit.PERMISSION_CODE_EDIT)
	public AjaxJson updateGoodsStatus(Long id, Integer value){
		int line = SP.publicMapper.updateColumnById(TbGoodsTransit.TABLE_NAME, "goods_status", value, id);
		return AjaxJson.getByLine(line);
	}

	/** 改 - 审核状态(0=待审核,1=通过,2=不通过) */
	@RequestMapping("updateAuditStatus")
//	@SaCheckPermission(TbGoodsTransit.PERMISSION_CODE_EDIT)
	public AjaxJson updateAuditStatus(Long id, Integer value){
		int line = SP.publicMapper.updateColumnById(TbGoodsTransit.TABLE_NAME, "audit_status", value, id);
		return AjaxJson.getByLine(line);
	}

	/** 改 - 删除状态(0=禁用,1=启用) */
	@RequestMapping("updateDelStatus")
//	@SaCheckPermission(TbGoodsTransit.PERMISSION_CODE_EDIT)
	public AjaxJson updateDelStatus(Long id, Integer value){
		int line = SP.publicMapper.updateColumnById(TbGoodsTransit.TABLE_NAME, "delete_status", value, id);
		return AjaxJson.getByLine(line);
	}

	/** 查看一级市场的在销/上架商品列表 */
	@GetMapping("getTransitList")
	public AjaxJson getTransitList(@RequestParam(value = "goodsType",required = false)String goodsType){
		SoMap so = SoMap.getRequestSoMap();
		so.put("deleteStatus" , DeleteStatus.DELETE_STATUS_ON.getCode());
		List<TbGoodsTransit> transitList = tbGoodsTransitService.getTransitList(so.startPage(),goodsType);
		return AjaxJson.getPageData(so.getDataCount(),transitList);
	}

	/** 审核上架的商品,可批量 */
	@GetMapping("judgeTransit")
	public AjaxJson judgeTransit(JudgeTransitParam judgeTransitParam){
		String judgedTransit = tbGoodsTransitService.judgeTransit(judgeTransitParam);
		return AjaxJson.getSuccess(judgedTransit);
	}


	/**
	 * 数据导入接口
	 * @param file
	 * @return
	 */
	@RequestMapping("goodsTransitImport")
	@ResponseBody
	public AjaxJson dataImport(@RequestParam("file") MultipartFile file){
		try {
			String importData = tbGoodsTransitService.importData(file);
			AjaxJson.getSuccess(importData);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return AjaxJson.getError();
	}

	/**
	 * 导出
	 * @param keyword
	 * @param filepath
	 * @return
	 */
	@RequestMapping("goodsTransitOutport")
	@ResponseBody
	public AjaxJson dataOutport(@RequestParam(value = "keyword",required = false) String keyword,
								@RequestParam(value = "filepath",required = false)String filepath){
		try {
			String importData = tbGoodsTransitService.outportExcel(keyword,filepath);
			AjaxJson.getSuccess(importData);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return AjaxJson.getError();
	}



}
