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

/**
 * 查庫流水賬記錄表 控制器
 *
 * @author BladeX
 * @since 2021-08-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("bank/checkwarehouse")
@Api(value = "查庫流水賬記錄表", tags = "查庫流水賬記錄表接口")
public class CheckWarehouseController extends BladeController {

	private final ICheckWarehouseService checkWarehouseService;

	/**
	 * 详情
	 */
	@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.getCheckUnit())){
			List<String> units = Func.toStrList(checkWarehouse.getCheckUnit());
			checkWarehouse.setCheckUnits(units);
		}
		if (StringUtils.isNotBlank(checkWarehouse.getCheckContent())){
			List<String> contents = Func.toStrList(checkWarehouse.getCheckContent());
			checkWarehouse.setCheckContents(contents);
		}
		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) {
		if (checkWarehouse.getId() == null){
			BladeUser currentUser = AuthUtil.getUser();
			checkWarehouse.setFillingPerson(currentUser.getUserName());
			checkWarehouse.setFillingDate(DateUtil.now());
		}
		return R.status(checkWarehouseService.saveOrUpdate(checkWarehouse));
	}


	/**
	 * 删除 查庫流水賬記錄表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(checkWarehouseService.deleteLogic(Func.toLongList(ids)));
	}


}
