package org.springblade.estate.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 io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.constant.ConfConstant;
import org.springblade.core.mp.support.BladePage;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.estate.dto.ApplyUserDTO;
import org.springblade.estate.dto.ApplyUserPageDTO;
import org.springblade.estate.wrapper.ArticleWrapper;
import org.springblade.system.expand.feign.IConfClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.estate.entity.ApplyUser;
import org.springblade.estate.vo.ApplyUserVO;
import org.springblade.estate.wrapper.ApplyUserWrapper;
import org.springblade.estate.service.IApplyUserService;

import java.util.List;
import java.util.Objects;

/**
 * 申请用户表---房屋成员 client
 *
 * @author lianghanqiang
 * @since 2021-04-23
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/applyuser")
public class ApplyUserClient implements IApplyUserClient {

    private final IApplyUserService applyUserService;
    private final IConfClient confClient;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public ApplyUserVO detail(@SpringQueryMap ApplyUser applyUser) {
        ApplyUser detail = applyUserService.getOne(Condition.getQueryWrapper(applyUser));
//		String filePrefix = confClient.getValueByToken(ConfConstant.FILE_SERVER_HOST);
        return ApplyUserWrapper.build().entityVO(detail);
        }

	@Override
	@GetMapping("/getById")
	public ApplyUser getById(@RequestParam Long id) {
		return this.applyUserService.getById(id);
	}

	/**
     * 查询 申请用户表---房屋成员
     */
    @Override
    @GetMapping("/list")
    public List<ApplyUserVO> list(@SpringQueryMap ApplyUser applyUser) {

		String filePrefix = confClient.getValueByToken(ConfConstant.FILE_SERVER_HOST);
        return ApplyUserWrapper.build().listVO(applyUserService.list(Condition.getQueryWrapper(applyUser)));
        }

	@Override
	@PostMapping("/page")
	public BladePage<ApplyUserVO> page(@RequestBody ApplyUserDTO applyUserDTO) {
		ApplyUser applyUser = new ApplyUser();
		BeanUtil.copyProperties(applyUserDTO, applyUser);
		QueryWrapper<ApplyUser> queryWrapper = Condition.getQueryWrapper(applyUser);
		queryWrapper.lambda().orderByDesc(ApplyUser::getCreateTime);
		IPage<ApplyUser> pages = applyUserService.page(Condition.getPage(applyUserDTO.getQuery()), queryWrapper);
		IPage<ApplyUserVO> pageVO = ApplyUserWrapper.build().pageVO(pages);
		BladePage<ApplyUserVO> bladePage = BladePage.of(pageVO);
		return bladePage;
	}

	@PostMapping("/pageCondition")
	@Override
	public BladePage<ApplyUserVO> pageCondition(@RequestBody ApplyUserDTO applyUserDTO){
		//获取待审核的申请记录
		LambdaQueryWrapper<ApplyUser> queryWrapper = Condition.getQueryWrapper(new ApplyUser()).lambda()
			.eq(ApplyUser::getAuditStatus,0)
			.in(ApplyUser::getRoomId, applyUserDTO.getRoomIds());
		IPage<ApplyUser> userIPage = applyUserService.page(Condition.getPage(applyUserDTO.getQuery()), queryWrapper);
		IPage<ApplyUserVO> pageVO = ApplyUserWrapper.build().pageVO(userIPage);
		BladePage<ApplyUserVO> bladePage = BladePage.of(pageVO);
		return bladePage;
	};

    @Override
	@PostMapping("/authentication")
	public boolean authentication(@RequestBody ApplyUser applyUser) {
		return this.applyUserService.authentication(applyUser);
	}

	@Override
	@PostMapping("/addApplyUserForFamily")
	public boolean addApplyUserForFamily(@RequestBody ApplyUser applyUser) {
		return this.applyUserService.addApplyUserForFamily(applyUser);
	}

	@Override
	@PostMapping("/appUpdateAuditStatus")
	public R appUpdateAuditStatus(@RequestBody ApplyUserDTO applyUserDTO) {
		return this.applyUserService.appUpdateAuditStatus(applyUserDTO);
	}


	/**
     * 新增 申请用户表---房屋成员
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody ApplyUser applyUser) {
        return applyUserService.save(applyUser);
        }

    /**
     * 修改 申请用户表---房屋成员
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody ApplyUser applyUser) {
        return applyUserService.updateById(applyUser);
        }

    /**
     * 新增或修改 申请用户表---房屋成员
     */
    @Override
    @PostMapping("/submit")
    public boolean submit(@Valid @RequestBody ApplyUser applyUser) {
        return applyUserService.saveOrUpdate(applyUser);
        }


    /**
     * 删除 申请用户表---房屋成员
     */
    @Override
    @PostMapping("/remove")
    public boolean remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return applyUserService.deleteLogic(Func.toLongList(ids));
        }

	@Override
	@PostMapping("/getAllApplyUserByMemberId")
	public BladePage<ApplyUserVO> getAllApplyUserByCondition(@RequestBody ApplyUserDTO applyUserDTO) {
    	ApplyUser applyUser = Objects.requireNonNull(BeanUtil.copy(applyUserDTO,ApplyUser.class));
		LambdaQueryWrapper<ApplyUser> lambda = Condition.getQueryWrapper(applyUser).lambda();
		if(applyUserDTO.getMemberId() != null){
			lambda.eq(ApplyUser::getMemberId,applyUserDTO.getMemberId());
		}
		if(applyUserDTO.getCheckStates().size()>0){
			lambda.in(ApplyUser::getAuditStatus,applyUserDTO.getCheckStates());
		}
		if(applyUserDTO.getRoomIds().size()>0){
			lambda.in(ApplyUser::getRoomId,applyUserDTO.getRoomIds());
		}
		lambda.orderByDesc(ApplyUser::getCreateTime);
		IPage<ApplyUser> page = this.applyUserService.page(Condition.getPage(applyUserDTO.getQuery()), lambda);
		IPage<ApplyUserVO> applyUserVOIPage = ApplyUserWrapper.build().pageVO(page);
		BladePage<ApplyUserVO> bladePage = new BladePage<>();
		bladePage.setCurrent(applyUserVOIPage.getCurrent());
		bladePage.setTotal(applyUserVOIPage.getTotal());
		bladePage.setSize(applyUserVOIPage.getSize());
		bladePage.setRecords(applyUserVOIPage.getRecords());
//		result.setSize(((int) page.getSize()));
//		result.setCurrent((int)page.getCurrent());
//		result.setTotal(page.getTotal());
//		result.setRecords(page.getRecords());
		return bladePage;
	}

	@Override
	@PostMapping("/getByAuditStatus")
	public List<ApplyUser> getByAuditStatus(@RequestBody ApplyUserDTO applyUserDTO) {
    	LambdaQueryWrapper<ApplyUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    	lambdaQueryWrapper.eq(applyUserDTO.getUserId() != null, ApplyUser::getUserId, applyUserDTO.getUserId());
    	lambdaQueryWrapper.eq(applyUserDTO.getMemberId() != null, ApplyUser::getMemberId, applyUserDTO.getMemberId());
    	lambdaQueryWrapper.eq(ApplyUser::getAuditStatus, applyUserDTO.getAuditStatus());
		return applyUserService.list(lambdaQueryWrapper);
	}

	@Override
	@PostMapping("/getListByMember")
	public List<ApplyUser> getListByMember(@RequestParam(value = "memberId") Long memberId, @RequestParam(value = "residentialId") Long residentialId,
										   @RequestParam(value = "roomId", required = false) Long roomId, @RequestParam(value = "auditStatus", required = false) Integer auditStatus) {
    	LambdaQueryWrapper<ApplyUser> lambda = new LambdaQueryWrapper<>();
    	lambda.eq(ApplyUser::getMemberId, memberId);
    	lambda.eq(residentialId != null, ApplyUser::getResidentialId, residentialId);
    	lambda.eq( roomId!= null, ApplyUser::getRoomId, roomId);
    	lambda.eq(auditStatus != null, ApplyUser::getAuditStatus, auditStatus);
		return applyUserService.list(lambda);
	}

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


}
