package org.springblade.business.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.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.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.business.entity.ConstructionLedger;
import org.springblade.business.vo.ConstructionLedgerVO;
import org.springblade.business.wrapper.ConstructionLedgerWrapper;
import org.springblade.business.service.IConstructionLedgerService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Date;

/**
 *  施工台账控制器
 *
 * @author BladeX
 * @since 2022-05-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/constructionLedger")
@Api(tags = "施工台账接口")
public class ConstructionLedgerController extends BladeController {

	private final IConstructionLedgerService constructionLedgerService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入id")
	public R<ConstructionLedgerVO> detail(@RequestParam String id) {
		ConstructionLedger detail = this.constructionLedgerService.getById(id);
		return R.data(ConstructionLedgerWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页（不建议）", notes = "传入constructionLedger")
	public R<IPage<ConstructionLedgerVO>> list(ConstructionLedger constructionLedger, Query query) {
		IPage<ConstructionLedger> pages = this.constructionLedgerService.page(Condition.getPage(query), Condition.getQueryWrapper(constructionLedger));
		return R.data(ConstructionLedgerWrapper.build().pageVO(pages));
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页（建议）", notes = "传入constructionLedger")
	public R<IPage<ConstructionLedgerVO>> page(ConstructionLedgerVO constructionLedger, Query query) {
		IPage<ConstructionLedgerVO> pages = this.constructionLedgerService.selectConstructionLedgerPage(Condition.getPage(query), constructionLedger);
		return R.data(pages);
	}

	/**
	 * 新增 
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增（不需要）", notes = "传入constructionLedger")
	public R<Boolean> save(@Valid @RequestBody ConstructionLedger constructionLedger) {
		setUserData(constructionLedger, true);
		return R.status(this.constructionLedgerService.save(constructionLedger));
	}

	/**
	 * 修改 
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入constructionLedger")
	public R<Boolean> update(@Valid @RequestBody ConstructionLedger constructionLedger) {
		setUserData(constructionLedger, false);
		return R.status(this.constructionLedgerService.updateById(constructionLedger));
	}

	/**
	 * 删除 
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R<Boolean> remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(this.constructionLedgerService.deleteLogic(Func.toLongList(ids)));
	}

	//设置登录用户信息
	private void setUserData(ConstructionLedger constructionLedger, boolean isCreate){
		//获取当前登录人
		BladeUser user = AuthUtil.getUser();
		//记录操作人信息
		if(isCreate){
			constructionLedger.setCreateUser(user.getUserId());
			constructionLedger.setCreateDept(Long.parseLong(user.getDeptId()));
		} else {
			constructionLedger.setUpdateUser(user.getUserId());
			constructionLedger.setUpdateTime(new Date());
		}
	}

}
