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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.bank.postchange.entity.PostChange;
import org.springblade.bank.postchange.vo.PostChangeVO;
import org.springblade.bank.postchange.wrapper.PostChangeWrapper;
import org.springblade.bank.postchange.service.IPostChangeService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 * 網點出納主管及專管員崗位表更記錄表 控制器
 *
 * @author BladeX
 * @since 2021-08-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("bank/postchange")
@Api(value = "網點出納主管及專管員崗位表更記錄表", tags = "網點出納主管及專管員崗位表更記錄表接口")
public class PostChangeController extends BladeController {

	private final IPostChangeService postChangeService;
	private final IUserService userService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入postChange")
	public R<PostChangeVO> detail(PostChange postChange) {
		PostChange detail = postChangeService.getOne(Condition.getQueryWrapper(postChange));
		return R.data(PostChangeWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 網點出納主管及專管員崗位表更記錄表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入postChange")
	public R<IPage<PostChangeVO>> list(PostChange postChange, Query query) {
		IPage<PostChange> pages = postChangeService.page(Condition.getPage(query), Condition.getQueryWrapper(postChange));
		return R.data(PostChangeWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 網點出納主管及專管員崗位表更記錄表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入postChange")
	public R<IPage<PostChangeVO>> page(PostChangeVO postChange, Query query) {
		postChange.setCurrentuserId(AuthUtil.getUser().getUserId());
		IPage<PostChangeVO> pages = postChangeService.selectPostChangePage(Condition.getPage(query), postChange);
		return R.data(pages);
	}

	/**
	 * 新增 網點出納主管及專管員崗位表更記錄表
	 */
	@PostMapping("/settingSave")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入postChange")
	public R settingSave(@Valid @RequestBody PostChange postChange) {
		PostChange one = postChangeService.getOne(new QueryWrapper<>(new PostChange()).lambda().eq(PostChange::getId, 1L));

		if (one == null){
			postChange.setId(1L);
			return R.status(postChangeService.save(postChange));
		}else{
			one.setPersonId(postChange.getPersonId());
			one.setPersonName(postChange.getPersonName());
			one.setPersonNo(postChange.getPersonNo());
			return R.status(postChangeService.updateById(one));
		}
	}

	/**
	 * 修改 網點出納主管及專管員崗位表更記錄表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入postChange")
	public R update(@Valid @RequestBody PostChange postChange) {
		return R.status(postChangeService.updateById(postChange));
	}

	/**
	 * 新增或修改 網點出納主管及專管員崗位表更記錄表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入postChange")
	public R submit(@Valid @RequestBody PostChange postChange) {

		if (postChange.getPersonId() == null || postChange.getPersonNo() == null){
			PostChange setting = postChangeService.getOne(new QueryWrapper<>(new PostChange()).lambda().eq(PostChange::getId, 1L));
			Assert.notNull(setting, "請先設置審批人！");
			postChange.setPersonId(setting.getPersonId());
			postChange.setPersonNo(setting.getPersonNo());
			postChange.setPersonName(setting.getPersonName());
		}

		if (StringUtils.isNotBlank(postChange.getPersonNo())){
			User user = userService.getById(AuthUtil.getUser().getUserId());
			if (user.getEhr().equals(postChange.getPersonNo())){
				Assert.notNull(null, "發佈人和發起人不能為同一人！");
			}
		}


		postChange.setProcess(2);

		PostChange last = postChangeService.getLastRecord();
		if (last != null){
			//上一条记录
			postChange.setPreviousId(last.getId());
		}
		postChange.setIsCompleted(0);

		return R.status(postChangeService.save(postChange));
	}


	/**
	 * 删除 網點出納主管及專管員崗位表更記錄表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(postChangeService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 获取审批人设置
	 */
	@GetMapping("/getSetting")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取审批人设置", notes = "")
	public R getSetting() {
		PostChange one = postChangeService.getOne(new QueryWrapper<>(new PostChange()).lambda().eq(PostChange::getId, 1L));
		return R.data(one);
	}

	/**
	 * 审批确认
	 */
	@PostMapping("/approve")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "审批确认", notes = "传入id")
	public R approve(@ApiParam(value = "主键集合", required = true) @RequestParam Long id) {
		PostChange postchange = postChangeService.getById(id);
		postchange.setProcess(3);
		postchange.setIsCompleted(1);

		return R.status(postChangeService.updateById(postchange));
	}

	/**
	 * 获取审批人设置
	 */
	@GetMapping("/getLastRecord")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取最新一条记录", notes = "")
	public R getLastRecord() {
		Query query = new Query();
		query.setSize(1);
		query.setCurrent(1);
		IPage<PostChangeVO> pages = postChangeService.selectPostChangePage(Condition.getPage(query), new PostChangeVO());
		List<PostChangeVO> records = pages.getRecords();
		if (records.size() > 0){
			return R.data(records.get(0));
		}else{
			return R.data(null);
		}
	}


}
