package org.springblade.person.feign;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.mp.support.BladePage;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.estate.entity.ApplyUser;
import org.springblade.estate.feign.IApplyUserClient;
import org.springblade.person.dto.HouseUserDTO;
import org.springblade.person.dto.MemberDTO;
import org.springblade.person.entity.HouseUser;
import org.springblade.person.service.HouseUserService;
import org.springblade.person.vo.HouseUserVO;
import org.springblade.person.wrapper.HouseUserWrapper;
import org.springblade.system.expand.feign.IConfClient;
import org.springblade.system.expand.feign.IOrgPositionClient;
import org.springblade.system.expand.util.QueryWrapperUtil;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * client
 *
 * @author lianghanqiang
 * @since 2021-04-14
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/houseuser")

public class HouseUserClient implements IHouseUserClient {

	private final HouseUserService houseUserService;
	private final IOrgPositionClient orgPositionClient;
	private final IConfClient confClient;

    @Override
	@PostMapping("logout")
	public Boolean logout(@RequestParam Long memberId) {
		QueryWrapper<HouseUser> queryWrapper = Condition.getQueryWrapper(new HouseUser());
		queryWrapper.lambda().in(HouseUser::getMemberId,memberId);
		return houseUserService.remove(queryWrapper);
    }

    /**
	 * 详情
	 */
	@Override
	@GetMapping("/detail")
	public HouseUserVO detail(@SpringQueryMap HouseUser houseUser) {
		HouseUser detail = houseUserService.getOne(Condition.getQueryWrapper(houseUser));
		return HouseUserWrapper.build().entityVO(detail);
	}

	/**
	 * 查询
	 */
	@Override
	@GetMapping("/list")
	public List<HouseUserVO> list(@SpringQueryMap HouseUser houseUser) {
		return HouseUserWrapper.build().listVO(houseUserService.list(Condition.getQueryWrapper(houseUser)));
	}

    @Override
	@PostMapping("/listInRoomIds")
	public List<HouseUser> listInRoomIds(@RequestBody List<Long> roomIds) {
		LambdaQueryWrapper<HouseUser> select = Condition.getQueryWrapper(new HouseUser()).lambda()
			.ne(HouseUser::getType, 0)
			.in(HouseUser::getRoomId, roomIds)
			.select(HouseUser::getId);
		return houseUserService.list(select);
    }

    @Override
	@GetMapping("/listMap")
	public List<HouseUserVO> listMap(@RequestParam Map<String, Object> houseUser) {
		LambdaQueryWrapper<HouseUser> queryWrapper = new QueryWrapperUtil<HouseUser>().getBasicQueryWrapperByMap(HouseUser::getOrgPosition, houseUser, new HouseUser(), AuthUtil.getUser(), orgPositionClient);
		return HouseUserWrapper.build().listVO(houseUserService.list(queryWrapper));
	}

	@Override
	@PostMapping("/getListByUserIds")
	public List<HouseUserVO> getListByUserIds(@RequestParam List<Long> userIds) {
		List<HouseUser> list = houseUserService.list(new QueryWrapper<>(new HouseUser()).lambda().in(HouseUser::getId, userIds));
		return HouseUserWrapper.build().listVO(list);
	}

	@Override
	@PostMapping("/getPageByRoomIds")
	public Page<HouseUserVO> getPageByRoomIds(@RequestParam List<Long> roomIds, @RequestBody Query query) {
		Page<HouseUserVO> result = new Page<>();
		IPage<HouseUser> page = houseUserService.page(Condition.getPage(query), new QueryWrapper<>(new HouseUser()).lambda().in(HouseUser::getRoomId, roomIds));

		IPage<HouseUserVO> houseUserVOIPage = HouseUserWrapper.build().pageVO(page);

		result.setSize(((int) houseUserVOIPage.getSize()));
		result.setCurrent((int) houseUserVOIPage.getCurrent());
		result.setTotal(houseUserVOIPage.getTotal());
		result.setRecords(houseUserVOIPage.getRecords());

		return result;
	}


	/**
	 * 新增
	 */
	@Override
	@PostMapping("/save")
	public boolean save(@Valid @RequestBody HouseUser houseUser) {
		return houseUserService.save(houseUser);
	}

	/**
	 * 修改
	 */
	@Override
	@PostMapping("/update")
	public boolean update(@Valid @RequestBody HouseUser houseUser) {
		houseUser.setFaceUrl(confClient.removePrefix(houseUser.getFaceUrl()));
		houseUser.setPassportImgUri(confClient.removePrefix(houseUser.getPassportImgUri()));
		houseUser.setEntryImgUri(confClient.removePrefix(houseUser.getEntryImgUri()));
		return houseUserService.updateById(houseUser);
	}

	/**
	 * 新增或修改
	 */
	@Override
	@PostMapping("/submit")
	public boolean submit(@Valid @RequestBody HouseUser houseUser) {
		return houseUserService.saveOrUpdate(houseUser);
	}


	/**
	 * 删除
	 */
	@Override
	@PostMapping("/remove")
	public boolean remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return houseUserService.deleteLogic(Func.toLongList(ids));
	}

	@Override
	@PostMapping("/deleteUserAndRelationship")
	public boolean deleteUserAndRelationship(@RequestBody HouseUserDTO userDto) {
		return houseUserService.deleteUserAndRelationship(userDto);
	}

	@Override
	@GetMapping("/getById")
	public HouseUser getById(@RequestParam Long id) {
		return houseUserService.getById(id);
	}

	@Override
	@PostMapping("/applySave")
	public R<HouseUser> applySave(@Valid HouseUser houseUser) {
		boolean save = houseUserService.save(houseUser);
		if(save){
			return R.data(houseUser);
		}else{
			return R.fail("新增用户失败");
		}
	}

	@Override
	@PostMapping("/houseUserCount")
	public Integer houseUserCount(@RequestBody HouseUser houseUser) {
		return houseUserService.count(Condition.getQueryWrapper(houseUser));
	}

	@Override
	@PostMapping("/yxfCount")
	public Integer yxfCount(@RequestBody List<Long> ids) {
		return houseUserService.count(Condition.getQueryWrapper(new HouseUser()).lambda().in(CollUtil.isNotEmpty(ids), HouseUser::getId, ids));
	}

	@Override
	@PostMapping("/count")
	public int count(@Valid @RequestBody HouseUser houseUser) {
		return houseUserService.count(Condition.getQueryWrapper(houseUser));
	}

	@Override
	@GetMapping("/getListByMember")
	public List<HouseUserVO> getListByMember(@RequestParam(value = "memberId") Long memberId, @RequestParam(value = "residentialId", required = false) Long residentialId, @RequestParam(value = "roomId", required = false) Long roomId) {
		LambdaQueryWrapper<HouseUser> lambda = new LambdaQueryWrapper<>();
		lambda.eq(HouseUser::getMemberId, memberId);
		lambda.eq(residentialId != null, HouseUser::getResidentialId, residentialId);
		lambda.eq(roomId != null, HouseUser::getRoomId, roomId);
		return HouseUserWrapper.build().listVO(houseUserService.list(lambda));
//		List<ApplyUser> applyUserList = applyUserClient.getListByMember(memberId, residentialId, null, CommonConstant.UserAuditStatus.pass.getValue());
//		List<HouseUser> userList = new ArrayList<>();
//		if(applyUserList.size() > 0){
//			List<Long> userIdList = applyUserList.stream().map(ApplyUser::getUserId).collect(Collectors.toList());
//			LambdaQueryWrapper<HouseUser> lambda = new LambdaQueryWrapper<>();
//			lambda.in(HouseUser::getId, userIdList);
//			userList = houseUserService.list(lambda);
//			return userList;
//		}
//		return userList;
	}

	@Override
	@PostMapping("/getPageByMember")
	public R<BladePage<HouseUserVO>> getPageByMember(@RequestBody MemberDTO memberDTO) {
		LambdaQueryWrapper<HouseUser> lambda = new LambdaQueryWrapper<>();
		lambda.eq(memberDTO.getId() != null, HouseUser::getMemberId, memberDTO.getId());
		lambda.eq(memberDTO.getResidentialId() != null, HouseUser::getResidentialId, memberDTO.getResidentialId());
		lambda.eq(memberDTO.getRoomId() != null, HouseUser::getRoomId, memberDTO.getRoomId());
		IPage<HouseUser> houseUserIPage = this.houseUserService.page(Condition.getPage(memberDTO.getQuery()), lambda);
		IPage<HouseUserVO> houseUserVOIPage = HouseUserWrapper.build().pageVO(houseUserIPage);
		return R.data(BladePage.of(houseUserVOIPage));
	}

	@Override
	@PostMapping("/updateUserByIds")
	public Boolean updateUserByIds(@RequestBody List<Long> ids, @RequestParam String memberId) {
		if (CollUtil.isEmpty(ids)) {
			return false;
		}
		LambdaUpdateWrapper<HouseUser> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.in(HouseUser::getId, ids)
			.set(HouseUser::getMemberId, memberId);
		return houseUserService.update(updateWrapper);
	}

}
