package com.pj.tb_group;

import java.io.IOException;
import java.util.List;
import com.pj.api.dto.GroupDto;
import com.pj.api.dto.HtGroupDto;
import com.pj.utils.so.SoMap;
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;
import org.springframework.web.multipart.MultipartFile;


/**
 * Controller: tb_group -- 互助组
 * @author qzy
 */
@RestController
@RequestMapping("/TbGroup/")
public class TbGroupController {

	/** 底层 Service 对象 */
	@Autowired
	TbGroupService tbGroupService;


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

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

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

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

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

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



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




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

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

	/**
	 *设置互助组组长
	 *
	 * @author loovi
	 * @date
	 */
	@RequestMapping("setLeader")
	@SaCheckPermission(TbGroup.PERMISSION_CODE_EDIT)
	public AjaxJson setLeader(@RequestParam("groupId") Long groupId,@RequestParam("leaderId")Long leaderId){
		int line = tbGroupService.setLeader(groupId,leaderId);

		return AjaxJson.getByLine(line);
	}
	/**
	*向互助组添加边民
	*
	* @author loovi
	* @date
	*/
	@RequestMapping("groupAddPeople")
	@SaCheckPermission(TbGroup.PERMISSION_CODE_EDIT)
	public AjaxJson AddGroupPeople(@RequestParam("groupId") Long groupId,@RequestParam("peopleIds") Long [] peopleIds){
		int line = tbGroupService.AddGroupPeople(groupId,peopleIds);
		return AjaxJson.getByLine(line);
	}
	/**
	 *互助组删除边民
	 *
	 * @author loovi
	 * @date
	 */
	@RequestMapping("groupDelPeople")
	@SaCheckPermission(TbGroup.PERMISSION_CODE_EDIT)
	public AjaxJson DelGroupPeople(@RequestParam("groupId") Long groupId,@RequestParam("peopleIds") Long [] peopleIds){
		int line = tbGroupService.DelGroupPeople(groupId,peopleIds);
		return AjaxJson.getByLine(line);
	}
	/**
	 *是否锁住互助组
	 *
	 * @author loovi
	 * @date
	 */
	@RequestMapping("isLock")
	@SaCheckPermission(TbGroup.PERMISSION_CODE_EDIT)
	public AjaxJson isLock(@RequestBody TbGroupDto tbGroupDto) {
		int line = tbGroupService.isLock(tbGroupDto);
		return AjaxJson.getByLine(line);
	}

	/**
	 *根据互助组id查看组内成员列表
	 *
	 * @author loovi
	 * @date
	 */
	@RequestMapping("getMemberListByGroupId")
//	@SaCheckPermission(TbGroup.PERMISSION_CODE_EDIT)
	public AjaxJson getMemberListByGroupId(Long id){
		AjaxJson ajaxJson =  tbGroupService.getMemberListByGroupId(id);
		return ajaxJson;
	}

	/**
	 *根据组长id查看互助组内成员列表
	 *
	 * @author loovi
	 * @date
	 */
	@RequestMapping("getMemberListByLeaderId")
//	@SaCheckPermission(TbGroup.PERMISSION_CODE_EDIT)
	public AjaxJson getMemberListByLeaderId(Long id){
		AjaxJson ajaxJson =  tbGroupService.getMemberListByLeaderId(id);
		return ajaxJson;
	}

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

	/** 用户在APP注册时选择身份则创建一个对应身份的实体 */
	@PostMapping("rpc/groupDto")
	public boolean groupDto(@RequestBody HtGroupDto groupDto){
		return tbGroupService.groupDto(groupDto);
	}

	/** 查 - 根据id */
	@RequestMapping("rpc/getRpcById")
	public GroupDto getRpcById(@RequestParam("id") String id){
		GroupDto t = tbGroupService.getRpcById(id);
		return t;
	}

}
