/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.rwxffkb.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.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.mdjfdj.entity.MdjfDjEntity;
import org.springblade.modules.mdjfdj.service.IMdjfDjService;
import org.springblade.modules.mdjfdj.vo.MdjfDjVO;
import org.springblade.modules.mdjfry.entity.MdjfRyEntity;
import org.springblade.modules.mdjfry.service.IMdjfRyService;
import org.springblade.modules.rwxffkb.entity.RwxffkbEntity;
import org.springblade.modules.rwxffkb.excel.RwxffkbExcel;
import org.springblade.modules.rwxffkb.service.IRwxffkbService;
import org.springblade.modules.rwxffkb.vo.RwxffkbVO;
import org.springblade.modules.rwxffkb.wrapper.RwxffkbWrapper;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 任务下发反馈 控制器
 *
 * @author ldx
 * @since 2023-09-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-rwxffkb/rwxffkb")
@Api(value = "任务下发反馈", tags = "任务下发反馈接口")
public class RwxffkbController extends BladeController {

	private final IRwxffkbService rwxffkbService;

	private final IMdjfDjService mdjfDjService;

	private final IMdjfRyService mdjfRyService;

	/**
	 * 任务下发反馈 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入rwxffkb")
	public R<RwxffkbVO> detail(RwxffkbEntity rwxffkb) {
		RwxffkbEntity detail = rwxffkbService.getOne(Condition.getQueryWrapper(rwxffkb));
		return R.data(RwxffkbWrapper.build().entityVO(detail));
	}
	/**
	 * 任务下发反馈 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入rwxffkb")
	public R<IPage<RwxffkbVO>> list(@ApiIgnore @RequestParam Map<String, Object> rwxffkb, Query query) {
		IPage<RwxffkbEntity> pages = rwxffkbService.page(Condition.getPage(query), Condition.getQueryWrapper(rwxffkb, RwxffkbEntity.class));
		return R.data(RwxffkbWrapper.build().pageVO(pages));
	}

	/**
	 * 任务下发反馈 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入rwxffkb")
	public R page(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		params.put("userId", AuthUtil.getUserId());
		IPage<RwxffkbVO> pages = rwxffkbService.selectRwxffkbPage(Condition.getPage(query), params);
		for (RwxffkbVO rwxffkbVO:pages.getRecords()) {
			List<MdjfRyEntity> mdjfry = mdjfRyService.getRyByMdIdJl(rwxffkbVO.getMdid());
			if (mdjfry.size() != 0) {
				MdjfRyEntity byId = mdjfRyService.getById(mdjfry.get(0).getId());
				if (byId != null) {
					rwxffkbVO.setType("1");
				}
			}
			rwxffkbVO.setMdjfRyList(mdjfry);
		}
		return R.data(pages);
	}

	/**
	 * 任务下发反馈 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入rwxffkb")
	public R save(@Valid @RequestBody RwxffkbEntity rwxffkb) {
		return R.status(rwxffkbService.save(rwxffkb));
	}

	/**
	 * 任务下发反馈 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入rwxffkb")
	public R update(@Valid @RequestBody RwxffkbVO rwxffkb) {
		rwxffkb.setRwzt("已签收");
		rwxffkb.setRwjssj(new Date());
		rwxffkb.setUserId(AuthUtil.getUserId());
		rwxffkbService.updateByIdJl(rwxffkb);
		/*if (rwxffkb.getRwly().equals("mdjf_dj")) {
			MdjfDjEntity mdjfDjEntity = new MdjfDjEntity();
			BeanUtil.copy(rwxffkb, mdjfDjEntity);
			mdjfDjService.save(mdjfDjEntity);
			mdjfDjService.saveJl(mdjfDjEntity);
			for (MdjfRyEntity mdjfRyEntity : rwxffkb.getMdjfRyList()) {
				MdjfRyEntity mdjfRy = new MdjfRyEntity();
				BeanUtil.copy(mdjfRyEntity, mdjfRy);
				mdjfRyService.save(mdjfRy);
				mdjfRyService.saveJl(mdjfRy);
			}
		}*/
		return R.data("操作成功");
	}

	/**
	 * 6位行政区划+13位时间戳+4位随机数  id
	 * @return
	 */
	public static String generateId(String xzqh) {
		StringBuilder id = new StringBuilder();
		id.append(xzqh);
		id.append(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()).substring(8,17));
		id.append(String.format("%03d", new Random().nextInt(1000)));
		return id.toString();
	}

	/**
	 * 任务下发反馈 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入rwxffkb")
	public R submit(@Valid @RequestBody RwxffkbEntity rwxffkb) {
		return R.status(rwxffkbService.saveOrUpdate(rwxffkb));
	}

	/**
	 * 任务下发反馈 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(rwxffkbService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-rwxffkb")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入rwxffkb")
	public void exportRwxffkb(@ApiIgnore @RequestParam Map<String, Object> rwxffkb, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<RwxffkbEntity> queryWrapper = Condition.getQueryWrapper(rwxffkb, RwxffkbEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Rwxffkb::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(RwxffkbEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<RwxffkbExcel> list = rwxffkbService.exportRwxffkb(queryWrapper);
		ExcelUtil.export(response, "任务下发反馈数据" + DateUtil.time(), "任务下发反馈数据表", list, RwxffkbExcel.class);
	}
}
