package org.springblade.modules.byMonth.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 io.swagger.annotations.ApiParam;
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.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.byMonth.entity.ByMonthEntity;
import org.springblade.modules.byMonth.excel.ByMonthExcel;
import org.springblade.modules.byMonth.service.IByMonthService;
import org.springblade.modules.byMonth.vo.ByMonthVO;
import org.springblade.modules.byMonth.wrapper.ByMonthWrapper;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

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

/**
 * 月评分确认 控制器
 *
 * @author BladeX
 * @since 2023-07-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-byMonth/byMonth")
@Api(value = "月评分确认", tags = "月评分确认接口")
public class ByMonthController extends BladeController {

	private final IByMonthService byMonthService;

	/**
	 * 月评分确认 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入byMonth")
	public R<ByMonthVO> detail(ByMonthEntity byMonth) {
		ByMonthEntity detail = byMonthService.getOne(Condition.getQueryWrapper(byMonth));
		return R.data(ByMonthWrapper.build().entityVO(detail));
	}
	/**
	 * 月评分确认 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入byMonth")
	public R<IPage<ByMonthVO>> list(@ApiIgnore @RequestParam Map<String, Object> byMonth, Query query) {
		IPage<ByMonthEntity> pages = byMonthService.page(Condition.getPage(query), Condition.getQueryWrapper(byMonth, ByMonthEntity.class));
		return R.data(ByMonthWrapper.build().pageVO(pages));
	}

	/**
	 * 月评分确认 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入byMonth")
	public R page(@RequestParam Map<String, Object> params, Query query) {
		IPage<ByMonthVO> pages = byMonthService.selectByMonthPage(Condition.getPage(query), params);
		for (ByMonthVO byMonthVO:pages.getRecords()){
			byMonthVO.setTotalScore(byMonthVO.getTotalScore() + byMonthVO.getYbshfs() + byMonthVO.getZdryfs() + byMonthVO.getXxyfs() + byMonthVO.getDljtfs() + byMonthVO.getXfjdfs() + byMonthVO.getMdjffs() + byMonthVO.getFxyjfs() + byMonthVO.getQfqzfs() + byMonthVO.getSqmjfs());
		}
		return R.data(pages);
	}

	/**
	 * 月评分确认 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入byMonth")
	public R save(@Valid @RequestBody ByMonthEntity byMonth) {
		return R.status(byMonthService.save(byMonth));
	}

	/**
	 * 月评分确认 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入byMonth")
	public R update(@Valid @RequestBody ByMonthEntity byMonth) {
		return R.status(byMonthService.updateById(byMonth));
	}

	/**
	 * 月评分确认 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入byMonth")
	public R submit(@Valid @RequestBody ByMonthEntity byMonth) {
		if (byMonth.getDjgzFs()!=null){
			double djfs = Double.parseDouble(byMonth.getDjgzFs());
			if (djfs>10){
				return R.fail("党建工作的分数不能大于10");
			}
		}
		if (byMonth.getCqkqFs()!=null){
			double cqfs = Double.parseDouble(byMonth.getCqkqFs());
			if (cqfs>10){
				return R.fail("出勤考核的分数不能大于10");
			}
		}
		if (byMonth.getGzjlFs()!=null){
			double gzfs = Double.parseDouble(byMonth.getGzjlFs());
			if (gzfs>10){
				return R.fail("工作纪律的分数不能大于10");
			}
		}
		return R.status(byMonthService.saveOrUpdate(byMonth));
	}

	/**
	 * 月评分确认 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(byMonthService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-byMonth")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入byMonth")
	public void exportByMonth(@ApiIgnore @RequestParam Map<String, Object> byMonth, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<ByMonthEntity> queryWrapper = Condition.getQueryWrapper(byMonth, ByMonthEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(ByMonth::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(ByMonthEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<ByMonthExcel> list = byMonthService.exportByMonth(queryWrapper);
		ExcelUtil.export(response, "月评分确认数据" + DateUtil.time(), "月评分确认数据表", list, ByMonthExcel.class);
	}

}
