package org.springblade.command.feign;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.command.pojo.entity.*;
import org.springblade.command.pojo.vo.FzyjFkVO;
import org.springblade.command.pojo.vo.FzyjVO;
import org.springblade.command.pojo.vo.FzyjYjVO;
import org.springblade.command.pojo.vo.GsdwVO;
import org.springblade.command.service.*;
import org.springblade.command.utils.FzyjUtil;
import org.springblade.common.config.YamlParamsConfig;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.system.feign.ISysClient;
import org.springblade.system.feign.IUserClient;
import org.springblade.system.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
public class FzyjClientImpl implements FzyjClient {

	private IFzyjService fzyjService;

	private IFzyjFkService fzyjFkService;

	private IFzyjShrService fzyjShrService;

	private IFzyjFjService fzyjFjService;

	private IFzyjYjService fzyjYjService;

	private IFzyjUserService fzyjUserService;

	private IUserClient userClient;

	private ISysClient sysClient;

	@Autowired
	private YamlParamsConfig yamlParamsConfig;

	@Override
	@PostMapping(API_PREFIX + "/pageByUser")
	public R pageByDept(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		params.put("userId", AuthUtil.getUserId());
		IPage<FzyjVO> pages = fzyjService.pageByDept(Condition.getPage(query), params);
		return R.data(pages);
	}

	@Override
	@PostMapping(API_PREFIX + "/detail")
	public R detail(@RequestParam Long syncId) {
		FzyjVO fzyjVO = fzyjService.detail(syncId);
		List<GsdwVO> gsdwList = fzyjService.getGsdwBySyncId(syncId);
		fzyjVO.setGsdwList(gsdwList);
		List<FzyjShrEntity> shrList = fzyjShrService.getShrList(syncId);
		fzyjVO.setShrList(shrList);
		FzyjFjEntity fj = fzyjFjService.getFjBySyncId(syncId);
		fzyjVO.setFj(fj);
		return R.data(fzyjVO);
	}

	@Override
	@PostMapping(API_PREFIX + "/getFkBySyncId")
	public R<FzyjFkEntity> getFkBySyncId(@RequestParam Long SyncId) {
		return R.data(fzyjFkService.getFkBySyncId(SyncId));
	}

	@Override
	@PostMapping(API_PREFIX + "/countByUserId")
	public int countByUserId() {
		return fzyjService.countByUserId(AuthUtil.getUserId());
	}

	@Override
	@PostMapping(API_PREFIX + "/countByDept")
	public int countByDept() {
		return fzyjService.countByDept(Long.valueOf(AuthUtil.getDeptId()));
	}

	@Override
	@PostMapping(API_PREFIX + "/countYqsByUserId")
	public int countYqsByUserId() {
		return fzyjService.countYqsByUserId(AuthUtil.getUserId());
	}

	@Override
	@PostMapping(API_PREFIX + "/countYqsByDept")
	public int countYqsByDept() {
		return fzyjService.countYqsByDept(Long.valueOf(AuthUtil.getDeptId()));
	}

	@Override
	@PostMapping(API_PREFIX + "/countYfkByUserId")
	public int countYfkByUserId() {
		return fzyjService.countYfkByUserId(AuthUtil.getUserId());
	}

	@Override
	@PostMapping(API_PREFIX + "/countYfkByDept")
	public int countYfkByDept() {
		return fzyjService.countYfkByDept(Long.valueOf(AuthUtil.getDeptId()));
	}

	@Override
	@PostMapping(API_PREFIX + "/yj")
	public R yj(@RequestBody FzyjYjEntity fzyjYjEntity) {
		return R.status(fzyjYjService.save(fzyjYjEntity));
	}

	@Override
	@PostMapping(API_PREFIX + "/zp")
	public R zp(@RequestBody FzyjUserEntity fzyjUserEntity) {
		return R.status(fzyjUserService.save(fzyjUserEntity));
	}

	@Override
	@PostMapping(API_PREFIX + "/updateFkBySyncId")
	public R updateFkBySyncId(@RequestBody FzyjFkEntity fzyjFkEntity) {
		return R.status(fzyjFkService.updateFkBySyncId(fzyjFkEntity));
	}

	@Override
	@PostMapping(API_PREFIX + "/saveFk")
	public R saveFk(@RequestBody FzyjFkEntity fzyjFkEntity) {
		return R.status(fzyjFkService.save(fzyjFkEntity));
	}

	@Override
	@PostMapping(API_PREFIX + "/saveOrUpdateShr")
	public void saveOrUpdateShr(List<FzyjShrEntity> shrList) {
		for (FzyjShrEntity shr : shrList) {
			fzyjShrService.saveOrUpdate(shr);
		}
	}

	@Override
	@PostMapping(API_PREFIX + "/getUnpushedFzyj")
	public R<List<FzyjVO>> getUnpushedFzyj() {
		return R.data(fzyjService.getUnpushedFzyj());
	}

	@Override
	@PostMapping(API_PREFIX + "/getUnpushedZp")
	public R<List<FzyjVO>> getUnpushedZp() {
		return R.data(fzyjService.getUnpushedZp());
	}

	@Override
	@PostMapping(API_PREFIX + "/updatePushStatus")
	public boolean updatePushStatus(@RequestParam Long syncId) {
		return fzyjService.updatePushStatus(syncId);
	}

	@Override
	@PostMapping(API_PREFIX + "/updateZpPushStatus")
	public boolean updateZpPushStatus(@RequestParam Long syncId) {
		return fzyjUserService.updateZpPushStatus(syncId);
	}

	@Override
	@PostMapping(API_PREFIX + "/saveFj")
	public boolean saveFj(@RequestBody FzyjFjEntity fzyjFjEntity) {
		return fzyjFjService.save(fzyjFjEntity);
	}

	@Override
	@PostMapping(API_PREFIX + "/feedbackUrgentRisks")
	public void feedbackUrgentRisks(@RequestBody FzyjFkVO fzyjFkVO) {
		if (fzyjFkVO.getFkrType() == 1) {
			// 管理员反馈
			User user = userClient.userInfoById(AuthUtil.getUserId()).getData();
			String deptName = sysClient.getDeptName(Long.valueOf(user.getDeptId().split(",")[0])).getData();
			fzyjFkVO.setFeedbackId(user.getJh()); // 反馈人工号
			fzyjFkVO.setFeedbackIdcard(user.getSfzh()); // 反馈人身份证号
			fzyjFkVO.setFeedbackName(user.getRealName()); // 反馈人姓名
			fzyjFkVO.setFeedbackUnit(user.getDeptId()); // 6位数字的单位编码
			fzyjFkVO.setSeeName(user.getRealName()); // 见面民警
			fzyjFkVO.setSeeUnit(deptName); // 见面单位
		} else {
			// 民警反馈
			User mj = userClient.userInfoById(AuthUtil.getUserId()).getData();
			String mjDeptName = sysClient.getDeptName(Long.valueOf(mj.getDeptId().split(",")[0])).getData();

			FzyjFkEntity fzyjFkEntity = fzyjFkService.getFkBySyncId(fzyjFkVO.getSyncId());
			User gly = userClient.userInfoById(fzyjFkEntity.getCreateUser()).getData();

			fzyjFkVO.setFeedbackId(gly.getJh()); // 反馈人工号
			fzyjFkVO.setFeedbackIdcard(gly.getSfzh()); // 反馈人身份证号
			fzyjFkVO.setFeedbackName(gly.getRealName()); // 反馈人姓名
			fzyjFkVO.setFeedbackUnit(gly.getDeptId()); // 6位数字的单位编码
			fzyjFkVO.setSeeName(mj.getRealName()); // 见面民警
			fzyjFkVO.setSeeUnit(mjDeptName); // 见面单位
		}

		// 调用反馈回写接口
		FzyjUtil.feedbackUrgentRisks(yamlParamsConfig.getFzyjAccount(), yamlParamsConfig.getFzyjApiKey(), yamlParamsConfig.getFzyjFeedbackUrgentRisksUrl(), fzyjFkVO);
	}

	@Override
	@PostMapping(API_PREFIX + "/fileupload")
	public String fileupload(@RequestBody FzyjFjEntity fzyjFjEntity) {
		return FzyjUtil.fileupload(yamlParamsConfig.getFzyjFileuploadUrl(), yamlParamsConfig.getLocalFilePath(), fzyjFjEntity);
	}

	@Override
	@PostMapping(API_PREFIX + "/dealUrgentRisks")
	public void dealUrgentRisks(@RequestBody FzyjYjVO fzyjYjVO) {
		FzyjFkVO fzyjFkVO = new FzyjFkVO();
		User user = userClient.userInfoById(AuthUtil.getUserId()).getData();
		String deptName = sysClient.getDeptName(Long.valueOf(user.getDeptId().split(",")[0])).getData();
		fzyjFkVO.setFeedbackId(user.getJh()); // 反馈人工号
		fzyjFkVO.setFeedbackUnit(user.getDeptId()); // 6位数字的单位编码
		fzyjFkVO.setSeeUnit(deptName); // 见面单位
		FzyjUtil.dealUrgentRisks(yamlParamsConfig.getFzyjAccount(), yamlParamsConfig.getFzyjApiKey(), yamlParamsConfig.getFzyjDealUrgentRisksUrl(), fzyjYjVO, fzyjFkVO);
	}
}
