package org.springblade.modules.sqmy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
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.annotation.PreAuth;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.sqmy.excel.JmhInfoExcel;
import org.springblade.modules.sqmy.pojo.entity.JmhInfoEntity;
import org.springblade.modules.sqmy.pojo.entity.JmhRyEntity;
import org.springblade.modules.sqmy.pojo.vo.JmhInfoVO;
import org.springblade.modules.sqmy.service.IJmhInfoService;
import org.springblade.modules.sqmy.service.IJmhRyService;
import org.springblade.modules.sqmy.wrapper.JmhInfoWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 每周见面会 控制器
 *
 * @author wth
 * @since 2024-11-12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/jmhInfo")
@Api(value = "每周见面会", tags = "每周见面会接口")
public class JmhInfoController extends BladeController {

	private final IJmhInfoService jmhInfoService;

	private final IJmhRyService jmhRyService;

	/**
	 * 每周见面会 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes  = "传入jmhInfo")
	public R<JmhInfoVO> detail(JmhInfoEntity jmhInfo) {
		// 查询会议信息
		JmhInfoVO jmhInfoVO = jmhInfoService.detailById(jmhInfo.getId());
		// 查询人员信息
		QueryWrapper<JmhRyEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("jmh_id", jmhInfoVO.getId());
		List<JmhRyEntity> ryList = jmhRyService.list(queryWrapper);
		jmhInfoVO.setRyList(ryList);
		return R.data(jmhInfoVO);
	}

	/**
	 * 每周见面会 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes  = "传入jmhInfo")
	public R<IPage<JmhInfoVO>> list(@RequestParam Map<String, Object> jmhInfo, Query query) {
		IPage<JmhInfoEntity> pages = jmhInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(jmhInfo, JmhInfoEntity.class));
		return R.data(JmhInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 每周见面会 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes  = "传入jmhInfo")
	public R page(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		IPage<JmhInfoVO> pages = jmhInfoService.selectJmhInfoPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 每周见面会 新增
	 */
	@Transactional
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes  = "传入jmhInfo")
	public R save(@Valid @RequestBody JmhInfoVO jmhInfo) {
		jmhInfoService.save(jmhInfo);
		if (null != jmhInfo.getRyList() && jmhInfo.getRyList().size() > 0) {
			for (JmhRyEntity jmhRyEntity : jmhInfo.getRyList()) {
				jmhRyEntity.setJmhId(jmhInfo.getId());
				jmhRyService.save(jmhRyEntity);
			}
		}
		return R.status(true);
	}

	/**
	 * 每周见面会 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes  = "传入jmhInfo")
	public R update(@Valid @RequestBody JmhInfoVO jmhInfo) {
		jmhInfoService.updateById(jmhInfo);
		if (null != jmhInfo.getRyList() && jmhInfo.getRyList().size() > 0) {
			for (JmhRyEntity jmhRyEntity : jmhInfo.getRyList()) {
				if (null != jmhRyEntity.getIsDeleted() && jmhRyEntity.getIsDeleted() == 1) {
					jmhRyService.removeById(jmhRyEntity.getId());
				} else {
					jmhRyEntity.setJmhId(jmhInfo.getId());
					jmhRyService.saveOrUpdate(jmhRyEntity);
				}
			}
		}
		return R.status(true);
	}

	/**
	 * 每周见面会 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes  = "传入jmhInfo")
	public R submit(@Valid @RequestBody JmhInfoEntity jmhInfo) {
		return R.status(jmhInfoService.saveOrUpdate(jmhInfo));
	}

	/**
	 * 每周见面会 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes  = "传入ids")
	public R remove(@RequestParam String ids) {
		return R.status(jmhInfoService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-jmhInfo")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "导出数据", notes  = "传入jmhInfo")
	public void exportJmhInfo(@RequestParam Map<String, Object> jmhInfo, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<JmhInfoEntity> queryWrapper = Condition.getQueryWrapper(jmhInfo, JmhInfoEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(JmhInfoEntity::getTenantId, bladeUser.getTenantId());
		//}
		//queryWrapper.lambda().eq(JmhInfoEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<JmhInfoExcel> list = jmhInfoService.exportJmhInfo(queryWrapper);
		ExcelUtil.export(response, "每周见面会数据" + DateUtil.time(), "每周见面会数据表", list, JmhInfoExcel.class);
	}

}
