package org.springblade.modules.mdjftj.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.apache.commons.lang3.StringUtils;
import org.springblade.common.cache.DictBizCache;
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.mdjfpj.entity.PjEntity;
import org.springblade.modules.mdjfpj.service.IPjService;
import org.springblade.modules.mdjfdj.entity.MdjfDjEntity;
import org.springblade.modules.mdjfdj.service.IMdjfDjService;
import org.springblade.modules.mdjfry.service.IMdjfRyService;
import org.springblade.modules.mdjftj.entity.MdjfTjEntity;
import org.springblade.modules.mdjftj.excel.MdjfTjExcel;
import org.springblade.modules.mdjftj.service.IMdjfTjService;
import org.springblade.modules.mdjftj.vo.MdjfTjVO;
import org.springblade.modules.mdjftj.wrapper.MdjfTjWrapper;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

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

/**
 * 矛盾纠纷调解表 控制器
 *
 * @author BladeX
 * @since 2023-08-02
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-mdjftj/mdjftj")
@Api(value = "矛盾纠纷调解表", tags = "矛盾纠纷调解表接口")
public class MdjfTjController extends BladeController {

	private final IMdjfTjService mdjftjService;

	private final IMdjfDjService mdjfDjService;

	private final IMdjfRyService mdjfRyService;

	private final IPjService pjService;

	private final IDeptService deptService;

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

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

	/**
	 * 矛盾纠纷调解表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入mdjftj")
	public R save(@Valid @RequestBody MdjfTjEntity mdjftj) {
//		if (mdjftj.getTjzt().equals("进行列控")) {
//			String ryids = mdjftj.getRyIds();
//			mdjfRyService.updateLkry(ryids);
//		}
		if (null != mdjftj.getQmzpMap() && !mdjftj.getQmzpMap().isEmpty()) {
			mdjfRyService.updateAllQmzp(mdjftj.getQmzpMap());
			mdjfRyService.updateAllQmzpJl(mdjftj.getQmzpMap());
		}
		if (null != mdjftj.getRyzpMap() && !mdjftj.getRyzpMap().isEmpty()) {
			mdjfRyService.updateAllRyzp(mdjftj.getRyzpMap());
			mdjfRyService.updateAllRyzpJl(mdjftj.getRyzpMap());
		}
		boolean save = mdjftjService.save(mdjftj);
		String deptId = AuthUtil.getDeptId().split(",")[0];
		Dept dept = deptService.getById(Long.valueOf(deptId));
		if (dept.getAncestors().contains("220200000000")) {
			mdjftjService.saveMdjfTjJl(mdjftj);
		}
		if (mdjftj.getPjEntity() != null) {
			for (PjEntity pj : mdjftj.getPjEntity()) {
				PjEntity pjEntity = new PjEntity();
				pjEntity.setJfId(mdjftj.getJfId());
				pjEntity.setTjId(mdjftj.getId());
				pjEntity.setRyId(pj.getRyId());
				pjEntity.setPj(pj.getPj());
				pjService.save(pjEntity);
				if (dept.getAncestors().contains("220200000000")) {
					pjService.saveMdjfPjJl(pjEntity);
				}
			}
		}
		if(save){
			MdjfDjEntity mdjfDjEntity = mdjfDjService.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");
			}
			mdjfDjService.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 = "传入mdjftj")
	public R update(@Valid @RequestBody MdjfTjEntity mdjftj) {
		return R.status(mdjftjService.updateById(mdjftj));
	}

	/**
	 * 矛盾纠纷调解表 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入mdjftj")
	public R submit(@Valid @RequestBody MdjfTjEntity mdjftj) {

		return R.status(mdjftjService.saveOrUpdate(mdjftj));
	}

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


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

	/**
	 * 查询移交政府
	 */
	@GetMapping("selectZf")
	public R selectZf(){
		return R.data(mdjftjService.selectZf(AuthUtil.getDeptId()));
	}
}
