package org.springblade.estate.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.common.constant.ConfConstant;
import org.springblade.core.mp.support.BladePage;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.estate.dto.ApplyUserDTO;
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(confClient).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(confClient).listVO(applyUserService.list(Condition.getQueryWrapper(applyUser)));
        }

	@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("/updateCheckState")
	public boolean updateCheckState(@RequestBody ApplyUser applyUser) {
		return this.applyUserService.updateCheckState(applyUser);
	}


	/**
     * 新增 申请用户表---房屋成员
     */
    @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::getCheckState,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(confClient).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::getCheckState, applyUserDTO.getCheckState());
		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::getCheckState, auditStatus);
		return applyUserService.list(lambda);
	}


}
