/*
 *      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.keypwd.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.bank.keypwd.entity.KeyPwd;
import org.springblade.bank.keypwd.mapper.KeyPwdMapper;
import org.springblade.bank.keypwd.service.IKeyPwdService;
import org.springblade.bank.keypwd.vo.KeyPwdVO;
import org.springblade.bank.userlog.entity.UserLog;
import org.springblade.bank.userlog.service.IUserLogService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.modules.desk.service.INoticeService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 網點鎖匙/密碼登記表 服务实现类
 *
 * @author BladeX
 * @since 2021-08-13
 */
@Service
@AllArgsConstructor
public class KeyPwdServiceImpl extends BaseServiceImpl<KeyPwdMapper, KeyPwd> implements IKeyPwdService {

	private final IUserLogService userLogService;
	private final IUserService userService;
	private final IDeptService deptService;
	private final INoticeService noticeService;

	@Override
	public IPage<KeyPwdVO> selectKeyPwdPage(IPage<KeyPwdVO> page, KeyPwdVO keyPwd) {
		keyPwd.setStatus(1);
		return page.setRecords(baseMapper.selectKeyPwdPage(page, keyPwd));
	}

	@Override
	public List<KeyPwd> getMyInitialted() {
		List<KeyPwd> list = this.list(new QueryWrapper<>(new KeyPwd()).lambda().eq(KeyPwd::getCreateUser, AuthUtil.getUser().getUserId()).eq(KeyPwd::getStatus, 1));
		return list;
	}

	@Override
	public List<KeyPwd> getKeepList(KeyPwdVO keyPwd) {
		keyPwd.setStatus(2);
		keyPwd.setProcess(3);
		keyPwd.setIsTurnIn(0); //未上缴的
		return baseMapper.selectKeyPwdPage(keyPwd);
	}

	@Override
	public KeyPwd getKeepOneByOrgNoCategory(String bankNo, String orgNo, String category) {
		LambdaQueryWrapper<KeyPwd> eq = new QueryWrapper<>(new KeyPwd()).lambda().eq(KeyPwd::getBankNo, bankNo).eq(KeyPwd::getOrgNo, orgNo)
			.eq(KeyPwd::getCategory, category).eq(KeyPwd::getStatus, 2).eq(KeyPwd::getProcess,3);
		return baseMapper.selectOne(eq);
	}

	@Override
	public List<KeyPwd> getList(KeyPwdVO keyPwd) {
		return baseMapper.selectKeyPwdPage(keyPwd);
	}

	@Override
	public boolean turnIn(KeyPwd keyPwd) {
		List<KeyPwd> updateList = new ArrayList<>();
		String[] categorys = keyPwd.getCategory().split(",");

		for (int i = 0; i < categorys.length; i++) {
			String category = categorys[i];
			KeyPwd keep = getKeepOneByOrgNoCategory(keyPwd.getBankNo(), keyPwd.getOrgNo(), category);
			Assert.notNull(keep, "當前用戶不持有該類型鑰匙密碼或實物！");
			keep.setReceiverId(keyPwd.getReceiverId());
			keep.setReceiverNo(keyPwd.getReceiverNo());
			keep.setReceiverName(keyPwd.getReceiverName());
			keep.setProcess(3);
			keep.setIsTurnIn(1);
			keep.setStatus(1);
			keep.setRemark(keyPwd.getRemark());
			keep.setBankNo(keyPwd.getBankNo());
			keep.setOrgNo(keyPwd.getOrgNo());
			keep.setOrgName(keyPwd.getOrgName());
			keep.setHandoverDate(keyPwd.getHandoverDate());
			keep.setHandoverPersonId(keyPwd.getHandoverPersonId());
			keep.setHandoverPersonNo(keyPwd.getHandoverPersonNo());
			keep.setHandoverPersonName(keyPwd.getHandoverPersonName());
			keep.setFillingPerson(keyPwd.getHandoverPersonName());
			keep.setFillingDate(DateUtil.now());
			keep.setCreateUser(AuthUtil.getUserId());
			keep.setCreateTime(keyPwd.getCreateTime());
			keep.setCreateDept(keyPwd.getCreateDept());

			keep.setSureTime(DateUtil.now());
			keep.setRemark(keyPwd.getRemark());
			updateList.add(keep);
//			this.updateById(keep);
		}

		if (updateList.size() > 0 && updateBatchById(updateList)){
			User user = userService.getById(AuthUtil.getUserId());
			Dept dept = deptService.getById(user.getDeptId());
			UserLog userLog = new UserLog();
			userLog.setTableName("keypwd");
			userLog.setBankNo(dept.getBankNo());
			userLog.setOrgNo(dept.getOrgNo());
			userLog.setOrgName(dept.getDeptName());
			userLog.setNewData(JSONUtil.toJsonStr(keyPwd));
			userLog.setPersonNo(user.getEhr());
			userLog.setPersonName(user.getName());
			userLog.setOperationType("turnin");
			userLogService.save(userLog);
			//發送通知 - 上繳
			noticeService.sendKeyPwdNotice(keyPwd, false, true);
		}

		return true;
	}

}
