/**
 * Copyright
 * All right reserved.
 * 项目名称:
 * 创建日期:2022/3/9
 */
package org.springblade.modules.business.desk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
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.tenant.annotation.TenantDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.desk.entity.NoticeEntity;
import org.springblade.modules.business.desk.entity.NoticeViewEntity;
import org.springblade.modules.business.desk.service.INoticeService;
import org.springblade.modules.business.desk.service.INoticeViewService;
import org.springblade.modules.business.desk.vo.NoticeVO;
import org.springblade.modules.business.desk.wrapper.NoticeWrapper;
import org.springblade.modules.business.equipment.inspection.report.entity.EquipmentInspectionReportEntity;
import org.springblade.modules.business.equipment.inspection.report.vo.EquipmentInspectionReportVO;
import org.springblade.modules.business.equipment.inspection.report.wrapper.EquipmentInspectionReportWrapper;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 * Date:2022-03-08
 * Title:通知公告 控制器
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author admin
 * @mender：（文件的修改者，文件创建者之外的人）
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@TenantDS
@RestController
@RequestMapping(AppConstant.APPLICATION_DESK_NAME+ "/notice")
@AllArgsConstructor
@ApiSort(2)
@Api(value = "通知公告", tags = "通知公告")
public class NoticeController extends BladeController {

	private final INoticeService noticeService;
	private final INoticeViewService noticeViewService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入notice")
	public R<NoticeVO> detail(NoticeEntity noticeEntity) {
		NoticeEntity detail = noticeService.getOne(Condition.getQueryWrapper(noticeEntity));
		return R.data(NoticeWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
//	@ApiImplicitParams({
//		@ApiImplicitParam(name = "category", value = "公告类型", paramType = "query", dataType = "integer"),
//		@ApiImplicitParam(name = "title", value = "公告标题", paramType = "query", dataType = "string")
//	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入notice")
	public R<IPage<NoticeVO>> list( Query query) {
//		() -> NoticeWrapper.build().noticeQuery(notice)
		LambdaQueryWrapper<NoticeEntity> wrapper = Wrappers.<NoticeEntity>query().lambda();
		wrapper.orderByDesc(NoticeEntity::getReleaseTime);

		IPage<NoticeEntity> pages = noticeService.page(Condition.getPage(query),wrapper);
		return R.data(NoticeWrapper.build().pageVO(pages));
	}

	/**
	 * 多表联合查询自定义分页
	 */
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "category", value = "公告类型", paramType = "query", dataType = "integer"),
		@ApiImplicitParam(name = "title", value = "公告标题", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入notice")
	public R<IPage<NoticeVO>> page(@ApiIgnore NoticeVO notice, Query query) {
		IPage<NoticeVO> pages = noticeService.selectNoticePage(Condition.getPage(query), notice);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入notice")
	public R save(@RequestBody NoticeEntity noticeEntity) {
		return R.status(noticeService.save(noticeEntity));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入notice")
	public R update(@RequestBody NoticeEntity noticeEntity) {
		return R.status(noticeService.updateById(noticeEntity));
	}


	@PostMapping("/view")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "查阅")
	public R userView() {

		BladeUser user = AuthUtil.getUser();
		LambdaQueryWrapper<NoticeViewEntity> wrapper = Wrappers.<NoticeViewEntity>query().lambda();
		wrapper.eq(NoticeViewEntity::getCreateUser,user.getUserId());
		wrapper.eq(NoticeViewEntity::getIsDeleted,0);
		NoticeViewEntity entity=noticeViewService.getOne(wrapper);
		if (null != entity){
			entity.setViewTime(new Date());
		}else{
			entity= new NoticeViewEntity();
			entity.setViewTime(new Date());

		}
		noticeViewService.saveOrUpdate(entity);

		return R.success("OK");
	}


	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入notice")
	public R submit(@RequestBody NoticeEntity noticeEntity) {
		return R.status(noticeService.saveOrUpdate(noticeEntity));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入notice")
	public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
		boolean temp = noticeService.deleteLogic(Func.toLongList(ids));
		return R.status(temp);
	}

	/**
	 * 查询最新的数据
	 */
	@GetMapping("/last/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<List<NoticeVO>> lastList() {
		LambdaQueryWrapper<NoticeEntity> wrapper = Wrappers.<NoticeEntity>query().lambda();
		wrapper.orderByDesc(NoticeEntity::getCreateTime);
		wrapper.last("limit 1");
		List<NoticeEntity> pages = noticeService.list(wrapper);
		return R.data(NoticeWrapper.build().listVO(pages));
	}

}
