/**
 *
 */
package org.springblade.finance.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.finance.dto.ParamPush;
import org.springblade.finance.dto.ParamPushDTO;
import org.springblade.finance.entity.DateRangeAccount;
import org.springblade.finance.entity.DealUserAccounts;
import org.springblade.finance.entity.FinanceBillAudit;
import org.springblade.finance.entity.FinanceBillAuditUser;
import org.springblade.finance.entity.IntBackFile;
import org.springblade.finance.entity.IntOdrangeAccountSubView;
import org.springblade.finance.entity.IntOndaterangeAccount;
import org.springblade.finance.enums.AuditStatusEnum;
import org.springblade.finance.service.DateRangeAccountService;
import org.springblade.finance.service.DealUserAccountsService;
import org.springblade.finance.service.FinanceBillAuditService;
import org.springblade.finance.service.FinanceBillAuditUserService;
import org.springblade.finance.service.IIntBackFileService;
import org.springblade.finance.service.IIntOdrangeAccountSubViewService;
import org.springblade.finance.service.IIntOndaterangeAccountService;
import org.springblade.finance.vo.UserVO;
import org.springblade.system.client.UserClient;
import org.springblade.system.client.model.UserModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
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.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 */
@RestController
@RequestMapping("finance/billAudit")
@Api(value = "结算单审核", tags = "结算单审核接口")
@Slf4j
public class FinanceBillAuditController extends BladeController {

	//财务部门ID
	@Value("${blade.finance-dept-id}")
	private String deptId;

	@Autowired
	private UserClient userService;

	@Autowired
	private FinanceBillAuditService financeBillAuditService;

	@Autowired
	private FinanceBillAuditUserService financeBillAuditUserService;

	@Autowired
	private DealUserAccountsService dealUserAccountsService;

	@Autowired
	private IIntBackFileService intBackFileService;

	@Autowired
	private DateRangeAccountService dateRangeAccountService;

	@Autowired
	private IIntOndaterangeAccountService intOndaterangeAccountService;

	@Autowired
	private IIntOdrangeAccountSubViewService intOdrangeAccountSubViewService;

	/**
	 *
	 * @return
	 */
//	@ApiOperation(value = "审核人列表查询")
//	@ApiOperationSupport(order = 1)
//	@GetMapping("/checkUserList")
//	public List<UserVO> userList(){
//		//查询前100个用户
//		IPage<User> pageResult = userService.selectUserPage(new Page<User>(1, 100), new User(), Long.parseLong(deptId), null);
//		return UserWrapper.build().listVO(pageResult != null && pageResult.getRecords() != null ? pageResult.getRecords() : new ArrayList<User>());
//	}
//	@GetMapping("/selectUserList")
//	@ApiOperation(value = "用户下拉列表")
//	public R selectList() {
//		List<User> list = userService.list();
//		List<Map<String, Object>> data = list.stream()
//			.map(obj -> {
//				Map<String, Object> map = new HashMap<>();
//				map.put("id", obj.getId());
//				map.put("name", obj.getRealName());
//				return map;
//			}).collect(Collectors.toList());
//		return R.data(data);
//	}

	@ApiOperation(value = "审核人列表查询")
	@ApiOperationSupport(order = 1)
	@GetMapping("/checkUserList")
	public List<UserVO> userList(){
		List<UserVO> list = new ArrayList<UserVO>();
		List<UserModel> userModels = userService.listByDeptId(Long.valueOf(deptId));
		for (UserModel userModel : userModels) {
			String s = userModel.getId().toString();
			UserVO userVO = new UserVO();
			BeanUtils.copyProperties(userModel,userVO);
			userVO.setId(s);
			list.add(userVO);
		}
		return list;
	}
	
	@GetMapping("/selectUserList")
	@ApiOperation(value = "用户下拉列表")
	public R selectList() {
		List<UserModel> list = userService.listByDeptId(null);
		List<Map<String, Object>> data = list.stream()
			.map(obj -> {
				Map<String, Object> map = new HashMap<>();
				map.put("id", obj.getId());
				map.put("name", obj.getRealName());
				return map;
			}).collect(Collectors.toList());
		return R.data(data);
	}
	
	/**
	 * @return
	 */
	@GetMapping("/list")
	@ApiOperation(value = "分页查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "dealUserIds", value = "结算用户ID，逗号隔开。全部渠道传空或者不传递", required = false),
		@ApiImplicitParam(name = "queryDate", value = "查询日期(格式：yyyy-MM)", dataType = "string", required = false),
		@ApiImplicitParam(name = "auditStatus", value = "结算单状态", required = false),
		@ApiImplicitParam(name = "checkUserId", value = "审核人员ID", required = false),
		@ApiImplicitParam(name = "checkStatus", value = "审核状态(1-已审核/0-待审核)", required = false)
	})
	public R<IPage<FinanceBillAudit>> list(String[] dealUserIds,
								String queryDate, Integer auditStatus,
								Long checkUserId, Integer checkStatus, Query query){
		QueryWrapper<FinanceBillAudit> queryWrapper = Wrappers.<FinanceBillAudit>query();
		if(dealUserIds != null && dealUserIds.length > 0) {
			List<String> strings = Arrays.asList(dealUserIds);
			List<String> collect = null;
			if(strings != null && strings.size() > 0){
				QueryWrapper<DealUserAccounts> dealQueryWrapper = new QueryWrapper<DealUserAccounts>();
				dealQueryWrapper.select("DEAL_ACCOUNT_ID").isNotNull("DEAL_ACCOUNT_ID");
				dealQueryWrapper.in("DEAL_USERID",strings);
				List<DealUserAccounts> dealList = dealUserAccountsService.list(dealQueryWrapper);
				collect = dealList.stream().map(DealUserAccounts::getDealAccountId).collect(Collectors.toList());
			}
			queryWrapper.in("ioa.DEAL_ACCOUNT_ID", collect);
		}
		if(!StringUtils.isEmpty(queryDate)) {
			queryWrapper.eq("ioa.THEMONTH", queryDate.replace("-", ""));
		}
		if(auditStatus != null) {
			queryWrapper.eq("fba.AUDITSTATUS", auditStatus);
		}
		if(checkUserId != null) {
			queryWrapper.eq("fbau.USERID", checkUserId);
		}
		if(checkStatus != null) {
			queryWrapper.eq("fbau.CHECKSTATUS", checkStatus);
		}
		queryWrapper.orderByDesc("fba.AID");
		Page<FinanceBillAudit> page = new Page<>(Func.toInt(query.getCurrent(), 1), Func.toInt(query.getSize(), 10));
		//没有查询条件
		List<Long> aidList = financeBillAuditService.customAidList(page, queryWrapper);

		IPage<FinanceBillAudit> customPage = new Page<FinanceBillAudit>(Func.toInt(query.getCurrent(), 1), Func.toInt(query.getSize(), 10));
		if(aidList != null && !aidList.isEmpty()) {
			customPage.setTotal(financeBillAuditService.customCount(queryWrapper));
			List<FinanceBillAudit> custom = financeBillAuditService.customList(Wrappers.<FinanceBillAudit>query().in("fba.AID", aidList));
			for (FinanceBillAudit financeBillAudit : custom) {
				DateRangeAccount dateRangeAccount = financeBillAudit.getDateRangeAccount();
				if(!StringUtils.isEmpty(dateRangeAccount.getChannelName()) && dateRangeAccount.getChannelName().equals("微信支付")){
					List<IntOdrangeAccountSubView> subList = intOdrangeAccountSubViewService.list(new LambdaQueryWrapper<IntOdrangeAccountSubView>()
						.eq(IntOdrangeAccountSubView::getSid, dateRangeAccount.getSid())
					);
					financeBillAudit.setSubList(subList);
				}
				//回传文件列表
				List<IntBackFile> backFiles = intBackFileService.list(new LambdaQueryWrapper<IntBackFile>()
					.eq(IntBackFile::getAid, financeBillAudit.getAid())
					.eq(IntBackFile::getIsDelete, 0)
					.orderByDesc(IntBackFile::getUploadTime)
				);
				if(backFiles != null && backFiles.size() > 0){
					financeBillAudit.setFid(backFiles.get(0).getFid().toString());
					financeBillAudit.setUploadFileName(backFiles.get(0).getUploadFileName());
					financeBillAudit.setUploadFileUri(backFiles.get(0).getUploadFileUri());
					financeBillAudit.setUploadTime(backFiles.get(0).getUploadTime());
				}
			}
			customPage.setRecords(custom);

			if(customPage != null && customPage.getRecords() != null) {
				customPage.getRecords().forEach(f -> {
					StringBuffer userName = new StringBuffer();
					for (FinanceBillAuditUser financeBillAuditUser : f.getUserList()) {
						if(financeBillAuditUser.getUserId() == 1394115436887109634L){
							financeBillAuditUser.setUserName("");
							continue;
						}
						userName.append(financeBillAuditUser.getUserName()).append(",");
					}
					f.setUserNames(userName.deleteCharAt(userName.length()-1).toString());
					//f.setUserNames(String.join("、", f.getUserList().stream().map(FinanceBillAuditUser::getUserName).collect(Collectors.toList())));
				});
			}
		}
		return R.data(customPage);
	}

	/**
	 * 数量查询
	 * @return
	 */
	@GetMapping("/dataCount")
	@ApiOperation(value = "数量查询")
	@ApiOperationSupport(order = 3)
	public R<JSONObject> dateCount(){
		JSONObject result = new JSONObject();
		int uploadCount = financeBillAuditService.count(Wrappers.<FinanceBillAudit>lambdaQuery().eq(FinanceBillAudit::getAuditStatus, AuditStatusEnum.fin_upload.getValue()));
		int rejectCount = financeBillAuditService.count(Wrappers.<FinanceBillAudit>lambdaQuery().eq(FinanceBillAudit::getAuditStatus, AuditStatusEnum.fin_reject.getValue()));
		int waitMeCount = financeBillAuditUserService.count(Wrappers.<FinanceBillAuditUser>lambdaQuery().eq(FinanceBillAuditUser::getUserId, SecureUtil.getUserId()).eq(FinanceBillAuditUser::getCheckStatus, 0));
		result.put("uploadCount", uploadCount);
		result.put("rejectCount", rejectCount);
		result.put("waitMeCount", waitMeCount);
		return R.data(result);
	}

	/**
	 * 推送结算单
	 * @param paramPush
	 * @return
	 */
	@PostMapping("/add")
	@ApiOperation(value = "推送结算单接口")
	@ApiOperationSupport(order = 4)
	public R<String> add(@RequestBody ParamPush paramPush){
		LambdaQueryWrapper<FinanceBillAudit> queryWrapper = Wrappers.<FinanceBillAudit>lambdaQuery()
					.eq(FinanceBillAudit::getAuditType, paramPush.getAuditType())
					.ne(FinanceBillAudit::getAuditStatus, AuditStatusEnum.fin_cancel.getValue())
					.eq(FinanceBillAudit::getSid, paramPush.getSid());
		int count = financeBillAuditService.count(queryWrapper);
		if(count > 0) {
			return R.fail("结算单已推送，不能重复操作。");
		}
		FinanceBillAudit drat = new FinanceBillAudit();
		drat.setSid(paramPush.getSid());
		drat.setCreateTime(new Date());
		drat.setCreateByUserId(SecureUtil.getUserId());
		drat.setCreateByName(SecureUtil.getNickName());
		drat.setAuditType(paramPush.getAuditType());
		if(paramPush.getUserList() != null && !paramPush.getUserList().isEmpty()) {
			drat.setAuditStatus(AuditStatusEnum.wait_check.getValue());
		}else {
			drat.setAuditStatus(AuditStatusEnum.wait_upload.getValue());
		}
		boolean ret = financeBillAuditService.save(drat);
		if(ret) {
			List<FinanceBillAuditUser> userList = new ArrayList<FinanceBillAuditUser>();
			userList.add(new FinanceBillAuditUser(drat.getAid(), SecureUtil.getUserId(), SecureUtil.getNickName(), new Date(), 1));
			if(paramPush.getUserList() != null && !paramPush.getUserList().isEmpty()) {
				paramPush.getUserList().forEach(f -> {
					f.setAid(drat.getAid());
					if(f.getUserId() == 1394115436887109634L){
						f.setCheckStatus(1);
						f.setCheckTime(new Date());
					}else {
						f.setCheckStatus(0);
					}
				});
				userList.addAll(paramPush.getUserList());
			}
			List<FinanceBillAuditUser> list = sysUser(userList);
			financeBillAuditUserService.saveBatch(list);

			int num = financeBillAuditUserService.count(Wrappers.<FinanceBillAuditUser>lambdaQuery().eq(FinanceBillAuditUser::getAid, drat.getAid()).eq(FinanceBillAuditUser::getCheckStatus, 0));
			if(num <= 0) {
				FinanceBillAudit entity = new FinanceBillAudit();
				entity.setAid(Long.valueOf(drat.getAid()));
				entity.setAuditStatus(AuditStatusEnum.wait_upload.getValue());
				financeBillAuditService.updateById(entity);
			}

			IntOndaterangeAccount one = intOndaterangeAccountService.getOne(new LambdaQueryWrapper<IntOndaterangeAccount>()
				.eq(IntOndaterangeAccount::getSid, paramPush.getSid())
			);

			if(one != null){
				if(paramPush.getAuditType() == 1){
					one.setAllowFirstPush(2);
				}else if(paramPush.getAuditType() == 2){
					one.setAllowSecondPush(2);
				}
				intOndaterangeAccountService.update(one,new LambdaUpdateWrapper<IntOndaterangeAccount>().eq(IntOndaterangeAccount::getSid,paramPush.getSid()));
			}
		}
		return R.status(ret);
	}


	@PostMapping("/addList")
	@ApiOperation(value = "批量推送结算单接口")
	@ApiOperationSupport(order = 4)
	public R<String> addList(@RequestBody ParamPushDTO paramPush){
		List<Long> sids = paramPush.getSids();
		int a = 0;
		for (Long sid : sids) {
			LambdaQueryWrapper<FinanceBillAudit> queryWrapper = Wrappers.<FinanceBillAudit>lambdaQuery()
				.eq(FinanceBillAudit::getAuditType, paramPush.getAuditType())
				.ne(FinanceBillAudit::getAuditStatus, AuditStatusEnum.fin_cancel.getValue())
				.eq(FinanceBillAudit::getSid, sid);
			int count = financeBillAuditService.count(queryWrapper);
			if(count > 0) {
				return R.fail("结算单已推送，不能重复操作。");
			}
			FinanceBillAudit drat = new FinanceBillAudit();
			drat.setSid(sid);
			drat.setCreateTime(new Date());
			drat.setCreateByUserId(SecureUtil.getUserId());
			drat.setCreateByName(SecureUtil.getNickName());
			drat.setAuditType(paramPush.getAuditType());
			if(paramPush.getUserList() != null && !paramPush.getUserList().isEmpty()) {
				drat.setAuditStatus(AuditStatusEnum.wait_check.getValue());
			}else {
				drat.setAuditStatus(AuditStatusEnum.wait_upload.getValue());
			}
			boolean ret = financeBillAuditService.save(drat);
			if(ret) {
				a++;
				List<FinanceBillAuditUser> userList = new ArrayList<FinanceBillAuditUser>();
				userList.add(new FinanceBillAuditUser(drat.getAid(), SecureUtil.getUserId(), SecureUtil.getNickName(), new Date(), 1));
				if(paramPush.getUserList() != null && !paramPush.getUserList().isEmpty()) {
					paramPush.getUserList().forEach(f -> {
						f.setAid(drat.getAid());
						if(f.getUserId() == 1394115436887109634L){
							f.setCheckStatus(1);
							f.setCheckTime(new Date());
						}else {
							f.setCheckStatus(0);
						}
					});
					userList.addAll(paramPush.getUserList());
				}
				List<FinanceBillAuditUser> list = sysUser(userList);
				financeBillAuditUserService.saveBatch(list);

				int num = financeBillAuditUserService.count(Wrappers.<FinanceBillAuditUser>lambdaQuery().eq(FinanceBillAuditUser::getAid, drat.getAid()).eq(FinanceBillAuditUser::getCheckStatus, 0));
				if(num <= 0) {
					FinanceBillAudit entity = new FinanceBillAudit();
					entity.setAid(Long.valueOf(drat.getAid()));
					entity.setAuditStatus(AuditStatusEnum.wait_upload.getValue());
					financeBillAuditService.updateById(entity);
				}

				IntOndaterangeAccount one = intOndaterangeAccountService.getOne(new LambdaQueryWrapper<IntOndaterangeAccount>()
					.eq(IntOndaterangeAccount::getSid, sid)
				);

				if(one != null){
					if(paramPush.getAuditType() == 1){
						one.setAllowFirstPush(2);
					}else if(paramPush.getAuditType() == 2){
						one.setAllowSecondPush(2);
					}
					intOndaterangeAccountService.update(one,new LambdaUpdateWrapper<IntOndaterangeAccount>().eq(IntOndaterangeAccount::getSid,sid));
				}
			}
		}
		return R.success("推送成功"+a);
	}

	/**
	 * 审核排序
	 * @param list
	 * @return
	 */
	private List<FinanceBillAuditUser> sysUser(List<FinanceBillAuditUser> list ){
		for (FinanceBillAuditUser financeBillAuditUser : list) {
			UserModel byId = userService.getById(financeBillAuditUser.getUserId());
			financeBillAuditUser.setPriority(byId.getAuditLevel());
		}
		return list;
	}


	/**
	 * 驳回
	 * @param aid
	 * @param rejectReason
	 * @return
	 */
	@PostMapping("/reject")
	@ApiOperation(value = "驳回接口")
	@ApiOperationSupport(order = 5)
	public R<String> reject(@RequestParam Long aid, String rejectReason,Integer auditStatus){
		if(aid == null || auditStatus == null){
			return R.fail("参数异常");
		}
		FinanceBillAudit entity = new FinanceBillAudit();
		entity.setAid(Long.valueOf(aid));
		entity.setRejectReason(rejectReason);
		entity.setAuditStatus(AuditStatusEnum.fin_reject.getValue());
		if(financeBillAuditService.updateById(entity)){
			IntOndaterangeAccount one = intOndaterangeAccountService.getOne(new LambdaQueryWrapper<IntOndaterangeAccount>()
				.eq(IntOndaterangeAccount::getSid, entity.getSid())
			);
			/*if(one != null){
				one.setAllowFirstPush(1);
				intOndaterangeAccountService.update(one,new LambdaUpdateWrapper<IntOndaterangeAccount>().eq(IntOndaterangeAccount::getSid,entity.getSid()));
			}*/
			if(one != null){
				if(auditStatus == 1){
					one.setAllowFirstPush(1);
				}else if(auditStatus == 2){
					one.setAllowSecondPush(1);
				}
				intOndaterangeAccountService.update(one,new LambdaUpdateWrapper<IntOndaterangeAccount>().eq(IntOndaterangeAccount::getSid,entity.getSid()));
			}
			return R.status(true);
		};
		return R.status(false);
	}

	/**
	 *
	 * @param aid
	 * @return
	 */
	@PostMapping("/cancel")
	@ApiOperation(value = "作废接口")
	@ApiOperationSupport(order = 6)
	public R<String> cancel(@RequestParam Long aid,Integer auditStatus){
		if(aid == null || auditStatus == null){
			return R.fail("参数异常");
		}
		FinanceBillAudit financeBillAudit = financeBillAuditService.getById(aid);
		if(financeBillAudit == null || financeBillAudit.getCreateByUserId() != SecureUtil.getUserId().longValue()) {
			return R.fail("只有推送者才能作废");
		}
		FinanceBillAudit entity = new FinanceBillAudit();
		entity.setAid(Long.valueOf(aid));
		entity.setAuditStatus(AuditStatusEnum.fin_cancel.getValue());
		boolean uptRet = financeBillAuditService.updateById(entity);
		if(uptRet) { //作废后，设置全部为审核状态。
			LambdaUpdateWrapper<FinanceBillAuditUser> updateWrapper = Wrappers.<FinanceBillAuditUser>lambdaUpdate()
					.set(FinanceBillAuditUser::getCheckStatus, 1).eq(FinanceBillAuditUser::getAid, aid);
			financeBillAuditUserService.update(updateWrapper);
		}
		IntOndaterangeAccount one = intOndaterangeAccountService.getOne(new LambdaQueryWrapper<IntOndaterangeAccount>()
			.eq(IntOndaterangeAccount::getSid, financeBillAudit.getSid())
		);
		/*if(one != null){
			one.setAllowFirstPush(1);
			intOndaterangeAccountService.update(one,new LambdaUpdateWrapper<IntOndaterangeAccount>().eq(IntOndaterangeAccount::getSid,financeBillAudit.getSid()));
		}*/
		if(one != null){
			if(auditStatus == 1){
				one.setAllowFirstPush(1);
			}else if(auditStatus == 2){
				one.setAllowSecondPush(1);
			}
			intOndaterangeAccountService.update(one,new LambdaUpdateWrapper<IntOndaterangeAccount>().eq(IntOndaterangeAccount::getSid,financeBillAudit.getSid()));
		}
		return R.status(uptRet);
	}

	/**
	 *
	 * @param aid
	 * @return
	 */
	@PostMapping("/verify")
	@ApiOperation(value = "审核接口")
	public R<String> verify(@RequestParam Long aid){
		List<FinanceBillAuditUser> userList = financeBillAuditUserService.list(Wrappers.<FinanceBillAuditUser>lambdaQuery().eq(FinanceBillAuditUser::getAid, aid));
		if(!userList.stream().anyMatch(f -> f.getUserId().longValue() == SecureUtil.getUserId())) {
			return R.fail("您没有审核权限");
		}
		if(userList.stream().anyMatch(f -> f.getUserId().longValue() == SecureUtil.getUserId() && f.getCheckStatus() == 1)){
			return R.fail("您已经审核通过");
		}

		List<FinanceBillAuditUser> collect = userList.stream()
			.filter(item -> item.getPriority() != 0 && item.getCheckStatus() == 0).collect(Collectors.toList())
			.stream()
			.sorted(Comparator.comparing(FinanceBillAuditUser::getPriority)).collect(Collectors.toList());

		log.info("审核人员列表 = {}",collect.toString());

		List<FinanceBillAuditUser> filterList = collect.stream().filter(item -> item.getUserId().equals(SecureUtil.getUserId())).collect(Collectors.toList());
		log.info("除该人员外，其他同等级审核人需要去除 = {}",collect.toString());

		collect.removeIf(obj -> !obj.getUserId().equals(SecureUtil.getUserId()) && filterList.get(0).getPriority() == obj.getPriority());
		log.info("过滤后审核人员列表 = {}",collect.toString());
		if(collect.size() > 1){
			for (int i = 0; i < collect.size(); i++) {
				if(collect.get(i).getUserId().equals(SecureUtil.getUserId())){
					log.info("当前审核用户 = {}",collect.get(i).getUserId());
					if(collect.get(i).getPriority() == 1){
						log.info("第一审核人，无需判断");
						break;
					}
					if(i >= 1){
						if(collect.get(i).getPriority() == collect.get(i-1).getPriority()){
							break;
						}else if(collect.get(i).getPriority().longValue() > collect.get(i-1).getPriority().longValue()){
							if(collect.get(i-1).getCheckStatus() == 1){
								break;
							}else {
								return R.fail("上级审核人" + collect.get(i-1).getUserName()+ "还未审核");
							}
						}
					}
					/*if(collect.get(i).getPriority() == collect.get(i-1).getPriority()){
						break;
					}else if(collect.get(i).getPriority().longValue() > collect.get(i-1).getPriority().longValue()){
						if(collect.get(i-1).getCheckStatus() == 1){
							break;
						}else {
							return R.fail("上级审核人" + collect.get(i-1).getUserName()+ "还未审核");
						}
					}*/
				}
			}
		}
		FinanceBillAuditUser financeBillAuditUser = userList.stream().filter(f -> f.getUserId().longValue() == SecureUtil.getUserId() && f.getCheckStatus() == 0).findAny().get();
		financeBillAuditUser.setCheckStatus(1);
		financeBillAuditUser.setCheckTime(new Date());
		boolean ret = financeBillAuditUserService.updateById(financeBillAuditUser);

		//判断是否所有人都已审核完成
		int count = financeBillAuditUserService.count(Wrappers.<FinanceBillAuditUser>lambdaQuery().eq(FinanceBillAuditUser::getAid, aid).eq(FinanceBillAuditUser::getCheckStatus, 0));
		if(count <= 0) {
			FinanceBillAudit entity = new FinanceBillAudit();
			entity.setAid(Long.valueOf(aid));
			entity.setAuditStatus(AuditStatusEnum.wait_upload.getValue());
			financeBillAuditService.updateById(entity);
		}
		return R.status(ret);
	}
}
