package com.pj.tb_enterprise;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pj.api.dto.EnterpriseDto;
import com.pj.project4sp.SP;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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


/**
 * Controller: tb_enterprise -- 商家
 * @author qzy
 */
@RestController
@RequestMapping("/TbEnterprise/")
public class TbEnterpriseController {


	/** 底层 Service 对象 */
	@Autowired
	TbEnterpriseService tbEnterpriseService;



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

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

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

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

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

	/** 查集合
	 *  - 根据条件（参数为空时代表忽略指定条件）查看审核状态列表
	 * @param judgeStatus 0=待审核 1=审核通过 2=审核不通过
	 * @param keyword 模糊查询关键字,非必填,后期可进行聚合模糊查询
	 * @return
	 */
	@RequestMapping("getList")
		@SaCheckPermission(TbEnterprise.PERMISSION_CODE)
	public AjaxJson getList(Integer judgeStatus,@RequestParam(value = "keyword",required = false)String keyword) {
		SoMap so = SoMap.getRequestSoMap();
		so.set("judge_status",judgeStatus);
		so.set("keyword",keyword);
		List<TbEnterprise> list = tbEnterpriseService.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

	@RequestMapping("getAll")
	@SaCheckPermission(TbEnterprise.PERMISSION_CODE)
	public AjaxJson getAll() {
		SoMap so = SoMap.getRequestSoMap();
		so.put("delete_status",1);
		List<TbEnterprise> list = tbEnterpriseService.getList(so);
		return AjaxJson.getSuccessData(list);
	}

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

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

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

	/**
	*商户审核
	*
	* @author loovi
	* @date
	*/
	@RequestMapping("audit")
//	@SaCheckPermission(TbEnterprise.PERMISSION_CODE_JUDGE)
	public AjaxJson audit(){
		SoMap soMap=SoMap.getRequestSoMap();
		Long id=soMap.getLong("id");
		Integer judgeStatus=soMap.getInt("judgeStatus");
		String judgeContent=soMap.getString("judgeContent");
		return AjaxJson.toAjax(tbEnterpriseService.audit(id,judgeStatus,judgeContent));
	}
	@RequestMapping("auditBatch")
//	@SaCheckPermission(TbEnterprise.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");
		tbEnterpriseService.auditBatch(ids,judgeStatus,judgeContent);
		return AjaxJson.getSuccess();
	}


	/**
	*是否锁住商户
	*
	* @author loovi
	* @date
	*/
	@RequestMapping("isLock")
	@SaCheckPermission(TbEnterprise.PERMISSION_CODE_EDIT)
	public AjaxJson isLock(@RequestBody  TbEnterpriseDto tbEnterpriseDto) {
		int line = tbEnterpriseService.isLock( tbEnterpriseDto);
		return AjaxJson.getByLine(line);
	}

	/*------            正常业务👆👆    rpc远程调用👇👇                         -----------*/

	/** 注册身份时对应的表添加数据 */
	@RequestMapping("rpc/enterpriseDto")
	public boolean enterpriseDto(@RequestBody EnterpriseDto enterpriseDto){
		boolean b = tbEnterpriseService.enterpriseDto(enterpriseDto);
		return b;
	}


	/**
	 * 远程调用: 根据电话查询外籍商户
	 */
	@RequestMapping("rpc/getEnterpriseDtoListByPhone")
	public List<com.pj.api.dto.EnterpriseDto> getEnterpriseDtoListByPhone(@RequestParam("phone") String phone){
		List<EnterpriseDto> byPhone = tbEnterpriseService.getEnterpriseDtoListByPhone(phone);
		return byPhone;
	}

}
