/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.bank.home.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springblade.bank.cardswallow.entity.CardSwallow;
import org.springblade.bank.cardswallow.service.ICardSwallowService;
import org.springblade.bank.checklist.entity.Checklist;
import org.springblade.bank.checklist.service.IChecklistService;
import org.springblade.bank.goodsuse.entity.GoodsUse;
import org.springblade.bank.goodsuse.service.IGoodsUseService;
import org.springblade.bank.keypwd.entity.KeyPwd;
import org.springblade.bank.keypwd.service.IKeyPwdService;
import org.springblade.bank.message.entity.Message;
import org.springblade.bank.message.service.IMessageService;
import org.springblade.bank.postchange.entity.PostChange;
import org.springblade.bank.postchange.service.IPostChangeService;
import org.springblade.bank.returns.entity.Returns;
import org.springblade.bank.returns.service.IReturnsService;
import org.springblade.bank.sealhandover.entity.SealHandover;
import org.springblade.bank.sealhandover.service.ISealHandoverService;
import org.springblade.bank.tellertrunk.entity.TellerTrunk;
import org.springblade.bank.tellertrunk.service.ITellerTrunkService;
import org.springblade.bank.warehouse.entity.CheckWarehouse;
import org.springblade.bank.warehouse.service.ICheckWarehouseService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.Menu;
import org.springblade.modules.system.entity.RoleMenu;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IMenuService;
import org.springblade.modules.system.service.IRoleMenuService;
import org.springblade.modules.system.vo.DeptVO;
import org.springblade.modules.system.vo.UserVO;
import org.springblade.modules.system.wrapper.UserWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 控制器
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping("bank/home")
@Api(value = "首頁", tags = "")
public class HomeController extends BladeController {

	private final IKeyPwdService keyPwdService;
	private final ISealHandoverService sealHandoverService;
	private final IPostChangeService postChangeService;
	private final ICheckWarehouseService checkWarehouseService;
	private final IChecklistService checklistService;
	private final ITellerTrunkService tellerTrunkService;
	private final IGoodsUseService goodsUseService;
	private final ICardSwallowService cardSwallowService;
	private final IReturnsService returnsService;
	private final IMessageService messageService;
	private final IRoleMenuService roleMenuService;
	private final IMenuService menuService;

	/**
	 * 获取我的發起
	 */
	@GetMapping("/myInitiated")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取我的發起", notes = "")
	public R myInitiated() {
		int count = 0;

		//網點鑰匙密碼實物交接登記表
		boolean keypwd = roleMenuService.checkMenuByMenuCodeAndRoles("keypwd", menuService);
		//業務印章交接登記表
		boolean sealhandover = roleMenuService.checkMenuByMenuCodeAndRoles("sealhandover", menuService);
		//網點出納主管及專管員崗位表更記錄表
		boolean postchange = roleMenuService.checkMenuByMenuCodeAndRoles("postchange", menuService);
		//查庫流水賬記錄表
		boolean checkwarehouse = roleMenuService.checkMenuByMenuCodeAndRoles("checkwarehouse", menuService);
		//核對記錄表
		boolean checklist = roleMenuService.checkMenuByMenuCodeAndRoles("checklist", menuService);
		//櫃員尾箱檢查流水賬記錄表
		boolean tellertrunk = roleMenuService.checkMenuByMenuCodeAndRoles("tellertrunk", menuService);
		//使用及出入庫登記表
		boolean goodsuse = roleMenuService.checkMenuByMenuCodeAndRoles("goodsuse", menuService);
		//新開戶退件登記表
		boolean returns = roleMenuService.checkMenuByMenuCodeAndRoles("returns", menuService);
		//自助設備吞卡資料
		boolean cardswallow = roleMenuService.checkMenuByMenuCodeAndRoles("cardswallow", menuService);
		//信息發佈
		boolean message = roleMenuService.checkMenuByMenuCodeAndRoles("message", menuService);

		if (keypwd){
			List<KeyPwd> keyPwdList = keyPwdService.getMyInitialted();
			count += keyPwdList.size();
		}
		if (sealhandover){
			List<SealHandover> sealList = sealHandoverService.getMyInitialted();
			count += sealList.size();
		}
		if (postchange){
			List<PostChange> postChangeList = postChangeService.getMyInitialted();
			count += postChangeList.size();
		}
		if (checkwarehouse){
			List<CheckWarehouse> checkWarehouses = checkWarehouseService.getMyInitialted();
			count += checkWarehouses.size();
		}
		if (checklist){
			List<Checklist> checklistList = checklistService.getMyInitialted();
			count += checklistList.size();
		}
		if (tellertrunk){
			List<TellerTrunk> tellerTrunkList = tellerTrunkService.getMyInitialted();
			count += tellerTrunkList.size();
		}
		if (goodsuse){
			List<GoodsUse> goodsUseList = goodsUseService.getMyInitialted();
			count += goodsUseList.size();
		}
		if (cardswallow){
			List<CardSwallow> cardSwallowList = cardSwallowService.getMyInitialted();
			count += cardSwallowList.size();
		}
		if (returns){
			List<Returns> returnsList = returnsService.getMyInitialted();
			count += returnsList.size();
		}
		if (message){
			List<Message> messageList = messageService.getMyInitialted();
			count += messageList.size();
		}

//		List<? extends BaseEntity> collect = Stream.of(postChangeList,goodsUseList,cardSwallowList,returnsList,messageList).flatMap(x -> x.stream()).collect(Collectors.toList());

		return R.data(count);
	}
	/**
	 * 获取待確認
	 */
	@GetMapping("/toBeConfirmed")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取当前用户的部门信息", notes = "")
	public R toBeConfirmed() {
		int count = 0;

		//網點鑰匙密碼實物交接登記表
		boolean keypwd = roleMenuService.checkMenuByMenuCodeAndRoles("keypwd", menuService);
		//業務印章交接登記表
		boolean sealhandover = roleMenuService.checkMenuByMenuCodeAndRoles("sealhandover", menuService);

		//網點出納主管及專管員崗位表更記錄表
		boolean postchange = roleMenuService.checkMenuByMenuCodeAndRoles("postchange", menuService);
		//使用及出入庫登記表
		boolean goodsuse = roleMenuService.checkMenuByMenuCodeAndRoles("goodsuse", menuService);
		//新開戶退件登記表
		boolean returns = roleMenuService.checkMenuByMenuCodeAndRoles("returns", menuService);
		//自助設備吞卡資料
		boolean cardswallow = roleMenuService.checkMenuByMenuCodeAndRoles("cardswallow", menuService);

		if (keypwd){
			List<KeyPwd> list = keyPwdService.list(new QueryWrapper<>(new KeyPwd()).lambda().eq(KeyPwd::getProcess, 2).eq(KeyPwd::getReceiverId, AuthUtil.getUserId()).eq(KeyPwd::getStatus, 1));
			count += list.size();
		}
		if (sealhandover){
			List<SealHandover> list = sealHandoverService.list(new QueryWrapper<>(new SealHandover()).lambda().eq(SealHandover::getProcess, 2).eq(SealHandover::getReceiverId, AuthUtil.getUserId()).eq(SealHandover::getStatus, 1));
			count += list.size();
		}

		if (postchange){
			List<PostChange> postChangeList = postChangeService.list(new QueryWrapper<>(new PostChange()).lambda().eq(PostChange::getProcess, 2).eq(PostChange::getPersonId, AuthUtil.getUserId()));
			count += postChangeList.size();
		}
		if (goodsuse){
			List<GoodsUse> goodsUseList = goodsUseService.list(new QueryWrapper<>(new GoodsUse()).lambda().eq(GoodsUse::getProcess, 2).eq(GoodsUse::getPersonId, AuthUtil.getUserId()));
			count += goodsUseList.size();
		}
		if (returns){
			List<Returns> returnsList = returnsService.list(new QueryWrapper<>(new Returns()).lambda().eq(Returns::getProcess, 3).eq(Returns::getPersonId, AuthUtil.getUserId()));
			count += returnsList.size();
		}
		if (cardswallow){
			List<CardSwallow> cardSwallowList = cardSwallowService.list(new QueryWrapper<>(new CardSwallow()).lambda().eq(CardSwallow::getProcess, 3).eq(CardSwallow::getPersonId, AuthUtil.getUserId()));
			count += cardSwallowList.size();
		}

		return R.data(count);
	}

	/**
	 * 获取我的確認
	 */
	@GetMapping("/myConfirmation")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R myConfirmation() {
		int count = 0;

		//網點鑰匙密碼實物交接登記表
		boolean keypwd = roleMenuService.checkMenuByMenuCodeAndRoles("keypwd", menuService);
		//業務印章交接登記表
		boolean sealhandover = roleMenuService.checkMenuByMenuCodeAndRoles("sealhandover", menuService);

		//網點出納主管及專管員崗位表更記錄表
		boolean postchange = roleMenuService.checkMenuByMenuCodeAndRoles("postchange", menuService);
		//使用及出入庫登記表
		boolean goodsuse = roleMenuService.checkMenuByMenuCodeAndRoles("goodsuse", menuService);
		//新開戶退件登記表
		boolean returns = roleMenuService.checkMenuByMenuCodeAndRoles("returns", menuService);
		//自助設備吞卡資料
		boolean cardswallow = roleMenuService.checkMenuByMenuCodeAndRoles("cardswallow", menuService);

		if (keypwd){
			List<KeyPwd> list = keyPwdService.list(new QueryWrapper<>(new KeyPwd()).lambda().eq(KeyPwd::getProcess, 3).eq(KeyPwd::getReceiverId, AuthUtil.getUserId()).eq(KeyPwd::getStatus, 1));
			count += list.size();
		}
		if (sealhandover){
			List<SealHandover> list = sealHandoverService.list(new QueryWrapper<>(new SealHandover()).lambda().eq(SealHandover::getProcess, 3).eq(SealHandover::getReceiverId, AuthUtil.getUserId()).eq(SealHandover::getStatus, 1));
			count += list.size();
		}

		if (postchange){
			List<PostChange> postChangeList = postChangeService.list(new QueryWrapper<>(new PostChange()).lambda().eq(PostChange::getProcess, 3).eq(PostChange::getPersonId, AuthUtil.getUserId()));
			count += postChangeList.size();
		}
		if (goodsuse){
			List<GoodsUse> goodsUseList = goodsUseService.list(new QueryWrapper<>(new GoodsUse()).lambda().eq(GoodsUse::getProcess, 3).eq(GoodsUse::getPersonId, AuthUtil.getUserId()));
			count += goodsUseList.size();
		}
		if (returns){
			List<Returns> returnsList = returnsService.list(new QueryWrapper<>(new Returns()).lambda().eq(Returns::getProcess, 4).eq(Returns::getPersonId, AuthUtil.getUserId()));
			count += returnsList.size();
		}
		if (cardswallow){
			List<CardSwallow> cardSwallowList = cardSwallowService.list(new QueryWrapper<>(new CardSwallow()).lambda().eq(CardSwallow::getProcess, 4).eq(CardSwallow::getPersonId, AuthUtil.getUserId()));
			count += cardSwallowList.size();
		}

		return R.data(count);
	}

	/**
	 * 待發佈
	 */
	@GetMapping("/toBoReleased")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "列表", notes = "传入account和realName")
	public R toBoReleased() {
		int count = 0;
		//新開戶退件登記表
		boolean returns = roleMenuService.checkMenuByMenuCodeAndRoles("returns", menuService);
		if (returns){
			List<Returns> returnsList = returnsService.list(new QueryWrapper<>(new Returns()).lambda().eq(Returns::getProcess, 2).eq(Returns::getPersonId, AuthUtil.getUserId()));
			count += returnsList.size();
		}
		return R.data(count);
	}

}
