package com.pj.project.app_user;

import java.util.List;

import com.pj.api.dto.AppUserDto;

import com.pj.api.dto.EnterpriseAuditDto;
import com.pj.api.dto.EnterpriseDto;
import com.pj.api.dto.PeopleDto;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

import cn.dev33.satoken.annotation.SaCheckPermission;


/**
 * Controller: app_user -- 移动端账号
 * @author qzy
 */
@RestController
@RequestMapping("/AppUser/")
public class AppUserController {

	/** 底层 Service 对象 */
	@Autowired
	AppUserService appUserService;

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

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

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

	/** 改 */
	@RequestMapping("update")
	@SaCheckPermission(AppUser.PERMISSION_CODE_EDIT)
	public AjaxJson update(AppUser a){
		appUserService.update(a);
		return AjaxJson.getSuccess();
	}

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

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

	/**
	 * 更换角色
	 *
	 * 当前用户已登录，app点击该按钮时，从缓存获取用户类型与选择要更换的角色id做对比，相同则进入此方法，故在此不做用户角色权限过滤
	 * @param appRoleId
	 * @return
	 */
	@RequestMapping("updateRole")
	AjaxJson updateRole(@RequestParam("appRoleId")String appRoleId) {
		return appUserService.updateRole(appRoleId);
	}

	/*-----               正常业务接口👆👆    不越界     rpc远程调用 👇👇                     -----*/


	/** 启/停边民的app账号登陆限制 */
	@RequestMapping("rpc/isLock")
	public int isLock(@RequestParam("id") String id,
						   @RequestParam("type") Integer type,
						   @RequestParam("status") Integer status) {
		int lock = appUserService.isLock(id,type,status);
		return lock;
	}

	/** 查单个app有效用户 - 根据id */
	@RequestMapping("rpc/getById")
	public AppUserDto getAppUserById(@RequestParam("id") Long id){
		AppUserDto appUser = appUserService.getUserById(id);
		return appUser;
	}
	/** 商户认证 */
	@RequestMapping("rpc/audit")
	public boolean audit(@RequestBody EnterpriseAuditDto dto){
		AppUser user=new AppUser();
		BeanUtils.copyProperties(dto, user);
		System.out.println("EnterpriseAuditDto:"+dto.getFkId());
		return appUserService.updateById(user);
	}

	/**
	 * 收购商认证
	 */
	@RequestMapping("rpc/saveAppUserInfo")
	public Boolean saveAppUserInfo(@RequestBody AppUserDto appUser) {
		boolean info = appUserService.saveAppUserInfo(appUser);
		return info;
	}


	/**
	 * 同步航通边民信息时,自动给边民注册app账号
	 */
	@RequestMapping("rpc/generatePeopleAccount")
	public boolean generatePeopleAccount(@RequestBody PeopleDto peopleDto) throws Exception {
		boolean info = appUserService.generatePeopleAccount(peopleDto);
		return info;
	}


	/**
	 * 同步航通边民信息时,自动给外籍商户注册app账号
	 */
	@RequestMapping("rpc/generateEnterpriseAccount")
	public int generateEnterpriseAccount(@RequestBody EnterpriseDto enterpriseDto) throws Exception {
		int account = appUserService.generateEnterpriseAccount(enterpriseDto);
		return account;
	}


}
