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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.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.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.mdjfhf.entity.HfEntity;
import org.springblade.modules.mdjfhf.excel.HfExcel;
import org.springblade.modules.mdjfhf.service.IHfService;
import org.springblade.modules.mdjfhf.vo.HfVO;
import org.springblade.modules.mdjfhf.wrapper.HfWrapper;
import org.springblade.modules.mdjfry.service.IMdjfRyService;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * 矛盾纠纷-回访 控制器
 *
 * @author BladeX
 * @since 2023-11-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-hf/hf")
@Api(value = "矛盾纠纷-回访", tags = "矛盾纠纷-回访接口")
public class HfController extends BladeController {

	private final IHfService hfService;
	private final IMdjfRyService mdjfRyService;
	private final IMdjfDjService mdjfDjService;

	private final IDeptService deptService;

	/**
	 * 矛盾纠纷-回访 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入hf")
	public R<HfVO> detail(HfEntity hf) {
		HfEntity detail = hfService.getOne(Condition.getQueryWrapper(hf));
		return R.data(HfWrapper.build().entityVO(detail));
	}

	/**
	 * 矛盾纠纷-回访 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入hf")
	public R<IPage<HfVO>> list(@ApiIgnore @RequestParam Map<String, Object> hf, Query query) {
		IPage<HfEntity> pages = hfService.page(Condition.getPage(query), Condition.getQueryWrapper(hf, HfEntity.class));
		return R.data(HfWrapper.build().pageVO(pages));
	}

	/**
	 * 矛盾纠纷-回访-app列表
	 */
	@GetMapping("/listByJfId")
	public R listByJfId(@RequestParam Map<String, Object> params) {
		QueryWrapper wrapper = new QueryWrapper<>();
		wrapper.eq("jf_id", Long.valueOf(params.get("jfId").toString()));
		List<HfEntity> data = hfService.list(wrapper);
		return R.data(data);
	}

	/**
	 * 矛盾纠纷-回访 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入hf")
	public R<IPage<HfVO>> page(HfVO hf, Query query) {
		IPage<HfVO> pages = hfService.selectHfPage(Condition.getPage(query), hf);
		return R.data(pages);
	}

	/**
	 * 矛盾纠纷-回访 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入hf")
	public R save(@Valid @RequestBody HfEntity hf) {
		String deptId = AuthUtil.getDeptId().split(",")[0];
		Dept dept = deptService.getById(Long.valueOf(deptId));
		if (null != hf.getHfzpMap() && !hf.getHfzpMap().isEmpty()) {
			mdjfRyService.updateAllHfzp(hf.getHfzpMap());
			if (dept.getAncestors().contains("220200000000")) {
				mdjfRyService.updateAllHfzpJl(hf.getHfzpMap());
			}
		}
		boolean save=false;
		Date date = new Date();
		//判断回访  回访后修改md的已回访次数
		MdjfDjEntity mdjfDjEntity = mdjfDjService.getById(hf.getJfId());
		//判断如果已回访次数小于要求回访次数  则已回访次数+1  修改回访状态
		if(StringUtils.isNotBlank(mdjfDjEntity.getMdjbV())){
			if (mdjfDjEntity.getYhfcs() < mdjfDjEntity.getXhfcs()) {
				int yhfcs = mdjfDjEntity.getYhfcs() + 1;
				if(yhfcs == mdjfDjEntity.getXhfcs()){
					mdjfDjEntity.setHfztNew("2");
				}
				if(yhfcs < mdjfDjEntity.getXhfcs()){
					mdjfDjEntity.setHfztNew("1");
				}
				mdjfDjEntity.setYhfcs(yhfcs);
				hf.setCszt("0");
				//回访次数新增后  在此判断回访状态
				//如果yhfcs == 1 说明是第一次回访  判断回访时间与回访截止时间大小 确定回访是否超时  及矛盾的超时次数
				if (yhfcs == 1 && date.compareTo(mdjfDjEntity.getHfjzsj()) > 0) {
					hf.setCszt("1");
					mdjfDjEntity.setCszt("1");
				}
				if (yhfcs == 2 && date.compareTo(mdjfDjEntity.getHfjzsj2()) > 0) {
					hf.setCszt("1");
					mdjfDjEntity.setCszt("1");
				}
				if (yhfcs == 3 && date.compareTo(mdjfDjEntity.getHfjzsj3()) > 0) {
					hf.setCszt("1");
					mdjfDjEntity.setCszt("1");
				}
				hf.setId(null);
				save = hfService.save(hf);
				if (dept.getAncestors().contains("220200000000")) {
					hfService.saveJl(hf);
				}
				mdjfDjService.saveOrUpdate(mdjfDjEntity);
				if (dept.getAncestors().contains("220200000000")) {
					mdjfDjService.saveOrUpdateJl(mdjfDjEntity);
				}
			}
		}else{
			hf.setId(null);
			hf.setCszt("0");
			mdjfDjEntity.setCscs(0);
			mdjfDjEntity.setCszt("0");
			mdjfDjEntity.setHfztNew("2");
			save = hfService.save(hf);
			if (dept.getAncestors().contains("220200000000")) {
				hfService.saveJl(hf);
			}
			mdjfDjService.saveOrUpdate(mdjfDjEntity);
			if (dept.getAncestors().contains("220200000000")) {
				mdjfDjService.saveOrUpdateJl(mdjfDjEntity);
			}
		}
		return R.status(save);
	}
	/**
	 * 矛盾纠纷-回访 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入hf")
	public R update(@Valid @RequestBody HfEntity hf) {
		return R.status(hfService.updateById(hf));
	}

	/**
	 * 矛盾纠纷-回访 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入hf")
	public R submit(@Valid @RequestBody HfEntity hf) {
		return R.status(hfService.saveOrUpdate(hf));
	}

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


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

}
