package org.springblade.audit.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.audit.entity.AuditUploadUser;
import org.springblade.audit.enums.AuditsEnum;
import org.springblade.audit.query.AuditPageQuery;
import org.springblade.audit.service.IAuditUploadUserService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zbc
 * @since 2021-12-02
 */
@RestController
@RequestMapping("/audit/user")
@Api(value = "审核上传人管理", tags = "审核上传人接口")
public class AuditUploadUserController {

	@Autowired
	private IAuditUploadUserService auditUploadUserService;


	@PostMapping("/save")
	@ApiOperation(value = "新增审核上传人")
	public R save(@Valid @RequestBody AuditUploadUser auditUploadUser) {
		List<AuditUploadUser> list = auditUploadUserService.list(new LambdaQueryWrapper<AuditUploadUser>()
			.eq(AuditUploadUser::getReportType, auditUploadUser.getReportType())
		);
		if(!list.isEmpty() || list.size() > 0){
			return R.fail("该报表分类已录入，不能重复录入");
		}
		auditUploadUser.setCreateTime(new Date());
		return R.status(auditUploadUserService.save(auditUploadUser));
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改审核上传人")
	public R update(@RequestBody AuditUploadUser auditUploadUser) {
		List<AuditUploadUser> list = auditUploadUserService.list(new LambdaQueryWrapper<AuditUploadUser>()
			.eq(AuditUploadUser::getReportType, auditUploadUser.getReportType())
			.ne(AuditUploadUser::getId,auditUploadUser.getId())
		);
		if(!list.isEmpty() || list.size() > 0){
			return R.fail("该报表分类已录入，不能重复录入");
		}
		return R.status(auditUploadUserService.updateById(auditUploadUser));
	}

	@GetMapping("/page")
	@ApiOperation(value = "审核上传人分页")
	public R<IPage<AuditUploadUser>> page(AuditPageQuery pageQuery, Query query) {
		IPage<AuditUploadUser> page = Condition.getPage(query);
		IPage<AuditUploadUser> pages = auditUploadUserService.getPageByQuery(page, pageQuery);
		List<AuditUploadUser> records = pages.getRecords();
		AuditsEnum[] values = AuditsEnum.values();
		for (AuditsEnum value : values) {
			for (AuditUploadUser record : records) {
				if(value.getCode().equals(record.getReportType())){
					record.setReportName(value.getName());
				}
			}
		}
		return R.data(pages);
	}

	@PostMapping("/getCode")
	@ApiOperation(value = "获取报表分类code")
	public R getCode() {
		return R.data(AuditsEnum.values());
	}

}

