package com.yeejoin.amos.boot.module.jg.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
import com.yeejoin.amos.boot.module.common.biz.utils.CommonResponseUtil;
import com.yeejoin.amos.boot.module.jg.api.dto.JgChangeRegistrationUnitDto;
import com.yeejoin.amos.boot.module.jg.api.dto.JgInstallationNoticeDto;
import com.yeejoin.amos.boot.module.jg.api.entity.JgChangeRegistrationUnit;
import com.yeejoin.amos.boot.module.jg.api.entity.JgInstallationNotice;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;

import java.io.*;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.yeejoin.amos.boot.module.jg.biz.service.impl.JgChangeRegistrationUnitServiceImpl;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.module.jg.api.dto.JgChangeRegistrationUnitDto;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.foundation.enumeration.UserType;

/**
 * 单位变更登记
 *
 * @author system_generator
 * @date 2023-12-22
 */
@RestController
@Api(tags = "单位变更登记Api")
@RequestMapping(value = "/jg-change-registration-unit")
public class JgChangeRegistrationUnitController extends BaseController {

    @Autowired
    JgChangeRegistrationUnitServiceImpl jgChangeRegistrationUnitServiceImpl;

	/**
	 * 新增
	 *
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PostMapping(value = "/save")
	@ApiOperation(httpMethod = "POST", value = "新增单位变更", notes = "新增单位变更")
	public ResponseModel<String> save(@RequestParam String submitType, @RequestBody Map<String, Object> model) {
		JgChangeRegistrationUnitDto jgChangeRegistrationUnitDto = BeanUtil.mapToBean(((LinkedHashMap) model.get("changeRegisInfo")), JgChangeRegistrationUnitDto.class, true);
		ReginParams reginParams = getSelectedOrgInfo();

		jgChangeRegistrationUnitServiceImpl.saveNotice(submitType, jgChangeRegistrationUnitDto, reginParams);
		return ResponseHelper.buildResponse("");
	}

	/**
	 * 根据sequenceNbr更新
	 *
	 * @param model 单位变更
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PutMapping(value = "/update")
	@ApiOperation(httpMethod = "PUT", value = "根据sequenceNbr更新单位变更", notes = "根据sequenceNbr更新单位变更")
	public ResponseModel<JgChangeRegistrationUnitDto> updateBySequenceNbrJgInstallationNotice(@RequestParam String submitType, @RequestBody Map<String, Object> model, @RequestParam(value = "op", required = false) String op) {
		JgChangeRegistrationUnitDto jgChangeRegistrationUnitDto = BeanUtil.mapToBean(((LinkedHashMap) model.get("changeRegisInfo")), JgChangeRegistrationUnitDto.class, true);
		if (Objects.isNull(jgChangeRegistrationUnitDto)) {
			throw new IllegalArgumentException("参数installationInfo不能为空");
		}
		Object o = ((LinkedHashMap<?, ?>) model.get("changeRegisInfo")).get("changeCertificateList");
		jgChangeRegistrationUnitDto.setChangeCertificateList((List<Map<String, Object>>) o);
		return ResponseHelper.buildResponse(jgChangeRegistrationUnitServiceImpl.updateInstallationNotice(submitType,jgChangeRegistrationUnitDto, op));
	}

	/**
	 * 根据sequenceNbr删除
	 *
	 * @param sequenceNbr 主键
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@DeleteMapping(value = "/delete")
	@ApiOperation(httpMethod = "DELETE", value = "根据sequenceNbr删除单位变更", notes = "根据sequenceNbr删除单位变更")
	public ResponseModel<Boolean> deleteBySequenceNbr(@RequestParam(value = "sequenceNbr") Long sequenceNbr) {
		return ResponseHelper.buildResponse(jgChangeRegistrationUnitServiceImpl.removeById(sequenceNbr));
	}

	/**
	 * 根据sequenceNbr批量删除
	 *
	 * @param sequenceNbrs 主键
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@DeleteMapping(value = "/batchDelete")
	@ApiOperation(value = "根据sequenceNbr删除维保合同备案", notes = "根据sequenceNbr删除维保合同备案")
	public ResponseModel<Boolean> deleteForBatch(@RequestParam("sequenceNbrs") Long[] sequenceNbrs) {
		try {
			return ResponseHelper.buildResponse(jgChangeRegistrationUnitServiceImpl.deleteForBatch(sequenceNbrs));
		} catch (Exception e) {
			return CommonResponseUtil.failure(e.getMessage());
		}
	}

	/**
	 * 根据sequenceNbr查询
	 *
	 * @param sequenceNbr 主键
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@GetMapping(value = "/details")
	@ApiOperation(httpMethod = "GET", value = "根据sequenceNbr查询单个单位变更", notes = "根据sequenceNbr查询单个单位变更")
	public ResponseModel<Map<String,
			Map<String, Object>>> selectOne(@RequestParam("sequenceNbr") Long sequenceNbr) {
		return ResponseHelper.buildResponse(jgChangeRegistrationUnitServiceImpl.queryBySequenceNbr(sequenceNbr));
	}


	/**
	 * 列表分页查询
	 *
	 * @param current 当前页
	 * @param current 每页大小
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PostMapping(value = "/page")
	@ApiOperation(httpMethod = "GET", value = "单位变更分页查询", notes = "单位变更分页查询")
	public ResponseModel<Page<JgChangeRegistrationUnitDto>> queryForPage(
			@ApiParam(value = "当前页码", required = true) @RequestParam(value = "current", defaultValue = "1") int current,
			@ApiParam(value = "每页大小", required = true) @RequestParam(value = "size", defaultValue = "20") int size,
			@ApiParam(value = "类型：enterprise-企业端、supervision-监管端", required = true) @RequestParam(value = "type", defaultValue = "enterprise") String type,
			@RequestBody(required = false) JgChangeRegistrationUnitDto model
	) {
		Page<JgChangeRegistrationUnit> page = new Page<>(current, size);
		ReginParams reginParams = getSelectedOrgInfo();
		return ResponseHelper.buildResponse(jgChangeRegistrationUnitServiceImpl.queryForJgChangeRegistrationUnitPage(page, model, type, reginParams));
	}

	/**
	 * 撤销
	 *
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PostMapping(value = "/cancel")
	@ApiOperation(httpMethod = "POST", value = "单位变更撤销", notes = "单位变更撤销")
	public ResponseModel<JgChangeRegistrationUnitDto> cancel(@RequestBody Map<String, Object> model) {
		JgChangeRegistrationUnitDto installationInfo = BeanUtil.mapToBean(((LinkedHashMap) model.get("changeRegisInfo")), JgChangeRegistrationUnitDto.class, true);
		if (Objects.isNull(installationInfo)) {
			throw new IllegalArgumentException("参数installationInfo不能为空");
		}
		jgChangeRegistrationUnitServiceImpl.cancel(installationInfo);
		return ResponseHelper.buildResponse(installationInfo);
	}

	/**
	 * 受理
	 *
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PostMapping(value = "/accept")
	@ApiOperation(httpMethod = "POST", value = "单位变更受理", notes = "单位变更受理")
	public ResponseModel<JgChangeRegistrationUnitDto> accept(@RequestBody Map<String, Object> model, String op) {
		// TODO 受理单位变更流程
		LinkedHashMap model1 = (LinkedHashMap)model.get("changeRegisInfo");
		String opinion = (String)model.get("opinion");
		LinkedHashMap installationInfo = (LinkedHashMap)model1.get("changeRegisInfo");
		JgChangeRegistrationUnitDto jgInstallationNoticeDto = JSON.parseObject(JSON.toJSONString(installationInfo), JgChangeRegistrationUnitDto.class);
		jgInstallationNoticeDto.setProcessAdvice(opinion);
		jgChangeRegistrationUnitServiceImpl.accept(jgInstallationNoticeDto,op);
		return ResponseHelper.buildResponse(null);
	}


	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@GetMapping(value = "/export")
	@ApiOperation(httpMethod = "GET", value = "导出使用登记证", notes = "导出使用登记证")
	public void exportImageZip(HttpServletResponse response, @RequestParam("sequenceNbr") String sequenceNbr)  throws IOException {
		jgChangeRegistrationUnitServiceImpl.exportUseRegistrationCertificate(sequenceNbr, response);
	}

}
