/*
 *      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.tellertrunk.controller;

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springblade.bank.keypwd.entity.KeyPwd;
import org.springblade.bank.sealhandover.entity.SealHandover;
import org.springblade.bank.userlog.entity.UserLog;
import org.springblade.bank.userlog.service.IUserLogService;
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.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
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.IDictService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.bank.tellertrunk.entity.TellerTrunk;
import org.springblade.bank.tellertrunk.vo.TellerTrunkVO;
import org.springblade.bank.tellertrunk.wrapper.TellerTrunkWrapper;
import org.springblade.bank.tellertrunk.service.ITellerTrunkService;
import org.springblade.core.boot.ctrl.BladeController;

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

/**
 * 櫃員尾箱檢查流水賬記錄表 控制器
 *
 * @author BladeX
 * @since 2021-08-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("bank/tellertrunk")
@Api(value = "櫃員尾箱檢查流水賬記錄表", tags = "櫃員尾箱檢查流水賬記錄表接口")
public class TellerTrunkController extends BladeController {

	private final ITellerTrunkService tellerTrunkService;
	private final IUserService userService;
	private final IDeptService deptService;
	private final IUserLogService userLogService;
	private final IDictService dictService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入tellerTrunk")
	public R<TellerTrunkVO> detail(TellerTrunk tellerTrunk) {
		TellerTrunk detail = tellerTrunkService.getOne(Condition.getQueryWrapper(tellerTrunk));
		return R.data(TellerTrunkWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 櫃員尾箱檢查流水賬記錄表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入tellerTrunk")
	public R<IPage<TellerTrunkVO>> list(TellerTrunk tellerTrunk, Query query) {
		IPage<TellerTrunk> pages = tellerTrunkService.page(Condition.getPage(query), Condition.getQueryWrapper(tellerTrunk));
		return R.data(TellerTrunkWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 櫃員尾箱檢查流水賬記錄表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入tellerTrunk")
	public R<IPage<TellerTrunkVO>> page(TellerTrunkVO tellerTrunk, Query query) {

		if (StringUtils.isNotBlank(tellerTrunk.getOrgNostr())){
			List<String> orgNos = Func.toStrList(tellerTrunk.getOrgNostr());
			tellerTrunk.setOrgNos(orgNos);
		}
		IPage<TellerTrunkVO> pages = tellerTrunkService.selectTellerTrunkPage(Condition.getPage(query), tellerTrunk);
		return R.data(pages);
	}

	/**
	 * 新增 櫃員尾箱檢查流水賬記錄表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入tellerTrunk")
	public R save(@Valid @RequestBody TellerTrunk tellerTrunk) {
		return R.status(tellerTrunkService.save(tellerTrunk));
	}

	/**
	 * 修改 櫃員尾箱檢查流水賬記錄表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入tellerTrunk")
	public R update(@Valid @RequestBody TellerTrunk tellerTrunk) {
		return R.status(tellerTrunkService.updateById(tellerTrunk));
	}

	/**
	 * 新增或修改 櫃員尾箱檢查流水賬記錄表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入tellerTrunk")
	public R submit(@Valid @RequestBody TellerTrunk tellerTrunk) {
		boolean isAdd = tellerTrunk.getId() == null;
		BladeUser currentUser = AuthUtil.getUser();
		User user = userService.getById(currentUser.getUserId());
		Dept dept = deptService.getById(user.getDeptId());
		UserLog userLog = new UserLog();

		TellerTrunk old = null;
		if (isAdd){
			tellerTrunk.setFillingPerson(currentUser.getUserName());
			tellerTrunk.setFillingDate(DateUtil.now());
		}else{
			old = tellerTrunkService.getById(tellerTrunk.getId());
		}

		if (tellerTrunkService.saveOrUpdate(tellerTrunk)){
			userLog.setTableName("tellertrunk");
			userLog.setBankNo(dept.getBankNo());
			userLog.setOrgNo(dept.getOrgNo());
			userLog.setOrgName(dept.getDeptName());
			userLog.setNewData(JSONUtil.toJsonStr(tellerTrunk));
			userLog.setPersonNo(user.getEhr());
			userLog.setPersonName(user.getName());

			if (isAdd){
				userLog.setOperationType("add");
			}else{
				userLog.setOperationType("edit");
				userLog.setOldData(JSONUtil.toJsonStr(old));
			}
			userLogService.save(userLog);

		}
		return R.status(true);
	}


	/**
	 * 删除 櫃員尾箱檢查流水賬記錄表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> idList = Func.toLongList(ids);
		List<UserLog> userLogList = new ArrayList<>();

		// 日誌記錄
		BladeUser currentUser = AuthUtil.getUser();
		User user = userService.getById(currentUser.getUserId());
		Dept dept = deptService.getById(user.getDeptId());
		idList.forEach(id -> {
			UserLog userLog = new UserLog();
			TellerTrunk tellerTrunk = tellerTrunkService.getById(id);

			userLog.setTableName("tellertrunk");
			userLog.setOperationType("del");
			userLog.setBankNo(dept.getBankNo());
			userLog.setOrgNo(dept.getOrgNo());
			userLog.setOrgName(dept.getDeptName());
			userLog.setOldData(JSONUtil.toJsonStr(tellerTrunk));
			userLog.setPersonNo(user.getEhr());
			userLog.setPersonName(user.getName());
			userLogList.add(userLog);
		});

		return R.status(tellerTrunkService.deleteLogic(idList) && userLogService.saveBatch(userLogList));
	}

	@GetMapping("/getList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入tellerTrunk")
	public R getList(TellerTrunkVO tellerTrunk) {

		if (StringUtils.isNotBlank(tellerTrunk.getOrgNostr())){
			List<String> orgNos = Func.toStrList(tellerTrunk.getOrgNostr());
			tellerTrunk.setOrgNos(orgNos);
		}
		List<TellerTrunk> list = tellerTrunkService.getList(tellerTrunk);

		list.forEach(item -> {
			String checkUnit = item.getCheckUnit();
			String checkContent = item.getCheckContent();
			String checkType = item.getCheckType();
			String checkResult = item.getCheckResult();
			String[] splitUnit = checkUnit.split(",");
			String[] splitType = checkType.split(",");
			String[] splitContent = checkContent.split(",");
			checkUnit = ""; checkType = ""; checkContent = "";


			for (String unit : splitUnit) {
				checkUnit += dictService.getValue("unit_type", unit) + " | ";
			}
			for (String purpose : splitType) {
				checkType += dictService.getValue("check_type", purpose) + " | ";
			}
			for (String content : splitContent) {
				checkContent += dictService.getValue("check_content_tellertrunk", content) + " | ";
			}

			String check_result = dictService.getValue("check_result", checkResult);
			item.setCheckResult(check_result);
			item.setCheckUnit(checkUnit.substring(0, checkUnit.length() - 1));
			item.setCheckType(checkType.substring(0, checkType.length() - 1));
			item.setCheckContent(checkContent.substring(0, checkContent.length() - 1));

		});

		return R.data(list);
	}


}
