package com.pj.tb_purchaser;

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

import com.pj.api.dto.PurchaserDto;
import com.pj.common.core.utils.poi.ExcelUtil;
import com.pj.tb_purchaser.param.PurchaserParam;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * Controller: tb_purchaser -- 收购商
 * @author qzy
 */
@RestController
@RequestMapping("/TbPurchaser/")
public class TbPurchaserController {

	/** 底层 Service 对象 */
	@Autowired
	TbPurchaserService tbPurchaserService;

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

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

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

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

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

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



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


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

	/**
	 * 导出
	 * @param keyword
	 * @return
	 */
	@RequestMapping("purchaserOutport")
	@ResponseBody
	public AjaxJson dataOutport(HttpServletResponse response, @RequestParam(value = "keyword",required = false) String keyword,@RequestParam(value = "filepath",required = false)String filepath){
		List<TbPurchaser> list = tbPurchaserService.list();
		ExcelUtil<TbPurchaser> tbPurchaserExcelUtil = new ExcelUtil<>(TbPurchaser.class);
		tbPurchaserExcelUtil.exportExcel(response,list,"测试");

		try {
			String importData = tbPurchaserService.outportExcel(keyword,filepath);
			AjaxJson.getSuccess(importData);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return AjaxJson.getError();
	}


	/**
	 *  收购商审核
	 * @param id 商户ID
	 * @param judgeStatus 审核状态(0=待审核,1=审核通过,2=审核驳回)
	 * @param judgeContent 审核内容,不通过时的备注
	 * @return
	 */
	@RequestMapping("audit")
	@SaCheckPermission(TbPurchaser.PERMISSION_CODE_EDIT)
	public AjaxJson audit(Long id,Integer judgeStatus,@RequestParam(value = "judgeContent",required = false) String judgeContent){
		boolean line = tbPurchaserService.audit(id,judgeStatus,judgeContent);
		if(line)return AjaxJson.getSuccess("认证完成!");
		return AjaxJson.getError("认证失败!");
	}
	@RequestMapping("auditBatch")
	@SaCheckPermission(TbPurchaser.PERMISSION_CODE_JUDGE)
	public AjaxJson auditBatch(){
		SoMap soMap=SoMap.getRequestSoMap();
		List<Long> ids=soMap.getListByComma("ids",Long.class);
		Integer judgeStatus=soMap.getInt("judgeStatus");
		String judgeContent=soMap.getString("judgeContent");
		tbPurchaserService.auditBatch(ids,judgeStatus,judgeContent);
		return AjaxJson.getSuccess();
	}

	/** 是否锁住收购商 */
	@RequestMapping("isLock")
	@SaCheckPermission(TbPurchaser.PERMISSION_CODE_EDIT)
	public AjaxJson isLock(@RequestBody TbPurchaserDto purchaserDto){

		int line = tbPurchaserService.isLock(purchaserDto);
		return AjaxJson.getByLine(line);
	}

	/*------            正常业务👆👆    rpc远程调用👇👇                         -----------*/
	/** 用户注册时,对应的表添加数据 */
	@RequestMapping("purchaserDto")
	public boolean purchaserDto(@RequestBody PurchaserDto purchaserDto){
		boolean b = tbPurchaserService.purchaserDto(purchaserDto);
		return b;
	}


}
