/*
 *      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.warehouse.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.checklist.entity.Checklist;
import org.springblade.bank.tellertrunk.entity.TellerTrunk;
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.warehouse.entity.CheckWarehouse;
import org.springblade.bank.warehouse.vo.CheckWarehouseVO;
import org.springblade.bank.warehouse.wrapper.CheckWarehouseWrapper;
import org.springblade.bank.warehouse.service.ICheckWarehouseService;
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/checkwarehouse")
@Api(value = "查庫流水賬記錄表", tags = "查庫流水賬記錄表接口")
public class CheckWarehouseController extends BladeController {

	private final ICheckWarehouseService checkWarehouseService;
	private final IUserService userService;
	private final IDeptService deptService;
	private final IUserLogService userLogService;
	private final IDictService dictService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入checkWarehouse")
	public R<CheckWarehouseVO> detail(CheckWarehouse checkWarehouse) {
		CheckWarehouse detail = checkWarehouseService.getOne(Condition.getQueryWrapper(checkWarehouse));
		return R.data(CheckWarehouseWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 查庫流水賬記錄表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入checkWarehouse")
	public R<IPage<CheckWarehouseVO>> list(CheckWarehouse checkWarehouse, Query query) {
		IPage<CheckWarehouse> pages = checkWarehouseService.page(Condition.getPage(query), Condition.getQueryWrapper(checkWarehouse));
		return R.data(CheckWarehouseWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 查庫流水賬記錄表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入checkWarehouse")
	public R<IPage<CheckWarehouseVO>> page(CheckWarehouseVO checkWarehouse, Query query) {
		if (StringUtils.isNotBlank(checkWarehouse.getOrgNostr())){
			List<String> orgNos = Func.toStrList(checkWarehouse.getOrgNostr());
			checkWarehouse.setOrgNos(orgNos);
		}
		IPage<CheckWarehouseVO> pages = checkWarehouseService.selectCheckWarehousePage(Condition.getPage(query), checkWarehouse);
		return R.data(pages);
	}

	/**
	 * 新增 查庫流水賬記錄表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入checkWarehouse")
	public R save(@Valid @RequestBody CheckWarehouse checkWarehouse) {
		return R.status(checkWarehouseService.save(checkWarehouse));
	}

	/**
	 * 修改 查庫流水賬記錄表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入checkWarehouse")
	public R update(@Valid @RequestBody CheckWarehouse checkWarehouse) {
		return R.status(checkWarehouseService.updateById(checkWarehouse));
	}

	/**
	 * 新增或修改 查庫流水賬記錄表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入checkWarehouse")
	public R submit(@Valid @RequestBody CheckWarehouse checkWarehouse) {

		boolean isAdd = checkWarehouse.getId() == null;
		BladeUser currentUser = AuthUtil.getUser();
		User user = userService.getById(currentUser.getUserId());
		Dept dept = deptService.getById(user.getDeptId());
		UserLog userLog = new UserLog();

		CheckWarehouse old = null;
		if (isAdd){
			checkWarehouse.setFillingPerson(user.getName());
			checkWarehouse.setFillingDate(DateUtil.now());
		}else{
			old = checkWarehouseService.getById(checkWarehouse.getId());
		}

		if (checkWarehouseService.saveOrUpdate(checkWarehouse)){
			userLog.setTableName("checkwarehouse");
			userLog.setBankNo(dept.getBankNo());
			userLog.setOrgNo(dept.getOrgNo());
			userLog.setOrgName(dept.getDeptName());
			userLog.setNewData(JSONUtil.toJsonStr(checkWarehouse));
			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();
			CheckWarehouse warehouse = checkWarehouseService.getById(id);
			userLog.setTableName("checkwarehouse");
			userLog.setOperationType("del");
			userLog.setBankNo(dept.getBankNo());
			userLog.setOrgNo(dept.getOrgNo());
			userLog.setOrgName(dept.getDeptName());
			userLog.setOldData(JSONUtil.toJsonStr(warehouse));
			userLog.setPersonNo(user.getEhr());
			userLog.setPersonName(user.getName());
			userLogList.add(userLog);
		});

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

	@GetMapping("/getList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入checkWarehouse")
	public R getList(CheckWarehouseVO checkWarehouse) {
		if (StringUtils.isNotBlank(checkWarehouse.getOrgNostr())){
			List<String> orgNos = Func.toStrList(checkWarehouse.getOrgNostr());
			checkWarehouse.setOrgNos(orgNos);
		}

		List<CheckWarehouse> list = checkWarehouseService.getList(checkWarehouse);

		list.forEach(item -> {
			String checkUnit = item.getCheckUnit();
			String checkPurpose = item.getCheckPurpose();
			String checkContent = item.getCheckContent();
			String checkResult = item.getCheckResult();

			String[] splitUnit = checkUnit.split(",");
			String[] splitPurpose = checkPurpose.split(",");
			String[] splitContent = checkContent.split(",");
			checkUnit = ""; checkPurpose = ""; checkContent = "";
			for (String unit : splitUnit) {
				checkUnit += dictService.getValue("unit_type", unit) + " | ";
			}
			for (String purpose : splitPurpose) {
				checkPurpose += dictService.getValue("check_purpose", purpose) + " | ";
			}
			for (String content : splitContent) {
				checkContent += dictService.getValue("check_content_warehouse", content) + " | ";
			}

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

		});

		return R.data(list);
	}

}
