/*
 *      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.mdjfxj.tjXj.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springblade.common.cache.DictBizCache;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.mdjfdj.entity.MdjfDjEntity;
import org.springblade.modules.mdjfpj.entity.PjEntity;
import org.springblade.modules.mdjftj.entity.MdjfTjEntity;
import org.springblade.modules.mdjfxj.djXj.entity.DjXjEntity;
import org.springblade.modules.mdjfxj.djXj.service.IDjXjService;
import org.springblade.modules.mdjfxj.hfXj.service.IHfXjService;
import org.springblade.modules.mdjfxj.pjXj.entity.PjXjEntity;
import org.springblade.modules.mdjfxj.pjXj.service.IPjXjService;
import org.springblade.modules.mdjfxj.ryXj.service.IRyXjService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.mdjfxj.tjXj.entity.TjXjEntity;
import org.springblade.modules.mdjfxj.tjXj.vo.TjXjVO;
import org.springblade.modules.mdjfxj.tjXj.excel.TjXjExcel;
import org.springblade.modules.mdjfxj.tjXj.wrapper.TjXjWrapper;
import org.springblade.modules.mdjfxj.tjXj.service.ITjXjService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.BladeConstant;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * 矛盾纠纷调解表 控制器
 *
 * @author BladeX
 * @since 2024-04-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-tjXj/tjXj")
@Api(value = "矛盾纠纷调解表", tags = "矛盾纠纷调解表接口")
public class TjXjController extends BladeController {

	private final IDjXjService djXjService;

	private final IHfXjService hfXjService;

	private final IRyXjService ryXjService;

	private final ITjXjService tjXjService;

	private final IPjXjService pjXjService;

	/**
	 * 矛盾纠纷调解表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入tjXj")
	public R<TjXjVO> detail(TjXjEntity tjXj) {
		TjXjEntity detail = tjXjService.getOne(Condition.getQueryWrapper(tjXj));
		return R.data(TjXjWrapper.build().entityVO(detail));
	}
	/**
	 * 矛盾纠纷调解表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入tjXj")
	public R<IPage<TjXjVO>> list(@ApiIgnore @RequestParam Map<String, Object> tjXj, Query query) {
		IPage<TjXjEntity> pages = tjXjService.page(Condition.getPage(query), Condition.getQueryWrapper(tjXj, TjXjEntity.class));
		return R.data(TjXjWrapper.build().pageVO(pages));
	}

	/**
	 * 矛盾纠纷调解表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入tjXj")
	public R<IPage<TjXjVO>> page(@RequestParam Map<String, Object> params, Query query) {
		IPage<TjXjVO> pages = tjXjService.selectTjXjPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 矛盾纠纷调解表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入mdjftj")
	public R save(@Valid @RequestBody TjXjEntity mdjftj) {
//		if (mdjftj.getTjzt().equals("进行列控")) {
//			String ryids = mdjftj.getRyIds();
//			mdjfRyService.updateLkry(ryids);
//		}
		if (null != mdjftj.getQmzpMap() && !mdjftj.getQmzpMap().isEmpty()) {
			ryXjService.updateAllQmzp(mdjftj.getQmzpMap());
		}
		if (null != mdjftj.getRyzpMap() && !mdjftj.getRyzpMap().isEmpty()) {
			ryXjService.updateAllRyzp(mdjftj.getRyzpMap());
		}
		boolean save = tjXjService.save(mdjftj);
		if (mdjftj.getPjEntity() != null) {
			for (PjEntity pj : mdjftj.getPjEntity()) {
				PjXjEntity pjEntity = new PjXjEntity();
				pjEntity.setJfId(mdjftj.getJfId());
				pjEntity.setTjId(mdjftj.getId());
				pjEntity.setRyId(pj.getRyId());
				pjEntity.setPj(pj.getPj());
				pjXjService.save(pjEntity);
			}
		}
		if(save){
			DjXjEntity mdjfDjEntity = djXjService.getById(mdjftj.getJfId());
			//判断调节状态是否为已调解 已调解的话需要修改该条矛盾的回访截止时间 hfjzsj  默认矛盾等级的第一次回访期限
			if("已调解".equals(mdjftj.getTjzt())){
				mdjfDjEntity.setHfztNew("0");
				if(StringUtils.isNotBlank(mdjfDjEntity.getMdjbV())){
					String qxValue = DictBizCache.getValue("mddjqx", mdjfDjEntity.getMdjbV());
					Date laterDate = getlaterDate(mdjftj.getCreateTime(), Integer.valueOf(qxValue));
					mdjfDjEntity.setHfjzsj(laterDate);
					mdjfDjEntity.setHfjzsj2(laterDate);
					mdjfDjEntity.setHfjzsj3(laterDate);
					//if A  需放回3次  分别保存对应的超时时间
					if("A".equals(mdjfDjEntity.getMdjbV())){
						Date aDate2 = getlaterDate(laterDate, Integer.valueOf(DictBizCache.getValue("mddjqx", "A2")));
						mdjfDjEntity.setHfjzsj2(aDate2);
						mdjfDjEntity.setHfjzsj3(getlaterDate(aDate2,Integer.valueOf(DictBizCache.getValue("mddjqx", "A3"))));
					}
					//if B  需放2次  分别保存对应的超时时间
					if("B".equals(mdjfDjEntity.getMdjbV())){
						Date bDate2 = getlaterDate(laterDate, Integer.valueOf(DictBizCache.getValue("mddjqx", "B2")));
						mdjfDjEntity.setHfjzsj2(bDate2);
						mdjfDjEntity.setHfjzsj3(bDate2);
					}

				}
			}
			if("已流转".equals(mdjftj.getTjzt())){
				mdjfDjEntity.setHfztNew("3");
			}
			djXjService.saveOrUpdate(mdjfDjEntity);
		}
		return R.status(save);
	}

	/**
	 * 根据矛盾已调解的时间、矛盾级别对应的回访期限，计算回访截止日期
	 */
	private static Date getlaterDate(Date date,int qxValue){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, qxValue);
		calendar.add(Calendar.HOUR, 0);
		calendar.add(Calendar.MINUTE, 0);
		calendar.add(Calendar.SECOND, 0);
//				calendar.add(Calendar.DAY_OF_MONTH, 0);
//		calendar.add(Calendar.HOUR, 0);
//		calendar.add(Calendar.MINUTE, qxValue);
//		calendar.add(Calendar.SECOND, 0);
		Date newDate = calendar.getTime();
		return newDate;
	}

	/**
	 * 矛盾纠纷调解表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入tjXj")
	public R update(@Valid @RequestBody TjXjEntity tjXj) {
		return R.status(tjXjService.updateById(tjXj));
	}

	/**
	 * 矛盾纠纷调解表 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入tjXj")
	public R submit(@Valid @RequestBody TjXjEntity tjXj) {
		return R.status(tjXjService.saveOrUpdate(tjXj));
	}

	/**
	 * 矛盾纠纷调解表 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(tjXjService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-tjXj")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入tjXj")
	public void exportTjXj(@ApiIgnore @RequestParam Map<String, Object> tjXj, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<TjXjEntity> queryWrapper = Condition.getQueryWrapper(tjXj, TjXjEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(TjXj::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(TjXjEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<TjXjExcel> list = tjXjService.exportTjXj(queryWrapper);
		ExcelUtil.export(response, "矛盾纠纷调解表数据" + DateUtil.time(), "矛盾纠纷调解表数据表", list, TjXjExcel.class);
	}
}
